Mixing CUDA and C++
•
2 Mar 2011, 20:08
•
Tutorials
Hello,
Another programming tutorial coming up...
CUDA
For those who don't know CUDA is an Nvidia library/tool for allowing you to run programs on a graphics card. A bit like OpenCL but more proprietary. It allows you to run lots of lightweight threads in parallel which can speed up tasks like Vector maths but is limited in terms memory access.
C++
Programming language.... nuff said.
Mixing
You might want to integrate CUDA into an existing program to speed up certain parts by performing tasks in parallel. When mixing together make sure you wrap your CUDA code (typically in a .cu file) with functions declared in a header (.h) which doesn't mention any CUDA primitives or types atall.
Steps
1. Separate out raw C++ code from CUDA code so that there is essentially a wrapper around everything CUDA related
2. Compile CUDA related stuff with NVCC and the rest with g++
3. Link with NVCC
You can use NVCC to compile C/C++ code but it doesn't seem to recognise all the flags g++ uses and gives errors
You can't link with g++ as it won't have all the CUDA primitives and symbols (which is sensible)
cuda_code.h is contains handles and contains nothing CUDA related!!
# My makefile
OBJECTS = cpp_code.o cuda_code.o
LINKEROPTIONS = -lm -lGL
NVCCOPTIONS = -arch sm_11
program : $(OBJECTS)
nvcc $(OBJECTS) -o program $(LINKEROPTIONS)
cpp_code.o : cpp_code.cc cpp_code.h
g++ -c dep1.cc
cuda_code.o : cuda_code.cu cuda_code.h
nvcc -c cuda_code.cu $(NVCCOPTIONS)
Notes
Tutorial aimed at more experienced programmers using linux
For those who are into programming i'd urge you to look at CUDA its pretty cool ;-)
Another programming tutorial coming up...
CUDA
For those who don't know CUDA is an Nvidia library/tool for allowing you to run programs on a graphics card. A bit like OpenCL but more proprietary. It allows you to run lots of lightweight threads in parallel which can speed up tasks like Vector maths but is limited in terms memory access.
C++
Programming language.... nuff said.
Mixing
You might want to integrate CUDA into an existing program to speed up certain parts by performing tasks in parallel. When mixing together make sure you wrap your CUDA code (typically in a .cu file) with functions declared in a header (.h) which doesn't mention any CUDA primitives or types atall.
Steps
1. Separate out raw C++ code from CUDA code so that there is essentially a wrapper around everything CUDA related
2. Compile CUDA related stuff with NVCC and the rest with g++
3. Link with NVCC
You can use NVCC to compile C/C++ code but it doesn't seem to recognise all the flags g++ uses and gives errors
You can't link with g++ as it won't have all the CUDA primitives and symbols (which is sensible)
cuda_code.h is contains handles and contains nothing CUDA related!!
# My makefile
OBJECTS = cpp_code.o cuda_code.o
LINKEROPTIONS = -lm -lGL
NVCCOPTIONS = -arch sm_11
program : $(OBJECTS)
nvcc $(OBJECTS) -o program $(LINKEROPTIONS)
cpp_code.o : cpp_code.cc cpp_code.h
g++ -c dep1.cc
cuda_code.o : cuda_code.cu cuda_code.h
nvcc -c cuda_code.cu $(NVCCOPTIONS)
Notes
Tutorial aimed at more experienced programmers using linux
For those who are into programming i'd urge you to look at CUDA its pretty cool ;-)
you're the true awesomener of the awesomeners.
YES!
xx<3