Skip to content

Windows CUDART link #354

@yhmtsai

Description

@yhmtsai

In #351 , how do we link cudart in Windows?
We set CUDA_RUNTIME_LIBS as the cudart shared libraries, but nvcc will use -cudart=static as its default.
It works fine in Linux, but it can not be resolved in Windows.
The problem is in benchmark/spmv which links to static and shared library.
The following are workaround solutions

  1. Do not link the cuda library in benchmark/spmv in Windows
    if (NOT MSVC)
    target_link_libraries(spmv ginkgo ${CUDA_RUNTIME_LIBS}
    ${CUBLAS} ${CUSPARSE})
    target_include_directories(spmv SYSTEM PRIVATE ${CUDA_INCLUDE_DIRS})
    endif()
  2. Force nvcc to use -cudart=shared in Windows.
  3. Use cudart shared library when giving -cudart=shared, otherwise use cudart static library.
    cudart_static needs rt, dl, and pthread, so it needs to add them into the libraries.
    if("${CMAKE_CUDA_FLAGS}" MATCHES "-cudart(=| )shared" OR "${GINKGO_CUDA_COMPILER_FLAGS}" MATCHES "-cudart(=| )shared")
    set(CUDA_RUNTIME_LIBS "${CUDA_RUNTIME_LIBS_DYNAMIC}" CACHE STRING "Path to a library" FORCE)
    else()
    set(CUDA_RUNTIME_LIBS "${CUDA_RUNTIME_LIBS_STATIC}" CACHE STRING "Path to a library" FORCE)
    if(NOT MSVC)
    set(THREADS_PREFER_PTHREAD_FLAG ON)
    find_package(Threads REQUIRED)
    # link cudart_static need rt, pthread, and dl
    set(CUDA_RUNTIME_LIBS "${CUDA_RUNTIME_LIBS_STATIC};rt;Threads::Threads;-Wl,--no-as-needed;dl" CACHE STRING "Path to a library" FORCE)
    endif()
    endif()

    if("${CUDA_RUNTIME_LIBS}" MATCHES "Threads::Threads")
    # need to import Threads
    set(THREADS_PREFER_PTHREAD_FLAG ON)
    find_package(Threads REQUIRED)
    endif()

it seems to be failed with Threads::Threads but successful with -pthread.

I prefer to choose 2 or 3

I think option 1 is weird.
option 2 does not change too many things in CMake, but it maybe reduces usability in Windows.
option 3 works fine but benchmark/spmv needs to include threads to use Threads::threads correctly.

Which option do you think better way? Alternatively, is there any idea to solve this problem?

edit:
delete the fail with Threads::Threads

Metadata

Metadata

Labels

is:help-wantedNeed ideas on how to solve this.mod:cudaThis is related to the CUDA module.plat:windowsThis is related to the Windows Operating system

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions