Justin
Justin

Reputation: 1939

Why can't I link to my CUDA static library that uses Dynamic Parallelism and Separable Compilation?

I'm trying to create the most basic CUDA application to demonstrate Dynamic Parallelism, Separate Compilation and Linking, a CUDA kernel in a static library, and I'm trying to use CMake to generate a Visual Studio solution. I'm using CMake 3.21.3, CUDA 11.4, and Visual Studio 2019 (16.11.5).

I have a .h and a .cu file, which I'm compiling into a static library. I also have a main.cpp file which includes the header from my library and links to it. This file is compiled to an executable. The code for my library and for my executable are in separate folders, like this:

src
 |-MyLib
 |  |-mylib.h
 |  |-mylib.cu
 |  |-CMakeLists.txt
 |
 |-MyMain
 |  |-main.cpp
 |  |-CMakeLists.txt
 |
 |-CMakeLists.txt

mylib.h and mylib.cu contain a function to initialize CUDA, two kernels: a parent one and a child one, and a host function to invoke the parent kernel. mylib.h #includes both cuda_runtime.h and device_launch_parameters.h to make Visual Studio happy.

main.cpp simply #includes mylib.h, calls the initCUDA function, then calls the host function to invoke the kernel.

The CMakeLists file for the library looks like this:

cmake_minimum_required(VERSION 3.17 FATAL_ERROR)
project(MyLib LANGUAGES CXX CUDA)

find_package(CUDAToolkit REQUIRED)

add_library(${PROJECT_NAME} STATIC mylib.h mylib.cu)

target_compile_options(${PROJECT_NAME} PRIVATE "$<$<AND:$<CONFIG:Debug>,$<COMPILE_LANGUAGE:CUDA>>:-G;-src-in-ptx>") # enable device debug flags

set_target_properties(${PROJECT_NAME} PROPERTIES CUDA_ARCHITECTURES "52") # this is to make CMake happy
set_target_properties(${PROJECT_NAME} PROPERTIES CUDA_SEPARABLE_COMPILATION ON)
set_target_properties(${PROJECT_NAME} PROPERTIES CUDA_RESOLVE_DEVICE_SYMBOLS ON)  # this is required for some reason

target_link_libraries(${PROJECT_NAME} ${CUDAToolkit_LIBRARY_DIR}/cudart.lib)

and the CMakeLists file for main.cpp looks like this:

cmake_minimum_required(VERSION 3.17 FATAL_ERROR)

project(CUDA_Dynamic_Parallelism)

add_executable(${PROJECT_NAME} main.cpp)
set_target_properties(${PROJECT_NAME} PROPERTIES CUDA_SEPARABLE_COMPILATION ON)
target_link_libraries(${PROJECT_NAME} MyLib)

CMake configures and generates the solution, no problem. However, when I try to build, the library seems to build ok, but when the executable is linking, I get the following error: MyLib.lib(MyLib.device-link.obj) : error LNK2001: unresolved external symbol __fatbinwrap_38_cuda_device_runtime_compute_86_cpp1_ii_8b1a5d37

Any ideas why this is happening and how to resolve it?

Upvotes: 1

Views: 580

Answers (1)

M. Steiner
M. Steiner

Reputation: 336

Just looking at the error, I would guess that there is a problem with the cuda architecture. You build your Lib with CUDA_ARCHITECTURES 52 but you do not specify any in the seconds project.

"__fatbinwrap_38_cuda_device_runtime_compute_86_cpp1_ii_8b1a5d37" seems to indicate that it is looking for a symbol with compute architecture 86. I would suggest playing around with that.

Upvotes: 1

Related Questions