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 set(BLAS_LIBRARIES PkgConfig::PKGC_BLAS)
94 find_package_handle_standard_args(BLAS
95 REQUIRED_VARS BLAS_LIBRARIES
96 VERSION_VAR PKGC_BLAS_VERSION)
101 macro(Check_Fortran_Libraries LIBRARIES _prefix _name _flags _list _thread)
102 # This macro checks for the existence of the combination of fortran libraries
103 # given by _list. If the combination is found, this macro checks (using the
104 # Check_Fortran_Function_Exists macro) whether can link against that library
105 # combination using the name of a routine given by _name using the linker
106 # flags given by _flags. If the combination of libraries is found and passes
107 # the link test, LIBRARIES is set to the list of complete library paths that
108 # have been found. Otherwise, LIBRARIES is set to FALSE.
110 # N.B. _prefix is the prefix applied to the names of all cached variables that
111 # are generated internally and marked advanced by this macro.
115 set(_libraries_work TRUE)
122 set(_libdir ENV DYLD_LIBRARY_PATH)
124 set(_libdir ENV LD_LIBRARY_PATH)
128 foreach(_library ${_list})
129 set(_combined_name ${_combined_name}_${_library})
134 set(CMAKE_FIND_LIBRARY_SUFFIXES .lib ${CMAKE_FIND_LIBRARY_SUFFIXES})
137 set(CMAKE_FIND_LIBRARY_SUFFIXES .lib ${CMAKE_FIND_LIBRARY_SUFFIXES})
139 set(CMAKE_FIND_LIBRARY_SUFFIXES .a ${CMAKE_FIND_LIBRARY_SUFFIXES})
142 if (CMAKE_SYSTEM_NAME STREQUAL "Linux")
143 # for ubuntu's libblas3gf and liblapack3gf packages
144 set(CMAKE_FIND_LIBRARY_SUFFIXES ${CMAKE_FIND_LIBRARY_SUFFIXES} .so.3gf)
147 find_library(${_prefix}_${_library}_LIBRARY
151 mark_as_advanced(${_prefix}_${_library}_LIBRARY)
152 set(${LIBRARIES} ${${LIBRARIES}} ${${_prefix}_${_library}_LIBRARY})
153 set(_libraries_work ${${_prefix}_${_library}_LIBRARY})
157 # Test this combination of libraries.
158 set(CMAKE_REQUIRED_LIBRARIES ${_flags} ${${LIBRARIES}} ${_thread})
159 # message("DEBUG: CMAKE_REQUIRED_LIBRARIES = ${CMAKE_REQUIRED_LIBRARIES}")
160 if (CMAKE_Fortran_COMPILER_LOADED)
161 check_fortran_function_exists("${_name}" ${_prefix}${_combined_name}_WORKS)
163 check_function_exists("${_name}_" ${_prefix}${_combined_name}_WORKS)
165 set(CMAKE_REQUIRED_LIBRARIES)
166 mark_as_advanced(${_prefix}${_combined_name}_WORKS)
167 set(_libraries_work ${${_prefix}${_combined_name}_WORKS})
169 if(NOT _libraries_work)
170 set(${LIBRARIES} FALSE)
172 #message("DEBUG: ${LIBRARIES} = ${${LIBRARIES}}")
175 set(BLAS_LINKER_FLAGS)
177 set(BLAS95_LIBRARIES)
178 if (NOT $ENV{BLA_VENDOR} STREQUAL "")
179 set(BLA_VENDOR $ENV{BLA_VENDOR})
182 set(BLA_VENDOR "All")
186 if (BLA_VENDOR STREQUAL "Goto" OR BLA_VENDOR STREQUAL "All")
187 if(NOT BLAS_LIBRARIES)
188 # gotoblas (http://www.tacc.utexas.edu/tacc-projects/gotoblas2)
189 check_fortran_libraries(
200 if (BLA_VENDOR STREQUAL "OpenBLAS" OR BLA_VENDOR STREQUAL "All")
201 if(NOT BLAS_LIBRARIES)
202 # OpenBLAS (http://www.openblas.net)
203 check_fortran_libraries(
214 if (BLA_VENDOR STREQUAL "FLAME" OR BLA_VENDOR STREQUAL "All")
215 if(NOT BLAS_LIBRARIES)
216 # FLAME's blis library (https://github.com/flame/blis)
217 check_fortran_libraries(
228 if (BLA_VENDOR STREQUAL "ATLAS" OR BLA_VENDOR STREQUAL "All")
229 if(NOT BLAS_LIBRARIES)
230 # BLAS in ATLAS library? (http://math-atlas.sourceforge.net/)
231 check_fortran_libraries(
242 # BLAS in PhiPACK libraries? (requires generic BLAS lib, too)
243 if (BLA_VENDOR STREQUAL "PhiPACK" OR BLA_VENDOR STREQUAL "All")
244 if(NOT BLAS_LIBRARIES)
245 check_fortran_libraries(
256 # BLAS in Alpha CXML library?
257 if (BLA_VENDOR STREQUAL "CXML" OR BLA_VENDOR STREQUAL "All")
258 if(NOT BLAS_LIBRARIES)
259 check_fortran_libraries(
270 # BLAS in Alpha DXML library? (now called CXML, see above)
271 if (BLA_VENDOR STREQUAL "DXML" OR BLA_VENDOR STREQUAL "All")
272 if(NOT BLAS_LIBRARIES)
273 check_fortran_libraries(
284 # BLAS in Sun Performance library?
285 if (BLA_VENDOR STREQUAL "SunPerf" OR BLA_VENDOR STREQUAL "All")
286 if(NOT BLAS_LIBRARIES)
287 check_fortran_libraries(
296 set(BLAS_LINKER_FLAGS "-xlic_lib=sunperf")
301 # BLAS in SCSL library? (SGI/Cray Scientific Library)
302 if (BLA_VENDOR STREQUAL "SCSL" OR BLA_VENDOR STREQUAL "All")
303 if(NOT BLAS_LIBRARIES)
304 check_fortran_libraries(
315 # BLAS in SGIMATH library?
316 if (BLA_VENDOR STREQUAL "SGIMATH" OR BLA_VENDOR STREQUAL "All")
317 if(NOT BLAS_LIBRARIES)
318 check_fortran_libraries(
329 # BLAS in IBM ESSL library? (requires generic BLAS lib, too)
330 if (BLA_VENDOR STREQUAL "IBMESSL" OR BLA_VENDOR STREQUAL "All")
331 if(NOT BLAS_LIBRARIES)
332 check_fortran_libraries(
343 #BLAS in acml library?
344 if (BLA_VENDOR MATCHES "ACML" OR BLA_VENDOR STREQUAL "All")
345 if( ((BLA_VENDOR STREQUAL "ACML") AND (NOT BLAS_ACML_LIB_DIRS)) OR
346 ((BLA_VENDOR STREQUAL "ACML_MP") AND (NOT BLAS_ACML_MP_LIB_DIRS)) OR
347 ((BLA_VENDOR STREQUAL "ACML_GPU") AND (NOT BLAS_ACML_GPU_LIB_DIRS))
349 # try to find acml in "standard" paths
351 file( GLOB _ACML_ROOT "C:/AMD/acml*/ACML-EULA.txt" )
353 file( GLOB _ACML_ROOT "/opt/acml*/ACML-EULA.txt" )
356 file( GLOB _ACML_GPU_ROOT "C:/AMD/acml*/GPGPUexamples" )
358 file( GLOB _ACML_GPU_ROOT "/opt/acml*/GPGPUexamples" )
360 list(GET _ACML_ROOT 0 _ACML_ROOT)
361 list(GET _ACML_GPU_ROOT 0 _ACML_GPU_ROOT)
363 get_filename_component( _ACML_ROOT ${_ACML_ROOT} PATH )
364 if( SIZEOF_INTEGER EQUAL 8 )
365 set( _ACML_PATH_SUFFIX "_int64" )
367 set( _ACML_PATH_SUFFIX "" )
369 if( CMAKE_Fortran_COMPILER_ID STREQUAL "Intel" )
370 set( _ACML_COMPILER32 "ifort32" )
371 set( _ACML_COMPILER64 "ifort64" )
372 elseif( CMAKE_Fortran_COMPILER_ID STREQUAL "SunPro" )
373 set( _ACML_COMPILER32 "sun32" )
374 set( _ACML_COMPILER64 "sun64" )
375 elseif( CMAKE_Fortran_COMPILER_ID STREQUAL "PGI" )
376 set( _ACML_COMPILER32 "pgi32" )
378 set( _ACML_COMPILER64 "win64" )
380 set( _ACML_COMPILER64 "pgi64" )
382 elseif( CMAKE_Fortran_COMPILER_ID STREQUAL "Open64" )
383 # 32 bit builds not supported on Open64 but for code simplicity
384 # We'll just use the same directory twice
385 set( _ACML_COMPILER32 "open64_64" )
386 set( _ACML_COMPILER64 "open64_64" )
387 elseif( CMAKE_Fortran_COMPILER_ID STREQUAL "NAG" )
388 set( _ACML_COMPILER32 "nag32" )
389 set( _ACML_COMPILER64 "nag64" )
391 set( _ACML_COMPILER32 "gfortran32" )
392 set( _ACML_COMPILER64 "gfortran64" )
395 if( BLA_VENDOR STREQUAL "ACML_MP" )
396 set(_ACML_MP_LIB_DIRS
397 "${_ACML_ROOT}/${_ACML_COMPILER32}_mp${_ACML_PATH_SUFFIX}/lib"
398 "${_ACML_ROOT}/${_ACML_COMPILER64}_mp${_ACML_PATH_SUFFIX}/lib" )
401 "${_ACML_ROOT}/${_ACML_COMPILER32}${_ACML_PATH_SUFFIX}/lib"
402 "${_ACML_ROOT}/${_ACML_COMPILER64}${_ACML_PATH_SUFFIX}/lib" )
405 elseif(BLAS_${BLA_VENDOR}_LIB_DIRS)
406 set(_${BLA_VENDOR}_LIB_DIRS ${BLAS_${BLA_VENDOR}_LIB_DIRS})
409 if( BLA_VENDOR STREQUAL "ACML_MP" )
410 foreach( BLAS_ACML_MP_LIB_DIRS ${_ACML_MP_LIB_DIRS})
411 check_fortran_libraries (
415 "" "acml_mp;acml_mv" "" ${BLAS_ACML_MP_LIB_DIRS}
421 elseif( BLA_VENDOR STREQUAL "ACML_GPU" )
422 foreach( BLAS_ACML_GPU_LIB_DIRS ${_ACML_GPU_LIB_DIRS})
423 check_fortran_libraries (
427 "" "acml;acml_mv;CALBLAS" "" ${BLAS_ACML_GPU_LIB_DIRS}
434 foreach( BLAS_ACML_LIB_DIRS ${_ACML_LIB_DIRS} )
435 check_fortran_libraries (
439 "" "acml;acml_mv" "" ${BLAS_ACML_LIB_DIRS}
447 # Either acml or acml_mp should be in LD_LIBRARY_PATH but not both
448 if(NOT BLAS_LIBRARIES)
449 check_fortran_libraries(
458 if(NOT BLAS_LIBRARIES)
459 check_fortran_libraries(
468 if(NOT BLAS_LIBRARIES)
469 check_fortran_libraries(
474 "acml;acml_mv;CALBLAS"
480 # Apple BLAS library?
481 if (BLA_VENDOR STREQUAL "Apple" OR BLA_VENDOR STREQUAL "All")
482 if(NOT BLAS_LIBRARIES)
483 check_fortran_libraries(
494 if (BLA_VENDOR STREQUAL "NAS" OR BLA_VENDOR STREQUAL "All")
495 if ( NOT BLAS_LIBRARIES )
496 check_fortran_libraries(
506 # Generic BLAS library?
507 if (BLA_VENDOR STREQUAL "Generic" OR BLA_VENDOR STREQUAL "All")
508 if(NOT BLAS_LIBRARIES)
509 check_fortran_libraries(
520 #BLAS in intel mkl 10 library? (em64t 64bit)
521 if (BLA_VENDOR MATCHES "Intel" OR BLA_VENDOR STREQUAL "All")
525 if (CMAKE_C_COMPILER_LOADED OR CMAKE_CXX_COMPILER_LOADED)
526 if(BLAS_FIND_QUIETLY OR NOT BLAS_FIND_REQUIRED)
527 find_package(Threads)
529 find_package(Threads REQUIRED)
532 set(BLAS_SEARCH_LIBS "")
535 set(BLAS_mkl_SEARCH_SYMBOL SGEMM)
536 set(_LIBRARIES BLAS95_LIBRARIES)
539 set(BLAS_mkl_DLL_SUFFIX "")
541 set(BLAS_mkl_DLL_SUFFIX "_dll")
544 # Find the main file (32-bit or 64-bit)
545 set(BLAS_SEARCH_LIBS_WIN_MAIN "")
546 if (BLA_VENDOR STREQUAL "Intel10_32" OR BLA_VENDOR STREQUAL "All")
547 list(APPEND BLAS_SEARCH_LIBS_WIN_MAIN
548 "mkl_blas95${BLAS_mkl_DLL_SUFFIX} mkl_intel_c${BLAS_mkl_DLL_SUFFIX}")
550 if (BLA_VENDOR MATCHES "^Intel10_64lp" OR BLA_VENDOR STREQUAL "All")
551 list(APPEND BLAS_SEARCH_LIBS_WIN_MAIN
552 "mkl_blas95_lp64${BLAS_mkl_DLL_SUFFIX} mkl_intel_lp64${BLAS_mkl_DLL_SUFFIX}")
555 # Add threading/sequential libs
556 set(BLAS_SEARCH_LIBS_WIN_THREAD "")
557 if (BLA_VENDOR MATCHES "_seq$" OR BLA_VENDOR STREQUAL "All")
558 list(APPEND BLAS_SEARCH_LIBS_WIN_THREAD
559 "mkl_sequential${BLAS_mkl_DLL_SUFFIX}")
561 if (NOT BLA_VENDOR MATCHES "_seq$" OR BLA_VENDOR STREQUAL "All")
563 list(APPEND BLAS_SEARCH_LIBS_WIN_THREAD
564 "libguide40 mkl_intel_thread${BLAS_mkl_DLL_SUFFIX}")
566 list(APPEND BLAS_SEARCH_LIBS_WIN_THREAD
567 "libiomp5md mkl_intel_thread${BLAS_mkl_DLL_SUFFIX}")
570 # Cartesian product of the above
571 foreach (MAIN ${BLAS_SEARCH_LIBS_WIN_MAIN})
572 foreach (THREAD ${BLAS_SEARCH_LIBS_WIN_THREAD})
573 list(APPEND BLAS_SEARCH_LIBS
574 "${MAIN} ${THREAD} mkl_core${BLAS_mkl_DLL_SUFFIX}")
578 if (BLA_VENDOR STREQUAL "Intel10_32" OR BLA_VENDOR STREQUAL "All")
579 list(APPEND BLAS_SEARCH_LIBS
580 "mkl_blas95 mkl_intel mkl_intel_thread mkl_core guide")
582 if (BLA_VENDOR STREQUAL "Intel10_64lp" OR BLA_VENDOR STREQUAL "All")
584 list(APPEND BLAS_SEARCH_LIBS
585 "mkl_blas95 mkl_intel_lp64 mkl_intel_thread mkl_core guide")
588 if (CMAKE_C_COMPILER MATCHES ".+gcc")
589 list(APPEND BLAS_SEARCH_LIBS
590 "mkl_blas95_lp64 mkl_intel_lp64 mkl_gnu_thread mkl_core gomp")
592 list(APPEND BLAS_SEARCH_LIBS
593 "mkl_blas95_lp64 mkl_intel_lp64 mkl_intel_thread mkl_core iomp5")
596 if (BLA_VENDOR STREQUAL "Intel10_64lp_seq" OR BLA_VENDOR STREQUAL "All")
597 list(APPEND BLAS_SEARCH_LIBS
598 "mkl_intel_lp64 mkl_sequential mkl_core")
602 set(BLAS_mkl_SEARCH_SYMBOL sgemm)
603 set(_LIBRARIES BLAS_LIBRARIES)
606 set(BLAS_mkl_DLL_SUFFIX "")
608 set(BLAS_mkl_DLL_SUFFIX "_dll")
611 # Find the main file (32-bit or 64-bit)
612 set(BLAS_SEARCH_LIBS_WIN_MAIN "")
613 if (BLA_VENDOR STREQUAL "Intel10_32" OR BLA_VENDOR STREQUAL "All")
614 list(APPEND BLAS_SEARCH_LIBS_WIN_MAIN
615 "mkl_intel_c${BLAS_mkl_DLL_SUFFIX}")
617 if (BLA_VENDOR MATCHES "^Intel10_64lp" OR BLA_VENDOR STREQUAL "All")
618 list(APPEND BLAS_SEARCH_LIBS_WIN_MAIN
619 "mkl_intel_lp64${BLAS_mkl_DLL_SUFFIX}")
622 # Add threading/sequential libs
623 set(BLAS_SEARCH_LIBS_WIN_THREAD "")
624 if (NOT BLA_VENDOR MATCHES "_seq$" OR BLA_VENDOR STREQUAL "All")
626 list(APPEND BLAS_SEARCH_LIBS_WIN_THREAD
627 "libguide40 mkl_intel_thread${BLAS_mkl_DLL_SUFFIX}")
629 list(APPEND BLAS_SEARCH_LIBS_WIN_THREAD
630 "libiomp5md mkl_intel_thread${BLAS_mkl_DLL_SUFFIX}")
632 if (BLA_VENDOR MATCHES "_seq$" OR BLA_VENDOR STREQUAL "All")
633 list(APPEND BLAS_SEARCH_LIBS_WIN_THREAD
634 "mkl_sequential${BLAS_mkl_DLL_SUFFIX}")
637 # Cartesian product of the above
638 foreach (MAIN ${BLAS_SEARCH_LIBS_WIN_MAIN})
639 foreach (THREAD ${BLAS_SEARCH_LIBS_WIN_THREAD})
640 list(APPEND BLAS_SEARCH_LIBS
641 "${MAIN} ${THREAD} mkl_core${BLAS_mkl_DLL_SUFFIX}")
645 if (BLA_VENDOR STREQUAL "Intel10_32" OR BLA_VENDOR STREQUAL "All")
646 list(APPEND BLAS_SEARCH_LIBS
647 "mkl_intel mkl_intel_thread mkl_core guide")
649 if (BLA_VENDOR STREQUAL "Intel10_64lp" OR BLA_VENDOR STREQUAL "All")
652 list(APPEND BLAS_SEARCH_LIBS
653 "mkl_intel_lp64 mkl_intel_thread mkl_core guide")
656 if (CMAKE_C_COMPILER MATCHES ".+gcc")
657 list(APPEND BLAS_SEARCH_LIBS
658 "mkl_intel_lp64 mkl_gnu_thread mkl_core gomp")
660 list(APPEND BLAS_SEARCH_LIBS
661 "mkl_intel_lp64 mkl_intel_thread mkl_core iomp5")
664 if (BLA_VENDOR STREQUAL "Intel10_64lp_seq" OR BLA_VENDOR STREQUAL "All")
665 list(APPEND BLAS_SEARCH_LIBS
666 "mkl_intel_lp64 mkl_sequential mkl_core")
669 #older vesions of intel mkl libs
670 if (BLA_VENDOR STREQUAL "Intel" OR BLA_VENDOR STREQUAL "All")
671 list(APPEND BLAS_SEARCH_LIBS
673 list(APPEND BLAS_SEARCH_LIBS
675 list(APPEND BLAS_SEARCH_LIBS
681 foreach (IT ${BLAS_SEARCH_LIBS})
682 string(REPLACE " " ";" SEARCH_LIBS ${IT})
683 if (NOT ${_LIBRARIES})
684 check_fortran_libraries(
687 ${BLAS_mkl_SEARCH_SYMBOL}
690 "${CMAKE_THREAD_LIBS_INIT};${LM}"
699 find_package_handle_standard_args(BLAS REQUIRED_VARS BLAS95_LIBRARIES)
700 set(BLAS95_FOUND ${BLAS_FOUND})
702 set(BLAS_LIBRARIES "${BLAS95_LIBRARIES}")
705 find_package_handle_standard_args(BLAS REQUIRED_VARS BLAS_LIBRARIES)
708 cmake_pop_check_state()
709 set(CMAKE_FIND_LIBRARY_SUFFIXES ${_blas_ORIG_CMAKE_FIND_LIBRARY_SUFFIXES})