From 68797f480bf57c1233a9f705b962043be42d20a3 Mon Sep 17 00:00:00 2001 From: Entropy-Enthalpy <55441397+Entropy-Enthalpy@users.noreply.github.com> Date: Fri, 4 Dec 2026 15:25:30 +0800 Subject: [PATCH 1/5] lcao: retain the CUDA memory pool through EXX work Add an opt-in pool release policy for CUDA EXX. Restore the previous release threshold after the run and preserve the ordinary allocator behavior on unsupported devices. --- source/source_esolver/esolver_ks_lcao.cpp | 4 + source/source_hamilt/module_xc/exx_info.cpp | 1 + source/source_hamilt/module_xc/exx_info_ri.h | 1 + .../module_parameter/input_parameter.h | 1 + .../module_parameter/read_inp_exx_dftu.cpp | 15 ++++ source/source_lcao/module_ri/exx_lri.h | 3 + source/source_lcao/module_ri/exx_lri.hpp | 4 +- source/source_lcao/module_ri/exx_lri_detail.h | 76 ++++++++++++++++++- 8 files changed, 103 insertions(+), 2 deletions(-) diff --git a/source/source_esolver/esolver_ks_lcao.cpp b/source/source_esolver/esolver_ks_lcao.cpp index b3e1b0b1762..cb0ff942467 100644 --- a/source/source_esolver/esolver_ks_lcao.cpp +++ b/source/source_esolver/esolver_ks_lcao.cpp @@ -375,6 +375,10 @@ void ESolver_KS_LCAO::after_all_runners(BaseCell &basecell) this->chr, hamilt_lcao, this->two_center_bundle_, this->orb_, this->pw_rho, this->pw_rhod, this->sf, this->locpp.vloc, this->exx_nao, this->exx_info_, this->solvent, GlobalV::ofs_running); +#ifdef __EXX + if (this->exx_nao.exd) this->exx_nao.exd->exx_ptr->finish_cuda_pool(); + if (this->exx_nao.exc) this->exx_nao.exc->exx_ptr->finish_cuda_pool(); +#endif ModuleBase::timer::end("ESolver_KS_LCAO", "after_all_runners"); } diff --git a/source/source_hamilt/module_xc/exx_info.cpp b/source/source_hamilt/module_xc/exx_info.cpp index a9fdc0e365f..7e804996ce9 100644 --- a/source/source_hamilt/module_xc/exx_info.cpp +++ b/source/source_hamilt/module_xc/exx_info.cpp @@ -235,6 +235,7 @@ bool init_exx_info(Exx_Info &exx_info, const Input_para &inp) throw std::invalid_argument("td_stype=2 EXX requires exx_real_number=0"); } exx_info.info_ri.pca_threshold = inp.exx_pca_threshold; + exx_info.info_ri.cuda_pool = inp.exx_cuda_pool; exx_info.info_ri.C_threshold = inp.exx_c_threshold; exx_info.info_ri.V_threshold = inp.exx_v_threshold; exx_info.info_ri.dm_threshold = inp.exx_dm_threshold; diff --git a/source/source_hamilt/module_xc/exx_info_ri.h b/source/source_hamilt/module_xc/exx_info_ri.h index 8c3af12e060..8a9328b8f8f 100644 --- a/source/source_hamilt/module_xc/exx_info_ri.h +++ b/source/source_hamilt/module_xc/exx_info_ri.h @@ -12,6 +12,7 @@ struct Exx_Info_RI std::map>> coulomb_param; bool real_number = false; + std::string cuda_pool = "default"; bool unfold_hybrid_images = false; bool coul_moment = false; bool rotate_abfs = false; diff --git a/source/source_io/module_parameter/input_parameter.h b/source/source_io/module_parameter/input_parameter.h index 77068674e88..b172108d05d 100644 --- a/source/source_io/module_parameter/input_parameter.h +++ b/source/source_io/module_parameter/input_parameter.h @@ -685,6 +685,7 @@ struct Input_para double exx_mixing_beta = 1.0; ///< mixing_beta for outer-loop when exx_separate_loop=1 std::string exx_real_number = "default"; ///< exx calculated in real or complex std::string device_exx = "cpu"; ///< computing device for LCAO EXX tensor contractions + std::string exx_cuda_pool = "default"; ///< lifetime of CUDA pool retention for EXX double exx_pca_threshold = 0.0001; ///< threshold to screen on-site ABFs in exx double exx_c_threshold = 0.0001; ///< threshold to screen C matrix in exx double exx_v_threshold = 0.1; ///< threshold to screen C matrix in exx diff --git a/source/source_io/module_parameter/read_inp_exx_dftu.cpp b/source/source_io/module_parameter/read_inp_exx_dftu.cpp index a61cc5b47f3..e00bae74a78 100644 --- a/source/source_io/module_parameter/read_inp_exx_dftu.cpp +++ b/source/source_io/module_parameter/read_inp_exx_dftu.cpp @@ -271,6 +271,21 @@ masking is unsupported.)"; }; this->add_item(item); } + { + Input_Item item("exx_cuda_pool"); + item.annotation = "CUDA memory pool retention for EXX contractions"; + item.category = "Exact Exchange (LCAO)"; + item.type = "String"; + item.description = R"(whole_job lets CUDA reuse freed blocks from the first electronic EXX +contraction until the LCAO solver finishes, then restores the threshold and requests a trim. +Requires a pool-capable CUDA device and its default pool with a zero release threshold. +Use default to leave allocation policy to CUDA or an external pool manager.)"; + item.default_value = "default"; + item.set_availability("basis_type==lcao"); + read_sync_string(input.exx_cuda_pool); + item.domain.set_choices({"default", "whole_job"}); + this->add_item(item); + } { Input_Item item("exx_pca_threshold"); item.annotation = "threshold to screen on-site ABFs in exx"; diff --git a/source/source_lcao/module_ri/exx_lri.h b/source/source_lcao/module_ri/exx_lri.h index 211f8c49349..83ec775c2ea 100644 --- a/source/source_lcao/module_ri/exx_lri.h +++ b/source/source_lcao/module_ri/exx_lri.h @@ -8,6 +8,7 @@ #include "lri_cv.h" #include "ewald_vq.h" +#include "exx_lri_detail.h" #include "source_hamilt/module_xc/exx_info.h" #include "source_basis/module_ao/orb_atomic_lm.h" #include "source_base/matrix.h" @@ -152,10 +153,12 @@ class Exx_LRI int abfs_Lmax() const { return abfs_Lmax_; } const Exx_Info_RI& get_info_ri() const { return info; } + void finish_cuda_pool() { cuda_pool_.release(true); } const std::array& hybrid_images_period() const { return hybrid_images_period_; } private: Exx_Info_RI info; + ExxLriDetail::CudaPool cuda_pool_; std::array hybrid_images_period_{{1, 1, 1}}; int abfs_Lmax_ = 0; MPI_Comm mpi_comm; diff --git a/source/source_lcao/module_ri/exx_lri.hpp b/source/source_lcao/module_ri/exx_lri.hpp index 4021a1575ca..2c45e0bf7c6 100644 --- a/source/source_lcao/module_ri/exx_lri.hpp +++ b/source/source_lcao/module_ri/exx_lri.hpp @@ -14,7 +14,6 @@ #include "source_cell/unitcell.h" #include "ri_2d_comm.h" #include "ri_util.h" -#include "exx_lri_detail.h" #include "source_lcao/module_ri/exx_abfs_ctor_orbs.h" #include "source_lcao/module_ri/exx_abfs_io.h" #include "source_lcao/module_ri/conv_coulomb_pot_k.h" @@ -908,6 +907,9 @@ void Exx_LRI::cal_exx_elec(const std::vector, std::set>> judge = RI_2D_Comm::get_2D_judge(ucell, pv.para_2d()); diff --git a/source/source_lcao/module_ri/exx_lri_detail.h b/source/source_lcao/module_ri/exx_lri_detail.h index 5d1da32ffe2..4fc81356b14 100644 --- a/source/source_lcao/module_ri/exx_lri_detail.h +++ b/source/source_lcao/module_ri/exx_lri_detail.h @@ -13,6 +13,12 @@ #include #include #include +#include +#include +#include +#if defined(__CUDA) && defined(__GPU_RI) && defined(__DDLA_RI) +#include +#endif #if defined(__GLIBC__) #include @@ -23,6 +29,74 @@ namespace ExxLriDetail { +class CudaPool +{ + public: +#if defined(__CUDA) && defined(__GPU_RI) && defined(__DDLA_RI) && defined(CUDART_VERSION) && CUDART_VERSION >= 11020 + CudaPool() = default; + CudaPool(const CudaPool&) = delete; + CudaPool& operator=(const CudaPool&) = delete; + ~CudaPool() { release(false); } + void retain() + { + if (pool_) return; + int driver = 0; + if (!available(cudaDriverGetVersion(&driver)) || driver < 11020) return; + int device = 0; + if (!available(cudaGetDevice(&device))) return; + int supported = 0; + if (!available(cudaDeviceGetAttribute(&supported, cudaDevAttrMemoryPoolsSupported, device)) || !supported) return; + cudaMemPool_t active = nullptr; + cudaMemPool_t standard = nullptr; + if (!available(cudaDeviceGetMemPool(&active, device))) return; + if (!available(cudaDeviceGetDefaultMemPool(&standard, device))) return; + if (active != standard) return; + std::uint64_t threshold = 0; + if (!available(cudaMemPoolGetAttribute(active, cudaMemPoolAttrReleaseThreshold, &threshold))) return; + if (threshold != 0) return; + threshold = UINT64_MAX; + if (!available(cudaMemPoolSetAttribute(active, cudaMemPoolAttrReleaseThreshold, &threshold))) return; + pool_ = active; + } + void release(bool trim) + { + if (!pool_) return; + const auto owned = pool_; + pool_ = nullptr; // Only attempt cleanup once, including during exception unwinding. + std::uint64_t value = 0; + const auto query = cudaMemPoolGetAttribute(owned, cudaMemPoolAttrReleaseThreshold, &value); + if (!cleanup_ok(query, "cannot confirm release threshold", trim) || value != UINT64_MAX) return; + value = 0; + const auto restore = cudaMemPoolSetAttribute(owned, cudaMemPoolAttrReleaseThreshold, &value); + if (!cleanup_ok(restore, "cannot confirm threshold restoration", trim)) return; + if (trim) cleanup_ok(cudaMemPoolTrimTo(owned, 0), "optional pool trim failed", false); + } + private: + cudaMemPool_t pool_ = nullptr; + static bool cleanup_ok(cudaError_t status, const char* operation, bool report_work_errors) + { + if (status == cudaSuccess) return true; + if (status == cudaErrorCudartUnloading) return false; + const char* detail = cudaGetErrorString(status); + if (report_work_errors && status != cudaErrorNotSupported && status != cudaErrorInvalidValue + && status != cudaErrorInvalidResourceHandle) + throw std::runtime_error(std::string("EXX CUDA pool: ") + operation + ": " + detail); + std::fprintf(stderr, "EXX CUDA pool: %s: %s\n", operation, detail); + return false; + } + static bool available(cudaError_t error) + { + if (error == cudaErrorNotSupported || error == cudaErrorCudartUnloading) return false; + if (error != cudaSuccess) + throw std::runtime_error(std::string("EXX CUDA memory pool: ") + cudaGetErrorString(error)); + return true; + } +#else + void retain() {} + void release(bool) {} +#endif +}; + using CoulombParam = std::map>>; @@ -41,4 +115,4 @@ extern CoulombParam build_center2_cut_coulomb_param(const CoulombParam& coulomb_ bool* synthesized_rcut = nullptr); } -#endif \ No newline at end of file +#endif From 24af9b29cd338d87793cd4d2f8089f2ab90ee83b Mon Sep 17 00:00:00 2001 From: Entropy-Enthalpy <55441397+Entropy-Enthalpy@users.noreply.github.com> Date: Fri, 4 Dec 2026 15:25:30 +0800 Subject: [PATCH 2/5] lcao: share EXX C derivatives across node-local ranks Let ranks on the same node share screened C-derivative tensors for EXX force and stress calculations. Keep node-local sharing optional and preserve the context while a derived workspace is alive. --- source/source_hamilt/module_xc/exx_info.cpp | 1 + source/source_hamilt/module_xc/exx_info_ri.h | 1 + .../module_parameter/input_parameter.h | 1 + .../module_parameter/read_inp_exx_dftu.cpp | 13 +++++++++++ source/source_lcao/module_ri/exx_lri.h | 9 ++++++++ source/source_lcao/module_ri/exx_lri.hpp | 22 ++++++++++++++++++- .../source_lcao/module_ri/exx_workspace.cpp | 3 +++ 7 files changed, 49 insertions(+), 1 deletion(-) diff --git a/source/source_hamilt/module_xc/exx_info.cpp b/source/source_hamilt/module_xc/exx_info.cpp index 7e804996ce9..5adf9b03bbe 100644 --- a/source/source_hamilt/module_xc/exx_info.cpp +++ b/source/source_hamilt/module_xc/exx_info.cpp @@ -240,6 +240,7 @@ bool init_exx_info(Exx_Info &exx_info, const Input_para &inp) exx_info.info_ri.V_threshold = inp.exx_v_threshold; exx_info.info_ri.dm_threshold = inp.exx_dm_threshold; exx_info.info_ri.C_grad_threshold = inp.exx_c_grad_threshold; + exx_info.info_ri.node_shared_dc = inp.exx_node_shared_dc; exx_info.info_ri.V_grad_threshold = inp.exx_v_grad_threshold; exx_info.info_ri.C_grad_R_threshold = inp.exx_c_grad_r_threshold; exx_info.info_ri.V_grad_R_threshold = inp.exx_v_grad_r_threshold; diff --git a/source/source_hamilt/module_xc/exx_info_ri.h b/source/source_hamilt/module_xc/exx_info_ri.h index 8a9328b8f8f..58c6f2c81cd 100644 --- a/source/source_hamilt/module_xc/exx_info_ri.h +++ b/source/source_hamilt/module_xc/exx_info_ri.h @@ -16,6 +16,7 @@ struct Exx_Info_RI bool unfold_hybrid_images = false; bool coul_moment = false; bool rotate_abfs = false; + bool node_shared_dc = false; double pca_threshold = 0; std::vector files_abfs; diff --git a/source/source_io/module_parameter/input_parameter.h b/source/source_io/module_parameter/input_parameter.h index b172108d05d..a2027b6a924 100644 --- a/source/source_io/module_parameter/input_parameter.h +++ b/source/source_io/module_parameter/input_parameter.h @@ -694,6 +694,7 @@ struct Input_para double exx_v_grad_threshold = 0.1; ///< threshold to screen nabla V matrix in exx double exx_c_grad_r_threshold = 0.0001; ///< threshold to screen nabla C * R matrix in exx double exx_v_grad_r_threshold = 0.1; ///< threshold to screen nabla V * R matrix in exx + bool exx_node_shared_dc = false; ///< share screened C derivatives among local MPI ranks std::string exx_ccp_rmesh_times = "default"; ///< how many times larger the radial mesh required for ///< calculating Columb potential is to that of atomic orbitals int exx_opt_orb_lmax = 0; ///< the maximum l of the spherical Bessel functions for opt ABFs diff --git a/source/source_io/module_parameter/read_inp_exx_dftu.cpp b/source/source_io/module_parameter/read_inp_exx_dftu.cpp index e00bae74a78..9f9b1505901 100644 --- a/source/source_io/module_parameter/read_inp_exx_dftu.cpp +++ b/source/source_io/module_parameter/read_inp_exx_dftu.cpp @@ -385,6 +385,19 @@ Use default to leave allocation policy to CUDA or an external pool manager.)"; read_sync_double(input.exx_v_grad_r_threshold); this->add_item(item); } + { + Input_Item item("exx_node_shared_dc"); + item.annotation = "share EXX C-derivative tensors among MPI ranks on each node"; + item.category = "Exact Exchange (LCAO)"; + item.type = "Boolean"; + item.default_value = "false"; + item.description = "Use LibRI's node-shared tensor transport for C derivatives in EXX force and stress " + "calculations. Requires MPI and LibRI with node-shared tensor support. A layout with " + "one rank per node uses ordinary rank-local storage."; + item.set_availability("basis_type==lcao"); + read_sync_bool(input.exx_node_shared_dc); + this->add_item(item); + } { Input_Item item("exx_ccp_rmesh_times"); item.annotation = "how many times larger the radial mesh required for " diff --git a/source/source_lcao/module_ri/exx_lri.h b/source/source_lcao/module_ri/exx_lri.h index 83ec775c2ea..371dd973d0a 100644 --- a/source/source_lcao/module_ri/exx_lri.h +++ b/source/source_lcao/module_ri/exx_lri.h @@ -13,6 +13,11 @@ #include "source_basis/module_ao/orb_atomic_lm.h" #include "source_base/matrix.h" #include +#if defined(__MPI) && defined(__has_include) +#if __has_include() +#include +#endif +#endif #include #include @@ -162,6 +167,10 @@ class Exx_LRI std::array hybrid_images_period_{{1, 1, 1}}; int abfs_Lmax_ = 0; MPI_Comm mpi_comm; + // Declare before exx_lri so all of its shared tensor views are destroyed first. +#ifdef RI_LRI_NODE_SHARED_H + std::shared_ptr dc_context_; +#endif const K_Vectors *p_kv = nullptr; std::shared_ptr MGT; std::vector orb_cutoff_; diff --git a/source/source_lcao/module_ri/exx_lri.hpp b/source/source_lcao/module_ri/exx_lri.hpp index 2c45e0bf7c6..71744d403a0 100644 --- a/source/source_lcao/module_ri/exx_lri.hpp +++ b/source/source_lcao/module_ri/exx_lri.hpp @@ -409,7 +409,27 @@ void Exx_LRI::cal_exx_ions(const UnitCell& ucell, { std::array>>, Ndim> dCs_order = LRI_CV_Tools::change_order(std::move(dCs)); - this->exx_lri.set_dCs(std::move(dCs_order), this->info.C_grad_threshold); + if (this->info.node_shared_dc) + { +#ifdef RI_LRI_NODE_SHARED_H + if (this->dc_context_) this->exx_lri.free_dCs(); + this->dc_context_ = std::make_shared(this->mpi_comm); + for (std::size_t axis = 0; axis < Ndim; ++axis) + { + const std::string name = "dCs_" + std::to_string(axis) + "_"; + RI::set_tensors_map2_node_shared(this->exx_lri.lri, *this->dc_context_, dCs_order[axis], + {RI::Label::ab::a, RI::Label::ab::b}, this->info.C_grad_threshold, name); + if (!calculate_stress) dCs_order[axis].clear(); + } + this->exx_lri.flag_finish.dCs = true; +#else + throw std::invalid_argument("exx_node_shared_dc requires MPI and LibRI node-shared tensor support"); +#endif + } + else + { + this->exx_lri.set_dCs(std::move(dCs_order), this->info.C_grad_threshold); + } if(calculate_stress) { std::array>>,3>,3> dCRs = LRI_CV_Tools::cal_dMRs(ucell,dCs_order); diff --git a/source/source_lcao/module_ri/exx_workspace.cpp b/source/source_lcao/module_ri/exx_workspace.cpp index 9e3f5db709e..2d061ed59c5 100644 --- a/source/source_lcao/module_ri/exx_workspace.cpp +++ b/source/source_lcao/module_ri/exx_workspace.cpp @@ -82,6 +82,9 @@ std::shared_ptr> Exx_LRI::make_lr_workspace(const UnitCell RI_Util::Vector3_to_array3(ucell.a3)}; const std::array period = {kv.nmp[0], kv.nmp[1], kv.nmp[2]}; workspace->exx_lri.set_parallel(this->mpi_comm, atoms_pos, lattice, period); +#ifdef RI_LRI_NODE_SHARED_H + workspace->dc_context_ = this->dc_context_; +#endif share_exx_geometry(this->exx_lri, workspace->exx_lri); return workspace; } From a01037618059dddc4ea3b6d490700946c7e99c7a Mon Sep 17 00:00:00 2001 From: Jiacheng Xu <13862180016@163.com> Date: Fri, 4 Dec 2026 22:07:53 +0800 Subject: [PATCH 3/5] Defer node-local EXX C derivative sharing until LibRI support lands --- source/source_hamilt/module_xc/exx_info.cpp | 1 - source/source_hamilt/module_xc/exx_info_ri.h | 1 - .../module_parameter/input_parameter.h | 1 - .../module_parameter/read_inp_exx_dftu.cpp | 13 ----------- source/source_lcao/module_ri/exx_lri.h | 9 -------- source/source_lcao/module_ri/exx_lri.hpp | 22 +------------------ .../source_lcao/module_ri/exx_workspace.cpp | 3 --- 7 files changed, 1 insertion(+), 49 deletions(-) diff --git a/source/source_hamilt/module_xc/exx_info.cpp b/source/source_hamilt/module_xc/exx_info.cpp index 5adf9b03bbe..7e804996ce9 100644 --- a/source/source_hamilt/module_xc/exx_info.cpp +++ b/source/source_hamilt/module_xc/exx_info.cpp @@ -240,7 +240,6 @@ bool init_exx_info(Exx_Info &exx_info, const Input_para &inp) exx_info.info_ri.V_threshold = inp.exx_v_threshold; exx_info.info_ri.dm_threshold = inp.exx_dm_threshold; exx_info.info_ri.C_grad_threshold = inp.exx_c_grad_threshold; - exx_info.info_ri.node_shared_dc = inp.exx_node_shared_dc; exx_info.info_ri.V_grad_threshold = inp.exx_v_grad_threshold; exx_info.info_ri.C_grad_R_threshold = inp.exx_c_grad_r_threshold; exx_info.info_ri.V_grad_R_threshold = inp.exx_v_grad_r_threshold; diff --git a/source/source_hamilt/module_xc/exx_info_ri.h b/source/source_hamilt/module_xc/exx_info_ri.h index 58c6f2c81cd..8a9328b8f8f 100644 --- a/source/source_hamilt/module_xc/exx_info_ri.h +++ b/source/source_hamilt/module_xc/exx_info_ri.h @@ -16,7 +16,6 @@ struct Exx_Info_RI bool unfold_hybrid_images = false; bool coul_moment = false; bool rotate_abfs = false; - bool node_shared_dc = false; double pca_threshold = 0; std::vector files_abfs; diff --git a/source/source_io/module_parameter/input_parameter.h b/source/source_io/module_parameter/input_parameter.h index a2027b6a924..b172108d05d 100644 --- a/source/source_io/module_parameter/input_parameter.h +++ b/source/source_io/module_parameter/input_parameter.h @@ -694,7 +694,6 @@ struct Input_para double exx_v_grad_threshold = 0.1; ///< threshold to screen nabla V matrix in exx double exx_c_grad_r_threshold = 0.0001; ///< threshold to screen nabla C * R matrix in exx double exx_v_grad_r_threshold = 0.1; ///< threshold to screen nabla V * R matrix in exx - bool exx_node_shared_dc = false; ///< share screened C derivatives among local MPI ranks std::string exx_ccp_rmesh_times = "default"; ///< how many times larger the radial mesh required for ///< calculating Columb potential is to that of atomic orbitals int exx_opt_orb_lmax = 0; ///< the maximum l of the spherical Bessel functions for opt ABFs diff --git a/source/source_io/module_parameter/read_inp_exx_dftu.cpp b/source/source_io/module_parameter/read_inp_exx_dftu.cpp index 9f9b1505901..e00bae74a78 100644 --- a/source/source_io/module_parameter/read_inp_exx_dftu.cpp +++ b/source/source_io/module_parameter/read_inp_exx_dftu.cpp @@ -385,19 +385,6 @@ Use default to leave allocation policy to CUDA or an external pool manager.)"; read_sync_double(input.exx_v_grad_r_threshold); this->add_item(item); } - { - Input_Item item("exx_node_shared_dc"); - item.annotation = "share EXX C-derivative tensors among MPI ranks on each node"; - item.category = "Exact Exchange (LCAO)"; - item.type = "Boolean"; - item.default_value = "false"; - item.description = "Use LibRI's node-shared tensor transport for C derivatives in EXX force and stress " - "calculations. Requires MPI and LibRI with node-shared tensor support. A layout with " - "one rank per node uses ordinary rank-local storage."; - item.set_availability("basis_type==lcao"); - read_sync_bool(input.exx_node_shared_dc); - this->add_item(item); - } { Input_Item item("exx_ccp_rmesh_times"); item.annotation = "how many times larger the radial mesh required for " diff --git a/source/source_lcao/module_ri/exx_lri.h b/source/source_lcao/module_ri/exx_lri.h index 371dd973d0a..83ec775c2ea 100644 --- a/source/source_lcao/module_ri/exx_lri.h +++ b/source/source_lcao/module_ri/exx_lri.h @@ -13,11 +13,6 @@ #include "source_basis/module_ao/orb_atomic_lm.h" #include "source_base/matrix.h" #include -#if defined(__MPI) && defined(__has_include) -#if __has_include() -#include -#endif -#endif #include #include @@ -167,10 +162,6 @@ class Exx_LRI std::array hybrid_images_period_{{1, 1, 1}}; int abfs_Lmax_ = 0; MPI_Comm mpi_comm; - // Declare before exx_lri so all of its shared tensor views are destroyed first. -#ifdef RI_LRI_NODE_SHARED_H - std::shared_ptr dc_context_; -#endif const K_Vectors *p_kv = nullptr; std::shared_ptr MGT; std::vector orb_cutoff_; diff --git a/source/source_lcao/module_ri/exx_lri.hpp b/source/source_lcao/module_ri/exx_lri.hpp index 71744d403a0..2c45e0bf7c6 100644 --- a/source/source_lcao/module_ri/exx_lri.hpp +++ b/source/source_lcao/module_ri/exx_lri.hpp @@ -409,27 +409,7 @@ void Exx_LRI::cal_exx_ions(const UnitCell& ucell, { std::array>>, Ndim> dCs_order = LRI_CV_Tools::change_order(std::move(dCs)); - if (this->info.node_shared_dc) - { -#ifdef RI_LRI_NODE_SHARED_H - if (this->dc_context_) this->exx_lri.free_dCs(); - this->dc_context_ = std::make_shared(this->mpi_comm); - for (std::size_t axis = 0; axis < Ndim; ++axis) - { - const std::string name = "dCs_" + std::to_string(axis) + "_"; - RI::set_tensors_map2_node_shared(this->exx_lri.lri, *this->dc_context_, dCs_order[axis], - {RI::Label::ab::a, RI::Label::ab::b}, this->info.C_grad_threshold, name); - if (!calculate_stress) dCs_order[axis].clear(); - } - this->exx_lri.flag_finish.dCs = true; -#else - throw std::invalid_argument("exx_node_shared_dc requires MPI and LibRI node-shared tensor support"); -#endif - } - else - { - this->exx_lri.set_dCs(std::move(dCs_order), this->info.C_grad_threshold); - } + this->exx_lri.set_dCs(std::move(dCs_order), this->info.C_grad_threshold); if(calculate_stress) { std::array>>,3>,3> dCRs = LRI_CV_Tools::cal_dMRs(ucell,dCs_order); diff --git a/source/source_lcao/module_ri/exx_workspace.cpp b/source/source_lcao/module_ri/exx_workspace.cpp index 2d061ed59c5..9e3f5db709e 100644 --- a/source/source_lcao/module_ri/exx_workspace.cpp +++ b/source/source_lcao/module_ri/exx_workspace.cpp @@ -82,9 +82,6 @@ std::shared_ptr> Exx_LRI::make_lr_workspace(const UnitCell RI_Util::Vector3_to_array3(ucell.a3)}; const std::array period = {kv.nmp[0], kv.nmp[1], kv.nmp[2]}; workspace->exx_lri.set_parallel(this->mpi_comm, atoms_pos, lattice, period); -#ifdef RI_LRI_NODE_SHARED_H - workspace->dc_context_ = this->dc_context_; -#endif share_exx_geometry(this->exx_lri, workspace->exx_lri); return workspace; } From cbb385695433d898da1bbb3e1cfb5aa9777b1f1c Mon Sep 17 00:00:00 2001 From: Jiacheng Xu <13862180016@163.com> Date: Fri, 4 Dec 2026 22:12:09 +0800 Subject: [PATCH 4/5] Enable CUDA memory pool retention automatically for GPU EXX --- source/source_hamilt/module_xc/exx_info.cpp | 1 - source/source_hamilt/module_xc/exx_info_ri.h | 1 - .../source_io/module_parameter/input_parameter.h | 1 - .../module_parameter/read_inp_exx_dftu.cpp | 15 --------------- source/source_lcao/module_ri/exx_lri.hpp | 5 ++++- 5 files changed, 4 insertions(+), 19 deletions(-) diff --git a/source/source_hamilt/module_xc/exx_info.cpp b/source/source_hamilt/module_xc/exx_info.cpp index 7e804996ce9..a9fdc0e365f 100644 --- a/source/source_hamilt/module_xc/exx_info.cpp +++ b/source/source_hamilt/module_xc/exx_info.cpp @@ -235,7 +235,6 @@ bool init_exx_info(Exx_Info &exx_info, const Input_para &inp) throw std::invalid_argument("td_stype=2 EXX requires exx_real_number=0"); } exx_info.info_ri.pca_threshold = inp.exx_pca_threshold; - exx_info.info_ri.cuda_pool = inp.exx_cuda_pool; exx_info.info_ri.C_threshold = inp.exx_c_threshold; exx_info.info_ri.V_threshold = inp.exx_v_threshold; exx_info.info_ri.dm_threshold = inp.exx_dm_threshold; diff --git a/source/source_hamilt/module_xc/exx_info_ri.h b/source/source_hamilt/module_xc/exx_info_ri.h index 8a9328b8f8f..8c3af12e060 100644 --- a/source/source_hamilt/module_xc/exx_info_ri.h +++ b/source/source_hamilt/module_xc/exx_info_ri.h @@ -12,7 +12,6 @@ struct Exx_Info_RI std::map>> coulomb_param; bool real_number = false; - std::string cuda_pool = "default"; bool unfold_hybrid_images = false; bool coul_moment = false; bool rotate_abfs = false; diff --git a/source/source_io/module_parameter/input_parameter.h b/source/source_io/module_parameter/input_parameter.h index b172108d05d..77068674e88 100644 --- a/source/source_io/module_parameter/input_parameter.h +++ b/source/source_io/module_parameter/input_parameter.h @@ -685,7 +685,6 @@ struct Input_para double exx_mixing_beta = 1.0; ///< mixing_beta for outer-loop when exx_separate_loop=1 std::string exx_real_number = "default"; ///< exx calculated in real or complex std::string device_exx = "cpu"; ///< computing device for LCAO EXX tensor contractions - std::string exx_cuda_pool = "default"; ///< lifetime of CUDA pool retention for EXX double exx_pca_threshold = 0.0001; ///< threshold to screen on-site ABFs in exx double exx_c_threshold = 0.0001; ///< threshold to screen C matrix in exx double exx_v_threshold = 0.1; ///< threshold to screen C matrix in exx diff --git a/source/source_io/module_parameter/read_inp_exx_dftu.cpp b/source/source_io/module_parameter/read_inp_exx_dftu.cpp index e00bae74a78..a61cc5b47f3 100644 --- a/source/source_io/module_parameter/read_inp_exx_dftu.cpp +++ b/source/source_io/module_parameter/read_inp_exx_dftu.cpp @@ -271,21 +271,6 @@ masking is unsupported.)"; }; this->add_item(item); } - { - Input_Item item("exx_cuda_pool"); - item.annotation = "CUDA memory pool retention for EXX contractions"; - item.category = "Exact Exchange (LCAO)"; - item.type = "String"; - item.description = R"(whole_job lets CUDA reuse freed blocks from the first electronic EXX -contraction until the LCAO solver finishes, then restores the threshold and requests a trim. -Requires a pool-capable CUDA device and its default pool with a zero release threshold. -Use default to leave allocation policy to CUDA or an external pool manager.)"; - item.default_value = "default"; - item.set_availability("basis_type==lcao"); - read_sync_string(input.exx_cuda_pool); - item.domain.set_choices({"default", "whole_job"}); - this->add_item(item); - } { Input_Item item("exx_pca_threshold"); item.annotation = "threshold to screen on-site ABFs in exx"; diff --git a/source/source_lcao/module_ri/exx_lri.hpp b/source/source_lcao/module_ri/exx_lri.hpp index 2c45e0bf7c6..3f4a8108966 100644 --- a/source/source_lcao/module_ri/exx_lri.hpp +++ b/source/source_lcao/module_ri/exx_lri.hpp @@ -908,7 +908,10 @@ void Exx_LRI::cal_exx_elec(const std::vector, std::set>> judge From 3ebd50ff605e523d97676cdab951cc385377c761 Mon Sep 17 00:00:00 2001 From: Jiacheng Xu <13862180016@163.com> Date: Fri, 4 Dec 2026 22:39:06 +0800 Subject: [PATCH 5/5] Use explicit CUDA types during EXX pool cleanup --- source/source_lcao/module_ri/exx_lri_detail.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/source/source_lcao/module_ri/exx_lri_detail.h b/source/source_lcao/module_ri/exx_lri_detail.h index 4fc81356b14..abf8b79d1f3 100644 --- a/source/source_lcao/module_ri/exx_lri_detail.h +++ b/source/source_lcao/module_ri/exx_lri_detail.h @@ -61,13 +61,13 @@ class CudaPool void release(bool trim) { if (!pool_) return; - const auto owned = pool_; + const cudaMemPool_t owned = pool_; pool_ = nullptr; // Only attempt cleanup once, including during exception unwinding. std::uint64_t value = 0; - const auto query = cudaMemPoolGetAttribute(owned, cudaMemPoolAttrReleaseThreshold, &value); + const cudaError_t query = cudaMemPoolGetAttribute(owned, cudaMemPoolAttrReleaseThreshold, &value); if (!cleanup_ok(query, "cannot confirm release threshold", trim) || value != UINT64_MAX) return; value = 0; - const auto restore = cudaMemPoolSetAttribute(owned, cudaMemPoolAttrReleaseThreshold, &value); + const cudaError_t restore = cudaMemPoolSetAttribute(owned, cudaMemPoolAttrReleaseThreshold, &value); if (!cleanup_ok(restore, "cannot confirm threshold restoration", trim)) return; if (trim) cleanup_ok(cudaMemPoolTrimTo(owned, 0), "optional pool trim failed", false); }