xref: /aosp_15_r20/external/pytorch/cmake/Modules/FindLAPACK.cmake (revision da0073e96a02ea20f0ac840b70461e3646d07c45)
1# - Find LAPACK library
2# This module finds an installed fortran library that implements the LAPACK
3# linear-algebra interface (see http://www.netlib.org/lapack/).
4#
5# The approach follows that taken for the autoconf macro file, acx_lapack.m4
6# (distributed at http://ac-archive.sourceforge.net/ac-archive/acx_lapack.html).
7#
8# This module sets the following variables:
9#  LAPACK_FOUND - set to true if a library implementing the LAPACK interface is found
10#  LAPACK_LIBRARIES - list of libraries (using full path name) for LAPACK
11
12# Note: I do not think it is a good idea to mixup different BLAS/LAPACK versions
13# Hence, this script wants to find a Lapack library matching your Blas library
14
15# Do nothing if LAPACK was found before
16IF(NOT LAPACK_FOUND)
17
18SET(LAPACK_LIBRARIES)
19SET(LAPACK_INFO)
20
21IF(LAPACK_FIND_QUIETLY OR NOT LAPACK_FIND_REQUIRED)
22  FIND_PACKAGE(BLAS)
23ELSE(LAPACK_FIND_QUIETLY OR NOT LAPACK_FIND_REQUIRED)
24  FIND_PACKAGE(BLAS REQUIRED)
25ENDIF(LAPACK_FIND_QUIETLY OR NOT LAPACK_FIND_REQUIRED)
26
27# Old search lapack script
28include(CheckFortranFunctionExists)
29include(CheckFunctionExists)
30
31macro(Check_Lapack_Libraries LIBRARIES _prefix _name _flags _list _blas)
32  # This macro checks for the existence of the combination of fortran libraries
33  # given by _list.  If the combination is found, this macro checks (using the
34  # Check_Fortran_Function_Exists macro) whether can link against that library
35  # combination using the name of a routine given by _name using the linker
36  # flags given by _flags.  If the combination of libraries is found and passes
37  # the link test, LIBRARIES is set to the list of complete library paths that
38  # have been found.  Otherwise, LIBRARIES is set to FALSE.
39  # N.B. _prefix is the prefix applied to the names of all cached variables that
40  # are generated internally and marked advanced by this macro.
41  set(_libraries_work TRUE)
42  set(${LIBRARIES})
43  set(_combined_name)
44  foreach(_library ${_list})
45    set(_combined_name ${_combined_name}_${_library})
46    if(_libraries_work)
47      if (WIN32)
48        find_library(${_prefix}_${_library}_LIBRARY
49          NAMES ${_library} PATHS ENV LIB PATHS ENV PATH)
50      else (WIN32)
51        if(APPLE)
52          find_library(${_prefix}_${_library}_LIBRARY
53            NAMES ${_library}
54            PATHS /usr/local/lib /usr/lib /usr/local/lib64 /usr/lib64 /usr/lib/aarch64-linux-gnu
55            ENV DYLD_LIBRARY_PATH)
56        else(APPLE)
57          find_library(${_prefix}_${_library}_LIBRARY
58            NAMES ${_library}
59            PATHS /usr/local/lib /usr/lib /usr/local/lib64 /usr/lib64 /usr/lib/aarch64-linux-gnu
60            ENV LD_LIBRARY_PATH)
61        endif(APPLE)
62      endif(WIN32)
63      mark_as_advanced(${_prefix}_${_library}_LIBRARY)
64      set(${LIBRARIES} ${${LIBRARIES}} ${${_prefix}_${_library}_LIBRARY})
65      set(_libraries_work ${${_prefix}_${_library}_LIBRARY})
66    endif(_libraries_work)
67  endforeach(_library ${_list})
68  if(_libraries_work)
69    # Test this combination of libraries.
70    set(CMAKE_REQUIRED_LIBRARIES ${_flags} ${${LIBRARIES}} ${_blas})
71    if (CMAKE_Fortran_COMPILER_WORKS)
72      check_fortran_function_exists(${_name} ${_prefix}${_combined_name}_WORKS)
73    else (CMAKE_Fortran_COMPILER_WORKS)
74      check_function_exists("${_name}_" ${_prefix}${_combined_name}_WORKS)
75    endif (CMAKE_Fortran_COMPILER_WORKS)
76    set(CMAKE_REQUIRED_LIBRARIES)
77    mark_as_advanced(${_prefix}${_combined_name}_WORKS)
78    set(_libraries_work ${${_prefix}${_combined_name}_WORKS})
79  endif(_libraries_work)
80  if(NOT _libraries_work)
81    set(${LIBRARIES} FALSE)
82  endif(NOT _libraries_work)
83endmacro(Check_Lapack_Libraries)
84
85
86if(BLAS_FOUND)
87
88  # Intel MKL
89  IF((NOT LAPACK_INFO) AND (BLAS_INFO STREQUAL "mkl"))
90    IF(MKL_LAPACK_LIBRARIES)
91      SET(LAPACK_LIBRARIES ${MKL_LAPACK_LIBRARIES} ${MKL_LIBRARIES})
92    ELSE(MKL_LAPACK_LIBRARIES)
93      SET(LAPACK_LIBRARIES ${MKL_LIBRARIES})
94    ENDIF(MKL_LAPACK_LIBRARIES)
95    SET(LAPACK_INCLUDE_DIR ${MKL_INCLUDE_DIR})
96    SET(LAPACK_INFO "mkl")
97  ENDIF()
98
99  # NVPL
100  IF((NOT LAPACK_INFO) AND (BLAS_INFO STREQUAL "nvpl"))
101    FIND_PACKAGE(NVPL_LAPACK REQUIRED)
102    SET(LAPACK_LIBRARIES nvpl::lapack_lp64_omp)
103    SET(LAPACK_INFO "nvpl")
104  ENDIF()
105
106  # Accelerate
107  IF((NOT LAPACK_INFO) AND (BLAS_INFO STREQUAL "accelerate"))
108    SET(CMAKE_REQUIRED_LIBRARIES ${BLAS_LIBRARIES})
109    check_function_exists("cheev_" ACCELERATE_LAPACK_WORKS)
110    set(CMAKE_REQUIRED_LIBRARIES)
111    if(ACCELERATE_LAPACK_WORKS)
112      SET(LAPACK_INFO "accelerate")
113    else()
114      message(STATUS "Strangely, this Accelerate library does not support Lapack?!")
115    endif()
116  endif()
117
118  # vecLib
119  IF((NOT LAPACK_INFO) AND (BLAS_INFO STREQUAL "veclib"))
120    SET(CMAKE_REQUIRED_LIBRARIES ${BLAS_LIBRARIES})
121    check_function_exists("cheev_" VECLIB_LAPACK_WORKS)
122    set(CMAKE_REQUIRED_LIBRARIES)
123    if(VECLIB_LAPACK_WORKS)
124      SET(LAPACK_INFO "veclib")
125    else()
126      message(STATUS "Strangely, this vecLib library does not support Lapack?!")
127    endif()
128  endif()
129
130  # FlexiBLAS
131  IF((NOT LAPACK_INFO) AND (BLAS_INFO STREQUAL "flexi"))
132    SET(CMAKE_REQUIRED_LIBRARIES ${BLAS_LIBRARIES})
133    check_function_exists("cheev_" FLEXIBLAS_LAPACK_WORKS)
134    set(CMAKE_REQUIRED_LIBRARIES)
135    if(FLEXIBLAS_LAPACK_WORKS)
136      SET(LAPACK_INFO "flexi")
137    else()
138      message(STATUS "Strangely, this FlexiBLAS library does not support Lapack?!")
139    endif()
140  endif()
141
142  # OpenBlas
143  IF((NOT LAPACK_INFO) AND (BLAS_INFO STREQUAL "open"))
144    SET(CMAKE_REQUIRED_LIBRARIES ${BLAS_LIBRARIES})
145    check_function_exists("cheev_" OPEN_LAPACK_WORKS)
146    if(OPEN_LAPACK_WORKS)
147      check_function_exists("cgesdd_" LAPACK_CGESDD_WORKS)
148      if(NOT LAPACK_CGESDD_WORKS)
149        find_library(GFORTRAN_LIBRARY
150          NAMES libgfortran.a gfortran
151          PATHS ${CMAKE_C_IMPLICIT_LINK_DIRECTORIES})
152       list(APPEND CMAKE_REQUIRED_LIBRARIES "${GFORTRAN_LIBRARY}")
153       unset(LAPACK_CGESDD_WORKS CACHE)
154       check_function_exists("cgesdd_" LAPACK_CGESDD_WORKS)
155       if(LAPACK_CGESDD_WORKS)
156         list(APPEND LAPACK_LIBRARIES "${GFORTRAN_LIBRARY}")
157       else()
158         message(WARNING "OpenBlas has been compiled with Lapack support, but cgesdd can not be used")
159         set(OPEN_LAPACK_WORKS NO)
160       endif()
161      endif()
162    endif()
163
164    set(CMAKE_REQUIRED_LIBRARIES)
165    if(OPEN_LAPACK_WORKS)
166      SET(LAPACK_INFO "open")
167    else()
168      message(STATUS "It seems OpenBlas has not been compiled with Lapack support")
169    endif()
170  endif()
171
172  # GotoBlas
173  IF((NOT LAPACK_INFO) AND (BLAS_INFO STREQUAL "goto"))
174    SET(CMAKE_REQUIRED_LIBRARIES ${BLAS_LIBRARIES})
175    check_function_exists("cheev_" GOTO_LAPACK_WORKS)
176    set(CMAKE_REQUIRED_LIBRARIES)
177    if(GOTO_LAPACK_WORKS)
178      SET(LAPACK_INFO "goto")
179    else()
180      message(STATUS "It seems GotoBlas has not been compiled with Lapack support")
181    endif()
182  endif()
183
184  # FLAME
185  IF((NOT LAPACK_INFO) AND (BLAS_INFO STREQUAL "FLAME"))
186    check_lapack_libraries(
187      LAPACK_LIBRARIES
188      LAPACK
189      cheev
190      ""
191      "flame"
192      "${BLAS_LIBRARIES}"
193      )
194    if(LAPACK_LIBRARIES)
195      SET(LAPACK_INFO "FLAME")
196    endif(LAPACK_LIBRARIES)
197  endif()
198
199  # ACML
200  IF((NOT LAPACK_INFO) AND (BLAS_INFO STREQUAL "acml"))
201    SET(CMAKE_REQUIRED_LIBRARIES ${BLAS_LIBRARIES})
202    check_function_exists("cheev_" ACML_LAPACK_WORKS)
203    set(CMAKE_REQUIRED_LIBRARIES)
204    if(ACML_LAPACK_WORKS)
205      SET(LAPACK_INFO "acml")
206    else()
207      message(STATUS "Strangely, this ACML library does not support Lapack?!")
208    endif()
209  endif()
210
211  # Generic LAPACK library?
212  IF((NOT LAPACK_INFO) AND (BLAS_INFO STREQUAL "generic"))
213    check_lapack_libraries(
214      LAPACK_LIBRARIES
215      LAPACK
216      cheev
217      ""
218      "lapack"
219      "${BLAS_LIBRARIES}"
220      )
221    if(LAPACK_LIBRARIES)
222      SET(LAPACK_INFO "generic")
223    endif(LAPACK_LIBRARIES)
224  endif()
225
226else(BLAS_FOUND)
227  message(STATUS "LAPACK requires BLAS")
228endif(BLAS_FOUND)
229
230if(LAPACK_INFO)
231  set(LAPACK_FOUND TRUE)
232else(LAPACK_INFO)
233  set(LAPACK_FOUND FALSE)
234endif(LAPACK_INFO)
235
236IF (NOT LAPACK_FOUND AND LAPACK_FIND_REQUIRED)
237  message(FATAL_ERROR "Cannot find a library with LAPACK API. Please specify library location.")
238ENDIF (NOT LAPACK_FOUND AND LAPACK_FIND_REQUIRED)
239IF(NOT LAPACK_FIND_QUIETLY)
240  IF(LAPACK_FOUND)
241    MESSAGE(STATUS "Found a library with LAPACK API (${LAPACK_INFO}).")
242  ELSE(LAPACK_FOUND)
243    MESSAGE(STATUS "Cannot find a library with LAPACK API. Not using LAPACK.")
244  ENDIF(LAPACK_FOUND)
245ENDIF(NOT LAPACK_FIND_QUIETLY)
246
247# Do nothing if LAPACK was found before
248ENDIF(NOT LAPACK_FOUND)
249