From 1f5e358b45fa888408dc70e58207219dba4482e5 Mon Sep 17 00:00:00 2001 From: Nils Wentzell Date: Tue, 5 Dec 2023 15:44:40 -0500 Subject: [PATCH] [cmake] Improve logic when MKL is detected as LAPACK distribution - If no BLA_VENDOR specified, link against single dynamic library and set MKL_INTERFACE_LAYER to GNU,LP64 at runtime --- c++/nda/CMakeLists.txt | 17 +++++++++-------- c++/nda/blas/interface/cxx_interface.cpp | 2 ++ 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/c++/nda/CMakeLists.txt b/c++/nda/CMakeLists.txt index a17de8028..b228b7aed 100644 --- a/c++/nda/CMakeLists.txt +++ b/c++/nda/CMakeLists.txt @@ -46,18 +46,16 @@ target_link_libraries(${PROJECT_NAME}_c PUBLIC OpenMP::OpenMP_CXX) message(STATUS "-------- Lapack detection -------------") -find_package(LAPACK REQUIRED) -if(LAPACK_LIBRARIES MATCHES "libmkl" AND NOT LAPACK_LIBRARIES MATCHES "libmkl_rt") - message(STATUS "Found MKL: Making sure to use single dynamic library") +find_package(LAPACK QUIET REQUIRED) +if(LAPACK_LIBRARIES MATCHES "libmkl" AND BLA_VENDOR STREQUAL "All") + message(STATUS "Detected MKL with no BLA_VENDOR set, defaulting to single dynamic library 'libmkl_rt'") set(BLA_VENDOR Intel10_64_dyn) - find_package(LAPACK REQUIRED) -endif() -if(LAPACK_LIBRARIES MATCHES "Accelerate.framework") - message(STATUS "Can't build with Accelerate Framework on OSX, trying to find OpenBLAS instead.") +elseif(LAPACK_LIBRARIES MATCHES "Accelerate.framework") + message(STATUS "Can't build with Accelerate Framework on OSX, trying to find OpenBLAS instead...") set(BLA_VENDOR OpenBLAS) # Accelerate Framework has issue in zdot set(LAPACK_ROOT "${LAPACK_ROOT};$ENV{LAPACK_ROOT};/opt/homebrew/opt/openblas;/usr/local/opt/openblas") - find_package(LAPACK REQUIRED) endif() +find_package(LAPACK REQUIRED) # Create an interface target add_library(blas_lapack INTERFACE) @@ -70,6 +68,9 @@ if(LAPACK_LIBRARIES MATCHES "libmkl") target_include_directories(${PROJECT_NAME}_c PUBLIC $ENV{MKLROOT}/include) endif() target_compile_definitions(${PROJECT_NAME}_c PUBLIC NDA_USE_MKL) + if(LAPACK_LIBRARIES MATCHES "libmkl_rt") + target_compile_definitions(${PROJECT_NAME}_c PRIVATE NDA_USE_MKL_RT) + endif() endif() # Link against interface target and export diff --git a/c++/nda/blas/interface/cxx_interface.cpp b/c++/nda/blas/interface/cxx_interface.cpp index 2201ca5ff..829417968 100644 --- a/c++/nda/blas/interface/cxx_interface.cpp +++ b/c++/nda/blas/interface/cxx_interface.cpp @@ -29,7 +29,9 @@ using namespace std::string_literals; #ifdef NDA_USE_MKL #include namespace nda::blas { +#ifdef NDA_USE_MKL_RT static int const mkl_interface_layer = mkl_set_interface_layer(MKL_INTERFACE_LP64 + MKL_INTERFACE_GNU); +#endif inline auto *mklcplx(nda::dcomplex *c) { return reinterpret_cast(c); } // NOLINT inline auto *mklcplx(nda::dcomplex const *c) { return reinterpret_cast(c); } // NOLINT inline auto *mklcplx(nda::dcomplex **c) { return reinterpret_cast(c); } // NOLINT