简体   繁体   English

在 OpenCL 内核中使用 printf 的问题

[英]Problem with using printf in OpenCL kernel

I use OpenCL 2.0 on AMD .我在AMD上使用OpenCL 2.0 The code is pretty simple.代码非常简单。 If I use 1 printf , the work is good.如果我使用 1 printf ,效果很好。 But if i add a second printf , then there will be crooked data.但是如果我添加第二个printf ,那么就会有歪曲的数据。 My Code in host C++:我在主机 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);

My Code in kernel:我在内核中的代码:

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

Console with One printf带有一个 printf 的控制台

在此处输入图片说明

And console with Two printf:并使用两个 printf 进行控制台:

在此处输入图片说明

I've already asked about this problem more than once, but no one knows.我已经不止一次问过这个问题,但没有人知道。

Your output prints new lines after each printf while there isn't a \\n in your code.当您的代码中没有 \\n 时,您的输出会在每个 printf 之后打印新行。 My system wouldn't do that;我的系统不会那样做; it would print 112233... in one line.它会在一行中打印 112233...。 You could try printf("%i\\n",i);.你可以试试 printf("%i\\n",i);。

The problem was with the video card drivers.问题出在显卡驱动程序上。 Today they released an update that fixes this bug.今天他们发布了一个更新来修复这个错误。

Just use setbuf(stdout,NULL);只需使用setbuf(stdout,NULL); . . Write it under the declaration.写在声明下面。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM