【问题标题】:Vector Addition Cuda program on cluster giving many errors集群上的向量加法 Cuda 程序给出了许多错误
【发布时间】:2017-06-20 10:10:58
【问题描述】:

我正在尝试在 Tesla K20 服务器上运行 Cuda 矢量添加程序,但遇到很多错误。我正在提交代码。

#include <stdio.h>
#include <stdlib.h>
#include "cuda_utils.h"
#include "timer.h"
/*
* **CUDA KERNEL** 
* 
* Compute the sum of two vectors 
*   C[i] = A[i] + B[i]
* 
*/
__global__ void vecAdd(float* a, float* b, float* c) {

/* Calculate index for this thread */
  int i = blockIdx.x * blockDim.x + threadIdx.x;

 /* Compute the element of C */
 c[i] = a[i] + b[i];
 }

 void compute_vec_add(int N, float *a, float* b, float *c);

/*
* 
* Host code to drive the CUDA Kernel
* 
*/
int main() {

float *d_a, *d_b, *d_c;
float *h_a, *h_b, *h_c, *h_temp;
int i;
int N = 1024 * 1024 * 512;

struct stopwatch_t* timer = NULL;
long double t_pcie_htd, t_pcie_dth, t_kernel, t_cpu;

/* Setup timers */
stopwatch_init();
timer = stopwatch_create();

/*
Create the vectors
*/
h_a = (float *) malloc(sizeof(float) * N);
h_b = (float *) malloc(sizeof(float) * N);
h_c = (float *) malloc(sizeof(float) * N);

/*
 Set the initial values of h_a, h_b, and h_c
 */
for (i = 0; i < N; i++) {
h_a[i] = (float) (rand() % 100) / 10.0;
h_b[i] = (float) (rand() % 100) / 10.0;
h_c[i] = 0.0;
}

/*
Allocate space on the GPU
*/
CUDA_CHECK_ERROR(cudaMalloc(&d_a, sizeof(float) * N));
CUDA_CHECK_ERROR(cudaMalloc(&d_b, sizeof(float) * N));
CUDA_CHECK_ERROR(cudaMalloc(&d_c, sizeof(float) * N));

/*
Copy d_a and d_b from CPU to GPU
*/
stopwatch_start(timer);
CUDA_CHECK_ERROR(cudaMemcpy(d_a, h_a, sizeof(float) * N, cudaMemcpyHostToDevice));
CUDA_CHECK_ERROR(cudaMemcpy(d_b, h_b, sizeof(float) * N, cudaMemcpyHostToDevice));
t_pcie_htd = stopwatch_stop(timer);
fprintf(stderr, "Time to transfer data from host to device: %Lg secs\n",t_pcie_htd);

/*
Run N/256 blocks of 256 threads each
*/
dim3 GS(N / 256, 1, 1);
dim3 BS(256, 1, 1);

stopwatch_start(timer);
vecAdd<<<GS, BS>>>(d_a, d_b, d_c);
cudaThreadSynchronize();
t_kernel = stopwatch_stop(timer);
fprintf(stderr, "Time to execute GPU kernel: %Lg secs\n", t_kernel);

/*
Copy d_cfrom GPU to CPU
*/
stopwatch_start(timer);
CUDA_CHECK_ERROR(cudaMemcpy(h_c, d_c, sizeof(float) * N, cudaMemcpyDeviceToHost));
t_pcie_dth = stopwatch_stop(timer);
fprintf(stderr, "Time to transfer data from device to host: %Lg secs\n",t_pcie_dth);

/* 
 Double check errors
 */
h_temp = (float *) malloc(sizeof(float) * N);
stopwatch_start(timer);
compute_vec_add(N, h_a, h_b, h_temp);
t_cpu = stopwatch_stop(timer);
fprintf(stderr, "Time to execute CPU program: %Lg secs\n", t_cpu);

int cnt = 0;
for (int i = 0; i < N; i++) {
if (abs(h_temp[i] - h_c[i]) > 1e-5)
  cnt++;
}
fprintf(stderr, "number of errors: %d out of %d\n", cnt, N);

/*
 Free the device memory
*/
cudaFree(d_a);
cudaFree(d_b);
cudaFree(d_c);

/*
 Free the host memory
*/
free(h_a);
free(h_b);
free(h_c);

/* 
 Free timer 
*/
stopwatch_destroy(timer);

if (cnt == 0) {
printf("\n\nSuccess\n");
}
}

