Skip to content

Commit 698faa2

Browse files
author
MayuriNFC
committed
nothing
1 parent 39ad6c9 commit 698faa2

File tree

250 files changed

+2086
-78
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

250 files changed

+2086
-78
lines changed

.gitmodules

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,12 @@
55
[submodule "cmake/examples"]
66
path = cmake/examples
77
url = https://github.com/ttroy50/cmake-examples.git
8+
[submodule "submodule/cmake-examples"]
9+
path = submodule/cmake-examples
10+
url = https://github.com/ttroy50/cmake-examples
11+
[submodule "submodule/GenericMakefile"]
12+
path = submodule/GenericMakefile
13+
url = https://github.com/mbcrawfo/GenericMakefile
14+
[submodule "submodule/CMakeTutorial"]
15+
path = submodule/CMakeTutorial
16+
url = https://github.com/BrightXiaoHan/CMakeTutorial
File renamed without changes.
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# Prerequisites
2+
*.d
3+
4+
# Compiled Object files
5+
*.slo
6+
*.lo
7+
*.o
8+
*.obj
9+
10+
# Precompiled Headers
11+
*.gch
12+
*.pch
13+
14+
# Compiled Dynamic libraries
15+
*.so
16+
*.dylib
17+
*.dll
18+
19+
# Fortran module files
20+
*.mod
21+
*.smod
22+
23+
# Compiled Static libraries
24+
*.lai
25+
*.la
26+
*.a
27+
*.lib
28+
29+
# Executables
30+
*.exe
31+
*.out
32+
*.app
33+
34+
# Generate Config files
35+
src/config
36+
include/config
37+
38+
# ide
39+
.vscode
40+
41+
# build
42+
build/
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[submodule "ImportExternalProject/spdlog"]
2+
path = ImportExternalProject/spdlog
3+
url = https://github.com/gabime/spdlog.git
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
cmake_minimum_required(VERSION 3.8)
2+
project(CUDA_MAT_MUL LANGUAGES CXX CUDA)
3+
4+
add_library(cudaMatMul cudaMatMul.cu cudaMatMul.h)
5+
target_compile_features(cudaMatMul PUBLIC cxx_std_11)
6+
7+
add_executable(main main.cc)
8+
target_link_libraries(main cudaMatMul)
9+
10+
set(CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/cmake;${CMAKE_MODULE_PATH}")
11+
find_package(cuBLAS)
12+
add_executable(cublasMatMul cublasMatMul.cu)
13+
target_compile_features(cublasMatMul PRIVATE cxx_std_11)
14+
target_link_libraries(cublasMatMul PRIVATE ${CUBLAS_LIBRARIES})
Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
# CMake 编写 CUDA 应用程序
2+
从cmake 3.9版本开始,cmake就原生支持了cuda c/c++。再这之前,是通过find_package(CUDA REQUIRED)来间接支持cuda c/c++的。这种写法不仅繁琐而且丑陋。所以这里主要介绍3.9版本之后引入的方法,cuda c/c++程序可以使用和普通的c/c++程序完全一样的操作,甚至两者可以混合使用,非常的方便,清晰明了。
3+
![](./imgs/cmake_cuda_support.png)
4+
本文便通过一个例子(矩阵乘法运算)来具体讲解。
5+
6+
## 准备工作
7+
#### 安装cuda driver与cuda toolkit
8+
具体参考nvidia官方文档,这里不再赘述。
9+
安装完成后测试nvcc命令是否可以正常运行
10+
```bash
11+
nvcc --version
12+
```
13+
如果不能正常运行,将cudatoolkit下的bin目录加入到环境变量中(linux下通常为/usr/local/cuda/bin)
14+
15+
## 开启CUDA支持的选项
16+
想要cmake支持.cu文件的编译,需要在CMakeLists.txt中开启CUDA的支持选项
17+
```cmake
18+
project(CUDA_MAT_MUL LANGUAGES CXX CUDA)
19+
```
20+
如果需要编译选项支持是否开启cuda支持(即cuda为可选项),也可以使用如下方法
21+
```cmake
22+
project(CUDA_MAT_MUL)
23+
if(CUDA_ENABLE)
24+
enable_language(CUDA)
25+
endif()
26+
```
27+
你可以像往常一样添加一个库
28+
```
29+
add_library(cudaMatMul cudaMatMul.cu cudaMatMul.h)
30+
target_compile_features(cudaMatMul PUBLIC cxx_std_11)
31+
```
32+
可以链接到普通的c++程序中
33+
```
34+
add_executable(main main.cc)
35+
target_link_libraries(main cudaMatMul)
36+
```
37+
38+
## 引入cuda依赖库
39+
如果我们想引入英伟达官方或者第三方的cuda库时,我们也可以编写一个供find_package调用的模块。这里以引入cublas库为例,新建一个FindcuBLAS.cmake(参考Github [CLblast](https://github.com/CNugteren/CLBlast/blob/master/cmake/Modules/FindcuBLAS.cmake)
40+
```cmake
41+
# ==================================================================================================
42+
# This file is part of the cuBLASt project. The project is licensed under Apache Version 2.0. This
43+
# project loosely follows the Google C++ styleguide and uses a tab-size of two spaces and a max-
44+
# width of 100 characters per line.
45+
#
46+
# Author(s):
47+
# Cedric Nugteren <www.cedricnugteren.nl>
48+
#
49+
# ==================================================================================================
50+
#
51+
# Defines the following variables:
52+
# CUBLAS_FOUND Boolean holding whether or not the cuBLAS library was found
53+
# CUBLAS_INCLUDE_DIRS The CUDA and cuBLAS include directory
54+
# CUDA_LIBRARIES The CUDA library
55+
# CUBLAS_LIBRARIES The cuBLAS library
56+
#
57+
# In case CUDA is not installed in the default directory, set the CUDA_ROOT variable to point to
58+
# the root of cuBLAS, such that 'cublas_v2.h' can be found in $CUDA_ROOT/include. This can either be
59+
# done using an environmental variable (e.g. export CUDA_ROOT=/path/to/cuBLAS) or using a CMake
60+
# variable (e.g. cmake -DCUDA_ROOT=/path/to/cuBLAS ..).
61+
#
62+
# ==================================================================================================
63+
64+
# Sets the possible install locations
65+
set(CUBLAS_HINTS
66+
${CUDA_ROOT}
67+
$ENV{CUDA_ROOT}
68+
$ENV{CUDA_TOOLKIT_ROOT_DIR}
69+
)
70+
set(CUBLAS_PATHS
71+
/usr
72+
/usr/local
73+
/usr/local/cuda
74+
)
75+
76+
# Finds the include directories
77+
find_path(CUBLAS_INCLUDE_DIRS
78+
NAMES cublas_v2.h cuda.h
79+
HINTS ${CUBLAS_HINTS}
80+
PATH_SUFFIXES include inc include/x86_64 include/x64
81+
PATHS ${CUBLAS_PATHS}
82+
DOC "cuBLAS include header cublas_v2.h"
83+
)
84+
mark_as_advanced(CUBLAS_INCLUDE_DIRS)
85+
86+
# Finds the libraries
87+
find_library(CUDA_LIBRARIES
88+
NAMES cudart
89+
HINTS ${CUBLAS_HINTS}
90+
PATH_SUFFIXES lib lib64 lib/x86_64 lib/x64 lib/x86 lib/Win32 lib/import lib64/import
91+
PATHS ${CUBLAS_PATHS}
92+
DOC "CUDA library"
93+
)
94+
mark_as_advanced(CUDA_LIBRARIES)
95+
find_library(CUBLAS_LIBRARIES
96+
NAMES cublas
97+
HINTS ${CUBLAS_HINTS}
98+
PATH_SUFFIXES lib lib64 lib/x86_64 lib/x64 lib/x86 lib/Win32 lib/import lib64/import
99+
PATHS ${CUBLAS_PATHS}
100+
DOC "cuBLAS library"
101+
)
102+
mark_as_advanced(CUBLAS_LIBRARIES)
103+
104+
# ==================================================================================================
105+
106+
# Notification messages
107+
if(NOT CUBLAS_INCLUDE_DIRS)
108+
message(STATUS "Could NOT find 'cuBLAS.h', install CUDA/cuBLAS or set CUDA_ROOT")
109+
endif()
110+
if(NOT CUDA_LIBRARIES)
111+
message(STATUS "Could NOT find CUDA library, install it or set CUDA_ROOT")
112+
endif()
113+
if(NOT CUBLAS_LIBRARIES)
114+
message(STATUS "Could NOT find cuBLAS library, install it or set CUDA_ROOT")
115+
endif()
116+
117+
# Determines whether or not cuBLAS was found
118+
include(FindPackageHandleStandardArgs)
119+
find_package_handle_standard_args(cuBLAS DEFAULT_MSG CUBLAS_INCLUDE_DIRS CUDA_LIBRARIES CUBLAS_LIBRARIES)
120+
121+
# ==================================================================================================
122+
```
123+
当我们编写需要依赖cublas库的程序时便可以这样写
124+
```cmake
125+
find_package(cuBLAS)
126+
add_executable(cublasMatMul cublasMatMul.cu)
127+
target_compile_features(cublasMatMul PRIVATE cxx_std_11)
128+
target_link_libraries(cublasMatMul PRIVATE ${CUBLAS_LIBRARIES})
129+
```
130+
完全和编写普通c++程序一样的体验,非常棒。

0 commit comments

Comments
 (0)