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".
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.