Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions source/source_esolver/esolver_ks_lcao.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,10 @@ void ESolver_KS_LCAO<TK, TR>::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");
}

Expand Down
3 changes: 3 additions & 0 deletions source/source_lcao/module_ri/exx_lri.h
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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<int, 3>& hybrid_images_period() const { return hybrid_images_period_; }

private:
Exx_Info_RI info;
ExxLriDetail::CudaPool cuda_pool_;
std::array<int, 3> hybrid_images_period_{{1, 1, 1}};
int abfs_Lmax_ = 0;
MPI_Comm mpi_comm;
Expand Down
7 changes: 6 additions & 1 deletion source/source_lcao/module_ri/exx_lri.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -908,6 +907,12 @@ void Exx_LRI<Tdata>::cal_exx_elec(const std::vector<std::map<TA, std::map<TAC, R
{
ModuleBase::TITLE("Exx_LRI","cal_exx_elec");
ModuleBase::timer::start("Exx_LRI", "cal_exx_elec");
#ifdef __GPU_RI
if (exx_lri.lri.cal_mode == RI::LRI_Cal_Mode::GPU)
{
cuda_pool_.retain();
}
#endif

const std::vector<std::tuple<std::set<TA>, std::set<TA>>> judge
= RI_2D_Comm::get_2D_judge(ucell, pv.para_2d());
Expand Down
76 changes: 75 additions & 1 deletion source/source_lcao/module_ri/exx_lri_detail.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@
#include <vector>
#include <map>
#include <string>
#include <cstdint>
#include <cstdio>
#include <stdexcept>
#if defined(__CUDA) && defined(__GPU_RI) && defined(__DDLA_RI)
#include <cuda_runtime_api.h>
#endif

#if defined(__GLIBC__)
#include <malloc.h>
Expand All @@ -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<Conv_Coulomb_Pot_K::Coulomb_Type, std::vector<std::map<std::string, std::string>>>;

Expand All @@ -41,4 +115,4 @@ extern CoulombParam build_center2_cut_coulomb_param(const CoulombParam& coulomb_
bool* synthesized_rcut = nullptr);
}

#endif
#endif
Loading