可分离编译后如何将主机代码与静态 CUDA 库链接? [英] How to link host code with a static CUDA library after separable compilation?
问题描述
好的,关于如何正确链接事物,我有一个非常令人不安的 CUDA 5.0 问题.如果能提供任何帮助,我将不胜感激!
Alright, I have a really troubling CUDA 5.0 question about how to link things properly. I'd be really grateful for any assistance!
使用 CUDA 5.0 的可分离编译特性,我生成了一个静态库 (*.a).通过 nvcc 运行时,这很好地与其他 *.cu 文件链接,我已经这样做了很多次了.
Using the separable compilation features of CUDA 5.0, I generated a static library (*.a). This nicely links with other *.cu files when run through nvcc, I have done this many times.
我现在想获取一个 *.cpp 文件,并使用 g++ 或其他方式将其链接到此静态库中的主机代码,但不使用 nvcc.如果我尝试这样做,我会收到类似
I'd now like to take a *.cpp file and link it against the host code in this static library using g++ or whatever, but not nvcc. If I attempt this, I get compiler errors like
对 __cudaRegisterLinkedBinary 的未定义引用
undefined reference to __cudaRegisterLinkedBinary
我同时使用 -lcuda
和 -lcudart
并且据我所知,这些库的顺序正确(意思是 -lmylib -lcuda -lcudart
).我不认为这是一个问题.也许我错了,但我觉得我错过了一步,我需要对我的静态库(设备链接?)做其他事情,然后才能将它与 g++ 一起使用.
I'm using both -lcuda
and -lcudart
and, to my knowledge, have the libraries in the correct order (meaning -lmylib -lcuda -lcudart
). I don't think it is an issue with that. Maybe I'm wrong, but I feel I'm missing a step and that I need to do something else to my static library (device linking?) before I can use it with g++.
我错过了什么重要的事情吗?这甚至可能吗?
Have I missed something crucial? Is this even possible?
额外问题:我希望最终结果是一个动态库.我怎样才能做到这一点?
Bonus question: I want the end result to be a dynamic library. How can I achieve this?
推荐答案
当你与 nvcc 链接时,它会与主机链接一起进行隐式设备链接.如果您使用主机编译器进行链接(如使用 g++),则需要添加显式步骤以使用 –dlink 选项进行设备链接,例如
When you link with nvcc, it does an implicit device link along with the host link. If you use the host compiler to link (like with g++), then you need to add an explicit step to do a device link with the –dlink option, e.g.
nvcc –arch=sm_35 –dc a.cu b.cu
nvcc –arch=sm_35 –dlink a.o b.o –o dlink.o
g++ a.o b.o dlink.o x.cpp –lcudart
目前我们只支持可重定位设备代码的静态库.我们有兴趣了解您希望如何在动态库中使用此类代码.请随时在评论中回答.
Currently we only support static libraries for relocatable device code. We’d be interested in learning how you would want to use such code in a dynamic library. Please feel free to answer in the comments.
回答下面评论中的问题有没有办法使用nvcc将mylib.a变成可以放入g++的东西?"
To answer the question in the comment below " Is there any way to use nvcc to turn mylib.a into something that can be put into g++?"
只需像使用对象一样使用库,如下所示:
Just use the library like an object, like this:
nvcc –arch=sm_35 –dlink mylib.a –o dlink.o
g++ mylib.a dlink.o x.cpp –lcudart
这篇关于可分离编译后如何将主机代码与静态 CUDA 库链接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!