How to use cmake to compile both C++ file and CUDA file - c++

I know how to use cmake to compile C++ file with cuda function like cublas. However, in my project, there is a kernel function that I write by myself. I see the g++ compiler do not know how to deal with the kernel<<<,>>>. Could you please help me fix this problem? The following is the CMakeLists that I used to compile the C++ files with cublas.
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
set(CMAKE_CXX_FLAGS_DEBUG "-g")
cmake_minimum_required(VERSION 3.0)
project(GPU_LMM)
find_package(GSL REQUIRED)
find_package(BLAS REQUIRED)
find_package(CUDA)
if (CUDA_FOUND)
message("CUDA found")
else()
message("CUDA not found, doing something alternatively")
endif()
include_directories(test_cuda PRIVARE
${GSL_INCLUDE_DIRS}
${BLAS_INCLUDE_DIRS}
${CUDA_INCLUDE_DIRS}
${CUDA_CUBLAS_DIRS}
${PROJECT_SOURCE_DIR})
add_executable(GPU_LMM main.cpp aux.cpp)
target_link_libraries( GPU_LMM PRIVATE
${GSL_LIBRARY}
${BLAS_LIBRARIES}
${CUDA_LIBRARIES}
${CUDA_CUBLAS_LIBRARIES})
I have three files to compile as the following.
main.cpp aux.cpp aux.hpp
The aux.cpp contains the cuda kernel function.
Thank your everybody!

If you have cuda kernels that have to be compiled with nvcc, their extension should be .cu, no .cpp (so that should be aux.cu). Then cmake will use the proper compiler for the proper file.
Since cmake 3.10.0, CUDA support is even native, you just need to activate the language.

Related

pybind11 and another libs in one project [duplicate]

I want to be able to call my C++ code as a python package. To do this I am using pybind11 with CMakelists (following this example https://github.com/pybind/cmake_example). My problem is that I have to include GSL libraries in the compilation of the code, and these need an explicit linker -lgsl .
If I were just to compile and run the C++ without wrapping it with python, the following Cmakelists.txt file does the job
cmake_minimum_required(VERSION 3.0)
set(CMAKE_BUILD_TYPE Debug)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++14")
project(myProject)
add_executable(
myexecutable
main.cpp
function1.cpp
)
find_package(GSL REQUIRED)
target_link_libraries(myexecutable GSL::gsl GSL::gslcblas)
but when using pybind11 the template I found doesn't allow the add_executable therefore target_link_libraries doesn't work.
I have trie this
project(myProject)
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED YES) # See below (1)
# Set source directory
set(SOURCE_DIR "project")
# Tell CMake that headers are also in SOURCE_DIR
include_directories(${SOURCE_DIR})
set(SOURCES "${SOURCE_DIR}/functions.cpp")
# Generate Python module
add_subdirectory(lib/pybind11)
pybind11_add_module(namr ${SOURCES} "${SOURCE_DIR}/bindings.cpp")
FIND_PACKAGE(GSL REQUIRED)
target_link_libraries(GSL::gsl GSL::gslcblas)
but this produces errors in the building.
Any idea ?
Function pybind11_add_module creates a library target, which can be used for link added module with other libraries:
pybind11_add_module(namr ${SOURCES} "${SOURCE_DIR}/bindings.cpp")
target_link_libraries(namr PUBLIC GSL::gsl GSL::gslcblas)
This is explicitely stated in documentation:
This function behaves very much like CMake’s builtin add_library (in fact, it’s a wrapper function around that command). It will add a library target called <name> to be built from the listed source files. In addition, it will take care of all the Python-specific compiler and linker flags as well as the OS- and Python-version-specific file extension. The produced target <name> can be further manipulated with regular CMake commands.

Separate CUDA compilation with CMAKE

