[英]printing from cuda kernels
我正在编写一个 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 的设备上启用普通printf()
,重要的是编译 CC 至少为 CC 2.0 并禁用默认值,其中包括 CC 1.0 的构建。
右键单击项目中的.cu
文件,选择Properties
,选择Configuration Properties
| CUDA C/C++
| Device
。 单击Code Generation
行,单击三角形,选择Edit
。 在 Code Generation 对话框中,取消选中Inherit from parent or project defaults
,在顶部窗口中键入compute_20,sm_20
,单击 OK。
您可以编写此代码以从 CUDA 内核中打印您想要的任何内容:
# if __CUDA_ARCH__>=200
printf("%d \n", tid);
#endif
并包含 <stdio.h>
解决此问题的一种方法是使用能够从内核打印的 cuPrintf 函数。 从文件夹中复制文件cuPrintf.cu
和cuPrintf.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
功能,那么如果有人可以向我展示这将非常有帮助。 感谢大家的合作
我在带有GeForce GTX 1060的Visual Studio 2015上有cuda v10.0.130,而我所要做的就是添加以下include语句:
#include <helper_cuda.h>
然后我能够毫无问题地使用printf语句。
我正在使用 GTX 1650、GTX1050 和 c++11。 对于最近的用户,这是我的建议:
在主机功能中:
#include<iostream>
using namespace std;
cout<< .....(anything you want) << endl;
在内核中:
if(threadIdx.x==0){
printf("ss=%4.2f \n", ss);
}
请注意,这个“如果”非常重要,我注意到没有人提到这一点。 因为您可能会使用很多线程,并且您绝对不想从每个线程中打印太多。 4.2f 表示 4 点,2 表示小数。 这可以防止打印过多的 00000。也不要忘记 \n 跳线。
您也可以考虑这样打印共享内存值:
if(threadIdx.x==0){
for(int i=0;i<64;i++){
for(int j=0;j<8; j++){
printf("%4.2f ", ashare[i*8+j]);
}
printf("\n");
}
printf("\n");
}
这可以漂亮地打印共享内存。 注意也需要限制只在threadIdx.x==0
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.