From dfc71eb145aef88d0e2f22c2e7739939bc8f351e Mon Sep 17 00:00:00 2001 From: Jiacheng Xu <13862180016@163.com> Date: Sun, 1 Nov 2026 23:35:52 +0800 Subject: [PATCH 1/4] perf(lcao): batch radial tables on the selected CUDA device --- source/source_lcao/center2orb_orb11.cpp | 249 +++++++++ source/source_lcao/center2orb_orb11.h | 3 + source/source_lcao/center2orb_orb21.cpp | 135 +++-- source/source_lcao/center2orb_orb21.h | 11 +- source/source_lcao/module_ri/ewald_vq.hpp | 12 +- source/source_lcao/module_ri/exx_lri.h | 15 +- source/source_lcao/module_ri/exx_lri.hpp | 49 +- .../source_lcao/module_ri/exx_lri_interface.h | 21 +- .../module_ri/exx_lri_interface.hpp | 45 +- source/source_lcao/module_ri/lri_cv.cpp | 12 +- source/source_lcao/module_ri/lri_cv.h | 5 +- .../source_lcao/module_ri/matrix_orbs11.cpp | 18 +- source/source_lcao/module_ri/matrix_orbs11.h | 1 + .../source_lcao/module_ri/matrix_orbs21.cpp | 22 +- source/source_lcao/module_ri/matrix_orbs21.h | 1 + .../module_rpa/rpa_lri_overlap.cpp | 18 +- source/source_lcao/setup_exx.cpp | 58 +- source/source_lcao/test/CMakeLists.txt | 35 ++ .../test/test_center2orb_orb11.cpp | 521 ++++++++++++++++++ 19 files changed, 1119 insertions(+), 112 deletions(-) create mode 100644 source/source_lcao/test/test_center2orb_orb11.cpp diff --git a/source/source_lcao/center2orb_orb11.cpp b/source/source_lcao/center2orb_orb11.cpp index 7241aaba141..08e68f34d89 100644 --- a/source/source_lcao/center2orb_orb11.cpp +++ b/source/source_lcao/center2orb_orb11.cpp @@ -7,11 +7,26 @@ #include "center2orb.h" #include "source_base/constants.h" +#include "source_base/math_integral.h" +#include "source_base/mathzone_add1.h" +#include "source_base/module_external/blas_connector.h" +#include "source_base/timer.h" + +#ifdef __CUDA +#include "source_base/module_device/memory_op.h" + +#include +#include +#endif #include "source_base/math_polyint.h" #include "source_base/sph_bessel_recursive.h" #include "source_base/ylm.h" +#include #include +#include +#include +#include Center2_Orb::Orb11::Orb11(const Numerical_Orbital_Lm& nA_in, const Numerical_Orbital_Lm& nB_in, @@ -86,6 +101,240 @@ void Center2_Orb::Orb11::init_radial_table(const std::set& radials) } } +namespace +{ +double radial_product(const Numerical_Orbital_Lm &left, const Numerical_Orbital_Lm &right, const int ik) +{ + if (!left.get_psif().empty() && !right.get_psi_k2().empty()) + { + return left.getPsif(ik) * right.getPsi_k2(ik); + } + if (!left.get_psi_k().empty() && !right.get_psi_k().empty()) + { + return left.getPsi_k(ik) * right.getPsi_k(ik); + } + if (!left.get_psi_k2().empty() && !right.get_psif().empty()) + { + return left.getPsi_k2(ik) * right.getPsif(ik); + } + return 0.0; +} +} // namespace + +void Center2_Orb::Orb11::init_radial_table(const std::vector &orbitals, const bool use_gpu) +{ + ModuleBase::timer::start("Center2_Orb", "init_radial_table"); +#ifndef __CUDA + if (use_gpu) + { + throw std::invalid_argument("GPU radial tables require a CUDA build"); + } +#else + if (use_gpu) + { + container::kernels::createGpuBlasHandle(); + } +#endif + // Group by quadrature grid and angular momentum. All orbitals in a batch + // belong to one Matrix_Orbs object and share its immutable Bessel table. + using Grid = std::tuple; + std::map> groups; + for (Orb11 *orbital : orbitals) + { + if (orbital->psb_ != orbitals.front()->psb_) + { + throw std::invalid_argument("A radial batch must share its Bessel table"); + } + const int rmesh = Center2_Orb::get_rmesh(orbital->nA.getRcut(), orbital->nB.getRcut(), orbital->dr_); + const int kmesh = orbital->nA.getNk(); + const double dk = orbital->nA.getDk(); + const int first_l = std::abs(orbital->nA.getL() - orbital->nB.getL()); + const int last_l = orbital->nA.getL() + orbital->nB.getL(); + for (int l = first_l; l <= last_l; l += 2) + { + orbital->Table_r[l].resize(rmesh); + orbital->Table_dr[l].resize(rmesh); + const Grid grid( + l, + kmesh, + rmesh, + dk); + groups[grid].push_back(orbital); + } + } + for (const auto &group : groups) + { + const int l = std::get<0>(group.first); + const int kmesh = std::get<1>(group.first); + const int rmesh = std::get<2>(group.first); + const double dk = std::get<3>(group.first); + const auto &tasks = group.second; + const auto &bessel = tasks.front()->psb_->get_jlx(); + const double *kpoint = tasks.front()->nA.getKpoint(); + // Bound all three device buffers together: at most 160 MiB, released + // before returning to the SCF/LibRI contraction stages. + constexpr std::size_t input_limit = 16 * 1024 * 1024 / sizeof(double); + constexpr std::size_t table_limit = 128 * 1024 * 1024 / sizeof(double); + constexpr std::size_t output_limit = 16 * 1024 * 1024 / sizeof(double); + if (kmesh < 3 || kmesh % 2 == 0 || static_cast(kmesh) > input_limit) + { + throw std::invalid_argument("Unsupported radial quadrature grid for batched tables"); + } + const std::size_t radial_limit = std::min(table_limit / (2 * kmesh), output_limit / 2); + const int radial_block = std::min(rmesh, radial_limit); + const std::size_t batch_limit = std::min(input_limit / kmesh, output_limit / (2 * radial_block)); + const int batch_size = std::min(tasks.size(), std::min(128, batch_limit)); + const std::size_t input_size = static_cast(batch_size) * kmesh; + const std::size_t table_size = static_cast(2 * radial_block) * kmesh; + const std::size_t output_size = static_cast(batch_size) * 2 * radial_block; + std::vector input(input_size); + std::vector table(table_size); + std::vector output(output_size); + double *input_ptr = input.data(); + double *table_ptr = table.data(); + double *output_ptr = output.data(); +#ifdef __CUDA + std::unique_ptr workspace; + if (use_gpu) + { + const int64_t workspace_size = input_size + table_size + output_size; + const container::TensorShape shape({workspace_size}); + workspace.reset( + new container::Tensor(container::DataType::DT_DOUBLE, container::DeviceType::GpuDevice, shape)); + input_ptr = workspace->data(); + table_ptr = input_ptr + input_size; + output_ptr = table_ptr + table_size; + } +#endif + for (int radial_start = 0; radial_start < rmesh; radial_start += radial_block) + { + const int rows = std::min(radial_block, rmesh - radial_start); + const int columns = 2 * rows; + const double derivative_factor = -(l + 1.0) / (2 * l + 1.0); + const double lower_factor = l / (l + 1.0); + for (int ir = 0; ir < rows; ++ir) + { + const int radial = radial_start + ir; + for (int ik = 0; ik < kmesh; ++ik) + { + const double lower = l == 0 ? 0.0 : bessel[l - 1][radial][ik]; + table[(2 * ir) * kmesh + ik] = bessel[l][radial][ik]; + table[(2 * ir + 1) * kmesh + ik] = + derivative_factor * kpoint[ik] * (bessel[l + 1][radial][ik] - lower_factor * lower); + } + } +#ifdef __CUDA + if (use_gpu) + { + const std::size_t count = static_cast(columns) * kmesh; + base_device::memory::synchronize_memory_op()( + table_ptr, table.data(), count); + } +#endif + for (std::size_t start = 0; start < tasks.size(); start += batch_size) + { + const int count = std::min(batch_size, tasks.size() - start); + for (int it = 0; it < count; ++it) + { + const Orb11 &orbital = *tasks[start + it]; + for (int ik = 0; ik < kmesh; ++ik) + { + const int weight = (ik == 0 || ik == kmesh - 1) ? 1 : (ik % 2 == 1 ? 4 : 2); + const double product = radial_product(orbital.nA, orbital.nB, ik); + input[it * kmesh + ik] = product * weight * dk * ModuleBase::FOUR_PI / 3.0; + } + } +#ifdef __CUDA + if (use_gpu) + { + const std::size_t size = static_cast(count) * kmesh; + base_device::memory::synchronize_memory_op< + double, base_device::DEVICE_GPU, base_device::DEVICE_CPU>()(input_ptr, input.data(), size); + } +#endif +#ifdef __CUDA + if (use_gpu) + { + const double one = 1.0; + const double zero = 0.0; + container::kernels::blas_gemm()( + 'T', + 'N', + columns, + count, + kmesh, + &one, + table_ptr, + kmesh, + input_ptr, + kmesh, + &zero, + output_ptr, + columns); + } + else +#endif + { + BlasConnector::gemm_cm( + 'T', + 'N', + columns, + count, + kmesh, + 1.0, + table_ptr, + kmesh, + input_ptr, + kmesh, + 0.0, + output_ptr, + columns, + base_device::AbacusDevice_t::CpuDevice); + } +#ifdef __CUDA + if (use_gpu) + { + const std::size_t size = static_cast(count) * columns; + base_device::memory::synchronize_memory_op< + double, base_device::DEVICE_CPU, base_device::DEVICE_GPU>()(output.data(), output_ptr, size); + } +#endif + for (int it = 0; it < count; ++it) + { + auto &values = tasks[start + it]->Table_r.at(l); + auto &derivatives = tasks[start + it]->Table_dr.at(l); + for (int ir = 0; ir < rows; ++ir) + { + values[radial_start + ir] = output[it * columns + 2 * ir]; + derivatives[radial_start + ir] = output[it * columns + 2 * ir + 1]; + } + } + } + } + // Preserve the analytic R=0 convention used by cal_overlap. + if (l > 0) + { + std::vector integrand(kmesh); + for (Orb11 *orbital : tasks) + { + for (int ik = 0; ik < kmesh; ++ik) + { + integrand[ik] = radial_product(orbital->nA, orbital->nB, ik) * std::pow(kpoint[ik], l); + } + double integral = 0.0; + ModuleBase::Integral::Simpson_Integral( + kmesh, + integrand.data(), + dk, + integral); + orbital->Table_r.at(l)[0] = + ModuleBase::FOUR_PI / ModuleBase::Mathzone_Add1::dualfac(2 * l + 1) * integral; + } + } + } + ModuleBase::timer::end("Center2_Orb", "init_radial_table"); +} + double Center2_Orb::Orb11::cal_overlap(const ModuleBase::Vector3& RA, const ModuleBase::Vector3& RB, const int& mA, diff --git a/source/source_lcao/center2orb_orb11.h b/source/source_lcao/center2orb_orb11.h index a7871eb1728..7931c735a4f 100644 --- a/source/source_lcao/center2orb_orb11.h +++ b/source/source_lcao/center2orb_orb11.h @@ -27,6 +27,9 @@ class Center2_Orb::Orb11 void init_radial_table(); void init_radial_table(const std::set& radials); // unit: Bohr/MOT.dr + /// Build full tables in bounded batches, using the same Simpson quadrature. + static void init_radial_table(const std::vector &orbitals, const bool use_gpu); + double cal_overlap(const ModuleBase::Vector3& RA, const ModuleBase::Vector3& RB, // unit: Bohr const int& mA, diff --git a/source/source_lcao/center2orb_orb21.cpp b/source/source_lcao/center2orb_orb21.cpp index 261c7402526..9ffe98256a9 100644 --- a/source/source_lcao/center2orb_orb21.cpp +++ b/source/source_lcao/center2orb_orb21.cpp @@ -1,6 +1,5 @@ //========================================================= // AUTHOR : Peize Lin -#include "source_io/module_parameter/parameter.h" // DATE : 2016-01-24 //========================================================= @@ -20,8 +19,9 @@ Center2_Orb::Orb21::Orb21(const Numerical_Orbital_Lm& nA1_in, { } -void Center2_Orb::Orb21::init_radial_table() +void Center2_Orb::Orb21::prepare_radial_table() { + auto products = std::make_shared(); const Numerical_Orbital_Lm& nA_short = (this->nA1.getNr() <= this->nA2.getNr()) ? this->nA1 : this->nA2; std::vector nA_tmp(nA_short.getNr()); @@ -39,67 +39,102 @@ void Center2_Orb::Orb21::init_radial_table() continue; } - this->nA[LA].set_orbital_info(nA_short.getLabel(), - nA_short.getType(), - LA, - 1, // N? - nA_short.getNr(), - nA_short.getRab(), - nA_short.getRadial(), - Numerical_Orbital_Lm::Psi_Type::Psi, - nA_tmp.data(), - nA_short.getNk(), - nA_short.getDk(), - nA_short.getDruniform(), - false, - true, - PARAM.inp.cal_force); // mohan add 2021-05-07 - - this->orb11s.insert(std::make_pair(LA, Center2_Orb::Orb11(this->nA[LA], nB, psb_, this->MGT))); - - this->orb11s.at(LA).init_radial_table(); + (*products)[LA].set_orbital_info( + nA_short.getLabel(), + nA_short.getType(), + LA, + 1, // N? + nA_short.getNr(), + nA_short.getRab(), + nA_short.getRadial(), + Numerical_Orbital_Lm::Psi_Type::Psi, + nA_tmp.data(), + nA_short.getNk(), + nA_short.getDk(), + nA_short.getDruniform(), + false, + true, + false); // Product orbitals use reciprocal-space gradients, not zty. + } + this->nA = std::move(products); + this->bind_product_orbitals(); +} + +void Center2_Orb::Orb21::bind_product_orbitals() +{ + this->orb11s.clear(); + for (const auto &product : *this->nA) + { + this->orb11s.emplace( + product.first, Orb11( + product.second, + this->nB, + this->psb_, + this->MGT)); + } +} + +void Center2_Orb::Orb21::init_radial_table() +{ + this->prepare_radial_table(); + for (auto &orbital : this->orb11s) + { + orbital.second.init_radial_table(); } } void Center2_Orb::Orb21::init_radial_table(const std::set& radials) { - const Numerical_Orbital_Lm& nA_short = (this->nA1.getNr() <= this->nA2.getNr()) ? this->nA1 : this->nA2; + this->prepare_radial_table(); + for (auto &orbital : this->orb11s) + { + orbital.second.init_radial_table(radials); + } +} - std::vector nA_tmp(nA_short.getNr()); - for (size_t ir = 0; ir != nA_tmp.size(); ++ir) +void Center2_Orb::Orb21::init_radial_table(const std::vector &orbitals, const bool use_gpu) +{ + const auto pairs = prepare_radial_tables(orbitals); + Orb11::init_radial_table(pairs, use_gpu); +} + +void Center2_Orb::Orb21::init_radial_table(const std::vector &orbitals, const std::set &radials) +{ + const auto pairs = prepare_radial_tables(orbitals); + for (Orb11 *pair : pairs) { - nA_tmp[ir] = this->nA1.getPsi(ir) * this->nA2.getPsi(ir); + pair->init_radial_table(radials); } +} - const int LA1 = this->nA1.getL(); - const int LA2 = this->nA2.getL(); - for (int LA = std::abs(LA1 - LA2); LA <= LA1 + LA2; ++LA) +std::vector Center2_Orb::Orb21::prepare_radial_tables(const std::vector &orbitals) +{ + std::vector pairs; + // A product depends on its two orbitals on center A, not the orbital on + // center B. Share it within this build and retain ownership with each user. + std::map< + const Numerical_Orbital_Lm *, std::map>> + products; + for (Orb21 *orbital : orbitals) { - if ((LA - std::abs(LA1 - LA2)) % 2 == 1) // if LA+LB-LAB == odd, then Gaunt_Coefficients = 0 + // Orbital transforms grow the shared Bessel cache, so prepare them serially. + auto &product = products[&orbital->nA1][&orbital->nA2]; + if (!product) { - continue; + orbital->prepare_radial_table(); + product = orbital->nA; + } + else + { + orbital->nA = product; + orbital->bind_product_orbitals(); + } + for (auto &pair : orbital->orb11s) + { + pairs.push_back(&pair.second); } - - this->nA[LA].set_orbital_info(nA_short.getLabel(), - nA_short.getType(), - LA, - 1, // N? - nA_short.getNr(), - nA_short.getRab(), - nA_short.getRadial(), - Numerical_Orbital_Lm::Psi_Type::Psi, - nA_tmp.data(), - nA_short.getNk(), - nA_short.getDk(), - nA_short.getDruniform(), - false, - true, - PARAM.inp.cal_force); // mohan add 2021-05-07 - - this->orb11s.insert(std::make_pair(LA, Center2_Orb::Orb11(this->nA[LA], nB, psb_, this->MGT))); - - this->orb11s.at(LA).init_radial_table(radials); } + return pairs; } double Center2_Orb::Orb21::cal_overlap(const ModuleBase::Vector3& RA, diff --git a/source/source_lcao/center2orb_orb21.h b/source/source_lcao/center2orb_orb21.h index bc7cb5c7fa1..ed813991b10 100644 --- a/source/source_lcao/center2orb_orb21.h +++ b/source/source_lcao/center2orb_orb21.h @@ -13,6 +13,7 @@ #include "source_basis/module_ao/orb_gaunt_table.h" #include +#include #include #include @@ -28,6 +29,9 @@ class Center2_Orb::Orb21 void init_radial_table(); void init_radial_table(const std::set& radials); // unit: Bohr/MOT.dr + static void init_radial_table(const std::vector &orbitals, const bool use_gpu); + static void init_radial_table(const std::vector &orbitals, const std::set &radials); + double cal_overlap(const ModuleBase::Vector3& RA, const ModuleBase::Vector3& RB, // unit: Bohr const int& mA1, @@ -48,8 +52,13 @@ class Center2_Orb::Orb21 const ModuleBase::Sph_Bessel_Recursive::D2* psb_ = nullptr; const ORB_gaunt_table& MGT; - std::map nA; + using ProductOrbitals = std::map; + std::shared_ptr nA; std::map orb11s; + + void prepare_radial_table(); + void bind_product_orbitals(); + static std::vector prepare_radial_tables(const std::vector &orbitals); }; // this->orb11s[LA].Table_r[LAB][ir] diff --git a/source/source_lcao/module_ri/ewald_vq.hpp b/source/source_lcao/module_ri/ewald_vq.hpp index d2c80047c68..d6f6e86a230 100644 --- a/source/source_lcao/module_ri/ewald_vq.hpp +++ b/source/source_lcao/module_ri/ewald_vq.hpp @@ -68,8 +68,16 @@ void Ewald_Vq::init(const UnitCell& ucell, const ModuleBase::Element_Basis_Index::Range range_abfs = ModuleBase::Element_Basis_Index::construct_range(abfs_in); this->index_abfs = ModuleBase::Element_Basis_Index::construct_index(range_abfs); - this->cv - .set_orbitals(ucell, orb, this->g_lcaos, this->g_abfs, this->g_abfs_ccp, kmesh_times_in, MGT_in, false); + this->cv.set_orbitals( + ucell, + orb, + this->g_lcaos, + this->g_abfs, + this->g_abfs_ccp, + kmesh_times_in, + MGT_in, + false, + false); this->gaunt.create(MGT_in->Gaunt_Coefficients.getBound1(), MGT_in->Gaunt_Coefficients.getBound2(), MGT_in->Gaunt_Coefficients.getBound3()); diff --git a/source/source_lcao/module_ri/exx_lri.h b/source/source_lcao/module_ri/exx_lri.h index 95f5e09839f..c80e6595434 100644 --- a/source/source_lcao/module_ri/exx_lri.h +++ b/source/source_lcao/module_ri/exx_lri.h @@ -90,10 +90,19 @@ class Exx_LRI const K_Vectors &kv_in, const LCAO_Orbitals& orb, const std::vector>>& abfs_in = {}); + void init( + const MPI_Comm &mpi_comm_in, + const UnitCell &ucell, + const K_Vectors &kv_in, + const LCAO_Orbitals &orb, + const std::vector>> &abfs_in, + const bool use_gpu); /// Refresh changed Coulomb kernels while retaining the initialized basis and Gaunt table. - void refresh_coulomb(const UnitCell& ucell, - const LCAO_Orbitals& orb, - const CoulombSettings& settings); + void refresh_coulomb( + const UnitCell &ucell, + const LCAO_Orbitals &orb, + const CoulombSettings &settings, + const bool use_gpu); void init_spencer(const MPI_Comm& mpi_comm_in, const UnitCell& ucell, const K_Vectors& kv_in, diff --git a/source/source_lcao/module_ri/exx_lri.hpp b/source/source_lcao/module_ri/exx_lri.hpp index 83e2684aea2..926f64c915c 100644 --- a/source/source_lcao/module_ri/exx_lri.hpp +++ b/source/source_lcao/module_ri/exx_lri.hpp @@ -38,6 +38,24 @@ void Exx_LRI::init(const MPI_Comm &mpi_comm_in, const K_Vectors &kv_in, const LCAO_Orbitals& orb, const std::vector>>& abfs_in) +{ + this->init( + mpi_comm_in, + ucell, + kv_in, + orb, + abfs_in, + false); +} + +template +void Exx_LRI::init( + const MPI_Comm &mpi_comm_in, + const UnitCell &ucell, + const K_Vectors &kv_in, + const LCAO_Orbitals &orb, + const std::vector>> &abfs_in, + const bool use_gpu) { ModuleBase::TITLE("Exx_LRI","init"); ModuleBase::timer::start("Exx_LRI", "init"); @@ -73,15 +91,21 @@ void Exx_LRI::init(const MPI_Comm &mpi_comm_in, this->exx_objs.clear(); const auto settings = RI_Util::update_coulomb_settings(this->info.coulomb_param, ucell, this->p_kv); this->MGT = std::make_shared(); - this->refresh_coulomb(ucell, orb, settings); + this->refresh_coulomb( + ucell, + orb, + settings, + use_gpu); ModuleBase::timer::end("Exx_LRI", "init"); } -template -void Exx_LRI::refresh_coulomb(const UnitCell& ucell, - const LCAO_Orbitals& orb, - const CoulombSettings& settings) +template +void Exx_LRI::refresh_coulomb( + const UnitCell &ucell, + const LCAO_Orbitals &orb, + const CoulombSettings &settings, + const bool use_gpu) { ModuleBase::TITLE("Exx_LRI", "refresh_coulomb"); ModuleBase::timer::start("Exx_LRI", "refresh_coulomb"); @@ -113,8 +137,16 @@ void Exx_LRI::refresh_coulomb(const UnitCell& ucell, auto& object = this->exx_objs[settings_list.first]; object.abfs_ccp = Conv_Coulomb_Pot_K::cal_orbs_ccp( this->abfs, settings_list.second.second, this->info.ccp_rmesh_times); - object.cv.set_orbitals(ucell, orb, this->lcaos, this->abfs, object.abfs_ccp, - this->info.kmesh_times, this->MGT, settings_list.second.first); + object.cv.set_orbitals( + ucell, + orb, + this->lcaos, + this->abfs, + object.abfs_ccp, + this->info.kmesh_times, + this->MGT, + settings_list.second.first, + use_gpu); object.cv.set_info_ri(&this->info); if (settings_list.first == Conv_Coulomb_Pot_K::Coulomb_Method::Ewald) { @@ -191,7 +223,8 @@ void Exx_LRI::init_spencer( this->exx_objs[Conv_Coulomb_Pot_K::Coulomb_Method::Center2].abfs_ccp, this->info.kmesh_times, this->MGT, - center2_settings->second.first); + center2_settings->second.first, + false); this->exx_objs[Conv_Coulomb_Pot_K::Coulomb_Method::Center2].cv.set_info_ri(&this->info); ModuleBase::timer::end("Exx_LRI", "init_spencer"); diff --git a/source/source_lcao/module_ri/exx_lri_interface.h b/source/source_lcao/module_ri/exx_lri_interface.h index 8b5bc61cf64..9ee6d6a05e4 100644 --- a/source/source_lcao/module_ri/exx_lri_interface.h +++ b/source/source_lcao/module_ri/exx_lri_interface.h @@ -68,10 +68,12 @@ class Exx_LRI_Interface // Processes in ESolver_KS_LCAO /// @brief in init: Exx_LRI::init() - void init(const MPI_Comm &mpi_comm, - const UnitCell &ucell, - const K_Vectors &kv, - const LCAO_Orbitals& orb); + void init( + const MPI_Comm &mpi_comm, + const UnitCell &ucell, + const K_Vectors &kv, + const LCAO_Orbitals &orb, + const bool use_gpu); /// @brief: in cal_exx_ions: Exx_LRI::cal_exx_ions() void cal_exx_ions(const UnitCell& ucell, const bool write_cv = false); @@ -96,8 +98,15 @@ class Exx_LRI_Interface void refresh_symmetry(const K_Vectors& kv, const UnitCell& ucell, const Parallel_2D& pv); /// @brief in beforescf: set xc type, opt_orb, do DM mixing - void exx_beforescf(const int istep, const K_Vectors& kv, const Charge_Mixing& chgmix, const UnitCell& ucell, const LCAO_Orbitals& orb, - const Parallel_2D& pv, const bool update_symmetry); + void exx_beforescf( + const int istep, + const K_Vectors &kv, + const Charge_Mixing &chgmix, + const UnitCell &ucell, + const LCAO_Orbitals &orb, + const Parallel_2D &pv, + const bool update_symmetry, + const bool use_gpu); /// @brief in eachiterinit: do DM mixing and calculate Hexx when entering 2nd SCF void exx_eachiterinit(const int istep, diff --git a/source/source_lcao/module_ri/exx_lri_interface.hpp b/source/source_lcao/module_ri/exx_lri_interface.hpp index b08514faad0..12b1d2f9467 100644 --- a/source/source_lcao/module_ri/exx_lri_interface.hpp +++ b/source/source_lcao/module_ri/exx_lri_interface.hpp @@ -18,14 +18,23 @@ #include #include -template -void Exx_LRI_Interface::init(const MPI_Comm &mpi_comm, - const UnitCell &ucell, - const K_Vectors &kv, - const LCAO_Orbitals& orb) +template +void Exx_LRI_Interface::init( + const MPI_Comm &mpi_comm, + const UnitCell &ucell, + const K_Vectors &kv, + const LCAO_Orbitals &orb, + const bool use_gpu) { ModuleBase::TITLE("Exx_LRI_Interface","init"); - this->exx_ptr->init(mpi_comm, ucell, kv, orb); + const std::vector>> abfs; + this->exx_ptr->init( + mpi_comm, + ucell, + kv, + orb, + abfs, + use_gpu); this->flag_finish.init = true; } @@ -110,14 +119,16 @@ void Exx_LRI_Interface::refresh_symmetry( } } -template -void Exx_LRI_Interface::exx_beforescf(const int istep, - const K_Vectors& kv, - const Charge_Mixing& chgmix, - const UnitCell& ucell, - const LCAO_Orbitals& orb, - const Parallel_2D& pv, - const bool update_symmetry) +template +void Exx_LRI_Interface::exx_beforescf( + const int istep, + const K_Vectors &kv, + const Charge_Mixing &chgmix, + const UnitCell &ucell, + const LCAO_Orbitals &orb, + const Parallel_2D &pv, + const bool update_symmetry, + const bool use_gpu) { ModuleBase::TITLE("Exx_LRI_Interface","exx_beforescf"); #ifdef __MPI @@ -148,7 +159,11 @@ void Exx_LRI_Interface::exx_beforescf(const int istep, this->exx_ptr->get_info_ri().coulomb_param, ucell, &kv); if (coulomb_settings != this->exx_ptr->coulomb_settings) { - this->exx_ptr->refresh_coulomb(ucell, orb, coulomb_settings); + this->exx_ptr->refresh_coulomb( + ucell, + orb, + coulomb_settings, + use_gpu); } this->cal_exx_ions(ucell,PARAM.inp.out_ri_cv); diff --git a/source/source_lcao/module_ri/lri_cv.cpp b/source/source_lcao/module_ri/lri_cv.cpp index 41d273d0b5b..2829fdef6cb 100644 --- a/source/source_lcao/module_ri/lri_cv.cpp +++ b/source/source_lcao/module_ri/lri_cv.cpp @@ -33,17 +33,17 @@ LRI_CV::~LRI_CV() pthread_rwlock_destroy(&rwlock_dCw); } - -template +template void LRI_CV::set_orbitals( const UnitCell &ucell, - const LCAO_Orbitals& orb, + const LCAO_Orbitals &orb, const std::vector>> &lcaos_in, const std::vector>> &abfs_in, const std::vector>> &abfs_ccp_in, const double &kmesh_times, std::shared_ptr MGT, - const bool& init_C) + const bool &init_C, + const bool use_gpu) { ModuleBase::TITLE("LRI_CV", "set_orbitals"); ModuleBase::timer::start("LRI_CV", "set_orbitals"); @@ -72,9 +72,9 @@ void LRI_CV::set_orbitals( this->abfs_ccp, this->lcaos, this->lcaos, ucell, orb, kmesh_times); - this->m_abfs_abfs.init_radial_table(); + this->m_abfs_abfs.init_radial_table(use_gpu); if (init_C) { - this->m_abfslcaos_lcaos.init_radial_table(); + this->m_abfslcaos_lcaos.init_radial_table(use_gpu); } ModuleBase::timer::end("LRI_CV", "set_orbitals"); diff --git a/source/source_lcao/module_ri/lri_cv.h b/source/source_lcao/module_ri/lri_cv.h index 1103f6fcdd4..b3bfd83b116 100644 --- a/source/source_lcao/module_ri/lri_cv.h +++ b/source/source_lcao/module_ri/lri_cv.h @@ -38,13 +38,14 @@ class LRI_CV void set_orbitals( const UnitCell &ucell, - const LCAO_Orbitals& orb, + const LCAO_Orbitals &orb, const std::vector>> &lcaos_in, const std::vector>> &abfs_in, const std::vector>> &abfs_ccp_in, const double &kmesh_times, std::shared_ptr MGT, - const bool& init_C); + const bool &init_C, + const bool use_gpu); std::map>> cal_Vs( const UnitCell &ucell, diff --git a/source/source_lcao/module_ri/matrix_orbs11.cpp b/source/source_lcao/module_ri/matrix_orbs11.cpp index 2673535cddb..6430faf7460 100644 --- a/source/source_lcao/module_ri/matrix_orbs11.cpp +++ b/source/source_lcao/module_ri/matrix_orbs11.cpp @@ -90,23 +90,37 @@ void Matrix_Orbs11::init_radial(const LCAO_Orbitals& orb_A, const LCAO_Orbitals& } */ -void Matrix_Orbs11::init_radial_table() +void Matrix_Orbs11::init_radial_table() { this->init_radial_table(false); } + +void Matrix_Orbs11::init_radial_table(const bool use_gpu) { ModuleBase::TITLE("Matrix_Orbs11", "init_radial_table"); ModuleBase::timer::start("Matrix_Orbs11", "init_radial_table"); + std::vector orbitals; for (auto& coA: center2_orb11_s) { for (auto& coB: coA.second) { for (auto& coC: coB.second) { for (auto& coD: coC.second) { for (auto& coE: coD.second) { for (auto& coF: coE.second) { - coF.second.init_radial_table(); + if (use_gpu) + { + orbitals.push_back(&coF.second); + } + else + { + coF.second.init_radial_table(); + } } } } } } } + if (use_gpu) + { + Center2_Orb::Orb11::init_radial_table(orbitals, true); + } ModuleBase::timer::end("Matrix_Orbs11", "init_radial_table"); } diff --git a/source/source_lcao/module_ri/matrix_orbs11.h b/source/source_lcao/module_ri/matrix_orbs11.h index 0fbb6ca1cc7..87dd7208191 100644 --- a/source/source_lcao/module_ri/matrix_orbs11.h +++ b/source/source_lcao/module_ri/matrix_orbs11.h @@ -29,6 +29,7 @@ class Matrix_Orbs11 const double kmesh_times); // extend Kcut, keep dK void init_radial_table(); + void init_radial_table(const bool use_gpu); void init_radial_table(const std::map>>& Rs); // unit: ucell.lat0 enum class Matrix_Order diff --git a/source/source_lcao/module_ri/matrix_orbs21.cpp b/source/source_lcao/module_ri/matrix_orbs21.cpp index 963d7786b42..8ff0fa2fe99 100644 --- a/source/source_lcao/module_ri/matrix_orbs21.cpp +++ b/source/source_lcao/module_ri/matrix_orbs21.cpp @@ -116,10 +116,13 @@ void Matrix_Orbs21::init_radial(const std::vectorinit_radial_table(false); } + +void Matrix_Orbs21::init_radial_table(const bool use_gpu) { ModuleBase::TITLE("Matrix_Orbs21", "init_radial_table"); ModuleBase::timer::start("Matrix_Orbs21", "init_radial_table"); + std::vector orbitals; for (auto& coA: center2_orb21_s) { for (auto& coB: coA.second) @@ -136,7 +139,14 @@ void Matrix_Orbs21::init_radial_table() { for (auto& coH: coG.second) { - coH.second.init_radial_table(); + if (use_gpu) + { + orbitals.push_back(&coH.second); + } + else + { + coH.second.init_radial_table(); + } } } } @@ -145,6 +155,10 @@ void Matrix_Orbs21::init_radial_table() } } } + if (use_gpu) + { + Center2_Orb::Orb21::init_radial_table(orbitals, true); + } ModuleBase::timer::end("Matrix_Orbs21", "init_radial_table"); } @@ -173,6 +187,7 @@ void Matrix_Orbs21::init_radial_table(const std::map orbitals; for (auto& coC: *center2_orb21_sAB) { for (auto& coD: coC.second) @@ -185,13 +200,14 @@ void Matrix_Orbs21::init_radial_table(const std::map>>& Rs); // unit: ucell.lat0 enum class Matrix_Order diff --git a/source/source_lcao/module_rpa/rpa_lri_overlap.cpp b/source/source_lcao/module_rpa/rpa_lri_overlap.cpp index 1bb5e27ad6f..54ded9c4921 100644 --- a/source/source_lcao/module_rpa/rpa_lri_overlap.cpp +++ b/source/source_lcao/module_rpa/rpa_lri_overlap.cpp @@ -57,14 +57,16 @@ void RPA_LRI::cal_large_Cs(const UnitCell& ucell, const LCAO_Orbitals& { throw std::invalid_argument("RPA_LRI::cal_large_Cs expected a Center2 cut-Coulomb object after init_spencer."); } - center2_obj_it->second.cv.set_orbitals(ucell, - orb, - this->lcaos, - exx_cut_coulomb->abfs, - center2_obj_it->second.abfs_ccp, - this->info.kmesh_times, - this->MGT, // get MGT from exx_cut_coulomb and used in `cal_abfs_overlap` - true); + center2_obj_it->second.cv.set_orbitals( + ucell, + orb, + this->lcaos, + exx_cut_coulomb->abfs, + center2_obj_it->second.abfs_ccp, + this->info.kmesh_times, + this->MGT, // get MGT from exx_cut_coulomb and used in `cal_abfs_overlap` + true, + false); const std::array period_Vs = LRI_CV_Tools::cal_latvec_range(1 + this->ccp_rmesh_times_cut, ucell, orb_cutoff_); diff --git a/source/source_lcao/setup_exx.cpp b/source/source_lcao/setup_exx.cpp index 0c17b3d2d9b..ab547da9465 100644 --- a/source/source_lcao/setup_exx.cpp +++ b/source/source_lcao/setup_exx.cpp @@ -50,6 +50,11 @@ void Exx_NAO::before_runner( Exx_Info& exx_info) { #ifdef __EXX +#ifdef __CUDA + const bool use_gpu_tables = inp.device == "gpu"; +#else + const bool use_gpu_tables = false; +#endif if (inp.calculation == "scf" || inp.calculation == "relax" || inp.calculation == "cell-relax" || inp.calculation == "md") { @@ -63,12 +68,22 @@ void Exx_NAO::before_runner( // initialize 2-center radial tables for EXX-LRI if (exx_info.info_ri.real_number) { - this->exd->init(MPI_COMM_WORLD, ucell, kv, orb); + this->exd->init( + MPI_COMM_WORLD, + ucell, + kv, + orb, + use_gpu_tables); this->exd->exx_before_all_runners(kv, ucell, pv); } else { - this->exc->init(MPI_COMM_WORLD, ucell, kv, orb); + this->exc->init( + MPI_COMM_WORLD, + ucell, + kv, + orb, + use_gpu_tables); this->exc->exx_before_all_runners(kv, ucell, pv); } } @@ -80,11 +95,21 @@ void Exx_NAO::before_runner( { if (exx_info.info_ri.real_number) { - this->exd->init(MPI_COMM_WORLD, ucell, kv, orb); + this->exd->init( + MPI_COMM_WORLD, + ucell, + kv, + orb, + use_gpu_tables); } else { - this->exc->init(MPI_COMM_WORLD, ucell, kv, orb); + this->exc->init( + MPI_COMM_WORLD, + ucell, + kv, + orb, + use_gpu_tables); } } } @@ -104,6 +129,11 @@ void Exx_NAO::before_scf( Exx_Info& exx_info) { #ifdef __EXX +#ifdef __CUDA + const bool use_gpu_tables = inp.device == "gpu"; +#else + const bool use_gpu_tables = false; +#endif if (inp.calculation != "nscf") { const bool update_symmetry = inp.calculation == "cell-relax" @@ -111,11 +141,27 @@ void Exx_NAO::before_scf( && ModuleSymmetry::Symmetry::symm_flag == 1; if (exx_info.info_ri.real_number) { - this->exd->exx_beforescf(istep, kv, *p_chgmix, ucell, orb, pv, update_symmetry); + this->exd->exx_beforescf( + istep, + kv, + *p_chgmix, + ucell, + orb, + pv, + update_symmetry, + use_gpu_tables); } else { - this->exc->exx_beforescf(istep, kv, *p_chgmix, ucell, orb, pv, update_symmetry); + this->exc->exx_beforescf( + istep, + kv, + *p_chgmix, + ucell, + orb, + pv, + update_symmetry, + use_gpu_tables); } } else diff --git a/source/source_lcao/test/CMakeLists.txt b/source/source_lcao/test/CMakeLists.txt index 53fde5e20ad..57a5d27ce61 100644 --- a/source/source_lcao/test/CMakeLists.txt +++ b/source/source_lcao/test/CMakeLists.txt @@ -3,6 +3,41 @@ abacus_disable_feature_definitions(__CUDA) abacus_disable_feature_definitions(__ROCM) if(ENABLE_LCAO) + addtest( + TARGET + MODULE_LCAO_radial_tables + LIBS + parameter + base + device + container + SOURCES + test_center2orb_orb11.cpp + ../center2orb.cpp + ../center2orb_orb11.cpp + ../center2orb_orb21.cpp + ${ABACUS_SOURCE_DIR}/source_basis/module_ao/orb_atomic_lm.cpp + ${ABACUS_SOURCE_DIR}/source_basis/module_ao/orb_gaunt_table.cpp) + if(USE_CUDA) + addtest( + TARGET + MODULE_LCAO_radial_tables_gpu + KEEP_FEATURE_DEFINITIONS + __CUDA + LIBS + parameter + base + device + container + SOURCES + test_center2orb_orb11.cpp + ../center2orb.cpp + ../center2orb_orb11.cpp + ../center2orb_orb21.cpp + ${ABACUS_SOURCE_DIR}/source_basis/module_ao/orb_atomic_lm.cpp + ${ABACUS_SOURCE_DIR}/source_basis/module_ao/orb_gaunt_table.cpp) + set_tests_properties(MODULE_LCAO_radial_tables_gpu PROPERTIES LABELS gpu) + endif() addtest( TARGET MODULE_LCAO_init_dm_from_file_test diff --git a/source/source_lcao/test/test_center2orb_orb11.cpp b/source/source_lcao/test/test_center2orb_orb11.cpp new file mode 100644 index 00000000000..4ab791beb9a --- /dev/null +++ b/source/source_lcao/test/test_center2orb_orb11.cpp @@ -0,0 +1,521 @@ +#include "source_base/module_device/device.h" +#include "source_lcao/center2orb_orb11.h" +#include "source_lcao/center2orb_orb21.h" + +#include "gtest/gtest.h" +#include +#include +#include + +namespace +{ +Numerical_Orbital_Lm make_orbital( + const int l, + const int index, + const int nr, + const int nk) +{ + const double dr = 0.02; + const double cutoff = (nr - 1) * dr; + std::vector radial(nr); + std::vector rab(nr, dr); + std::vector values(nr); + for (int ir = 0; ir < nr; ++ir) + { + const double r = ir * dr; + radial[ir] = r; + values[ir] = std::pow(r, l) * std::pow(cutoff - r, 2) * std::exp(-(0.7 + 0.03 * index) * r); + } + Numerical_Orbital_Lm orbital; + const double *rab_ptr = rab.data(); + const double *radial_ptr = radial.data(); + const double *values_ptr = values.data(); + orbital.set_orbital_info( + "X", + 0, + l, + index, + nr, + rab_ptr, + radial_ptr, + Numerical_Orbital_Lm::Psi_Type::Psi, + values_ptr, + nk, + 0.1, + 0.01, + false, + true, + true); + return orbital; +} + +void compare_pairs(const bool use_gpu) +{ + // More than 128 same-grid pairs exercise full and partial batches. + std::vector basis; + for (int l = 0; l <= 3; ++l) + { + basis.push_back(make_orbital( + l, + l, + 101, + 65)); + } + basis.push_back(make_orbital( + 2, + 4, + 81, + 65)); + ORB_gaunt_table gaunt; + gaunt.init_Gaunt_CH(3); + gaunt.init_Gaunt(3); + ModuleBase::Sph_Bessel_Recursive::D2 bessel; + bessel.set_dx(0.001); + bessel.cal_jlx(7, 405, 65); + std::vector> reference; + std::vector> candidate; + std::vector tasks; + for (int i = 0; i < 140; ++i) + { + const int left = i < 130 ? 1 : i % 5; + const int right = i < 130 ? 2 : (i + 2) % 5; + reference.emplace_back(new Center2_Orb::Orb11( + basis[left], + basis[right], + &bessel, + gaunt)); + candidate.emplace_back(new Center2_Orb::Orb11( + basis[left], + basis[right], + &bessel, + gaunt)); + reference.back()->init_radial_table(); + tasks.push_back(candidate.back().get()); + } + Center2_Orb::Orb11::init_radial_table(tasks, use_gpu); + const ModuleBase::Vector3 origin(0.0, 0.0, 0.0); + for (int i = 0; i < 140; ++i) + { + SCOPED_TRACE(i); + const int left = i < 130 ? 1 : i % 5; + const int right = i < 130 ? 2 : (i + 2) % 5; + const int last_left = 2 * basis[left].getL(); + const int last_right = 2 * basis[right].getL(); + for (double distance : {0.0, + 0.037, + 0.73, + 1.91, + 3.87, + 4.2}) + { + const double x = 0.8 * distance; + const double y = 0.36 * distance; + const double z = 0.48 * distance; + const ModuleBase::Vector3 position(x, y, z); + for (int ma : {0, last_left}) + { + for (int mb : {0, last_right}) + { + const double expected = reference[i]->cal_overlap( + origin, + position, + ma, + mb); + const double actual = candidate[i]->cal_overlap( + origin, + position, + ma, + mb); + const double tolerance = 2e-11 * (1.0 + std::abs(expected)); + EXPECT_NEAR(actual, expected, tolerance); + const auto grad_expected = reference[i]->cal_grad_overlap( + origin, + position, + ma, + mb); + const auto grad_actual = candidate[i]->cal_grad_overlap( + origin, + position, + ma, + mb); + const double error = (grad_actual - grad_expected).norm(); + const double grad_tolerance = 2e-10 * (1.0 + grad_expected.norm()); + EXPECT_LT(error, grad_tolerance); + } + } + } + } +} + +void compare_products(const bool use_gpu) +{ + std::vector basis; + for (int l = 0; l <= 3; ++l) + { + basis.push_back(make_orbital( + l, + l, + 101, + 65)); + } + ORB_gaunt_table gaunt; + gaunt.init_Gaunt_CH(3); + gaunt.init_Gaunt(3); + ModuleBase::Sph_Bessel_Recursive::D2 bessel; + bessel.set_dx(0.001); + bessel.cal_jlx(7, 405, 65); + std::vector> reference; + std::vector> candidate; + std::vector> selective; + std::vector tasks; + std::vector selective_tasks; + for (int i = 0; i < 4; ++i) + { + reference.emplace_back(new Center2_Orb::Orb21( + basis[1], + basis[2], + basis[i], + &bessel, + gaunt)); + candidate.emplace_back(new Center2_Orb::Orb21( + basis[1], + basis[2], + basis[i], + &bessel, + gaunt)); + selective.emplace_back(new Center2_Orb::Orb21( + basis[1], + basis[2], + basis[i], + &bessel, + gaunt)); + reference.back()->init_radial_table(); + tasks.push_back(candidate.back().get()); + selective_tasks.push_back(selective.back().get()); + } + Center2_Orb::Orb21::init_radial_table(tasks, use_gpu); + std::set radials{10000}; + for (size_t first : {0, + 3, + 72, + 190, + 386}) + { + for (size_t offset = 0; offset < 6; ++offset) + { + radials.insert(first + offset); + } + } + Center2_Orb::Orb21::init_radial_table(selective_tasks, radials); + const ModuleBase::Vector3 origin(0.0, 0.0, 0.0); + for (int i = 0; i < 4; ++i) + { + for (double distance : {0.0, + 0.037, + 0.73, + 1.91, + 3.87}) + { + const double x = 0.8 * distance; + const double y = 0.36 * distance; + const double z = 0.48 * distance; + const ModuleBase::Vector3 position(x, y, z); + const double expected = reference[i]->cal_overlap( + origin, + position, + 1, + 2, + i); + const double actual = candidate[i]->cal_overlap( + origin, + position, + 1, + 2, + i); + const double tolerance = 2e-11 * (1.0 + std::abs(expected)); + EXPECT_NEAR(actual, expected, tolerance); + const double selective_actual = selective[i]->cal_overlap( + origin, + position, + 1, + 2, + i); + EXPECT_NEAR(selective_actual, expected, tolerance); + const auto grad_expected = reference[i]->cal_grad_overlap( + origin, + position, + 1, + 2, + i); + const auto grad_actual = candidate[i]->cal_grad_overlap( + origin, + position, + 1, + 2, + i); + const double error = (grad_actual - grad_expected).norm(); + const double grad_tolerance = 2e-10 * (1.0 + grad_expected.norm()); + EXPECT_LT(error, grad_tolerance); + const auto selective_grad = selective[i]->cal_grad_overlap( + origin, + position, + 1, + 2, + i); + const double selective_error = (selective_grad - grad_expected).norm(); + EXPECT_LT(selective_error, grad_tolerance); + } + } +} + +void compare_high_l_products(const bool use_gpu) +{ + // Heavy-element auxiliary bases include l=6; multiplying by an f orbital + // requires product angular momenta up to 9 and Bessel functions up to 13. + const auto left = make_orbital( + 6, + 0, + 101, + 65); + const auto middle = make_orbital( + 3, + 1, + 101, + 65); + const auto right = make_orbital( + 3, + 2, + 81, + 65); + ORB_gaunt_table gaunt; + gaunt.init_Gaunt_CH(9); + gaunt.init_Gaunt(9); + ModuleBase::Sph_Bessel_Recursive::D2 bessel; + bessel.set_dx(0.001); + bessel.cal_jlx(13, 405, 65); + Center2_Orb::Orb21 reference( + left, + middle, + right, + &bessel, + gaunt); + Center2_Orb::Orb21 candidate( + left, + middle, + right, + &bessel, + gaunt); + reference.init_radial_table(); + const std::vector tasks{&candidate}; + Center2_Orb::Orb21::init_radial_table(tasks, use_gpu); + const ModuleBase::Vector3 origin(0.0, 0.0, 0.0); + for (double distance : {0.0, + 0.037, + 0.73, + 1.91, + 3.57, + 3.8}) + { + const double x = 0.8 * distance; + const double y = 0.36 * distance; + const double z = 0.48 * distance; + const ModuleBase::Vector3 position(x, y, z); + for (int ma : {0, 6, 12}) + { + for (int mb : {0, 3, 6}) + { + for (int mc : {0, 3, 6}) + { + const double expected = reference.cal_overlap( + origin, + position, + ma, + mb, + mc); + const double actual = candidate.cal_overlap( + origin, + position, + ma, + mb, + mc); + const double tolerance = 2e-11 * (1.0 + std::abs(expected)); + EXPECT_NEAR(actual, expected, tolerance); + const auto grad_expected = reference.cal_grad_overlap( + origin, + position, + ma, + mb, + mc); + const auto grad_actual = candidate.cal_grad_overlap( + origin, + position, + ma, + mb, + mc); + const double error = (grad_actual - grad_expected).norm(); + const double grad_tolerance = 2e-10 * (1.0 + grad_expected.norm()); + EXPECT_LT(error, grad_tolerance); + } + } + } + } +} + +void compare_radial_tiles(const bool use_gpu) +{ + // The paired value/derivative Bessel matrix exceeds the 128 MiB workspace + // limit. Check interpolation across its radial tile boundary and tail. + const auto orbital = make_orbital( + 0, + 0, + 527, + 4097); + ORB_gaunt_table gaunt; + gaunt.init_Gaunt_CH(0); + gaunt.init_Gaunt(0); + ModuleBase::Sph_Bessel_Recursive::D2 bessel; + bessel.set_dx(0.001); + bessel.cal_jlx(1, 2109, 4097); + Center2_Orb::Orb11 reference( + orbital, + orbital, + &bessel, + gaunt); + Center2_Orb::Orb11 candidate( + orbital, + orbital, + &bessel, + gaunt); + reference.init_radial_table(); + const std::vector tasks{&candidate}; + Center2_Orb::Orb11::init_radial_table(tasks, use_gpu); + const ModuleBase::Vector3 origin(0.0, 0.0, 0.0); + for (double distance : {0.0, + 0.037, + 1.91, + 20.465, + 20.475, + 21.035, + 21.1}) + { + const ModuleBase::Vector3 position(distance, 0.0, 0.0); + const double expected = reference.cal_overlap( + origin, + position, + 0, + 0); + const double actual = candidate.cal_overlap( + origin, + position, + 0, + 0); + const double tolerance = 2e-11 * (1.0 + std::abs(expected)); + EXPECT_NEAR(actual, expected, tolerance); + const auto grad_expected = reference.cal_grad_overlap( + origin, + position, + 0, + 0); + const auto grad_actual = candidate.cal_grad_overlap( + origin, + position, + 0, + 0); + const double error = (grad_actual - grad_expected).norm(); + const double grad_tolerance = 2e-10 * (1.0 + grad_expected.norm()); + EXPECT_LT(error, grad_tolerance); + } +} +} // namespace + +TEST(Center2Orb11, BatchedCpuMatchesIndividualTables) +{ + compare_pairs(false); + compare_products(false); + compare_high_l_products(false); + compare_radial_tiles(false); +} + +TEST(Center2Orb21, ProductGradientMatchesFiniteDifference) +{ + const auto left = make_orbital( + 2, + 0, + 101, + 65); + const auto middle = make_orbital( + 1, + 1, + 101, + 65); + const auto right = make_orbital( + 3, + 2, + 101, + 65); + ORB_gaunt_table gaunt; + gaunt.init_Gaunt_CH(3); + gaunt.init_Gaunt(3); + ModuleBase::Sph_Bessel_Recursive::D2 bessel; + bessel.set_dx(0.001); + bessel.cal_jlx(7, 405, 65); + Center2_Orb::Orb21 orbital( + left, + middle, + right, + &bessel, + gaunt); + orbital.init_radial_table(); + const ModuleBase::Vector3 origin(0.0, 0.0, 0.0); + const ModuleBase::Vector3 position(0.813, 0.321, 0.473); + const double step = 1e-5; + for (int ma : {0, 2, 4}) + { + for (int mb : {0, 1, 2}) + { + for (int mc : {0, 3, 6}) + { + const auto gradient = orbital.cal_grad_overlap( + origin, + position, + ma, + mb, + mc); + auto plus = position; + auto minus = position; + plus.x += step; + minus.x -= step; + const double upper = orbital.cal_overlap( + origin, + plus, + ma, + mb, + mc); + const double lower = orbital.cal_overlap( + origin, + minus, + ma, + mb, + mc); + const double finite_difference = (upper - lower) / (2 * step); + const double tolerance = 2e-7 * (1.0 + std::abs(finite_difference)); + EXPECT_NEAR(gradient.x, finite_difference, tolerance); + } + } + } +} + +#ifdef __CUDA +TEST(Center2Orb11, BatchedGpuMatchesIndividualTables) +{ + const bool available = base_device::information::probe_gpu_availability(); + if (!available) + { + GTEST_SKIP() << "A CUDA device is required"; + } + compare_pairs(true); + compare_products(true); + compare_high_l_products(true); + compare_radial_tiles(true); +} +#endif From 026ea367ba60741fc7c5b4e7f8b2c77f21eca552 Mon Sep 17 00:00:00 2001 From: Jiacheng Xu <13862180016@163.com> Date: Mon, 2 Nov 2026 18:37:03 +0800 Subject: [PATCH 2/4] ci(cuda): run radial table GPU test --- .github/workflows/cuda.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/cuda.yml b/.github/workflows/cuda.yml index 3ed2cd15224..b00c17f2258 100644 --- a/.github/workflows/cuda.yml +++ b/.github/workflows/cuda.yml @@ -105,7 +105,7 @@ jobs: GTEST_COLOR: 'yes' OMP_NUM_THREADS: '2' run: | - ctest --test-dir build -V --timeout 1700 -R '^(MODULE_LCAO_tddft_radial_interpolation_cuda_test|MODULE_LCAO_tddft_snap_psibeta_half_test)$' + ctest --test-dir build -V --timeout 1700 -R '^(MODULE_LCAO_tddft_radial_interpolation_cuda_test|MODULE_LCAO_tddft_snap_psibeta_half_test|MODULE_LCAO_radial_tables_gpu)$' - name: Test 11_PW_GPU run: | From a853060efb2ab0e4bb10eb050af3c770e2bc3989 Mon Sep 17 00:00:00 2001 From: Jiacheng Xu <13862180016@163.com> Date: Mon, 2 Nov 2026 22:21:53 +0800 Subject: [PATCH 3/4] fix(lcao): preserve selectively initialized radial tables Keep existing product orbitals and radial entries when selective initialization is called again with additional radii. Prepare only new objects in selective batches so existing Orb11 references remain valid. Add a regression for individual and mixed-batch initialization, checking overlaps and gradients against complete tables. --- source/source_lcao/center2orb_orb21.cpp | 19 ++++-- source/source_lcao/center2orb_orb21.h | 2 + .../test/test_center2orb_orb11.cpp | 62 +++++++++++++++++++ 3 files changed, 79 insertions(+), 4 deletions(-) diff --git a/source/source_lcao/center2orb_orb21.cpp b/source/source_lcao/center2orb_orb21.cpp index 9ffe98256a9..51a5e157d00 100644 --- a/source/source_lcao/center2orb_orb21.cpp +++ b/source/source_lcao/center2orb_orb21.cpp @@ -85,7 +85,10 @@ void Center2_Orb::Orb21::init_radial_table() void Center2_Orb::Orb21::init_radial_table(const std::set& radials) { - this->prepare_radial_table(); + if (!this->nA) + { + this->prepare_radial_table(); + } for (auto &orbital : this->orb11s) { orbital.second.init_radial_table(radials); @@ -100,10 +103,18 @@ void Center2_Orb::Orb21::init_radial_table(const std::vector &orbitals, void Center2_Orb::Orb21::init_radial_table(const std::vector &orbitals, const std::set &radials) { - const auto pairs = prepare_radial_tables(orbitals); - for (Orb11 *pair : pairs) + std::vector uninitialized; + for (Orb21 *orbital : orbitals) + { + if (!orbital->nA) + { + uninitialized.push_back(orbital); + } + } + prepare_radial_tables(uninitialized); + for (Orb21 *orbital : orbitals) { - pair->init_radial_table(radials); + orbital->init_radial_table(radials); } } diff --git a/source/source_lcao/center2orb_orb21.h b/source/source_lcao/center2orb_orb21.h index ed813991b10..6932a2134eb 100644 --- a/source/source_lcao/center2orb_orb21.h +++ b/source/source_lcao/center2orb_orb21.h @@ -27,9 +27,11 @@ class Center2_Orb::Orb21 const ORB_gaunt_table& MGT_in); void init_radial_table(); + /// Selective calls retain previously populated entries for the same orbitals. void init_radial_table(const std::set& radials); // unit: Bohr/MOT.dr static void init_radial_table(const std::vector &orbitals, const bool use_gpu); + /// Selective batches retain each orbital's previously populated entries. static void init_radial_table(const std::vector &orbitals, const std::set &radials); double cal_overlap(const ModuleBase::Vector3& RA, diff --git a/source/source_lcao/test/test_center2orb_orb11.cpp b/source/source_lcao/test/test_center2orb_orb11.cpp index 4ab791beb9a..b7984085a08 100644 --- a/source/source_lcao/test/test_center2orb_orb11.cpp +++ b/source/source_lcao/test/test_center2orb_orb11.cpp @@ -436,6 +436,68 @@ TEST(Center2Orb11, BatchedCpuMatchesIndividualTables) compare_radial_tiles(false); } +TEST(Center2Orb21, SelectiveInitializationAccumulates) +{ + const Numerical_Orbital_Lm left = make_orbital(1, 0, 101, 65); + const Numerical_Orbital_Lm middle = make_orbital(2, 1, 101, 65); + const Numerical_Orbital_Lm right = make_orbital(1, 2, 101, 65); + ORB_gaunt_table gaunt; + gaunt.init_Gaunt_CH(3); + gaunt.init_Gaunt(3); + ModuleBase::Sph_Bessel_Recursive::D2 bessel; + bessel.set_dx(0.001); + bessel.cal_jlx(7, 405, 65); + Center2_Orb::Orb21 reference(left, middle, right, &bessel, gaunt); + Center2_Orb::Orb21 individual(left, middle, right, &bessel, gaunt); + Center2_Orb::Orb21 batched(left, middle, right, &bessel, gaunt); + Center2_Orb::Orb21 additional(left, middle, right, &bessel, gaunt); + reference.init_radial_table(); + + const std::set first_radials{72, 73, 74, 75, 76, 77}; + const std::set second_radials{190, 191, 192, 193, 194, 195}; + individual.init_radial_table(first_radials); + individual.init_radial_table(second_radials); + std::vector tasks{&batched}; + Center2_Orb::Orb21::init_radial_table(tasks, first_radials); + // A later batch may contain both initialized and new orbitals. + tasks.push_back(&additional); + Center2_Orb::Orb21::init_radial_table(tasks, second_radials); + additional.init_radial_table(first_radials); + + const ModuleBase::Vector3 origin(0.0, 0.0, 0.0); + for (const Center2_Orb::Orb21 *candidate : {&individual, &batched, &additional}) + { + for (const double distance : {0.73, 1.91}) + { + SCOPED_TRACE(distance); + const double x = 0.8 * distance; + const double y = 0.36 * distance; + const double z = 0.48 * distance; + const ModuleBase::Vector3 position(x, y, z); + for (int ma1 = 0; ma1 < 3; ++ma1) + { + for (int ma2 = 0; ma2 < 5; ++ma2) + { + for (int mb = 0; mb < 3; ++mb) + { + const double expected = reference.cal_overlap(origin, position, ma1, ma2, mb); + const double actual = candidate->cal_overlap(origin, position, ma1, ma2, mb); + const double tolerance = 2e-11 * (1.0 + std::abs(expected)); + EXPECT_NEAR(actual, expected, tolerance); + const ModuleBase::Vector3 grad_expected + = reference.cal_grad_overlap(origin, position, ma1, ma2, mb); + const ModuleBase::Vector3 grad_actual + = candidate->cal_grad_overlap(origin, position, ma1, ma2, mb); + const double error = (grad_actual - grad_expected).norm(); + const double grad_tolerance = 2e-10 * (1.0 + grad_expected.norm()); + EXPECT_LT(error, grad_tolerance); + } + } + } + } + } +} + TEST(Center2Orb21, ProductGradientMatchesFiniteDifference) { const auto left = make_orbital( From 75d39679bbce4fae9caccf0e48c8133b7e3e4664 Mon Sep 17 00:00:00 2001 From: Jiacheng Xu <13862180016@163.com> Date: Tue, 3 Nov 2026 10:19:41 +0800 Subject: [PATCH 4/4] test(lcao): cover shared product orbital lifetimes Exercise destruction and full reinitialization of the first Orb21 in a shared batch. Check the remaining object after a selective update against independent full tables, including retained and new overlaps and gradients. Validation: CPU/CUDA radial-table tests with one and four OpenMP threads (18 test executions passed), plus the ownership regression under AddressSanitizer with CUDA disabled. --- .../test/test_center2orb_orb11.cpp | 124 ++++++++++++++++++ 1 file changed, 124 insertions(+) diff --git a/source/source_lcao/test/test_center2orb_orb11.cpp b/source/source_lcao/test/test_center2orb_orb11.cpp index b7984085a08..b245e1351bc 100644 --- a/source/source_lcao/test/test_center2orb_orb11.cpp +++ b/source/source_lcao/test/test_center2orb_orb11.cpp @@ -498,6 +498,130 @@ TEST(Center2Orb21, SelectiveInitializationAccumulates) } } +TEST(Center2Orb21, SharedProductOrbitalsSurviveOwnerChanges) +{ + const Numerical_Orbital_Lm left = make_orbital( + 1, + 0, + 101, + 65); + const Numerical_Orbital_Lm middle = make_orbital( + 2, + 1, + 101, + 65); + const Numerical_Orbital_Lm right = make_orbital( + 1, + 2, + 101, + 65); + ORB_gaunt_table gaunt; + gaunt.init_Gaunt_CH(3); + gaunt.init_Gaunt(3); + ModuleBase::Sph_Bessel_Recursive::D2 bessel; + bessel.set_dx(0.001); + bessel.cal_jlx(7, 405, 65); + Center2_Orb::Orb21 reference( + left, + middle, + right, + &bessel, + gaunt); + reference.init_radial_table(); + + const std::set first_radials{72, + 73, + 74, + 75, + 76, + 77}; + const std::set second_radials{190, + 191, + 192, + 193, + 194, + 195}; + const ModuleBase::Vector3 origin(0.0, 0.0, 0.0); + for (const bool reinitialize_first : {false, true}) + { + const char *transition = reinitialize_first ? "reinitialize" : "destroy"; + SCOPED_TRACE(transition); + std::unique_ptr first(new Center2_Orb::Orb21( + left, + middle, + left, + &bessel, + gaunt)); + Center2_Orb::Orb21 survivor( + left, + middle, + right, + &bessel, + gaunt); + { + const std::vector tasks{first.get(), &survivor}; + Center2_Orb::Orb21::init_radial_table(tasks, first_radials); + } + if (reinitialize_first) + { + first->init_radial_table(); + } + else + { + first.reset(); + } + // New rows must still be able to read the shared product orbitals. + survivor.init_radial_table(second_radials); + + for (const double distance : {0.73, 1.91}) + { + SCOPED_TRACE(distance); + const double x = 0.8 * distance; + const double y = 0.36 * distance; + const double z = 0.48 * distance; + const ModuleBase::Vector3 position(x, y, z); + for (int ma1 = 0; ma1 < 3; ++ma1) + { + for (int ma2 = 0; ma2 < 5; ++ma2) + { + for (int mb = 0; mb < 3; ++mb) + { + const double expected = reference.cal_overlap( + origin, + position, + ma1, + ma2, + mb); + const double actual = survivor.cal_overlap( + origin, + position, + ma1, + ma2, + mb); + const double tolerance = 2e-11 * (1.0 + std::abs(expected)); + EXPECT_NEAR(actual, expected, tolerance); + const ModuleBase::Vector3 grad_expected = reference.cal_grad_overlap( + origin, + position, + ma1, + ma2, + mb); + const ModuleBase::Vector3 grad_actual = survivor.cal_grad_overlap( + origin, + position, + ma1, + ma2, + mb); + const double error = (grad_actual - grad_expected).norm(); + const double grad_tolerance = 2e-10 * (1.0 + grad_expected.norm()); + EXPECT_LT(error, grad_tolerance); + } + } + } + } + } +} + TEST(Center2Orb21, ProductGradientMatchesFiniteDifference) { const auto left = make_orbital(