fix some MKL detection issues of CMake (#94402)
This PR rewrites some logic of FindMKL.cmake and FindOpenMP.cmake to better detect the corresponding libraries and fix the infinitely recursion between them. It also contains some other fixes without changing the CMake interface.
Pull Request resolved: https://github.com/pytorch/pytorch/pull/94402
Approved by: https://github.com/malfet, https://github.com/Skylion007
diff --git a/cmake/Dependencies.cmake b/cmake/Dependencies.cmake
index 0e9096e..0012d26 100644
--- a/cmake/Dependencies.cmake
+++ b/cmake/Dependencies.cmake
@@ -217,7 +217,7 @@
message(STATUS "MKL OpenMP type: ${MKL_OPENMP_TYPE}")
message(STATUS "MKL OpenMP library: ${MKL_OPENMP_LIBRARY}")
include_directories(AFTER SYSTEM ${MKL_INCLUDE_DIR})
- list(APPEND Caffe2_PUBLIC_DEPENDENCY_LIBS caffe2::mkl)
+ list(APPEND Caffe2_DEPENDENCY_LIBS caffe2::mkl)
set(CAFFE2_USE_MKL ON)
set(BLAS_INFO "mkl")
set(BLAS_FOUND 1)
diff --git a/cmake/Modules/FindMKL.cmake b/cmake/Modules/FindMKL.cmake
index 83df105..d299631 100644
--- a/cmake/Modules/FindMKL.cmake
+++ b/cmake/Modules/FindMKL.cmake
@@ -41,10 +41,11 @@
ELSE (WIN32)
SET(DEFAULT_INTEL_COMPILER_DIR "/opt/intel")
SET(DEFAULT_INTEL_MKL_DIR "/opt/intel/mkl")
- if (EXISTS "/opt/intel/oneapi")
- SET(DEFAULT_INTEL_COMPILER_DIR "/opt/intel/oneapi")
- if (EXISTS "/opt/intel/oneapi/mkl/latest")
- SET(DEFAULT_INTEL_MKL_DIR "/opt/intel/oneapi/mkl/latest")
+ SET(DEFAULT_INTEL_ONEAPI_DIR "/opt/intel/oneapi")
+ if (EXISTS "${DEFAULT_INTEL_ONEAPI_DIR}")
+ SET(DEFAULT_INTEL_COMPILER_DIR "${DEFAULT_INTEL_ONEAPI_DIR}")
+ if (EXISTS "${DEFAULT_INTEL_ONEAPI_DIR}/mkl/latest")
+ SET(DEFAULT_INTEL_MKL_DIR "${DEFAULT_INTEL_ONEAPI_DIR}/mkl/latest")
endif()
endif()
ENDIF (WIN32)
@@ -379,7 +380,7 @@
# Include files
IF (MKL_LIBRARIES)
- FIND_PATH(MKL_INCLUDE_DIR "mkl_cblas.h")
+ FIND_PATH(MKL_INCLUDE_DIR NAMES "mkl_cblas.h" PATHS "/usr/include/mkl")
MARK_AS_ADVANCED(MKL_INCLUDE_DIR)
ENDIF (MKL_LIBRARIES)
diff --git a/cmake/Modules/FindOpenMP.cmake b/cmake/Modules/FindOpenMP.cmake
index 04e4ef8..d491cf3 100644
--- a/cmake/Modules/FindOpenMP.cmake
+++ b/cmake/Modules/FindOpenMP.cmake
@@ -227,8 +227,9 @@
# http://openmp.llvm.org/
#
# So here, before we test each flag combination, we first try directly
- # linking against any `libomp` MKL has found (if any). This allows us to
- # do sensible things in tricky (yet common) conditions like:
+ # linking against any `libomp` MKL has linked to (if any and when MKL is
+ # specified). This allows us to do sensible things in tricky (yet common)
+ # conditions like:
# - using `clang` (so no native GNU OpenMP), and
# - having `brew` `libomp` installed at `/usr/local/`, and
# - having `conda` `mkl` installed at `$HOME/conda/`, with includes a copy
@@ -236,19 +237,14 @@
# Rather than blindly picking one, we pick what ever `FindMKL.cmake` choses
# to avoid conflicts.
#
- # Crucially, we only do so for non-GNU compilers. For GNU ones,
# `FindMKL.cmake` calls `FindOpenMP.cmake` when trying to find `gomp` and
- # thus will cause infinite recursion if this is not taken care of. Moreover,
- # for them, since the compiler provices the OpenMP library, it is most
- # likely that only one viable gomp library can be found in search path by
- # `FindOpenMP.cmake`, so the chance of having conflicts is slow.
- #
- # TODO: refactor to solve this weird dependency where
- # - for non-GNU, FindOpenMP.cmake replies on FindMKL.cmake to finish first, but
- # - for GNU, FindMKL.cmake replies on FindOpenMP.cmake to finish first.
+ # thus will cause infinite recursion if this is not taken care of. Therefore,
+ # we record an internal flag to detect repeatedly inclusion.
- if(NOT "${CMAKE_${LANG}_COMPILER_ID}" STREQUAL "GNU")
+ if(NOT "${CMAKE_${LANG}_COMPILER_ID}" STREQUAL "GNU" AND BLAS STREQUAL "MKL" AND NOT IN_FIND_OMP)
+ set(IN_FIND_OMP ON CACHE BOOL "" FORCE)
find_package(MKL QUIET)
+ unset(IN_FIND_OMP CACHE)
if(MKL_FOUND AND MKL_OPENMP_LIBRARY)
# If we already link OpenMP via MKL, use that. Otherwise at run-time
# OpenMP will complain about being initialized twice (OMP: Error #15),