我目前正在尝试使用 CUDA 例程创建一个库,但遇到了麻烦。我将使用一个相当小的示例来解释我的问题,我的实际库会更大。
我已经成功编写了 test.cu
,这是一个包含 __global__
CUDA 函数及其包装器的源文件(用于分配和复制内存)。我还可以使用以下命令成功地将这个文件编译成共享库:
nvcc -c test.cu -o test.o -lpthread -lrt -lcuda -lcudart -Xcompiler -fPIC
gcc -m64 -shared -fPIC -o libtest.so test.o -lpthread -lrt -lcuda -lcudart -L/opt/cuda/lib64
生成的libtest.so
导出我需要的所有符号。
我现在编译我的纯 C main.c
并将其链接到我的库:
gcc -std=c99 main.c -o main -lpthread -ltest -L.
这一步也成功了,但是在执行 ./main
时,所有被调用的 CUDA 函数都会返回错误:
test.cu:17:cError(): cudaGetDeviceCount: [38] no CUDA-capable device is detected
test.cu:17:cError(): cudaMalloc: [38] no CUDA-capable device is detected
test.cu:17:cError(): cudaMemcpy: [38] no CUDA-capable device is detected
test.cu:17:cError(): cudaMemcpy: [38] no CUDA-capable device is detected
test.cu:17:cError(): cudaFree: [38] no CUDA-capable device is detected
(错误消息是通过我自己的调试功能创建的)
在最初的步骤中,我遇到了完全相同的问题,因为我直接从 test.cu
创建可执行文件,因为我忘记链接 libpthread (-lpthread
)。但是,正如您在上面看到的,我已将所有源文件链接到 libpthread。根据ldd
,libtest.so
和main
都依赖于libpthread,这是理所应当的。
我在 ArchLinux 上使用 CUDA 5(是的,我确实意识到它是测试版)以及 gcc 4.6.3 和 nvidia 驱动程序版本 302.06.03。
如果能帮助解决这个问题,我们将不胜感激!
最佳答案
这是一个简单的例子......
// File: test.cu
#include <stdio.h>
__global__ void myk(void)
{
printf("Hello from thread %d block %d\n", threadIdx.x, blockIdx.x);
}
extern "C"
void entry(void)
{
myk<<<1,1>>>();
printf("CUDA status: %d\n", cudaDeviceSynchronize());
}
编译/链接nvcc -m64 -arch=sm_20 -o libtest.so --shared -Xcompiler -fPIC test.cu
.
// File: main.c
#include <stdio.h>
void entry(void);
int main(void)
{
entry();
}
编译/链接gcc -std=c99 -o main -L. -ltest main.c
.
关于gcc - 创建 CUDA 共享库和 libpthread 的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11885565/