Debian Codegraph @

cmake/FindMKL.cmake

raw
1 # @lint-ignore-every LICENSELINT
2 # Copyright (c) Meta Platforms, Inc. and affiliates.
3 # All rights reserved.
4 #
5 # This source code is licensed under the BSD-style license found in the
6 # LICENSE file in the root directory of this source tree.
7
8 # Adapted from CMake's FindBLAS module.
9 # Distributed under the OSI-approved BSD 3-Clause License. See accompanying
10 # file Copyright.txt or https://cmake.org/licensing for details.
11
12 #[=======================================================================[.rst:
13 FindMKL
14 --------
15
16 Find Intel MKL library.
17
18 Input Variables
19 ^^^^^^^^^^^^^^^
20
21 The following variables may be set to influence this module's behavior:
22
23 ``BLA_STATIC``
24 if ``ON`` use static linkage
25
26 ``BLA_VENDOR``
27 If set, checks only the specified vendor, if not set checks all the
28 possibilities. List of vendors valid in this module:
29
30 * ``Intel10_32`` (intel mkl v10 32 bit)
31 * ``Intel10_64lp`` (intel mkl v10+ 64 bit, threaded code, lp64 model)
32 * ``Intel10_64lp_seq`` (intel mkl v10+ 64 bit, sequential code, lp64 model)
33 * ``Intel10_64ilp`` (intel mkl v10+ 64 bit, threaded code, ilp64 model)
34 * ``Intel10_64ilp_seq`` (intel mkl v10+ 64 bit, sequential code, ilp64 model)
35 * ``Intel10_64_dyn`` (intel mkl v10+ 64 bit, single dynamic library)
36 * ``Intel`` (obsolete versions of mkl 32 and 64 bit)
37
38
39 Result Variables
40 ^^^^^^^^^^^^^^^^
41
42 This module defines the following variables:
43
44 ``MKL_FOUND``
45 library implementing the BLAS interface is found
46 ``MKL_LIBRARIES``
47 uncached list of libraries (using full path name) to link against
48 to use MKL (may be empty if compiler implicitly links MKL)
49
50 .. note::
51
52 C or CXX must be enabled to use Intel Math Kernel Library (MKL).
53
54 For example, to use Intel MKL libraries and/or Intel compiler:
55
56 .. code-block:: cmake
57
58 set(BLA_VENDOR Intel10_64lp)
59 find_package(MKL)
60
61 Hints
62 ^^^^^
63
64 Set the ``MKLROOT`` environment variable to a directory that contains an MKL
65 installation, or add the directory to the dynamic library loader environment
66 variable for your platform (``LIB``, ``DYLD_LIBRARY_PATH`` or
67 ``LD_LIBRARY_PATH``).
68
69 #]=======================================================================]
70
71 include(CheckFunctionExists)
72 include(CMakePushCheckState)
73 include(FindPackageHandleStandardArgs)
74 cmake_push_check_state()
75 set(CMAKE_REQUIRED_QUIET ${BLAS_FIND_QUIETLY})
76
77
78 set(_blas_ORIG_CMAKE_FIND_LIBRARY_SUFFIXES ${CMAKE_FIND_LIBRARY_SUFFIXES})
79 if(BLA_STATIC)
80 if(WIN32)
81 set(CMAKE_FIND_LIBRARY_SUFFIXES .lib ${CMAKE_FIND_LIBRARY_SUFFIXES})
82 else()
83 set(CMAKE_FIND_LIBRARY_SUFFIXES .a ${CMAKE_FIND_LIBRARY_SUFFIXES})
84 endif()
85 else()
86 if(CMAKE_SYSTEM_NAME STREQUAL "Linux")
87 # for ubuntu's libblas3gf and liblapack3gf packages
88 set(CMAKE_FIND_LIBRARY_SUFFIXES ${CMAKE_FIND_LIBRARY_SUFFIXES} .so.3gf)
89 endif()
90 endif()
91
92 macro(CHECK_BLAS_LIBRARIES LIBRARIES _prefix _name _flags _list _threadlibs _addlibdir _subdirs)
93 # This macro checks for the existence of the combination of fortran libraries
94 # given by _list. If the combination is found, this macro checks (using the
95 # Check_Fortran_Function_Exists macro) whether can link against that library
96 # combination using the name of a routine given by _name using the linker
97 # flags given by _flags. If the combination of libraries is found and passes
98 # the link test, LIBRARIES is set to the list of complete library paths that
99 # have been found. Otherwise, LIBRARIES is set to FALSE.
100
101 # N.B. _prefix is the prefix applied to the names of all cached variables that
102 # are generated internally and marked advanced by this macro.
103 # _addlibdir is a list of additional search paths. _subdirs is a list of path
104 # suffixes to be used by find_library().
105
106 set(_libraries_work TRUE)
107 set(${LIBRARIES})
108 set(_combined_name)
109
110 set(_extaddlibdir "${_addlibdir}")
111 if(WIN32)
112 list(APPEND _extaddlibdir ENV LIB)
113 elseif(APPLE)
114 list(APPEND _extaddlibdir ENV DYLD_LIBRARY_PATH)
115 else()
116 list(APPEND _extaddlibdir ENV LD_LIBRARY_PATH)
117 endif()
118 list(APPEND _extaddlibdir "${CMAKE_C_IMPLICIT_LINK_DIRECTORIES}")
119
120 foreach(_library ${_list})
121 if(_library MATCHES "^-Wl,--(start|end)-group$")
122 # Respect linker flags like --start/end-group (required by MKL)
123 set(${LIBRARIES} ${${LIBRARIES}} "${_library}")
124 else()
125 set(_combined_name ${_combined_name}_${_library})
126 if(NOT "${_threadlibs}" STREQUAL "")
127 set(_combined_name ${_combined_name}_threadlibs)
128 endif()
129 if(_libraries_work)
130 find_library(${_prefix}_${_library}_LIBRARY
131 NAMES ${_library}
132 PATHS ${_extaddlibdir}
133 PATH_SUFFIXES ${_subdirs}
134 )
135 #message("DEBUG: find_library(${_library}) got ${${_prefix}_${_library}_LIBRARY}")
136 mark_as_advanced(${_prefix}_${_library}_LIBRARY)
137 set(${LIBRARIES} ${${LIBRARIES}} ${${_prefix}_${_library}_LIBRARY})
138 set(_libraries_work ${${_prefix}_${_library}_LIBRARY})
139 endif()
140 endif()
141 endforeach()
142
143 if(_libraries_work)
144 # Test this combination of libraries.
145 set(CMAKE_REQUIRED_LIBRARIES ${_flags} ${${LIBRARIES}} ${_threadlibs})
146 #message("DEBUG: CMAKE_REQUIRED_LIBRARIES = ${CMAKE_REQUIRED_LIBRARIES}")
147 if(CMAKE_Fortran_COMPILER_LOADED)
148 check_fortran_function_exists("${_name}" ${_prefix}${_combined_name}_WORKS)
149 else()
150 check_function_exists("${_name}_" ${_prefix}${_combined_name}_WORKS)
151 endif()
152 set(CMAKE_REQUIRED_LIBRARIES)
153 set(_libraries_work ${${_prefix}${_combined_name}_WORKS})
154 endif()
155
156 if(_libraries_work)
157 if("${_list}" STREQUAL "")
158 set(${LIBRARIES} "${LIBRARIES}-PLACEHOLDER-FOR-EMPTY-LIBRARIES")
159 else()
160 set(${LIBRARIES} ${${LIBRARIES}} ${_threadlibs})
161 endif()
162 else()
163 set(${LIBRARIES} FALSE)
164 endif()
165 #message("DEBUG: ${LIBRARIES} = ${${LIBRARIES}}")
166 endmacro()
167
168 set(MKL_LIBRARIES)
169 if(NOT $ENV{BLA_VENDOR} STREQUAL "")
170 set(BLA_VENDOR $ENV{BLA_VENDOR})
171 else()
172 if(NOT BLA_VENDOR)
173 set(BLA_VENDOR "All")
174 endif()
175 endif()
176 if(BLA_VENDOR_THREADING)
177 set(BLAS_mkl_THREADING ${BLA_VENDOR_THREADING})
178 else()
179 if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
180 set(BLAS_mkl_THREADING "gnu")
181 else()
182 set(BLAS_mkl_THREADING "intel")
183 endif()
184 endif()
185
186 if(CMAKE_C_COMPILER_LOADED OR CMAKE_CXX_COMPILER_LOADED)
187 # System-specific settings
188 if(WIN32)
189 if(BLA_STATIC)
190 set(BLAS_mkl_DLL_SUFFIX "")
191 else()
192 set(BLAS_mkl_DLL_SUFFIX "_dll")
193 endif()
194 else()
195 if(BLA_STATIC)
196 set(BLAS_mkl_START_GROUP "-Wl,--start-group")
197 set(BLAS_mkl_END_GROUP "-Wl,--end-group")
198 else()
199 set(BLAS_mkl_START_GROUP "")
200 set(BLAS_mkl_END_GROUP "")
201 endif()
202 if(BLAS_mkl_THREADING STREQUAL "gnu")
203 set(BLAS_mkl_OMP "gomp")
204 else()
205 set(BLAS_mkl_OMP "iomp5")
206 endif()
207 set(BLAS_mkl_LM "-lm")
208 set(BLAS_mkl_LDL "-ldl")
209 endif()
210
211 if(BLAS_FIND_QUIETLY OR NOT BLAS_FIND_REQUIRED)
212 find_package(Threads)
213 else()
214 find_package(Threads REQUIRED)
215 endif()
216
217 set(BLAS_mkl_INTFACE "intel")
218 if(BLA_VENDOR MATCHES "_64ilp")
219 set(BLAS_mkl_ILP_MODE "ilp64")
220 else()
221 set(BLAS_mkl_ILP_MODE "lp64")
222 endif()
223
224 set(BLAS_SEARCH_LIBS "")
225
226 set(BLAS_mkl_SEARCH_SYMBOL sgemm)
227 set(_LIBRARIES MKL_LIBRARIES)
228 if(WIN32)
229 # Find the main file (32-bit or 64-bit)
230 set(BLAS_SEARCH_LIBS_WIN_MAIN "")
231 if(BLA_VENDOR STREQUAL "Intel10_32" OR BLA_VENDOR STREQUAL "All")
232 list(APPEND BLAS_SEARCH_LIBS_WIN_MAIN
233 "mkl_intel_c${BLAS_mkl_DLL_SUFFIX}")
234 endif()
235 if(BLA_VENDOR MATCHES "^Intel10_64i?lp" OR BLA_VENDOR STREQUAL "All")
236 list(APPEND BLAS_SEARCH_LIBS_WIN_MAIN
237 "mkl_intel_${BLAS_mkl_ILP_MODE}${BLAS_mkl_DLL_SUFFIX}")
238 endif()
239
240 # Add threading/sequential libs
241 set(BLAS_SEARCH_LIBS_WIN_THREAD "")
242 if(BLA_VENDOR MATCHES "^Intel10_64i?lp$" OR BLA_VENDOR STREQUAL "All")
243 # old version
244 list(APPEND BLAS_SEARCH_LIBS_WIN_THREAD
245 "libguide40 mkl_intel_thread${BLAS_mkl_DLL_SUFFIX}")
246 # mkl >= 10.3
247 list(APPEND BLAS_SEARCH_LIBS_WIN_THREAD
248 "libiomp5md mkl_intel_thread${BLAS_mkl_DLL_SUFFIX}")
249 endif()
250 if(BLA_VENDOR MATCHES "^Intel10_64i?lp_seq$" OR BLA_VENDOR STREQUAL "All")
251 list(APPEND BLAS_SEARCH_LIBS_WIN_THREAD
252 "mkl_sequential${BLAS_mkl_DLL_SUFFIX}")
253 endif()
254
255 # Cartesian product of the above
256 foreach(MAIN ${BLAS_SEARCH_LIBS_WIN_MAIN})
257 foreach(THREAD ${BLAS_SEARCH_LIBS_WIN_THREAD})
258 list(APPEND BLAS_SEARCH_LIBS
259 "${MAIN} ${THREAD} mkl_core${BLAS_mkl_DLL_SUFFIX}")
260 endforeach()
261 endforeach()
262 else()
263 if(BLA_VENDOR STREQUAL "Intel10_32" OR BLA_VENDOR STREQUAL "All")
264 # old version
265 list(APPEND BLAS_SEARCH_LIBS
266 "mkl_${BLAS_mkl_INTFACE} mkl_${BLAS_mkl_THREADING}_thread mkl_core guide")
267
268 # mkl >= 10.3
269 list(APPEND BLAS_SEARCH_LIBS
270 "${BLAS_mkl_START_GROUP} mkl_${BLAS_mkl_INTFACE} mkl_${BLAS_mkl_THREADING}_thread mkl_core ${BLAS_mkl_END_GROUP} ${BLAS_mkl_OMP}")
271 endif()
272 if(BLA_VENDOR MATCHES "^Intel10_64i?lp$" OR BLA_VENDOR STREQUAL "All")
273 # old version
274 list(APPEND BLAS_SEARCH_LIBS
275 "mkl_${BLAS_mkl_INTFACE}_${BLAS_mkl_ILP_MODE} mkl_${BLAS_mkl_THREADING}_thread mkl_core guide")
276
277 # mkl >= 10.3
278 list(APPEND BLAS_SEARCH_LIBS
279 "${BLAS_mkl_START_GROUP} mkl_${BLAS_mkl_INTFACE}_${BLAS_mkl_ILP_MODE} mkl_${BLAS_mkl_THREADING}_thread mkl_core ${BLAS_mkl_END_GROUP} ${BLAS_mkl_OMP}")
280 endif()
281 if(BLA_VENDOR MATCHES "^Intel10_64i?lp_seq$" OR BLA_VENDOR STREQUAL "All")
282 list(APPEND BLAS_SEARCH_LIBS
283 "${BLAS_mkl_START_GROUP} mkl_${BLAS_mkl_INTFACE}_${BLAS_mkl_ILP_MODE} mkl_sequential mkl_core ${BLAS_mkl_END_GROUP}")
284 endif()
285
286 #older vesions of intel mkl libs
287 if(BLA_VENDOR STREQUAL "Intel" OR BLA_VENDOR STREQUAL "All")
288 list(APPEND BLAS_SEARCH_LIBS
289 "mkl")
290 list(APPEND BLAS_SEARCH_LIBS
291 "mkl_ia32")
292 list(APPEND BLAS_SEARCH_LIBS
293 "mkl_em64t")
294 endif()
295 endif()
296
297 if(BLA_VENDOR MATCHES "^Intel10_64_dyn$" OR BLA_VENDOR STREQUAL "All")
298 # mkl >= 10.3 with single dynamic library
299 list(APPEND BLAS_SEARCH_LIBS
300 "mkl_rt")
301 endif()
302
303 # MKL uses a multitude of partially platform-specific subdirectories:
304 if(BLA_VENDOR STREQUAL "Intel10_32")
305 set(BLAS_mkl_ARCH_NAME "ia32")
306 else()
307 set(BLAS_mkl_ARCH_NAME "intel64")
308 endif()
309 if(WIN32)
310 set(BLAS_mkl_OS_NAME "win")
311 elseif(APPLE)
312 set(BLAS_mkl_OS_NAME "mac")
313 else()
314 set(BLAS_mkl_OS_NAME "lin")
315 endif()
316 if(DEFINED ENV{MKLROOT})
317 file(TO_CMAKE_PATH "$ENV{MKLROOT}" BLAS_mkl_MKLROOT)
318 # If MKLROOT points to the subdirectory 'mkl', use the parent directory instead
319 # so we can better detect other relevant libraries in 'compiler' or 'tbb':
320 get_filename_component(BLAS_mkl_MKLROOT_LAST_DIR "${BLAS_mkl_MKLROOT}" NAME)
321 if(BLAS_mkl_MKLROOT_LAST_DIR STREQUAL "mkl")
322 get_filename_component(BLAS_mkl_MKLROOT "${BLAS_mkl_MKLROOT}" DIRECTORY)
323 endif()
324 endif()
325 set(BLAS_mkl_LIB_PATH_SUFFIXES
326 "compiler/lib" "compiler/lib/${BLAS_mkl_ARCH_NAME}_${BLAS_mkl_OS_NAME}"
327 "mkl/lib" "mkl/lib/${BLAS_mkl_ARCH_NAME}_${BLAS_mkl_OS_NAME}"
328 "lib/${BLAS_mkl_ARCH_NAME}_${BLAS_mkl_OS_NAME}")
329
330 foreach(IT ${BLAS_SEARCH_LIBS})
331 string(REPLACE " " ";" SEARCH_LIBS ${IT})
332 if(NOT ${_LIBRARIES})
333 check_blas_libraries(
334 ${_LIBRARIES}
335 BLAS
336 ${BLAS_mkl_SEARCH_SYMBOL}
337 ""
338 "${SEARCH_LIBS}"
339 "${CMAKE_THREAD_LIBS_INIT};${BLAS_mkl_LM};${BLAS_mkl_LDL}"
340 "${BLAS_mkl_MKLROOT}"
341 "${BLAS_mkl_LIB_PATH_SUFFIXES}"
342 )
343 endif()
344 endforeach()
345
346 unset(BLAS_mkl_ILP_MODE)
347 unset(BLAS_mkl_INTFACE)
348 unset(BLAS_mkl_THREADING)
349 unset(BLAS_mkl_OMP)
350 unset(BLAS_mkl_DLL_SUFFIX)
351 unset(BLAS_mkl_LM)
352 unset(BLAS_mkl_LDL)
353 unset(BLAS_mkl_MKLROOT)
354 unset(BLAS_mkl_MKLROOT_LAST_DIR)
355 unset(BLAS_mkl_ARCH_NAME)
356 unset(BLAS_mkl_OS_NAME)
357 unset(BLAS_mkl_LIB_PATH_SUFFIXES)
358 endif()
359
360
361 find_package_handle_standard_args(MKL REQUIRED_VARS MKL_LIBRARIES)
362
363 cmake_pop_check_state()
364 set(CMAKE_FIND_LIBRARY_SUFFIXES ${_blas_ORIG_CMAKE_FIND_LIBRARY_SUFFIXES})