@@ -163,6 +163,7 @@ else()
163163endif ()
164164set_property (CACHE BLAS PROPERTY STRINGS "ATLAS;BLIS;Eigen;FLAME;Generic;MKL;OpenBLAS;vecLib;APL" )
165165message (STATUS "Trying to find preferred BLAS backend of choice: " ${BLAS} )
166+ set (BLAS_CHECK_F2C 0)
166167
167168if (BLAS STREQUAL "Eigen" )
168169 # Eigen is header-only and we do not have any dependent libraries
@@ -175,17 +176,20 @@ elseif(BLAS STREQUAL "ATLAS")
175176 set (BLAS_INFO "atlas" )
176177 set (BLAS_FOUND 1)
177178 set (BLAS_LIBRARIES ${ATLAS_LIBRARIES} cblas)
179+ set (BLAS_CHECK_F2C 1)
178180elseif (BLAS STREQUAL "OpenBLAS" )
179181 find_package (OpenBLAS REQUIRED)
180182 include_directories (SYSTEM ${OpenBLAS_INCLUDE_DIR} )
181183 list (APPEND Caffe2_DEPENDENCY_LIBS ${OpenBLAS_LIB} )
182184 set (BLAS_INFO "open" )
183185 set (BLAS_FOUND 1)
184186 set (BLAS_LIBRARIES ${OpenBLAS_LIB} )
187+ set (BLAS_CHECK_F2C 1)
185188elseif (BLAS STREQUAL "BLIS" )
186189 find_package (BLIS REQUIRED)
187190 include_directories (SYSTEM ${BLIS_INCLUDE_DIR} )
188191 list (APPEND Caffe2_DEPENDENCY_LIBS ${BLIS_LIB} )
192+ set (BLAS_CHECK_F2C 1)
189193elseif (BLAS STREQUAL "MKL" )
190194 if (BLAS_SET_BY_USER)
191195 find_package (MKL REQUIRED)
@@ -215,6 +219,7 @@ elseif(BLAS STREQUAL "NVPL")
215219 set (BLAS_INFO "nvpl" )
216220 set (BLAS_FOUND 1)
217221 set (BLAS_USE_CBLAS_DOT TRUE )
222+ set (BLAS_CHECK_F2C 1)
218223elseif (BLAS STREQUAL "vecLib" )
219224 find_package (vecLib REQUIRED)
220225 include_directories (SYSTEM ${vecLib_INCLUDE_DIR} )
@@ -226,12 +231,14 @@ elseif(BLAS STREQUAL "FlexiBLAS")
226231 find_package (FlexiBLAS REQUIRED)
227232 include_directories (SYSTEM ${FlexiBLAS_INCLUDE_DIR} )
228233 list (APPEND Caffe2_DEPENDENCY_LIBS ${FlexiBLAS_LIB} )
234+ set (BLAS_CHECK_F2C 1)
229235elseif (BLAS STREQUAL "APL" )
230236 find_package (APL REQUIRED)
231237 include_directories (SYSTEM ${APL_INCLUDE_DIR} )
232238 set (BLAS_INFO "apl" )
233239 set (BLAS_FOUND 1)
234240 set (BLAS_LIBRARIES ${APL_LIBRARIES} )
241+ set (BLAS_CHECK_F2C 1)
235242elseif (BLAS STREQUAL "Generic" )
236243 # On Debian family, the CBLAS ABIs have been merged into libblas.so
237244 if (ENV{GENERIC_BLAS_LIBRARIES} STREQUAL "" )
@@ -245,10 +252,89 @@ elseif(BLAS STREQUAL "Generic")
245252 set (GENERIC_BLAS_FOUND TRUE )
246253 set (BLAS_INFO "generic" )
247254 set (BLAS_FOUND 1)
255+ set (BLAS_CHECK_F2C 1)
248256else ()
249257 message (FATAL_ERROR "Unrecognized BLAS option: " ${BLAS} )
250258endif ()
251259
260+ # Determine if blas was compiled with the f2c conventions
261+ if (BLAS_LIBRARIES AND BLAS_CHECK_F2C)
262+ # Push host architecture when cross-compiling otherwise check would fail
263+ # when cross-compiling for arm64 on x86_64
264+ cmake_push_check_state(RESET)
265+ if (CMAKE_SYSTEM_NAME STREQUAL "Darwin" AND CMAKE_OSX_ARCHITECTURES MATCHES "^(x86_64|arm64)$" )
266+ list (APPEND CMAKE_REQUIRED_FLAGS "-arch ${CMAKE_HOST_SYSTEM_PROCESSOR} " )
267+ endif ()
268+
269+ # Set values through env variables if cross compiling
270+ if (CMAKE_CROSSCOMPILING )
271+ if ("$ENV{PYTORCH_BLAS_F2C} " STREQUAL "ON" )
272+ SET (BLAS_F2C TRUE )
273+ else ()
274+ SET (BLAS_F2C FALSE )
275+ endif ()
276+
277+ if ("$ENV{PYTORCH_BLAS_USE_CBLAS_DOT} " STREQUAL "ON" )
278+ SET (BLAS_USE_CBLAS_DOT TRUE )
279+ else ()
280+ SET (BLAS_USE_CBLAS_DOT FALSE )
281+ endif ()
282+ else ()
283+ SET (CMAKE_REQUIRED_LIBRARIES ${BLAS_LIBRARIES} )
284+ CHECK_C_SOURCE_RUNS("
285+ #include <stdlib.h>
286+ #include <stdio.h>
287+ float x[4] = { 1, 2, 3, 4 };
288+ float y[4] = { .1, .01, .001, .0001 };
289+ int four = 4;
290+ int one = 1;
291+ extern double sdot_();
292+ int main() {
293+ int i;
294+ double r = sdot_(&four, x, &one, y, &one);
295+ exit((float)r != (float).1234);
296+ }" BLAS_F2C_DOUBLE_WORKS )
297+ CHECK_C_SOURCE_RUNS("
298+ #include <stdlib.h>
299+ #include <stdio.h>
300+ float x[4] = { 1, 2, 3, 4 };
301+ float y[4] = { .1, .01, .001, .0001 };
302+ int four = 4;
303+ int one = 1;
304+ extern float sdot_();
305+ int main() {
306+ int i;
307+ double r = sdot_(&four, x, &one, y, &one);
308+ exit((float)r != (float).1234);
309+ }" BLAS_F2C_FLOAT_WORKS )
310+
311+ if (BLAS_F2C_DOUBLE_WORKS AND NOT BLAS_F2C_FLOAT_WORKS)
312+ MESSAGE (STATUS "This BLAS uses the F2C return conventions" )
313+ SET (BLAS_F2C TRUE )
314+ else (BLAS_F2C_DOUBLE_WORKS AND NOT BLAS_F2C_FLOAT_WORKS)
315+ SET (BLAS_F2C FALSE )
316+ endif (BLAS_F2C_DOUBLE_WORKS AND NOT BLAS_F2C_FLOAT_WORKS)
317+ CHECK_C_SOURCE_RUNS("
318+ #include <stdlib.h>
319+ #include <stdio.h>
320+ float x[4] = { 1, 2, 3, 4 };
321+ float y[4] = { .1, .01, .001, .0001 };
322+ extern float cblas_sdot();
323+ int main() {
324+ int i;
325+ double r = cblas_sdot(4, x, 1, y, 1);
326+ exit((float)r != (float).1234);
327+ }" BLAS_USE_CBLAS_DOT )
328+ if (BLAS_USE_CBLAS_DOT)
329+ SET (BLAS_USE_CBLAS_DOT TRUE )
330+ else (BLAS_USE_CBLAS_DOT)
331+ SET (BLAS_USE_CBLAS_DOT FALSE )
332+ endif (BLAS_USE_CBLAS_DOT)
333+ SET (CMAKE_REQUIRED_LIBRARIES)
334+ endif (CMAKE_CROSSCOMPILING )
335+ cmake_pop_check_state()
336+ endif (BLAS_LIBRARIES)
337+
252338if (NOT INTERN_BUILD_MOBILE)
253339 set (AT_MKL_SEQUENTIAL 0)
254340 set (USE_BLAS 1)
0 commit comments