8000 Check F2C BLAS for OpenBLAS and other vendors · pytorch/pytorch@cec624e · GitHub
[go: up one dir, main page]

Skip to content

Commit cec624e

Browse files
committed
Check F2C BLAS for OpenBLAS and other vendors
ghstack-source-id: 89d8a99 Pull Request resolved: #143846
1 parent 7314cf4 commit cec624e

File tree

2 files changed

+86
-77
lines changed

2 files changed

+86
-77
lines changed

cmake/Dependencies.cmake

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,7 @@ else()
163163
endif()
164164
set_property(CACHE BLAS PROPERTY STRINGS "ATLAS;BLIS;Eigen;FLAME;Generic;MKL;OpenBLAS;vecLib;APL")
165165
message(STATUS "Trying to find preferred BLAS backend of choice: " ${BLAS})
166+
set(BLAS_CHECK_F2C 0)
166167

167168
if(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)
178180
elseif(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)
185188
elseif(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)
189193
elseif(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)
218223
elseif(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)
229235
elseif(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)
235242
elseif(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)
248256
else()
249257
message(FATAL_ERROR "Unrecognized BLAS option: " ${BLAS})
250258
endif()
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+
252338
if(NOT INTERN_BUILD_MOBILE)
253339
set(AT_MKL_SEQUENTIAL 0)
254340
set(USE_BLAS 1)

cmake/Modules/FindBLAS.cmake

Lines changed: 0 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -308,83 +308,6 @@ if((NOT BLAS_LIBRARIES)
308308
endif(BLAS_LIBRARIES)
309309
endif()
310310

311-
# Determine if blas was compiled with the f2c conventions
312-
IF (BLAS_LIBRARIES)
313-
# Push host architecture when cross-compiling otherwise check would fail
314-
# when cross-compiling for arm64 on x86_64
315-
cmake_push_check_state(RESET)
316-
if(CMAKE_SYSTEM_NAME STREQUAL "Darwin" AND CMAKE_OSX_ARCHITECTURES MATCHES "^(x86_64|arm64)$")
317-
list(APPEND CMAKE_REQUIRED_FLAGS "-arch ${CMAKE_HOST_SYSTEM_PROCESSOR}")
318-
endif()
319-
320-
# Set values through env variables if cross compiling
321-
IF (CMAKE_CROSSCOMPILING)
322-
IF("$ENV{PYTORCH_BLAS_F2C}" STREQUAL "ON")
323-
SET(BLAS_F2C TRUE)
324-
ELSE()
325-
SET(BLAS_F2C FALSE)
326-
ENDIF()
327-
328-
IF("$ENV{PYTORCH_BLAS_USE_CBLAS_DOT}" STREQUAL "ON")
329-
SET(BLAS_USE_CBLAS_DOT TRUE)
330-
ELSE()
331-
SET(BLAS_USE_CBLAS_DOT FALSE)
332-
ENDIF()
333-
ELSE ()
334-
SET(CMAKE_REQUIRED_LIBRARIES ${BLAS_LIBRARIES})
335-
CHECK_C_SOURCE_RUNS("
336-
#include <stdlib.h>
337-
#include <stdio.h>
338-
float x[4] = { 1, 2, 3, 4 };
339-
float y[4] = { .1, .01, .001, .0001 };
340-
int four = 4;
341-
int one = 1;
342-
extern double sdot_();
343-
int main() {
344-
int i;
345-
double r = sdot_(&four, x, &one, y, &one);
346-
exit((float)r != (float).1234);
347-
}" BLAS_F2C_DOUBLE_WORKS )
348-
CHECK_C_SOURCE_RUNS("
349-
#include <stdlib.h>
350-
#include <stdio.h>
351-
float x[4] = { 1, 2, 3, 4 };
352-
float y[4] = { .1, .01, .001, .0001 };
353-
int four = 4;
354-
int one = 1;
355-
extern float sdot_();
356-
int main() {
357-
int i;
358-
double r = sdot_(&four, x, &one, y, &one);
359-
exit((float)r != (float).1234);
360-
}" BLAS_F2C_FLOAT_WORKS )
361-
IF (BLAS_F2C_DOUBLE_WORKS AND NOT BLAS_F2C_FLOAT_WORKS)
362-
MESSAGE(STATUS "This BLAS uses the F2C return conventions")
363-
SET(BLAS_F2C TRUE)
364-
ELSE (BLAS_F2C_DOUBLE_WORKS AND NOT BLAS_F2C_FLOAT_WORKS)
365-
SET(BLAS_F2C FALSE)
366-
ENDIF(BLAS_F2C_DOUBLE_WORKS AND NOT BLAS_F2C_FLOAT_WORKS)
367-
CHECK_C_SOURCE_RUNS("
368-
#include <stdlib.h>
369-
#include <stdio.h>
370-
float x[4] = { 1, 2, 3, 4 };
371-
float y[4] = { .1, .01, .001, .0001 };
372-
extern float cblas_sdot();
373-
int main() {
374-
int i;
375-
double r = cblas_sdot(4, x, 1, y, 1);
376-
exit((float)r != (float).1234);
377-
}" BLAS_USE_CBLAS_DOT )
378-
IF (BLAS_USE_CBLAS_DOT)
379-
SET(BLAS_USE_CBLAS_DOT TRUE)
380-
ELSE (BLAS_USE_CBLAS_DOT)
381-
SET(BLAS_USE_CBLAS_DOT FALSE)
382-
ENDIF(BLAS_USE_CBLAS_DOT)
383-
SET(CMAKE_REQUIRED_LIBRARIES)
384-
ENDIF(CMAKE_CROSSCOMPILING)
385-
cmake_pop_check_state()
386-
ENDIF(BLAS_LIBRARIES)
387-
388311
# epilogue
389312

390313
if(BLAS_LIBRARIES)

0 commit comments

Comments
 (0)
0