I want to compile .cu and .cpp to .o files separately, then link them to executable. I have few simple files: cuda_func.cu. cuda_func.h and main.cpp. In main cpp I include cuda_func.h and run cuda_func(). I've come up with following cmake code:
project(cuda)
cmake_minimum_required(VERSION 2.8)
# CUDA PACKAGE
find_package(CUDA REQUIRED)
set(CUDA_SEPARABLE_COMPILATION ON)
set(CUDA_PROPAGATE_HOST_FLAGS OFF)
set(CUDA_HOST_COMPILER g++)
# COMPILE CU FILES
file(GLOB CUDA_FILES *.cu)
list( APPEND CUDA_NVCC_FLAGS "-gencode arch=compute_30,code=sm_30; -std=c++11")
CUDA_COMPILE(CU_O ${CUDA_FILES})
SET(CMAKE_EXE_LINKER_FLAGS "-L/usr/local/cuda/lib -lcudart")
# SETUP FOR CPP FILES
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
include_directories(${CMAKE_CURRENT_SOURCE_DIR})
# COMPILE AND LINK
add_executable(main main.cpp ${CU_O})
But I get undefined reference to "cudaMemcpy" error. When I compile it by hand, using nvcc and g++ to get .o files and g++ finally to make executable it works fine. It seems like the cuda library isnt linked properly at the end. What should I do?
Cmake 3.8 added native support for CUDA (here is a document from NVIDIA that explains how to use it this way), you can simply add cuda as a language and the appropriate compiler will be used automatically for each file.
cmake_minimum_required (VERSION 3.8)
project(youProject LANGUAGES CXX CUDA)
add_executable(yourExecutable)
target_sources(yourExecutable <you can add c++ as well as cuda files here>)
// Or if you want to separate cuda and C++ more clearly, put your cuda code
// in a library and link to it (which obviously allows you to compile them separately)
add_library(yourCudaLib <you can add your cuda files here>)
target_link_libraries(yourProject PRIVATE yourCudaLib)

Linking of CUDA library in CMake

I am using CMake 3.10 and have a problem linking a compiled library to a test executable in CMake.
I searched a lot and found that in earlier versions there was a problem where you could not link intermediate libraries in the result executable. I was not able to tell if this was resolved or still an issue.
My CMake files look like this:
Algo:
cmake_minimum_required (VERSION 3.9)
project(${MODULE_NAME}_core LANGUAGES CXX CUDA)
add_subdirectory("${core_impl_dir}" implementation)
set(cuda_src "parallel/ParallelComputation.cu")
set(cuda_hdr "parallel/ParallelComputation.h")
add_library(${PROJECT_NAME} STATIC "${cuda_src}" "${cuda_hdr}"
)
target_include_directories (${PROJECT_NAME} PUBLIC "include/"
"parallel/"
)
source_group("parallel" FILES "${cuda_src}" "${cuda_hdr}")
set_property(TARGET ${PROJECT_NAME} PROPERTY FOLDER ${MODULE_NAME})
Test:
project(${MODULE_NAME}_gtest LANGUAGES CXX CUDA)
add_subdirectory("${gtest_impl_dir}" implementation)
add_executable(${PROJECT_NAME} "${gtest_impl_src}")
target_link_libraries(${PROJECT_NAME} ${MODULE_NAME}_core)
enable_testing()
find_package(GTest REQUIRED)
include_directories("${GTEST_INCLUDE_DIRS}")
target_link_libraries(${PROJECT_NAME} ${GTEST_BOTH_LIBRARIES})
source_group("Implementation\\Source Files" FILES "${gtest_impl_src}" )
set_property(TARGET ${PROJECT_NAME} PROPERTY FOLDER ${MODULE_NAME})
add_test(${PROJECT_NAME} ${PROJECT_NAME})
Building just Algo works fine, but when also building Test, I get linking errors, for example
../implementation/libmatrix1_testCuda_core.a(ParallelComputation.cu.o): In Funktion 'cudaError cudaMalloc(float**, unsigned long)':
tmpxft_00005ad0_00000000-5_ParallelComputation.cudafe1.cpp:(.text+0x4f2): Undefined reference 'cudaMalloc'
EDIT
using make VERBOSE=1 I got this linking command:
/usr/bin/c++ -Wl,--no-as-needed -pthread -g -std=c++14 -Wall
CMakeFiles/matrix1_testCuda_gtest.dir//tests/eclipseProject/algos/testCuda/test/src/main.cpp.o
CMakeFiles/matrix1_testCuda_gtest.dir/cmake_device_link.o -o
matrix1_testCuda_gtest ../implementation/libmatrix1_testCuda_core.a
/usr/lib/libgtest.a /usr/lib/libgtest_main.a
I got this to work by calling
find_package(CUDA 9.0 REQUIRED)
in both CMake files.
Also, in the Algo file (which contains the device code), I had to do
target_link_libraries(${PROJECT_NAME} ${CUDA_LIBRARIES})
I was expecting that the language support for CUDA would make those steps unnecessary, but apparently not.
I just ran into something very similar to this where the root problem was that most of my binary was being compiled with my system cxx compiler, and the cuda bits were being compiled with the cuda gcc compiler (9.1 for system, 8.3 for cuda).
Surprisingly it was fixed by changing:
project(MyProject LANGUAGES CXX CUDA)
to
project(MyProject LANGUAGES CUDA CXX)
After that change, CMake picked up the cuda version of the gcc compiler as the main compiler, and my binary started building again. I'm not sure if this could introduce problems for other packages, but it fixed the linking problem I was hitting.
Adding the possible way in CMake 3.18 and further
When you wish not to include any CUDA code, but e.g. using only calls to cufft from C++ it is sufficient to do the following
find_package(CUDAToolkit)
target_link_libraries(project CUDA::cudart)
target_link_libraries(project CUDA::cufft)
If you are however enabling CUDA support, unless you want to get into troubles call it after enabling CUDA.
include(CheckLanguage)
check_language(CUDA)
if(CMAKE_CUDA_COMPILER)
enable_language(CUDA)
find_package(CUDAToolkit)
target_link_libraries(project CUDA::cudart)
target_link_libraries(project CUDA::cuda_driver)
else()
message(STATUS "No CUDA compiler found")
endif()
Because CUDAToolkit respects enable CUDA runtime added but it does not work vice versa.

