I am looking for help getting started with a project involving CUDA. My goal is to have a project that I can compile in the native g++ compiler but uses CUDA code. I understand that I have to compile my CUDA code in nvcc compiler, but from my understanding I can somehow compile the CUDA code into a cubin file or a ptx file.
Here are my questions:
Assume the following:
Using the CUDA Toolkit you can accelerate your C or C++ applications by updating the computationally intensive portions of your code to run on GPUs. To accelerate your applications, you can call functions from drop-in libraries as well as develop custom applications using languages including C, C++, Fortran and Python.
In order to compile CUDA code files, you have to use nvcc compiler. Cuda codes can only be compiled and executed on node that have a GPU. Heracles has 4 Nvidia Tesla P100 GPUs on node18. Cuda Compiler is installed on node 18, so you need ssh to compile cuda programs.
You need to check "CUDA 5.0" here. Set up the "Additional Include Directories" for Cuda in the "General" Tab of the Compiler options (Project Properties). Add the cuda libfile to "Additional Dependencies" in the "Input" Tab of the Linker. Mark the File as Cuda file (you've done that).
The CUDA/C++ compiler nvcc is used only to compile the CUDA source file, and the MPI C compiler mpicc is used to compile the C code and to perform the linking.
I was able to resolve my issue with a couple of different posts including these ones. Don't forget that if you are using a 64 bit machine to link to the 64 bit library! It seams kind of obvious, but for clowns like me, that is something I forgot. Here is the make file that I now use... if you can digest this make file, you should be able to do what I was trying to do which was separate compilation of cuda code and other G++ code. Also keep in mind that you have to have the gcc, g++ compilers at certain versions (I am using g++-4.4 and it is working for me) Anyway, here is the make file...
all: program program: cudacode.o g++ -o program -L/usr/local/cuda/lib64 -lcuda -lcudart main.cpp cudacode.o cudacode.o: nvcc -c -arch=sm_20 cudacode.cu clean: rm -f *.o program
Hopefully you can see that the first thing I do is compile the cudacode (that has been saved as a .cu
) using the nvcc
compiler and -c
option (also note that you may want to remove the -arch=sm_20
). This created a cudacode.o
. I then use the g++ compiler with the -o
option and link to the lib64 library and link the -lcuda
and -lcudart
libraries along with compiling my main.cpp
and then linking the cudacode.o
. Hope this helps someone!
My answer to this recent question likely describes what you need.
A couple of additional notes:
.cu
to a .cubin
or .ptx
file. You need to compile it to a .o
object file and then link it with the .o
object files from your .cpp files compiled with g++.cudaFunc.cu
, you also need to put a C or C++ wrapper function in that file that launches the kernel (unless you are using the CUDA driver API, which is unlikely and not recommended). Also add a header file with the prototype of this wrapper function so that you can include it in your C++ code which needs to call the CUDA code. Then you link the files together using your standard g++ link line.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With