Sunday, May 9, 2010

CUDA compilation on Linux (Makefiles)

I have been struggling with compiling complex CUDA code, with complex I mean a lot of .cu .F .cpp and .c files. I have created a makefile that can help you compile that kind of code. I'll try to give a quick view of it here explaining some parts:

First you'll need to specify the CUDA path and the compilers and linkers. Here I am using gcc and g++ for .c and .cpp files and nvcc for .cu files:

CUDA_INSTALL_PATH ?= /usr/local/cuda

CXX := g++
CC := gcc
LINK := g++ -fPIC
NVCC := nvcc -ccbin /usr/bin

Then you need to specify where to locate the CUDA header files :

# Includes

# Common flags

Then you specify where to locate the CUDA binaries for linking :

LIB_CUDA := -L$(CUDA_INSTALL_PATH)/lib -lcudart

Then you specify the object files that the executable depends on and the name of the executable flle:

OBJS = sample_cpp_objectfile.cpp.o
TARGET = exec

Finally you specify the compilation rules. The following are generic rules so all you need to do is change the OBJS and TARGET variables for compilation:

.SUFFIXES: .c .cpp .cu .o

%.c.o: %.c
$(CC) $(CFLAGS) -c $< -o $@
$(NVCC) $(NVCCFLAGS) -c $< -o $@

%.cpp.o: %.cpp
$(CXX) $(CXXFLAGS) -c $< -o $@

$(TARGET): $(OBJS) Makefile

No comments:

Post a Comment