Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to link host code with a static CUDA library after separable compilation?

Alright, I have a really troubling CUDA 5.0 question about how to link things properly. I'd be really grateful for any assistance!

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.

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

undefined reference to __cudaRegisterLinkedBinary

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?

like image 527
user2333829 Avatar asked Apr 29 '13 22:04

user2333829


1 Answers

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

There is an example of exactly this in the Using Separate Compilation chapter of the nvcc doc.

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.

Edit:

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
like image 82
harrism Avatar answered Oct 01 '22 17:10

harrism