-
Notifications
You must be signed in to change notification settings - Fork 7
/
CMakeLists.txt
381 lines (351 loc) · 13.5 KB
/
CMakeLists.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
set(_USE_CUDNN False)
set(_USE_CUDA False)
set(_USE_BLAS False)
set(_USE_EIGEN False)
set(_USE_BUILD_IN False)
function(ResetAll)
set(_USE_CUDNN False)
set(_USE_CUDA False)
set(_USE_BLAS False)
set(_USE_EIGEN False)
set(_USE_BUILD_IN False)
endfunction()
if(BLAS_BACKEND STREQUAL "EIGEN")
ResetAll()
set(_USE_EIGEN True)
elseif(BLAS_BACKEND STREQUAL "OPENBLAS")
ResetAll()
set(_USE_BLAS True)
elseif(BLAS_BACKEND STREQUAL "CUDA")
ResetAll()
set(_USE_CUDA True)
elseif(BLAS_BACKEND STREQUAL "CUDNN")
ResetAll()
set(_USE_CUDA True)
set(_USE_CUDNN True)
else()
ResetAll()
set(_USE_BUILD_IN True)
endif()
if (_USE_CUDA)
project(sayuri LANGUAGES CXX CUDA)
cmake_minimum_required(VERSION 3.15)
else()
project(sayuri)
cmake_minimum_required(VERSION 3.10)
endif()
if (BOARD_SIZE)
message("Use the specific ${BOARD_SIZE} x ${BOARD_SIZE} board size.")
add_definitions(-DMAX_BOARD_SIZE=${BOARD_SIZE})
endif()
if (DISABLE_FP16)
message("Disable the CUDA FP16.")
else()
add_definitions(-DENABLE_FP16)
endif()
# Set up the root.
set(SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/src)
set(GAME_SOURCES_DIR ${SOURCE_DIR}/game)
set(NEURAL_SOURCES_DIR ${SOURCE_DIR}/neural)
set(PATTERN_SOURCES_DIR ${SOURCE_DIR}/pattern)
set(MCTS_SOURCES_DIR ${SOURCE_DIR}/mcts)
set(SUMMARY_SOURCES_DIR ${SOURCE_DIR}/summary)
set(SELFPLAY_SOURCES_DIR ${SOURCE_DIR}/selfplay)
set(UTILS_SOURCES_DIR ${SOURCE_DIR}/utils)
set(IncludePath "${CMAKE_CURRENT_SOURCE_DIR}/src")
include_directories(${IncludePath})
# Set all source file.
set(BASIC_SOURCES
${SOURCE_DIR}/main.cc
${SOURCE_DIR}/config.cc
${SOURCE_DIR}/version.cc
)
set(GAME_SOURCES
${GAME_SOURCES_DIR}/board.cc
${GAME_SOURCES_DIR}/pattern_board.cc
${GAME_SOURCES_DIR}/book.cc
${GAME_SOURCES_DIR}/game_state.cc
${GAME_SOURCES_DIR}/strings.cc
${GAME_SOURCES_DIR}/sgf.cc
${GAME_SOURCES_DIR}/zobrist.cc
${GAME_SOURCES_DIR}/symmetry.cc
${GAME_SOURCES_DIR}/gtp.cc
${GAME_SOURCES_DIR}/iterator.cc
)
set(NEURAL_SOURCES
${NEURAL_SOURCES_DIR}/loader.cc
${NEURAL_SOURCES_DIR}/description.cc
${NEURAL_SOURCES_DIR}/encoder.cc
${NEURAL_SOURCES_DIR}/network.cc
${NEURAL_SOURCES_DIR}/training_data.cc
${NEURAL_SOURCES_DIR}/winograd_helper.cc
${NEURAL_SOURCES_DIR}/blas/sgemm.cc
${NEURAL_SOURCES_DIR}/blas/blas.cc
${NEURAL_SOURCES_DIR}/blas/convolution.cc
${NEURAL_SOURCES_DIR}/blas/winograd_convolution3.cc
${NEURAL_SOURCES_DIR}/blas/fullyconnect.cc
${NEURAL_SOURCES_DIR}/blas/batchnorm.cc
${NEURAL_SOURCES_DIR}/blas/biases.cc
${NEURAL_SOURCES_DIR}/blas/se_unit.cc
${NEURAL_SOURCES_DIR}/blas/blas_forward_pipe.cc
)
set(PATTERN_SOURCES
${PATTERN_SOURCES_DIR}/mm.cc
${PATTERN_SOURCES_DIR}/mm_trainer.cc
${PATTERN_SOURCES_DIR}/pattern.cc
${PATTERN_SOURCES_DIR}/gammas_dict.cc
)
set(MCTS_SOURCES
${MCTS_SOURCES_DIR}/time_control.cc
${MCTS_SOURCES_DIR}/node.cc
${MCTS_SOURCES_DIR}/search.cc
)
set(SUMMARY_SOURCES
${SUMMARY_SOURCES_DIR}/accuracy.cc
)
set(SELFPLAY_SOURCES
${SELFPLAY_SOURCES_DIR}/pipe.cc
${SELFPLAY_SOURCES_DIR}/engine.cc
)
set(UTILS_SOURCES
${UTILS_SOURCES_DIR}/log.cc
${UTILS_SOURCES_DIR}/parse_float.cc
${UTILS_SOURCES_DIR}/splitter.cc
${UTILS_SOURCES_DIR}/random.cc
${UTILS_SOURCES_DIR}/time.cc
${UTILS_SOURCES_DIR}/filesystem.cc
${UTILS_SOURCES_DIR}/option.cc
${UTILS_SOURCES_DIR}/komi.cc
${UTILS_SOURCES_DIR}/gogui_helper.cc
${UTILS_SOURCES_DIR}/gzip_helper.cc
)
if(DEBUG_MODE)
message(STATUS "Set Debug Mode")
set(CMAKE_BUILD_TYPE DEBUG)
else()
set(CMAKE_BUILD_TYPE RELEASE)
endif()
if(USE_FAST_PARSER)
message(STATUS "Use fast parser library.")
add_definitions(-DUSE_FAST_PARSER)
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/third_party/fast_float/include/fast_float)
endif()
if (USE_AVX)
message(STATUS "Use the AVX instructions")
set(CMAKE_CXX_FLAGS "-mavx -mfma ${CMAKE_CXX_FLAGS}")
endif()
if (USE_ZLIB)
add_definitions(-DUSE_ZLIB)
find_package(ZLIB REQUIRED)
include_directories(${ZLIB_INCLUDE_DIRS})
message(STATUS "Use the zlib library")
endif()
# Define the neural network default backend.
if(_USE_CUDA)
if(_USE_CUDNN)
message(STATUS "Backend is cuDNN.")
message(" The CUDA-only backend is another choice. Please add the flag -DBLAS_BACKEND=CUDA")
message(" to disable cuDNN.")
else()
message(STATUS "Backend is CUDA-only.")
message(" The cuDNN backend is another choice. Please add the flag -DBLAS_BACKEND=CUDNN")
message(" to enable it. The cuDNN library is required.")
endif()
add_definitions(-DUSE_CUDA)
enable_language(CUDA)
aux_source_directory(${NEURAL_SOURCES_DIR}/cuda CUDA_SOURCES)
set(CUDA_STANDARD 14)
if (CMAKE_CUDA_COMPILER_VERSION VERSION_GREATER_EQUAL 12.0)
set(CMAKE_CUDA_FLAGS
" \
-gencode arch=compute_50,code=sm_50 \
-gencode arch=compute_50,code=compute_50 \
-gencode arch=compute_53,code=sm_53 \
-gencode arch=compute_53,code=compute_53 \
-gencode arch=compute_60,code=sm_60 \
-gencode arch=compute_60,code=compute_60 \
-gencode arch=compute_61,code=sm_61 \
-gencode arch=compute_61,code=compute_61 \
-gencode arch=compute_62,code=sm_62 \
-gencode arch=compute_62,code=compute_62 \
-gencode arch=compute_70,code=sm_70 \
-gencode arch=compute_70,code=compute_70 \
-gencode arch=compute_72,code=sm_72 \
-gencode arch=compute_72,code=compute_72 \
-gencode arch=compute_75,code=sm_75 \
-gencode arch=compute_75,code=compute_75 \
-gencode arch=compute_80,code=sm_80 \
-gencode arch=compute_80,code=compute_80 \
-gencode arch=compute_86,code=sm_86 \
-gencode arch=compute_86,code=compute_86 \
-gencode arch=compute_89,code=sm_89 \
-gencode arch=compute_89,code=compute_89 \
-gencode arch=compute_90,code=sm_90 \
-gencode arch=compute_90,code=compute_90 \
-Wno-deprecated-gpu-targets \
"
)
elseif (CMAKE_CUDA_COMPILER_VERSION VERSION_GREATER_EQUAL 11.0)
set(CMAKE_CUDA_FLAGS
" \
-gencode arch=compute_35,code=sm_35 \
-gencode arch=compute_35,code=compute_35 \
-gencode arch=compute_37,code=sm_37 \
-gencode arch=compute_37,code=compute_37 \
-gencode arch=compute_50,code=sm_50 \
-gencode arch=compute_50,code=compute_50 \
-gencode arch=compute_53,code=sm_53 \
-gencode arch=compute_53,code=compute_53 \
-gencode arch=compute_60,code=sm_60 \
-gencode arch=compute_60,code=compute_60 \
-gencode arch=compute_61,code=sm_61 \
-gencode arch=compute_61,code=compute_61 \
-gencode arch=compute_62,code=sm_62 \
-gencode arch=compute_62,code=compute_62 \
-gencode arch=compute_70,code=sm_70 \
-gencode arch=compute_70,code=compute_70 \
-gencode arch=compute_72,code=sm_72 \
-gencode arch=compute_72,code=compute_72 \
-gencode arch=compute_75,code=sm_75 \
-gencode arch=compute_75,code=compute_75 \
-gencode arch=compute_80,code=sm_80 \
-gencode arch=compute_80,code=compute_80 \
-Wno-deprecated-gpu-targets \
"
)
elseif (CMAKE_CUDA_COMPILER_VERSION VERSION_GREATER_EQUAL 10.2)
set(CMAKE_CUDA_FLAGS
" \
-gencode arch=compute_30,code=sm_30 \
-gencode arch=compute_30,code=compute_30 \
-gencode arch=compute_35,code=sm_35 \
-gencode arch=compute_35,code=compute_35 \
-gencode arch=compute_37,code=sm_37 \
-gencode arch=compute_37,code=compute_37 \
-gencode arch=compute_50,code=sm_50 \
-gencode arch=compute_50,code=compute_50 \
-gencode arch=compute_53,code=sm_53 \
-gencode arch=compute_53,code=compute_53 \
-gencode arch=compute_60,code=sm_60 \
-gencode arch=compute_60,code=compute_60 \
-gencode arch=compute_61,code=sm_61 \
-gencode arch=compute_61,code=compute_61 \
-gencode arch=compute_62,code=sm_62 \
-gencode arch=compute_62,code=compute_62 \
-gencode arch=compute_70,code=sm_70 \
-gencode arch=compute_70,code=compute_70 \
-gencode arch=compute_72,code=sm_72 \
-gencode arch=compute_72,code=compute_72 \
-gencode arch=compute_75,code=sm_75 \
-gencode arch=compute_75,code=compute_75 \
-Wno-deprecated-gpu-targets \
"
)
else()
message(WARNING "CUDA 10.2 or greater is recommended, but attempting to build anyways")
set(CMAKE_CUDA_FLAGS
" \
-gencode arch=compute_30,code=sm_30 \
-gencode arch=compute_30,code=compute_30 \
-gencode arch=compute_37,code=sm_37 \
-gencode arch=compute_53,code=sm_53 \
-gencode arch=compute_53,code=compute_53 \
-gencode arch=compute_70,code=sm_70 \
-gencode arch=compute_70,code=compute_70 \
"
)
endif()
set(CMAKE_CUDA_FLAGS "-Xptxas -O3 -Xcompiler -fPIC ${CMAKE_CUDA_FLAGS}")
# Use the shared library.
if(CMAKE_CUDA_FLAGS)
list(REMOVE_ITEM CMAKE_CUDA_FLAGS "-cudart static")
endif()
string(APPEND CMAKE_CUDA_FLAGS "-cudart shared")
set(CMAKE_CUDA_RUNTIME_LIBRARY SHARED)
else()
if(_USE_EIGEN)
message(STATUS "Backend is Eigen")
elseif(_USE_BLAS)
message(STATUS "Backend is OpenBlas")
elseif(_USE_BUILD_IN)
message(STATUS "Backend is built-in matrix.")
message(" Built-in matrix is very slow. Open Blas and Eigen are the better choice.")
message(" If you want to use Eigen, please add flag -DBLAS_BACKEND=EIGEN. And you need")
message(" to put the Eigen library to third_party directory. If you want to use OpenBlas,")
message(" please add flag -DBLAS_BACKEND=OPENBLAS. OpenBlas library is required.\n")
endif()
message(" The GPU backend is much faster than CPU backend. If you want to speed up with")
message(" GPUs, please add flag -DBLAS_BACKEND=CUDA or -DBLAS_BACKEND=CUDNN. CUDA library")
message(" and Nvida GPU are both required.\n")
endif()
# Find all required packages.
find_package(Threads REQUIRED)
set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD_REQUIRED on)
set(GCC_COMPILE_FLAGS "-Wall -Wextra -ffast-math -march=native -flto")
set(GCC_DISABLED_WARNING_COMPILE_FLAGS "-Wno-ignored-attributes -Wno-maybe-uninitialized -Wno-mismatched-tags")
set(CMAKE_CXX_FLAGS "${GCC_COMPILE_FLAGS} ${GCC_DISABLED_WARNING_COMPILE_FLAGS} ${CMAKE_CXX_FLAGS}")
set(CMAKE_CXX_FLAGS_DEBUG "-g -Og ${CMAKE_CXX_FLAGS}")
set(CMAKE_CXX_FLAGS_RELEASE "-g -O3 -DNDEBUG ${CMAKE_CXX_FLAGS}")
set(CMAKE_EXE_LINKER_FLAGS "-flto -g")
if(_USE_EIGEN)
message(STATUS "Include Eigen blas library")
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/third_party/Eigen)
add_definitions(-DUSE_EIGEN)
add_definitions(-DUSE_BLAS)
elseif(_USE_BLAS)
message(STATUS "Include Open BLAS library")
message(" Looking for system BLAS/OpenBLAS library...")
find_package(BLAS REQUIRED)
find_path(
BLAS_INCLUDE_DIRS openblas_config.h
/usr/include
/usr/local/include
/usr/include/openblas
/opt/OpenBLAS/include
/usr/include/x86_64-linux-gnu
$ENV{BLAS_HOME}/include)
add_definitions(-DUSE_OPENBLAS)
add_definitions(-DUSE_BLAS)
if((UNIX AND NOT APPLE) OR WIN32)
include_directories(${BLAS_INCLUDE_DIRS})
endif()
if(APPLE)
include_directories("/System/Library/Frameworks/Accelerate.framework/Versions/Current/Headers")
endif()
message(" BLAS/OpenBLAS library be found.\n")
endif()
add_executable(sayuri
${BASIC_SOURCES}
${GAME_SOURCES}
${PATTERN_SOURCES}
${MCTS_SOURCES}
${NEURAL_SOURCES}
${UTILS_SOURCES}
${SUMMARY_SOURCES}
${SELFPLAY_SOURCES}
${CUDA_SOURCES}
)
target_link_libraries(sayuri Threads::Threads)
target_link_libraries(sayuri ${BLAS_LIBRARIES})
if (USE_ZLIB)
target_link_libraries(sayuri ${ZLIB_LIBRARIES})
endif()
if(_USE_CUDA)
target_compile_definitions(sayuri PRIVATE USE_CUDA_BACKEND)
find_package(CUDA REQUIRED)
if(_USE_CUDNN)
message(STATUS "Include CuDNN library")
message(" Looking for cuDNN library...")
find_path(CUDNN_INCLUDE_DIR cudnn.h HINTS ${CUDNN_ROOT_DIR} ${CUDA_TOOLKIT_ROOT_DIR} PATH_SUFFIXES cuda/include include)
if((NOT CUDNN_INCLUDE_DIR))
message(ERROR "${ColorBoldRed} cudnn.h was NOT found, specify CUDNN_INCLUDE_DIR to indicate where it is.")
endif()
add_definitions(-DUSE_CUDNN)
find_library(CUDNN_LIBRARY libcudnn.so PATHS /usr/local/cuda/lib64 /opt/cuda/lib64)
include_directories(SYSTEM ${CUDNN_INCLUDE_DIR})
message(" The cuDNN library be found.\n")
endif()
include_directories(SYSTEM ${CUDA_INCLUDE_DIRS})
target_link_libraries(sayuri ${CUDNN_LIBRARY} ${CUDA_CUBLAS_LIBRARIES} ${CUDA_LIBRARIES})
endif()