CUDA 4.1 printf()错误

dvg*_*rco 8 cuda

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

error : calling a host function("printf") from a __device__/__global__ function("kernel") is not allowed
Run Code Online (Sandbox Code Playgroud)

码:

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

#include <stdio.h>

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

int main()
{
  kernel<<<1, 1>>>();
  return 0;
}
Run Code Online (Sandbox Code Playgroud)

我在这里错过了什么吗?

tal*_*ies 13

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

nvcc -arch=sm_21 [other options] .....
Run Code Online (Sandbox Code Playgroud)

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

  • 非常感谢!在VS2010中,我更改了项目 - > {项目名称}属性 - > CUDA C/C++ - >设备 - > [代码生成]:compute_10,sm_10到compute_20,sm_20并且它工作正常. (2认同)