printf with -arch = sm_20不会在内核文件中显示任何内容

mah*_*ood 1 printf cuda

我在我的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)

然后,您应该看到内核的输出或错误消息.

  • 然后检查您的安装并确保安装了最新的(或最新的)驱动程序. (2认同)
  • 嗨,我也遇到了同样的问题,并尝试了您的答案。我没有收到任何错误,但是没有打印内核内部的printf行。 (2认同)