Skip to content

Commit

Permalink
If MKL is found, override BLAS/LAPACK checks and use MKL for this. In…
Browse files Browse the repository at this point in the history
… SuiteSparse check, do not re-check for BLAS/LAPACK if it is already found. Fix MKL threading layer selection in the C++ extension.
  • Loading branch information
tskisner committed Nov 10, 2020
1 parent 604f4bb commit 133196b
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 23 deletions.
15 changes: 11 additions & 4 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ if(NOT TOAST_STATIC_DEPS AND NOT $ENV{TOAST_STATIC_DEPS} STREQUAL "")
set(TOAST_STATIC_DEPS $ENV{TOAST_STATIC_DEPS})
endif()

# OpenMP
find_package(OpenMP)

if(TOAST_STATIC_DEPS)
Expand All @@ -65,18 +66,24 @@ endif()
# First look for MKL, since that will provide both FFT support and BLAS/LAPACK
find_package(MKL)

if(NOT MKL_FOUND)
if(MKL_FOUND)
# Use MKL for BLAS / LAPACK
set(BLAS_LIBRARIES "${MKL_LIBRARIES}")
set(LAPACK_LIBRARIES "${MKL_LIBRARIES}")
set(BLAS_FOUND TRUE)
set(LAPACK_FOUND TRUE)
message(NOTICE "Using BLAS_LIBRARIES=${BLAS_LIBRARIES}")
else()
# Search for FFTW instead
find_package(FFTW)
if(NOT FFTW_FOUND)
message(FATAL_ERROR "Could not find a supported FFT library (MKL or FFTW)")
endif()
find_package(BLAS)
find_package(LAPACK)
endif()

find_package(BLAS)

if(BLAS_FOUND)
find_package(LAPACK)
if(LAPACK_FOUND)
find_package(LAPACKnames)
else()
Expand Down
25 changes: 17 additions & 8 deletions cmake/FindMKL.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -28,23 +28,26 @@ if(NOT MKL_DIR)
"C:/Program Files (x86)/IntelSWTools/compilers_and_libraries/windows/mkl"
)
endif()
else(WIN32)
else()
set(MKL_DIR "/opt/intel/mkl")
if(DEFINED ENV{MKLROOT})
# This is the standard MKL environment variable.
set(MKL_DIR "$ENV{MKLROOT}")
endif()
endif(WIN32)
endif()
endif()

find_package(Threads)

# First look in user-specified or default location

if(NOT MKL_FOUND)
set(MKL_LIB_DIR "${MKL_DIR}/lib")
find_library(
MKL_RT_LIB
NAMES "mkl_rt"
PATHS "${MKL_DIR}"
PATH_SUFFIXES "lib" "lib64"
PATHS "${MKL_LIB_DIR}"
PATH_SUFFIXES "intel64"
NO_DEFAULT_PATH
)
# find includes
Expand All @@ -55,8 +58,7 @@ if(NOT MKL_FOUND)
NO_DEFAULT_PATH
)
if(MKL_RT_LIB)
set(MKL_FOUND True)
set(MKL_LIBRARIES ${MKL_RT_LIB})
set(MKL_FOUND TRUE)
endif()
endif()

Expand All @@ -72,11 +74,17 @@ if(NOT MKL_FOUND)
NAMES "mkl_dfti.h"
)
if(MKL_RT_LIB)
set(MKL_FOUND True)
set(MKL_LIBRARIES ${MKL_RT_LIB})
set(MKL_FOUND TRUE)
endif()
endif()

if(MKL_FOUND)
set(MKL_LIBRARIES
${MKL_RT_LIB} ${CMAKE_THREAD_LIBS_INIT} -lm -l${CMAKE_DL_LIBS}
CACHE STRING "MKL dynamic libraries"
)
endif()

include(FindPackageHandleStandardArgs)

find_package_handle_standard_args(MKL
Expand All @@ -88,3 +96,4 @@ mark_as_advanced(
MKL_INCLUDE_DIRS
MKL_LIBRARIES
)

20 changes: 12 additions & 8 deletions cmake/FindSuiteSparse.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -291,19 +291,23 @@ endmacro()
unset(SUITESPARSE_FOUND_REQUIRED_VARS)

# BLAS.
find_package(BLAS QUIET)
if (NOT BLAS_FOUND)
suitesparse_report_not_found(
"Did not find BLAS library (required for SuiteSparse).")
endif (NOT BLAS_FOUND)
find_package(BLAS QUIET)
if (NOT BLAS_FOUND)
suitesparse_report_not_found(
"Did not find BLAS library (required for SuiteSparse).")
endif (NOT BLAS_FOUND)
endif()
list(APPEND SUITESPARSE_FOUND_REQUIRED_VARS BLAS_FOUND)

# LAPACK.
find_package(LAPACK QUIET)
if (NOT LAPACK_FOUND)
suitesparse_report_not_found(
"Did not find LAPACK library (required for SuiteSparse).")
endif (NOT LAPACK_FOUND)
find_package(LAPACK QUIET)
if (NOT LAPACK_FOUND)
suitesparse_report_not_found(
"Did not find LAPACK library (required for SuiteSparse).")
endif (NOT LAPACK_FOUND)
endif()
list(APPEND SUITESPARSE_FOUND_REQUIRED_VARS LAPACK_FOUND)

suitesparse_find_component(AMD REQUIRED FILES amd.h LIBRARIES amd)
Expand Down
11 changes: 8 additions & 3 deletions src/libtoast/src/toast_sys_environment.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ extern "C" {
# include <omp.h>
#endif // ifdef _OPENMP

#ifdef HAVE_MKL
extern "C" {
#include <mkl.h>
}
#endif

// These variables are autogenerated and compiled
// into the library by the version.cmake script
extern "C" {
Expand Down Expand Up @@ -149,11 +155,10 @@ toast::Environment::Environment() {

// Select MKL threading layer
#ifdef HAVE_MKL
# include <mkl.h>
# ifdef USE_MKL_GNU_THREADS
auto threading = mkl_set_threading_layer(MKL_THREADING_GNU);
auto td = mkl_set_threading_layer(MKL_THREADING_GNU);
# else // ifdef USE_MKL_GNU_THREADS
auto threading = mkl_set_threading_layer(MKL_THREADING_INTEL);
auto td = mkl_set_threading_layer(MKL_THREADING_INTEL);
# endif // ifdef USE_MKL_GNU_THREADS
#endif // ifdef HAVE_MKL

Expand Down

0 comments on commit 133196b

Please sign in to comment.