CUDA 5.0 separate compilation of library with cmake

后端 未结 4 1095
情话喂你
情话喂你 2020-12-05 11:59

The buildtime of my cuda library is increasing and so I thought that separate compilation introduced in CUDA 5.0 might help me. I couldn\'t figure out how to achieve separat

4条回答
  •  失恋的感觉
    2020-12-05 12:39

    I finally got it running ;)

    In Addition to the answer of @PHD and my comment on it I modified: set(BUILD_SHARED_LIBS OFF) in my CMakeLists.txt since shared libs are not supported for separate compilation according to the nvcc manually v5.0 page 40.

    In addition to that use the latest rev (1223) from the repository instead of rev 1221. I contacted the developer and he fixed some issue blocking this. This revision doesn't set the nvcc -arch=sm_xx flag correctly, so I added this manually for my project and informed the developer of FindCUDA.cmake. So this might get fixed in the future.

    Don't forget to get cmake > 2.8.10 for this to work.

    Hope this helps anyone but me ;)

    Here is my CMakeLists.txt:

    #Required for CUDA-Check
    cmake_minimum_required(VERSION 2.8.10)
    
    project(gpulib)
    
    set(CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/CMake/cuda" ${CMAKE_MODULE_PATH})
    # ============================================
    # === Target
    # ============================================
    file(GLOB_RECURSE gpuacc_SRCS "*.cu")
    include_directories(.)
    
    # ---------------------------------------
    # Find Cuda
    find_package(CUDA REQUIRED)
    
    set(CUDA_ATTACH_VS_BUILD_RULE_TO_CUDA_FILE ON)
    
    set(BUILD_SHARED_LIBS OFF)
    
    set(CUDA_SEPARABLE_COMPILATION ON)
    #list(APPEND CUDA_NVCC_FLAGS -arch=sm_20)
    
    set(LIB_NAME "gpuacceleration")
    cuda_add_library(${LIB_NAME}
      ${gpuacc_SRCS} 
      OPTIONS -DSTUFF="blah blah"
      RELEASE -DNDEBUG
      DEBUG -g -DDEBUG
    )
    
    set(PUBLIC_HEADERS "myheader1.h;myheader2.h")
    
    INSTALL(FILES ${PUBLIC_HEADERS} DESTINATION include)
    INSTALL(FILES "${CMAKE_BINARY_DIR}/src/lib${LIB_NAME}.a" DESTINATION lib)
    

    EDIT: this is not working! The problem is that there are undefined references to all cuda functions (eg. cudaMalloc) when linking the generated library when building a executable in the main project.

    Still working on it

提交回复
热议问题