1 # Distributed under the OSI-approved BSD 3-Clause License. See accompanying
2 # file Copyright.txt or https://cmake.org/licensing for details.
10 # This module finds an installed fortran library that implements the
11 # BLAS linear-algebra interface (see http://www.netlib.org/blas/). The
12 # list of libraries searched for is taken from the autoconf macro file,
13 # acx_blas.m4 (distributed at
14 # http://ac-archive.sourceforge.net/ac-archive/acx_blas.html).
16 # This module sets the following variables:
20 # BLAS_FOUND - set to true if a library implementing the BLAS interface
22 # BLAS_LINKER_FLAGS - uncached list of required linker flags (excluding -l
24 # BLAS_LIBRARIES - uncached list of libraries (using full path name) to
25 # link against to use BLAS
26 # BLAS95_LIBRARIES - uncached list of libraries (using full path name)
27 # to link against to use BLAS95 interface
28 # BLAS95_FOUND - set to true if a library implementing the BLAS f95 interface
31 # The following variables can be used to control this module:
35 # BLA_STATIC if set on this determines what kind of linkage we do (static)
36 # BLA_VENDOR if set checks only the specified vendor, if not set checks
37 # all the possibilities
38 # BLA_F95 if set on tries to find the f95 interfaces for BLAS/LAPACK
39 # BLA_PREFER_PKGCONFIG if set pkg-config will be used to search for a BLAS
40 # library first and if one is found that is preferred
42 # List of vendors (BLA_VENDOR) valid in this module:
54 # * Intel10_32 (intel mkl v10 32 bit)
55 # * Intel10_64lp (intel mkl v10 64 bit, lp thread model, lp64 model)
56 # * Intel10_64lp_seq (intel mkl v10 64 bit, sequential code, lp64 model)
57 # * Intel (older versions of mkl 32 and 64 bit)
67 # C/CXX should be enabled to use Intel mkl
70 include(${CMAKE_CURRENT_LIST_DIR}/CheckFunctionExists.cmake)
71 include(${CMAKE_CURRENT_LIST_DIR}/CheckFortranFunctionExists.cmake)
72 include(${CMAKE_CURRENT_LIST_DIR}/CMakePushCheckState.cmake)
73 include(${CMAKE_CURRENT_LIST_DIR}/FindPackageHandleStandardArgs.cmake)
74 cmake_push_check_state()
75 set(CMAKE_REQUIRED_QUIET ${BLAS_FIND_QUIETLY})
77 set(_blas_ORIG_CMAKE_FIND_LIBRARY_SUFFIXES ${CMAKE_FIND_LIBRARY_SUFFIXES})
79 # Check the language being used
80 if( NOT (CMAKE_C_COMPILER_LOADED OR CMAKE_CXX_COMPILER_LOADED OR CMAKE_Fortran_COMPILER_LOADED) )
81 if(BLAS_FIND_REQUIRED)
82 message(FATAL_ERROR "FindBLAS requires Fortran, C, or C++ to be enabled.")
84 message(STATUS "Looking for BLAS... - NOT found (Unsupported languages)")
89 if(BLA_PREFER_PKGCONFIG)
90 find_package(PkgConfig)
91 pkg_check_modules(PKGC_BLAS IMPORTED_TARGET blas)
93 # FIXME: We should not interpret the INTERFACE_LINK_LIBRARIES property
94 # because it could have generator expressions and such. This is a
95 # workaround for pkg_check_modules not providing a first-class way to
96 # get the list of libraries.
97 get_property(BLAS_LIBRARIES TARGET PkgConfig::PKGC_BLAS PROPERTY INTERFACE_LINK_LIBRARIES)
98 find_package_handle_standard_args(BLAS
99 REQUIRED_VARS BLAS_LIBRARIES
100 VERSION_VAR PKGC_BLAS_VERSION)
105 macro(Check_Fortran_Libraries LIBRARIES _prefix _name _flags _list _thread)
106 # This macro checks for the existence of the combination of fortran libraries
107 # given by _list. If the combination is found, this macro checks (using the
108 # Check_Fortran_Function_Exists macro) whether can link against that library
109 # combination using the name of a routine given by _name using the linker
110 # flags given by _flags. If the combination of libraries is found and passes
111 # the link test, LIBRARIES is set to the list of complete library paths that
112 # have been found. Otherwise, LIBRARIES is set to FALSE.
114 # N.B. _prefix is the prefix applied to the names of all cached variables that
115 # are generated internally and marked advanced by this macro.
119 set(_libraries_work TRUE)
126 set(_libdir ENV DYLD_LIBRARY_PATH)
128 set(_libdir ENV LD_LIBRARY_PATH)
132 foreach(_library ${_list})
133 set(_combined_name ${_combined_name}_${_library})
138 set(CMAKE_FIND_LIBRARY_SUFFIXES .lib ${CMAKE_FIND_LIBRARY_SUFFIXES})
141 set(CMAKE_FIND_LIBRARY_SUFFIXES .lib ${CMAKE_FIND_LIBRARY_SUFFIXES})
143 set(CMAKE_FIND_LIBRARY_SUFFIXES .a ${CMAKE_FIND_LIBRARY_SUFFIXES})
146 if (CMAKE_SYSTEM_NAME STREQUAL "Linux")
147 # for ubuntu's libblas3gf and liblapack3gf packages
148 set(CMAKE_FIND_LIBRARY_SUFFIXES ${CMAKE_FIND_LIBRARY_SUFFIXES} .so.3gf)
151 find_library(${_prefix}_${_library}_LIBRARY
155 mark_as_advanced(${_prefix}_${_library}_LIBRARY)
156 set(${LIBRARIES} ${${LIBRARIES}} ${${_prefix}_${_library}_LIBRARY})
157 set(_libraries_work ${${_prefix}_${_library}_LIBRARY})
161 # Test this combination of libraries.
162 set(CMAKE_REQUIRED_LIBRARIES ${_flags} ${${LIBRARIES}} ${_thread})
163 # message("DEBUG: CMAKE_REQUIRED_LIBRARIES = ${CMAKE_REQUIRED_LIBRARIES}")
164 if (CMAKE_Fortran_COMPILER_LOADED)
165 check_fortran_function_exists("${_name}" ${_prefix}${_combined_name}_WORKS)
167 check_function_exists("${_name}_" ${_prefix}${_combined_name}_WORKS)
169 set(CMAKE_REQUIRED_LIBRARIES)
170 mark_as_advanced(${_prefix}${_combined_name}_WORKS)
171 set(_libraries_work ${${_prefix}${_combined_name}_WORKS})
173 if(NOT _libraries_work)
174 set(${LIBRARIES} FALSE)
176 #message("DEBUG: ${LIBRARIES} = ${${LIBRARIES}}")
179 set(BLAS_LINKER_FLAGS)
181 set(BLAS95_LIBRARIES)
182 if (NOT $ENV{BLA_VENDOR} STREQUAL "")
183 set(BLA_VENDOR $ENV{BLA_VENDOR})
186 set(BLA_VENDOR "All")
190 if (BLA_VENDOR STREQUAL "Goto" OR BLA_VENDOR STREQUAL "All")
191 if(NOT BLAS_LIBRARIES)
192 # gotoblas (http://www.tacc.utexas.edu/tacc-projects/gotoblas2)
193 check_fortran_libraries(
204 if (BLA_VENDOR STREQUAL "OpenBLAS" OR BLA_VENDOR STREQUAL "All")
205 if(NOT BLAS_LIBRARIES)
206 # OpenBLAS (http://www.openblas.net)
207 check_fortran_libraries(
218 if (BLA_VENDOR STREQUAL "FLAME" OR BLA_VENDOR STREQUAL "All")
219 if(NOT BLAS_LIBRARIES)
220 # FLAME's blis library (https://github.com/flame/blis)
221 check_fortran_libraries(
232 if (BLA_VENDOR STREQUAL "ATLAS" OR BLA_VENDOR STREQUAL "All")
233 if(NOT BLAS_LIBRARIES)
234 # BLAS in ATLAS library? (http://math-atlas.sourceforge.net/)
235 check_fortran_libraries(
246 # BLAS in PhiPACK libraries? (requires generic BLAS lib, too)
247 if (BLA_VENDOR STREQUAL "PhiPACK" OR BLA_VENDOR STREQUAL "All")
248 if(NOT BLAS_LIBRARIES)
249 check_fortran_libraries(
260 # BLAS in Alpha CXML library?
261 if (BLA_VENDOR STREQUAL "CXML" OR BLA_VENDOR STREQUAL "All")
262 if(NOT BLAS_LIBRARIES)
263 check_fortran_libraries(
274 # BLAS in Alpha DXML library? (now called CXML, see above)
275 if (BLA_VENDOR STREQUAL "DXML" OR BLA_VENDOR STREQUAL "All")
276 if(NOT BLAS_LIBRARIES)
277 check_fortran_libraries(
288 # BLAS in Sun Performance library?
289 if (BLA_VENDOR STREQUAL "SunPerf" OR BLA_VENDOR STREQUAL "All")
290 if(NOT BLAS_LIBRARIES)
291 check_fortran_libraries(
300 set(BLAS_LINKER_FLAGS "-xlic_lib=sunperf")
305 # BLAS in SCSL library? (SGI/Cray Scientific Library)
306 if (BLA_VENDOR STREQUAL "SCSL" OR BLA_VENDOR STREQUAL "All")
307 if(NOT BLAS_LIBRARIES)
308 check_fortran_libraries(
319 # BLAS in SGIMATH library?
320 if (BLA_VENDOR STREQUAL "SGIMATH" OR BLA_VENDOR STREQUAL "All")
321 if(NOT BLAS_LIBRARIES)
322 check_fortran_libraries(
333 # BLAS in IBM ESSL library? (requires generic BLAS lib, too)
334 if (BLA_VENDOR STREQUAL "IBMESSL" OR BLA_VENDOR STREQUAL "All")
335 if(NOT BLAS_LIBRARIES)
336 check_fortran_libraries(
347 #BLAS in acml library?
348 if (BLA_VENDOR MATCHES "ACML" OR BLA_VENDOR STREQUAL "All")
349 if( ((BLA_VENDOR STREQUAL "ACML") AND (NOT BLAS_ACML_LIB_DIRS)) OR
350 ((BLA_VENDOR STREQUAL "ACML_MP") AND (NOT BLAS_ACML_MP_LIB_DIRS)) OR
351 ((BLA_VENDOR STREQUAL "ACML_GPU") AND (NOT BLAS_ACML_GPU_LIB_DIRS))
353 # try to find acml in "standard" paths
355 file( GLOB _ACML_ROOT "C:/AMD/acml*/ACML-EULA.txt" )
357 file( GLOB _ACML_ROOT "/opt/acml*/ACML-EULA.txt" )
360 file( GLOB _ACML_GPU_ROOT "C:/AMD/acml*/GPGPUexamples" )
362 file( GLOB _ACML_GPU_ROOT "/opt/acml*/GPGPUexamples" )
364 list(GET _ACML_ROOT 0 _ACML_ROOT)
365 list(GET _ACML_GPU_ROOT 0 _ACML_GPU_ROOT)
367 get_filename_component( _ACML_ROOT ${_ACML_ROOT} PATH )
368 if( SIZEOF_INTEGER EQUAL 8 )
369 set( _ACML_PATH_SUFFIX "_int64" )
371 set( _ACML_PATH_SUFFIX "" )
373 if( CMAKE_Fortran_COMPILER_ID STREQUAL "Intel" )
374 set( _ACML_COMPILER32 "ifort32" )
375 set( _ACML_COMPILER64 "ifort64" )
376 elseif( CMAKE_Fortran_COMPILER_ID STREQUAL "SunPro" )
377 set( _ACML_COMPILER32 "sun32" )
378 set( _ACML_COMPILER64 "sun64" )
379 elseif( CMAKE_Fortran_COMPILER_ID STREQUAL "PGI" )
380 set( _ACML_COMPILER32 "pgi32" )
382 set( _ACML_COMPILER64 "win64" )
384 set( _ACML_COMPILER64 "pgi64" )
386 elseif( CMAKE_Fortran_COMPILER_ID STREQUAL "Open64" )
387 # 32 bit builds not supported on Open64 but for code simplicity
388 # We'll just use the same directory twice
389 set( _ACML_COMPILER32 "open64_64" )
390 set( _ACML_COMPILER64 "open64_64" )
391 elseif( CMAKE_Fortran_COMPILER_ID STREQUAL "NAG" )
392 set( _ACML_COMPILER32 "nag32" )
393 set( _ACML_COMPILER64 "nag64" )
395 set( _ACML_COMPILER32 "gfortran32" )
396 set( _ACML_COMPILER64 "gfortran64" )
399 if( BLA_VENDOR STREQUAL "ACML_MP" )
400 set(_ACML_MP_LIB_DIRS
401 "${_ACML_ROOT}/${_ACML_COMPILER32}_mp${_ACML_PATH_SUFFIX}/lib"
402 "${_ACML_ROOT}/${_ACML_COMPILER64}_mp${_ACML_PATH_SUFFIX}/lib" )
405 "${_ACML_ROOT}/${_ACML_COMPILER32}${_ACML_PATH_SUFFIX}/lib"
406 "${_ACML_ROOT}/${_ACML_COMPILER64}${_ACML_PATH_SUFFIX}/lib" )
409 elseif(BLAS_${BLA_VENDOR}_LIB_DIRS)
410 set(_${BLA_VENDOR}_LIB_DIRS ${BLAS_${BLA_VENDOR}_LIB_DIRS})
413 if( BLA_VENDOR STREQUAL "ACML_MP" )
414 foreach( BLAS_ACML_MP_LIB_DIRS ${_ACML_MP_LIB_DIRS})
415 check_fortran_libraries (
419 "" "acml_mp;acml_mv" "" ${BLAS_ACML_MP_LIB_DIRS}
425 elseif( BLA_VENDOR STREQUAL "ACML_GPU" )
426 foreach( BLAS_ACML_GPU_LIB_DIRS ${_ACML_GPU_LIB_DIRS})
427 check_fortran_libraries (
431 "" "acml;acml_mv;CALBLAS" "" ${BLAS_ACML_GPU_LIB_DIRS}
438 foreach( BLAS_ACML_LIB_DIRS ${_ACML_LIB_DIRS} )
439 check_fortran_libraries (
443 "" "acml;acml_mv" "" ${BLAS_ACML_LIB_DIRS}
451 # Either acml or acml_mp should be in LD_LIBRARY_PATH but not both
452 if(NOT BLAS_LIBRARIES)
453 check_fortran_libraries(
462 if(NOT BLAS_LIBRARIES)
463 check_fortran_libraries(
472 if(NOT BLAS_LIBRARIES)
473 check_fortran_libraries(
478 "acml;acml_mv;CALBLAS"
484 # Apple BLAS library?
485 if (BLA_VENDOR STREQUAL "Apple" OR BLA_VENDOR STREQUAL "All")
486 if(NOT BLAS_LIBRARIES)
487 check_fortran_libraries(
498 if (BLA_VENDOR STREQUAL "NAS" OR BLA_VENDOR STREQUAL "All")
499 if ( NOT BLAS_LIBRARIES )
500 check_fortran_libraries(
510 # Generic BLAS library?
511 if (BLA_VENDOR STREQUAL "Generic" OR BLA_VENDOR STREQUAL "All")
512 if(NOT BLAS_LIBRARIES)
513 check_fortran_libraries(
524 #BLAS in intel mkl 10 library? (em64t 64bit)
525 if (BLA_VENDOR MATCHES "Intel" OR BLA_VENDOR STREQUAL "All")
529 if (CMAKE_C_COMPILER_LOADED OR CMAKE_CXX_COMPILER_LOADED)
530 if(BLAS_FIND_QUIETLY OR NOT BLAS_FIND_REQUIRED)
531 find_package(Threads)
533 find_package(Threads REQUIRED)
536 set(BLAS_SEARCH_LIBS "")
539 set(BLAS_mkl_SEARCH_SYMBOL SGEMM)
540 set(_LIBRARIES BLAS95_LIBRARIES)
543 set(BLAS_mkl_DLL_SUFFIX "")
545 set(BLAS_mkl_DLL_SUFFIX "_dll")
548 # Find the main file (32-bit or 64-bit)
549 set(BLAS_SEARCH_LIBS_WIN_MAIN "")
550 if (BLA_VENDOR STREQUAL "Intel10_32" OR BLA_VENDOR STREQUAL "All")
551 list(APPEND BLAS_SEARCH_LIBS_WIN_MAIN
552 "mkl_blas95${BLAS_mkl_DLL_SUFFIX} mkl_intel_c${BLAS_mkl_DLL_SUFFIX}")
554 if (BLA_VENDOR MATCHES "^Intel10_64lp" OR BLA_VENDOR STREQUAL "All")
555 list(APPEND BLAS_SEARCH_LIBS_WIN_MAIN
556 "mkl_blas95_lp64${BLAS_mkl_DLL_SUFFIX} mkl_intel_lp64${BLAS_mkl_DLL_SUFFIX}")
559 # Add threading/sequential libs
560 set(BLAS_SEARCH_LIBS_WIN_THREAD "")
561 if (BLA_VENDOR MATCHES "_seq$" OR BLA_VENDOR STREQUAL "All")
562 list(APPEND BLAS_SEARCH_LIBS_WIN_THREAD
563 "mkl_sequential${BLAS_mkl_DLL_SUFFIX}")
565 if (NOT BLA_VENDOR MATCHES "_seq$" OR BLA_VENDOR STREQUAL "All")
567 list(APPEND BLAS_SEARCH_LIBS_WIN_THREAD
568 "libguide40 mkl_intel_thread${BLAS_mkl_DLL_SUFFIX}")
570 list(APPEND BLAS_SEARCH_LIBS_WIN_THREAD
571 "libiomp5md mkl_intel_thread${BLAS_mkl_DLL_SUFFIX}")
574 # Cartesian product of the above
575 foreach (MAIN ${BLAS_SEARCH_LIBS_WIN_MAIN})
576 foreach (THREAD ${BLAS_SEARCH_LIBS_WIN_THREAD})
577 list(APPEND BLAS_SEARCH_LIBS
578 "${MAIN} ${THREAD} mkl_core${BLAS_mkl_DLL_SUFFIX}")
582 if (BLA_VENDOR STREQUAL "Intel10_32" OR BLA_VENDOR STREQUAL "All")
583 list(APPEND BLAS_SEARCH_LIBS
584 "mkl_blas95 mkl_intel mkl_intel_thread mkl_core guide")
586 if (BLA_VENDOR STREQUAL "Intel10_64lp" OR BLA_VENDOR STREQUAL "All")
588 list(APPEND BLAS_SEARCH_LIBS
589 "mkl_blas95 mkl_intel_lp64 mkl_intel_thread mkl_core guide")
592 if (CMAKE_C_COMPILER MATCHES ".+gcc")
593 list(APPEND BLAS_SEARCH_LIBS
594 "mkl_blas95_lp64 mkl_intel_lp64 mkl_gnu_thread mkl_core gomp")
596 list(APPEND BLAS_SEARCH_LIBS
597 "mkl_blas95_lp64 mkl_intel_lp64 mkl_intel_thread mkl_core iomp5")
600 if (BLA_VENDOR STREQUAL "Intel10_64lp_seq" OR BLA_VENDOR STREQUAL "All")
601 list(APPEND BLAS_SEARCH_LIBS
602 "mkl_intel_lp64 mkl_sequential mkl_core")
606 set(BLAS_mkl_SEARCH_SYMBOL sgemm)
607 set(_LIBRARIES BLAS_LIBRARIES)
610 set(BLAS_mkl_DLL_SUFFIX "")
612 set(BLAS_mkl_DLL_SUFFIX "_dll")
615 # Find the main file (32-bit or 64-bit)
616 set(BLAS_SEARCH_LIBS_WIN_MAIN "")
617 if (BLA_VENDOR STREQUAL "Intel10_32" OR BLA_VENDOR STREQUAL "All")
618 list(APPEND BLAS_SEARCH_LIBS_WIN_MAIN
619 "mkl_intel_c${BLAS_mkl_DLL_SUFFIX}")
621 if (BLA_VENDOR MATCHES "^Intel10_64lp" OR BLA_VENDOR STREQUAL "All")
622 list(APPEND BLAS_SEARCH_LIBS_WIN_MAIN
623 "mkl_intel_lp64${BLAS_mkl_DLL_SUFFIX}")
626 # Add threading/sequential libs
627 set(BLAS_SEARCH_LIBS_WIN_THREAD "")
628 if (NOT BLA_VENDOR MATCHES "_seq$" OR BLA_VENDOR STREQUAL "All")
630 list(APPEND BLAS_SEARCH_LIBS_WIN_THREAD
631 "libguide40 mkl_intel_thread${BLAS_mkl_DLL_SUFFIX}")
633 list(APPEND BLAS_SEARCH_LIBS_WIN_THREAD
634 "libiomp5md mkl_intel_thread${BLAS_mkl_DLL_SUFFIX}")
636 if (BLA_VENDOR MATCHES "_seq$" OR BLA_VENDOR STREQUAL "All")
637 list(APPEND BLAS_SEARCH_LIBS_WIN_THREAD
638 "mkl_sequential${BLAS_mkl_DLL_SUFFIX}")
641 # Cartesian product of the above
642 foreach (MAIN ${BLAS_SEARCH_LIBS_WIN_MAIN})
643 foreach (THREAD ${BLAS_SEARCH_LIBS_WIN_THREAD})
644 list(APPEND BLAS_SEARCH_LIBS
645 "${MAIN} ${THREAD} mkl_core${BLAS_mkl_DLL_SUFFIX}")
649 if (BLA_VENDOR STREQUAL "Intel10_32" OR BLA_VENDOR STREQUAL "All")
650 list(APPEND BLAS_SEARCH_LIBS
651 "mkl_intel mkl_intel_thread mkl_core guide")
653 if (BLA_VENDOR STREQUAL "Intel10_64lp" OR BLA_VENDOR STREQUAL "All")
656 list(APPEND BLAS_SEARCH_LIBS
657 "mkl_intel_lp64 mkl_intel_thread mkl_core guide")
660 if (CMAKE_C_COMPILER MATCHES ".+gcc")
661 list(APPEND BLAS_SEARCH_LIBS
662 "mkl_intel_lp64 mkl_gnu_thread mkl_core gomp")
664 list(APPEND BLAS_SEARCH_LIBS
665 "mkl_intel_lp64 mkl_intel_thread mkl_core iomp5")
668 if (BLA_VENDOR STREQUAL "Intel10_64lp_seq" OR BLA_VENDOR STREQUAL "All")
669 list(APPEND BLAS_SEARCH_LIBS
670 "mkl_intel_lp64 mkl_sequential mkl_core")
673 #older vesions of intel mkl libs
674 if (BLA_VENDOR STREQUAL "Intel" OR BLA_VENDOR STREQUAL "All")
675 list(APPEND BLAS_SEARCH_LIBS
677 list(APPEND BLAS_SEARCH_LIBS
679 list(APPEND BLAS_SEARCH_LIBS
685 foreach (IT ${BLAS_SEARCH_LIBS})
686 string(REPLACE " " ";" SEARCH_LIBS ${IT})
687 if (NOT ${_LIBRARIES})
688 check_fortran_libraries(
691 ${BLAS_mkl_SEARCH_SYMBOL}
694 "${CMAKE_THREAD_LIBS_INIT};${LM}"
703 find_package_handle_standard_args(BLAS REQUIRED_VARS BLAS95_LIBRARIES)
704 set(BLAS95_FOUND ${BLAS_FOUND})
706 set(BLAS_LIBRARIES "${BLAS95_LIBRARIES}")
709 find_package_handle_standard_args(BLAS REQUIRED_VARS BLAS_LIBRARIES)
712 cmake_pop_check_state()
713 set(CMAKE_FIND_LIBRARY_SUFFIXES ${_blas_ORIG_CMAKE_FIND_LIBRARY_SUFFIXES})