Use print in CUDA kernel

Hi *,
i use GeForce GT520 con compute capability 2.1
I would like to use a printf() within cuda kernel and i wrote this code:

#include<stdio.h>
#include<stdlib.h>

global void stampa(float f)
{
printf(“Hello thread %d, f=%f\n”, threadIdx.x, f);
}

void main(){
stampa<<<1,5>>>(5.5);
cudaDeviceSynchronize();
}

When i compile with nvcc -arch=sm_21 --compile stampa.cu -o stampa.o …
i obtain the error: error: calling a host function(“mexPrintf”) from a global function(“stampa”) is not allowed

Why?
Thanks,
Davide

you can’t do that because you can only call cuda functions from the global and device functions.

There is a method for use printf within a cuda kernel?

no not really. I think your question comes from a lack of understanding of the underlying hardware architecture.

you need to transfer your device memory to your host before you can printf it from there.

if you’re looking for a “hello world” type thing look for the “adding with cuda” example.

The problem is connected to this:

(“mexPrintf”)

Do you have matlab installed? Are you compiling this from within matlab somehow?
What OS are you using?

Are the code and compile commands you have shown the exact ones you are using?
Are you issuing this compile command from a linux command prompt?

Yes i use this code within a Matlab mexfunction

#include <cuda.h>
#include <cuda_runtime.h>
#include<stdio.h>
#include<stdlib.h>
#include <mex.h>

__global__ void stampa(float f)
{
    printf("Hello thread %d, f=%f\n", threadIdx.x, f);
}



void mexFunction(int nlhs,mxArray *plhs[],int nrhs, const mxArray *prhs[]){
//...
stampa<<<1,5>>>(5.5);
cudaDeviceSynchronize();

}

I have Matlab and i compiling this code within matlab…I use Linux.
This is the command that i use to compile it within matlab (in bold):

<b>nvcc  --compile stampa.cu   -o stampa.o  --compiler-options -fPIC  -I/home/matlab/r2014a/extern/include -arch=sm_20</b>
stampa.cu(15): error: calling a __host__ function("mexPrintf") from a __global__ function("stampa") is not allowed

1 error detected in the compilation of "/tmp/tmpxft_00004662_00000000-6_stampa.cpp1.ii".
<b>mex ('stampa.o', '-L/usr/local/cuda-5.0/lib64', '-lcudart')</b>
Building with 'gcc'.
Error using mex
gcc: error: stampa.o: No such file or directory

matlab is intercepting the printf function in your CUDA kernel, and replacing it with it’s own printf function (mexPrintf) which is not usable within a CUDA kernel.
This may be of interest:

simplePrintf
This CUDA Runtime API sample is a very basic sample that implements how to use the printf function in the device code. Specifically, for devices with compute capability less than 2.0, the function cuPrintf is called; otherwise, printf can be used directly.
I have a compute capability 2.1

Thanks, i resolve it

Hi davide
Could you please tell me how you resolved that?
I have the same problem.
tnx in advance