3
votes

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.

1

1 Answers

6
votes

It looks like you've got a weird quote character at either end of printf's formatter string.

If you copy and paste this program, it ought to compile and run without error:

#include <stdio.h>

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

int main()
{
  helloCUDA<<<1, 5>>>(1.2345f);
  cudaDeviceReset();
  return 0;
}

And the output:

$ nvcc -arch=sm_20 test.cu -run
Hello thread 0, f=1.234500
Hello thread 1, f=1.234500
Hello thread 2, f=1.234500
Hello thread 3, f=1.234500
Hello thread 4, f=1.234500

I don't understand the need for the weird macro which begins the program. I'd get rid of it.