如何使用 CMake 3.15 查找和 link CUDA 库?

How to find and link CUDA libraries using CMake 3.15?

我在我的类 Unix 系统上使用 CMake 3.15-rc3。

我需要 link 我正在使用多个 CUDA 库构建的程序,包括 cublascufftcusolvercurand , nppicc, nppial, nppist, nppidei, nppig, nppitc, npps.

根据我在网上找到的内容,我需要做这样的事情:

add_executable(test benchmark.cpp)
find_package(CUDALibs)
target_link_libraries(test CUDA::cudart CUDA::cublas CUDA::cufft CUDA::cusolver CUDA::curand CUDA::nppicc CUDA::nppial CUDA::nppist CUDA::nppidei CUDA::nppig CUDA::nppitc CUDA::npps)

当我 运行 make 我得到以下错误:

CMake Warning at CMakeLists.txt:27 (find_package):
  By not providing "FindCUDALibs.cmake" in CMAKE_MODULE_PATH this project has
  asked CMake to find a package configuration file provided by "CUDALibs",
  but CMake did not find one.

  Could not find a package configuration file provided by "CUDALibs" with any
  of the following names:

    CUDALibsConfig.cmake
    cudalibs-config.cmake

  Add the installation prefix of "CUDALibs" to CMAKE_PREFIX_PATH or set
  "CUDALibs_DIR" to a directory containing one of the above files.  If
  "CUDALibs" provides a separate development package or SDK, be sure it has
  been installed.


看来我需要一个 CUDALibsConfig.cmake 文件。我从哪里获得这个文件以及如何告诉 cmake 使用它?

如果我使用以下内容,它会起作用:

find_package(CUDA REQUIRED)
target_link_libraries(run_benchmarks tf libmxnet.so ${CUDA_LIBRARIES} ${CUDA_cusparse_LIBRARY} ${CUDA_cublas_LIBRARY} ${CUDA_npp_LIBRARY})

但根据 this find_package(cuda) 已弃用,所以我想学习正确的用法。

编辑 我尝试了其中一个回复中的建议。 我将 CUDA 添加到项目 LANGUAGES:

project(
        test_project
        DESCRIPTION "Test project"
        LANGUAGES CXX CUDA
        )

然后我用了find_package( FindCUDAToolkit REQUIRED)

但是,当我 运行 cmake 时,出现以下错误:

 nchafni   dev  …  sample_code  benchmarks  build  1  cmake ..
-- The CXX compiler identification is GNU 7.5.0
-- The CUDA compiler identification is NVIDIA 10.1.243
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Check for working CUDA compiler: /usr/local/cuda-10.1/bin/nvcc
-- Check for working CUDA compiler: /usr/local/cuda-10.1/bin/nvcc -- works
-- Detecting CUDA compiler ABI info
-- Detecting CUDA compiler ABI info - done
CMake Error at CMakeLists.txt:17 (find_package):
  By not providing "FindFindCUDAToolkit.cmake" in CMAKE_MODULE_PATH this
  project has asked CMake to find a package configuration file provided by
  "FindCUDAToolkit", but CMake did not find one.

  Could not find a package configuration file provided by "FindCUDAToolkit"
  with any of the following names:

    FindCUDAToolkitConfig.cmake
    findcudatoolkit-config.cmake

  Add the installation prefix of "FindCUDAToolkit" to CMAKE_PREFIX_PATH or
  set "FindCUDAToolkit_DIR" to a directory containing one of the above files.
  If "FindCUDAToolkit" provides a separate development package or SDK, be
  sure it has been installed.


-- Configuring incomplete, errors occurred!

我错过了什么?

您链接的文档说您需要将 CUDA 添加到 project() 命令的语言列表中。并找到它说使用 FindCUDAToolkit 模块的 CUDA 库,而不是 CUDALibs.

find_package(CUDA) 对于用 CUDA 编写的程序/使用 CUDA 编译器(例如 NVCC)编译的程序已弃用。文档页面 says(强调我的):

It is no longer necessary to use this module or call find_package(CUDA) for compiling CUDA code. Instead, list CUDA among the languages named in the top-level call to the project() command, or call the enable_language() command with CUDA. Then one can add CUDA (.cu) sources to programs directly in calls to add_library() and add_executable().

但是 find_package(CUDA) 并没有真正弃用 - 从 CMake 版本 3.15 开始 - 对于仅使用 CUDA-enabled/CUDA-bundled/CUDA-utilizing 库的 C++ 代码。

在 CMake 3.17 中,引入了一个新的 macro/command:FindCUDAToolkit()(还有这个 find_package(CUDAToolkit)。您不能在您的 CMake 版本中使用它;find_package(CUDA) 会很好,即使它有点笨重和过时。

编辑: 升级到较新的 CMake 版本实际上非常容易:KitWare 提供二进制文件 releases,它们几乎没有依赖关系。在 Linux 系统上,它们将是:

    linux-vdso.so.1
    libdl.so.2
    librt.so.1
    libpthread.so.0
    libm.so.6
    libc.so.6
    /lib64/ld-linux-x86-64.so.2

...如果没有这些,您将很难找到一个系统。此外,即使安装在任意路径下,CMake 也能够区分其共享文件的版本和 CMake 使用的任何系统版本。所以 - 没有理由坚持使用旧版本。