在调查可重定位设备代码的一些问题时,我偶然发现了一些我不太理解的东西。
这是如图所示的用例slide 6 http://on-demand.gputechconf.com/gtc-express/2012/presentations/gpu-object-linking.pdf#6。我用了一个罗伯特·克罗维拉的回答 https://stackoverflow.com/a/17287863/1043187作为重现代码的基础。我们的想法是,我们将一些可重定位设备代码编译到静态库(例如一些数学/工具箱库)中,并且我们希望将该预编译库的一些函数使用到我们程序的另一个设备库中:
libutil.a ---> libtest.so ---> test_pgm
假设这个外部库包含以下函数:
__device__ int my_square (int a);
libutil.a
例如生成如下(在另一个项目中):
nvcc ${NVCC_FLAGS} -dc util.cu
nvcc ${NVCC_FLAGS} -dlink util.o -o util_dlink.o
nvcc ${NVCC_FLAGS} -lib util_dlink.o util.o -o libutil.a
然后,在我们的项目中,生成libtest.so
:
nvcc ${NVCC_FLAGS} -dc test.cu
nvcc ${NVCC_FLAGS} -dlink test.o libutil.a -o test_dlink.o
g++ -shared -Wl,-soname,libtest.so -o libtest.so test.o test_dlink.o libutil.a -L${CUDA_LIBDIR} -lcudart
但生成时出现以下错误test_dlink.o
:
nvlink error : Undefined reference to '_Z9my_squarei' in 'test.o'
链接器找不到我们的虚拟对象my_square(int)
功能。如果我们改为使用(假设我们有权访问util.o
):
nvcc ${NVCC_FLAGS} -dlink test.o util.o -o test_dlink.o
链接器成功,之后一切正常。
进一步调查:
$ nm -C libutil.a
util_dlink.o:
U atexit
U __cudaRegisterFatBinary
0000000000000015 T __cudaRegisterLinkedBinary_39_tmpxft_0000106a_00000000_6_util_cpp1_ii_843d693d
...
util.o:
U __cudaInitModule
U __cudaRegisterLinkedBinary_39_tmpxft_0000106a_00000000_6_util_cpp1_ii_843d693d
...
0000000000000015 T my_square(int)
...
该符号位于存档中util.o
, but nvlink
(由nvcc
)好像没找到。这是为什么?根据官方文档 http://docs.nvidia.com/cuda/cuda-compiler-driver-nvcc/index.html#libraries:
设备链接器具有读取静态主机库的能力
格式(Linux 和 Mac 上为 .a,Windows 上为 .lib)。
我们当然可以提取目标文件并与其链接:
ar x libutil.a `ar t libutil.a | grep -v "dlink"`
nvcc ${NVCC_FLAGS} -dlink test.o util.o -o test_dlink.o
但这感觉不像预期的解决方案......那么我在这里错过了什么?其他nvcc
解决这个问题的选项?生成时是否有错误libutil.a
and/or libtest.so
?
请注意,这是在 Arch Linux 上使用 CUDA 6.5 进行测试的。
编辑:修复带有注释行的重现代码
Makefile
NVCC_FLAGS=-m64 -arch=sm_20 -Xcompiler '-fPIC'
CUDA_LIBDIR=${CUDA_HOME}/lib64
testmain : main.cpp libtest.so
g++ -c main.cpp
g++ -o testmain -L. -ldl -Wl,-rpath,. -ltest -L${CUDA_LIBDIR} -lcudart main.o
libutil.a : util.cu util.cuh
nvcc ${NVCC_FLAGS} -dc util.cu
# ---> FOLLOWING LINES THAT WERE WRONG <---
# nvcc ${NVCC_FLAGS} -dlink util.o -o util_dlink.o
# nvcc ${NVCC_FLAGS} -lib util.o util_dlink.o -o libutil.a
# INSTEAD:
nvcc ${NVCC_FLAGS} -lib util.o -o libutil.a
# Assuming util is an external library, so util.o is not available
rm util.o
libtest.so : test.cu test.h libutil.a util.cuh
nvcc ${NVCC_FLAGS} -dc test.cu
# Use NVCC for device linking + G++
nvcc -v ${NVCC_FLAGS} -dlink test.o libutil.a -o test_dlink.o
g++ -shared -o libtest.so test.o test_dlink.o libutil.a -L${CUDA_LIBDIR} -lcudart
# Or let NVCC generate the shared library
#nvcc -v ${NVCC_FLAGS} -shared -L. -lutil test.o -o libtest.so
clean :
rm -f testmain *.o *.a *.so
test.h
#ifndef TEST_H
# define TEST_H
int my_test_func();
#endif //! TEST_H
test.cu
#include <stdio.h>
#include "test.h"
#include "util.cuh"
#define DSIZE 1024
#define DVAL 10
#define SQVAL 3
#define nTPB 256
#define cudaCheckErrors(msg) \
do { \
cudaError_t __err = cudaGetLastError(); \
if (__err != cudaSuccess) { \
fprintf(stderr, "Fatal error: %s (%s at %s:%d)\n", \
msg, cudaGetErrorString(__err), \
__FILE__, __LINE__); \
fprintf(stderr, "*** FAILED - ABORTING\n"); \
exit(1); \
} \
} while (0)
__global__ void my_kernel(int *data){
int idx = threadIdx.x + (blockDim.x *blockIdx.x);
if (idx < DSIZE) data[idx] =+ DVAL + my_square (SQVAL);
}
int my_test_func()
{
int *d_data, *h_data;
h_data = (int *) malloc(DSIZE * sizeof(int));
if (h_data == 0) {printf("malloc fail\n"); exit(1);}
cudaMalloc((void **)&d_data, DSIZE * sizeof(int));
cudaCheckErrors("cudaMalloc fail");
for (int i = 0; i < DSIZE; i++) h_data[i] = 0;
cudaMemcpy(d_data, h_data, DSIZE * sizeof(int), cudaMemcpyHostToDevice);
cudaCheckErrors("cudaMemcpy fail");
my_kernel<<<((DSIZE+nTPB-1)/nTPB), nTPB>>>(d_data);
cudaDeviceSynchronize();
cudaCheckErrors("kernel");
cudaMemcpy(h_data, d_data, DSIZE * sizeof(int), cudaMemcpyDeviceToHost);
cudaCheckErrors("cudaMemcpy 2");
for (int i = 0; i < DSIZE; i++)
if (h_data[i] != DVAL + SQVAL*SQVAL)
{
printf("Results check failed at offset %d, data was: %d, should be %d\n",
i, h_data[i], DVAL);
exit(1);
}
printf("Results check passed!\n");
return 0;
}
util.cuh
#ifndef UTIL_CUH
# define UTIL_CUH
__device__ int my_square (int a);
#endif //! UTIL_CUH
util.cu
#include "util.cuh"
__device__ int my_square (int a)
{
return a * a;
}
main.cpp
#include "test.h"
int main()
{
my_test_func();
return 0;
}