我刚刚在我的笔记本上安装了CUDA 5.5并尝试使用NVCC从这个链接编译一个基本的hello world程序http://computer-graphics.se/hello-world-for-cuda.html
我尝试的代码是:
// This is the REAL "hello world" for CUDA!
// It takes the string "Hello ", prints it, then passes it to CUDA with an array
// of offsets. Then the offsets are added in parallel to produce the string "World!"
// By Ingemar Ragnemalm 2010
#include <stdio.h>
const int N = 16;
const int blocksize = 16;
__global__
void hello(char *a, int *b)
{
a[threadIdx.x] += b[threadIdx.x];
}
int main()
{
char a[N] = "Hello \0\0\0\0\0\0";
int b[N] = {15, 10, 6, 0, -11, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
char *ad;
int *bd;
const int csize = N*sizeof(char);
const int isize = N*sizeof(int);
printf("%s", a);
cudaMalloc( (void**)&ad, csize );
cudaMalloc( (void**)&bd, isize );
cudaMemcpy( ad, a, csize, cudaMemcpyHostToDevice );
cudaMemcpy( bd, b, isize, cudaMemcpyHostToDevice );
dim3 dimBlock( blocksize, 1 );
dim3 dimGrid( 1, 1 );
hello<<<dimGrid, dimBlock>>>(ad, bd);
cudaMemcpy( a, ad, csize, cudaMemcpyDeviceToHost );
cudaFree( ad );
cudaFree( bd );
printf("%s\n", a);
return EXIT_SUCCESS;
}
应该打印出#Hello; Hello world!&#34;,但在使用&#34; nvcc hello.cu -o a.out&#34;编译后,我的输出是#34;你好你好&#34;,有人可以告诉我发生了什么事吗?
答案 0 :(得分:1)
这是由于CUDA驱动程序安装损坏造成的。更正的安装允许正确运行的代码无错误地运行。
[此社区wiki条目是根据评论汇总的,以便将此问题从未答复的队列中删除]