我在我的cuda程序中添加了一些printf语句
__device__ __global__ void Kernel(float *, float * ,int );
void DeviceFunc(float *temp_h , int numvar , float *temp1_h)
{ .....
//Kernel call
printf("calling kernel\n");
Kernel<<<dimGrid , dimBlock>>>(a_d , b_d , numvar);
printf("kernel called\n");
....
}
int main(int argc , char **argv)
{ ....
printf("beforeDeviceFunc\n\n");
DeviceFunc(a_h , numvar , b_h); //Showing the data
printf("after DeviceFunc\n\n");
....
}
Run Code Online (Sandbox Code Playgroud)
同样在Kernel.cu中,我写道:
#include<cuda.h>
#include <stdio.h>
__device__ __global__ void Kernel(float *a_d , float *b_d ,int size)
{
int idx = threadIdx.x ;
int idy = threadIdx.y ;
//Allocating memory in the share memory of the device
__shared__ float temp[16][16];
//Copying the data to the shared memory
temp[idy][idx] = a_d[(idy * (size+1)) + idx] ;
printf("idx=%d, idy=%d, size=%d", idx, idy, size);
....
}
Run Code Online (Sandbox Code Playgroud)
然后我printf()
像这样编译:
nvcc -c -arch sm_20 main.cu
nvcc -c -arch sm_20 Kernel.cu
nvcc -arch sm_20 main.o Kernel.o -o main
Run Code Online (Sandbox Code Playgroud)
现在,当我运行程序时,我看到:
beforeDeviceFunc
calling kernel
kernel called
after DeviceFunc
Run Code Online (Sandbox Code Playgroud)
所以内核中的printf没有打印出来.我该如何解决这个问题?
ter*_*era 10
printf()
只有在内核成功完成时才会显示输出,因此请检查所有CUDA函数调用的返回代码,并确保没有报告错误.
此外,printf()
输出仅显示在程序中的某些点上.编程指南的附录B.20.2将这些列为
<<<>>>
或cuLaunchKernel()
启动(在启动时启动,如果CUDA_LAUNCH_BLOCKING环境变量设置为1,则在启动结束时),cudaDeviceSynchronize()
,cuCtxSynchronize()
,cudaStreamSynchronize()
,cuStreamSynchronize()
,cudaEventSynchronize()
,或者cuEventSynchronize()
,cudaMemcpy*()
或cuMemcpy*()
,cuModuleLoad()
或cuModuleUnload()
,cudaDeviceReset()
或cuCtxDestroy()
.cudaStreamAddCallback()
或添加的流回调之前cuStreamAddCallback()
.要轻松检查这是您的问题,请在内核调用后输入以下代码:
{
cudaError_t cudaerr = cudaDeviceSynchronize();
if (cudaerr != cudaSuccess)
printf("kernel launch failed with error \"%s\".\n",
cudaGetErrorString(cudaerr));
}
Run Code Online (Sandbox Code Playgroud)
然后,您应该看到内核的输出或错误消息.
归档时间: |
|
查看次数: |
4522 次 |
最近记录: |