从cuda内核打印

我正在编写一个cuda程序,并尝试使用printf函数在cuda内核中打印一些内容。 但是当我编译程序时,我收到了一个错误

error : calling a host function("printf") from a __device__/__global__ function("agent_movement_top") is not allowed error MSB3721: The command ""C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v4.2\bin\nvcc.exe" -gencode=arch=compute_10,code=\"sm_10,compute_10\" --use-local-env --cl-version 2008 -ccbin "c:\Program Files (x86)\Microsoft Visual Studio 9.0\VC\bin" -I"C:\ProgramData\NVIDIA Corporation\NVIDIA GPU Computing SDK 4.2\C\common\inc" -I"C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v4.2\include" -G --keep-dir "Debug" -maxrregcount=0 --machine 32 --compile -g -Xcompiler "/EHsc /nologo /Od /Zi /MDd " -o "Debug\test.cu.obj" "C:\Users\umdutta\Desktop\SANKHA_ALL_MATERIALS\PROGRAMMING_FOLDER\ABM_MODELLING_2D_3D\TRY_NUM_2\test_proj_test\test_proj\test_proj\test.cu"" exited with code 2. 

我正在使用具有大于2.0的计算能力的卡GTX 560 ti,当我从cuda内核中搜索了一些关于打印的内容时,我还看到我需要将编译器从sm_10更改为sm_2.0以充分利用卡片。 还有人建议cuPrintf服务于此目的。 我有点困惑,我应该做什么以及什么应该是在我的控制台屏幕上获取打印输出的最简单,最快捷的方法。 如果我需要将nvcc编译器从1.0更改为2.0,那么我该怎么办? 还有一件事我想提一下,我正在使用Windows 7.0并在visual studio 2010中编程。感谢您的帮助。

要在Compute Capability> = 2.0的设备上使用plain printf() ,重要的是编译CC至少CC 2.0并禁用默认值,其中包括CC 1.0的构建。

右键单击项目中的.cu文件,选择“ Properties ,然后选择“ Configuration Properties CUDA C/C++ | Device 。 单击Code Generation行,单击三角形,然后选择Edit 。 在“代码生成”对话框中,取消选中“ Inherit from parent or project defaults compute_20,sm_20在顶部窗口中键入compute_20,sm_20 ,然后单击“确定”。

解决此问题的一种方法是使用能够从内核打印的cuPrintf函数。 从文件夹中复制文件cuPrintf.cucuPrintf.cuh

 C:\ProgramData\NVIDIA Corporation\NVIDIA GPU Computing SDK 4.2\C\src\simplePrintf 

到项目文件夹。 然后将头文件cuPrintf.cuh添加到项目中并添加

 #include "cuPrintf.cu" 

你的代码。 那么你的代码应该用下面提到的格式编写:

 #include “cuPrintf.cu” __global__ void testKernel(int val) { cuPrintf(“Value is: %d\n”, val); } int main() { cudaPrintfInit(); testKernel<<< 2, 3 >>>(10); cudaPrintfDisplay(stdout, true); cudaPrintfEnd(); return 0; } 

通过遵循上述过程,可以从设备function在控制台窗口上获得打印。 虽然我以上面提到的方式解决了我的问题,但我仍然没有从设备function中使用printf的解决方案。 如果确实并且绝对有必要将我的nvcc编译器从sm_10升级到sm_21以启用printffunction,那么如果有人可以向我展示光线,那将非常有用。 谢谢你的合作

您可以编写此代码以在CUDA内核中打印您想要的任何内容:

 # if __CUDA_ARCH__>=200 printf("%d \n", tid); #endif 

并包括