# Use print in CUDA kernel

**URL:** <https://forums.developer.nvidia.com/t/use-print-in-cuda-kernel/34539>\
**Category:** CUDA Programming and Performance\
**Created:** [August 21, 2014, 11:33am UTC](https://forums.developer.nvidia.com/t/use-print-in-cuda-kernel/34539 "2014-08-21T11:33:36Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![davide1705](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@davide1705](https://forums.developer.nvidia.com/u/davide1705)\
**Post date:** [August 21, 2014, 11:33am UTC](https://forums.developer.nvidia.com/t/use-print-in-cuda-kernel/34539/1 "2014-08-21T11:33:36Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![turret](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@turret](https://forums.developer.nvidia.com/u/turret)\
**Post date:** [August 21, 2014, 1:13pm UTC](https://forums.developer.nvidia.com/t/use-print-in-cuda-kernel/34539/2 "2014-08-21T13:13:49Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![davide1705](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@davide1705](https://forums.developer.nvidia.com/u/davide1705)\
**Post date:** [August 21, 2014, 1:33pm UTC](https://forums.developer.nvidia.com/t/use-print-in-cuda-kernel/34539/3 "2014-08-21T13:33:50Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![turret](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@turret](https://forums.developer.nvidia.com/u/turret)\
**Post date:** [August 21, 2014, 1:45pm UTC](https://forums.developer.nvidia.com/t/use-print-in-cuda-kernel/34539/4 "2014-08-21T13:45:51Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Robert\_Crovella](https://sea2.discourse-cdn.com/nvidia/user_avatar/forums.developer.nvidia.com/robert_crovella/32/14043_2.png) [@Robert\_Crovella](https://forums.developer.nvidia.com/u/Robert_Crovella)\
**Post date:** [August 21, 2014, 1:44pm UTC](https://forums.developer.nvidia.com/t/use-print-in-cuda-kernel/34539/5 "2014-08-21T13:44:59Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![davide1705](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@davide1705](https://forums.developer.nvidia.com/u/davide1705)\
**Post date:** [August 21, 2014, 1:51pm UTC](https://forums.developer.nvidia.com/t/use-print-in-cuda-kernel/34539/6 "2014-08-21T13:51:45Z")

</div>

> [@txbob](#):
>
> 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

```auto
#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):

```auto
<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

```

---

<div class="post-metadata">

**Author:** ![Robert\_Crovella](https://sea2.discourse-cdn.com/nvidia/user_avatar/forums.developer.nvidia.com/robert_crovella/32/14043_2.png) [@Robert\_Crovella](https://forums.developer.nvidia.com/u/Robert_Crovella)\
**Post date:** [August 21, 2014, 2:09pm UTC](https://forums.developer.nvidia.com/t/use-print-in-cuda-kernel/34539/7 "2014-08-21T14:09:30Z")

</div>

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:

> **[Forward CUDA printf output to the MATLAB console - MATLAB Answers - MATLAB...](https://www.mathworks.com/matlabcentral/answers/132358-forward-cuda-printf-output-to-the-matlab-console)**
>
> Forward CUDA printf output to the MATLAB console. Learn more about mex, printf, cuda MATLAB

---

<div class="post-metadata">

**Author:** ![davide1705](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@davide1705](https://forums.developer.nvidia.com/u/davide1705)\
**Post date:** [August 21, 2014, 2:29pm UTC](https://forums.developer.nvidia.com/t/use-print-in-cuda-kernel/34539/8 "2014-08-21T14:29:19Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![davide1705](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@davide1705](https://forums.developer.nvidia.com/u/davide1705)\
**Post date:** [August 21, 2014, 2:55pm UTC](https://forums.developer.nvidia.com/t/use-print-in-cuda-kernel/34539/9 "2014-08-21T14:55:14Z")

</div>

Thanks, i resolve it

---

<div class="post-metadata">

**Author:** ![Maryam\_H](https://sea2.discourse-cdn.com/nvidia/user_avatar/forums.developer.nvidia.com/maryam_h/32/10564_2.png) [@Maryam\_H](https://forums.developer.nvidia.com/u/Maryam_H)\
**Post date:** [December 19, 2014, 8:41pm UTC](https://forums.developer.nvidia.com/t/use-print-in-cuda-kernel/34539/10 "2014-12-19T20:41:49Z")

</div>

> [@davide1705](#):
>
> Thanks, i resolve it

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