从cuda内核打印

时间:2012-12-31 22:45:38

标签: c visual-studio-2010 cuda gpgpu

我正在编写一个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中编程。感谢您的帮助。

4 个答案:

答案 0 :(得分:8)

要在Compute Capability> = 2.0的设备上启用普通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,然后单击“确定”。

答案 1 :(得分:6)

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

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

#endif  

并包含< stdio.h>

答案 2 :(得分:5)

解决此问题的一种方法是使用能够从内核打印的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;
}

按照上述步骤,可以从设备功能在控制台窗口上打印。 虽然我以上述方式解决了我的问题,但我仍然没有使用设备函数中printf的解决方案。如果确实并且绝对有必要将我的nvcc编译器从sm_10升级到sm_21以启用printf功能,那么如果有人可以向我展示它,那将非常有用。感谢您的合作

答案 3 :(得分:0)

我在带有GeForce GTX 1060的Visual Studio 2015上拥有cuda v10.0.130,我要做的就是添加以下include语句:

#include <helper_cuda.h>

然后,我可以使用printf语句而没有任何问题。