在Cuda 4.0编译错误中使用printf()

smi*_*dha 3 printf cuda

我有一个GTX 570(Fermi架构),它具有计算能力2.0.我的计算机上有Cuda 4.0版,我使用的是Ubuntu 10.10

使用Cuda 4.0,可以使用printf()内核.以下是Cuda 4.0 编程指南第125页的示例代码

#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();
}
Run Code Online (Sandbox Code Playgroud)

我收到以下编译错误.

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".
Run Code Online (Sandbox Code Playgroud)

为什么不识别printf?我尝试添加标志-arch=sm_20,但我得到了同样的错误.

Jar*_*ock 6

看起来你printf的格式化程序字符串的两端都有一个奇怪的引号字符.

如果您复制并粘贴此程序,它应该编译并运行而不会出现错误:

#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;
}
Run Code Online (Sandbox Code Playgroud)

并输出:

$ 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
Run Code Online (Sandbox Code Playgroud)

我不明白开始该程序的奇怪宏的必要性.我会摆脱它.

  • 嘿,非常感谢!代码现在正在运行.顺便说一句,你是来自斯坦福在线GPU计算视频讲座的Jared Hoberock吗?那些讲座真是太神奇了!非常感谢您精彩的材料! (2认同)