forked from icl-utk-edu/blaspp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBLASConfig.cmake
311 lines (285 loc) · 11.8 KB
/
BLASConfig.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
# 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.
include( "cmake/util.cmake" )
# Check if this file has already been run with these settings (see bottom).
set( run_ true )
if (DEFINED blas_config_cache
AND "${blas_config_cache}" STREQUAL "${BLAS_LIBRARIES}")
message( DEBUG "BLAS config already done for '${BLAS_LIBRARIES}'" )
set( run_ false )
endif()
#===============================================================================
# Matching endif at bottom.
if (run_)
#-------------------------------------------------------------------------------
# Search to identify library and get version. Besides providing the
# specific version, this is also helpful to identify libraries given by
# CMake's find_package( BLAS ) or by the user in BLAS_LIBRARIES.
message( STATUS "Checking BLAS library version" )
set( found false )
#---------------------------------------- Apple Accelerate
# There's no accelerate_version() function that I could find,
# so just look for the framework.
if (NOT found)
if ("${BLAS_LIBRARIES}" MATCHES "-framework Accelerate|Accelerate.framework")
message( "${blue} Accelerate framework${plain}" )
list( APPEND blaspp_defs_ "-DBLAS_HAVE_ACCELERATE" )
set( found true )
if (NOT DEFINED blas_return_float_f2c)
set( blas_return_float_f2c true )
endif()
endif()
endif()
#---------------------------------------- Intel MKL
if (NOT found)
try_run(
run_result compile_result ${CMAKE_CURRENT_BINARY_DIR}
SOURCES
"${CMAKE_CURRENT_SOURCE_DIR}/config/mkl_version.cc"
LINK_LIBRARIES
${BLAS_LIBRARIES} ${openmp_lib} # not "..." quoted; screws up OpenMP
COMPILE_DEFINITIONS
${blaspp_defs_}
COMPILE_OUTPUT_VARIABLE
compile_output
RUN_OUTPUT_VARIABLE
run_output
)
# For cross-compiling, if it links, assume the run is okay.
if (CMAKE_CROSSCOMPILING AND compile_result)
message( DEBUG "cross: mkl" )
set( run_result "0" CACHE STRING "" FORCE )
set( run_output "MKL_VERSION=unknown" CACHE STRING "" FORCE )
endif()
debug_try_run( "mkl_version.cc" "${compile_result}" "${compile_output}"
"${run_result}" "${run_output}" )
if (compile_result AND "${run_output}" MATCHES "MKL_VERSION")
message( "${blue} ${run_output}${plain}" )
list( APPEND blaspp_defs_ "-DBLAS_HAVE_MKL" )
set( found true )
endif()
endif()
#---------------------------------------- BLIS
if (NOT found)
try_run(
run_result compile_result ${CMAKE_CURRENT_BINARY_DIR}
SOURCES
"${CMAKE_CURRENT_SOURCE_DIR}/config/blis_version.cc"
LINK_LIBRARIES
${BLAS_LIBRARIES} ${openmp_lib} # not "..." quoted; screws up OpenMP
COMPILE_DEFINITIONS
${blaspp_defs_}
COMPILE_OUTPUT_VARIABLE
compile_output
RUN_OUTPUT_VARIABLE
run_output
)
# For cross-compiling, if it links, assume the run is okay.
if (CMAKE_CROSSCOMPILING AND compile_result)
message( DEBUG "cross: blis" )
set( run_result "0" CACHE STRING "" FORCE )
set( run_output "BLIS_VERSION=unknown" CACHE STRING "" FORCE )
endif()
debug_try_run( "blis_version.cc" "${compile_result}" "${compile_output}"
"${run_result}" "${run_output}" )
if (compile_result AND "${run_output}" MATCHES "BLIS_VERSION")
message( "${blue} ${run_output}${plain}" )
list( APPEND blaspp_defs_ "-DBLAS_HAVE_BLIS" )
set( found true )
endif()
endif()
#---------------------------------------- IBM ESSL
if (NOT found)
try_run(
run_result compile_result ${CMAKE_CURRENT_BINARY_DIR}
SOURCES
"${CMAKE_CURRENT_SOURCE_DIR}/config/essl_version.cc"
LINK_LIBRARIES
${BLAS_LIBRARIES} ${openmp_lib} # not "..." quoted; screws up OpenMP
COMPILE_DEFINITIONS
${blaspp_defs_}
COMPILE_OUTPUT_VARIABLE
compile_output
RUN_OUTPUT_VARIABLE
run_output
)
# For cross-compiling, if it links, assume the run is okay.
if (CMAKE_CROSSCOMPILING AND compile_result)
message( DEBUG "cross: essl" )
set( run_result "0" CACHE STRING "" FORCE )
set( run_output "ESSL_VERSION=unknown" CACHE STRING "" FORCE )
endif()
debug_try_run( "essl_version.cc" "${compile_result}" "${compile_output}"
"${run_result}" "${run_output}" )
if (compile_result AND "${run_output}" MATCHES "ESSL_VERSION")
message( "${blue} ${run_output}${plain}" )
list( APPEND blaspp_defs_ "-DBLAS_HAVE_ESSL" )
set( found true )
endif()
endif()
#---------------------------------------- OpenBLAS
if (NOT found)
try_run(
run_result compile_result ${CMAKE_CURRENT_BINARY_DIR}
SOURCES
"${CMAKE_CURRENT_SOURCE_DIR}/config/openblas_version.cc"
LINK_LIBRARIES
${BLAS_LIBRARIES} ${openmp_lib} # not "..." quoted; screws up OpenMP
COMPILE_DEFINITIONS
${blaspp_defs_}
COMPILE_OUTPUT_VARIABLE
compile_output
RUN_OUTPUT_VARIABLE
run_output
)
# For cross-compiling, if it links, assume the run is okay.
if (CMAKE_CROSSCOMPILING AND compile_result)
message( DEBUG "cross: openblas" )
set( run_result "0" CACHE STRING "" FORCE )
set( run_output "OPENBLAS_VERSION=unknown" CACHE STRING "" FORCE )
endif()
debug_try_run( "openblas_version.cc" "${compile_result}" "${compile_output}"
"${run_result}" "${run_output}" )
if (compile_result AND "${run_output}" MATCHES "OPENBLAS_VERSION")
message( "${blue} ${run_output}${plain}" )
list( APPEND blaspp_defs_ "-DBLAS_HAVE_OPENBLAS" )
set( found true )
endif()
endif()
# todo: detect Accelerate
# todo: detect Cray libsci
#-------------------------------------------------------------------------------
message( STATUS "Checking BLAS complex return type" )
try_run(
run_result compile_result ${CMAKE_CURRENT_BINARY_DIR}
SOURCES
"${CMAKE_CURRENT_SOURCE_DIR}/config/return_complex.cc"
LINK_LIBRARIES
${BLAS_LIBRARIES} ${openmp_lib} # not "..." quoted; screws up OpenMP
COMPILE_DEFINITIONS
${blaspp_defs_}
COMPILE_OUTPUT_VARIABLE
compile_output
RUN_OUTPUT_VARIABLE
run_output
)
# For cross-compiling, user must provide extra info.
if (CMAKE_CROSSCOMPILING AND compile_result)
message( DEBUG "cross: blas_complex_return = '${blas_complex_return}'" )
set( run_result "0" CACHE STRING "" FORCE )
if (blas_complex_return STREQUAL "return")
set( run_output "ok" CACHE STRING "" FORCE )
elseif (blas_complex_return STREQUAL "argument")
set( run_output "failed" CACHE STRING "" FORCE )
else()
message( FATAL_ERROR " ${red}When cross-compiling, one must define either\n"
" `blas_complex_return=return` (GNU gfortran convention) or\n"
" `blas_complex_return=argument` (Intel ifort convention).${plain}" )
endif()
endif()
debug_try_run( "return_complex.cc" "${compile_result}" "${compile_output}"
"${run_result}" "${run_output}" )
if (compile_result AND "${run_output}" MATCHES "ok")
message( "${blue} BLAS (zdotc) returns complex (GNU gfortran convention)${plain}" )
# nothing to define
else()
try_run(
run_result compile_result ${CMAKE_CURRENT_BINARY_DIR}
SOURCES
"${CMAKE_CURRENT_SOURCE_DIR}/config/return_complex_argument.cc"
LINK_LIBRARIES
${BLAS_LIBRARIES} ${openmp_lib} # not "..." quoted; screws up OpenMP
COMPILE_DEFINITIONS
${blaspp_defs_}
COMPILE_OUTPUT_VARIABLE
compile_output
RUN_OUTPUT_VARIABLE
run_output
)
# For cross-compiling, user must provide extra info.
if (CMAKE_CROSSCOMPILING AND compile_result)
message( DEBUG "cross: blas_complex_return = '${blas_complex_return}' (2)" )
set( run_result "0" CACHE STRING "" FORCE )
set( run_output "ok" CACHE STRING "" FORCE )
assert( blas_complex_return STREQUAL "argument" ) # follows from above
endif()
debug_try_run( "return_complex_argument.cc"
"${compile_result}" "${compile_output}"
"${run_result}" "${run_output}" )
if (compile_result AND "${run_output}" MATCHES "ok")
message( "${blue} BLAS (zdotc) returns complex as hidden argument (Intel ifort convention)${plain}" )
list( APPEND blaspp_defs_ "-DBLAS_COMPLEX_RETURN_ARGUMENT" )
else()
message( FATAL_ERROR "Error - Cannot detect zdotc return value. Please check the BLAS installation." )
endif()
endif()
#-------------------------------------------------------------------------------
message( STATUS "Checking BLAS float return type" )
try_run(
run_result compile_result ${CMAKE_CURRENT_BINARY_DIR}
SOURCES
"${CMAKE_CURRENT_SOURCE_DIR}/config/return_float.cc"
LINK_LIBRARIES
${BLAS_LIBRARIES} ${openmp_lib} # not "..." quoted; screws up OpenMP
COMPILE_DEFINITIONS
${blaspp_defs_}
COMPILE_OUTPUT_VARIABLE
compile_output
RUN_OUTPUT_VARIABLE
run_output
)
# For cross-compiling, assume the run is okay unless the user provides
# ${blas_return_float_f2c}.
if (CMAKE_CROSSCOMPILING AND compile_result)
message( DEBUG "cross: not blas_return_float_f2c = '${blas_return_float_f2c}'" )
set( run_result "0" CACHE STRING "" FORCE )
if (blas_return_float_f2c)
set( run_output "failed" CACHE STRING "" FORCE )
else()
set( run_output "ok" CACHE STRING "" FORCE )
endif()
endif()
debug_try_run( "return_float.cc" "${compile_result}" "${compile_output}"
"${run_result}" "${run_output}" )
if (compile_result AND "${run_output}" MATCHES "ok")
message( "${blue} BLAS (sdot) returns float as float (standard)${plain}" )
# nothing to define
else()
# Detect clapack, macOS Accelerate
try_run(
run_result compile_result ${CMAKE_CURRENT_BINARY_DIR}
SOURCES
"${CMAKE_CURRENT_SOURCE_DIR}/config/return_float_f2c.cc"
LINK_LIBRARIES
${BLAS_LIBRARIES} ${openmp_lib} # not "..." quoted; screws up OpenMP
COMPILE_DEFINITIONS
${blaspp_defs_}
COMPILE_OUTPUT_VARIABLE
compile_output
RUN_OUTPUT_VARIABLE
run_output
)
# For cross-compiling, user must provide ${blas_return_float_f2c}.
if (CMAKE_CROSSCOMPILING AND compile_result)
message( DEBUG "cross: blas_return_float_f2c = ${blas_return_float_f2c}" )
set( run_result "0" CACHE STRING "" FORCE )
set( run_output "ok" CACHE STRING "" FORCE )
assert( blas_return_float_f2c ) # follows from above
endif()
debug_try_run( "return_float_f2c.cc" "${compile_result}" "${compile_output}"
"${run_result}" "${run_output}" )
if (compile_result AND "${run_output}" MATCHES "ok")
message( "${blue} BLAS (sdot) returns float as double (f2c convention)${plain}" )
list( APPEND blaspp_defs_ "-DBLAS_HAVE_F2C" )
endif()
endif()
endif() # run_
#===============================================================================
# Mark as already run (see top).
set( blas_config_cache "${BLAS_LIBRARIES}" CACHE INTERNAL "" )
#-------------------------------------------------------------------------------
message( DEBUG "
blaspp_defs_ = '${blaspp_defs_}'
")