xref: /aosp_15_r20/external/pytorch/cmake/Modules/FindBLAS.cmake (revision da0073e96a02ea20f0ac840b70461e3646d07c45)
1*da0073e9SAndroid Build Coastguard Worker# - Find BLAS library
2*da0073e9SAndroid Build Coastguard Worker# This module finds an installed fortran library that implements the BLAS
3*da0073e9SAndroid Build Coastguard Worker# linear-algebra interface (see http://www.netlib.org/blas/).
4*da0073e9SAndroid Build Coastguard Worker# The list of libraries searched for is taken
5*da0073e9SAndroid Build Coastguard Worker# from the autoconf macro file, acx_blas.m4 (distributed at
6*da0073e9SAndroid Build Coastguard Worker# http://ac-archive.sourceforge.net/ac-archive/acx_blas.html).
7*da0073e9SAndroid Build Coastguard Worker#
8*da0073e9SAndroid Build Coastguard Worker# This module sets the following variables:
9*da0073e9SAndroid Build Coastguard Worker#  BLAS_FOUND - set to true if a library implementing the BLAS interface is found.
10*da0073e9SAndroid Build Coastguard Worker#  BLAS_INFO - name of the detected BLAS library.
11*da0073e9SAndroid Build Coastguard Worker#  BLAS_F2C - set to true if following the f2c return convention
12*da0073e9SAndroid Build Coastguard Worker#  BLAS_LIBRARIES - list of libraries to link against to use BLAS
13*da0073e9SAndroid Build Coastguard Worker#  BLAS_INCLUDE_DIR - include directory
14*da0073e9SAndroid Build Coastguard Worker
15*da0073e9SAndroid Build Coastguard Worker# Do nothing if BLAS was found before
16*da0073e9SAndroid Build Coastguard WorkerIF(NOT BLAS_FOUND)
17*da0073e9SAndroid Build Coastguard Worker
18*da0073e9SAndroid Build Coastguard WorkerSET(BLAS_LIBRARIES)
19*da0073e9SAndroid Build Coastguard WorkerSET(BLAS_INCLUDE_DIR)
20*da0073e9SAndroid Build Coastguard WorkerSET(BLAS_INFO)
21*da0073e9SAndroid Build Coastguard WorkerSET(BLAS_F2C)
22*da0073e9SAndroid Build Coastguard Worker
23*da0073e9SAndroid Build Coastguard WorkerSET(WITH_BLAS "" CACHE STRING "Blas type [accelerate/acml/atlas/blis/generic/goto/mkl/open/veclib]")
24*da0073e9SAndroid Build Coastguard Worker
25*da0073e9SAndroid Build Coastguard Worker# Old FindBlas
26*da0073e9SAndroid Build Coastguard WorkerINCLUDE(CheckCSourceRuns)
27*da0073e9SAndroid Build Coastguard WorkerINCLUDE(CheckFortranFunctionExists)
28*da0073e9SAndroid Build Coastguard Worker
29*da0073e9SAndroid Build Coastguard WorkerMACRO(Check_Fortran_Libraries LIBRARIES _prefix _name _flags _list)
30*da0073e9SAndroid Build Coastguard Worker  # This macro checks for the existence of the combination of fortran libraries
31*da0073e9SAndroid Build Coastguard Worker  # given by _list.  If the combination is found, this macro checks (using the
32*da0073e9SAndroid Build Coastguard Worker  # Check_Fortran_Function_Exists macro) whether can link against that library
33*da0073e9SAndroid Build Coastguard Worker  # combination using the name of a routine given by _name using the linker
34*da0073e9SAndroid Build Coastguard Worker  # flags given by _flags.  If the combination of libraries is found and passes
35*da0073e9SAndroid Build Coastguard Worker  # the link test, LIBRARIES is set to the list of complete library paths that
36*da0073e9SAndroid Build Coastguard Worker  # have been found.  Otherwise, LIBRARIES is set to NOTFOUND.
37*da0073e9SAndroid Build Coastguard Worker  # N.B. _prefix is the prefix applied to the names of all cached variables that
38*da0073e9SAndroid Build Coastguard Worker  # are generated internally and marked advanced by this macro.
39*da0073e9SAndroid Build Coastguard Worker
40*da0073e9SAndroid Build Coastguard Worker  set(__list)
41*da0073e9SAndroid Build Coastguard Worker  foreach(_elem ${_list})
42*da0073e9SAndroid Build Coastguard Worker    if(__list)
43*da0073e9SAndroid Build Coastguard Worker      set(__list "${__list} - ${_elem}")
44*da0073e9SAndroid Build Coastguard Worker    else(__list)
45*da0073e9SAndroid Build Coastguard Worker      set(__list "${_elem}")
46*da0073e9SAndroid Build Coastguard Worker    endif(__list)
47*da0073e9SAndroid Build Coastguard Worker  endforeach(_elem)
48*da0073e9SAndroid Build Coastguard Worker  message(STATUS "Checking for [${__list}]")
49*da0073e9SAndroid Build Coastguard Worker
50*da0073e9SAndroid Build Coastguard Worker  set(_libraries_work TRUE)
51*da0073e9SAndroid Build Coastguard Worker  set(${LIBRARIES})
52*da0073e9SAndroid Build Coastguard Worker  set(_combined_name)
53*da0073e9SAndroid Build Coastguard Worker  foreach(_library ${_list})
54*da0073e9SAndroid Build Coastguard Worker    set(_combined_name ${_combined_name}_${_library})
55*da0073e9SAndroid Build Coastguard Worker    if(_libraries_work)
56*da0073e9SAndroid Build Coastguard Worker      if ( WIN32 )
57*da0073e9SAndroid Build Coastguard Worker        find_library(${_prefix}_${_library}_LIBRARY
58*da0073e9SAndroid Build Coastguard Worker          NAMES ${_library}
59*da0073e9SAndroid Build Coastguard Worker          PATHS ENV LIB
60*da0073e9SAndroid Build Coastguard Worker          PATHS ENV PATH )
61*da0073e9SAndroid Build Coastguard Worker      endif ( WIN32 )
62*da0073e9SAndroid Build Coastguard Worker      if ( APPLE )
63*da0073e9SAndroid Build Coastguard Worker        find_library(${_prefix}_${_library}_LIBRARY
64*da0073e9SAndroid Build Coastguard Worker          NAMES ${_library}
65*da0073e9SAndroid Build Coastguard Worker          PATHS /usr/local/lib /usr/lib /usr/local/lib64 /usr/lib64 /opt/OpenBLAS/lib /usr/lib/aarch64-linux-gnu
66*da0073e9SAndroid Build Coastguard Worker          ENV DYLD_LIBRARY_PATH )
67*da0073e9SAndroid Build Coastguard Worker      else ( APPLE )
68*da0073e9SAndroid Build Coastguard Worker        find_library(${_prefix}_${_library}_LIBRARY
69*da0073e9SAndroid Build Coastguard Worker          NAMES ${_library}
70*da0073e9SAndroid Build Coastguard Worker          PATHS /usr/local/lib /usr/lib /usr/local/lib64 /usr/lib64 /opt/OpenBLAS/lib /usr/lib/aarch64-linux-gnu ${CMAKE_C_IMPLICIT_LINK_DIRECTORIES}
71*da0073e9SAndroid Build Coastguard Worker          ENV LD_LIBRARY_PATH )
72*da0073e9SAndroid Build Coastguard Worker      endif( APPLE )
73*da0073e9SAndroid Build Coastguard Worker      mark_as_advanced(${_prefix}_${_library}_LIBRARY)
74*da0073e9SAndroid Build Coastguard Worker      set(${LIBRARIES} ${${LIBRARIES}} ${${_prefix}_${_library}_LIBRARY})
75*da0073e9SAndroid Build Coastguard Worker      set(_libraries_work ${${_prefix}_${_library}_LIBRARY})
76*da0073e9SAndroid Build Coastguard Worker      MESSAGE(STATUS "  Library ${_library}: ${${_prefix}_${_library}_LIBRARY}")
77*da0073e9SAndroid Build Coastguard Worker    endif(_libraries_work)
78*da0073e9SAndroid Build Coastguard Worker  endforeach(_library ${_list})
79*da0073e9SAndroid Build Coastguard Worker  if(_libraries_work)
80*da0073e9SAndroid Build Coastguard Worker    # Test this combination of libraries.
81*da0073e9SAndroid Build Coastguard Worker    set(CMAKE_REQUIRED_LIBRARIES ${_flags} ${${LIBRARIES}})
82*da0073e9SAndroid Build Coastguard Worker    if (CMAKE_Fortran_COMPILER_WORKS)
83*da0073e9SAndroid Build Coastguard Worker      check_fortran_function_exists(${_name} ${_prefix}${_combined_name}_WORKS)
84*da0073e9SAndroid Build Coastguard Worker    else (CMAKE_Fortran_COMPILER_WORKS)
85*da0073e9SAndroid Build Coastguard Worker      check_function_exists("${_name}_" ${_prefix}${_combined_name}_WORKS)
86*da0073e9SAndroid Build Coastguard Worker    endif(CMAKE_Fortran_COMPILER_WORKS)
87*da0073e9SAndroid Build Coastguard Worker    set(CMAKE_REQUIRED_LIBRARIES)
88*da0073e9SAndroid Build Coastguard Worker    mark_as_advanced(${_prefix}${_combined_name}_WORKS)
89*da0073e9SAndroid Build Coastguard Worker    set(_libraries_work ${${_prefix}${_combined_name}_WORKS})
90*da0073e9SAndroid Build Coastguard Worker  endif(_libraries_work)
91*da0073e9SAndroid Build Coastguard Worker  if(NOT _libraries_work)
92*da0073e9SAndroid Build Coastguard Worker    set(${LIBRARIES} NOTFOUND)
93*da0073e9SAndroid Build Coastguard Worker  endif(NOT _libraries_work)
94*da0073e9SAndroid Build Coastguard Workerendmacro(Check_Fortran_Libraries)
95*da0073e9SAndroid Build Coastguard Worker
96*da0073e9SAndroid Build Coastguard Worker# Intel MKL?
97*da0073e9SAndroid Build Coastguard Workerif((NOT BLAS_LIBRARIES)
98*da0073e9SAndroid Build Coastguard Worker    AND ((NOT WITH_BLAS) OR (WITH_BLAS STREQUAL "mkl")))
99*da0073e9SAndroid Build Coastguard Worker  FIND_PACKAGE(MKL)
100*da0073e9SAndroid Build Coastguard Worker  IF(MKL_FOUND)
101*da0073e9SAndroid Build Coastguard Worker    SET(BLAS_INFO "mkl")
102*da0073e9SAndroid Build Coastguard Worker    SET(BLAS_LIBRARIES ${MKL_LIBRARIES})
103*da0073e9SAndroid Build Coastguard Worker    SET(BLAS_INCLUDE_DIR ${MKL_INCLUDE_DIR})
104*da0073e9SAndroid Build Coastguard Worker    SET(BLAS_VERSION ${MKL_VERSION})
105*da0073e9SAndroid Build Coastguard Worker  ENDIF(MKL_FOUND)
106*da0073e9SAndroid Build Coastguard Workerendif()
107*da0073e9SAndroid Build Coastguard Worker
108*da0073e9SAndroid Build Coastguard Worker#BLIS?
109*da0073e9SAndroid Build Coastguard Workerif((NOT BLAS_LIBRARIES)
110*da0073e9SAndroid Build Coastguard Worker    AND ((NOT WITH_BLAS) OR (WITH_BLAS STREQUAL "blis")))
111*da0073e9SAndroid Build Coastguard Worker  check_fortran_libraries(
112*da0073e9SAndroid Build Coastguard Worker  BLAS_LIBRARIES
113*da0073e9SAndroid Build Coastguard Worker  BLAS
114*da0073e9SAndroid Build Coastguard Worker  sgemm
115*da0073e9SAndroid Build Coastguard Worker  ""
116*da0073e9SAndroid Build Coastguard Worker  "blis")
117*da0073e9SAndroid Build Coastguard Worker  if(BLAS_LIBRARIES)
118*da0073e9SAndroid Build Coastguard Worker    set(BLAS_INFO "blis")
119*da0073e9SAndroid Build Coastguard Worker  endif(BLAS_LIBRARIES)
120*da0073e9SAndroid Build Coastguard Workerendif()
121*da0073e9SAndroid Build Coastguard Worker
122*da0073e9SAndroid Build Coastguard Worker# Apple BLAS library?
123*da0073e9SAndroid Build Coastguard Workerif((NOT BLAS_LIBRARIES)
124*da0073e9SAndroid Build Coastguard Worker    AND ((NOT WITH_BLAS) OR (WITH_BLAS STREQUAL "accelerate")))
125*da0073e9SAndroid Build Coastguard Worker  check_fortran_libraries(
126*da0073e9SAndroid Build Coastguard Worker  BLAS_LIBRARIES
127*da0073e9SAndroid Build Coastguard Worker  BLAS
128*da0073e9SAndroid Build Coastguard Worker  sgemm
129*da0073e9SAndroid Build Coastguard Worker  ""
130*da0073e9SAndroid Build Coastguard Worker  "Accelerate")
131*da0073e9SAndroid Build Coastguard Worker  if (BLAS_LIBRARIES)
132*da0073e9SAndroid Build Coastguard Worker    set(BLAS_INFO "accelerate")
133*da0073e9SAndroid Build Coastguard Worker    set(BLAS_IS_ACCELERATE 1)
134*da0073e9SAndroid Build Coastguard Worker  endif(BLAS_LIBRARIES)
135*da0073e9SAndroid Build Coastguard Workerendif()
136*da0073e9SAndroid Build Coastguard Worker
137*da0073e9SAndroid Build Coastguard Workerif((NOT BLAS_LIBRARIES)
138*da0073e9SAndroid Build Coastguard Worker    AND ((NOT WITH_BLAS) OR (WITH_BLAS STREQUAL "veclib")))
139*da0073e9SAndroid Build Coastguard Worker  FIND_PACKAGE(vecLib)
140*da0073e9SAndroid Build Coastguard Worker  if(vecLib_FOUND)
141*da0073e9SAndroid Build Coastguard Worker    SET(BLAS_INFO "veclib")
142*da0073e9SAndroid Build Coastguard Worker  else()
143*da0073e9SAndroid Build Coastguard Worker    check_fortran_libraries(
144*da0073e9SAndroid Build Coastguard Worker      BLAS_LIBRARIES
145*da0073e9SAndroid Build Coastguard Worker      BLAS
146*da0073e9SAndroid Build Coastguard Worker      sgemm
147*da0073e9SAndroid Build Coastguard Worker      ""
148*da0073e9SAndroid Build Coastguard Worker      "vecLib")
149*da0073e9SAndroid Build Coastguard Worker    if (BLAS_LIBRARIES)
150*da0073e9SAndroid Build Coastguard Worker      set(BLAS_INFO "veclib")
151*da0073e9SAndroid Build Coastguard Worker    endif(BLAS_LIBRARIES)
152*da0073e9SAndroid Build Coastguard Worker  endif()
153*da0073e9SAndroid Build Coastguard Workerendif()
154*da0073e9SAndroid Build Coastguard Worker
155*da0073e9SAndroid Build Coastguard Workerif((NOT BLAS_LIBRARIES)
156*da0073e9SAndroid Build Coastguard Worker    AND ((NOT WITH_BLAS) OR (WITH_BLAS STREQUAL "flexi")))
157*da0073e9SAndroid Build Coastguard Worker  check_fortran_libraries(
158*da0073e9SAndroid Build Coastguard Worker  BLAS_LIBRARIES
159*da0073e9SAndroid Build Coastguard Worker  BLAS
160*da0073e9SAndroid Build Coastguard Worker  sgemm
161*da0073e9SAndroid Build Coastguard Worker  ""
162*da0073e9SAndroid Build Coastguard Worker  "flexiblas")
163*da0073e9SAndroid Build Coastguard Worker  if(BLAS_LIBRARIES)
164*da0073e9SAndroid Build Coastguard Worker    set(BLAS_INFO "flexi")
165*da0073e9SAndroid Build Coastguard Worker  endif(BLAS_LIBRARIES)
166*da0073e9SAndroid Build Coastguard Workerendif()
167*da0073e9SAndroid Build Coastguard Worker
168*da0073e9SAndroid Build Coastguard Workerif((NOT BLAS_LIBRARIES)
169*da0073e9SAndroid Build Coastguard Worker    AND ((NOT WITH_BLAS) OR (WITH_BLAS STREQUAL "open")))
170*da0073e9SAndroid Build Coastguard Worker  check_fortran_libraries(
171*da0073e9SAndroid Build Coastguard Worker  BLAS_LIBRARIES
172*da0073e9SAndroid Build Coastguard Worker  BLAS
173*da0073e9SAndroid Build Coastguard Worker  sgemm
174*da0073e9SAndroid Build Coastguard Worker  ""
175*da0073e9SAndroid Build Coastguard Worker  "openblas")
176*da0073e9SAndroid Build Coastguard Worker  if(BLAS_LIBRARIES)
177*da0073e9SAndroid Build Coastguard Worker    set(BLAS_INFO "open")
178*da0073e9SAndroid Build Coastguard Worker  endif(BLAS_LIBRARIES)
179*da0073e9SAndroid Build Coastguard Workerendif()
180*da0073e9SAndroid Build Coastguard Worker
181*da0073e9SAndroid Build Coastguard Workerif((NOT BLAS_LIBRARIES)
182*da0073e9SAndroid Build Coastguard Worker    AND ((NOT WITH_BLAS) OR (WITH_BLAS STREQUAL "open")))
183*da0073e9SAndroid Build Coastguard Worker  check_fortran_libraries(
184*da0073e9SAndroid Build Coastguard Worker  BLAS_LIBRARIES
185*da0073e9SAndroid Build Coastguard Worker  BLAS
186*da0073e9SAndroid Build Coastguard Worker  sgemm
187*da0073e9SAndroid Build Coastguard Worker  ""
188*da0073e9SAndroid Build Coastguard Worker  "openblas;pthread;m")
189*da0073e9SAndroid Build Coastguard Worker  if(BLAS_LIBRARIES)
190*da0073e9SAndroid Build Coastguard Worker    set(BLAS_INFO "open")
191*da0073e9SAndroid Build Coastguard Worker  endif(BLAS_LIBRARIES)
192*da0073e9SAndroid Build Coastguard Workerendif()
193*da0073e9SAndroid Build Coastguard Worker
194*da0073e9SAndroid Build Coastguard Workerif((NOT BLAS_LIBRARIES)
195*da0073e9SAndroid Build Coastguard Worker    AND ((NOT WITH_BLAS) OR (WITH_BLAS STREQUAL "open")))
196*da0073e9SAndroid Build Coastguard Worker  check_fortran_libraries(
197*da0073e9SAndroid Build Coastguard Worker  BLAS_LIBRARIES
198*da0073e9SAndroid Build Coastguard Worker  BLAS
199*da0073e9SAndroid Build Coastguard Worker  sgemm
200*da0073e9SAndroid Build Coastguard Worker  ""
201*da0073e9SAndroid Build Coastguard Worker  "openblas;pthread;m;gomp")
202*da0073e9SAndroid Build Coastguard Worker  if(BLAS_LIBRARIES)
203*da0073e9SAndroid Build Coastguard Worker    set(BLAS_INFO "open")
204*da0073e9SAndroid Build Coastguard Worker  endif(BLAS_LIBRARIES)
205*da0073e9SAndroid Build Coastguard Workerendif()
206*da0073e9SAndroid Build Coastguard Worker
207*da0073e9SAndroid Build Coastguard Workerif((NOT BLAS_LIBRARIES) AND (WIN32)
208*da0073e9SAndroid Build Coastguard Worker    AND ((NOT WITH_BLAS) OR (WITH_BLAS STREQUAL "open")))
209*da0073e9SAndroid Build Coastguard Worker  check_fortran_libraries(
210*da0073e9SAndroid Build Coastguard Worker  BLAS_LIBRARIES
211*da0073e9SAndroid Build Coastguard Worker  BLAS
212*da0073e9SAndroid Build Coastguard Worker  sgemm
213*da0073e9SAndroid Build Coastguard Worker  ""
214*da0073e9SAndroid Build Coastguard Worker  "libopenblas")
215*da0073e9SAndroid Build Coastguard Worker  if(BLAS_LIBRARIES)
216*da0073e9SAndroid Build Coastguard Worker    set(BLAS_INFO "open")
217*da0073e9SAndroid Build Coastguard Worker  endif(BLAS_LIBRARIES)
218*da0073e9SAndroid Build Coastguard Workerendif()
219*da0073e9SAndroid Build Coastguard Worker
220*da0073e9SAndroid Build Coastguard Workerif((NOT BLAS_LIBRARIES)
221*da0073e9SAndroid Build Coastguard Worker    AND ((NOT WITH_BLAS) OR (WITH_BLAS STREQUAL "goto")))
222*da0073e9SAndroid Build Coastguard Worker  check_fortran_libraries(
223*da0073e9SAndroid Build Coastguard Worker  BLAS_LIBRARIES
224*da0073e9SAndroid Build Coastguard Worker  BLAS
225*da0073e9SAndroid Build Coastguard Worker  sgemm
226*da0073e9SAndroid Build Coastguard Worker  ""
227*da0073e9SAndroid Build Coastguard Worker  "goto2;gfortran")
228*da0073e9SAndroid Build Coastguard Worker  if (BLAS_LIBRARIES)
229*da0073e9SAndroid Build Coastguard Worker    set(BLAS_INFO "goto")
230*da0073e9SAndroid Build Coastguard Worker  endif(BLAS_LIBRARIES)
231*da0073e9SAndroid Build Coastguard Workerendif()
232*da0073e9SAndroid Build Coastguard Worker
233*da0073e9SAndroid Build Coastguard Workerif((NOT BLAS_LIBRARIES)
234*da0073e9SAndroid Build Coastguard Worker    AND ((NOT WITH_BLAS) OR (WITH_BLAS STREQUAL "goto")))
235*da0073e9SAndroid Build Coastguard Worker  check_fortran_libraries(
236*da0073e9SAndroid Build Coastguard Worker  BLAS_LIBRARIES
237*da0073e9SAndroid Build Coastguard Worker  BLAS
238*da0073e9SAndroid Build Coastguard Worker  sgemm
239*da0073e9SAndroid Build Coastguard Worker  ""
240*da0073e9SAndroid Build Coastguard Worker  "goto2;gfortran;pthread")
241*da0073e9SAndroid Build Coastguard Worker  if (BLAS_LIBRARIES)
242*da0073e9SAndroid Build Coastguard Worker    set(BLAS_INFO "goto")
243*da0073e9SAndroid Build Coastguard Worker  endif(BLAS_LIBRARIES)
244*da0073e9SAndroid Build Coastguard Workerendif()
245*da0073e9SAndroid Build Coastguard Worker
246*da0073e9SAndroid Build Coastguard Workerif((NOT BLAS_LIBRARIES)
247*da0073e9SAndroid Build Coastguard Worker    AND ((NOT WITH_BLAS) OR (WITH_BLAS STREQUAL "acml")))
248*da0073e9SAndroid Build Coastguard Worker  check_fortran_libraries(
249*da0073e9SAndroid Build Coastguard Worker  BLAS_LIBRARIES
250*da0073e9SAndroid Build Coastguard Worker  BLAS
251*da0073e9SAndroid Build Coastguard Worker  sgemm
252*da0073e9SAndroid Build Coastguard Worker  ""
253*da0073e9SAndroid Build Coastguard Worker  "acml;gfortran")
254*da0073e9SAndroid Build Coastguard Worker  if (BLAS_LIBRARIES)
255*da0073e9SAndroid Build Coastguard Worker    set(BLAS_INFO "acml")
256*da0073e9SAndroid Build Coastguard Worker  endif(BLAS_LIBRARIES)
257*da0073e9SAndroid Build Coastguard Workerendif()
258*da0073e9SAndroid Build Coastguard Worker
259*da0073e9SAndroid Build Coastguard Workerif((NOT BLAS_LIBRARIES)
260*da0073e9SAndroid Build Coastguard Worker    AND ((NOT WITH_BLAS) OR (WITH_BLAS STREQUAL "FLAME")))
261*da0073e9SAndroid Build Coastguard Worker  # FLAME's blis library (https://github.com/flame/blis)
262*da0073e9SAndroid Build Coastguard Worker  check_fortran_libraries(
263*da0073e9SAndroid Build Coastguard Worker  BLAS_LIBRARIES
264*da0073e9SAndroid Build Coastguard Worker  BLAS
265*da0073e9SAndroid Build Coastguard Worker  sgemm
266*da0073e9SAndroid Build Coastguard Worker  ""
267*da0073e9SAndroid Build Coastguard Worker  "blis")
268*da0073e9SAndroid Build Coastguard Worker  if (BLAS_LIBRARIES)
269*da0073e9SAndroid Build Coastguard Worker    set(BLAS_INFO "FLAME")
270*da0073e9SAndroid Build Coastguard Worker  endif(BLAS_LIBRARIES)
271*da0073e9SAndroid Build Coastguard Workerendif()
272*da0073e9SAndroid Build Coastguard Worker
273*da0073e9SAndroid Build Coastguard Worker# BLAS in ATLAS library? (http://math-atlas.sourceforge.net/)
274*da0073e9SAndroid Build Coastguard Workerif((NOT BLAS_LIBRARIES)
275*da0073e9SAndroid Build Coastguard Worker    AND ((NOT WITH_BLAS) OR (WITH_BLAS STREQUAL "atlas")))
276*da0073e9SAndroid Build Coastguard Worker  FIND_PACKAGE(Atlas)
277*da0073e9SAndroid Build Coastguard Worker  if(Atlas_FOUND)
278*da0073e9SAndroid Build Coastguard Worker    SET(BLAS_INFO "atlas")
279*da0073e9SAndroid Build Coastguard Worker  else()
280*da0073e9SAndroid Build Coastguard Worker    check_fortran_libraries(
281*da0073e9SAndroid Build Coastguard Worker      BLAS_LIBRARIES
282*da0073e9SAndroid Build Coastguard Worker      BLAS
283*da0073e9SAndroid Build Coastguard Worker      sgemm
284*da0073e9SAndroid Build Coastguard Worker      ""
285*da0073e9SAndroid Build Coastguard Worker      "ptf77blas;atlas;gfortran")
286*da0073e9SAndroid Build Coastguard Worker    if (BLAS_LIBRARIES)
287*da0073e9SAndroid Build Coastguard Worker      set(BLAS_INFO "atlas")
288*da0073e9SAndroid Build Coastguard Worker    endif(BLAS_LIBRARIES)
289*da0073e9SAndroid Build Coastguard Worker  endif()
290*da0073e9SAndroid Build Coastguard Workerendif()
291*da0073e9SAndroid Build Coastguard Worker
292*da0073e9SAndroid Build Coastguard Worker# Generic BLAS library?
293*da0073e9SAndroid Build Coastguard Workerif((NOT BLAS_LIBRARIES)
294*da0073e9SAndroid Build Coastguard Worker    AND ((NOT WITH_BLAS) OR (WITH_BLAS STREQUAL "generic")))
295*da0073e9SAndroid Build Coastguard Worker  if(ENV{GENERIC_BLAS_LIBRARIES} STREQUAL "")
296*da0073e9SAndroid Build Coastguard Worker    set(GENERIC_BLAS "blas")
297*da0073e9SAndroid Build Coastguard Worker  else()
298*da0073e9SAndroid Build Coastguard Worker    set(GENERIC_BLAS $ENV{GENERIC_BLAS_LIBRARIES})
299*da0073e9SAndroid Build Coastguard Worker  endif()
300*da0073e9SAndroid Build Coastguard Worker  check_fortran_libraries(
301*da0073e9SAndroid Build Coastguard Worker  BLAS_LIBRARIES
302*da0073e9SAndroid Build Coastguard Worker  BLAS
303*da0073e9SAndroid Build Coastguard Worker  sgemm
304*da0073e9SAndroid Build Coastguard Worker  ""
305*da0073e9SAndroid Build Coastguard Worker  "${GENERIC_BLAS}")
306*da0073e9SAndroid Build Coastguard Worker  if (BLAS_LIBRARIES)
307*da0073e9SAndroid Build Coastguard Worker    set(BLAS_INFO "generic")
308*da0073e9SAndroid Build Coastguard Worker  endif(BLAS_LIBRARIES)
309*da0073e9SAndroid Build Coastguard Workerendif()
310*da0073e9SAndroid Build Coastguard Worker
311*da0073e9SAndroid Build Coastguard Worker# Determine if blas was compiled with the f2c conventions
312*da0073e9SAndroid Build Coastguard WorkerIF (BLAS_LIBRARIES)
313*da0073e9SAndroid Build Coastguard Worker   # Push host architecture when cross-compiling otherwise check would fail
314*da0073e9SAndroid Build Coastguard Worker   # when cross-compiling for arm64 on x86_64
315*da0073e9SAndroid Build Coastguard Worker   cmake_push_check_state(RESET)
316*da0073e9SAndroid Build Coastguard Worker  if(CMAKE_SYSTEM_NAME STREQUAL "Darwin" AND CMAKE_OSX_ARCHITECTURES MATCHES "^(x86_64|arm64)$")
317*da0073e9SAndroid Build Coastguard Worker    list(APPEND CMAKE_REQUIRED_FLAGS "-arch ${CMAKE_HOST_SYSTEM_PROCESSOR}")
318*da0073e9SAndroid Build Coastguard Worker  endif()
319*da0073e9SAndroid Build Coastguard Worker
320*da0073e9SAndroid Build Coastguard Worker# Set values through env variables if cross compiling
321*da0073e9SAndroid Build Coastguard Worker  IF (CMAKE_CROSSCOMPILING)
322*da0073e9SAndroid Build Coastguard Worker    IF("$ENV{PYTORCH_BLAS_F2C}" STREQUAL "ON")
323*da0073e9SAndroid Build Coastguard Worker      SET(BLAS_F2C TRUE)
324*da0073e9SAndroid Build Coastguard Worker    ELSE()
325*da0073e9SAndroid Build Coastguard Worker      SET(BLAS_F2C FALSE)
326*da0073e9SAndroid Build Coastguard Worker    ENDIF()
327*da0073e9SAndroid Build Coastguard Worker
328*da0073e9SAndroid Build Coastguard Worker    IF("$ENV{PYTORCH_BLAS_USE_CBLAS_DOT}" STREQUAL "ON")
329*da0073e9SAndroid Build Coastguard Worker      SET(BLAS_USE_CBLAS_DOT TRUE)
330*da0073e9SAndroid Build Coastguard Worker    ELSE()
331*da0073e9SAndroid Build Coastguard Worker      SET(BLAS_USE_CBLAS_DOT FALSE)
332*da0073e9SAndroid Build Coastguard Worker    ENDIF()
333*da0073e9SAndroid Build Coastguard Worker  ELSE ()
334*da0073e9SAndroid Build Coastguard Worker    SET(CMAKE_REQUIRED_LIBRARIES ${BLAS_LIBRARIES})
335*da0073e9SAndroid Build Coastguard Worker    CHECK_C_SOURCE_RUNS("
336*da0073e9SAndroid Build Coastguard Worker  #include <stdlib.h>
337*da0073e9SAndroid Build Coastguard Worker  #include <stdio.h>
338*da0073e9SAndroid Build Coastguard Worker  float x[4] = { 1, 2, 3, 4 };
339*da0073e9SAndroid Build Coastguard Worker  float y[4] = { .1, .01, .001, .0001 };
340*da0073e9SAndroid Build Coastguard Worker  int four = 4;
341*da0073e9SAndroid Build Coastguard Worker  int one = 1;
342*da0073e9SAndroid Build Coastguard Worker  extern double sdot_();
343*da0073e9SAndroid Build Coastguard Worker  int main() {
344*da0073e9SAndroid Build Coastguard Worker    int i;
345*da0073e9SAndroid Build Coastguard Worker    double r = sdot_(&four, x, &one, y, &one);
346*da0073e9SAndroid Build Coastguard Worker    exit((float)r != (float).1234);
347*da0073e9SAndroid Build Coastguard Worker  }" BLAS_F2C_DOUBLE_WORKS )
348*da0073e9SAndroid Build Coastguard Worker    CHECK_C_SOURCE_RUNS("
349*da0073e9SAndroid Build Coastguard Worker  #include <stdlib.h>
350*da0073e9SAndroid Build Coastguard Worker  #include <stdio.h>
351*da0073e9SAndroid Build Coastguard Worker  float x[4] = { 1, 2, 3, 4 };
352*da0073e9SAndroid Build Coastguard Worker  float y[4] = { .1, .01, .001, .0001 };
353*da0073e9SAndroid Build Coastguard Worker  int four = 4;
354*da0073e9SAndroid Build Coastguard Worker  int one = 1;
355*da0073e9SAndroid Build Coastguard Worker  extern float sdot_();
356*da0073e9SAndroid Build Coastguard Worker  int main() {
357*da0073e9SAndroid Build Coastguard Worker    int i;
358*da0073e9SAndroid Build Coastguard Worker    double r = sdot_(&four, x, &one, y, &one);
359*da0073e9SAndroid Build Coastguard Worker    exit((float)r != (float).1234);
360*da0073e9SAndroid Build Coastguard Worker  }" BLAS_F2C_FLOAT_WORKS )
361*da0073e9SAndroid Build Coastguard Worker    IF (BLAS_F2C_DOUBLE_WORKS AND NOT BLAS_F2C_FLOAT_WORKS)
362*da0073e9SAndroid Build Coastguard Worker      MESSAGE(STATUS "This BLAS uses the F2C return conventions")
363*da0073e9SAndroid Build Coastguard Worker      SET(BLAS_F2C TRUE)
364*da0073e9SAndroid Build Coastguard Worker    ELSE (BLAS_F2C_DOUBLE_WORKS AND NOT BLAS_F2C_FLOAT_WORKS)
365*da0073e9SAndroid Build Coastguard Worker      SET(BLAS_F2C FALSE)
366*da0073e9SAndroid Build Coastguard Worker    ENDIF(BLAS_F2C_DOUBLE_WORKS AND NOT BLAS_F2C_FLOAT_WORKS)
367*da0073e9SAndroid Build Coastguard Worker    CHECK_C_SOURCE_RUNS("
368*da0073e9SAndroid Build Coastguard Worker  #include <stdlib.h>
369*da0073e9SAndroid Build Coastguard Worker  #include <stdio.h>
370*da0073e9SAndroid Build Coastguard Worker  float x[4] = { 1, 2, 3, 4 };
371*da0073e9SAndroid Build Coastguard Worker  float y[4] = { .1, .01, .001, .0001 };
372*da0073e9SAndroid Build Coastguard Worker  extern float cblas_sdot();
373*da0073e9SAndroid Build Coastguard Worker  int main() {
374*da0073e9SAndroid Build Coastguard Worker    int i;
375*da0073e9SAndroid Build Coastguard Worker    double r = cblas_sdot(4, x, 1, y, 1);
376*da0073e9SAndroid Build Coastguard Worker    exit((float)r != (float).1234);
377*da0073e9SAndroid Build Coastguard Worker  }" BLAS_USE_CBLAS_DOT )
378*da0073e9SAndroid Build Coastguard Worker    IF (BLAS_USE_CBLAS_DOT)
379*da0073e9SAndroid Build Coastguard Worker      SET(BLAS_USE_CBLAS_DOT TRUE)
380*da0073e9SAndroid Build Coastguard Worker    ELSE (BLAS_USE_CBLAS_DOT)
381*da0073e9SAndroid Build Coastguard Worker      SET(BLAS_USE_CBLAS_DOT FALSE)
382*da0073e9SAndroid Build Coastguard Worker    ENDIF(BLAS_USE_CBLAS_DOT)
383*da0073e9SAndroid Build Coastguard Worker    SET(CMAKE_REQUIRED_LIBRARIES)
384*da0073e9SAndroid Build Coastguard Worker  ENDIF(CMAKE_CROSSCOMPILING)
385*da0073e9SAndroid Build Coastguard Worker  cmake_pop_check_state()
386*da0073e9SAndroid Build Coastguard WorkerENDIF(BLAS_LIBRARIES)
387*da0073e9SAndroid Build Coastguard Worker
388*da0073e9SAndroid Build Coastguard Worker# epilogue
389*da0073e9SAndroid Build Coastguard Worker
390*da0073e9SAndroid Build Coastguard Workerif(BLAS_LIBRARIES)
391*da0073e9SAndroid Build Coastguard Worker  set(BLAS_FOUND TRUE)
392*da0073e9SAndroid Build Coastguard Workerelse(BLAS_LIBRARIES)
393*da0073e9SAndroid Build Coastguard Worker  set(BLAS_FOUND FALSE)
394*da0073e9SAndroid Build Coastguard Workerendif(BLAS_LIBRARIES)
395*da0073e9SAndroid Build Coastguard Worker
396*da0073e9SAndroid Build Coastguard WorkerIF (NOT BLAS_FOUND AND BLAS_FIND_REQUIRED)
397*da0073e9SAndroid Build Coastguard Worker  message(FATAL_ERROR "Cannot find a library with BLAS API. Please specify library location.")
398*da0073e9SAndroid Build Coastguard WorkerENDIF(NOT BLAS_FOUND AND BLAS_FIND_REQUIRED)
399*da0073e9SAndroid Build Coastguard WorkerIF(NOT BLAS_FIND_QUIETLY)
400*da0073e9SAndroid Build Coastguard Worker  IF(BLAS_FOUND)
401*da0073e9SAndroid Build Coastguard Worker    MESSAGE(STATUS "Found a library with BLAS API (${BLAS_INFO}). Full path: (${BLAS_LIBRARIES})")
402*da0073e9SAndroid Build Coastguard Worker  ELSE(BLAS_FOUND)
403*da0073e9SAndroid Build Coastguard Worker    MESSAGE(STATUS "Cannot find a library with BLAS API. Not using BLAS.")
404*da0073e9SAndroid Build Coastguard Worker  ENDIF(BLAS_FOUND)
405*da0073e9SAndroid Build Coastguard WorkerENDIF(NOT BLAS_FIND_QUIETLY)
406*da0073e9SAndroid Build Coastguard Worker
407*da0073e9SAndroid Build Coastguard Worker# Do nothing is BLAS was found before
408*da0073e9SAndroid Build Coastguard WorkerENDIF(NOT BLAS_FOUND)
409*da0073e9SAndroid Build Coastguard Worker
410*da0073e9SAndroid Build Coastguard Worker# Blas has bfloat16 support?
411*da0073e9SAndroid Build Coastguard WorkerIF(BLAS_LIBRARIES)
412*da0073e9SAndroid Build Coastguard Worker  INCLUDE(CheckFunctionExists)
413*da0073e9SAndroid Build Coastguard Worker  SET(CMAKE_REQUIRED_LIBRARIES ${BLAS_LIBRARIES})
414*da0073e9SAndroid Build Coastguard Worker  check_function_exists("sbgemm_" BLAS_HAS_SBGEMM)
415*da0073e9SAndroid Build Coastguard Worker  set(CMAKE_REQUIRED_LIBRARIES)
416*da0073e9SAndroid Build Coastguard Worker  IF(BLAS_HAS_SBGEMM)
417*da0073e9SAndroid Build Coastguard Worker    add_compile_options(-DBLAS_HAS_SBGEMM)
418*da0073e9SAndroid Build Coastguard Worker  ENDIF(BLAS_HAS_SBGEMM)
419*da0073e9SAndroid Build Coastguard WorkerENDIF(BLAS_LIBRARIES)
420