Build CUDA library using Xcode and CMake fail

I am trying to use CMake to generate a Xcode project and build a CUDA library with it.
The code I used for collecting & building CUDA library "caffe2_cpp_gpu" is as follows:
list(APPEND CUDA_NVCC_FLAGS "-std=c++11" "-Wno-deprecated-gpu-targets")
file(GLOB CUDA_SOURCES "${PROJECT_SOURCE_DIR}/source/caffe2/operator/*.cu" )
cuda_add_library(caffe2_cpp_gpu STATIC ${CUDA_SOURCES})
if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang" OR "${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang++" OR "${CMAKE_CXX_COMPILER_ID}" STREQUAL "AppleClang")
list(APPEND ALL_LIBRARIES -Wl,-force_load caffe2_cpp_gpu)
else()
list(APPEND ALL_LIBRARIES -Wl,--whole-archive caffe2_cpp_gpu -Wl,--no-whole-archive)
endif()
Note that this code can be run if Unix Makefile is used.
But cannot run if Xcode is used.
In the project it shows a file is missing:
missing file
And the CUDA library "caffe2_cpp_gpu" cannot be created.
Does anyone know how to solve it?
I found the answer...
If I append another kind of file (.cc, .h, etc.) into the CUDA libary, it will work. It seems like XCode cannot build single .cu file into static library (.a).
So here is what I did:
file(GLOB All_SOURCE "${PROJECT_SOURCE_DIR}/source/caffe2/operator/*.cu" "${PROJECT_SOURCE_DIR}/source/caffe2/operator/*.cc" "${PROJECT_SOURCE_DIR}/source/caffe2/operator/*.h")
cuda_add_library(caffe2_lib ${All_SOURCE})
Instead of building library seperately for CPU and GPU. I merge them into a single library file.

Cmake file for C++/CUDA project

I'm having trouble compiling my application using cmake and make. The source files of the project are organized as follows:
SOURCE/
CMakeLists.txt
myApp.cc
include/
classA.hh
classB.hh
src/
classA.cc
classB.cc
classB.cu
My CMakeLists.txt file is as follows:
CMAKE_MINIMUM_REQUIRED(VERSION 2.8)
PROJECT(myApp)
FIND_PACKAGE(VTK REQUIRED)
INCLUDE(${VTK_USE_FILE})
FIND_PACKAGE(GDCM REQUIRED)
IF(GDCM_FOUND)
INCLUDE(${GDCM_USE_FILE})
SET(GDCM_LIBRARIES gdcmCommon vtkgdcm)
ELSE(GDCM_FOUND)
MESSAGE(FATAL_ERROR "Cannot find GDCM, did you set GDCM_DIR?")
ENDIF(GDCM_FOUND)
SET(CUDA_TOOLKIT_ROOT_DIR="/Developer/NVIDIA/CUDA-7.5/")
FIND_PACKAGE(CUDA REQUIRED)
SET(CUDA_PROPAGATE_HOST_FLAGS ON)
SET(CUDA_NVCC_FLAGS ${CUDA_NVCC_FLAGS} -gencode arch=compute_30,code=sm_30)
SET(LIB_TYPE SHARED)
SET(CUDA_SEPARABLE_COMPILATION ON)
LINK_DIRECTORIES(/lib/FFTW/INSTALL/lib)
INCLUDE_DIRECTORIES(/lib/FFTW/INSTALL/include)
INCLUDE_DIRECTORIES(${PROJECT_SOURCE_DIR}/include)
FILE(GLOB headers ${PROJECT_SOURCE_DIR}/include/*.hh)
FILE(GLOB sources ${PROJECT_SOURCE_DIR}/src/*.cc)
FILE(GLOB cudafile ${PROJECT_SOURCE_DIR}/src/*.cu)
CUDA_ADD_EXECUTABLE(myApp myApp ${headers} ${sources} ${cudafile})
TARGET_LINK_LIBRARIES(myApp ${VTK_LIBRARIES} ${GDCM_LIBRARIES} fftw3)
When I try to compile the project using make (after successfully running cmake) I get:
nvcc fatal : A single input file is required for a non-link phase when an outputfile is specified
CMake Error at myApp_generated_classB.cu.o.cmake:207 (message):
Error generating
../BUILD/CMakeFiles/myApp.dir/src/./myApp_generated_classB.cu.o
Is breaking up the source file of a class into .cc and .cu files problematic?
This is not a comprehensive explanation of what exactly causes the problem stated in the question; nevertheless it solves the problem in a fairly satisfactory way.
First, apparently there is a conflict between using FIND_PACKAGE(VTK) (and hence FIND_PACKAGE(GDCM) which seems to require VTK CMake files for vtkgdcm) and nvcc. This has been recently reported on Mantis. To avoid this conflict, I use:
LINK_DIRECTORIES( {VTK_Directory}/INSTALL/lib)
INCLUDE_DIRECTORIES({VTK_Directory}/INSTALL/include/vtk-6.2)
LINK_DIRECTORIES( {GDCM_Directory}/INSTALL/lib)
INCLUDE_DIRECTORIES({GDCM_Directory}/INSTALL/include/gdcm-2.4)
instead of,
FIND_PACKAGE(VTK REQUIRED)
FIND_PACKAGE(GDCM REQUIRED)
Second, as for the CUDA part of the project, I put everything into a .cu file and use CUDA_COMPILE(cuda_o myCUDAstudff.cu) to create an object file. Then I use the native C++ compiler to create an executable as usual using ADD_EXECUTABLE( ... ${cuda_o}). Since I am using the native C++ compiler as opposed to nvcc, I need to include the following header files in my kernel (myCUDAstudff.cu) file:
#include <cuda.h>
#include <cuda_runtime.h>
and also link to libcudart in TARGET_LINK_LIBRARIES(), for which I used the shared library. I couldn't figure out a way to do the same with libcudart_static.a though.
Alternatively, one can use CUDA_ADD_EXECUTABLE(... myCUDAstudff.cu) instead of all the above steps (i.e., CUDA_COMPILE(), ADD_EXECUTABLE(),...).