CUDA cuPrintf
February 8th, 2010 by JeremyI finally got an Nvidia developer account a few days ago which gave me access to a very useful library to use with CUDA.
cuPrintf allows printf equivalent statements to be placed inside CUDA kernels without the need for -deviceemu.
The following example demonstrates a simple use for cuPrintf and displays the current thread ID.
演示了打印當前線程的ID.
-
#include <cuda.h>
-
#include "cuPrintf.cu"
-
-
__global__ void cuPrintfExample()
-
{
-
int tid;
-
tid = blockIdx.x* blockDim.x + threadIdx.x;
-
cuPrintf("%d\n", tid);
-
}
-
-
int main()
-
{
-
cudaPrintfInit();
-
cuPrintfExample <<< 5, 2 >>> ();
-
cudaPrintfDisplay(stdout,true);
-
cudaPrintfEnd();
-
return 0;
-
}
cudaPrintfInit and cudaPrintfEnd only need be called once throughout your entire project.
Output is not automatically displayed on the screen, but stored in a buffer which is cleared and displayed when cudaPrintfDisplay is called. The size of the buffer can be specified with the optional argument cudaPrintfInit(size_t bufferLen).
cudaPrintfEnd simply frees the memory allocated by cudaPrintfInit.
When cudaPrintfDisplay is called, output stored in the buffer is displayed to the console. The second argument in this call either displays the current thread (true) or doesn’t (false). The first arguemnt, specified by stdout in this example, simply defines the descriptor where the cuPrintf log is sent.
On another note, I’ve found that using cuPrintf impacts on the performance of my kernels, presumably due to the data transfer performed every time cuPrintfDisplay() is called.
http://www.jeremykemp.co.uk/08/02/2010/cuda-cuprintf/