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_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..3f4a8108966 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,12 @@ 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..abf8b79d1f3 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 cudaMemPool_t owned = pool_; + pool_ = nullptr; // Only attempt cleanup once, including during exception unwinding. + std::uint64_t value = 0; + const cudaError_t query = cudaMemPoolGetAttribute(owned, cudaMemPoolAttrReleaseThreshold, &value); + if (!cleanup_ok(query, "cannot confirm release threshold", trim) || value != UINT64_MAX) return; + value = 0; + 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); + } + 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