CUDA可分离编译和CMake

时间:2015-02-18 14:56:30

标签: cuda makefile cmake dynamic-linking

我有一个包含cpp和cu源文件的大型库项目。我想在一个独立的共享对象中编译它,但由于我有一些设备函数,我决定将它拆分为包含大部分函数的共享对象和仅包含设备函数的存档文件。这是我为它写的Makefile的一部分 - 所有(非模板)设备函数都放在文件device.cu中:

生成文件

LIB_NAME = libexample.so
CUDA = /usr/local/cuda/include
CXX = g++
CXXFLAGS = -c -O2 -fPIC -Wall -I. -I./code -I./code/header -I$(CUDA)
SOURCES = src1.cpp src2.cpp src3.cpp
OBJECTS = $(SOURCES:.cpp=.o)
NVCC = nvcc
CU_SOURCES = src1_cu.cu src2_cu.cu src3_cu.cu device.cu
CU_OBJECTS = $(CU_SOURCES:.cu=.o)
BUILDDIR = .
VPATH = code/src/common_src code/src/src_CUDA
DEVICE_LINK = dev_link.o
GENCODE_FLAGS := -gencode arch=compute_20,code=sm_20

NVCCFLAGS = -x cu -O2 --compiler-options '-fPIC' $(GENCODE_FLAGS) -I. -I./code -I./code/header -I$(CUDA) -dc 
NVCCLINK = --compiler-options '-fPIC' $(GENCODE_FLAGS) -dlink

all: lib/$(LIB_NAME)

lib/$(LIB_NAME): $(OBJECTS) $(CU_OBJECTS) $(DEVICE_LINK)
    $(CXX) -shared -Wl,-soname,libexample.so $^ -o $@
    ar rcs lib/device.a device.o

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

%.o: %.cu
    $(NVCC) $(NVCCFLAGS) $< -o $@

$(DEVICE_LINK): $(CU_OBJECTS)
    $(NVCC) $(NVCCLINK) $^ -o $@

我决定更改构建系统,然后切换到CMake以生成Makefile和Visual Studio项目。很明显如何编写一个有效的CMakeLists.txt文件没有可分离的编译,但是我找不到适用于我的解决方案的解决方案(我在这里阅读了一些提议的解决方案)但他们似乎不适合我!)。你能帮我写一下说的CMakeLists.txt文件吗?这是我到目前为止所做的:

的CMakeLists.txt

cmake_minimum_required(VERSION 2.8.10)

# Set Library & project name
set(LIB_NAME example)
project(${LIB_NAME})

message("LIBRARY ${LIB_NAME}")
enable_language(CXX)

# Check if CUDA is installed on this system
find_package(CUDA REQUIRED)

# Set source directories
set(COMMON_SRCS_DIR ${CMAKE_SOURCE_DIR}/code/src/common_src)
set(CUDA_SRCS_DIR ${CMAKE_SOURCE_DIR}/code/src/src_CUDA)

# Set source files
set(COMMON_SRCS ${COMMON_SRCS_DIR}/src1.cpp
        ${COMMON_SRCS_DIR}/src2.cpp
        ${COMMON_SRCS_DIR}/src3.cpp
)

# Set CUDA device library name
set(DEVICE_LIB "device")

# Set CUDA objects
cuda_compile(SRC1_CU_O ${CUDA_SRCS_DIR}/src1_cu.cu)
cuda_compile(SRC2_CU_O ${CUDA_SRCS_DIR}/src2_cu.cu)
cuda_compile(SRC3_CU_O ${CUDA_SRCS_DIR}/src3_cu.cu)
cuda_compile(DEVICE_CU_O ${CUDA_SRCS_DIR}/device.cu)

# Set header file directories
include_directories(${CMAKE_SOURCE_DIR})
include_directories(${CMAKE_SOURCE_DIR}/code)
include_directories(${CMAKE_SOURCE_DIR}/code/header)
include_directories(${CUDA_INCLUDE_DIRS})

# Get CUDA compute capability - contains CUDA_GENCODE define
include(CudaParams.cmake)

# Set include stuff
cuda_include_directories(${CMAKE_SOURCE_DIR})
cuda_include_directories(${CMAKE_SOURCE_DIR}/code)
cuda_include_directories(${CMAKE_SOURCE_DIR}/code/header)
cuda_include_directories(${CUDA_INCLUDE_DIRS})

set(CUDA_SEPARABLE_COMPILATION ON)

# Set compiler flags
set(CUDA_NVCC_FLAGS "${CUDA_NVCC_FLAGS} ${CUDA_GENCODE} --compiler-options '-fPIC' -O2")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -march=native -O2")

# Generate main target
cuda_add_library(${LIB_NAME} SHARED ${COMMON_SRCS} 
    ${SRC1_CU_O} ${SRC2_CU_O} ${SRC3_CU_O} ${DEVICE_CU_O})
cuda_add_library(${DEVICE_LIB} STATIC ${DEVICE_CU_O})

# Install instructions
INSTALL(TARGETS ${LIB_NAME}
    LIBRARY DESTINATION ${CMAKE_SOURCE_DIR}/lib
    ARCHIVE DESTINATION ${CMAKE_SOURCE_DIR}/lib
    )

正如您所看到的,在CMakeLists.txt文件中没有提及dev_link.o因为我根本不知道我可以把它放在哪里!

0 个答案:

没有答案