关于CUDA的CMake的使用方法
CMake
CMake是一个现代化C++、CUDA、C构建系统,对于大型项目来说,一个易用的构建系统是非常重要的,能够减少环境的配置时间,动态库的配置时间。因此熟练掌握cmake的用法是C++程序员一个标准的技能,能够让我们快速构建系统编译以及运行。
关于CUDA的CMake
CUDA是NVIDIA开发的并行计算库,能够让我们快速编写CUDA代码然后通过CUDA RUNTIME进行控制RTX等硬件运行。但是配置一个好的CUDA运行环境是非常不容易的,因此本文记录一下如何搭建一个CUDA的CMake系统。
环境
- CUDA 11.7
- RTX 2080ti
CMake语法
|
|
- CMake的版本,因为有些CMake关键字在低版本中是不存在的,因此需要指定版本号
- 定义项目的名称,并将名称放入
PROJECT_NAME
变量中 - 定义项目使用的语言, 具体参考project Supported languages include C, CXX (i.e. C++), CUDA, OBJC (i.e. Objective-C), OBJCXX, Fortran, HIP, ISPC, and ASM. By default C and CXX are enabled if no language options are given. Specify language NONE, or use the LANGUAGES keyword and list no languages, to skip enabling any languages.
- 设置CUDA架构的生成代码的号 如set(CMAKE_CUDA_ARCHITECTURES 75) 对应sm75
- 包含标准install GNUInstallDirs¶
- 寻找cuda工具库 FindCUDAToolkit 还有各种各样的find_package
- 添加可执行文件
- 定义link
用法
|
|
构建
mkdir build && cd build
cmake ..
-- The CXX compiler identification is GNU 7.5.0
-- The CUDA compiler identification is NVIDIA 11.7.64
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: /usr/bin/c++ - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Detecting CUDA compiler ABI info
-- Detecting CUDA compiler ABI info - done
-- Check for working CUDA compiler: /usr/local/cuda-11.7/bin/nvcc - skipped
-- Detecting CUDA compile features
-- Detecting CUDA compile features - done
-- Found CUDAToolkit: /usr/local/cuda-11.7/include (found version "11.7.64")
-- Performing Test CMAKE_HAVE_LIBC_PTHREAD
-- Performing Test CMAKE_HAVE_LIBC_PTHREAD - Failed
-- Looking for pthread_create in pthreads
-- Looking for pthread_create in pthreads - not found
-- Looking for pthread_create in pthread
-- Looking for pthread_create in pthread - found
-- Found Threads: TRUE
11.7.64
-- Configuring done
-- Generating done
-- Build files have been written to: /home/lay/work/study/soft/cc/demo/build
make
具体的代码在CUDALibrarySamples
这样就可以运行了。