【发布时间】:2013-01-27 19:25:22
【问题描述】:
我正在使用 Visual Studio 2010 和具有计算能力 2.0 的 GTX480。
我尝试将 sm 设置为 2.0,但是当我尝试在内核中使用 printf() 时,我得到:
错误:从 __device__/__global__ 调用主机函数(“printf”) 不允许使用函数(“测试”)
这是我的代码:
#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”'page _106'“B.16.4 Examples” 最后,这对我有用:D 谢谢。
#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;
}
【问题讨论】:
-
在内核调用后添加
cudaDeviceSynchronize()。仅包括iostream和cuda_runtime.h。 -
@sgar91:内核 printf 需要
cstdio -
@talonmies..
iostream是cstdio的超集。我可以通过包含iostream来使用printf。 -
由于英语不是您的第一语言,如果您将来使用拼写检查器将不胜感激。
-
感谢您的建议
标签: cuda