Create CUDA .so lib file

Hi, does anyone know how to create CUDA .so dynamically linked library file?

I’m using CMake (www.cmake.org) and findcuda.cmake (http://www.sci.utah.edu/~abe/FindCuda.html)

It works great and have no problem building libraries (shared or static) with it.

Have fun :-)

thanks for that.

I was trying to use extern “C” to interface the kernel function which is in the .cu file with the rest of the code in .c file. I generated .o for the .cu file with NVCC, the used scons to try to link the .c and .o file together. I had the function prototype in c file and declared extern “C” in both files, also inlcuded the .cu file in the c file. But here are the first few lines of errors:

scons: Reading SConscript files …

scons: done reading SConscript files.

scons: Building targets …

gcc -o myMatrixMult2.os -c -O2 -Wall -ansi -pedantic -I/usr/local/cuda/include “-L/usr/local/cuda/lib -lcudart” -fPIC myMatrixMult2.c

In file included from myMatrixMult2.c:9:

myMatrixMult2_kernel.cu: In function ‘multiplyKernel’:

myMatrixMult2_kernel.cu:32: error: ‘blockIdx’ undeclared (first use in this function)

Any help would be appraciated, thanks.

Hi, do I need both of those tools?

I tried the findcuda.cmake.

I got confused and the documentation seems to be insufficient.

So far I can only generate the executable and .a files for the example code, but how do I make it point to the directory where my .cu files are?

After I get the .a file from findchua.make, do I then use Cmake to generate .so file?

Can you give me a little guidance on using these tools to generate .so file from .cu files?

Thanks a lot.

No, CMake does everything for you. You just define what files you want linked together. To generate a shared library:

CUDA_ADD_LIBRARAY(mylib SHARED file1.cu file2.cu file3.cu my_cp_file.cc ...)

And if you want a static:

CUDA_ADD_LIBRARAY(mylib STATIC file1.cu file2.cu file3.cu my_cpp_file.cc ...)

CUDA_ADD_LIBRARY works just like ADD_LIBRARY in the normal CMake but it accepts .cu files to be compiled with nvcc as well as cpp or C files.

Thanks for that, I’ve successfully generated the .so file.

One issue I have is right now I have to copy my source file into the CMake-cuda/src directory to be able to build them, is that how it’s supposed to be done or is there another proper way of doing this?

Thanks.

If you look at the documentation for find_package: http://www.cmake.org/HTML/cmake-2.6.html#c…nd:find_package it searches in CMAKE_MODULE_PATH.

set(CMAKE_MODULE_PATH

    "/some/path/to/CMake/cuda"

    "${CMAKE_MODULE_PATH}")

It doesn’t seem to be linking with libcublas.so or the cufft shared lib.

I am getting undefined symbol error for cublas functions.

So which of configuration settings in CMake-cuda do I need to change?

I think I need to add ‘-L/usr/local/cuda/lib -lcublas -lcufft’ somewhere but I am not really sure.

Appreciate your help, thanks.

It seems that the Numpy array with a Ctype pointer won’t let cublasGetMatrix touch it.

Any ideas?

It is also not linking with the 10,000 other libraries on your system: why should it link to something unless you tell it to?

Please see the CMake documentation, again (or is it the first time?). The command you want this time is target_link_libraries.

I have created .so file. I want to link this file to c program but there is some linking error as below :

/tmp/cc9yA33G.o: In function main': main.c:(.text+0x24): undefined reference to my_cuda_func’
collect2: ld returned 1 exit status

I have created 3 program files:

  1. main.c
  2. add.cu
  3. add.h

CUDA is a language in the C++ family. Standard caveats for C/C++ interop therefore apply. In particular, to make a function in C++ code callable from C, it must be declared as extern “C”.