CUDA:使用单独的文件链接共享库.so



我正在尝试使用nvcc 6.0从单独的。cu文件编译一个。so库。我设法使用-rdc=true分别编译每个文件。当我尝试用c链接我的库时,我得到了一堆错误。我已经编译了从一个单一的库。我在nvcc 5.0的一个问题中读到,这里不支持这一点。我进入了nvcc 6.0的手册,但无法找到(或理解)如果是这样的话。下面是我的makefile(我在编写makefile方面不是很有经验,所以任何建议都非常欢迎)。错误被粘贴在

之后
NCC = /usr/local/cuda-6.0/bin/nvcc
CC = g++
LCUDA = -L/usr/local/cuda/lib64 -lcuda -lcudart
LNUM = -lm
OOP = -arch=sm_30 -rdc=true --shared -Xcompiler -fPIC -c
all: cuda_ddm.so
cuda_ddm.so : wfpt.o stationary.o
$(CC) -Wall -shared -include ./c_cuda_ddm.h -o $@ $^ $(LCUDA) 
wfpt.o : wfpt.cu
$(NCC) $(OOP) $@ $^ 
test.o : test.cu
$(NCC) $(OOP) $@ $^

错误:

(编辑:我改变了编译错误,以说明当前的情况。)

/usr/local/cuda-6.0/bin/nvcc -arch=sm_30 -rdc=true --shared -Xcompiler -fPIC -c wfpt.o wfpt.cu 
/usr/local/cuda-6.0/bin/nvcc -arch=sm_30 -rdc=true --shared -Xcompiler -fPIC -c test.o test.cu 
g++ -Wall -shared -o cuda_ddm.so wfpt.o test.o -L/usr/local/cuda/lib64 -lcuda -lcudart 
test.o: In function `big_random_block(int)':
tmpxft_00003c24_00000000-3_test.cudafe1.cpp:(.text+0x5e): multiple definition of `big_random_block(int)'
wfpt.o:tmpxft_00003bc7_00000000-3_wfpt.cudafe1.cpp:(.text+0x5e): first defined here
test.o: In function `big_random_block_int(int)':
tmpxft_00003c24_00000000-3_test.cudafe1.cpp:(.text+0xde): multiple definition of `big_random_block_int(int)'
wfpt.o:tmpxft_00003bc7_00000000-3_wfpt.cudafe1.cpp:(.text+0xde): first defined here
test.o: In function `value(float, float, int)':
tmpxft_00003c24_00000000-3_test.cudafe1.cpp:(.text+0x169): multiple definition of `value(float, float, int)'
wfpt.o:tmpxft_00003bc7_00000000-3_wfpt.cudafe1.cpp:(.text+0x169): first defined here
test.o: In function `__device_stub__Z14float_to_colorPhPKf(unsigned char*, float const*)':
tmpxft_00003c24_00000000-3_test.cudafe1.cpp:(.text+0x788): multiple definition of `__device_stub__Z14float_to_colorPhPKf(unsigned char*, float const*)'
wfpt.o:tmpxft_00003bc7_00000000-3_wfpt.cudafe1.cpp:(.text+0xb30): first defined here
test.o: In function `float_to_color(unsigned char*, float const*)':
tmpxft_00003c24_00000000-3_test.cudafe1.cpp:(.text+0x7f9): multiple definition of `float_to_color(unsigned char*, float const*)'
wfpt.o:tmpxft_00003bc7_00000000-3_wfpt.cudafe1.cpp:(.text+0xba1): first defined here
test.o: In function `__device_stub__Z14float_to_colorP6uchar4PKf(uchar4*, float const*)':
tmpxft_00003c24_00000000-3_test.cudafe1.cpp:(.text+0x81e): multiple definition of `__device_stub__Z14float_to_colorP6uchar4PKf(uchar4*, float const*)'
wfpt.o:tmpxft_00003bc7_00000000-3_wfpt.cudafe1.cpp:(.text+0xbc6): first defined here
test.o: In function `float_to_color(uchar4*, float const*)':
tmpxft_00003c24_00000000-3_test.cudafe1.cpp:(.text+0x88f): multiple definition of `float_to_color(uchar4*, float const*)'
wfpt.o:tmpxft_00003bc7_00000000-3_wfpt.cudafe1.cpp:(.text+0xc37): first defined here
collect2: error: ld returned 1 exit status
make: *** [cuda_ddm.so] Error 1
编辑:

为了澄清情况,我更改了代码,以100%确定两个文件中没有重叠的代码。我在两个 # include "c_cuda_ddm.hcu" 中都有以下内容:

# ifndef DDM_HEADER
# define DDM_HEADER
#include "book.h"
#include "math.h"
# define TOL 1e-7
# define PI 3.1415926535
# define DIM_X 0
# define DIM_Y 2
# define DIM_U 2
# define DIM_THETA 3
# define DIM_PTHETA 0
# define INDEX_V 0
# define INDEX_A 1
# define INDEX_W 2
# define CUE_LEFT 1
# define CUE_RIGHT 0
# define ANTISACCADE_TYPE 0
# define PROSACCADE_TYPE 1
// Number of threads for the predictive posterior
# define DDMBLOCKS 256
# define PPBLOCKS 1024
# define LLHBLOCKS 16 
# endif
__device__ double lp_ddm(double t, double v, double a, double w);
extern "C"
int llh_ddm(double *t, double *v, double *a, double *w, int ny,
    double *llh);

extern "C"
int llh_stationary_antisaccades(double *x, double *y, double *u,
    double *theta, double *ptheta, int ny, double *llh);
extern "C"
int lpp_stationary_antisaccades(double *x, double *y, double *u,
    double *theta, double *ptheta, int ny, int ns, double *llh);

big_random_block(), float_to_color(一个内核)以及可能所有其他重复的定义都来自book.h。

这个头文件不同于(我认为这是常见的做法)其他头文件,因为它不仅包括函数原型,还包括实际的函数定义。

因此,book.h只能(成功/正确/安全)包含在单个文件(即编译单元)在您的整个项目。如果你将它包含在多个文件中,你将在多个模块中定义相同的函数,这将导致当你试图将这些模块链接在一起时出现问题。

修复是只包括book.h在一个文件中,或者更好的是,只是抓取你需要的,并创建自己的适当组织的头文件。book.h是一个头文件,旨在伴随CUDA的范例书。虽然我确信它对书中的所有项目都有效,但很明显,你不能随便拿起它,把它撒在任何项目上。一些头文件可能是这样工作的。

作为题外话,我希望重申单独的编译(和链接)不能通过仅编译的步骤(-rdc=true -c)来完成。它还需要一个设备链接步骤。如果您的两个目标文件(wfpt.ostationary.o)实际上不共享或不需要任何CUDA符号或入口点,则可能无关紧要。但如果模块之间有共享cuda入口点,则设备链接步骤是必要的。但是,这并不是问题的关键,如果最终需要这样做,您肯定会发现问题中描述的编译顺序是不正确的。

最新更新