-
Notifications
You must be signed in to change notification settings - Fork 1
/
FindMKL.cmake
298 lines (257 loc) · 9.78 KB
/
FindMKL.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
# Distributed under the OSI-approved BSD 3-Clause License. See accompanying
# file Copyright.txt or https://cmake.org/licensing for details.
MKLROOT=/opt/intel/mkl
#.rst:
# FindMKL
# -------
#
# Find a Intel® Math Kernel Library (Intel® MKL) installation and provide
# all necessary variables and macros to compile software for it.
#
# MKLROOT is required in your system
#
# we use mkl_link_tool to get the library needed depending on variables
# There are few sets of libraries:
#
# Array indexes modes:
#
# ::
#
# LP - 32 bit indexes of arrays
# ILP - 64 bit indexes of arrays
#
#
#
# Threading:
#
# ::
#
# SEQUENTIAL - no threading
# INTEL - Intel threading library
# GNU - GNU threading library
# MPI support
# NOMPI - no MPI support
# INTEL - Intel MPI library
# OPEN - Open MPI library
# SGI - SGI MPT Library
#
#
#
#
# The following are set after the configuration is done:
#
# ::
#
# MKL_FOUND - system has MKL
# MKL_ROOT_DIR - path to the MKL base directory
# MKL_INCLUDE_DIR - the MKL include directory
# MKL_LIBRARIES - MKL libraries
# MKL_LIBRARY_DIR - MKL library dir (for dlls!)
#
#
#
# Sample usage:
#
# If MKL is required (i.e., not an optional part):
#
# ::
#
# find_package(MKL REQUIRED)
# if (MKL_FOUND)
# include_directories(${MKL_INCLUDE_DIR})
# # and for each of your dependent executable/library targets:
# target_link_libraries(<YourTarget> ${MKL_LIBRARIES})
# endif()
# NOTES
#
# If you want to use the module and your build type is not supported
# out-of-the-box, please contact me to exchange information on how
# your system is setup and I'll try to add support for it.
#
# AUTHOR
#
# Joan MASSICH (joan.massich-vall.AT.inria.fr).
# Alexandre GRAMFORT (Alexandre.Gramfort.AT.inria.fr)
# Théodore PAPADOPOULO (papadop.AT.inria.fr)
set(CMAKE_FIND_DEBUG_MODE 1)
# unset this variable defined in matio
unset(MSVC)
# Find MKL ROOT
find_path(MKL_ROOT_DIR NAMES include/mkl_cblas.h PATHS $ENV{MKLROOT})
# Convert symlinks to real paths
get_filename_component(MKL_ROOT_DIR ${MKL_ROOT_DIR} REALPATH)
if (NOT MKL_ROOT_DIR)
if (MKL_FIND_REQUIRED)
message(FATAL_ERROR "Could not find MKL: please set environment variable {MKLROOT}")
else()
unset(MKL_ROOT_DIR CACHE)
endif()
else()
set(MKL_INCLUDE_DIR ${MKL_ROOT_DIR}/include)
# set arguments to call the MKL provided tool for linking
set(MKL_LINK_TOOL ${MKL_ROOT_DIR}/tools/mkl_link_tool)
if (WIN32)
set(MKL_LINK_TOOL ${MKL_LINK_TOOL}.exe)
endif()
# check that the tools exists or quit
if (NOT EXISTS "${MKL_LINK_TOOL}")
message(FATAL_ERROR "cannot find MKL tool: ${MKL_LINK_TOOL}")
endif()
# first the libs
set(MKL_LINK_TOOL_COMMAND ${MKL_LINK_TOOL} "-libs")
# possible versions
# <11.3|11.2|11.1|11.0|10.3|10.2|10.1|10.0|ParallelStudioXE2016|ParallelStudioXE2015|ComposerXE2013SP1|ComposerXE2013|ComposerXE2011|CompilerPro>
# not older than MKL 10 (2011)
if (MKL_INCLUDE_DIR MATCHES "Composer.*2013")
list(APPEND MKL_LINK_TOOL_COMMAND "--mkl=ComposerXE2013")
elseif (MKL_INCLUDE_DIR MATCHES "Composer.*2011")
list(APPEND MKL_LINK_TOOL_COMMAND "--mkl=ComposerXE2011")
elseif (MKL_INCLUDE_DIR MATCHES "10.3")
list(APPEND MKL_LINK_TOOL_COMMAND "--mkl=10.3")
elseif(MKL_INCLUDE_DIR MATCHES "2013") # version 11 ...
list(APPEND MKL_LINK_TOOL_COMMAND "--mkl=11.1")
elseif(MKL_INCLUDE_DIR MATCHES "2015")
list(APPEND MKL_LINK_TOOL_COMMAND "--mkl=11.2")
elseif(MKL_INCLUDE_DIR MATCHES "2016")
list(APPEND MKL_LINK_TOOL_COMMAND "--mkl=11.3")
elseif(MKL_INCLUDE_DIR MATCHES "2017")
list(APPEND MKL_LINK_TOOL_COMMAND "--mkl=11.3")
elseif(MKL_INCLUDE_DIR MATCHES "2018")
list(APPEND MKL_LINK_TOOL_COMMAND "--mkl=11.3")
elseif (MKL_INCLUDE_DIR MATCHES "10")
list(APPEND MKL_LINK_TOOL_COMMAND "--mkl=10.2")
else()
list(APPEND MKL_LINK_TOOL_COMMAND "--mkl=11.3")
endif()
if (CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
list(APPEND MKL_LINK_TOOL_COMMAND "--compiler=clang")
elseif(CMAKE_CXX_COMPILER_ID STREQUAL "Intel")
list(APPEND MKL_LINK_TOOL_COMMAND "--compiler=intel_c")
elseif(CMAKE_CXX_COMPILER_ID STREQUAL "MSVC")
list(APPEND MKL_LINK_TOOL_COMMAND "--compiler=ms_c")
else()
list(APPEND MKL_LINK_TOOL_COMMAND "--compiler=gnu_c")
endif()
if (APPLE)
list(APPEND MKL_LINK_TOOL_COMMAND "--os=mac")
elseif(WIN32)
list(APPEND MKL_LINK_TOOL_COMMAND "--os=win")
else()
list(APPEND MKL_LINK_TOOL_COMMAND "--os=lnx")
endif()
set(MKL_LIB_DIR)
if (${CMAKE_SIZEOF_VOID_P} EQUAL 8)
list(APPEND MKL_LINK_TOOL_COMMAND "--arch=intel64")
set(MKL_LIB_DIR "intel64")
else()
list(APPEND MKL_LINK_TOOL_COMMAND "--arch=ia32")
set(MKL_LIB_DIR "ia32")
endif()
if (MKL_USE_sdl)
list(APPEND MKL_LINK_TOOL_COMMAND "--linking=sdl")
else()
if (BLA_STATIC)
list(APPEND MKL_LINK_TOOL_COMMAND "--linking=static")
else()
list(APPEND MKL_LINK_TOOL_COMMAND "--linking=dynamic")
endif()
endif()
if (MKL_USE_parallel)
list(APPEND MKL_LINK_TOOL_COMMAND "--parallel=yes")
else()
list(APPEND MKL_LINK_TOOL_COMMAND "--parallel=no")
endif()
if (FORCE_BUILD_32BITS)
list(APPEND MKL_LINK_TOOL_COMMAND "--interface=cdecl")
set(MKL_USE_interface "cdecl" CACHE STRING "disabled by FORCE_BUILD_32BITS" FORCE)
else()
list(APPEND MKL_LINK_TOOL_COMMAND "--interface=${MKL_USE_interface}")
endif()
if (MKL_USE_parallel)
if (UNIX AND NOT APPLE)
list(APPEND MKL_LINK_TOOL_COMMAND "--openmp=gomp")
else()
list(APPEND MKL_LINK_TOOL_COMMAND "--threading-library=iomp5")
list(APPEND MKL_LINK_TOOL_COMMAND "--openmp=iomp5")
endif()
endif()
execute_process(COMMAND ${MKL_LINK_TOOL_COMMAND}
OUTPUT_VARIABLE MKL_LIBS
RESULT_VARIABLE COMMAND_WORKED
TIMEOUT 2 ERROR_QUIET)
set(MKL_LIBRARIES)
if (NOT ${COMMAND_WORKED} EQUAL 0)
message(FATAL_ERROR "Cannot find the MKL libraries correctly. Please check your MKL input variables and mkl_link_tool. The command executed was:\n ${MKL_LINK_TOOL_COMMAND}.")
endif()
set(MKL_LIBRARY_DIR)
if (WIN32)
set(MKL_LIBRARY_DIR "${MKL_ROOT_DIR}/lib/${MKL_LIB_DIR}/" "${MKL_ROOT_DIR}/../compiler/lib/${MKL_LIB_DIR}")
# remove unwanted break
string(REGEX REPLACE "\n" "" MKL_LIBS ${MKL_LIBS})
# get the list of libs
separate_arguments(MKL_LIBS)
foreach(i ${MKL_LIBS})
find_library(FULLPATH_LIB ${i} PATHS "${MKL_LIBRARY_DIR}")
if (FULLPATH_LIB)
list(APPEND MKL_LIBRARIES ${FULLPATH_LIB})
elseif(i)
list(APPEND MKL_LIBRARIES ${i})
endif()
unset(FULLPATH_LIB CACHE)
endforeach()
else() # UNIX and macOS
# remove unwanted break
string(REGEX REPLACE "\n" "" MKL_LIBS ${MKL_LIBS})
if (MKL_LINK_TOOL_COMMAND MATCHES "static")
string(REPLACE "$(MKLROOT)" "${MKL_ROOT_DIR}" MKL_LIBRARIES ${MKL_LIBS})
# hack for lin with libiomp5.a
if (APPLE)
string(REPLACE "-liomp5" "${MKL_ROOT_DIR}/../compiler/lib/libiomp5.a" MKL_LIBRARIES ${MKL_LIBRARIES})
else()
string(REPLACE "-liomp5" "${MKL_ROOT_DIR}/../compiler/lib/${MKL_LIB_DIR}/libiomp5.a" MKL_LIBRARIES ${MKL_LIBRARIES})
endif()
separate_arguments(MKL_LIBRARIES)
else() # dynamic or sdl
# get the lib dirs
string(REGEX REPLACE "^.*-L[^/]+([^\ ]+).*" "${MKL_ROOT_DIR}\\1" INTEL_LIB_DIR ${MKL_LIBS})
if (NOT EXISTS ${INTEL_LIB_DIR})
# Work around a bug in mkl 2018
set(INTEL_LIB_DIR1 "${INTEL_LIB_DIR}_lin")
if (NOT EXISTS ${INTEL_LIB_DIR1})
message(FATAL_ERROR "MKL installation broken. Directory ${INTEL_LIB_DIR} does not exist.")
endif()
set(INTEL_LIB_DIR ${INTEL_LIB_DIR1})
endif()
set(MKL_LIBRARY_DIR ${INTEL_LIB_DIR} "${MKL_ROOT_DIR}/../compiler/lib/${MKL_LIB_DIR}")
# get the list of libs
separate_arguments(MKL_LIBS)
# set full path to libs
foreach(i ${MKL_LIBS})
string(REGEX REPLACE " -" "-" i ${i})
string(REGEX REPLACE "-l([^\ ]+)" "\\1" i ${i})
string(REGEX REPLACE "-L.*" "" i ${i})
find_library(FULLPATH_LIB ${i} PATHS "${MKL_LIBRARY_DIR}")
if (FULLPATH_LIB)
list(APPEND MKL_LIBRARIES ${FULLPATH_LIB})
elseif(i)
list(APPEND MKL_LIBRARIES ${i})
endif()
unset(FULLPATH_LIB CACHE)
endforeach()
endif()
endif()
# now definitions
string(REPLACE "-libs" "-opts" MKL_LINK_TOOL_COMMAND "${MKL_LINK_TOOL_COMMAND}")
execute_process(COMMAND ${MKL_LINK_TOOL_COMMAND} OUTPUT_VARIABLE RESULT_OPTS TIMEOUT 2 ERROR_QUIET)
string(REGEX MATCHALL "[-/]D[^\ ]*" MKL_DEFINITIONS ${RESULT_OPTS})
if (CMAKE_FIND_DEBUG_MODE)
message(STATUS "Exectuted command: \n${MKL_LINK_TOOL_COMMAND}")
message(STATUS "Found MKL_LIBRARIES:\n${MKL_LIBRARIES} ")
message(STATUS "Found MKL_DEFINITIONS:\n${MKL_DEFINITIONS} ")
message(STATUS "Found MKL_LIBRARY_DIR:\n${MKL_LIBRARY_DIR} ")
message(STATUS "Found MKL_INCLUDE_DIR:\n${MKL_INCLUDE_DIR} ")
endif()
include(FindPackageHandleStandardArgs)
find_package_handle_standard_args(MKL DEFAULT_MSG MKL_INCLUDE_DIR MKL_LIBRARIES)
mark_as_advanced(MKL_INCLUDE_DIR MKL_LIBRARIES MKL_DEFINITIONS MKL_ROOT_DIR)
endif()