Makefiles are quite straightforward and easy to write (in reasonable situations). But GNU Make is not cross-platform. CMake is cross-platform and cross-application (it can generate projects for different IDEs and the Makefile itself).
It also allows you to split the source directory from the directory with intermediate files and the compiled binary. Now CMake natively supports CUDA.
Here is CMakeLists.txt example I use (simply place it next to your source files to try yourself):
1CMAKE_MINIMUM_REQUIRED(VERSION 2.8)
2PROJECT(lbmslv)
3
4FIND_PACKAGE(CUDA REQUIRED)
5FIND_PACKAGE(MPI REQUIRED)
6
7INCLUDE(FindCUDA)
8
9INCLUDE_DIRECTORIES(/usr/local/cuda/include ${MPI_INCLUDE_PATH})
10
11FILE(GLOB SOURCES "*.cu" "*.cpp" "*.c" "*.h")
12CUDA_ADD_EXECUTABLE(lbmslv ${SOURCES})
13
14LIST(APPEND CMAKE_CXX_FLAGS "-std=c++0x -O3 -ffast-math -Wall")
15
16LIST(APPEND CUDA_NVCC_FLAGS --compiler-options -fno-strict-aliasing -lineinfo -use_fast_math -Xptxas -dlcm=cg)
17LIST(APPEND CUDA_NVCC_FLAGS -gencode arch=compute_20,code=sm_20)
18LIST(APPEND CUDA_NVCC_FLAGS -gencode arch=compute_30,code=sm_30)
19LIST(APPEND CUDA_NVCC_FLAGS -gencode arch=compute_35,code=sm_35)
20
21TARGET_LINK_LIBRARIES(lbmslv /usr/local/cuda/lib64/libcudart.so ${MPI_LIBRARIES})
This file automatically adds all sources in the directory where 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 also switches on the new (hah, new for 3 years :)) C++ standard.
So, create the CMakeLists.txt file and place it in your sources directory (say src). Then create a bin directory (choose another name if you want) next to src. Change directory to bin, generate the Makefile from CMakeLists.txt, and build it with ordinary make:
1cd bin
2cmake ../src
3make
I’ve updated the MPI part.