简体   繁体   English

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

[英]Getting a printf statement to work in openCL Kernel code

In the following snippet I'm trying to use printf() in the code to try and print out the result of the input[i] calculation so I can see that it's working correctly.在下面的代码片段中,我尝试在代码中使用printf()来尝试打印input[i]计算的结果,以便我可以看到它正常工作。

This isn't working as I had hoped however because the quotation marks in printf() mess with the string format for the kernel such that the entire program won't compile.然而,这并没有像我希望的那样工作,因为printf()的引号与内核的字符串格式混淆,因此整个程序将无法编译。 I've tried using the escape character \\" which does allow me to type the string for the kernel but when compiled gives me expected expression and missing character errors.我试过使用转义字符\\" ,它允许我为内核键入字符串,但编译时给了我预期的表达式和丢失字符错误。

Does anyone know how to solve this?有谁知道如何解决这个问题? And is this the best way to check kernel code results?这是检查内核代码结果的最佳方法吗?

    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";

您需要对引号进行转义,并且需要对格式字符串中的\\n转义\\

"       printf(\"input[%d] %f \\n\", i, input[i] )\n"

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

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