我正在尝试编译从here复制的简单helloworld示例。我正在使用CentOS 6.4环境。
// 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;
}
尝试编译它可以正常工作:
$ nvcc hello_world.cu -o hello_world.bin
但是当我运行它时:
$ ./hello_world.bin
Hello Hello
它不打印预期的'Hello World',而是打印'Hello Hello'。如果我从__global__
函数中注释掉一些代码,那么根本没有任何影响,或者甚至在hello()函数中添加printf也不会产生任何结果。似乎没有调用该函数。我错过了什么?我可以检查什么?
我还尝试了其他一些示例源代码,这些代码可以在另一个框中运行。问题似乎是一样的,所以在这台计算机上有些不对。
编辑:
$ nvcc --version
nvcc: NVIDIA (R) Cuda compiler driver
Copyright (c) 2005-2013 NVIDIA Corporation
Built on Wed_Jul_17_18:36:13_PDT_2013
Cuda compilation tools, release 5.5, V5.5.0
$ nvidia-smi -a
-bash: nvidia-smi: command not found
$ cat /proc/driver/nvidia/version
NVRM version: NVIDIA UNIX x86_64 Kernel Module 319.60 Wed Sep 25 14:28:26 PDT 2013
GCC version: gcc version 4.4.7 20120313 (Red Hat 4.4.7-3) (GCC)
$ dmesg | grep NVRM
NVRM: loading NVIDIA UNIX x86_64 Kernel Module 319.60 Wed Sep 25 14:28:26 PDT 2013
NVRM: loading NVIDIA UNIX x86_64 Kernel Module 319.60 Wed Sep 25 14:28:26 PDT 2013
答案 0 :(得分:2)
感谢@RobertCrovella的建议,我在代码中添加了return value checks:
#include <stdio.h>
const int N = 16;
const int blocksize = 16;
#define gpuErrchk(ans) { gpuAssert((ans), __FILE__, __LINE__); }
inline void gpuAssert(cudaError_t code, char *file, int line, bool abort=true)
{
if (code != cudaSuccess)
{
fprintf(stderr,"GPUassert: %s %s %d\n", cudaGetErrorString(code), file, li ne);
if (abort) exit(code);
}
}
__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);
gpuErrchk(cudaMalloc( (void**)&ad, csize ));
gpuErrchk(cudaMalloc( (void**)&bd, isize ));
gpuErrchk(cudaMemcpy( ad, a, csize, cudaMemcpyHostToDevice ));
gpuErrchk(cudaMemcpy( bd, b, isize, cudaMemcpyHostToDevice ));
dim3 dimBlock( blocksize, 1 );
dim3 dimGrid( 1, 1 );
hello<<<dimGrid, dimBlock>>>(ad, bd);
gpuErrchk( cudaPeekAtLastError() );
gpuErrchk( cudaDeviceSynchronize() );
gpuErrchk(cudaMemcpy( a, ad, csize, cudaMemcpyDeviceToHost ));
gpuErrchk(cudaFree( ad ));
gpuErrchk(cudaFree( bd ));
printf("%s\n", a);
return EXIT_SUCCESS;
}
这导致在运行代码时发现此错误:
$ nvcc hello_world.cu -o hello_world.bin
$ ./hello_world.bin
GPUassert: CUDA driver version is insufficient for CUDA runtime version hello_world.cu 39
我在云提供商上运行了这个,它提供了CUDA环境的设置,所以我怀疑在之后我做过的env中出了什么问题。在我的环境中,使用
设置cuda envmodule load cuda55/toolkit/5.5.22
应该完全设置环境。这是我一开始不知道的事情,所以在使用之前,我曾试图自己设置一些路径。由于这是在我的.bash_profile:
export CUDA_INSTALL_PATH=/cm/shared/apps/cuda55/toolkit/current
export PATH=$PATH:$CUDA_INSTALL_PATH/bin
export LD_LIBRARY_PATH=$CUDA_INSTALL_PATH/lib64
export PATH=$PATH:$CUDA_INSTALL_PATH/lib
一旦我删除了我添加到.bash_profile的内容并进行了注销/登录,一切都开始正常运行。