forked from LeelaChessZero/lc0
-
Notifications
You must be signed in to change notification settings - Fork 0
/
meson.build
483 lines (403 loc) · 15.5 KB
/
meson.build
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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
# This file is part of Leela Chess Zero.
# Copyright (C) 2018-2019 The LCZero Authors
#
# Leela Chess is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Leela Chess is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Leela Chess. If not, see <http://www.gnu.org/licenses/>.
project('lc0', 'cpp',
default_options : ['cpp_std=c++14', 'b_ndebug=if-release', 'b_lto=true'],
meson_version: '>=0.45')
cc = meson.get_compiler('cpp')
if cc.get_id() == 'clang'
# Thread safety annotation
add_project_arguments('-Wthread-safety', language : 'cpp')
endif
if cc.get_id() == 'clang' or cc.get_id() == 'gcc'
add_project_arguments('-Wextra', language : 'cpp')
add_project_arguments('-pedantic', language : 'cpp')
if get_option('buildtype') == 'release'
add_project_arguments('-march=native', language : 'cpp')
endif
endif
# Files to compile.
deps = []
files = []
includes = []
has_backends = false
# Third party files.
includes += include_directories('third_party', is_system: true)
# Both protobuf and protoc must be the same version, so couple them together.
protobuf_lib = cc.find_library('libprotobuf', dirs : get_option('protobuf_libdir'), required : false)
if not protobuf_lib.found()
protobuf_dep = dependency('protobuf', required : false)
else
protobuf_dep = protobuf_lib
endif
protoc = find_program('protoc', required : false)
# For tensorflow skip system protobuf, chances are it will not work.
if get_option('protobuf-3-6-0')
deps += subproject('protobuf-3.6.0').get_variable('protobuf_dep')
protoc = subproject('protobuf-3.6.0').get_variable('protoc')
elif not protobuf_dep.found() or not protoc.found() or get_option('tensorflow')
deps += subproject('protobuf').get_variable('protobuf_dep')
protoc = subproject('protobuf').get_variable('protoc')
else
deps += protobuf_dep
if protobuf_lib.found()
includes += include_directories(get_option('protobuf_include'))
endif
endif
gen = generator(protoc, output: ['@[email protected]', '@[email protected]'],
arguments : ['--proto_path=@CURRENT_SOURCE_DIR@/libs/lczero-common', '--cpp_out=@BUILD_DIR@', '@INPUT@'])
if run_command('checkdir.py', 'libs/lczero-common/proto').returncode() != 0
if run_command('git', 'status').returncode() == 0
message('updating git submodule libs/lczero-common')
run_command('git', 'submodule', 'update', '--init', '--recursive')
else
message('cloning lczero-common.git into libs/lczero-common')
run_command('git', 'clone', '--depth=1',
'https://github.com/LeelaChessZero/lczero-common.git',
'libs/lczero-common/')
endif
endif
files += gen.process('libs/lczero-common/proto/net.proto',
preserve_path_from : meson.current_source_dir() + '/libs/lczero-common/')
#############################################################################
## Main files
#############################################################################
files += [
'src/engine.cc',
'src/version.cc',
'src/benchmark/benchmark.cc',
'src/chess/bitboard.cc',
'src/chess/board.cc',
'src/chess/position.cc',
'src/chess/uciloop.cc',
'src/mcts/node.cc',
'src/mcts/params.cc',
'src/mcts/search.cc',
'src/neural/cache.cc',
'src/neural/encoder.cc',
'src/neural/factory.cc',
'src/neural/loader.cc',
'src/neural/network_check.cc',
'src/neural/network_demux.cc',
'src/neural/network_legacy.cc',
'src/neural/network_mux.cc',
'src/neural/network_random.cc',
'src/neural/network_rr.cc',
'src/neural/network_st_batch.cc',
'src/neural/writer.cc',
'src/selfplay/game.cc',
'src/selfplay/loop.cc',
'src/selfplay/tournament.cc',
'src/syzygy/syzygy.cc',
'src/utils/commandline.cc',
'src/utils/configfile.cc',
'src/utils/histogram.cc',
'src/utils/logging.cc',
'src/utils/optionsdict.cc',
'src/utils/optionsparser.cc',
'src/utils/random.cc',
'src/utils/string.cc',
'src/utils/transpose.cc',
'src/utils/weights_adapter.cc',
]
includes += include_directories('src')
#############################################################################
## Platform specific files
############################################################################
if host_machine.system() == 'windows'
files += 'src/utils/filesystem.win32.cc'
else
files += 'src/utils/filesystem.posix.cc'
deps += [
cc.find_library('pthread'),
]
endif
#############################################################################
## BACKENDS
#############################################################################
if get_option('build_backends')
## ~~~~~~~~~~
## Tensorflow
## ~~~~~~~~~~
# Installed from https://github.com/FloopCZ/tensorflow_cc
tensorflow_include = get_option('tensorflow_include')
tensorflow_libdir = get_option('tensorflow_libdir')
tf_dl_lib = cc.find_library('dl', required: false)
tf_tensorflow_lib = cc.find_library('libtensorflow_cc',
dirs: tensorflow_libdir, required: false)
if get_option('tensorflow') and tf_dl_lib.found() and tf_tensorflow_lib.found()
includes += include_directories(
tensorflow_include,
tensorflow_include[0] + '/bazel-genfiles',
tensorflow_include[0] + '/tensorflow/contrib/makefile/downloads',
tensorflow_include[0] + '/tensorflow/contrib/makefile/downloads/absl',
tensorflow_include[0] + '/tensorflow/contrib/makefile/downloads/gemmlowp',
tensorflow_include[0] + '/tensorflow/contrib/makefile/downloads/nsync/public',
tensorflow_include[0] + '/tensorflow/contrib/makefile/gen/protobuf-host/include',
is_system: true
)
deps += [tf_dl_lib, tf_tensorflow_lib]
files += 'src/neural/network_tf.cc'
has_backends = true
endif
## ~~~~~
## Blas
## ~~~~~
shared_files = []
has_blas = false
accelerate_lib = dependency('Accelerate', required: false)
mkl_libdirs = get_option('mkl_libdirs')
mkl_lib = cc.find_library('mkl_rt', dirs: mkl_libdirs, required: false)
if not mkl_lib.found()
mkl_lib = cc.find_library('mklml', dirs: mkl_libdirs, required: false)
endif
openblas_libdirs = get_option('openblas_libdirs')
openblas_lib = cc.find_library('openblas.dll', dirs: openblas_libdirs, required: false)
if not openblas_lib.found()
openblas_lib = cc.find_library('openblas', dirs: openblas_libdirs, required: false)
endif
if get_option('blas')
add_project_arguments('-DUSE_MKL', language : 'cpp')
includes += include_directories(get_option('mkl_include'))
deps += [ mkl_lib ]
has_blas = true
if get_option('accelerate') and accelerate_lib.found()
includes += include_directories('/System/Library/Frameworks/Accelerate.framework/Frameworks/vecLib.framework/Headers')
deps += [ accelerate_lib ]
has_blas = true
elif get_option('openblas') and openblas_lib.found()
add_project_arguments('-DUSE_OPENBLAS', language : 'cpp')
required_openblas_header = 'openblas_config.h'
if not cc.has_header(required_openblas_header)
openblas_headers_found = false
# add the first valid include directory
foreach d : get_option('openblas_include')
if not openblas_headers_found and cc.has_header(required_openblas_header, args: '-I' + d)
includes += include_directories(d)
openblas_headers_found = true
endif
endforeach
if not openblas_headers_found
error('Failed to detect OpenBLAS headers. Did you install libopenblas-dev?')
endif
endif
deps += [ openblas_lib ]
has_blas = true
endif
ispc = find_program('ispc', required: false)
ispc_extra_args = []
if has_blas and get_option('ispc') and ispc.found()
ispc_native_only = get_option('ispc_native_only')
if host_machine.system() == 'windows'
outputnames = [ '@[email protected]']
if not ispc_native_only
outputnames += ['@BASENAME@_sse2.obj', '@BASENAME@_sse4.obj',
'@BASENAME@_avx.obj', '@BASENAME@_avx11.obj',
'@BASENAME@_avx2.obj' ]
endif
else
ispc_extra_args += ['--pic']
outputnames = [ '@[email protected]']
if not ispc_native_only
outputnames += ['@BASENAME@_sse2.o', '@BASENAME@_sse4.o',
'@BASENAME@_avx.o', '@BASENAME@_avx11.o',
'@BASENAME@_avx2.o' ]
endif
endif
if ispc_native_only
ispc_target = 'host'
else
ispc_target = 'sse2-i32x8,sse4-i32x8,avx1-i32x8,avx1.1-i32x8,avx2-i32x8'
endif
iscp_gen = generator(ispc,
output: [ '@BASENAME@_ispc.h', outputnames ],
arguments: [ '-O2', '--wno-perf', '--arch=x86-64',
'--target=' + ispc_target,
'@INPUT@', '-o', '@OUTPUT1@' ,'-h', '@OUTPUT0@' ]
+ ispc_extra_args
)
endif
endif
if get_option('blas') and has_blas
blas_files = [
'src/neural/blas/convolution1.cc',
'src/neural/blas/fully_connected_layer.cc',
'src/neural/blas/se_unit.cc',
'src/neural/blas/network_blas.cc',
'src/neural/blas/winograd_convolution3.cc'
]
shared_files = [
'src/neural/shared/activation.cc',
'src/neural/shared/winograd_filter.cc',
]
files += blas_files
has_backends = true
if get_option('ispc') and ispc.found()
files += iscp_gen.process('src/neural/blas/winograd_transform.ispc')
add_project_arguments('-DUSE_ISPC', language : 'cpp')
endif
endif
## ~~~~~
## OpenCL
## ~~~~~
has_opencl = false
opencl_libdirs = get_option('opencl_libdirs')
opencl_lib=cc.find_library('OpenCL', dirs: opencl_libdirs, required: false)
opencl_framework=dependency('OpenCL', required: false)
if opencl_framework.found()
deps += [ opencl_framework ]
has_opencl = true
elif opencl_lib.found()
deps += [ opencl_lib ]
has_opencl = true
endif
if get_option('opencl') and has_opencl
opencl_files = [
'src/neural/opencl/network_opencl.cc',
'src/neural/opencl/OpenCL.cc',
'src/neural/opencl/OpenCLTuner.cc',
'src/neural/opencl/OpenCLBuffers.cc',
]
shared_files = [
'src/neural/shared/activation.cc',
'src/neural/shared/winograd_filter.cc',
]
includes += include_directories(get_option('opencl_include'))
files += opencl_files
has_backends = true
endif
files += shared_files
## ~~~~~
## cuDNN
## ~~~~~
cudnn_libdirs = get_option('cudnn_libdirs')
cu_blas = cc.find_library('cublas', dirs: cudnn_libdirs, required: false)
cu_dnn = cc.find_library('cudnn', dirs: cudnn_libdirs, required: false)
cu_dart = cc.find_library('cudart', dirs: cudnn_libdirs, required: false)
nvcc = find_program('nvcc', '/usr/local/cuda/bin/nvcc', '/opt/cuda/bin/nvcc',
required: false)
#.cc files
cuda_files = [
'src/neural/cuda/network_cudnn.cc',
'src/neural/cuda/layers.cc',
]
# .cu files compiled without any specific sm version
cuda_files_nvcc_common = [
'src/neural/cuda/common_kernels.cu',
]
# .cu files compiled with sm_70 (volta+). Only used by cudnn-fp16
cuda_files_nvcc_fp16 = [
'src/neural/cuda/fp16_kernels.cu',
]
if get_option('cudnn') and cu_blas.found() and cu_dnn.found() and cu_dart.found() and nvcc.found()
deps += [cu_blas, cu_dnn, cu_dart]
foreach d : get_option('cudnn_include')
if run_command('checkdir.py', d).returncode() == 0
includes += include_directories(d)
endif
endforeach
includes += include_directories('src/neural/cuda/')
cuda_arguments = ['@EXTRA_ARGS@', '-c', '@INPUT@', '-o', '@OUTPUT@',
'-I', meson.current_source_dir() + '/src']
if host_machine.system() == 'windows'
# meson bug fixed in v0.51
if meson.version().version_compare('<0.51') or get_option('default_library') != 'static'
cuda_arguments += ['-Xcompiler', '-MD']
else
cuda_arguments += ['-Xcompiler', '-MT']
endif
else
cuda_arguments += ['--std=c++14', '-Xcompiler', '-fPIC']
endif
foreach x : get_option('cudnn_include')
cuda_arguments += ['-I', x]
endforeach
if host_machine.system() == 'windows'
outputname = '@[email protected]'
else
outputname = '@[email protected]'
endif
cuda_gen = generator(nvcc,
output: outputname,
arguments: cuda_arguments,
)
files += cuda_files
files += cuda_gen.process(cuda_files_nvcc_common)
files += cuda_gen.process(cuda_files_nvcc_fp16, extra_args: ['-arch=compute_70', '-code=sm_70'])
has_backends = true
endif
endif # if get_option('build_backends')
if not has_backends and get_option('build_backends')
error('''
No usable computation backends (cudnn/opencl/blas/etc) enabled.
If you want to build with the random backend only, add
-Dbuild_backends=false to the build command line.''')
endif
#############################################################################
## Dependencies
#############################################################################
## ~~~~
## zlib
## ~~~~
# Pick latest from https://wrapdb.mesonbuild.com/zlib and put into
# subprojects/zlib.wrap
deps += dependency('zlib', fallback: ['zlib', 'zlib_dep'])
## ~~~~~~~~
## Profiler
## ~~~~~~~~
if get_option('buildtype') != 'release'
deps += cc.find_library('libprofiler',
dirs: ['/usr/local/lib'], required: false)
endif
#############################################################################
## Main Executable
#############################################################################
if not get_option('popcnt')
add_project_arguments('-DNO_POPCNT', language : 'cpp')
endif
if not get_option('pext')
add_project_arguments('-DNO_PEXT', language : 'cpp')
endif
executable('lc0', 'src/main.cc',
files, include_directories: includes, dependencies: deps, install: true)
### Tests
if get_option('gtest')
gtest = dependency('gtest', fallback: ['gtest', 'gtest_dep'])
lc0_lib = library('lc0_lib', files, include_directories: includes, dependencies: deps)
test('ChessBoard',
executable('chessboard_test', 'src/chess/board_test.cc',
include_directories: includes, link_with: lc0_lib, dependencies: gtest
), args: '--gtest_output=xml:chessboard.xml', timeout: 90)
test('HashCat',
executable('hashcat_test', 'src/utils/hashcat_test.cc',
include_directories: includes, link_with: lc0_lib, dependencies: gtest
), args: '--gtest_output=xml:hashcat.xml', timeout: 90)
test('PositionTest',
executable('position_test', 'src/chess/position_test.cc',
include_directories: includes, link_with: lc0_lib, dependencies: gtest
), args: '--gtest_output=xml:position.xml', timeout: 90)
test('OptionsParserTest',
executable('optionsparser_test', 'src/utils/optionsparser_test.cc',
include_directories: includes, link_with: lc0_lib, dependencies: gtest
), timeout: 90)
test('SyzygyTest',
executable('syzygy_test', 'src/syzygy/syzygy_test.cc',
include_directories: includes, link_with: lc0_lib, dependencies: gtest
), args: '--gtest_output=xml:syzygy.xml', timeout: 90)
test('EncodePositionForNN',
executable('encoder_test', 'src/neural/encoder_test.cc',
include_directories: includes, link_with: lc0_lib, dependencies: gtest
), args: '--gtest_output=xml:encoder.xml', timeout: 90)
endif