gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 06:04:22 38 4
gpt4 key购买 nike

在下面的代码片段中,我尝试在代码中使用 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";

最佳答案

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

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

关于c - 让 printf 语句在 openCL 内核代码中工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13158955/

38 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com