forked from icl-utk-edu/blaspp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCMakeLists.txt
497 lines (443 loc) · 16.1 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
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
484
485
486
487
488
489
490
491
492
493
494
495
496
497
# Copyright (c) 2017-2022, University of Tennessee. All rights reserved.
# SPDX-License-Identifier: BSD-3-Clause
# This program is free software: you can redistribute it and/or modify it under
# the terms of the BSD 3-Clause license. See the accompanying LICENSE file.
#
# CMake script for BLAS++ library
# repo: http://bitbucket.org/icl/blaspp
# Tests require TestSweeper library from
# http://bitbucket.org/icl/testsweeper
cmake_minimum_required( VERSION 3.15 )
# 3.1 target_compile_features
# 3.8 target_compile_features( cxx_std_11 )
# 3.14 install( LIBRARY DESTINATION lib ) default
# 3.15 $<$COMPILE_LANG_AND_ID # optional
# 3.15 message DEBUG, string REPEAT
project(
blaspp
VERSION 2022.07.00
LANGUAGES CXX
)
# When built as a sub-project, add a namespace to make targets unique,
# e.g., `make tester` becomes `make blaspp_tester`.
if (CMAKE_PROJECT_NAME STREQUAL PROJECT_NAME)
set( blaspp_is_project true )
set( blaspp_ "" )
else()
set( blaspp_is_project false )
set( blaspp_ "blaspp_" )
endif()
#-------------------------------------------------------------------------------
# Options
if (blaspp_is_project)
set( log "" CACHE STRING "Shorthand for CMAKE_MESSAGE_LOG_LEVEL" )
set_property( CACHE log PROPERTY STRINGS
FATAL_ERROR SEND_ERROR WARNING AUTHOR_WARNING DEPRECATION
NOTICE STATUS VERBOSE DEBUG TRACE )
if (log)
set( CMAKE_MESSAGE_LOG_LEVEL "${log}" )
endif()
endif()
option( BUILD_SHARED_LIBS "Build shared libraries" true )
option( build_tests "Build test suite" "${blaspp_is_project}" )
option( color "Use ANSI color output" true )
option( use_cmake_find_blas "Use CMake's find_package( BLAS ) rather than the search in BLAS++" false )
option( use_openmp "Use OpenMP, if available" true )
set( gpu_backend "auto" CACHE STRING "GPU backend to use" )
set_property( CACHE gpu_backend PROPERTY STRINGS
auto cuda hip none )
# Deprecated since 2021-04
set( use_cuda "" CACHE STRING "Use CUDA, if available [deprecated: use gpu_backend]" )
set( use_hip "" CACHE STRING "Use HIP, if available [deprecated: use gpu_backend]" )
if (use_cuda)
message( DEPRECATION "WARNING: `use_cuda=${use_cuda}` is deprecated; "
"setting `gpu_backend = cuda`" )
set( gpu_backend "cuda" CACHE STRING "" FORCE )
elseif (use_hip)
message( DEPRECATION "WARNING: `use_hip=${use_hip}` is deprecated; "
"setting `gpu_backend = hip`" )
set( gpu_backend "hip" CACHE STRING "" FORCE )
elseif (NOT use_cuda STREQUAL "" OR NOT use_hip STREQUAL "")
# use_cuda or use_hip is explicitly false; the other may be unset or false.
# If both are unset, they are ignored.
message( DEPRECATION "WARNING: `use_cuda=${use_cuda}` and `use_hip=${use_hip}` are deprecated; "
"setting `gpu_backend = none`" )
set( gpu_backend "none" CACHE STRING "" FORCE )
endif()
# Default prefix=/opt/slate
if (CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT
AND blaspp_is_project)
set( prefix "/opt/slate" CACHE PATH "Shorthand for CMAKE_INSTALL_PREFIX" )
set( CMAKE_INSTALL_PREFIX "${prefix}"
CACHE PATH
"Install path prefix, prepended onto install directories."
FORCE
)
message( STATUS "Setting CMAKE_INSTALL_PREFIX = ${CMAKE_INSTALL_PREFIX}" )
# Append the new CMAKE_INSTALL_PREFIX, since CMake appended the old value.
# This helps find TestSweeper.
list( APPEND CMAKE_SYSTEM_PREFIX_PATH ${CMAKE_INSTALL_PREFIX} )
else()
message( STATUS "Using CMAKE_INSTALL_PREFIX = ${CMAKE_INSTALL_PREFIX}" )
endif()
# Provide menu of options. (Why doesn't CMake do this?)
set_property( CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS
None Debug Release RelWithDebInfo MinSizeRel )
# Provide menu of options.
set( BLA_VENDOR "" CACHE STRING
"BLAS Vendor for use in CMake's FindBLAS. If empty, use BLAS++ search. Some obsolete options are omitted here." )
set_property(
CACHE BLA_VENDOR PROPERTY STRINGS
"" All Goto OpenBLAS FLAME ATLAS IBMESSL
Intel10_32 Intel10_64lp Intel10_64lp_seq Intel10_64ilp Intel10_64ilp_seq
Intel10_64_dyn Apple NAS Arm Arm_mp Arm_ilp64 Arm_ilp64_mp Generic )
#-----------------------------------
# BLAS options
# todo: Goto, BLIS, FLAME, others?
set( blas "auto" CACHE STRING
"BLAS library to search for" )
set_property(
CACHE blas PROPERTY STRINGS
"auto" "Apple Accelerate" "Cray LibSci" "IBM ESSL"
"Intel MKL" "OpenBLAS" "AMD ACML" "generic" )
set( blas_fortran "auto" CACHE STRING
"For Intel MKL: use Intel ifort or GNU gfortran conventions?" )
set_property(
CACHE blas_fortran PROPERTY STRINGS
"auto" "GNU gfortran conventions" "Intel ifort conventions" )
set( blas_int "auto" CACHE STRING
"BLAS integer size: int (LP64) or int64_t (ILP64)" )
set_property(
CACHE blas_int PROPERTY STRINGS
"auto" "int (LP64)" "int64_t (ILP64)" )
set( blas_threaded "auto" CACHE STRING
"Multi-threaded BLAS?" )
set_property(
CACHE blas_threaded PROPERTY STRINGS
"auto" "true" "false" )
message( DEBUG "Settings:
CMAKE_VERSION = ${CMAKE_VERSION}
CMAKE_INSTALL_PREFIX = ${CMAKE_INSTALL_PREFIX}
CMAKE_BUILD_TYPE = ${CMAKE_BUILD_TYPE}
BUILD_SHARED_LIBS = ${BUILD_SHARED_LIBS}
BLA_VENDOR = ${BLA_VENDOR}
blas = ${blas}
blas_fortran = ${blas_fortran}
blas_int = ${blas_int}
blas_threaded = ${blas_threaded}
build_tests = ${build_tests}
color = ${color}
use_cmake_find_blas = ${use_cmake_find_blas}
gpu_backend = ${gpu_backend}
use_hip = ${use_hip}
use_cuda = ${use_cuda}
use_openmp = ${use_openmp}
blaspp_is_project = ${blaspp_is_project}
blaspp_ = ${blaspp_}
" )
#-------------------------------------------------------------------------------
# Enforce out-of-source build
string( TOLOWER "${CMAKE_CURRENT_SOURCE_DIR}" source_dir )
string( TOLOWER "${CMAKE_CURRENT_BINARY_DIR}" binary_dir )
if ("${source_dir}" STREQUAL "${binary_dir}")
message( FATAL_ERROR
"Compiling BLAS++ with CMake requires an out-of-source build. To proceed:
rm -rf CMakeCache.txt CMakeFiles/ # delete files in ${CMAKE_CURRENT_SOURCE_DIR}
mkdir build
cd build
cmake ..
make" )
endif()
#-------------------------------------------------------------------------------
# Build library.
add_library(
blaspp
src/asum.cc
src/axpy.cc
src/batch_gemm.cc
src/batch_hemm.cc
src/batch_her2k.cc
src/batch_herk.cc
src/batch_symm.cc
src/batch_syr2k.cc
src/batch_syrk.cc
src/batch_trmm.cc
src/batch_trsm.cc
src/copy.cc
src/dot.cc
src/gemm.cc
src/gemv.cc
src/ger.cc
src/geru.cc
src/hemm.cc
src/hemv.cc
src/her.cc
src/her2.cc
src/her2k.cc
src/herk.cc
src/iamax.cc
src/nrm2.cc
src/rot.cc
src/rotg.cc
src/rotm.cc
src/rotmg.cc
src/scal.cc
src/swap.cc
src/symm.cc
src/symv.cc
src/syr.cc
src/syr2.cc
src/syr2k.cc
src/syrk.cc
src/trmm.cc
src/trmv.cc
src/trsm.cc
src/trsv.cc
src/version.cc
src/device_batch_gemm.cc
src/device_batch_hemm.cc
src/device_batch_her2k.cc
src/device_batch_herk.cc
src/device_batch_symm.cc
src/device_batch_syr2k.cc
src/device_batch_syrk.cc
src/device_batch_trmm.cc
src/device_batch_trsm.cc
src/device_error.cc
src/device_gemm.cc
src/device_hemm.cc
src/device_her2k.cc
src/device_herk.cc
src/device_queue.cc
src/device_symm.cc
src/device_syr2k.cc
src/device_syrk.cc
src/device_axpy.cc
src/device_scal.cc
src/device_swap.cc
src/device_copy.cc
src/device_trmm.cc
src/device_trsm.cc
src/device_utils.cc
src/cublas_wrappers.cc
src/rocblas_wrappers.cc
src/stub_wrappers.cc
)
#-------------------------------------------------------------------------------
# CUDA support.
set( blaspp_use_cuda false ) # output in blasppConfig.cmake.in
if (gpu_backend MATCHES "^(auto|cuda)$")
include( CheckLanguage )
check_language( CUDA )
if (gpu_backend STREQUAL "cuda" AND NOT CMAKE_CUDA_COMPILER)
message( FATAL_ERROR "gpu_backend = ${gpu_backend}, but CUDA was not found")
endif()
else()
message( STATUS "Skipping CUDA search. gpu_backend = ${gpu_backend}" )
endif()
if (CMAKE_CUDA_COMPILER)
enable_language( CUDA )
# todo: check for cuda 10.0+ and force newer CMake version - if possible
# CMake 3.17 adds find_package( CUDAToolkit )
# with CUDA::cudart and CUDA::cublas targets.
# For compatibility with older CMake, for now do search ourselves.
find_library( cudart_lib cudart ${CMAKE_CUDA_IMPLICIT_LINK_DIRECTORIES} )
find_library( cublas_lib cublas ${CMAKE_CUDA_IMPLICIT_LINK_DIRECTORIES} )
set( blaspp_defs_cuda_ "-DBLAS_HAVE_CUBLAS" )
set( blaspp_use_cuda true )
# Some platforms need these to be public libraries.
target_link_libraries(
blaspp PUBLIC "${cudart_lib}" "${cublas_lib}" )
target_include_directories(
blaspp PUBLIC "${CMAKE_CUDA_TOOLKIT_INCLUDE_DIRECTORIES}" )
message( STATUS "Building CUDA support in BLAS++" )
else()
message( STATUS "Not building CUDA support in BLAS++" )
endif()
#-------------------------------------------------------------------------------
# HIP/ROCm support.
set( blaspp_use_hip false ) # output in blasppConfig.cmake.in
if (NOT CMAKE_CUDA_COMPILER
AND gpu_backend MATCHES "^(auto|hip)$")
message( STATUS "Looking for HIP/ROCm" )
if (gpu_backend STREQUAL "hip")
find_package( rocblas REQUIRED )
else()
find_package( rocblas QUIET )
endif()
else()
message( STATUS "Skipping HIP/ROCm search. gpu_backend = ${gpu_backend}" )
endif()
if (rocblas_FOUND)
set( blaspp_defs_hip_ "-DBLAS_HAVE_ROCBLAS" )
set( blaspp_use_hip true )
# Some platforms need these to be public libraries.
target_link_libraries( blaspp PUBLIC roc::rocblas )
message( STATUS "Building HIP/ROCm support in BLAS++" )
else()
message( STATUS "Not building HIP/ROCm support in BLAS++" )
endif()
#-------------------------------------------------------------------------------
# Clean stale defines.h from Makefile-based build.
file( REMOVE "${CMAKE_CURRENT_SOURCE_DIR}/include/blas/defines.h" )
# Include directory.
# During build it's {source}/include; after install it's {prefix}/include.
target_include_directories(
blaspp
PUBLIC
"$<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}/include>" # defines.h
"$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>"
"$<INSTALL_INTERFACE:include>"
)
# OpenMP support.
set( openmp_lib "" )
set( blaspp_use_openmp false ) # output in blasppConfig.cmake.in
if (NOT use_openmp)
message( STATUS "User has requested to NOT use OpenMP" )
else()
find_package( OpenMP )
if (OpenMP_CXX_FOUND)
set( openmp_lib "OpenMP::OpenMP_CXX" )
set( blaspp_use_openmp true )
target_link_libraries( blaspp PUBLIC "${openmp_lib}" )
endif()
endif()
# Get git commit id.
if (EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/.git")
execute_process( COMMAND git rev-parse --short HEAD
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
OUTPUT_VARIABLE blaspp_id )
string( STRIP "${blaspp_id}" blaspp_id )
message( STATUS "blaspp_id = ${blaspp_id}" )
# Don't put in blaspp_defs_ as the quotes cause parsing issues.
target_compile_definitions(
blaspp PRIVATE BLASPP_ID="${blaspp_id}" )
endif()
# Use and export -std=c++11; don't allow -std=gnu++11 extensions.
target_compile_features( blaspp PUBLIC cxx_std_11 )
set_target_properties( blaspp PROPERTIES
CXX_EXTENSIONS OFF
WINDOWS_EXPORT_ALL_SYMBOLS ON
)
if (CMAKE_VERSION VERSION_GREATER_EQUAL 3.15)
# Conditionally add -Wall. See CMake tutorial.
set( gcc_like_cxx "$<COMPILE_LANG_AND_ID:CXX,ARMClang,AppleClang,Clang,GNU>" )
target_compile_options(
blaspp PRIVATE "$<${gcc_like_cxx}:$<BUILD_INTERFACE:-Wall>>" )
endif()
#-------------------------------------------------------------------------------
# Search for BLAS library.
if (BLA_VENDOR OR use_cmake_find_blas)
message( DEBUG "Using CMake's FindBLAS" )
find_package( BLAS )
else()
message( DEBUG "Using BLASFinder" )
include( "cmake/BLASFinder.cmake" )
endif()
if (NOT BLAS_FOUND)
message( FATAL_ERROR "BLAS++ requires a BLAS library and none was found."
" Ensure that it is accessible in environment variables"
" $CPATH, $LIBRARY_PATH, and $LD_LIBRARY_PATH." )
endif()
include( "cmake/BLASConfig.cmake" )
# Only tester needs cblas, but always config it so LAPACK++ tester can use it.
include( "cmake/CBLASConfig.cmake" )
# Cache blaspp_defs_ that was built in BLASFinder, BLASConfig, CBLASConfig.
set( blaspp_defs_ "${blaspp_defs_}"
CACHE INTERNAL "Constants defined for BLAS" )
# Concat defines.
set( blaspp_defines ${blaspp_defs_} ${blaspp_defs_cuda_} ${blaspp_defs_hip_}
CACHE INTERNAL "")
if (true)
# Extract definitions as #define VAR or #define VAR VALUE.
set( blaspp_header_defines "" )
foreach (def IN LISTS blaspp_defines)
string( REGEX REPLACE "^-D" "" def "${def}" )
string( REGEX REPLACE "=" " " def "${def}" )
string( APPEND blaspp_header_defines "#define ${def}\n" )
endforeach()
cmake_host_system_information( RESULT HOSTNAME QUERY HOSTNAME )
# ctime format: Mon Nov 16 15:19:47 2020
string( TIMESTAMP datetime "%a %b %d %H:%M:%S %Y" )
# Pass defines via header.
configure_file(
include/blas/defines.h.in # in source dir
include/blas/defines.h # in binary dir
)
else()
# Pass defines via compiler flags.
target_compile_definitions( blaspp PRIVATE ${blaspp_defines} )
endif()
set( blaspp_libraries "${BLAS_LIBRARIES};${openmp_lib}" CACHE INTERNAL "" )
message( DEBUG "blaspp_libraries = '${blaspp_libraries}'" )
# blaspp_libraries could be private, but then if an application directly
# calls blas, cblas, lapack, lapacke, mkl, essl, etc., it would need to
# devine the exact same blaspp_libraries. For example, the tester calls
# cblas. Instead, make it public.
target_link_libraries( blaspp PUBLIC ${blaspp_libraries} )
# Add 'make lib' target.
if (blaspp_is_project)
add_custom_target( lib DEPENDS blaspp )
endif()
#-------------------------------------------------------------------------------
if (build_tests)
# Only tester needs lapack.
include( "cmake/LAPACKFinder.cmake" )
add_subdirectory( test )
endif()
#-------------------------------------------------------------------------------
# Install rules.
# GNU Filesystem Conventions
include( GNUInstallDirs )
if (WIN32)
set( install_configdir "blaspp" )
else()
set( install_configdir "${CMAKE_INSTALL_LIBDIR}/blaspp" )
endif()
# Install library and add to <package>Targets.cmake
install(
TARGETS blaspp
EXPORT blasppTargets
LIBRARY DESTINATION "${CMAKE_INSTALL_LIBDIR}"
ARCHIVE DESTINATION "${CMAKE_INSTALL_LIBDIR}"
RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}"
)
# Install header files
install(
# / copies contents, not directory itself
DIRECTORY "${PROJECT_SOURCE_DIR}/include/"
DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}"
FILES_MATCHING REGEX "\\.(h|hh)$"
)
install(
FILES "${PROJECT_BINARY_DIR}/include/blas/defines.h"
DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}/blas"
)
# Install <package>Targets.cmake
install(
EXPORT blasppTargets
DESTINATION "${install_configdir}"
)
# Also export <package>Targets.cmake in build directory
export(
EXPORT blasppTargets
FILE "blasppTargets.cmake"
)
# Install <package>Config.cmake and <package>ConfigVersion.cmake,
# to enable find_package( <package> ).
include( CMakePackageConfigHelpers )
configure_package_config_file(
"blasppConfig.cmake.in"
"blasppConfig.cmake"
INSTALL_DESTINATION "${install_configdir}"
)
write_basic_package_version_file(
"blasppConfigVersion.cmake"
VERSION "${blaspp_VERSION}"
COMPATIBILITY AnyNewerVersion
)
install(
FILES "${CMAKE_CURRENT_BINARY_DIR}/blasppConfig.cmake"
"${CMAKE_CURRENT_BINARY_DIR}/blasppConfigVersion.cmake"
DESTINATION "${install_configdir}"
)