当我尝试在内核中使用printf()时出现错误 [英] I get an error when I try to use printf() in a kernel

查看:114
本文介绍了当我尝试在内核中使用printf()时出现错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Visual Studio 2010和具有计算功能2.0的GTX480。

I'm using Visual Studio 2010 and a GTX480 with compute capability 2.0.

我尝试将sm设置为2.0,但是当我尝试使用printf()时在内核中,我得到:

I have tried setting sm to 2.0, but when I attempt to use printf() in a kernel, I get:


错误:从__device __ / __ global__
函数调用主机函数( printf) (测试)是不允许的

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

这是我的代码:

#include "util\cuPrintf.cu"
#include <cuda.h>
#include <iostream>
#include <stdio.h>
#include <conio.h>
#include <cuda_runtime.h>

__global__ void test (void)
{
  printf("Hello, world from the device!\n");
}

void main(void)
{
    test<<<1,1>>>();
    getch();
}

我在这里找到一个示例: CUDA_C_Programming_Guide'页面_106'B。 16.4示例
最后,对我有用:D谢谢。

I find a example here: "CUDA_C_Programming_Guide" 'page _106' "B.16.4 Examples" at last,it is work for me :D thank you.

#include "stdio.h"
#include <conio.h>

// printf() is only supported
// for devices of compute capability 2.0 and higher

  #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.2345f);
    cudaDeviceSynchronize();
    getch();
    return 0;
}


推荐答案

要使用 printf 在内核代码中,您必须做三件事:

To use printf in kernel code, you have to do three things:


  1. 确保 cstdio stdio.h 包含在内核编译单元中。 CUDA通过重载实现内核 printf ,因此您必须包含该文件

  2. 编译代码以实现计算能力2.x或3.x并在受支持的GPU上运行它(因此将 -arch = sm_20 之类的内容传递给nvcc或Visual Studio或Nsight Eclipse版本中的IDE等效项)

  3. 通过在主机代码中包含显式或隐式同步点来确保内核已完成运行(例如, cudaDeviceSynchronize )。

  1. make sure that cstdio or stdio.h are included in the kernel compilation unit. CUDA implements kernel printf by overloading, so you must include that file
  2. Compile your code for compute capability 2.x or 3.x and run it on a supported GPU (so pass something like -arch=sm_20 to nvcc or the IDE equivalent in Visual Studio or Nsight Eclipse edition)
  3. Ensure that the kernel has finished running by including an explicit or implicit synchronization point in your host code (cudaDeviceSynchronize for example).

这篇关于当我尝试在内核中使用printf()时出现错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