-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathCMakeLists.txt
419 lines (385 loc) · 13.7 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
cmake_minimum_required (VERSION 3.12.4)
project (cmfrec VERSION 3.5.1)
set(CMAKE_BUILD_TYPE Release)
### Note: this build script allows configuring 4 things manually:
### (a) Numeric precison (single/double)
### (b) Integer size (standard/64-bit)
### (c) Providing a custom BLAS/LAPACK instead of using FindBLAS and FindLAPACK
### (d) Whether to let the LAPACK Library handle NAN propagation (most of them can)
### Option (a): Numeric precision
# Library will by default compile with double precision (double),
# but can be changed to single-precision (float) if desired.
option(USE_FLOAT "Build for single-precision (float) type" OFF)
if (USE_FLOAT)
message(STATUS "Will build for single-precision (float) type.")
set(real_t float)
add_compile_definitions(USE_FLOAT)
else()
message(STATUS "Will build for double-precision (double) type.")
set(real_t double) # <- this is the default
add_compile_definitions(USE_DOUBLE)
endif()
### Option (b): Integer size
# By default, will use 'int' type, but can also use int64_t,
# in which case it will also need to be linked against
# the ILP64 versions of BLAS and LAPACK. Note that the
# 'FindBLAS' script won't search for OpenBLAS'es ILP64,
# so if turning this ON and using something other than MKL,
# must also pass the BLAS and LAPACK link arguments.
option(USE_INT64 "Use larger int width" OFF)
if (USE_INT64)
message(STATUS "Will use int64_t instead of machine's 'int' type.")
set(int_t int64_t)
add_compile_definitions(USE_INT64)
else()
message(STATUS "Will use machine's native 'int' type.")
set(int_t int) # <- this is the default
add_compile_definitions(USE_INT)
endif()
### Option (c): Custom BLAS/LAPACK linkage
# If a custom BLAS/LAPACK is to be provided, please add it here
# by specifing the necessary link arguments. Be aware that it
# should match with the integer size specified above if it was changed.
set(BLAS_LIBRARIES "")
set(LAPACK_LIBRARIES "")
### Option (d): NAN propagation in LAPACK
# This option will avoid passing inputs with any NAN values to the
# LAPACK library, in case the library does not deal with them properly
# such as some earlier versions of OpenBLAS. It is recommended NOT to
# set this on if the LAPACK library follows the rules for NANs
# (that is, the resulting output will not have NANs in any extra place
# in which it shouldn't according to the inputs that had NAN values).
# Note that it is still assummed that the BLAS library will propagate
# NANs properly according to IEEE754 rules.
option(NO_NAN_PROPAGATION "In case the LAPACK library doesn't do proper NAN propagation" OFF)
if (NO_NAN_PROPAGATION)
add_compile_definitions(FORCE_NO_NAN_PROPAGATION)
endif()
### Option (e): Optimizing for a given CPU architecture
include(CheckCSourceCompiles)
option(USE_MARCH_NATIVE "Build with -march=native" OFF)
if (USE_MARCH_NATIVE AND NOT MSVC)
set(OLD_FLAGS ${CMAKE_REQUIRED_FLAGS})
set(CMAKE_REQUIRED_FLAGS "-march=native")
check_c_source_compiles(
"
int main(int argc, char **argv)
{
return 0;
}
"
SUPPORTS_MARCH_NATIVE
)
set(CMAKE_REQUIRED_FLAGS ${OLD_FLAGS})
if (SUPPORTS_MARCH_NATIVE)
message(STATUS "Adding flag -march=native.")
set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} -march=native")
else()
set(CMAKE_REQUIRED_FLAGS "-mcpu=native")
check_c_source_compiles(
"
int main(int argc, char **argv)
{
return 0;
}
"
SUPPORTS_MCPU_NATIVE
)
set(CMAKE_REQUIRED_FLAGS ${OLD_FLAGS})
if (SUPPORTS_MCPU_NATIVE)
message(STATUS "Adding flag -mcpu=native.")
set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} -mcpu=native")
else()
message(WARNING "Flags -march=native and -mcpu=native not supported by the compiler.")
endif()
endif()
endif()
### Option (f): No long doubles
# This option will avoid using C type 'long double', which in some machies
# can have a large performance overhead. In x86 machines, it is recommended
# to use keep long doubles, especially if dealing with data that's larger
# than 2^53. This 'long double' type is always disabled on windows OS.
option(NO_LONG_DOUBLE "Avoid using long double type" OFF)
if (NO_LONG_DOUBLE)
add_compile_definitions(NO_LONG_DOUBLE)
endif()
### Option (g): Visibility of exported symbols
# By default, will hide all functions that are not meant to be exported,
# but this can be changed if desired
option(HIDE_INTERNAL_SYMBOLS "Set hidden visibility for non-exported symbols" ON)
if (HIDE_INTERNAL_SYMBOLS AND NOT WIN32)
set(OLD_FLAGS ${CMAKE_REQUIRED_FLAGS})
set(CMAKE_REQUIRED_FLAGS " -fvisibility=hidden")
check_c_source_compiles(
"
__attribute__((visibility (\"default\")))
int myfun() {
return 0;
}
int main(int argc, char **argv)
{
return myfun();
}
"
SUPPORTS_FVISIBILITY_HIDDEN
)
set(CMAKE_REQUIRED_FLAGS ${OLD_FLAGS})
if (SUPPORTS_FVISIBILITY_HIDDEN)
set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} -fvisibility=hidden")
add_compile_definitions(EXPLICITLTY_EXPORT_SYMBOLS)
else()
message(STATUS "Hidden symbols visibility not supported by the compiler, will export all symbols.")
endif()
else()
message(STATUS "Making all symbols in the library visible.")
endif()
### ------ Rest of the script is not meant to be user-configurable ----- ###
# Setting C99 standard
set(CMAKE_C_STANDARD 99)
# Setting the list of files to compile
set(SRC_FILES ${PROJECT_SOURCE_DIR}/src/cblas_wrappers.c
${PROJECT_SOURCE_DIR}/src/collective.c
${PROJECT_SOURCE_DIR}/src/common.c
${PROJECT_SOURCE_DIR}/src/helpers.c
${PROJECT_SOURCE_DIR}/src/lbfgs.c
${PROJECT_SOURCE_DIR}/src/offsets.c)
set(BUILD_SHARED_LIBS True)
add_library(cmfrec SHARED ${SRC_FILES})
add_compile_definitions(CMFREC_COMPILE_TIME)
# Adding the internal headers
target_include_directories(cmfrec PRIVATE ${PROJECT_SOURCE_DIR}/src)
# OpenMP for multi-threading
find_package(OpenMP)
if (OpenMP_C_FOUND)
target_link_libraries(cmfrec PUBLIC OpenMP::OpenMP_C)
else()
message(STATUS "OpenMP not found - will compile without multi-threading support")
endif()
# Compiler optimizations
if (MSVC)
if (NOT (${CMAKE_C_FLAGS_RELEASE} MATCHES "/O2"))
set(OLD_FLAGS ${CMAKE_REQUIRED_FLAGS})
set(CMAKE_REQUIRED_FLAGS "/O2")
check_c_source_compiles(
"
int main(int argc, char **argv)
{
return 0;
}
"
SUPPORTS_O2
)
set(CMAKE_REQUIRED_FLAGS ${OLD_FLAGS})
if (SUPPORTS_O2)
set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} /O2")
endif()
endif()
if (NOT (${CMAKE_C_FLAGS_RELEASE} MATCHES "/fp:contract"))
set(OLD_FLAGS ${CMAKE_REQUIRED_FLAGS})
set(CMAKE_REQUIRED_FLAGS "/fp:contract")
check_c_source_compiles(
"
int main(int argc, char **argv)
{
return 0;
}
"
SUPPORTS_FPCONTRACT
)
set(CMAKE_REQUIRED_FLAGS ${OLD_FLAGS})
if (SUPPORTS_FPCONTRACT)
set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} /fp:contract")
endif()
endif()
if (NOT (${CMAKE_C_FLAGS_RELEASE} MATCHES "/fp:except-"))
set(OLD_FLAGS ${CMAKE_REQUIRED_FLAGS})
set(CMAKE_REQUIRED_FLAGS "/fp:except-")
check_c_source_compiles(
"
int main(int argc, char **argv)
{
return 0;
}
"
SUPPORTS_FPNOEX
)
set(CMAKE_REQUIRED_FLAGS ${OLD_FLAGS})
if (SUPPORTS_FPNOEX)
set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} /fp:except-")
endif()
endif()
else()
if (NOT (${CMAKE_C_FLAGS_RELEASE} MATCHES "-O3 "))
set(OLD_FLAGS ${CMAKE_REQUIRED_FLAGS})
set(CMAKE_REQUIRED_FLAGS "-O3")
check_c_source_compiles(
"
int main(int argc, char **argv)
{
return 0;
}
"
SUPPORTS_O3
)
set(CMAKE_REQUIRED_FLAGS ${OLD_FLAGS})
if (SUPPORTS_O3)
set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} -O3")
else()
set(CMAKE_REQUIRED_FLAGS "-O2")
check_c_source_compiles(
"
int main(int argc, char **argv)
{
return 0;
}
"
SUPPORTS_O2
)
set(CMAKE_REQUIRED_FLAGS ${OLD_FLAGS})
if (SUPPORTS_O2)
set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} -O2")
endif()
endif()
endif()
if (NOT (${CMAKE_C_FLAGS_RELEASE} MATCHES "-fno-trapping-math"))
set(OLD_FLAGS ${CMAKE_REQUIRED_FLAGS})
set(CMAKE_REQUIRED_FLAGS "-fno-trapping-math")
check_c_source_compiles(
"
int main(int argc, char **argv)
{
return 0;
}
"
SUPPORTS_FNTM
)
set(CMAKE_REQUIRED_FLAGS ${OLD_FLAGS})
if (SUPPORTS_FNTM)
set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} -fno-trapping-math")
endif()
endif()
if (NOT (${CMAKE_C_FLAGS_RELEASE} MATCHES "-fno-math-errno"))
set(OLD_FLAGS ${CMAKE_REQUIRED_FLAGS})
set(CMAKE_REQUIRED_FLAGS "-fno-math-errno")
check_c_source_compiles(
"
int main(int argc, char **argv)
{
return 0;
}
"
SUPPORTS_FNE
)
set(CMAKE_REQUIRED_FLAGS ${OLD_FLAGS})
if (SUPPORTS_FNE)
set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} -fno-math-errno")
endif()
endif()
if (NOT (${CMAKE_C_FLAGS_RELEASE} MATCHES "-ffp-contract=fast"))
set(OLD_FLAGS ${CMAKE_REQUIRED_FLAGS})
set(CMAKE_REQUIRED_FLAGS "-ffp-contract=fast")
check_c_source_compiles(
"
int main(int argc, char **argv)
{
return 0;
}
"
SUPPORTS_FFPCONTRACT
)
set(CMAKE_REQUIRED_FLAGS ${OLD_FLAGS})
if (SUPPORTS_FFPCONTRACT)
set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} -ffp-contract=fast")
endif()
endif()
check_c_source_compiles(
"
void fma_extra(double *a, double w, double *b, int n)
{
#pragma clang fp reassociate(on)
for (int ix = 0; ix < n; ix++)
a[ix] += w * b[ix] * b[ix];
}
int main(int argc, char **argv)
{
double a[] = {1.,2.,3.};
double b[] = {4.,5.,6.};
double w = 2.;
int n = 3;
fma_extra(a, w, b, n);
return 0;
}
"
SUPPORTS_CLANG_FP_REASSOCIATE
)
if (SUPPORTS_CLANG_FP_REASSOCIATE)
add_compile_definitions(CLANG_FP_REASSOCIATE)
endif()
endif()
# Link-time optimization if supported
# https://stackoverflow.com/questions/31355692/how-do-i-enable-link-time-optimization-lto-with-cmake
include(CheckIPOSupported)
check_ipo_supported(RESULT LTO_SUPPORTED)
if (LTO_SUPPORTED)
set_property(TARGET cmfrec PROPERTY INTERPROCEDURAL_OPTIMIZATION TRUE)
endif()
# Linkage to BLAS and LAPACK
if (BLAS_LIBRARIES STREQUAL "")
if (USE_INT64)
set(BLA_VENDOR "Intel10_64ilp")
endif()
find_package(BLAS REQUIRED)
endif()
if (LAPACK_LIBRARIES STREQUAL "")
if (USE_INT64)
set(BLA_VENDOR "Intel10_64ilp")
endif()
find_package(LAPACK REQUIRED)
endif()
# https://stackoverflow.com/questions/59578248/how-do-i-manipulate-cmake-lists-as-sets
list(APPEND union_list ${LAPACK_LIBRARIES} ${BLAS_LIBRARIES})
list(REMOVE_DUPLICATES union_list)
# See https://github.com/xianyi/OpenBLAS/issues/3237
set(CMAKE_REQUIRED_LINK_OPTIONS ${union_list})
set(CMAKE_REQUIRED_LIBRARIES ${union_list})
CHECK_LIBRARY_EXISTS("" "openblas_get_num_threads" "" HAS_OPENBLAS)
if (HAS_OPENBLAS)
message(STATUS "Using OpenBLAS - will replace its SYR function.")
add_compile_definitions(AVOID_BLAS_SYR)
add_compile_definitions(HAS_OPENBLAS)
else()
CHECK_LIBRARY_EXISTS("" "mkl_get_max_threads" "" HAS_MKL)
if (HAS_MKL)
add_compile_definitions(HAS_MKL)
else()
CHECK_LIBRARY_EXISTS("" "catlas_saxpby" "" HAS_ATLAS)
if (HAS_ATLAS)
message(STATUS "Using ATLAS - will replace its SYR function.")
message(WARNING "Note: ATLAS multi-threading might make this library very slow.")
add_compile_definitions(HAS_ATLAS)
add_compile_definitions(AVOID_BLAS_SYR)
endif()
endif()
endif()
target_link_libraries(cmfrec PUBLIC ${union_list})
# Public header with the data types substituted according to what was built
configure_file(${PROJECT_SOURCE_DIR}/include/cmfrec.h.in ${CMAKE_BINARY_DIR}/cmfrec.h @ONLY)
set_target_properties(cmfrec PROPERTIES PUBLIC_HEADER ${CMAKE_BINARY_DIR}/cmfrec.h SOVERSION 3 VERSION ${PROJECT_VERSION})
# Install target
include(GNUInstallDirs)
install(
TARGETS cmfrec
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
PUBLIC_HEADER DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
)
configure_file(cmfrec.pc.in cmfrec.pc @ONLY)
install(FILES ${CMAKE_BINARY_DIR}/cmfrec.pc DESTINATION ${CMAKE_INSTALL_DATAROOTDIR}/pkgconfig)
# uninstall target
if(NOT TARGET uninstall)
configure_file(
"${CMAKE_CURRENT_SOURCE_DIR}/cmake_uninstall.cmake.in"
"${CMAKE_CURRENT_BINARY_DIR}/cmake_uninstall.cmake"
IMMEDIATE @ONLY)
add_custom_target(uninstall
COMMAND ${CMAKE_COMMAND} -P ${CMAKE_CURRENT_BINARY_DIR}/cmake_uninstall.cmake)
endif()