使用CMAKE使用设备并行性编译CUDA内核

时间:2017-07-01 23:29:15

标签: cmake cuda

这个问题是关于如何使用CMAKE使用设备并行性编译CUDA代码。 当使用CUDA设备并行时,我们必须使用可重定位代码进行编译,这在CMAKE中引入了第二个“链接”阶段,但是我们还必须链接-lcudadevrt(我怀疑这可能是我的问题的根源)。

我知道如何手动和使用Makefile进行此过程,但使用CMAKE& FindCUDA我无法编译一个简单的示例代码。

的CMakeLists.txt:

cmake_minimum_required(VERSION 3.7)
project(cuda_device_parallelism)

set(CMAKE_CXX_STANDARD 11)
find_package(CUDA REQUIRED)

set(CUDA_SEPARABLE_COMPILATION ON)

list(APPEND CUDA_NVCC_FLAGS -arch=sm_35 -Wno-deprecated-gpu-targets)

link_directories(${CUDA_TOOLKIT_ROOT_DIR}/lib64)

cuda_add_library(kernel kernel.cu)
target_link_libraries(kernel cudadevrt)

add_executable(cuda_device_parallelism main.cpp)
target_link_libraries(cuda_device_parallelism kernel)

main.cpp中:

#include "kernel.cuh"

int main() {
    launchDynamic();

    return 0;
}

kernel.cuh:

void launchDynamic();

kernel.cu:

#include "kernel.cuh"

#include <cstdio>
#include <cuda_runtime.h>


__global__ void child() {
    int idx = threadIdx.x + blockIdx.x * blockDim.x;
    printf("I am child %d \n", idx);
}

__global__ void parent() {
    int idx = threadIdx.x + blockIdx.x * blockDim.x;
    printf("I am parent %d \n", idx);
    child<<<1,32>>>();
}

void launchDynamic() {
    parent<<<1,32>>>();
    cudaDeviceSynchronize();
}

输出(路径简化):

[ 20%] Building NVCC (Device) object CMakeFiles/kernel.dir/kernel_generated_kernel.cu.o
nvcc kernel.cu -dc -o kernel_generated_kernel.cu.o -ccbin /usr/bin/cc -m64 -Xcompiler ,\"-g\" -arch=sm_35 -Wno-deprecated-gpu-targets -DNVCC -I/usr/local/cuda/include
[ 40%] Building NVCC intermediate link file CMakeFiles/kernel.dir/kernel_intermediate_link.o
nvcc -arch=sm_35 -Wno-deprecated-gpu-targets -lcudadevrt -m64 -ccbin /usr/bin/cc -dlink kernel_generated_kernel.cu.o -o kernel_intermediate_link.o
[ 60%] Linking CXX static library libkernel.a
/usr/bin/ar qc libkernel.a kernel_generated_kernel.cu.o kernel_intermediate_link.o
[ 80%] Building CXX object CMakeFiles/cuda_device_parallelism.dir/main.cpp.o
/usr/bin/c++ -I/usr/local/cuda/include  -g -std=gnu++11 -o main.cpp.o -c main.cpp
[100%] Linking CXX executable cuda_device_parallelism
/usr/bin/c++ -g   main.cpp.o  -o cuda_device_parallelism  -L/usr/local/cuda/lib64 -Wl,-rpath,/usr/local/cuda/lib64 libkernel.a /usr/local/cuda/lib64/libcudart_static.a -lpthread -ldl /usr/lib/x86_64-linux-gnu/librt.so /usr/local/cuda/lib64/libcudadevrt.a -lcudadevrt 
libkernel.a(kernel_intermediate_link.o): In function `__cudaRegisterLinkedBinary_66_tmpxft_00001a84_00000000_17_cuda_device_runtime_compute_61_cpp1_ii_8b1a5d37':
link.stub:(.text+0x98): undefined reference to `__fatbinwrap_66_tmpxft_00001a84_00000000_17_cuda_device_runtime_compute_61_cpp1_ii_8b1a5d37'

---编辑--------------------- 我正在使用CUDA 8.0:

nvcc --version
nvcc: NVIDIA (R) Cuda compiler driver
Copyright (c) 2005-2016 NVIDIA Corporation
Built on Tue_Jan_10_13:22:03_CST_2017
Cuda compilation tools, release 8.0, V8.0.61

和CMake 3.7.2

0 个答案:

没有答案