3

What do we have to do to use cuPrintf()? (device compute capability 1.2, Ubuntu 12) I couldn't find "cuPrintf.cu" and "cudaPrintf.cuh", so i downloaded their code and include them:

#include "cuPrintf.cuh"
#include "cuPrintf.cu"

By the way this is the rest of the code:

__global__ void hello_kernel (float f) {
printf ("Thread number %d. f = %d\n", threadIdx.x, f);
}

    int main () {
    dim3 gridSize = dim3 (1);
    dim3 blockSize = dim3 (16);
    cudaPrintfInit ();
    hello_kernel <<< gridSize, blockSize >>> (1.2345f);
    cudaPrintfDisplay (stdout, true);
    cudaPrintfEnd ();
    return (0);
}

But nvcc still gives a mistake:

max@max-Lenovo-G560:~/CUDA/matrixMult$ nvcc printfTest.cu -o printfTest

printfTest.cu(5): error: calling a __host__ function("printf") from a __global__
function("hello_kernel") is not allowed

Thanks!

Max
  • 441
  • 2
  • 7
  • 14

2 Answers2

3

In your kernel instead of this:

printf ("Thread number %d. f = %d\n", threadIdx.x, f);

you should do this:

cuPrintf ("Thread number %d. f = %d\n", threadIdx.x, f);

Other than that, I believe your code is correct (it works for me).

This SO question/answer gives more tips about using cuPrintf properly.

Community
  • 1
  • 1
Robert Crovella
  • 143,785
  • 11
  • 213
  • 257
  • Oops! First I tried to use printf(), but then found out that compute capability is lower than 2.0. Finally I forgot to change printf() to cuPrintf(). Sorry for stupid question :( – Max Nov 28 '12 at 16:35
-1

Include <stdio.h> and compile with -arch=sm_20.

Details:

code:

#include <stdio.h>
__global__ void hello_kernel (float f) {
    printf ("Thread number %d. f = %d\n", threadIdx.x, f);
}

int main(){
    return 0;
}

compilations:

 nvcc -arch=sm_20 -o printfTest printfTest.cu
lashgar
  • 5,184
  • 3
  • 37
  • 45