From 12243502ebfdecc45962aa6b7d355eb9b09107ac Mon Sep 17 00:00:00 2001 From: Jiacheng Xu <169599847+Stardust0831@users.noreply.github.com> Date: Thu, 19 Nov 2026 02:30:18 +0800 Subject: [PATCH 1/4] feat(xc): evaluate Libxc LDA and GGA potentials on CUDA --- docs/advanced/install.md | 8 + source/CMakeLists.txt | 5 + source/source_estate/module_pot/pot_xc.cpp | 3 +- source/source_estate/module_pot/pot_xc.h | 4 +- .../module_pot/potential_types.cpp | 4 +- source/source_hamilt/module_xc/libxc_abacus.h | 31 +++ source/source_hamilt/module_xc/libxc_host.h | 19 ++ source/source_hamilt/module_xc/libxc_pot.cpp | 179 ++++++++++++++---- .../source_hamilt/module_xc/libxc_setup.cpp | 34 +++- .../module_xc/test/CMakeLists.txt | 3 +- .../module_xc/unittests/test_libxc_pot.cpp | 104 ++++++++++ .../{test => unittests}/test_libxc_tools.cpp | 11 +- .../source_hamilt/module_xc/xc_functional.h | 12 ++ source/source_hamilt/module_xc/xc_pot.cpp | 18 +- .../module_parameter/input_parameter.h | 1 + .../module_parameter/read_inp_estruc.cpp | 23 +++ .../unittests/test_read_inp_estruc.cpp | 27 +++ source/source_io/test_serial/CMakeLists.txt | 3 +- .../source_lcao/module_lr/test/CMakeLists.txt | 2 +- .../{test => unittests}/test_xc_kxc.cpp | 4 +- .../source_lcao/module_rdmft/rdmft_tools.cpp | 4 +- .../module_dfpt/dfpt_xc_analytic.cpp | 4 +- .../module_dfpt/test_serial/CMakeLists.txt | 2 +- .../test_dfpt_xc_analytic.cpp} | 12 +- 24 files changed, 453 insertions(+), 64 deletions(-) create mode 100644 source/source_hamilt/module_xc/libxc_host.h create mode 100644 source/source_hamilt/module_xc/unittests/test_libxc_pot.cpp rename source/source_hamilt/module_xc/{test => unittests}/test_libxc_tools.cpp (97%) create mode 100644 source/source_io/module_parameter/unittests/test_read_inp_estruc.cpp rename source/source_lcao/module_lr/{test => unittests}/test_xc_kxc.cpp (94%) rename source/source_pw/module_dfpt/{test_serial/dfpt_xc_analytic_serial_test.cpp => unittests/test_dfpt_xc_analytic.cpp} (97%) diff --git a/docs/advanced/install.md b/docs/advanced/install.md index 86d63cfd1ee..aa9725b2458 100644 --- a/docs/advanced/install.md +++ b/docs/advanced/install.md @@ -23,6 +23,14 @@ Dependency: [Libxc](https://tddft.org/programs/libxc/) >= 5.1.7 . cmake -B build -DENABLE_LIBXC=ON ``` +For CUDA evaluation of the Libxc LDA/GGA potential (including the semilocal part +of HSE), build Libxc 7.1 or later with `-DENABLE_CUDA=ON`. Configure ABACUS with +`-DUSE_CUDA=ON -DENABLE_LIBXC=ON -DLibxc_DIR=/lib/cmake/Libxc`, then set +`device gpu` and `xc_device gpu` in INPUT. `xc_device` defaults to `cpu`, which +also works with a CUDA-enabled Libxc. Built-in functionals, meta-GGA, response +kernels and separate force/stress evaluations continue to use the CPU. This +option does not select the exact-exchange backend. + ## Build with ML-ALGO If machine learning algorithms, including DeePKS and machine learning based kinetic energy density functional (ML-KEDF) for OFDFT, is required for DFT calculation, the following prerequisites and steps are needed: diff --git a/source/CMakeLists.txt b/source/CMakeLists.txt index ae735fb8d83..e40db8067e8 100644 --- a/source/CMakeLists.txt +++ b/source/CMakeLists.txt @@ -69,6 +69,11 @@ set(_abacus_feature_definitions $<$:__DFTD4> $<$:GATHER_INFO>) +if(USE_CUDA AND ENABLE_LIBXC AND Libxc_ENABLE_CUDA AND + Libxc_VERSION VERSION_GREATER_EQUAL "7.1.0") + list(APPEND _abacus_feature_definitions ABACUS_LIBXC_CUDA) +endif() + if(ABACUS_COMMIT_INFO_INCLUDE_DIR) list(APPEND _abacus_feature_definitions COMMIT_INFO) endif() diff --git a/source/source_estate/module_pot/pot_xc.cpp b/source/source_estate/module_pot/pot_xc.cpp index a4e2061699c..2a21d4384f4 100644 --- a/source/source_estate/module_pot/pot_xc.cpp +++ b/source/source_estate/module_pot/pot_xc.cpp @@ -57,7 +57,8 @@ void PotXC::cal_v_eff(const Charge*const chg, const UnitCell*const ucell, Module domag_z_, gga_grad_, hybrid_alpha, - hse_omega); + hse_omega, + xc_gpu_); *(this->etxc_) = std::get<0>(etxc_vtxc_v); *(this->vtxc_) = std::get<1>(etxc_vtxc_v); v_eff += std::get<2>(etxc_vtxc_v); diff --git a/source/source_estate/module_pot/pot_xc.h b/source/source_estate/module_pot/pot_xc.h index 50737e3ec95..4377cfbfa45 100644 --- a/source/source_estate/module_pot/pot_xc.h +++ b/source/source_estate/module_pot/pot_xc.h @@ -14,12 +14,13 @@ class PotXC : public PotBase PotXC(const bool domag, const bool domag_z, const int gga_grad, + const bool xc_gpu, const ModulePW::PW_Basis* rho_basis_in, double* etxc_in, double* vtxc_in, ModuleBase::matrix* vofk_in = nullptr) : vofk(vofk_in), etxc_(etxc_in), vtxc_(vtxc_in), - domag_(domag), domag_z_(domag_z), gga_grad_(gga_grad) + domag_(domag), domag_z_(domag_z), gga_grad_(gga_grad), xc_gpu_(xc_gpu) { this->rho_basis_ = rho_basis_in; this->dynamic_mode = true; @@ -35,6 +36,7 @@ class PotXC : public PotBase const bool domag_; const bool domag_z_; const int gga_grad_; + const bool xc_gpu_; }; } // namespace elecstate diff --git a/source/source_estate/module_pot/potential_types.cpp b/source/source_estate/module_pot/potential_types.cpp index b93c59345c4..bb8fa652a0b 100644 --- a/source/source_estate/module_pot/potential_types.cpp +++ b/source/source_estate/module_pot/potential_types.cpp @@ -56,7 +56,9 @@ PotBase* Potential::get_pot_type(const std::string& pot_type, const Input_para& } else if (pot_type == "xc") { - return new PotXC(PARAM.globalv.domag, PARAM.globalv.domag_z, input.gga_grad, this->rho_basis_, this->etxc_, this->vtxc_, &(this->vofk_eff)); + const bool xc_gpu = input.xc_device == "gpu"; + return new PotXC(PARAM.globalv.domag, PARAM.globalv.domag_z, input.gga_grad, xc_gpu, + this->rho_basis_, this->etxc_, this->vtxc_, &(this->vofk_eff)); } else if (pot_type == "pcc" || pot_type == "pcc_2d") { diff --git a/source/source_hamilt/module_xc/libxc_abacus.h b/source/source_hamilt/module_xc/libxc_abacus.h index a6846c3c0b7..459267a7eca 100644 --- a/source/source_hamilt/module_xc/libxc_abacus.h +++ b/source/source_hamilt/module_xc/libxc_abacus.h @@ -69,6 +69,22 @@ namespace XC_Functional_Libxc const double hybrid_alpha, const double hse_omega); + extern std::vector init_func( + const std::vector &func_id, + const int xc_polarized, + const double hybrid_alpha, + const double hse_omega, + const bool use_gpu); + + // Host arrays in/out; the bounded CUDA workspace is released on return. + void xc_cuda_exc_vxc(xc_func_type& func, + const int nrxx, + const double* rho, + const double* sigma, + double* exc, + double* vrho, + double* vsigma); + /** * @brief Per-component external parameters that init_func would apply. * @@ -105,6 +121,21 @@ namespace XC_Functional_Libxc const double hybrid_alpha, const double hse_omega); + extern std::tuple v_xc_libxc( + const std::vector &func_id, + const int &nrxx, // number of real-space grid + const double &omega, // volume of cell + const double tpiba, + const Charge* const chr, // charge density + const int nspin, + const bool domag, + const bool domag_z, + const int gga_grad, + const std::map* scaling_factor, + const double hybrid_alpha, + const double hse_omega, + const bool use_gpu); + // Reciprocal-metric derivative of the exact gga_grad=2 Libxc energy // graph. The returned lower-triangular tensor is the unnormalized local // grid sum; Stress_Func performs the pool reduction and divides by nxyz. diff --git a/source/source_hamilt/module_xc/libxc_host.h b/source/source_hamilt/module_xc/libxc_host.h new file mode 100644 index 00000000000..261356e130b --- /dev/null +++ b/source/source_hamilt/module_xc/libxc_host.h @@ -0,0 +1,19 @@ +#ifndef LIBXC_HOST_H +#define LIBXC_HOST_H + +#include + +namespace XC_Functional_Libxc +{ +// A CUDA-enabled Libxc defaults to device pointers. Keep host callers explicit. +inline int init_func_host(xc_func_type* func, const int id, const int spin) +{ +#ifdef XC_FLAGS_ON_HOST + return xc_func_init_flags(func, id, spin, XC_FLAGS_ON_HOST); +#else + return xc_func_init(func, id, spin); +#endif +} +} // namespace XC_Functional_Libxc + +#endif diff --git a/source/source_hamilt/module_xc/libxc_pot.cpp b/source/source_hamilt/module_xc/libxc_pot.cpp index 361cdca5914..b15508e82bd 100644 --- a/source/source_hamilt/module_xc/libxc_pot.cpp +++ b/source/source_hamilt/module_xc/libxc_pot.cpp @@ -6,6 +6,7 @@ #include "source_base/parallel_reduce.h" #include "source_base/timer.h" #include "source_base/tool_title.h" +#include "source_base/module_device/memory_op.h" #ifdef __EXX #include "source_hamilt/module_xc/exx_info.h" #endif @@ -14,6 +15,78 @@ #include #include +#include + +void XC_Functional_Libxc::xc_cuda_exc_vxc(xc_func_type& func, + const int nrxx, + const double* rho, + const double* sigma, + double* exc, + double* vrho, + double* vsigma) +{ + ModuleBase::timer::start("Libxc", "xc_cuda_exc_vxc"); +#if defined(__CUDA) && defined(ABACUS_LIBXC_CUDA) + if (nrxx > 0) + { + const bool is_lda = func.info->family == XC_FAMILY_LDA; + const bool is_gga = func.info->family == XC_FAMILY_GGA || func.info->family == XC_FAMILY_HYB_GGA; + if (!is_lda && !is_gga) + { + throw std::domain_error("Libxc CUDA evaluation supports LDA and GGA potentials only."); + } + // At most 5.5 MiB per rank for polarized GGA; Libxc's mix workspace + // is also bounded by this batch size. No grid-sized cache is retained. + const int capacity = std::min(nrxx, 65536); + const int nspin = func.dim.rho; + const int nsigma = func.dim.sigma; + const std::size_t rho_count = static_cast(capacity) * nspin; + const std::size_t sigma_count = static_cast(capacity) * nsigma; + const std::size_t count = 2 * rho_count + 2 * sigma_count + capacity; + double* buffer = nullptr; + resmem_dd_op()(buffer, count); + std::unique_ptr workspace(buffer); + double* d_rho = buffer; + double* d_sigma = d_rho + rho_count; + double* d_exc = d_sigma + sigma_count; + double* d_vrho = d_exc + capacity; + double* d_vsigma = d_vrho + rho_count; + for (int start = 0; start < nrxx;) + { + const int remaining = nrxx - start; + const int points = std::min(capacity, remaining); + const std::size_t rho_offset = static_cast(start) * nspin; + const std::size_t sigma_offset = static_cast(start) * nsigma; + const std::size_t nrho = static_cast(points) * nspin; + const std::size_t nsig = static_cast(points) * nsigma; + const double* rho_in = rho + rho_offset; + double* exc_out = exc + start; + double* vrho_out = vrho + rho_offset; + syncmem_d2d_h2d_op()(d_rho, rho_in, nrho); + if (is_lda) + { + xc_lda_exc_vxc(&func, points, d_rho, d_exc, d_vrho); + } + else + { + const double* sigma_in = sigma + sigma_offset; + double* vsigma_out = vsigma + sigma_offset; + syncmem_d2d_h2d_op()(d_sigma, sigma_in, nsig); + xc_gga_exc_vxc(&func, points, d_rho, d_sigma, d_exc, d_vrho, d_vsigma); + syncmem_d2d_d2h_op()(vsigma_out, d_vsigma, nsig); + } + // Blocking copies complete Libxc's default-stream kernels before + // the host consumes derivatives or reuses the device workspace. + syncmem_d2d_d2h_op()(exc_out, d_exc, points); + syncmem_d2d_d2h_op()(vrho_out, d_vrho, nrho); + start += points; + } + } +#else + throw std::domain_error("Libxc CUDA evaluation is unavailable in this build."); +#endif + ModuleBase::timer::end("Libxc", "xc_cuda_exc_vxc"); +} void XC_Functional_Libxc::gradcorr_ncgga_sf_libxc(const std::vector& func_id, const std::size_t nrxx, @@ -194,6 +267,26 @@ std::tuple XC_Functional_Libxc::v_xc_libxc( const std::map* scaling_factor, const double hybrid_alpha, const double hse_omega) +{ + return v_xc_libxc(func_id, nrxx, omega, tpiba, chr, nspin_in, domag, domag_z, gga_grad, + scaling_factor, hybrid_alpha, hse_omega, false); +} + +std::tuple XC_Functional_Libxc::v_xc_libxc( // Peize Lin update for nspin==4 at + // 2023.01.14 + const std::vector& func_id, + const int& nrxx, // number of real-space grid + const double& omega, // volume of cell + const double tpiba, + const Charge* const chr, + const int nspin_in, + const bool domag, + const bool domag_z, + const int gga_grad, + const std::map* scaling_factor, + const double hybrid_alpha, + const double hse_omega, + const bool use_gpu) { ModuleBase::TITLE("XC_Functional_Libxc", "v_xc_libxc"); ModuleBase::timer::start("XC_Functional_Libxc", "v_xc_libxc"); @@ -213,11 +306,13 @@ std::tuple XC_Functional_Libxc::v_xc_libxc( // https://www.tddft.org/programs/libxc/manual/libxc-5.1.x/ //---------------------------------------------------------- + const int spin_mode = nspin == 1 ? XC_UNPOLARIZED : XC_POLARIZED; std::vector funcs = XC_Functional_Libxc::init_func( /* func_id = */ func_id, - /* xc_polarized = */ (1 == nspin) ? XC_UNPOLARIZED : XC_POLARIZED, + /* xc_polarized = */ spin_mode, /* hybrid_alpha = */ hybrid_alpha, - /* hse_omega = */ hse_omega); + /* hse_omega = */ hse_omega, + use_gpu); const bool is_gga = [&funcs]() { for (xc_func_type& func: funcs) @@ -314,49 +409,61 @@ std::tuple XC_Functional_Libxc::v_xc_libxc( std::vector vsigma(nrxx * ((1 == nspin) ? 1 : 3)); ModuleBase::timer::start("Libxc", "xc_lda/gga_exc_vxc"); - switch (func.info->family) + if (use_gpu) + { + const double* rho_data = rho.data(); + const double* sigma_data = sigma.data(); + double* exc_data = exc.data(); + double* vrho_data = vrho.data(); + double* vsigma_data = vsigma.data(); + xc_cuda_exc_vxc(func, nrxx, rho_data, sigma_data, exc_data, vrho_data, vsigma_data); + } + else { - case XC_FAMILY_LDA: { - constexpr int nr_batch_size = 1024; + switch (func.info->family) + { + case XC_FAMILY_LDA: { + constexpr int nr_batch_size = 1024; #ifdef _OPENMP #pragma omp parallel for schedule(static) #endif - for (int ir_start = 0; ir_start < nrxx; ir_start += nr_batch_size) - { - const int ir_end = std::min(ir_start + nr_batch_size, nrxx); - const int nrxx_thread = ir_end - ir_start; - xc_lda_exc_vxc(&func, - nrxx_thread, - rho.data() + ir_start * nspin, - exc.data() + ir_start, - vrho.data() + ir_start * nspin); + for (int ir_start = 0; ir_start < nrxx; ir_start += nr_batch_size) + { + const int ir_end = std::min(ir_start + nr_batch_size, nrxx); + const int nrxx_thread = ir_end - ir_start; + xc_lda_exc_vxc(&func, + nrxx_thread, + rho.data() + ir_start * nspin, + exc.data() + ir_start, + vrho.data() + ir_start * nspin); + } + break; } - break; - } - case XC_FAMILY_GGA: - case XC_FAMILY_HYB_GGA: { - constexpr int nr_batch_size = 1024; + case XC_FAMILY_GGA: + case XC_FAMILY_HYB_GGA: { + constexpr int nr_batch_size = 1024; #ifdef _OPENMP #pragma omp parallel for schedule(static) #endif - for (int ir_start = 0; ir_start < nrxx; ir_start += nr_batch_size) - { - const int ir_end = std::min(ir_start + nr_batch_size, nrxx); - const int nrxx_thread = ir_end - ir_start; - xc_gga_exc_vxc(&func, - nrxx_thread, - rho.data() + ir_start * nspin, - sigma.data() + ir_start * ((1 == nspin) ? 1 : 3), - exc.data() + ir_start, - vrho.data() + ir_start * nspin, - vsigma.data() + ir_start * ((1 == nspin) ? 1 : 3)); + for (int ir_start = 0; ir_start < nrxx; ir_start += nr_batch_size) + { + const int ir_end = std::min(ir_start + nr_batch_size, nrxx); + const int nrxx_thread = ir_end - ir_start; + xc_gga_exc_vxc(&func, + nrxx_thread, + rho.data() + ir_start * nspin, + sigma.data() + ir_start * ((1 == nspin) ? 1 : 3), + exc.data() + ir_start, + vrho.data() + ir_start * nspin, + vsigma.data() + ir_start * ((1 == nspin) ? 1 : 3)); + } + break; + } + default: { + throw std::domain_error("func.info->family =" + std::to_string(func.info->family) + " unfinished in " + + std::string(__FILE__) + " line " + std::to_string(__LINE__)); + } } - break; - } - default: { - throw std::domain_error("func.info->family =" + std::to_string(func.info->family) + " unfinished in " - + std::string(__FILE__) + " line " + std::to_string(__LINE__)); - } } ModuleBase::timer::end("Libxc", "xc_lda/gga_exc_vxc"); diff --git a/source/source_hamilt/module_xc/libxc_setup.cpp b/source/source_hamilt/module_xc/libxc_setup.cpp index 498116f1c3a..3a21841350b 100644 --- a/source/source_hamilt/module_xc/libxc_setup.cpp +++ b/source/source_hamilt/module_xc/libxc_setup.cpp @@ -1,6 +1,7 @@ #ifdef __LIBXC #include "libxc_abacus.h" +#include "libxc_host.h" #include "xc_functional.h" #include "source_base/tool_quit.h" #include "source_base/formatter.h" @@ -282,17 +283,44 @@ const std::vector external_xc_func_ext_params(const int id) return {}; } -std::vector -XC_Functional_Libxc::init_func(const std::vector &func_id, +std::vector +XC_Functional_Libxc::init_func(const std::vector& func_id, const int xc_polarized, const double hybrid_alpha, const double hse_omega) { + return init_func(func_id, xc_polarized, hybrid_alpha, hse_omega, false); +} + +std::vector +XC_Functional_Libxc::init_func(const std::vector& func_id, + const int xc_polarized, + const double hybrid_alpha, + const double hse_omega, + const bool use_gpu) +{ +#if !defined(ABACUS_LIBXC_CUDA) || !defined(__CUDA) + if (use_gpu) + { + ModuleBase::WARNING_QUIT("XC_Functional_Libxc::init_func", + "xc_device gpu requires CUDA-enabled ABACUS and Libxc >= 7.1 with ENABLE_CUDA=ON."); + } +#endif std::vector funcs; for (int id : func_id) { funcs.push_back({}); // create placeholder - xc_func_init(&funcs.back(), id, xc_polarized); // instantiate the XC term + int status = 0; +#ifdef XC_FLAGS_ON_HOST + const int flags = use_gpu ? XC_FLAGS_ON_DEVICE : XC_FLAGS_ON_HOST; + status = xc_func_init_flags(&funcs.back(), id, xc_polarized, flags); +#else + status = init_func_host(&funcs.back(), id, xc_polarized); +#endif + if (status != 0) + { + ModuleBase::WARNING_QUIT("XC_Functional_Libxc::init_func", "Libxc functional initialization failed."); + } // Search for external parameters. User-supplied parameters take precedence // over ABACUS built-in overrides. diff --git a/source/source_hamilt/module_xc/test/CMakeLists.txt b/source/source_hamilt/module_xc/test/CMakeLists.txt index c784d69b931..55e9ed0692c 100644 --- a/source/source_hamilt/module_xc/test/CMakeLists.txt +++ b/source/source_hamilt/module_xc/test/CMakeLists.txt @@ -100,7 +100,8 @@ addtest( TARGET MODULE_HAMILT_XCTest_VXC LIBS parameter MPI::MPI_CXX Libxc::xc psi device container SOURCES test_xc5.cpp - test_libxc_tools.cpp + ../unittests/test_libxc_tools.cpp + ../unittests/test_libxc_pot.cpp ../xc_grad.cpp ../xc_grad_prepare.cpp ../xc_grad_kernel.cpp diff --git a/source/source_hamilt/module_xc/unittests/test_libxc_pot.cpp b/source/source_hamilt/module_xc/unittests/test_libxc_pot.cpp new file mode 100644 index 00000000000..a7fcecab3c1 --- /dev/null +++ b/source/source_hamilt/module_xc/unittests/test_libxc_pot.cpp @@ -0,0 +1,104 @@ +#include "../libxc_abacus.h" +#include "source_base/module_device/device.h" +#include "gtest/gtest.h" + +#include +#include +#include +#include + +#if defined(__CUDA) && defined(ABACUS_LIBXC_CUDA) +namespace +{ +class LibxcCudaPotential : public testing::TestWithParam> +{ +}; + +TEST_P(LibxcCudaPotential, MatchesHostAcrossBatchBoundaries) +{ + if (!base_device::information::probe_gpu_availability()) + { + GTEST_SKIP() << "CUDA-enabled ABACUS and Libxc are required."; + } + const std::tuple parameters = GetParam(); + const int id = std::get<0>(parameters); + const int spin = std::get<1>(parameters); + const int points = std::get<2>(parameters); + const int nsigma = spin == 1 ? 1 : 3; + const std::vector ids = {id}; + std::vector host = XC_Functional_Libxc::init_func(ids, spin, 0.31, 0.09); + std::vector device = XC_Functional_Libxc::init_func(ids, spin, 0.31, 0.09, true); + const bool is_lda = host[0].info->family == XC_FAMILY_LDA; + const std::size_t rho_size = static_cast(points) * spin; + const std::size_t sigma_size = static_cast(points) * nsigma; + std::vector rho(rho_size); + std::vector sigma(sigma_size); + for (int i = 0; i < points; ++i) + { + // Include vacuum, threshold, ordinary and strongly polarized densities. + const double density = i % 17 == 0 ? 0.0 : (i % 19 == 0 ? 1.0e-12 : 0.05 + 0.001 * (i % 997)); + rho[i * spin] = density; + if (spin == 2) + { + rho[i * spin + 1] = density * (i % 23 == 0 ? 1.0e-8 : 0.4); + } + for (int j = 0; j < nsigma; ++j) + { + sigma[i * nsigma + j] = density * density * (j == 1 ? 0.1 : 0.3); + } + } + std::vector exc_host(points, 23.0); + std::vector vrho_host(rho_size, 23.0); + std::vector vsigma_host(sigma_size, 23.0); + std::vector exc_device(points, 23.0); + std::vector vrho_device(rho_size, 23.0); + std::vector vsigma_device(sigma_size, 23.0); + const double* rho_data = rho.data(); + const double* sigma_data = sigma.data(); + double* exc_host_data = exc_host.data(); + double* vrho_host_data = vrho_host.data(); + double* vsigma_host_data = vsigma_host.data(); + double* exc_device_data = exc_device.data(); + double* vrho_device_data = vrho_device.data(); + double* vsigma_device_data = vsigma_device.data(); + xc_func_set_dens_threshold(&host[0], 1.0e-10); + xc_func_set_dens_threshold(&device[0], 1.0e-10); + if (is_lda) + { + xc_lda_exc_vxc(&host[0], points, rho_data, exc_host_data, vrho_host_data); + } + else + { + xc_gga_exc_vxc(&host[0], points, rho_data, sigma_data, exc_host_data, vrho_host_data, vsigma_host_data); + } + XC_Functional_Libxc::xc_cuda_exc_vxc(device[0], points, rho_data, sigma_data, + exc_device_data, vrho_device_data, vsigma_device_data); + for (std::size_t i = 0; i < exc_host.size(); ++i) + { + const double tolerance = 2.0e-11 * std::max(1.0, std::abs(exc_host[i])); + ASSERT_NEAR(exc_device[i], exc_host[i], tolerance) << i; + } + for (std::size_t i = 0; i < vrho_host.size(); ++i) + { + const double tolerance = 2.0e-10 * std::max(1.0, std::abs(vrho_host[i])); + ASSERT_NEAR(vrho_device[i], vrho_host[i], tolerance) << i; + } + if (!is_lda) + { + for (std::size_t i = 0; i < vsigma_host.size(); ++i) + { + const double tolerance = 2.0e-10 * std::max(1.0, std::abs(vsigma_host[i])); + ASSERT_NEAR(vsigma_device[i], vsigma_host[i], tolerance) << i; + } + } + XC_Functional_Libxc::finish_func(host); + XC_Functional_Libxc::finish_func(device); +} + +INSTANTIATE_TEST_SUITE_P(LdaGgaHse, + LibxcCudaPotential, + testing::Combine(testing::Values(XC_LDA_X, XC_GGA_C_PBE, XC_HYB_GGA_XC_HSE06), + testing::Values(XC_UNPOLARIZED, XC_POLARIZED), + testing::Values(1, 65536, 65537, 131075))); +} // namespace +#endif diff --git a/source/source_hamilt/module_xc/test/test_libxc_tools.cpp b/source/source_hamilt/module_xc/unittests/test_libxc_tools.cpp similarity index 97% rename from source/source_hamilt/module_xc/test/test_libxc_tools.cpp rename to source/source_hamilt/module_xc/unittests/test_libxc_tools.cpp index a0fada52c62..19a4e3a4dd9 100644 --- a/source/source_hamilt/module_xc/test/test_libxc_tools.cpp +++ b/source/source_hamilt/module_xc/unittests/test_libxc_tools.cpp @@ -1,5 +1,6 @@ #include #include "../libxc_abacus.h" +#include "../libxc_host.h" #include "gtest/gtest.h" #include @@ -17,7 +18,7 @@ TEST(LibxcSanitizer, FullPolarizationUsesTheWeightedEnergyDerivative) for (std::size_t ifunc = 0; ifunc < functional_ids.size(); ++ifunc) { xc_func_type func; - ASSERT_EQ(xc_func_init(&func, functional_ids[ifunc], XC_POLARIZED), 0); + ASSERT_EQ(XC_Functional_Libxc::init_func_host(&func, functional_ids[ifunc], XC_POLARIZED), 0); xc_func_set_dens_threshold(&func, 1.0e-6); const std::function evaluate @@ -80,7 +81,7 @@ TEST(LibxcSanitizer, FullPolarizationUsesTheWeightedEnergyDerivative) TEST(LibxcSanitizer, GgaSigmaReverseMatchesTheWeightedEnergy) { xc_func_type func; - ASSERT_EQ(xc_func_init(&func, XC_GGA_C_PBE, XC_POLARIZED), 0); + ASSERT_EQ(XC_Functional_Libxc::init_func_host(&func, XC_GGA_C_PBE, XC_POLARIZED), 0); xc_func_set_dens_threshold(&func, 1.0e-6); xc_func_set_sigma_threshold(&func, 1.0e-2); @@ -165,7 +166,7 @@ TEST(LibxcSanitizer, GgaSigmaReverseMatchesTheWeightedEnergy) TEST(LibxcSanitizer, UnpolarizedSelfSigmaReverseMatchesTheWeightedEnergy) { xc_func_type func; - ASSERT_EQ(xc_func_init(&func, XC_GGA_C_PBE, XC_UNPOLARIZED), 0); + ASSERT_EQ(XC_Functional_Libxc::init_func_host(&func, XC_GGA_C_PBE, XC_UNPOLARIZED), 0); xc_func_set_dens_threshold(&func, 1.0e-6); xc_func_set_sigma_threshold(&func, 1.0e-2); @@ -260,8 +261,8 @@ class LibxcToolsSgnVxcTest : public testing::Test void SetUp() override { - xc_func_init(&gga_func, XC_GGA_X_PBE, XC_UNPOLARIZED); - xc_func_init(&lda_func, XC_LDA_X, XC_UNPOLARIZED); + XC_Functional_Libxc::init_func_host(&gga_func, XC_GGA_X_PBE, XC_UNPOLARIZED); + XC_Functional_Libxc::init_func_host(&lda_func, XC_LDA_X, XC_UNPOLARIZED); } void TearDown() override diff --git a/source/source_hamilt/module_xc/xc_functional.h b/source/source_hamilt/module_xc/xc_functional.h index 882edab71ed..4d7533fcbb1 100644 --- a/source/source_hamilt/module_xc/xc_functional.h +++ b/source/source_hamilt/module_xc/xc_functional.h @@ -76,6 +76,18 @@ class XC_Functional const double hybrid_alpha, const double hse_omega); + static std::tuple v_xc( + const int &nrxx, // number of real-space grid + const Charge* const chr, + const UnitCell *ucell, // charge density + const int nspin, + const bool domag, + const bool domag_z, + const int gga_grad, + const double hybrid_alpha, + const double hse_omega, + const bool use_gpu); + //------------------- // xc_functional.cpp //------------------- diff --git a/source/source_hamilt/module_xc/xc_pot.cpp b/source/source_hamilt/module_xc/xc_pot.cpp index 278de827546..fe53c7c4bc2 100644 --- a/source/source_hamilt/module_xc/xc_pot.cpp +++ b/source/source_hamilt/module_xc/xc_pot.cpp @@ -29,6 +29,21 @@ std::tuple XC_Functional::v_xc( const int gga_grad, const double hybrid_alpha, const double hse_omega) +{ + return v_xc(nrxx, chr, ucell, nspin, domag, domag_z, gga_grad, hybrid_alpha, hse_omega, false); +} + +std::tuple XC_Functional::v_xc( + const int& nrxx, + const Charge* const chr, + const UnitCell* ucell, + const int nspin, + const bool domag, + const bool domag_z, + const int gga_grad, + const double hybrid_alpha, + const double hse_omega, + const bool use_gpu) { ModuleBase::TITLE("XC_Functional", "v_xc"); @@ -47,7 +62,8 @@ std::tuple XC_Functional::v_xc( gga_grad, &(scaling_factor_xc), hybrid_alpha, - hse_omega); + hse_omega, + use_gpu); #else ModuleBase::WARNING_QUIT("v_xc", "compile with LIBXC"); #endif diff --git a/source/source_io/module_parameter/input_parameter.h b/source/source_io/module_parameter/input_parameter.h index b837b985a85..51dacd8ca0c 100644 --- a/source/source_io/module_parameter/input_parameter.h +++ b/source/source_io/module_parameter/input_parameter.h @@ -87,6 +87,7 @@ struct Input_para double nelec_delta = 0.0; ///< change in the number of total electrons double nupdown = 0.0; std::string dft_functional = "default"; ///< input DFT functional. + std::string xc_device = "cpu"; ///< Device for Libxc LDA/GGA potential evaluation. int gga_grad = 0; ///< Noncollinear GGA: 0 original, 1 local axis, 2 regularized mapped-density LCA. double xc_temperature = 0.0; ///< only relevant if finite temperature functional is used double pseudo_rcut = 15.0; ///< cut-off radius for calculating msh diff --git a/source/source_io/module_parameter/read_inp_estruc.cpp b/source/source_io/module_parameter/read_inp_estruc.cpp index 777dbdbfe78..b8abd5e8233 100644 --- a/source/source_io/module_parameter/read_inp_estruc.cpp +++ b/source/source_io/module_parameter/read_inp_estruc.cpp @@ -292,6 +292,29 @@ The other way is only available when compiling with LIBXC, and it allows for sup read_sync_string(input.dft_functional); this->add_item(item); } + { + Input_Item item("xc_device"); + item.domain.set_choices({"cpu", "gpu"}); + item.domain.add_requirement("xc_device==gpu", "device==gpu"); + item.annotation = "device for Libxc LDA/GGA potential evaluation"; + item.category = "Electronic structure"; + item.type = "String"; + item.description = "Selects CPU or CUDA evaluation of the Libxc LDA/GGA potential, including the " + "semilocal part of HSE. GPU evaluation requires CUDA-enabled ABACUS and Libxc >= 7.1 " + "built with ENABLE_CUDA=ON. Built-in functionals, meta-GGA, response kernels and " + "separate force/stress evaluations retain their CPU implementation."; + item.default_value = "cpu"; + read_sync_string(input.xc_device); +#if !defined(ABACUS_LIBXC_CUDA) || !defined(__CUDA) + item.check_value = [](const Input_Item&, const Parameter& para) { + if (para.input.xc_device == "gpu") + { + ModuleBase::WARNING_QUIT("ReadInput", "xc_device gpu requires CUDA-enabled ABACUS and Libxc >= 7.1.0 built with ENABLE_CUDA=ON."); + } + }; +#endif + this->add_item(item); + } { Input_Item item("xc_temperature"); item.annotation = "temperature for finite temperature functionals"; diff --git a/source/source_io/module_parameter/unittests/test_read_inp_estruc.cpp b/source/source_io/module_parameter/unittests/test_read_inp_estruc.cpp new file mode 100644 index 00000000000..1c3df0ff99f --- /dev/null +++ b/source/source_io/module_parameter/unittests/test_read_inp_estruc.cpp @@ -0,0 +1,27 @@ +#include "source_io/module_parameter/read_input.h" +#include + +TEST(LibxcDeviceInput, DefaultAndAllowedDevices) +{ + ModuleIO::ReadInput schema(0); + for (const auto& entry : schema.get_input_lists()) + { + if (entry.first == "xc_device") + { + const auto& domain = entry.second.domain; + const Parameter defaults; + EXPECT_EQ(domain.value(defaults).format(), "cpu"); + EXPECT_TRUE(domain.validate_explicit(entry.first, {"gpu"}).empty()); + EXPECT_FALSE(domain.validate_explicit(entry.first, {"invalid"}).empty()); + + const ModuleIO::InputValue gpu(std::string{"gpu"}); + const ModuleIO::InputValue cpu(std::string{"cpu"}); + ModuleIO::InputValues values = {{"xc_device", gpu}, {"device", cpu}}; + EXPECT_FALSE(domain.validate_requirements(entry.first, values).empty()); + values["device"] = gpu; + EXPECT_TRUE(domain.validate_requirements(entry.first, values).empty()); + return; + } + } + FAIL() << "xc_device is missing from the INPUT schema"; +} diff --git a/source/source_io/test_serial/CMakeLists.txt b/source/source_io/test_serial/CMakeLists.txt index 7619d73da6e..f6537c7b1a0 100644 --- a/source/source_io/test_serial/CMakeLists.txt +++ b/source/source_io/test_serial/CMakeLists.txt @@ -43,7 +43,8 @@ addtest( addtest( TARGET MODULE_IO_read_item_serial LIBS parameter base device io_input_serial - SOURCES read_input_item_test.cpp test_read_inp_sccs.cpp) + SOURCES read_input_item_test.cpp test_read_inp_sccs.cpp + ../module_parameter/unittests/test_read_inp_estruc.cpp) addtest( TARGET MODULE_IO_input_domain_reader diff --git a/source/source_lcao/module_lr/test/CMakeLists.txt b/source/source_lcao/module_lr/test/CMakeLists.txt index c6180ff87ed..f3c530cfaf3 100644 --- a/source/source_lcao/module_lr/test/CMakeLists.txt +++ b/source/source_lcao/module_lr/test/CMakeLists.txt @@ -18,7 +18,7 @@ if(ENABLE_LIBXC) addtest( TARGET MODULE_LR_xc_kxc LIBS Libxc::xc - SOURCES test_xc_kxc.cpp ../potentials/xc_kxc.cpp) + SOURCES ../unittests/test_xc_kxc.cpp ../potentials/xc_kxc.cpp) endif() addtest( diff --git a/source/source_lcao/module_lr/test/test_xc_kxc.cpp b/source/source_lcao/module_lr/unittests/test_xc_kxc.cpp similarity index 94% rename from source/source_lcao/module_lr/test/test_xc_kxc.cpp rename to source/source_lcao/module_lr/unittests/test_xc_kxc.cpp index c4b0584538a..9ec1dfd4f9f 100644 --- a/source/source_lcao/module_lr/test/test_xc_kxc.cpp +++ b/source/source_lcao/module_lr/unittests/test_xc_kxc.cpp @@ -1,5 +1,5 @@ #include "../potentials/xc_kxc.h" -#include +#include "source_hamilt/module_xc/libxc_host.h" #include #include #include @@ -72,7 +72,7 @@ TEST(XcKxc, InternalAuxiliaryIsNotAnIndependentMixedContribution) TEST(XcKxc, RuntimeLdaFlagsDetermineAvailability) { xc_func_type functional = {}; - ASSERT_EQ(xc_func_init(&functional, XC_LDA_X, XC_UNPOLARIZED), 0); + ASSERT_EQ(XC_Functional_Libxc::init_func_host(&functional, XC_LDA_X, XC_UNPOLARIZED), 0); const xc_func_info_type* info = xc_func_get_info(&functional); const int flags = xc_func_info_get_flags(info); if ((flags & XC_FLAGS_HAVE_KXC) != 0) diff --git a/source/source_lcao/module_rdmft/rdmft_tools.cpp b/source/source_lcao/module_rdmft/rdmft_tools.cpp index 5950678e353..6bddb6ae42f 100644 --- a/source/source_lcao/module_rdmft/rdmft_tools.cpp +++ b/source/source_lcao/module_rdmft/rdmft_tools.cpp @@ -278,7 +278,7 @@ void Veff_rdmft, double>::contributeHR() ModuleBase::matrix vofk = *vloc_; vofk.zero_out(); ModuleBase::matrix v_matrix_XC(this->nspin, charge_->nrxx); - elecstate::PotXC potXC(PARAM.globalv.domag, PARAM.globalv.domag_z, PARAM.inp.gga_grad, rho_basis_, etxc, vtxc, &vofk); + elecstate::PotXC potXC(PARAM.globalv.domag, PARAM.globalv.domag_z, PARAM.inp.gga_grad, false, rho_basis_, etxc, vtxc, &vofk); potXC.cal_v_eff(charge_, ucell, v_matrix_XC); // if need meta-GGA, go to study veff_lcao.cpp and modify the code @@ -360,7 +360,7 @@ void Veff_rdmft::contributeHR() ModuleBase::matrix vofk = *vloc_; vofk.zero_out(); ModuleBase::matrix v_matrix_XC(this->nspin, charge_->nrxx); - elecstate::PotXC potXC(PARAM.globalv.domag, PARAM.globalv.domag_z, PARAM.inp.gga_grad, rho_basis_, etxc, vtxc, &vofk); + elecstate::PotXC potXC(PARAM.globalv.domag, PARAM.globalv.domag_z, PARAM.inp.gga_grad, false, rho_basis_, etxc, vtxc, &vofk); potXC.cal_v_eff(charge_, ucell, v_matrix_XC); for(int is=0; isnspin; ++is) diff --git a/source/source_pw/module_dfpt/dfpt_xc_analytic.cpp b/source/source_pw/module_dfpt/dfpt_xc_analytic.cpp index 3b83d58ad76..ed3ac59f1ac 100644 --- a/source/source_pw/module_dfpt/dfpt_xc_analytic.cpp +++ b/source/source_pw/module_dfpt/dfpt_xc_analytic.cpp @@ -5,7 +5,7 @@ #include "source_base/constants.h" #include "source_base/tool_quit.h" -#include +#include "source_hamilt/module_xc/libxc_host.h" #include #include @@ -122,7 +122,7 @@ void XC_First_Order_Analytic::init_kernel_fields_(const std::vector& rho for (size_t ifunc = 0; ifunc < func_id_.size(); ++ifunc) { xc_func_type func; - if (xc_func_init(&func, func_id_[ifunc], XC_UNPOLARIZED) != 0) + if (XC_Functional_Libxc::init_func_host(&func, func_id_[ifunc], XC_UNPOLARIZED) != 0) { ModuleBase::WARNING_QUIT("XC_First_Order_Analytic", "xc_func_init failed"); } diff --git a/source/source_pw/module_dfpt/test_serial/CMakeLists.txt b/source/source_pw/module_dfpt/test_serial/CMakeLists.txt index 906b63bb92c..3024c15e40e 100644 --- a/source/source_pw/module_dfpt/test_serial/CMakeLists.txt +++ b/source/source_pw/module_dfpt/test_serial/CMakeLists.txt @@ -141,7 +141,7 @@ if(ENABLE_LIBXC) addtest( TARGET MODULE_DFPT_xc_analytic_serial LIBS parameter dfpt_planewave_serial device base symmetry Libxc::xc - SOURCES dfpt_xc_analytic_serial_test.cpp + SOURCES ../unittests/test_dfpt_xc_analytic.cpp ../dfpt_xc_analytic.cpp ../dfpt_rho.cpp ../dfpt_rho_mix.cpp diff --git a/source/source_pw/module_dfpt/test_serial/dfpt_xc_analytic_serial_test.cpp b/source/source_pw/module_dfpt/unittests/test_dfpt_xc_analytic.cpp similarity index 97% rename from source/source_pw/module_dfpt/test_serial/dfpt_xc_analytic_serial_test.cpp rename to source/source_pw/module_dfpt/unittests/test_dfpt_xc_analytic.cpp index 0c86d3d84b5..7dfbd6bf73a 100644 --- a/source/source_pw/module_dfpt/test_serial/dfpt_xc_analytic_serial_test.cpp +++ b/source/source_pw/module_dfpt/unittests/test_dfpt_xc_analytic.cpp @@ -7,7 +7,7 @@ #include "source_base/constants.h" #include "source_basis/module_pw/pw_basis.h" -#include +#include "source_hamilt/module_xc/libxc_host.h" #include #include @@ -137,7 +137,7 @@ void libxc_vxc(const std::vector& func_id, for (size_t ifunc = 0; ifunc < func_id.size(); ++ifunc) { xc_func_type func; - ASSERT_EQ(xc_func_init(&func, func_id[ifunc], XC_UNPOLARIZED), 0); + ASSERT_EQ(XC_Functional_Libxc::init_func_host(&func, func_id[ifunc], XC_UNPOLARIZED), 0); std::vector vr(n, 0.0); std::vector vs(n, 0.0); std::vector zk(n, 0.0); @@ -182,7 +182,7 @@ TEST(DFPTXcAnalytic, LdaKernelMatchesVrhofiniteDifference) for (size_t ifunc = 0; ifunc < func_id.size(); ++ifunc) { xc_func_type func; - ASSERT_EQ(xc_func_init(&func, func_id[ifunc], XC_UNPOLARIZED), 0); + ASSERT_EQ(XC_Functional_Libxc::init_func_host(&func, func_id[ifunc], XC_UNPOLARIZED), 0); std::vector zk(pw.nrxx, 0.0); std::vector vxc(pw.nrxx, 0.0); xc_lda_exc_vxc(&func, pw.nrxx, rho_p.data(), zk.data(), vxc.data()); @@ -413,7 +413,7 @@ TEST(DFPTXcAnalytic, UniformDensityKeepsLocalGgaResponse) for (size_t ifunc = 0; ifunc < func_id.size(); ++ifunc) { xc_func_type func; - ASSERT_EQ(xc_func_init(&func, func_id[ifunc], XC_UNPOLARIZED), 0); + ASSERT_EQ(XC_Functional_Libxc::init_func_host(&func, func_id[ifunc], XC_UNPOLARIZED), 0); double rho = 0.1; double sig = 0.0; double v2rho2 = 0.0; @@ -452,7 +452,7 @@ TEST(DFPTXcAnalytic, ConfiguredExtParamsDifferentiateTheConfiguredFunctional) kernel.apply(a, dvxc); xc_func_type func; - ASSERT_EQ(xc_func_init(&func, XC_LDA_XC_KSDT, XC_UNPOLARIZED), 0); + ASSERT_EQ(XC_Functional_Libxc::init_func_host(&func, XC_LDA_XC_KSDT, XC_UNPOLARIZED), 0); const double theta = 0.007; xc_func_set_ext_params(&func, &theta); double rho = 0.1; @@ -493,7 +493,7 @@ TEST(DFPTXcAnalytic, SignedComponentWeightsEnterEveryKernelField) for (int i = 0; i < 2; ++i) { xc_func_type func; - ASSERT_EQ(xc_func_init(&func, ids_ref[i], XC_UNPOLARIZED), 0); + ASSERT_EQ(XC_Functional_Libxc::init_func_host(&func, ids_ref[i], XC_UNPOLARIZED), 0); double rho = 0.1; if (ids_ref[i] == XC_GGA_X_PBE) { From 93b5903cbd8c8a2d09a5089c4ce69600bd890781 Mon Sep 17 00:00:00 2001 From: Jiacheng Xu <169599847+Stardust0831@users.noreply.github.com> Date: Thu, 19 Nov 2026 02:45:34 +0800 Subject: [PATCH 2/4] fix(xc): diagnose a stale compiler header for CUDA Libxc --- source/source_hamilt/module_xc/libxc_host.h | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/source/source_hamilt/module_xc/libxc_host.h b/source/source_hamilt/module_xc/libxc_host.h index 261356e130b..afa0527e7f4 100644 --- a/source/source_hamilt/module_xc/libxc_host.h +++ b/source/source_hamilt/module_xc/libxc_host.h @@ -3,6 +3,10 @@ #include +#if defined(ABACUS_LIBXC_CUDA) && !defined(XC_FLAGS_ON_HOST) +#error "CMake found CUDA Libxc >= 7.1.0 but xc.h lacks host/device selection. Check the compiler include path." +#endif + namespace XC_Functional_Libxc { // A CUDA-enabled Libxc defaults to device pointers. Keep host callers explicit. From 44efc7fb162ab84f2492582ab40bb040a2a1151e Mon Sep 17 00:00:00 2001 From: Jiacheng Xu <169599847+Stardust0831@users.noreply.github.com> Date: Thu, 19 Nov 2026 03:26:18 +0800 Subject: [PATCH 3/4] docs(xc): record the HSE force limitation in released Libxc --- docs/advanced/install.md | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/docs/advanced/install.md b/docs/advanced/install.md index aa9725b2458..7f30c06f0dc 100644 --- a/docs/advanced/install.md +++ b/docs/advanced/install.md @@ -24,13 +24,19 @@ cmake -B build -DENABLE_LIBXC=ON ``` For CUDA evaluation of the Libxc LDA/GGA potential (including the semilocal part -of HSE), build Libxc 7.1 or later with `-DENABLE_CUDA=ON`. Configure ABACUS with +of HSE), build Libxc 7.1.0 or later with `-DENABLE_CUDA=ON`. Configure ABACUS with `-DUSE_CUDA=ON -DENABLE_LIBXC=ON -DLibxc_DIR=/lib/cmake/Libxc`, then set `device gpu` and `xc_device gpu` in INPUT. `xc_device` defaults to `cpu`, which also works with a CUDA-enabled Libxc. Built-in functionals, meta-GGA, response kernels and separate force/stress evaluations continue to use the CPU. This option does not select the exact-exchange backend. +For HSE forces, Libxc 7.1.0 exceeded a host/device agreement tolerance of +`1e-6 eV/Å` on a displaced Si64 cell. The WPBEH exchange term loses precision +at small gradients. Its original logarithm is present in the 7.1.0 through +7.1.2 releases. The Si64 forces agreed within `1.1e-10 eV/Å` using the +[corrected Libxc development revision](https://gitlab.com/libxc/libxc/-/blob/0a70f0eab900537913f4fdf94077b55fa019c2c1/python/gga_exc/gga_x_wpbeh.py#L325). + ## Build with ML-ALGO If machine learning algorithms, including DeePKS and machine learning based kinetic energy density functional (ML-KEDF) for OFDFT, is required for DFT calculation, the following prerequisites and steps are needed: From 65bedae416fda606b67bdbacf689a7c91ac205ff Mon Sep 17 00:00:00 2001 From: Jiacheng Xu <169599847+Stardust0831@users.noreply.github.com> Date: Sun, 22 Nov 2026 21:24:13 +0800 Subject: [PATCH 4/4] refactor(xc): name the Libxc device selector device_xc --- docs/advanced/install.md | 2 +- source/source_estate/module_pot/potential_types.cpp | 2 +- source/source_hamilt/module_xc/libxc_setup.cpp | 2 +- source/source_io/module_parameter/input_parameter.h | 2 +- source/source_io/module_parameter/read_inp_estruc.cpp | 10 +++++----- .../unittests/test_read_inp_estruc.cpp | 6 +++--- 6 files changed, 12 insertions(+), 12 deletions(-) diff --git a/docs/advanced/install.md b/docs/advanced/install.md index 7f30c06f0dc..db39a6e415d 100644 --- a/docs/advanced/install.md +++ b/docs/advanced/install.md @@ -26,7 +26,7 @@ cmake -B build -DENABLE_LIBXC=ON For CUDA evaluation of the Libxc LDA/GGA potential (including the semilocal part of HSE), build Libxc 7.1.0 or later with `-DENABLE_CUDA=ON`. Configure ABACUS with `-DUSE_CUDA=ON -DENABLE_LIBXC=ON -DLibxc_DIR=/lib/cmake/Libxc`, then set -`device gpu` and `xc_device gpu` in INPUT. `xc_device` defaults to `cpu`, which +`device gpu` and `device_xc gpu` in INPUT. `device_xc` defaults to `cpu`, which also works with a CUDA-enabled Libxc. Built-in functionals, meta-GGA, response kernels and separate force/stress evaluations continue to use the CPU. This option does not select the exact-exchange backend. diff --git a/source/source_estate/module_pot/potential_types.cpp b/source/source_estate/module_pot/potential_types.cpp index bb8fa652a0b..2ac24878041 100644 --- a/source/source_estate/module_pot/potential_types.cpp +++ b/source/source_estate/module_pot/potential_types.cpp @@ -56,7 +56,7 @@ PotBase* Potential::get_pot_type(const std::string& pot_type, const Input_para& } else if (pot_type == "xc") { - const bool xc_gpu = input.xc_device == "gpu"; + const bool xc_gpu = input.device_xc == "gpu"; return new PotXC(PARAM.globalv.domag, PARAM.globalv.domag_z, input.gga_grad, xc_gpu, this->rho_basis_, this->etxc_, this->vtxc_, &(this->vofk_eff)); } diff --git a/source/source_hamilt/module_xc/libxc_setup.cpp b/source/source_hamilt/module_xc/libxc_setup.cpp index 3a21841350b..a8617213575 100644 --- a/source/source_hamilt/module_xc/libxc_setup.cpp +++ b/source/source_hamilt/module_xc/libxc_setup.cpp @@ -303,7 +303,7 @@ XC_Functional_Libxc::init_func(const std::vector& func_id, if (use_gpu) { ModuleBase::WARNING_QUIT("XC_Functional_Libxc::init_func", - "xc_device gpu requires CUDA-enabled ABACUS and Libxc >= 7.1 with ENABLE_CUDA=ON."); + "device_xc gpu requires CUDA-enabled ABACUS and Libxc >= 7.1 with ENABLE_CUDA=ON."); } #endif std::vector funcs; diff --git a/source/source_io/module_parameter/input_parameter.h b/source/source_io/module_parameter/input_parameter.h index 51dacd8ca0c..a5474e4489a 100644 --- a/source/source_io/module_parameter/input_parameter.h +++ b/source/source_io/module_parameter/input_parameter.h @@ -87,7 +87,7 @@ struct Input_para double nelec_delta = 0.0; ///< change in the number of total electrons double nupdown = 0.0; std::string dft_functional = "default"; ///< input DFT functional. - std::string xc_device = "cpu"; ///< Device for Libxc LDA/GGA potential evaluation. + std::string device_xc = "cpu"; ///< Device for Libxc LDA/GGA potential evaluation. int gga_grad = 0; ///< Noncollinear GGA: 0 original, 1 local axis, 2 regularized mapped-density LCA. double xc_temperature = 0.0; ///< only relevant if finite temperature functional is used double pseudo_rcut = 15.0; ///< cut-off radius for calculating msh diff --git a/source/source_io/module_parameter/read_inp_estruc.cpp b/source/source_io/module_parameter/read_inp_estruc.cpp index b8abd5e8233..66dd7359687 100644 --- a/source/source_io/module_parameter/read_inp_estruc.cpp +++ b/source/source_io/module_parameter/read_inp_estruc.cpp @@ -293,9 +293,9 @@ The other way is only available when compiling with LIBXC, and it allows for sup this->add_item(item); } { - Input_Item item("xc_device"); + Input_Item item("device_xc"); item.domain.set_choices({"cpu", "gpu"}); - item.domain.add_requirement("xc_device==gpu", "device==gpu"); + item.domain.add_requirement("device_xc==gpu", "device==gpu"); item.annotation = "device for Libxc LDA/GGA potential evaluation"; item.category = "Electronic structure"; item.type = "String"; @@ -304,12 +304,12 @@ The other way is only available when compiling with LIBXC, and it allows for sup "built with ENABLE_CUDA=ON. Built-in functionals, meta-GGA, response kernels and " "separate force/stress evaluations retain their CPU implementation."; item.default_value = "cpu"; - read_sync_string(input.xc_device); + read_sync_string(input.device_xc); #if !defined(ABACUS_LIBXC_CUDA) || !defined(__CUDA) item.check_value = [](const Input_Item&, const Parameter& para) { - if (para.input.xc_device == "gpu") + if (para.input.device_xc == "gpu") { - ModuleBase::WARNING_QUIT("ReadInput", "xc_device gpu requires CUDA-enabled ABACUS and Libxc >= 7.1.0 built with ENABLE_CUDA=ON."); + ModuleBase::WARNING_QUIT("ReadInput", "device_xc gpu requires CUDA-enabled ABACUS and Libxc >= 7.1.0 built with ENABLE_CUDA=ON."); } }; #endif diff --git a/source/source_io/module_parameter/unittests/test_read_inp_estruc.cpp b/source/source_io/module_parameter/unittests/test_read_inp_estruc.cpp index 1c3df0ff99f..9609df864a5 100644 --- a/source/source_io/module_parameter/unittests/test_read_inp_estruc.cpp +++ b/source/source_io/module_parameter/unittests/test_read_inp_estruc.cpp @@ -6,7 +6,7 @@ TEST(LibxcDeviceInput, DefaultAndAllowedDevices) ModuleIO::ReadInput schema(0); for (const auto& entry : schema.get_input_lists()) { - if (entry.first == "xc_device") + if (entry.first == "device_xc") { const auto& domain = entry.second.domain; const Parameter defaults; @@ -16,12 +16,12 @@ TEST(LibxcDeviceInput, DefaultAndAllowedDevices) const ModuleIO::InputValue gpu(std::string{"gpu"}); const ModuleIO::InputValue cpu(std::string{"cpu"}); - ModuleIO::InputValues values = {{"xc_device", gpu}, {"device", cpu}}; + ModuleIO::InputValues values = {{"device_xc", gpu}, {"device", cpu}}; EXPECT_FALSE(domain.validate_requirements(entry.first, values).empty()); values["device"] = gpu; EXPECT_TRUE(domain.validate_requirements(entry.first, values).empty()); return; } } - FAIL() << "xc_device is missing from the INPUT schema"; + FAIL() << "device_xc is missing from the INPUT schema"; }