使printf语句在openCL内核代码中工作

Chu*_*cky 0 c string kernel escaping opencl

在下面的代码片段中,我试图printf()在代码中尝试打印出input[i]计算结果,以便我可以看到它正常工作.

但这并不像我希望的那样有效,因为引号printf()与内核的字符串格式相混淆,导致整个程序无法编译.我已经尝试使用转义字符\",它允许我输入内核的字符串但是在编译时给出了预期的表达式和缺少的字符错误.

有谁知道如何解决这个问题?这是检查内核代码结果的最佳方法吗?

    const char *KernelSource =         "\n"
"__kernel void relax(                    \n"
"   __global double* input,                \n"
"   __global double* output,               \n"
"   __global int N)             \n"
"{                                        \n"
"   int i = get_global_id(0);             \n"
"   if(i > 0 && i < N-1){                         \n"
"       input[i] = 0.25*input[i-1]+0.5*input[i]+0.25*input[i+1];  \n"
"       printf("input[%d] %f \n", i, input[i] )\n"
"   }                                        \n"
"}                                        \n"
"\n";
Run Code Online (Sandbox Code Playgroud)

nos*_*nos 5

你需要转义引号,你需要转义\格式字符串中的\n.

"       printf(\"input[%d] %f \\n\", i, input[i] )\n"
Run Code Online (Sandbox Code Playgroud)

编辑; 你还需要在printf格式内的\n中转义\