簡體   English   中英

在 OpenCL 內核中使用 printf 的問題

[英]Problem with using printf in OpenCL kernel

我在AMD上使用OpenCL 2.0 代碼非常簡單。 如果我使用 1 printf ,效果很好。 但是如果我添加第二個printf ,那么就會有歪曲的數據。 我在主機 C++ 中的代碼:

cl_int errcode;
// Get available platforms
vector<Platform> platforms;
Platform::get(&platforms);
// Select the default platform and create a context using this platform and the GPU
cl_context_properties cps[3] = {
    CL_CONTEXT_PLATFORM,
    (cl_context_properties)(platforms[0])(),
    0
};
Context context(CL_DEVICE_TYPE_GPU, cps);


vector<Device> devices = context.getInfo<CL_CONTEXT_DEVICES>();

CommandQueue queue = CommandQueue(context, devices[0]);

// Read source file
string name;
name += "CalcN.cl";
std::ifstream sourceFile(name);
std::string sourceCode(
    std::istreambuf_iterator<char>(sourceFile),
    (std::istreambuf_iterator<char>()));
Program::Sources source(1, std::make_pair(sourceCode.c_str(), sourceCode.length() + 1));


Program program = Program(context, source);

errcode = program.build(devices);
if (errcode != CL_SUCCESS)
{
    cout << "There were error during build kernel code. Please, check program code. Errcode = " << errcode << "\n";
    cout << "BUILD LOG: " + program.getBuildInfo<CL_PROGRAM_BUILD_LOG>(devices[0]) + "\n";
    getchar();
}


// Make kernel
Kernel kernel(program, "Optimization");

NDRange global(1);
queue.enqueueNDRangeKernel(kernel, 0, global);

我在內核中的代碼:

__kernel void Optimization() 
{
   for(int i = 0;i<100;i++)
   {
       printf("%d",i);
       printf("%d",i);
   }
}

帶有一個 printf 的控制台

在此處輸入圖片說明

並使用兩個 printf 進行控制台:

在此處輸入圖片說明

我已經不止一次問過這個問題,但沒有人知道。

當您的代碼中沒有 \\n 時,您的輸出會在每個 printf 之后打印新行。 我的系統不會那樣做; 它會在一行中打印 112233...。 你可以試試 printf("%i\\n",i);。

問題出在顯卡驅動程序上。 今天他們發布了一個更新來修復這個錯誤。

只需使用setbuf(stdout,NULL); . 寫在聲明下面。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM