CUDA 初学者错误

发布于 2024-10-09 05:35:26 字数 1051 浏览 0 评论 0原文

我正在用 CUDA 编写一个小程序,但出现以下错误:

contraste.cu(167): error: calling a host function from a __device__/__global__ function is not allowed

我不明白为什么。你能帮助我并告诉我我的错误吗?看来我的程序是正确的。这是导致问题的一堆代码:

 __global__ void kernel_contraste(float power, 
     unsigned char tab_in[], 
     unsigned char tab_out[], 
     int nbl, int nbc) {

 int x = threadIdx.x;
 printf("I am the thread %d\n", x);

}

我的主程序的一部分:

 unsigned char *dimg, *dimg_res;
  .....

   cudaMalloc((void **)dimg, h * w * sizeof(char));
  cudaMemcpy(dimg, r.data, h*w*sizeof(char), cudaMemcpyHostToDevice);  

  cudaMalloc((void **)dimg_res, h*w*sizeof(char));
  dim3  nbThreadparBloc(256);
  dim3  numblocs(1);

  kernel_contraste<<<numblocs, nbThreadparBloc >>>(puissance, dimg, dimg_res, h, w);
  cudaThreadSynchronize(); 
  .....


   cudaFree(dimg);
  cudaFree(dimg_res);

第 167 行是我在函数 kernel_contraste 中调用 printf 的行。

作为信息,该程序将图像作为输入(太阳光栅文件)和功率,然后计算该图像的对比度。

谢谢 !!

I am writing a small program in CUDA and i have the following errors :

contraste.cu(167): error: calling a host function from a __device__/__global__ function is not allowed

I don't understand why. Can you please help me and show me my errors. It seems that my program is correct. Here is a the bunch of code causing the problems :

 __global__ void kernel_contraste(float power, 
     unsigned char tab_in[], 
     unsigned char tab_out[], 
     int nbl, int nbc) {

 int x = threadIdx.x;
 printf("I am the thread %d\n", x);

}

Part of my main program :

 unsigned char *dimg, *dimg_res;
  .....

   cudaMalloc((void **)dimg, h * w * sizeof(char));
  cudaMemcpy(dimg, r.data, h*w*sizeof(char), cudaMemcpyHostToDevice);  

  cudaMalloc((void **)dimg_res, h*w*sizeof(char));
  dim3  nbThreadparBloc(256);
  dim3  numblocs(1);

  kernel_contraste<<<numblocs, nbThreadparBloc >>>(puissance, dimg, dimg_res, h, w);
  cudaThreadSynchronize(); 
  .....


   cudaFree(dimg);
  cudaFree(dimg_res);

The line 167 is the line where i call the printf in function kernel_contraste.

For information, this program takes an image as an input( a sun Rasterfile ) and a power then it calculates the contraste of that image.

Thanks !!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

风铃鹿 2024-10-16 05:35:26

请查看此处有关 cuprintf() 的教程。我想这就是你想要的。

look Here for a tutorial on cuprintf(). I think its what you want.

终止放荡 2024-10-16 05:35:26

正如错误消息所示,您无法从 GPU 上运行的内核函数调用主机函数(在本例中为 printf)。

As the error message says you cannot call a host function (printf in this case) from a kernel function that runs on the GPU.

夏至、离别 2024-10-16 05:35:26

您正在尝试从 GPU 设备调用 printf。你必须有费米卡才能做到这一点......

you are trying to call printf from gpu device. You have to have Fermi card to do that...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文