链接C code使用CUDA code [英] Linking C code with Cuda code

查看:112
本文介绍了链接C code使用CUDA code的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我在写一个很基本的CUDA code(向量加法)自学CUDA编程的基础知识。我想起来了,我想使它连接在一起的一个.c和.CU文件工作时我写一个.CU文件,但。我的main.c文件如下:

So I'm writing a very basic CUDA code (vector addition) to teach myself the basics of CUDA programming. I've got it working when I write one .cu file, but now I am trying to make it work with a .c and .cu file linked together. My main.c file is as follows:

#include "Test.h"
#include <stdlib.h>

int main(int argc, char *argv[]) {
        int n = 1000;
        size_t size = n * sizeof(float);
        int i;

        float *h_a = malloc(size), *h_b = malloc(size), *h_c = malloc(size);

        for(i = 0; i < n; i++) {
                h_a[i] = h_b[i] = i;
        }

        addVec(h_a, h_b, h_c, n);

        exit(0);
}

下面,Test.h只是说:

Here, Test.h simply says:

void addVec(float *, float *, float *, int);

我vecAdd.cu文件说:

My vecAdd.cu file says:

#include "Test.h"

__global__ void vecAdd(float *a, float *b, float *c, int n) {
        int i = blockDim.x * blockIdx.x + threadIdx.x;

        if(i < n)
                c[i] = a[i] + b[i];
}

void addVec(float *a, float *b, float *c, int n) {
        float *d_a, *d_b, *d_c;
        size_t size = n * sizeof(float);

        cudaMalloc(&d_a, size);
        cudaMalloc(&d_b, size);
        cudaMalloc(&d_c, size);

        ...
}

我然后运行命令:

I then run the commands:

gcc -c -Wall -O3 main.c -o ../obj/main.o
nvcc -c -O3 vecAdd.cu -o ../obj/vecAdd.o
gcc -L/usr/local/cuda/lib64 -lcudart ../obj/main.o ../obj/vecAdd.o -o ../bin/nvTest

前两个做工精细。最后一个,当我尝试这两个目标文件链接,告诉我,我有一个未定义的引用addVec,虽然它在vecAdd.cu定义......我究竟做错了什么?

The first two work fine. The last one, when I try to link the two object files, tells me that I have an undefined reference to addVec, though it is defined in vecAdd.cu... what am I doing wrong?

推荐答案

您有一个C / C ++链接的问题,基本上是相同的,所描述的此处。这是因为NVCC是使用C ++编译器的主机code(创建C ++风格联动引用,即重整),因此GCC是国际preting的main.c作为交流(不是C ++)文件,并创建空调风格联动引用。

You have a C/C++ linkage problem that is basically identical to that described here. This is because nvcc is using a c++ compiler for host code (creating c++ style linkage references i.e. "mangling") and gcc is interpreting main.c as a c (not c++) file and therefore creating c style linkage references.

有至少2种方法来解决这个问题:

There are at least 2 ways to fix it:


  1. 将您的main.c成的main.cpp和使用G ++,你正在使用gcc,现在(你的第一个和第三编译和链接步骤)。然后,一切都将是一致的C ++风格的引用。

  2. 您的C ++模块(vecAdd.cu)内声明的外部参考应该是描述C形式<一个href=\"http://stackoverflow.com/questions/13553015/cuda-c-linker-error-undefined-reference\">here.

  1. convert your main.c into a main.cpp and use g++ where you are using gcc now (for your first and 3rd compile and link steps). Then everything will be consistently c++ style references.
  2. Declare within your C++ module (vecAdd.cu) that the external reference should be C style as described here.

这篇关于链接C code使用CUDA code的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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