现在,对于我正在运行的这段代码,我得到了大量的错误列表。我在同一目录中有 timer.c 和 cuda_utils.h。 对于编译,

nvcc vecAdd.cu timer.c -o vecAdd

然后我得到的错误是:

/tmp/tmpxft_000014db_00000000-17_vecAdd.o: In function `main':
tmpxft_000014db_00000000-4_vecAdd.cudafe1.cpp:(.text+0x7e): undefined reference to `stopwatch_init()'
tmpxft_000014db_00000000-4_vecAdd.cudafe1.cpp:(.text+0x83): undefined reference to `stopwatch_create()'
tmpxft_000014db_00000000-4_vecAdd.cudafe1.cpp:(.text+0x278): undefined reference to `stopwatch_start(stopwatch_t*)'
tmpxft_000014db_00000000-4_vecAdd.cudafe1.cpp:(.text+0x2ff): undefined reference to `stopwatch_stop(stopwatch_t*)'
tmpxft_000014db_00000000-4_vecAdd.cudafe1.cpp:(.text+0x380): undefined reference to `stopwatch_start(stopwatch_t*)'
tmpxft_000014db_00000000-4_vecAdd.cudafe1.cpp:(.text+0x3dc): undefined reference to `stopwatch_stop(stopwatch_t*)'
tmpxft_000014db_00000000-4_vecAdd.cudafe1.cpp:(.text+0x416): undefined reference to `stopwatch_start(stopwatch_t*)'
tmpxft_000014db_00000000-4_vecAdd.cudafe1.cpp:(.text+0x45e): undefined reference to `stopwatch_stop(stopwatch_t*)'
tmpxft_000014db_00000000-4_vecAdd.cudafe1.cpp:(.text+0x4b0): undefined reference to `stopwatch_start(stopwatch_t*)'
tmpxft_000014db_00000000-4_vecAdd.cudafe1.cpp:(.text+0x4de): undefined reference to `stopwatch_stop(stopwatch_t*)'
tmpxft_000014db_00000000-4_vecAdd.cudafe1.cpp:(.text+0x61f): undefined reference to `stopwatch_destroy(stopwatch_t*)'
collect2: error: ld returned 1 exit status

有人可以解释一下为什么会出现这些错误。此外,我是 Cuda 编程的初学者。 我的猜测是它与链接有关。

【问题讨论】:

  • 您的编译器不知道stopwatch_init() 是什么。这是在哪里定义的?
  • 它在我用于编译的 timer.c 中定义。
  • 嗯,这肯定有问题,因为编译器清楚地说“未定义”
  • 编译命令应该不一样吧,我的意思是vecAdd.cu 之后 timer.c?
  • CUDA 不是 C !

标签: cuda nvidia


【解决方案1】:

nvcc.cu 代码解释为 C++ 代码,这将导致与符号名称冲突。解决方案是将#include "timer.h"extern "C" {} 括在vecAdd.cu 中。

问题在于,如果 .cu 文件包含来自另一个 .c 文件的 C 函数,比如 function.c,这些函数将被解释为 C++ 函数,而 为这些函数设置特殊符号名称。稍后,在编译 function.c 时,这些函数将使用正常的符号名称进行编译。在链接阶段,由于 .cu 文件中的符号名称与编译后的 function.o 文件中的符号名称不匹配,您将获得未解析的引用。因此,您需要在标头周围使用 extern "C"{} 语法,包括外部 C 代码函数。

