From 93b346676b6ae6ceb25a2c167804b535bcd65aac Mon Sep 17 00:00:00 2001 From: userchba <1075002153@qq.com> Date: Thu, 3 Dec 2026 13:28:28 +0800 Subject: [PATCH 1/2] feat(hsolver): add optional LibDDLA eigensolver for LCAO --- CMakeLists.txt | 26 ++- cmake/CollectBuildInfoVars.cmake | 8 + docs/advanced/install.md | 22 ++ source/CMakeLists.txt | 2 + source/Makefile | 6 + source/Makefile.Objects | 1 + source/Makefile.vars | 3 + source/source_base/global_function.h | 2 +- source/source_estate/elecstate_print.cpp | 1 + source/source_hsolver/CMakeLists.txt | 3 + source/source_hsolver/diag_lcao.cpp | 11 + source/source_hsolver/diago_ddla.cpp | 201 +++++++++++++++++ source/source_hsolver/diago_ddla.h | 34 +++ source/source_hsolver/hsolver_lcao.h | 2 +- .../source_hsolver/unittests/CMakeLists.txt | 10 + .../unittests/test_diag_sk_cut.cpp | 9 + .../unittests/test_diago_ddla.cpp | 209 ++++++++++++++++++ .../module_parameter/read_inp_estruc.cpp | 16 +- tests/02_NAO_Gamma/CASES_DDLA.txt | 1 + tests/02_NAO_Gamma/CMakeLists.txt | 11 + .../02_NAO_Gamma/scf_force_stress_ddla/INPUT | 33 +++ tests/02_NAO_Gamma/scf_force_stress_ddla/KPT | 4 + .../02_NAO_Gamma/scf_force_stress_ddla/README | 3 + tests/02_NAO_Gamma/scf_force_stress_ddla/STRU | 22 ++ .../scf_force_stress_ddla/result.ref | 8 + tests/03_NAO_multik/CASES_DDLA.txt | 1 + tests/03_NAO_multik/CMakeLists.txt | 11 + .../03_NAO_multik/scf_angle_spin4_ddla/INPUT | 35 +++ tests/03_NAO_multik/scf_angle_spin4_ddla/KPT | 4 + .../03_NAO_multik/scf_angle_spin4_ddla/README | 3 + tests/03_NAO_multik/scf_angle_spin4_ddla/STRU | 22 ++ .../scf_angle_spin4_ddla/result.ref | 5 + 32 files changed, 718 insertions(+), 11 deletions(-) create mode 100644 source/source_hsolver/diago_ddla.cpp create mode 100644 source/source_hsolver/diago_ddla.h create mode 100644 source/source_hsolver/unittests/test_diago_ddla.cpp create mode 100644 tests/02_NAO_Gamma/CASES_DDLA.txt create mode 100644 tests/02_NAO_Gamma/scf_force_stress_ddla/INPUT create mode 100644 tests/02_NAO_Gamma/scf_force_stress_ddla/KPT create mode 100644 tests/02_NAO_Gamma/scf_force_stress_ddla/README create mode 100644 tests/02_NAO_Gamma/scf_force_stress_ddla/STRU create mode 100644 tests/02_NAO_Gamma/scf_force_stress_ddla/result.ref create mode 100644 tests/03_NAO_multik/CASES_DDLA.txt create mode 100644 tests/03_NAO_multik/scf_angle_spin4_ddla/INPUT create mode 100644 tests/03_NAO_multik/scf_angle_spin4_ddla/KPT create mode 100644 tests/03_NAO_multik/scf_angle_spin4_ddla/README create mode 100644 tests/03_NAO_multik/scf_angle_spin4_ddla/STRU create mode 100644 tests/03_NAO_multik/scf_angle_spin4_ddla/result.ref diff --git a/CMakeLists.txt b/CMakeLists.txt index 8e53bad92fe..4d650c9de5c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -78,6 +78,8 @@ cmake_dependent_option(ENABLE_LIBRI "Enable LibRI for hybrid functional" OFF cmake_dependent_option( ENABLE_LIBRI_GPU "Enable experimental LibRI CUDA contractions through DDLA" OFF "ENABLE_LIBRI;USE_CUDA" OFF) +cmake_dependent_option(ENABLE_DDLA "Enable the LibDDLA LCAO eigensolver" OFF + "ENABLE_LCAO;ENABLE_MPI" OFF) cmake_dependent_option(ENABLE_PEXSI "Enable PEXSI for LCAO" OFF "ENABLE_LCAO;ENABLE_MPI" OFF) cmake_dependent_option(ENABLE_MLALGO "Enable the machine learning algorithms" @@ -293,6 +295,10 @@ if(ENABLE_PEXSI) set(CMAKE_CXX_STANDARD 14) endif() +if(ENABLE_DDLA) + find_package(LibDDLA 0.0.7 REQUIRED CONFIG) +endif() + if(ENABLE_MPI) if(NOT CMAKE_CROSSCOMPILING) # FindMPI runs a probe executable to determine the MPI library version, @@ -546,7 +552,9 @@ endif() if(ENABLE_CNPY) # Allow user to specify a local CNPY installation root (containing include/ and lib/). - set(CNPY_DIR "" CACHE PATH "Path to local CNPY installation root") + set(CNPY_DIR + "" + CACHE PATH "Path to local CNPY installation root") find_path(cnpy_SOURCE_DIR cnpy.h HINTS ${CNPY_DIR}/include) if(NOT cnpy_SOURCE_DIR) include(FetchContent) @@ -561,14 +569,20 @@ if(ENABLE_CNPY) # (i.e. the find_path branch above found a pre-built library), create an # imported target so that downstream linking works without FetchContent. if(CNPY_DIR AND NOT TARGET cnpy) - find_library(cnpy_LIBRARY NAMES cnpy HINTS ${CNPY_DIR}/lib) + find_library( + cnpy_LIBRARY + NAMES cnpy + HINTS ${CNPY_DIR}/lib) if(NOT cnpy_LIBRARY) - message(FATAL_ERROR "CNPY_DIR is set to '${CNPY_DIR}' but no libcnpy was found under its lib directory.") + message( + FATAL_ERROR + "CNPY_DIR is set to '${CNPY_DIR}' but no libcnpy was found under its lib directory." + ) endif() add_library(cnpy UNKNOWN IMPORTED) - set_target_properties(cnpy PROPERTIES - IMPORTED_LOCATION "${cnpy_LIBRARY}" - INTERFACE_INCLUDE_DIRECTORIES "${cnpy_SOURCE_DIR}") + set_target_properties( + cnpy PROPERTIES IMPORTED_LOCATION "${cnpy_LIBRARY}" + INTERFACE_INCLUDE_DIRECTORIES "${cnpy_SOURCE_DIR}") endif() # find ZLIB and link diff --git a/cmake/CollectBuildInfoVars.cmake b/cmake/CollectBuildInfoVars.cmake index 94cfd82fb32..4d3e31b8e02 100644 --- a/cmake/CollectBuildInfoVars.cmake +++ b/cmake/CollectBuildInfoVars.cmake @@ -178,6 +178,12 @@ else() set(ABACUS_ELPA_VERSION "no") endif() +if(ENABLE_DDLA) + set(ABACUS_DDLA_VERSION "yes (v${LibDDLA_VERSION})") +else() + set(ABACUS_DDLA_VERSION "no") +endif() + if(ENABLE_LIBXC AND Libxc_VERSION) set(ABACUS_LIBXC_VERSION "yes (v${Libxc_VERSION})") elseif(ENABLE_LIBXC) @@ -432,6 +438,7 @@ list(APPEND CMAKE_FIND_PACKAGES_LIST " ROCm Found=${ROCM_FOUND}") list(APPEND CMAKE_FIND_PACKAGES_LIST " ELPA Found=${ELPA_FOUND}") list(APPEND CMAKE_FIND_PACKAGES_LIST " Libxc Found=${Libxc_FOUND}") list(APPEND CMAKE_FIND_PACKAGES_LIST " PEXSI Found=${PEXSI_FOUND}") +list(APPEND CMAKE_FIND_PACKAGES_LIST " LibDDLA Found=${LibDDLA_FOUND}") list(APPEND CMAKE_FIND_PACKAGES_LIST " LibRI Found=${LIBRI_FOUND}") foreach(package_line ${CMAKE_FIND_PACKAGES_LIST}) set(ABACUS_CMAKE_FIND_PACKAGES "${ABACUS_CMAKE_FIND_PACKAGES}${package_line}") @@ -462,6 +469,7 @@ message(" -------------------------------------------------------------------") message(" Math Libraries : BLAS = ${ABACUS_BLAS_VENDOR}") message(" FFTW = ${ABACUS_FFTW_VERSION}") message(" ELPA = ${ABACUS_ELPA_VERSION}") +message(" LibDDLA = ${ABACUS_DDLA_VERSION}") if(ENABLE_ABACUS_LIBM) message(" Internal libmath enabled") endif() diff --git a/docs/advanced/install.md b/docs/advanced/install.md index 1d786c03b20..869951abd94 100644 --- a/docs/advanced/install.md +++ b/docs/advanced/install.md @@ -216,6 +216,28 @@ Currently supported math functions: cmake -B build -DENABLE_ABACUS_LIBM=1 ``` +## Build with the optional LibDDLA eigensolver + +LibDDLA >= 0.0.7 can provide the distributed LCAO generalized eigensolver. +It is disabled by default. To enable it, configure ABACUS with +`-DENABLE_DDLA=ON -DENABLE_MPI=ON -DENABLE_LCAO=ON` and add the installed +LibDDLA prefix to `CMAKE_PREFIX_PATH` (or set `LibDDLA_DIR` to its CMake package directory). +The existing native solvers do not require LibDDLA. + +Select `ks_solver ddla` and `kpar 1` in INPUT. Both real Gamma-only and complex +k-point matrices are supported. Use `device cpu` for CPU execution. For +`device gpu`, build ABACUS with CUDA or ROCm and link a GPU-enabled LibDDLA +installation; use one MPI rank per visible GPU on each node. An unavailable +backend is rejected rather than falling back silently. GPU solves currently +use the full MPI process grid, with a common visible-device list for all ranks +on each node. + +This initial integration is confined to diagonalization: H/S are copied into +the selected backend for each solve, and eigenvectors return to the existing +host density workflow. It adds no DDLA state to density, RT, LR/BSE or +postprocessing. K-point pooling (`kpar>1`) and persistent device storage across +solves are outside this contribution. + ## Build with PEXSI support ABACUS supports the PEXSI library for gamma only LCAO calculations. PEXSI version >=2.0.0 is required. diff --git a/source/CMakeLists.txt b/source/CMakeLists.txt index 5530884693e..8490479c13a 100644 --- a/source/CMakeLists.txt +++ b/source/CMakeLists.txt @@ -42,6 +42,7 @@ set(_abacus_feature_definitions $<$:__ELPA> $<$:__FFT_TWO_CENTER> $<$:__PEXSI> + $<$:__DDLA> $<$:__DEBUG> $<$:__MPI> $<$:__DSP> @@ -293,6 +294,7 @@ set(_abacus_feature_libs $<$:nlohmann_json::nlohmann_json> $<$:ELPA::ELPA> $<$:PEXSI::PEXSI> + $<$:ddla::ddla> $<$:cnpy> $<$:ZLIB::ZLIB> $<$:LibRI::LibRI> diff --git a/source/Makefile b/source/Makefile index cf1ec0826f5..7f804045255 100644 --- a/source/Makefile +++ b/source/Makefile @@ -216,6 +216,12 @@ ifdef TensorFlow_DIR INCLUDES += -I${TensorFlow_INCLUDE_DIR} endif +ifdef DDLA_DIR + INCLUDES += -I${DDLA_DIR}/include + LIBS += -L${DDLA_DIR}/lib64 -L${DDLA_DIR}/lib -lddla + HONG += -D__DDLA +endif + ifdef PEXSI_DIR OBJS_ABACUS += ${OBJS_HSOLVER_PEXSI} INCLUDES += -I${PEXSI_DIR}/include -I${PARMETIS_DIR}/include -I${DSUPERLU_DIR}/include diff --git a/source/Makefile.Objects b/source/Makefile.Objects index fa8f050a220..0971b95de32 100644 --- a/source/Makefile.Objects +++ b/source/Makefile.Objects @@ -558,6 +558,7 @@ OBJS_HSOLVER_LCAO=hsolver_lcao.o\ diag_lcao.o\ diag_sk_cut.o\ diago_scalapack.o\ + diago_ddla.o\ diago_lapack.o\ diago_elpa.o\ diago_elpa_native.o\ diff --git a/source/Makefile.vars b/source/Makefile.vars index 656337ca69e..2db165d220f 100644 --- a/source/Makefile.vars +++ b/source/Makefile.vars @@ -88,3 +88,6 @@ ENABLE_MLALGO ?= OFF # ON: use gnu compiler and check segmental defaults # OFF: nothing #====================================================================== + +# Optional LibDDLA >= 0.0.7 LCAO eigensolver (MPI build only): +# DDLA_DIR = /path/to/libddla diff --git a/source/source_base/global_function.h b/source/source_base/global_function.h index 7aa625b3135..c732fe8281b 100644 --- a/source/source_base/global_function.h +++ b/source/source_base/global_function.h @@ -374,7 +374,7 @@ T ddot_real(const int& dim, const std::complex* psi_L, const std::complex* static inline bool IS_COLUMN_MAJOR_KS_SOLVER(std::string ks_solver) { return ks_solver == "genelpa" || ks_solver == "elpa" || ks_solver == "scalapack_gvx" || ks_solver == "cusolver" - || ks_solver == "cusolvermp" || ks_solver == "pexsi" || ks_solver == "lapack"; + || ks_solver == "cusolvermp" || ks_solver == "pexsi" || ks_solver == "lapack" || ks_solver == "ddla"; } } // namespace GlobalFunc diff --git a/source/source_estate/elecstate_print.cpp b/source/source_estate/elecstate_print.cpp index f129585164f..ace3af04acd 100644 --- a/source/source_estate/elecstate_print.cpp +++ b/source/source_estate/elecstate_print.cpp @@ -56,6 +56,7 @@ void print_scf_iterinfo(const std::string& ks_solver, {"dav_subspace", "DS"}, {"lobpcg", "LB"}, {"scalapack_gvx", "GV"}, + {"ddla", "DD"}, {"cusolver", "CU"}, {"bpcg", "BP"}, {"pexsi", "PE"}, diff --git a/source/source_hsolver/CMakeLists.txt b/source/source_hsolver/CMakeLists.txt index 5b49a78264d..99205cc7c9f 100644 --- a/source/source_hsolver/CMakeLists.txt +++ b/source/source_hsolver/CMakeLists.txt @@ -25,6 +25,9 @@ if(ENABLE_LCAO) if(ENABLE_MPI) list(APPEND objects diago_scalapack.cpp parallel_k2d.cpp) endif() + if(ENABLE_DDLA) + list(APPEND objects diago_ddla.cpp) + endif() if(ENABLE_ELPA) list(APPEND objects diago_elpa.cpp diago_elpa_native.cpp) endif() diff --git a/source/source_hsolver/diag_lcao.cpp b/source/source_hsolver/diag_lcao.cpp index 0cfbf310d32..a836969a203 100644 --- a/source/source_hsolver/diag_lcao.cpp +++ b/source/source_hsolver/diag_lcao.cpp @@ -1,6 +1,9 @@ #include "diag_lcao.h" #include "diago_lapack.h" +#if defined(__DDLA) && defined(__MPI) +#include "diago_ddla.h" +#endif #include "source_base/parallel_blkcyc.h" #include "source_base/tool_quit.h" @@ -98,6 +101,14 @@ void diag_lcao(ModuleBase::MatrixBlock& h, else #endif #endif + #if defined(__DDLA) && defined(__MPI) + if (method == "ddla") + { + DiagoDdla solver(order, bands, options.use_gpu); + solver.diag(h, s, psi, eigenvalues); + } + else + #endif if (method == "lapack") { DiagoLapack solver(order, bands); diff --git a/source/source_hsolver/diago_ddla.cpp b/source/source_hsolver/diago_ddla.cpp new file mode 100644 index 00000000000..9efb61b8a04 --- /dev/null +++ b/source/source_hsolver/diago_ddla.cpp @@ -0,0 +1,201 @@ +#include "diago_ddla.h" + +#if defined(__DDLA) && defined(__MPI) +#include "source_base/module_device/device.h" +#include "source_base/parallel_blkcyc.h" +#include "source_base/parallel_common.h" +#include "source_base/parallel_device.h" +#include "source_base/timer.h" +#include "source_base/tool_quit.h" + +#include +#include + +#if defined(__CUDA) +#include +#elif defined(__ROCM) +#include +#endif + +#include +#include +#include +#include +#include +#include +#include + +namespace hsolver +{ +namespace +{ +// The handle outlives every backend buffer, including during stack unwinding. +class Handle +{ + public: + ddla::DdlaHandle_t value = nullptr; + + ~Handle() + { + ddla::ddlaDestroy(value); + } + + Handle() = default; + Handle(const Handle&) = delete; + Handle& operator=(const Handle&) = delete; +}; + +void check_gpu_sharing(const ddla::DdlaHandle_t& handle, const MPI_Comm comm) +{ + int devices = 0; + ddla::DDLA_CHECK(ddla::ddlaGetDeviceCount(handle, &devices)); + const int local_rank = base_device::information::get_node_rank_with_mpi_shared(MPI_COMM_WORLD); + double invalid = local_rank >= devices ? 1.0 : 0.0; + Parallel_Common::reduce_data(&invalid, 1, comm); + if (invalid != 0.0) + { + throw std::runtime_error("GPU DDLA requires one MPI rank per visible GPU on each node"); + } +} + +void check_gpu_binding(const MPI_Comm comm) +{ + const int bound = base_device::DeviceContext::instance().get_device_id(); + int current = -1; +#if defined(__CUDA) + if (cudaGetDevice(¤t) != cudaSuccess) + { + throw std::runtime_error("cannot query the current CUDA device"); + } +#elif defined(__ROCM) + if (hipGetDevice(¤t) != hipSuccess) + { + throw std::runtime_error("cannot query the current HIP device"); + } +#endif + double invalid = bound >= 0 && bound != current ? 1.0 : 0.0; + Parallel_Common::reduce_data(&invalid, 1, comm); + if (invalid != 0.0) + { + throw std::runtime_error("LibDDLA changed the GPU selected by ABACUS"); + } +} +} // namespace + +template +void DiagoDdla::diag(ModuleBase::MatrixBlock& h, + ModuleBase::MatrixBlock& s, + psi::Psi& vectors, + double* eigenvalues) +{ + ModuleBase::timer::start("DiagoDdla", "diag"); + int info = 0; + try + { + if (order_ <= 0 || bands_ <= 0 || bands_ > order_ || h.desc == nullptr || s.desc == nullptr) + { + throw std::invalid_argument("invalid DDLA problem dimensions or missing BLACS descriptors"); + } + const int* desc = h.desc; + const int* desc_end = desc + 9; + if (!std::equal(desc, desc_end, s.desc) || desc[1] < 0 || desc[4] <= 0 || desc[2] != order_ || desc[3] != order_ + || desc[4] != desc[5] || desc[6] != 0 || desc[7] != 0) + { + throw std::invalid_argument("DDLA requires matching square, source-zero block-cyclic H/S layouts"); + } + // Borrow the caller's BLACS grid, including its communicator. No grid + // or backend state escapes this numerical solve. + Parallel_Blkcyclic layout; + layout.bind_descriptor(order_, order_, desc[4], desc[1]); + const MPI_Comm comm = layout.comm(); + const int rows = layout.nrloc(); + const int cols = layout.ncloc(); + if (h.row != rows || h.col != cols || s.row != rows || s.col != cols || desc[8] < std::max(1, rows)) + { + throw std::invalid_argument("DDLA matrix blocks do not match their descriptors"); + } + const ddla::DdlaBackend backend = use_gpu_ ? ddla::DdlaBackend::GPU : ddla::DdlaBackend::CPU; + if (!ddla::ddlaBackendAvailable(backend)) + { + throw std::runtime_error("the linked LibDDLA installation does not support the selected CPU/GPU backend"); + } + if (use_gpu_) + { +#if !defined(__CUDA) && !defined(__ROCM) + throw std::runtime_error("GPU DDLA requires a CUDA/ROCm-enabled ABACUS build"); +#endif + const int grid_size = Parallel_Common::comm_size(comm); + const int world_size = Parallel_Common::comm_size(MPI_COMM_WORLD); + if (grid_size != world_size) + { + throw std::runtime_error("GPU DDLA currently requires the full MPI process grid and kpar=1"); + } + } + Handle handle; + ddla::DDLA_CHECK(ddla::ddlaInit(handle.value, backend)); + // kpar=1 keeps every rank in the same solve. The GPU guard must run + // before creating CCL communicators, which cannot share a GPU. + if (use_gpu_) + { + check_gpu_sharing(handle.value, comm); + } + const int process_rows = layout.nrproc(); + const int process_cols = layout.ncproc(); + ddla::DDLA_CHECK(ddla::ddlaSet(handle.value, comm, process_rows, process_cols, 'R')); + if (use_gpu_) + { + check_gpu_binding(comm); + } + + const std::size_t leading = desc[8]; + const std::size_t storage = leading * std::max(1, cols); + const std::size_t pitch = leading * sizeof(T); + const std::size_t width = static_cast(rows) * sizeof(T); + ddla::vector h_work(handle.value, storage); + ddla::vector s_work(handle.value, storage); + if (rows > 0 && cols > 0) + { + ddla::DDLA_CHECK(ddla::ddlaMemcpy2D(handle.value, h_work.data(), pitch, h.p, pitch, width, cols)); + ddla::DDLA_CHECK(ddla::ddlaMemcpy2D(handle.value, s_work.data(), pitch, s.p, pitch, width, cols)); + } + std::vector values(order_); + ddla::phegvd(handle.value, 'V', 'U', order_, bands_, h_work.data(), desc, + s_work.data(), desc, values.data(), h_work.data(), desc, info); + if (info == 0) + { + Parallel_Blkcyclic output; + output.bind_descriptor(order_, bands_, desc[4], desc[1]); + const int output_cols = output.ncloc(); + if (rows > 0 && output_cols > 0) + { + ddla::DDLA_CHECK(ddla::ddlaMemcpy2D(handle.value, vectors.get_pointer(), width, + h_work.data(), pitch, width, output_cols)); + } + // The host density workflow may consume Psi immediately. + ddla::DDLA_CHECK(ddla::ddlaSynchronize(handle.value)); + const double* values_begin = values.data(); + const double* values_end = values_begin + bands_; + std::copy(values_begin, values_end, eigenvalues); + } + } + catch (const std::exception& error) + { + const std::string message = std::string("LibDDLA generalized eigensolve failed: ") + error.what(); + ModuleBase::WARNING_QUIT("DiagoDdla::diag", message); + } + if (info != 0) + { + std::string message = "LibDDLA phegvd returned info=" + std::to_string(info); + if (info > order_) + { + message += "; overlap matrix is not positive definite"; + } + ModuleBase::WARNING_QUIT("DiagoDdla::diag", message); + } + ModuleBase::timer::end("DiagoDdla", "diag"); +} + +template class DiagoDdla; +template class DiagoDdla>; +} // namespace hsolver +#endif diff --git a/source/source_hsolver/diago_ddla.h b/source/source_hsolver/diago_ddla.h new file mode 100644 index 00000000000..0de0f75b235 --- /dev/null +++ b/source/source_hsolver/diago_ddla.h @@ -0,0 +1,34 @@ +#ifndef HSOLVER_DIAGO_DDLA_H +#define HSOLVER_DIAGO_DDLA_H + +#if defined(__DDLA) && defined(__MPI) +#include "source_base/matrix_block.h" +#include "source_psi/psi.h" + +namespace hsolver +{ +/** Optional distributed H C = S C e solver. All SDK state is local to diag(). + * H and S are preserved. The lowest bands eigenvectors are returned in the + * existing host Psi layout; consumers need no DDLA context or buffer types. + */ +template +class DiagoDdla +{ + public: + DiagoDdla(int order, int bands, bool use_gpu) : order_(order), bands_(bands), use_gpu_(use_gpu) + { + } + + void diag(ModuleBase::MatrixBlock& h, + ModuleBase::MatrixBlock& s, + psi::Psi& vectors, + double* eigenvalues); + + private: + const int order_; + const int bands_; + const bool use_gpu_; +}; +} // namespace hsolver +#endif +#endif diff --git a/source/source_hsolver/hsolver_lcao.h b/source/source_hsolver/hsolver_lcao.h index 3baeab16e03..e960db93cbd 100644 --- a/source/source_hsolver/hsolver_lcao.h +++ b/source/source_hsolver/hsolver_lcao.h @@ -74,7 +74,7 @@ class HSolverLCAO const int nlocal; // global dimension of the NAO Hamiltonian const int nbands; // number of bands to be solved for const double nelec; // total number of electrons for density normalization and PEXSI - const bool use_gpu; // true if running on GPU, only used by the native-ELPA branch + const bool use_gpu; // true if running on GPU, passed to the selected diagonalizer const int world_nproc; const int world_rank; const double overlap_cutoff; diff --git a/source/source_hsolver/unittests/CMakeLists.txt b/source/source_hsolver/unittests/CMakeLists.txt index bc8ffc0c765..01e58fa6242 100644 --- a/source/source_hsolver/unittests/CMakeLists.txt +++ b/source/source_hsolver/unittests/CMakeLists.txt @@ -173,6 +173,16 @@ if(ENABLE_MPI) ../diago_lapack.cpp ${_dsp_cpack_fallback_src}) set(lcao_sk_cut_test_libs parameter base psi device) + if(ENABLE_DDLA) + list(APPEND lcao_sk_cut_test_sources ../diago_ddla.cpp) + list(APPEND lcao_sk_cut_test_libs ddla::ddla) + addtest( + TARGET MODULE_HSOLVER_diago_ddla + LIBS parameter base psi device ddla::ddla + KEEP_FEATURE_DEFINITIONS __CUDA __ROCM + SOURCES test_diago_ddla.cpp ../diago_ddla.cpp ../diago_lapack.cpp + ${_dsp_cpack_fallback_src}) + endif() if(TARGET ELPA::ELPA) list(APPEND lcao_sk_cut_test_sources ../diago_elpa.cpp ../diago_elpa_native.cpp) diff --git a/source/source_hsolver/unittests/test_diag_sk_cut.cpp b/source/source_hsolver/unittests/test_diag_sk_cut.cpp index f532fdfbb0f..ae60176dad9 100644 --- a/source/source_hsolver/unittests/test_diag_sk_cut.cpp +++ b/source/source_hsolver/unittests/test_diag_sk_cut.cpp @@ -346,6 +346,15 @@ TEST(DiagSkCut, ReducedRankSmallerThanProcessGrid) check_projection("elpa", true, phase, 2, 2); } #endif +#ifdef __DDLA +TEST(DiagSkCut, DdlaProjectedSolve) +{ + const std::complex phase(0.0, 1.0); + check_projection("ddla", true, 1.0, 2, 1); + check_projection>("ddla", true, phase, 2, 1); + check_projection>("ddla", true, phase, 1, 2); +} +#endif } // namespace int main(int argc, char** argv) diff --git a/source/source_hsolver/unittests/test_diago_ddla.cpp b/source/source_hsolver/unittests/test_diago_ddla.cpp new file mode 100644 index 00000000000..4951b4a62f1 --- /dev/null +++ b/source/source_hsolver/unittests/test_diago_ddla.cpp @@ -0,0 +1,209 @@ +#include "source_hsolver/diago_ddla.h" +#include "source_hsolver/diago_lapack.h" +#include "source_base/parallel_blkcyc.h" +#include "source_base/parallel_comm.h" +#include "source_base/parallel_device.h" +#include "source_base/parallel_global.h" + +#include + +#include +#include +#include +#include +#include +#include +#include + +namespace +{ +template +T conjugate(T value) +{ + return value; +} + +template <> +std::complex conjugate(std::complex value) +{ + return std::conj(value); +} + +template +void check_solve(int order, int bands, int block, int padding, bool swap_grid, bool gpu, T phase) +{ + Parallel_Blkcyclic layout; + layout.init(order, order, block, MPI_COMM_WORLD, swap_grid); + Parallel_Blkcyclic output; + output.bind_descriptor(order, bands, block, layout.ictxt()); + const int rows = layout.nrloc(); + const int cols = layout.ncloc(); + int desc[9]; + const int* layout_desc = layout.desc(); + const int* layout_desc_end = layout_desc + 9; + std::copy(layout_desc, layout_desc_end, desc); + desc[8] += padding; + const int leading = desc[8]; + const std::size_t storage = static_cast(leading) * std::max(1, cols); + const double nan = std::numeric_limits::quiet_NaN(); + const T poison = T(nan); + std::vector h(storage, poison); + std::vector s(storage, poison); + std::vector h_full(order * order); + std::vector s_full(order * order); + for (int j = 0; j < order; ++j) + { + for (int i = 0; i <= j; ++i) + { + T hv = T(i + 1.0); + T sv = T(2.0 + i * 0.1); + if (i != j) + { + hv = 0.13 * phase / double(j - i); + sv = 0.04 * conjugate(phase) / double(j - i); + } + h_full[i + j * order] = hv; + h_full[j + i * order] = conjugate(hv); + s_full[i + j * order] = sv; + s_full[j + i * order] = conjugate(sv); + } + } + // A replicated LAPACK solve supplies an independent eigenvalue reference. + std::vector reference_vectors(order * bands); + psi::Psi reference_psi(reference_vectors.data(), 1, bands, order, order, true); + const std::size_t global_order = order; + ModuleBase::MatrixBlock h_ref{h_full.data(), global_order, global_order, nullptr}; + ModuleBase::MatrixBlock s_ref{s_full.data(), global_order, global_order, nullptr}; + std::vector reference_values(bands); + hsolver::DiagoLapack lapack(order, bands); + lapack.diag(h_ref, s_ref, reference_psi, reference_values.data()); + + const std::size_t local_rows = rows; + const std::size_t local_cols = cols; + ModuleBase::MatrixBlock hk{h.data(), local_rows, local_cols, desc}; + ModuleBase::MatrixBlock sk{s.data(), local_rows, local_cols, desc}; + const std::size_t output_storage = std::max(1, output.size()); + std::vector vectors(output_storage); + const int output_bands = output.ncloc(); + psi::Psi psi(vectors.data(), 1, output_bands, rows, rows, true); + std::vector values(bands); + hsolver::DiagoDdla solver(order, bands, gpu); + for (int repeat = 0; repeat < 2; ++repeat) + { + const double shift = repeat * 0.3; + for (int j = 0; j < cols; ++j) + { + const int gj = layout.icglb(j); + for (int i = 0; i < rows; ++i) + { + const int gi = layout.irglb(i); + if (gi <= gj) + { + const int global = gi + gj * order; + h[i + j * leading] = h_full[global] + shift * s_full[global]; + s[i + j * leading] = s_full[global]; + } + } + } + const std::vector h_before = h; + const std::vector s_before = s; + solver.diag(hk, sk, psi, values.data()); + const std::size_t bytes = storage * sizeof(T); + EXPECT_EQ(std::memcmp(h.data(), h_before.data(), bytes), 0); + EXPECT_EQ(std::memcmp(s.data(), s_before.data(), bytes), 0); + for (int b = 0; b < bands; ++b) + { + const double expected_value = reference_values[b] + shift; + EXPECT_NEAR(values[b], expected_value, 1e-10); + } + std::vector full_vectors(order * bands); + for (int j = 0; j < output.ncloc(); ++j) + { + const int gj = output.icglb(j); + for (int i = 0; i < rows; ++i) + { + const int gi = output.irglb(i); + full_vectors[gi + gj * order] = vectors[i + j * rows]; + } + } + const int count = order * bands; + Parallel_Common::reduce_data(full_vectors.data(), count, MPI_COMM_WORLD); + for (int b = 0; b < bands; ++b) + { + for (int i = 0; i < order; ++i) + { + T residual = T(0); + for (int j = 0; j < order; ++j) + { + residual += (h_full[i + j * order] + (shift - values[b]) * s_full[i + j * order]) + * full_vectors[j + b * order]; + } + EXPECT_LT(std::abs(residual), 1e-9); + } + for (int a = 0; a < bands; ++a) + { + T overlap = T(0); + for (int i = 0; i < order; ++i) + { + for (int j = 0; j < order; ++j) + { + overlap += conjugate(full_vectors[i + a * order]) * s_full[i + j * order] + * full_vectors[j + b * order]; + } + } + const T expected = a == b ? T(1) : T(0); + const T difference = overlap - expected; + EXPECT_LT(std::abs(difference), 1e-9); + } + } + } +} + +TEST(DiagoDdla, RealPartial) +{ + check_solve(7, 3, 2, 0, false, false, 1.0); +} +TEST(DiagoDdla, ComplexPartialPaddedSwappedGrid) +{ + const std::complex phase(0.6, 0.8); + check_solve>(7, 3, 1, 3, true, false, phase); +} +TEST(DiagoDdla, EmptyLocalBlocks) +{ + check_solve(2, 1, 16, 0, false, false, 1.0); +} +TEST(DiagoDdla, FullSpectrum) +{ + const std::complex phase(0.6, 0.8); + check_solve>(7, 7, 2, 0, false, false, phase); +} +TEST(DiagoDdla, GpuRealAndComplex) +{ + if (std::getenv("ABACUS_TEST_DDLA_GPU") == nullptr) + { + GTEST_SKIP() << "Set ABACUS_TEST_DDLA_GPU inside a GPU allocation"; + } + check_solve(7, 3, 2, 0, false, true, 1.0); + const std::complex phase(0.6, 0.8); + check_solve>(7, 3, 1, 3, true, true, phase); + check_solve(2, 1, 16, 0, false, true, 1.0); +} +} // namespace + +int main(int argc, char** argv) +{ + int nproc = 0; + int nthread = 0; + int rank = 0; + Parallel_Global::read_pal_param(argc, argv, nproc, nthread, rank); + POOL_WORLD = MPI_COMM_NULL; + KP_WORLD = MPI_COMM_NULL; + INT_BGROUP = MPI_COMM_NULL; + BP_WORLD = MPI_COMM_NULL; + GRID_WORLD = MPI_COMM_NULL; + DIAG_WORLD = MPI_COMM_NULL; + testing::InitGoogleTest(&argc, argv); + const int result = RUN_ALL_TESTS(); + Parallel_Global::finalize_mpi(); + return result; +} diff --git a/source/source_io/module_parameter/read_inp_estruc.cpp b/source/source_io/module_parameter/read_inp_estruc.cpp index ce58dd9caab..7965a3fed8e 100644 --- a/source/source_io/module_parameter/read_inp_estruc.cpp +++ b/source/source_io/module_parameter/read_inp_estruc.cpp @@ -37,11 +37,12 @@ void ReadInput::item_elec_stru() // Electronic Structure { Input_Item item("ks_solver"); - item.domain.set_choices({"cg", "dav", "bpcg", "dav_subspace", "lobpcg", "genelpa", "elpa", "lapack", "scalapack_gvx", "cusolver", "cusolvermp", "pexsi"}); + item.domain.set_choices({"cg", "dav", "bpcg", "dav_subspace", "lobpcg", "genelpa", "elpa", "lapack", "scalapack_gvx", "cusolver", "cusolvermp", "pexsi", "ddla"}); item.domain.add_requirement("basis_type==pw", "ks_solver in [cg, dav, bpcg, dav_subspace, lobpcg]"); - item.domain.add_requirement("basis_type==lcao", "ks_solver in [genelpa, elpa, lapack, scalapack_gvx, cusolver, cusolvermp, pexsi]"); + item.domain.add_requirement("basis_type==lcao", "ks_solver in [genelpa, elpa, lapack, scalapack_gvx, cusolver, cusolvermp, pexsi, ddla]"); item.domain.add_requirement("ks_solver==genelpa", "device==cpu"); - item.annotation = "cg; dav; dav_subspace; bpcg; lobpcg; lapack; genelpa; elpa; scalapack_gvx; cusolver; cusolvermp; pexsi"; + item.domain.add_requirement("ks_solver==ddla", "kpar==1"); + item.annotation = "cg; dav; dav_subspace; bpcg; lobpcg; lapack; genelpa; elpa; scalapack_gvx; cusolver; cusolvermp; pexsi; ddla"; item.category = "Electronic structure"; item.type = "String"; item.description = R"(Choose the diagonalization methods for the Hamiltonian matrix expanded in a certain basis set. @@ -59,6 +60,7 @@ For numerical atomic orbitals basis, * lapack: Use LAPACK to diagonalize the Hamiltonian, only used for serial version * genelpa: Use the CPU-only GEN-ELPA interface to diagonalize the Hamiltonian. * scalapack_gvx: Use Scalapack to diagonalize the Hamiltonian. +* ddla: Use the optional LibDDLA distributed generalized eigensolver (ENABLE_DDLA=ON, LibDDLA >= 0.0.7, MPI and LCAO). Supports real and complex matrices. Select device=cpu or device=gpu with a matching CPU/GPU-enabled LibDDLA installation. GPU execution requires a GPU-enabled ABACUS build and one MPI rank per visible GPU. This initial implementation requires kpar=1 and returns wavefunctions to the existing host density workflow. Native defaults are unchanged. * cusolver: Use CUSOLVER to diagonalize the Hamiltonian, at least one GPU is needed. * cusolvermp: Use CUSOLVER to diagonalize the Hamiltonian, supporting multi-GPU devices. Note that you should set the number of MPI processes equal to the number of GPUs. * elpa: The ELPA solver supports both CPU and GPU. By setting the `device` to GPU, you can launch the ELPA solver with GPU acceleration (provided that you have installed a GPU-supported version of ELPA, which requires you to manually compile and install ELPA, and the ABACUS should be compiled with -DENABLE_ELPA=ON and -DUSE_CUDA=ON). The ELPA solver also supports multi-GPU acceleration. @@ -176,6 +178,14 @@ Then the user has to correct the input file and restart the calculation.)"; #endif } } + else if (ks_solver == "ddla") + { +#if !defined(__DDLA) || !defined(__MPI) + ModuleBase::WARNING_QUIT("ReadInput", + "ks_solver ddla requires a build with ENABLE_DDLA=ON. " + "Choose a native eigensolver or rebuild with LibDDLA >= 0.0.7."); +#endif + } else if (ks_solver == "pexsi") { #ifdef __PEXSI diff --git a/tests/02_NAO_Gamma/CASES_DDLA.txt b/tests/02_NAO_Gamma/CASES_DDLA.txt new file mode 100644 index 00000000000..b50b06b4f26 --- /dev/null +++ b/tests/02_NAO_Gamma/CASES_DDLA.txt @@ -0,0 +1 @@ +scf_force_stress_ddla diff --git a/tests/02_NAO_Gamma/CMakeLists.txt b/tests/02_NAO_Gamma/CMakeLists.txt index 53189cb1764..980e6e41462 100644 --- a/tests/02_NAO_Gamma/CMakeLists.txt +++ b/tests/02_NAO_Gamma/CMakeLists.txt @@ -12,3 +12,14 @@ else() COMMAND ${BASH} ../integrate/Autotest.sh -a ${ABACUS_BIN_PATH} -n 4 -j auto WORKING_DIRECTORY ${ABACUS_TEST_DIR}/02_NAO_Gamma) endif() + +if(ENABLE_DDLA) + add_test( + NAME 02_NAO_Gamma_ddla + COMMAND ${BASH} ../integrate/Autotest.sh -a ${ABACUS_BIN_PATH} -n 2 -o 1 -j + 1 -f CASES_DDLA.txt + WORKING_DIRECTORY ${ABACUS_TEST_DIR}/02_NAO_Gamma) + set_tests_properties( + 02_NAO_Gamma_ddla PROPERTIES PROCESSORS 2 ENVIRONMENT + "OMP_NUM_THREADS=1;OPENBLAS_NUM_THREADS=1") +endif() diff --git a/tests/02_NAO_Gamma/scf_force_stress_ddla/INPUT b/tests/02_NAO_Gamma/scf_force_stress_ddla/INPUT new file mode 100644 index 00000000000..2d9ce62d9b6 --- /dev/null +++ b/tests/02_NAO_Gamma/scf_force_stress_ddla/INPUT @@ -0,0 +1,33 @@ +INPUT_PARAMETERS +#Parameters (1.General) +suffix autotest +calculation scf + +nbands 6 +symmetry 1 +pseudo_dir ../../PP_ORB +orbital_dir ../../PP_ORB +gamma_only 1 + +#Parameters (2.Iteration) +ecutwfc 20 +scf_thr 1e-8 +scf_nmax 100 + +#Parameters (3.Basis) +basis_type lcao + +#Parameters (4.Smearing) +smearing_method gauss +smearing_sigma 0.002 + +#Parameters (5.Mixing) +mixing_type broyden +mixing_beta 0.4 + + +cal_force 1 +cal_stress 1 +ks_solver ddla +kpar 1 +device cpu diff --git a/tests/02_NAO_Gamma/scf_force_stress_ddla/KPT b/tests/02_NAO_Gamma/scf_force_stress_ddla/KPT new file mode 100644 index 00000000000..c289c0158aa --- /dev/null +++ b/tests/02_NAO_Gamma/scf_force_stress_ddla/KPT @@ -0,0 +1,4 @@ +K_POINTS +0 +Gamma +1 1 1 0 0 0 diff --git a/tests/02_NAO_Gamma/scf_force_stress_ddla/README b/tests/02_NAO_Gamma/scf_force_stress_ddla/README new file mode 100644 index 00000000000..2b6889a2221 --- /dev/null +++ b/tests/02_NAO_Gamma/scf_force_stress_ddla/README @@ -0,0 +1,3 @@ +CPU LibDDLA eigensolver regression; requires ENABLE_DDLA=ON and MPI. +Same physical input and reference as ../scf_force_stress. +DDLA returns host wavefunctions to the unchanged density/force/stress workflow. diff --git a/tests/02_NAO_Gamma/scf_force_stress_ddla/STRU b/tests/02_NAO_Gamma/scf_force_stress_ddla/STRU new file mode 100644 index 00000000000..ff825ccafe8 --- /dev/null +++ b/tests/02_NAO_Gamma/scf_force_stress_ddla/STRU @@ -0,0 +1,22 @@ +ATOMIC_SPECIES +Si 14 Si_ONCV_PBE-1.0.upf upf201 + +NUMERICAL_ORBITAL +Si_gga_8au_60Ry_2s2p1d.orb + +LATTICE_CONSTANT +20 // add lattice constant + +LATTICE_VECTORS +0.0 0.5 0.5 +0.5 0.0 0.5 +0.5 0.5 0.0 + +ATOMIC_POSITIONS +Direct + +Si // Element type +0.0 // magnetism +2 +0.00 0.00 0.00 1 1 1 +0.25 0.27 0.25 1 1 1 diff --git a/tests/02_NAO_Gamma/scf_force_stress_ddla/result.ref b/tests/02_NAO_Gamma/scf_force_stress_ddla/result.ref new file mode 100644 index 00000000000..98108b81ac8 --- /dev/null +++ b/tests/02_NAO_Gamma/scf_force_stress_ddla/result.ref @@ -0,0 +1,8 @@ +etotref -204.1266249420426000 +etotperatomref -102.0633124710 +totalforceref 0.793772 +totalstressref 16.211881 +pointgroupref C_1h +spacegroupref C_2h +nksibzref 1 +totaltimeref +0.61787 diff --git a/tests/03_NAO_multik/CASES_DDLA.txt b/tests/03_NAO_multik/CASES_DDLA.txt new file mode 100644 index 00000000000..5b68d4ed6e7 --- /dev/null +++ b/tests/03_NAO_multik/CASES_DDLA.txt @@ -0,0 +1 @@ +scf_angle_spin4_ddla diff --git a/tests/03_NAO_multik/CMakeLists.txt b/tests/03_NAO_multik/CMakeLists.txt index c881d60dd0d..686e58a9bdc 100644 --- a/tests/03_NAO_multik/CMakeLists.txt +++ b/tests/03_NAO_multik/CMakeLists.txt @@ -12,3 +12,14 @@ else() COMMAND ${BASH} ../integrate/Autotest.sh -a ${ABACUS_BIN_PATH} -n 4 -j auto WORKING_DIRECTORY ${ABACUS_TEST_DIR}/03_NAO_multik) endif() + +if(ENABLE_DDLA) + add_test( + NAME 03_NAO_multik_ddla + COMMAND ${BASH} ../integrate/Autotest.sh -a ${ABACUS_BIN_PATH} -n 2 -o 1 -j + 1 -f CASES_DDLA.txt + WORKING_DIRECTORY ${ABACUS_TEST_DIR}/03_NAO_multik) + set_tests_properties( + 03_NAO_multik_ddla PROPERTIES PROCESSORS 2 ENVIRONMENT + "OMP_NUM_THREADS=1;OPENBLAS_NUM_THREADS=1") +endif() diff --git a/tests/03_NAO_multik/scf_angle_spin4_ddla/INPUT b/tests/03_NAO_multik/scf_angle_spin4_ddla/INPUT new file mode 100644 index 00000000000..03809a4049b --- /dev/null +++ b/tests/03_NAO_multik/scf_angle_spin4_ddla/INPUT @@ -0,0 +1,35 @@ +INPUT_PARAMETERS +#Parameters (1.General) +suffix autotest +calculation scf + +symmetry 0 +pseudo_dir ../../PP_ORB +orbital_dir ../../PP_ORB +gamma_only 0 + +#Parameters (2.Iteration) +ecutwfc 15 +scf_thr 1e-7 +scf_nmax 50 + +noncolin 1 +nspin 4 +#Parameters (3.Basis) +basis_type lcao + +#Parameters (4.Smearing) +smearing_method gauss +smearing_sigma 0.02 + +#Parameters (5.Mixing) +mixing_type broyden +mixing_beta 0.7 +mixing_ndim 15 +mixing_gg0 1.0 + +cal_force 1 +cal_stress 1 +ks_solver ddla +kpar 1 +device cpu diff --git a/tests/03_NAO_multik/scf_angle_spin4_ddla/KPT b/tests/03_NAO_multik/scf_angle_spin4_ddla/KPT new file mode 100644 index 00000000000..230094748bd --- /dev/null +++ b/tests/03_NAO_multik/scf_angle_spin4_ddla/KPT @@ -0,0 +1,4 @@ +K_POINTS +0 +Gamma +1 2 1 0 0 0 diff --git a/tests/03_NAO_multik/scf_angle_spin4_ddla/README b/tests/03_NAO_multik/scf_angle_spin4_ddla/README new file mode 100644 index 00000000000..d448569aa5c --- /dev/null +++ b/tests/03_NAO_multik/scf_angle_spin4_ddla/README @@ -0,0 +1,3 @@ +CPU LibDDLA eigensolver regression; requires ENABLE_DDLA=ON and MPI. +Same physical input and reference as ../scf_angle_spin4. +DDLA returns host wavefunctions to the unchanged density/force/stress workflow. diff --git a/tests/03_NAO_multik/scf_angle_spin4_ddla/STRU b/tests/03_NAO_multik/scf_angle_spin4_ddla/STRU new file mode 100644 index 00000000000..1df1e78433d --- /dev/null +++ b/tests/03_NAO_multik/scf_angle_spin4_ddla/STRU @@ -0,0 +1,22 @@ +ATOMIC_SPECIES +Fe 1.000 Fe_ONCV_PBE-1.0.upf + +NUMERICAL_ORBITAL +Fe_gga_6au_100Ry_4s2p2d1f.orb + +LATTICE_CONSTANT +7 + +LATTICE_VECTORS +1.00 0.00 0.00 +0.00 1.00 0.00 +0.00 0.00 1.00 + +ATOMIC_POSITIONS +Direct + +Fe +0.0 +2 +0.00 0.00 0.00 1 1 1 mag 1.0 angle1 90 angle2 0 +0.50 0.50 0.50 1 1 1 mag 1.0 angle1 90 angle2 180 diff --git a/tests/03_NAO_multik/scf_angle_spin4_ddla/result.ref b/tests/03_NAO_multik/scf_angle_spin4_ddla/result.ref new file mode 100644 index 00000000000..ed188b3fc21 --- /dev/null +++ b/tests/03_NAO_multik/scf_angle_spin4_ddla/result.ref @@ -0,0 +1,5 @@ +etotref -6267.4651674420470000 +etotperatomref -3133.7325837210 +totalforceref 0.000000 +totalstressref 3912.961117 +totaltimeref 15.08 From 4dec9ac9b483584f448569073c1c07cc861a3d20 Mon Sep 17 00:00:00 2001 From: userchba <1075002153@qq.com> Date: Thu, 3 Dec 2026 14:22:34 +0800 Subject: [PATCH 2/2] chore: drop unrelated CMake formatting churn Restore the upstream formatting of the ENABLE_CNPY block so the ENABLE_DDLA diff stays focused. --- CMakeLists.txt | 20 ++++++-------------- 1 file changed, 6 insertions(+), 14 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 4d650c9de5c..4908bd81701 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -552,9 +552,7 @@ endif() if(ENABLE_CNPY) # Allow user to specify a local CNPY installation root (containing include/ and lib/). - set(CNPY_DIR - "" - CACHE PATH "Path to local CNPY installation root") + set(CNPY_DIR "" CACHE PATH "Path to local CNPY installation root") find_path(cnpy_SOURCE_DIR cnpy.h HINTS ${CNPY_DIR}/include) if(NOT cnpy_SOURCE_DIR) include(FetchContent) @@ -569,20 +567,14 @@ if(ENABLE_CNPY) # (i.e. the find_path branch above found a pre-built library), create an # imported target so that downstream linking works without FetchContent. if(CNPY_DIR AND NOT TARGET cnpy) - find_library( - cnpy_LIBRARY - NAMES cnpy - HINTS ${CNPY_DIR}/lib) + find_library(cnpy_LIBRARY NAMES cnpy HINTS ${CNPY_DIR}/lib) if(NOT cnpy_LIBRARY) - message( - FATAL_ERROR - "CNPY_DIR is set to '${CNPY_DIR}' but no libcnpy was found under its lib directory." - ) + message(FATAL_ERROR "CNPY_DIR is set to '${CNPY_DIR}' but no libcnpy was found under its lib directory.") endif() add_library(cnpy UNKNOWN IMPORTED) - set_target_properties( - cnpy PROPERTIES IMPORTED_LOCATION "${cnpy_LIBRARY}" - INTERFACE_INCLUDE_DIRECTORIES "${cnpy_SOURCE_DIR}") + set_target_properties(cnpy PROPERTIES + IMPORTED_LOCATION "${cnpy_LIBRARY}" + INTERFACE_INCLUDE_DIRECTORIES "${cnpy_SOURCE_DIR}") endif() # find ZLIB and link