forked from icl-utk-edu/blaspp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBLASFinder.cmake
519 lines (450 loc) · 20.4 KB
/
BLASFinder.cmake
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
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
# Copyright (c) 2017-2023, 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.
# Convert to list, as blas_libs is later, to match cached value.
string( REGEX REPLACE "([^ ])( +|\\\;)" "\\1;" BLAS_LIBRARIES "${BLAS_LIBRARIES}" )
string( REGEX REPLACE "-framework;" "-framework " BLAS_LIBRARIES "${BLAS_LIBRARIES}" )
message( DEBUG "BLAS_LIBRARIES '${BLAS_LIBRARIES}'" )
message( DEBUG " cached '${blas_libraries_cached}'" )
message( DEBUG "blas '${blas}'" )
message( DEBUG " cached '${blas_cached}'" )
message( DEBUG "blas_int '${blas_int}'" )
message( DEBUG " cached '${blas_int_cached}'" )
message( DEBUG "blas_fortran '${blas_fortran}'" )
message( DEBUG " cached '${blas_fortran_cached}'" )
message( DEBUG "blas_threaded '${blas_threaded}'" )
message( DEBUG " cached '${blas_threaded_cached}'" )
message( DEBUG "" )
include( "cmake/util.cmake" )
message( STATUS "${bold}Looking for BLAS libraries and options${not_bold} (blas = ${blas})" )
#-----------------------------------
# Check if this file has already been run with these settings (see bottom).
set( run_ true )
if (BLAS_LIBRARIES
AND NOT "${blas_libraries_cached}" STREQUAL "${BLAS_LIBRARIES}")
# Ignore blas, etc. if BLAS_LIBRARIES changes.
# Set to empty, rather than unset, so when cmake is invoked again
# they don't force a search.
message( DEBUG "clear blas, blas_fortran, blas_int, blas_threaded" )
set( blas "" CACHE INTERNAL "" )
set( blas_fortran "" CACHE INTERNAL "" )
set( blas_int "" CACHE INTERNAL "" )
set( blas_threaded "" CACHE INTERNAL "" )
elseif (NOT ( "${blas_cached}" STREQUAL "${blas}"
AND "${blas_fortran_cached}" STREQUAL "${blas_fortran}"
AND "${blas_int_cached}" STREQUAL "${blas_int}"
AND "${blas_threaded_cached}" STREQUAL "${blas_threaded}"))
# Ignore BLAS_LIBRARIES if blas* changed.
message( DEBUG "unset BLAS_LIBRARIES" )
set( BLAS_LIBRARIES "" CACHE INTERNAL "" )
else()
message( DEBUG "BLAS search already done for
blas = ${blas}
blas_fortran = ${blas_fortran}
blas_int = ${blas_int}
blas_threaded = ${blas_threaded}
BLAS_LIBRARIES = ${BLAS_LIBRARIES}" )
set( run_ false )
endif()
#===============================================================================
# Matching endif at bottom.
if (run_)
#-------------------------------------------------------------------------------
# Prints the BLAS_{name,libs}_lists.
# This uses CMAKE_MESSAGE_LOG_LEVEL rather than message( DEBUG, ... )
# because the extra "-- " cmake prints were quite distracting.
# Usage: cmake -DCMAKE_MESSAGE_LOG_LEVEL=DEBUG ..
#
function( debug_print_list msg )
if ("${CMAKE_MESSAGE_LOG_LEVEL}" MATCHES "DEBUG|TRACE")
message( "---------- lists: ${msg}" )
message( "blas_name_list = ${blas_name_list}" )
message( "blas_libs_list = ${blas_libs_list}" )
message( "\nrow; ${red}blas_name;${plain} blas_libs" )
set( i 0 )
foreach (name IN LISTS blas_name_list)
list( GET blas_libs_list ${i} libs )
message( "${i}; ${red}${name};${plain} ${libs}" )
math( EXPR i "${i} + 1" )
endforeach()
message( "" )
endif()
endfunction()
#-------------------------------------------------------------------------------
# Setup.
#---------------------------------------- compiler
string( COMPARE EQUAL "${CMAKE_CXX_COMPILER_ID}" "GNU" gnu_compiler)
string( COMPARE EQUAL "${CMAKE_CXX_COMPILER_ID}" "IntelLLVM" intelllvm_compiler )
string( COMPARE EQUAL "${CMAKE_CXX_COMPILER_ID}" "Intel" intel_compiler )
string( REGEX MATCH "XL|XLClang" ibm_compiler "${CMAKE_CXX_COMPILER_ID}" )
#---------------------------------------- Fortran manglings to test
if (ibm_compiler)
# For IBM XL, change default mangling search order to lower, add_, upper,
# ESSL includes all 3, but Netlib LAPACK has only one mangling.
set( fortran_mangling_list
"-DBLAS_FORTRAN_LOWER"
"-DBLAS_FORTRAN_ADD_"
"-DBLAS_FORTRAN_UPPER"
)
else()
# For all others, mangling search order as add_, lower, upper,
# since add_ is the most common.
set( fortran_mangling_list
"-DBLAS_FORTRAN_ADD_"
"-DBLAS_FORTRAN_LOWER"
"-DBLAS_FORTRAN_UPPER"
)
endif()
#---------------------------------------- integer sizes to test
set( int_size_list
" " # int (LP64)
"-DBLAS_ILP64" # int64_t (ILP64)
)
#-------------------------------------------------------------------------------
# Parse options: BLAS_LIBRARIES, blas, blas_int, blas_threaded, blas_fortran.
#---------------------------------------- BLAS_LIBRARIES
if (BLAS_LIBRARIES)
set( test_blas_libraries true )
endif()
#---------------------------------------- blas
string( TOLOWER "${blas}" blas_ )
string( REGEX MATCH "auto|apple|accelerate" test_accelerate "${blas_}" )
string( REGEX MATCH "auto|aocl|blis" test_blis "${blas_}" )
string( REGEX MATCH "auto|cray|libsci|default" test_default "${blas_}" )
string( REGEX MATCH "auto|ibm|essl" test_essl "${blas_}" )
string( REGEX MATCH "auto|intel|mkl" test_mkl "${blas_}" )
string( REGEX MATCH "auto|openblas" test_openblas "${blas_}" )
string( REGEX MATCH "auto|generic" test_generic "${blas_}" )
message( DEBUG "
BLAS_LIBRARIES = '${BLAS_LIBRARIES}'
blas = '${blas}'
blas_ = '${blas_}'
test_blas_libraries = '${test_blas_libraries}'
test_accelerate = '${test_accelerate}'
test_blis = '${test_blis}'
test_default = '${test_default}'
test_essl = '${test_essl}'
test_mkl = '${test_mkl}'
test_openblas = '${test_openblas}'
test_generic = '${test_generic}'" )
#---------------------------------------- blas_fortran
string( TOLOWER "${blas_fortran}" blas_fortran_ )
string( REGEX MATCH "auto|gfortran" test_gfortran "${blas_fortran_}" )
string( REGEX MATCH "auto|ifort" test_ifort "${blas_fortran_}" )
message( DEBUG "
blas_fortran = '${blas_fortran}'
blas_fortran_ = '${blas_fortran_}'
test_gfortran = '${test_gfortran}'
test_ifort = '${test_ifort}'")
#---------------------------------------- blas_int
string( TOLOWER "${blas_int}" blas_int_ )
# This regex is similar to "\b(lp64|int)\b".
set( regex_int32 "(^|[^a-zA-Z0-9_])(auto|lp64|int|int32|int32_t)($|[^a-zA-Z0-9_])" )
set( regex_int64 "(^|[^a-zA-Z0-9_])(auto|ilp64|int64|int64_t)($|[^a-zA-Z0-9_])" )
string( REGEX MATCH ${regex_int32} test_int "${blas_int_}" )
string( REGEX MATCH ${regex_int64} test_int64 "${blas_int_}" )
if (CMAKE_CROSSCOMPILING AND test_int AND test_int64)
message( FATAL_ERROR " ${red}When cross-compiling, one must define either\n"
" `blas_int=int32` (usual convention) or\n"
" `blas_int=int64` (ilp64 convention).${plain}" )
endif()
message( DEBUG "
blas_int = '${blas_int}'
blas_int_ = '${blas_int_}'
test_int = '${test_int}'
test_int64 = '${test_int64}'")
#---------------------------------------- blas_threaded
string( TOLOWER "${blas_threaded}" blas_threaded_ )
# These regex are similar to "\b(yes|...)\b".
set( regex_yes "(^|[^a-zA-Z0-9_])(auto|y|yes|true|on|1)($|[^a-zA-Z0-9_])" )
set( regex_no "(^|[^a-zA-Z0-9_])(auto|n|no|false|off|0)($|[^a-zA-Z0-9_])" )
string( REGEX MATCH ${regex_yes} test_threaded "${blas_threaded_}" )
string( REGEX MATCH ${regex_no} test_sequential "${blas_threaded_}" )
message( DEBUG "
blas_threaded = '${blas_threaded}'
blas_threaded_ = '${blas_threaded_}'
test_threaded = '${test_threaded}'
test_sequential = '${test_sequential}'")
#-------------------------------------------------------------------------------
# Build list of libraries to check.
# todo: BLAS_?(ROOT|DIR)
set( blas_name_list "" )
set( blas_libs_list "" )
#---------------------------------------- BLAS_LIBRARIES
if (test_blas_libraries)
# Escape ; semi-colons so we can append it as one item to a list.
string( REPLACE ";" "\\;" BLAS_LIBRARIES_ESC "${BLAS_LIBRARIES}" )
message( DEBUG "BLAS_LIBRARIES ${BLAS_LIBRARIES}" )
message( DEBUG " => ${BLAS_LIBRARIES_ESC}" )
list( APPEND blas_name_list "\$BLAS_LIBRARIES" )
list( APPEND blas_libs_list "${BLAS_LIBRARIES_ESC}" )
debug_print_list( "BLAS_LIBRARIES" )
endif()
#---------------------------------------- default; Cray libsci
if (test_default)
list( APPEND blas_name_list "default (no library)" )
list( APPEND blas_libs_list " " ) # Use space so APPEND works later.
debug_print_list( "default" )
endif()
#---------------------------------------- Intel MKL
if (test_mkl)
# todo: MKL_?(ROOT|DIR)
if (test_threaded AND OpenMP_CXX_FOUND)
if (test_gfortran AND gnu_compiler)
# GNU compiler + OpenMP: require gnu_thread library.
if (test_int)
list( APPEND blas_name_list "Intel MKL lp64, GNU threads (gomp), gfortran")
list( APPEND blas_libs_list "-lmkl_gf_lp64 -lmkl_gnu_thread -lmkl_core" )
endif()
if (test_int64)
list( APPEND blas_name_list "Intel MKL ilp64, GNU threads (gomp), gfortran")
list( APPEND blas_libs_list "-lmkl_gf_ilp64 -lmkl_gnu_thread -lmkl_core" )
endif()
elseif (test_ifort AND intelllvm_compiler)
# IntelLLVM compiler + OpenMP: require intel_thread library.
if (test_int)
list( APPEND blas_name_list "Intel MKL lp64, Intel threads (iomp5), ifort")
list( APPEND blas_libs_list "-lmkl_intel_lp64 -lmkl_intel_thread -lmkl_core" )
elseif (test_int64)
list( APPEND blas_name_list "Intel MKL ilp64, Intel threads (iomp5), ifort")
list( APPEND blas_libs_list "-lmkl_intel_ilp64 -lmkl_intel_thread -lmkl_core" )
endif()
elseif (test_ifort AND intel_compiler)
# Intel compiler + OpenMP: require intel_thread library.
if (test_int)
list( APPEND blas_name_list "Intel MKL lp64, Intel threads (iomp5), ifort")
list( APPEND blas_libs_list "-lmkl_intel_lp64 -lmkl_intel_thread -lmkl_core" )
endif()
if (test_int64)
list( APPEND blas_name_list "Intel MKL ilp64, Intel threads (iomp5), ifort")
list( APPEND blas_libs_list "-lmkl_intel_ilp64 -lmkl_intel_thread -lmkl_core" )
endif()
else()
# MKL doesn't have libraries for other OpenMP backends.
message( "Skipping threaded MKL for non-GNU, non-Intel compiler with OpenMP" )
endif()
endif()
#----------
if (test_sequential)
# If Intel compiler, prefer Intel ifort interfaces.
if (test_ifort AND intel_compiler)
if (test_int)
list( APPEND blas_name_list "Intel MKL lp64, sequential, ifort" )
list( APPEND blas_libs_list "-lmkl_intel_lp64 -lmkl_sequential -lmkl_core" )
endif()
if (test_int64)
list( APPEND blas_name_list "Intel MKL ilp64, sequential, ifort" )
list( APPEND blas_libs_list "-lmkl_intel_ilp64 -lmkl_sequential -lmkl_core" )
endif()
endif() # ifort
# Otherwise, prefer GNU gfortran interfaces.
if (test_gfortran)
if (test_int)
list( APPEND blas_name_list "Intel MKL lp64, sequential, gfortran" )
list( APPEND blas_libs_list "-lmkl_gf_lp64 -lmkl_sequential -lmkl_core" )
endif()
if (test_int64)
list( APPEND blas_name_list "Intel MKL ilp64, sequential, gfortran" )
list( APPEND blas_libs_list "-lmkl_gf_ilp64 -lmkl_sequential -lmkl_core" )
endif()
endif() # gfortran
# Not Intel compiler, lower preference for Intel ifort interfaces.
# todo: same as Intel block above.
if (test_ifort AND NOT intel_compiler)
if (test_int)
list( APPEND blas_name_list "Intel MKL lp64, sequential, ifort" )
list( APPEND blas_libs_list "-lmkl_intel_lp64 -lmkl_sequential -lmkl_core" )
endif()
if (test_int64)
list( APPEND blas_name_list "Intel MKL ilp64, sequential, ifort" )
list( APPEND blas_libs_list "-lmkl_intel_ilp64 -lmkl_sequential -lmkl_core" )
endif()
endif() # ifort && not intel
endif() # sequential
debug_print_list( "mkl" )
endif() # MKL
#---------------------------------------- IBM ESSL
if (test_essl)
# todo: ESSL_?(ROOT|DIR)
if (test_threaded)
#message( "essl OpenMP_CXX_FOUND ${OpenMP_CXX_FOUND}" )
#if (ibm_compiler)
# if (test_int)
# list( APPEND blas_name_list "IBM ESSL int (lp64), multi-threaded" )
# list( APPEND blas_libs_list "-lesslsmp -lxlsmp" )
# # ESSL manual says '-lxlf90_r -lxlfmath' also,
# # but this doesn't work on Summit
# endif()
#
# if (test_int64)
# list( APPEND blas_name_list "IBM ESSL int64 (ilp64), multi-threaded" )
# list( APPEND blas_libs_list "-lesslsmp6464 -lxlsmp" )
# endif()
#else
if (OpenMP_CXX_FOUND)
if (test_int)
list( APPEND blas_name_list "IBM ESSL int (lp64), multi-threaded, with OpenMP" )
list( APPEND blas_libs_list "-lesslsmp" )
endif()
if (test_int64)
list( APPEND blas_name_list "IBM ESSL int64 (ilp64), multi-threaded, with OpenMP" )
list( APPEND blas_libs_list "-lesslsmp6464" )
endif()
endif()
endif() # threaded
if (test_sequential)
if (test_int)
list( APPEND blas_name_list "IBM ESSL int (lp64), sequential" )
list( APPEND blas_libs_list "-lessl" )
endif()
if (test_int64)
list( APPEND blas_name_list "IBM ESSL int64 (ilp64), sequential" )
list( APPEND blas_libs_list "-lessl6464" )
endif()
endif() # sequential
debug_print_list( "essl" )
endif()
#---------------------------------------- OpenBLAS
if (test_openblas)
# todo: OPENBLAS_?(ROOT|DIR)
list( APPEND blas_name_list "OpenBLAS" )
list( APPEND blas_libs_list "-lopenblas" )
debug_print_list( "openblas" )
endif()
#---------------------------------------- BLIS (also used by AMD AOCL)
if (test_blis)
list( APPEND blas_name_list "BLIS" )
list( APPEND blas_libs_list "-lflame -lblis" )
debug_print_list( "blis" )
endif()
#---------------------------------------- Apple Accelerate
if (test_accelerate)
list( APPEND blas_name_list "Apple Accelerate" )
list( APPEND blas_libs_list "-framework Accelerate" )
debug_print_list( "accelerate" )
endif()
#---------------------------------------- generic -lblas
if (test_generic)
list( APPEND blas_name_list "generic" )
list( APPEND blas_libs_list "-lblas" )
debug_print_list( "generic" )
endif()
#-------------------------------------------------------------------------------
# Check each BLAS library.
unset( BLAS_FOUND CACHE )
unset( blaspp_defs_ CACHE )
set( i 0 )
foreach (blas_name IN LISTS blas_name_list)
message( TRACE "i: ${i}" )
list( GET blas_libs_list ${i} blas_libs )
math( EXPR i "${i}+1" )
if (i GREATER 1)
message( "" )
endif()
message( "${blas_name}" )
message( " libs: ${blas_libs}" )
# Strip to deal with default lib being space, " ".
# Undo escaping \; semi-colons and split on spaces to make list.
# But keep '-framework Accelerate' together as one item.
message( DEBUG " blas_libs: '${blas_libs}'" )
string( STRIP "${blas_libs}" blas_libs )
string( REGEX REPLACE "([^ ])( +|\\\;)" "\\1;" blas_libs "${blas_libs}" )
string( REGEX REPLACE "-framework;" "-framework " blas_libs "${blas_libs}" )
message( DEBUG " blas_libs: '${blas_libs}' (split)" )
foreach (mangling IN LISTS fortran_mangling_list)
foreach (int_size IN LISTS int_size_list)
set( label " ${mangling} ${int_size}" )
pad_string( "${label}" 50 label )
# Try to link a simple hello world with the library.
try_compile(
link_result ${CMAKE_CURRENT_BINARY_DIR}
SOURCES
"${CMAKE_CURRENT_SOURCE_DIR}/config/hello.cc"
LINK_LIBRARIES
${blas_libs} ${openmp_lib} # not "..." quoted; screws up OpenMP
COMPILE_DEFINITIONS
"${mangling} ${int_size}"
OUTPUT_VARIABLE
link_output
)
debug_try_compile( "hello.cc" "${link_result}" "${link_output}" )
# If hello didn't link, assume library not found,
# so break both mangling & int_size loops.
if (NOT link_result)
message( "${label} ${red} no (library not found)${plain}" )
break()
endif()
# Try to link and run simple BLAS routine with the library.
try_run(
run_result compile_result ${CMAKE_CURRENT_BINARY_DIR}
SOURCES
"${CMAKE_CURRENT_SOURCE_DIR}/config/blas.cc"
LINK_LIBRARIES
${blas_libs} ${openmp_lib} # not "..." quoted; screws up OpenMP
COMPILE_DEFINITIONS
"${mangling} ${int_size}"
COMPILE_OUTPUT_VARIABLE
compile_output
RUN_OUTPUT_VARIABLE
run_output
)
# For cross-compiling, if it links, assume the run is okay.
# User must set blas_int=int64 for ILP64, otherwise assumes int32.
if (CMAKE_CROSSCOMPILING AND compile_result)
message( DEBUG "cross: blas_int = '${blas_int}'" )
set( run_result "0" CACHE STRING "" FORCE )
set( run_output "ok" CACHE STRING "" FORCE )
endif()
debug_try_run( "blas.cc" "${compile_result}" "${compile_output}"
"${run_result}" "${run_output}" )
if (NOT compile_result)
# If int32 didn't link, int64 won't either, so break int_size loop.
message( "${label} ${red} no (didn't link: routine not found)${plain}" )
break()
elseif ("${run_output}" MATCHES "ok")
# If it runs and prints ok, we're done, so break all 3 loops.
message( "${label} ${blue} yes${plain}" )
set( BLAS_FOUND true CACHE INTERNAL "" )
set( BLAS_LIBRARIES "${blas_libs}" CACHE STRING "" FORCE )
if (mangling MATCHES "[^ ]") # non-empty
list( APPEND blaspp_defs_ "${mangling}" )
endif()
if (int_size MATCHES "[^ ]") # non-empty
list( APPEND blaspp_defs_ "${int_size}" )
endif()
break()
else()
message( "${label} ${red} no (didn't run: int mismatch, etc.)${plain}" )
endif()
endforeach()
# Break loops as described above.
if (NOT link_result OR BLAS_FOUND)
break()
endif()
endforeach()
# Break loops as described above.
if (BLAS_FOUND)
break()
endif()
endforeach()
endif() # run_
#===============================================================================
# Mark as already run (see top).
set( blas_libraries_cached ${BLAS_LIBRARIES} CACHE INTERNAL "" )
set( blas_cached ${blas} CACHE INTERNAL "" )
set( blas_fortran_cached ${blas_fortran} CACHE INTERNAL "" )
set( blas_int_cached ${blas_int} CACHE INTERNAL "" )
set( blas_threaded_cached ${blas_threaded} CACHE INTERNAL "" )
#-------------------------------------------------------------------------------
if (BLAS_FOUND)
message( "${blue} Found BLAS library: ${BLAS_LIBRARIES}${plain}" )
else()
message( "${red} BLAS library not found.${plain}" )
endif()
message( DEBUG "
BLAS_FOUND = '${BLAS_FOUND}'
BLAS_LIBRARIES = '${BLAS_LIBRARIES}'
blaspp_defs_ = '${blaspp_defs_}'
")
message( "" )