Example of CMake file for CUDA+CPP code

Makefiles are quite straightforward and easy to write (in reasonable situations). But GNU Make is not crossplafrom. CMake is cross-platform, cross-application (it can generate projects for different IDEs and Makefile itself).

It also allows you to split source directory and directory with intermediate files and compiled binary. Now CMake natively supports CUDA.

Here is CMakeLists example I use (simply place it next to your source files to try yourself):

CMAKE_MINIMUM_REQUIRED(VERSION 2.8)
PROJECT(lbmslv)

FIND_PACKAGE(CUDA REQUIRED)
FIND_PACKAGE(MPI REQUIRED)

INCLUDE(FindCUDA)

INCLUDE_DIRECTORIES(/usr/local/cuda/include ${MPI_INCLUDE_PATH})

FILE(GLOB SOURCES "*.cu" "*.cpp" "*.c" "*.h")
CUDA_ADD_EXECUTABLE(lbmslv ${SOURCES})

LIST(APPEND CMAKE_CXX_FLAGS "-std=c++0x -O3 -ffast-math -Wall")

LIST(APPEND CUDA_NVCC_FLAGS --compiler-options -fno-strict-aliasing -lineinfo -use_fast_math -Xptxas -dlcm=cg)
LIST(APPEND CUDA_NVCC_FLAGS -gencode arch=compute_20,code=sm_20)
LIST(APPEND CUDA_NVCC_FLAGS -gencode arch=compute_30,code=sm_30)
LIST(APPEND CUDA_NVCC_FLAGS -gencode arch=compute_35,code=sm_35)

TARGET_LINK_LIBRARIES(lbmslv /usr/local/cuda/lib64/libcudart.so ${MPI_LIBRARIES})

This file automatically adds all sources in the same directory it is placed. I’ve also included lines for newer GPU architectures (feel free to uncomment them). I prefer to choose one to reduce compilation time. It aso switches on new (phah, new for 3 years :)) C++ standart.

So, create CMakeLists.txt file and place it in your sources directory (say src). Then create bin (choose another name if you want) directory next to src. Change directory to bin, generate Makefile from CMakeLists.txt and build it with ordinary make:

cd bin
cmake ../src
make

I’ve updated MPI part. Now it is correct.