Usage of printf() in Cuda 4.0 Compilation Error

I have a GTX 570 (Fermi architecture) which is of compute Capability 2.0. I have Cuda version 4.0 on my computer and I am using Ubuntu 10.10

With Cuda 4.0 it is possible to use printf() inside kernels. Here is an example code from page 125 of the Cuda 4.0 programming guide

#if defined(__CUDA_ARCH__) && (__CUDA_ARCH__ < 200)

#define printf(f, ...) ((void)(f, __VA_ARGS__),0)

#endif

__global__ void helloCUDA(float f)

{

printf(“Hello thread %d, f=%f\n”, threadIdx.x, f);

}

void main()

{

helloCUDA<<<1, 5>>>(1.2345f);

cudaDeviceReset();

}

I am getting the following compilation error.

gaurish108 MyPractice: nvcc printf_inkernel.cu -o printf_inkernel

printf_inkernel.cu(10): error: unrecognized token

printf_inkernel.cu(10): error: expected an expression

printf_inkernel.cu(10): error: unrecognized token

printf_inkernel.cu(10): error: unrecognized token

printf_inkernel.cu(10): error: unrecognized token

printf_inkernel.cu(10): error: unrecognized token

printf_inkernel.cu(10): error: unrecognized token

printf_inkernel.cu(10): error: unrecognized token

printf_inkernel.cu(15): warning: return type of function "main" must be "int"

8 errors detected in the compilation of "/tmp/tmpxft_000014cd_00000000-4_printf_inkernel.cpp1.ii".

Why is it not recognizing printf? I tried adding the flag -arch=sm_20 , but I get the same error.

Looking closely at your code, the string inside printf() uses typographical (left and right) quotation marks, not the simple quotation mark that corresponds to ASCII 0x22. This happens when you copy the code examples directly from the PDF file.

Note to Nvidia: It might be worth fixing this in the Programming Guide.

Thank you for the observation! Alas, even after the correction I am still getting a compilation error albeit a different one

printf_inkernel.cu(10): warning: expression has no effect

printf_inkernel.cu(10): warning: expression has no effect

printf_inkernel.cu(10): warning: expression has no effect

printf_inkernel.cu(15): warning: return type of function "main" must be "int"

printf_inkernel.cu(10): error: identifier "printf" is undefined

printf_inkernel.cu(15): warning: return type of function "main" must be "int"

1 error detected in the compilation of "/tmp/tmpxft_00000786_00000000-9_printf_inkernel.cpp4.ii".

Add a line with:

#include “stdio.h”

Whoops! I feel quite stupid for not including that line.

Okay now the code compiles. But it is not printing anything to output.

Here is the code again

// printf() is only supported

// for devices of compute capability 2.0 and above

#include<stdio.h>

#include<stdlib.h>

#if defined(__CUDA_ARCH__) && (__CUDA_ARCH__ < 200)

#define printf(f, ...) ((void)(f, __VA_ARGS__),0)

#endif

__global__ void helloCUDA(float f)

{

printf("Hello thread %d, f=%f\n", threadIdx.x, f);

}

int main()

{

helloCUDA<<<1, 5>>>(1.2345);

cudaDeviceReset();

return 0;

}

here are the compilation warnings

printf_inkernel.cu(13): warning: expression has no effect

printf_inkernel.cu(13): warning: expression has no effect

printf_inkernel.cu(13): warning: expression has no effect

However when executed I do not see any output. Probably I need to include CUDA specific header files?

By default the compiler compiles for sm_10, which does not support device-side printf() [see also the #ifdef in the posted code]. Try adding -arch=sm_20 to your nvcc commandline.