Skip to content

Commit 52a15fc

Browse files
authored
CMake: Rewrite FindMKL.cmake (#7595)
* Rewrite FindMKL.cmake * Report MKL BLACS interface; block mkl_threads with non-OpenMP build * Remove cache variable MKL_THREADING * Block MKL_BLACS auto-detection when cross-compiling * Hide oneMKL internal result variables * Use MKL_FOUND for oneMKL consumers * CMake: Resolve DFT-D4 dependencies after math libraries * Move MKL-BLACS report into MKL_FOUND if clause
1 parent 86ae025 commit 52a15fc

8 files changed

Lines changed: 346 additions & 159 deletions

File tree

.github/workflows/build_test_cmake.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,11 @@ jobs:
2323

2424
- tag: gnu
2525
external_toolchain_args: ""
26-
build_args: "-DENABLE_LIBXC=ON -DENABLE_MLALGO=ON -DENABLE_LIBRI=ON"
26+
build_args: "-DENABLE_LIBXC=ON -DENABLE_MLALGO=ON -DENABLE_LIBRI=ON -DENABLE_DFTD4=ON"
2727
name: "Build extra components with GNU toolchain"
2828
- tag: intel
2929
external_toolchain_args: "--with-intel"
30-
build_args: "-DENABLE_LIBXC=ON -DENABLE_PEXSI=ON -DENABLE_MLALGO=ON -DENABLE_LIBRI=ON"
30+
build_args: "-DENABLE_LIBXC=ON -DENABLE_PEXSI=ON -DENABLE_MLALGO=ON -DENABLE_LIBRI=ON -DENABLE_DFTD4=ON"
3131
name: "Build extra components with Intel toolchain"
3232

3333
- tag: cuda

CMakeLists.txt

Lines changed: 45 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -244,14 +244,10 @@ if(NOT DEFINED CMAKE_CXX_STANDARD)
244244
endif()
245245
set(CMAKE_CXX_STANDARD_REQUIRED ON)
246246

247+
# Enable DFT-D4 languages before finding OpenMP.
247248
if(ENABLE_DFTD4)
248-
# DFTD4 requires enabling C and Fortran to work
249249
enable_language(C)
250250
enable_language(Fortran)
251-
# Avoid custom-lapack fallback when resolving DFT-D4 dependencies
252-
find_package(BLAS REQUIRED)
253-
find_package(LAPACK REQUIRED)
254-
find_package(dftd4 4.2.0 REQUIRED)
255251
endif()
256252

257253
macro(set_if_higher VARIABLE VALUE)
@@ -341,6 +337,12 @@ if(DEBUG_INFO)
341337
endif()
342338

343339
if(ENABLE_MPI)
340+
if(NOT CMAKE_CROSSCOMPILING)
341+
# FindMPI runs a probe executable to determine the MPI library version,
342+
# which FindMKL.cmake uses to auto-detect the BLACS interface.
343+
# When cross compiling, assume users know what they are doing.
344+
set(MPI_DETERMINE_LIBRARY_VERSION TRUE)
345+
endif()
344346
find_package(MPI COMPONENTS CXX REQUIRED)
345347
abacus_add_feature_definitions(__MPI)
346348
endif()
@@ -363,6 +365,44 @@ if(USE_OPENMP)
363365
find_package(OpenMP REQUIRED)
364366
endif()
365367

368+
if(DEFINED ENV{MKLROOT} AND NOT DEFINED MKLROOT)
369+
set(MKLROOT "$ENV{MKLROOT}")
370+
endif()
371+
if(USE_KML)
372+
set(_kml_components BLAS LAPACK FFTW3)
373+
if(ENABLE_MPI)
374+
list(APPEND _kml_components ScaLAPACK)
375+
endif()
376+
if(ENABLE_FLOAT_FFTW)
377+
list(APPEND _kml_components FFTW3_FLOAT)
378+
endif()
379+
380+
find_package(KML REQUIRED COMPONENTS ${_kml_components})
381+
abacus_add_feature_definitions(__KML)
382+
elseif(MKLROOT OR MKL_ROOT)
383+
find_package(MKL REQUIRED)
384+
abacus_add_feature_definitions(__MKL)
385+
elseif(NOT USE_SW)
386+
find_package(Lapack REQUIRED)
387+
# ScaLAPACK is a distributed-memory library and is only needed for the
388+
# MPI build. A serial build (e.g. the native Windows serial version)
389+
# must not require it.
390+
if(ENABLE_MPI)
391+
find_package(ScaLAPACK REQUIRED)
392+
endif()
393+
if(NOT CMAKE_CXX_COMPILER_ID MATCHES "GNU|Intel|Clang")
394+
message(WARNING "Cannot determine the required Fortran runtime.")
395+
endif()
396+
endif()
397+
398+
if(NOT USE_KML AND NOT MKL_FOUND AND NOT USE_SW)
399+
find_package(FFTW3 REQUIRED)
400+
endif()
401+
402+
if(ENABLE_DFTD4)
403+
find_package(dftd4 4.2.0 REQUIRED)
404+
endif()
405+
366406
include(CheckLanguage)
367407
check_language(CUDA)
368408
if(CMAKE_CUDA_COMPILER)
@@ -534,42 +574,6 @@ if(ENABLE_ASAN)
534574
add_link_options(-fsanitize=address)
535575
endif()
536576

537-
if(DEFINED ENV{MKLROOT} AND NOT DEFINED MKLROOT)
538-
set(MKLROOT "$ENV{MKLROOT}")
539-
endif()
540-
if(USE_KML)
541-
set(_kml_components BLAS LAPACK FFTW3)
542-
if(ENABLE_MPI)
543-
list(APPEND _kml_components ScaLAPACK)
544-
endif()
545-
if(ENABLE_FLOAT_FFTW)
546-
list(APPEND _kml_components FFTW3_FLOAT)
547-
endif()
548-
549-
find_package(KML REQUIRED COMPONENTS ${_kml_components})
550-
abacus_add_feature_definitions(__KML)
551-
elseif(MKLROOT)
552-
set(MKL_INTERFACE lp64)
553-
set(ENABLE_SCALAPACK ON)
554-
find_package(MKL REQUIRED)
555-
abacus_add_feature_definitions(__MKL)
556-
elseif(NOT USE_SW)
557-
find_package(Lapack REQUIRED)
558-
# ScaLAPACK is a distributed-memory library and is only needed for the
559-
# MPI build. A serial build (e.g. the native Windows serial version)
560-
# must not require it.
561-
if(ENABLE_MPI)
562-
find_package(ScaLAPACK REQUIRED)
563-
endif()
564-
if(NOT CMAKE_CXX_COMPILER_ID MATCHES "GNU|Intel|Clang")
565-
message(WARNING "Cannot determine the required Fortran runtime.")
566-
endif()
567-
endif()
568-
569-
if(NOT USE_KML AND NOT MKLROOT AND NOT USE_SW)
570-
find_package(FFTW3 REQUIRED)
571-
endif()
572-
573577
if(ENABLE_FLOAT_FFTW)
574578
abacus_add_feature_definitions(__ENABLE_FLOAT_FFTW)
575579
endif()

cmake/CollectBuildInfoVars.cmake

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ else()
165165
set(ABACUS_ELPA_VERSION "no")
166166
endif()
167167

168-
if(MKLROOT)
168+
if(MKL_FOUND)
169169
set(ABACUS_MKL_SUPPORT "yes (version unknown)")
170170
find_path(MKL_VERSION_HEADER mkl_version.h PATHS ${MKL_INCLUDE} NO_DEFAULT_PATH)
171171
if(MKL_VERSION_HEADER)
@@ -190,9 +190,9 @@ else()
190190
set(ABACUS_LIBXC_VERSION "no")
191191
endif()
192192

193-
if(NOT USE_SW AND NOT MKLROOT AND FFTW3_VERSION)
193+
if(NOT USE_SW AND NOT MKL_FOUND AND FFTW3_VERSION)
194194
set(ABACUS_FFTW_VERSION "yes (v${FFTW3_VERSION})")
195-
elseif(NOT USE_SW AND NOT MKLROOT)
195+
elseif(NOT USE_SW AND NOT MKL_FOUND)
196196
if(FFTW3_INCLUDE_DIR AND EXISTS "${FFTW3_INCLUDE_DIR}/fftw3.h")
197197
file(STRINGS "${FFTW3_INCLUDE_DIR}/fftw3.h" _fftw_ver_line REGEX "^#define[\t ]+FFTW_VERSION[\t ]+\"[^\"]+\"")
198198
if(_fftw_ver_line)

cmake/modules/FindKML.cmake

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,18 @@ if(KML_FOUND)
267267
endif()
268268
endif()
269269

270+
# Compatibility with packages that consume the standard CMake math targets.
271+
if(TARGET KML::BLAS AND NOT TARGET BLAS::BLAS)
272+
add_library(BLAS::BLAS INTERFACE IMPORTED)
273+
set_property(TARGET BLAS::BLAS PROPERTY
274+
INTERFACE_LINK_LIBRARIES KML::BLAS)
275+
endif()
276+
if(TARGET KML::LAPACK AND NOT TARGET LAPACK::LAPACK)
277+
add_library(LAPACK::LAPACK INTERFACE IMPORTED)
278+
set_property(TARGET LAPACK::LAPACK PROPERTY
279+
INTERFACE_LINK_LIBRARIES KML::LAPACK)
280+
endif()
281+
270282
mark_as_advanced(
271283
KML_INCLUDE_DIR
272284
KML_RUNTIME_LIBRARY

0 commit comments

Comments
 (0)