CUDA 4.1 printf()错误

时间:2012-03-17 11:33:44

标签: cuda

即使我有一张费米卡(gtx 560),我在VS2010上也收到了这个错误:

error : calling a host function("printf") from a __device__/__global__ function("kernel") is not allowed

代码:

#include "cuda_runtime.h"
#include "device_launch_parameters.h"

#include <stdio.h>

__global__ void kernel()
{
  printf("hello");
}

int main()
{
  kernel<<<1, 1>>>();
  return 0;
}

我在这里错过了什么吗?

1 个答案:

答案 0 :(得分:13)

您需要确保编译正确的架构。只有Fermi和Kepler卡(因此计算能力2.0,2.1,3.0和3.5设备)在内核中支持printf。如果您编译代码如下:

nvcc -arch=sm_21 [other options] .....

代码应该正确构建。默认体系结构是compute 1.0,这就是您收到错误的原因。如果你使用Visual Studio,应该有一个项目选项来选择目标架构,虽然我不能准确地告诉你在哪里找到它,因为我不使用它与CUDA。