C语言 从 cuda 内核打印
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14106615/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
printing from cuda kernels
提问by duttasankha
I am writing a cuda program and trying to print something inside the cuda kernels using the printf function. But when I am compiling the program then I am getting an error
我正在编写一个 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.
I am using the card GTX 560 ti having a compute capability greater than 2.0 and when I have searched a bit about the printing from cuda kernels I also saw that I need to change the compiler from sm_10 to sm_2.0 to take the full advantage of the card. Also some suggested for cuPrintf to serve the purpose. I am bit confused what should I do and what should be the simplest and quickest way to get the printouts on my console screen. If I need to change the nvcc compiler from 1.0 to 2.0 then what should I do? One more thing I would like to mention that I am using windows 7.0 and programming in visual studio 2010. Thanks for all your help.
我正在使用计算能力大于 2.0 的 GTX 560 ti 卡,当我搜索了一些关于 cuda 内核打印的信息时,我还发现我需要将编译器从 sm_10 更改为 sm_2.0 以充分利用卡片。还有一些人建议 cuPrintf 用于此目的。我有点困惑我应该做什么以及在我的控制台屏幕上获取打印输出的最简单和最快捷的方法是什么。如果我需要将 nvcc 编译器从 1.0 更改为 2.0,我该怎么办?还有一件事我想提一下,我正在使用 windows 7.0 并在 Visual Studio 2010 中编程。感谢您的帮助。
采纳答案by Roger Dahl
To enable use of plain printf()on devices of Compute Capability >= 2.0, it's important to compile for CC of at least CC 2.0 and disable the default, which includes a build for CC 1.0.
要printf()在 Compute Capability >= 2.0 的设备上启用plain ,重要的是编译至少为 CC 2.0 的 CC 并禁用默认设置,其中包括 CC 1.0 的构建。
Right-click the .cufile in your project, select Properties, select Configuration Properties| CUDA C/C++| Device. Click on the Code Generationline, click the triangle, select Edit. In the Code Generation dialog box, uncheck Inherit from parent or project defaults, type compute_20,sm_20in the top window, click OK.
右键单击.cu项目中的文件,选择Properties,选择Configuration Properties| CUDA C/C++| Device. 单击Code Generation直线,单击三角形,选择Edit。在代码生成对话框中,取消选中Inherit from parent or project defaults,compute_20,sm_20在顶部窗口中键入,单击确定。
回答by Heba
you can write this code to print whatever you want from inside the CUDA Kernel:
您可以编写此代码以从 CUDA 内核中打印您想要的任何内容:
# if __CUDA_ARCH__>=200
printf("%d \n", tid);
#endif
and include < stdio.h >
并包含 <stdio.h>
回答by duttasankha
One way of solving this problem is by using cuPrintf function which is capable of printing from the kernels. Copy the files cuPrintf.cuand cuPrintf.cuhfrom the folder
解决这个问题的一种方法是使用能够从内核打印的 cuPrintf 函数。复制文件cuPrintf.cu并cuPrintf.cuh从文件夹中
C:\ProgramData\NVIDIA Corporation\NVIDIA GPU Computing SDK 4.2\C\src\simplePrintf
to the project folder. Then add the header file cuPrintf.cuhto your project and add
到项目文件夹。然后将头文件添加cuPrintf.cuh到您的项目中并添加
#include "cuPrintf.cu"
to your code. Then your code should be written in a format mentioned below :
到您的代码。那么你的代码应该以下面提到的格式编写:
#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;
}
By following the above procedure one can get a print on the console window from the device function.
Though I solved my issues in the above mentioned way I still don't have the solution of using printffrom the device function. If it is true and absolutely necessary to upgrade my nvcc compiler from sm_10 to sm_21 to enable the printffeature then it would be very much helpful if someone could show me the light. Thanks for all your cooperation
通过执行上述过程,可以从设备功能在控制台窗口上打印。虽然我以上述方式解决了我的问题,但我仍然没有printf从设备功能使用的解决方案。如果确实并且绝对有必要将我的 nvcc 编译器从 sm_10 升级到 sm_21 以启用该printf功能,那么如果有人可以向我展示一下,那将非常有帮助。谢谢大家的合作

