8000 Avoid linking multiple OMP runtimes in libtorch_cpu.so if BLAS used i… · pytorch/pytorch@e872bf8 · GitHub
[go: up one dir, main page]

Skip to content

Commit e872bf8

Browse files
vinithakvpytorchmergebot
authored andcommitted
Avoid linking multiple OMP runtimes in libtorch_cpu.so if BLAS used is OpenBLAS. (#147725)
When PyTorch is built with OpenBLAS support and libopenblas is ldrectly linked with libgomp.so the libtorch_cpu.so ends up getting multiple omp runtimes linked against it. This may result in unexpected runtime behaviour /regression. This patch fixes this by avoiding linking against libomp.so if OpenBLAS is linked against libgomp.so Fixes #146603 Pull Request resolved: #147725 Approved by: https://github.com/albanD
1 parent a1a4fee commit e872bf8

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

cmake/Modules/FindOpenBLAS.cmake

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,32 @@ ELSE (OpenBLAS_FOUND)
5959
ENDIF (OpenBLAS_FIND_REQUIRED)
6060
ENDIF (OpenBLAS_FOUND)
6161

62+
IF(OpenBLAS_LIB)
63+
# Run ldd on the OpenBLAS library
64+
execute_process(
65+
COMMAND ldd "${OpenBLAS_LIB}"
66+
OUTPUT_VARIABLE LDD_OUTPUT
67+
ERROR_VARIABLE LDD_ERROR
68+
RESULT_VARIABLE LDD_RESULT
69+
OUTPUT_STRIP_TRAILING_WHITESPACE
70+
)
71+
72+
if(NOT LDD_RESULT EQUAL 0)
73+
message(WARNING "ldd failed on ${OpenBLAS_LIB}: ${LDD_ERROR}")
74+
endif()
75+
76+
# Check if the output contains "libgomp"
77+
string(FIND "${LDD_OUTPUT}" "libgomp" LIBGOMP_FOUND_INDEX)
78+
if(LIBGOMP_FOUND_INDEX GREATER -1)
79+
message(STATUS "OpenBLAS is directly linked against libgomp")
80+
set(OPENBLAS_USES_LIBGOMP TRUE CACHE BOOL "OpenBLAS uses libgomp")
81+
else()
82+
message(STATUS "OpenBLAS is not directly linked against libgomp")
83+
set(OPENBLAS_USES_LIBGOMP FALSE CACHE BOOL "OpenBLAS uses libgomp")
84+
endif()
85+
86+
ENDIF(OpenBLAS_LIB)
87+
6288
MARK_AS_ADVANCED(
6389
OpenBLAS_INCLUDE_DIR
6490
OpenBLAS_LIB

cmake/Modules/FindOpenMP.cmake

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,18 @@ function(_OPENMP_GET_FLAGS LANG FLAG_MODE OPENMP_FLAG_VAR OPENMP_LIB_NAMES_VAR)
277277
mark_as_advanced(OpenMP_libomp_LIBRARY)
278278
endif()
279279

280+
# Check if we are using OpenBLAS which is linked against libgomp
281+
# we may end up with multiple omp runtimes linked
282+
# against libtorch_cpu.so
283+
if(OpenBLAS_LIB AND OPENBLAS_USES_LIBGOMP)
284+
find_library(OpenMP_libomp_LIBRARY
285+
NAMES gomp
286+
HINTS ${CMAKE_${LANG}_IMPLICIT_LINK_DIRECTORIES}
287+
DOC "libomp location for OpenMP"
288+
)
289+
mark_as_advanced(OpenMP_libomp_LIBRARY)
290+
endif()
291+
280292
if (NOT OpenMP_libomp_LIBRARY)
281293
find_library(OpenMP_libomp_LIBRARY
282294
NAMES omp gomp iomp5

0 commit comments

Comments
 (0)
0