Jose Vega Jose Vega - 4 years ago 246
C Question

printf inside CUDA __global__ function

I am currently writing a matrix multiplication on a GPU and would like to debug my code, but since I can not use printf inside a device function, is there something else I can do to see what is going on inside that function. This my current function:

__global__ void MatrixMulKernel(Matrix Ad, Matrix Bd, Matrix Xd){

int tx = threadIdx.x;
int ty = threadIdx.y;

int bx = blockIdx.x;
int by = blockIdx.y;

float sum = 0;

for( int k = 0; k < Ad.width ; ++k){
float Melement = Ad.elements[ty * Ad.width + k];
float Nelement = Bd.elements[k * Bd.width + tx];
sum += Melement * Nelement;

Xd.elements[ty * Xd.width + tx] = sum;

I would love to know if Ad and Bd is what I think it is, and see if that function is actually being called.

Tom Tom
Answer Source


To avoid misleading people, as M. Tibbits points out printf is available in any GPU of compute capability 2.0 and higher.


You have choices:

  • Use a GPU debugger, i.e. cuda-gdb on Linux or Nexus on Windows
  • Use cuprintf, which is available for registered developers (sign up here)
  • Manually copy the data that you want to see, then dump that buffer on the host after your kernel has completed (remember to synchronise)

Regarding your code snippet:

  • Consider passing the Matrix structs in via pointer (i.e. cudaMemcpy them to the device, then pass in the device pointer), right now you will have no problem but if the function signature gets very large then you may hit the 256 byte limit
  • You have inefficient reads from Ad, you will have a 32-byte transaction to the memory for each read into Melement - consider using shared memory as a staging area (c.f. the transposeNew sample in the SDK)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download