(引自here


验证

使用nvcc -c vecAdd.cu 编译问题中提供的vecAdd.cu 并使用nm vecAdd.o 列出符号会打印以下行:

...
00000000000007cf t _Z10cudaLaunchIcE9cudaErrorPT_
00000000000007aa t _Z10cudaMallocIfE9cudaErrorPPT_m
                 U _Z14stopwatch_initv
                 U _Z14stopwatch_stopP11stopwatch_t
0000000000000016 T _Z15compute_vec_addiPfS_S_
                 U _Z15stopwatch_startP11stopwatch_t
                 U _Z16stopwatch_createv
                 U _Z17stopwatch_destroyP11stopwatch_t
0000000000000672 T _Z29__device_stub__Z6vecAddPfS_S_PfS_S_
0000000000000703 T _Z6vecAddPfS_S_
...

你可以看到stopwatch_init变成了_Z14stopwatch_iniv,以此类推。

由于timer.ctimer.h 没有定义,所以我为它们编写了一个最少的代码。

// timer.h
struct stopwatch_t { double t; };
void stopwatch_init();
struct stopwatch_t *stopwatch_create();
void stopwatch_start(struct stopwatch_t *timer);
long double stopwatch_stop(struct stopwatch_t *timer);
void stopwatch_destroy(struct stopwatch_t *timer);

// timer.c
void stopwatch_init() { }
struct stopwatch_t *stopwatch_create() { return 0; }
void stopwatch_start(struct stopwatch_t *timer) { }
long double stopwatch_stop(struct stopwatch_t *timer) { return 0; }
void stopwatch_destroy(struct stopwatch_t *timer) { }

使用上面的代码,nvcc -c timer.cnm timer.o 产生:

0000000000000007 T stopwatch_create
0000000000000029 T stopwatch_destroy
0000000000000000 T stopwatch_init
0000000000000012 T stopwatch_start
000000000000001d T stopwatch_stop

您可以看到timer.c 函数会出现符号名称冲突。

修改vecAdd.cu到:

#include <stdio.h>
#include <stdlib.h>
#include "cuda_utils.h"
extern "C" {
    #include "timer.h"
}
...

nvcc -c vecAdd.cunm vecAdd.o 产生:

...
                 U __stack_chk_fail
                 U stderr
                 U stopwatch_create
                 U stopwatch_destroy
                 U stopwatch_init
                 U stopwatch_start
                 U stopwatch_stop
00000000000007cf t _Z10cudaLaunchIcE9cudaErrorPT_
00000000000007aa t _Z10cudaMallocIfE9cudaErrorPPT_m
...

您可以看到 C 函数的符号名称没有变化。在这种情况下,问题中的编译命令:nvcc vecAdd.cu timer.c -o vecAdd 将起作用。


编辑

正如 OP 在评论中提到的,g++ -c timer.cnvcc vecAdd.cu timer.o -o vecAdd 也可以工作,因为默认情况下 g++ 会将 .c 文件视为 C++ 代码。

g++ -c timer.cnm timer.o 打印:

0000000000000000 T _Z14stopwatch_initv
000000000000001d T _Z14stopwatch_stopP11stopwatch_t
0000000000000012 T _Z15stopwatch_startP11stopwatch_t
0000000000000007 T _Z16stopwatch_createv
0000000000000029 T _Z17stopwatch_destroyP11stopwatch_t

【讨论】:

  • 谢谢。我得到了它。 timer.c的编译有问题,我用的是gcc而不是g++。它解决了问题。
猜你喜欢
  • 1970-01-01
  • 2023-03-23
  • 2016-02-14
  • 2021-06-25
  • 2012-02-13
  • 1970-01-01
  • 2012-11-03
  • 1970-01-01
  • 2021-08-11
相关资源
最近更新 更多