CUDA 初学者错误
我正在用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
请查看此处有关
cuprintf()
的教程。我想这就是你想要的。look Here for a tutorial on
cuprintf()
. I think its what you want.正如错误消息所示,您无法从 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.
您正在尝试从 GPU 设备调用
printf
。你必须有费米卡才能做到这一点......you are trying to call
printf
from gpu device. You have to have Fermi card to do that...