Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: address memory leak with single precision #4492

Merged
merged 2 commits into from
Jun 27, 2024
Merged
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
30 changes: 13 additions & 17 deletions source/module_esolver/esolver_ks_pw.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -874,19 +874,17 @@ template <typename T, typename Device>
void ESolver_KS_PW<T, Device>::cal_force(ModuleBase::matrix& force)
{
Forces<double, Device> ff(GlobalC::ucell.nat);
if (this->__kspw_psi != nullptr)
if (this->__kspw_psi != nullptr && GlobalV::precision_flag == "single")
{
this->__kspw_psi = nullptr;
delete reinterpret_cast<psi::Psi<std::complex<double>, Device>*>(this->__kspw_psi);
}

if (this->__kspw_psi == nullptr)
{
this->__kspw_psi = GlobalV::precision_flag == "single"
? new psi::Psi<std::complex<double>, Device>(this->kspw_psi[0])
: reinterpret_cast<psi::Psi<std::complex<double>, Device>*>(this->kspw_psi);
}
// Refresh __kspw_psi
this->__kspw_psi = GlobalV::precision_flag == "single"
? new psi::Psi<std::complex<double>, Device>(this->kspw_psi[0])
: reinterpret_cast<psi::Psi<std::complex<double>, Device>*>(this->kspw_psi);

//! Calculate forces
// Calculate forces
ff.cal_force(force,
*this->pelec,
this->pw_rhod,
Expand All @@ -901,17 +899,15 @@ template <typename T, typename Device>
void ESolver_KS_PW<T, Device>::cal_stress(ModuleBase::matrix& stress)
{
Stress_PW<double, Device> ss(this->pelec);
if (this->__kspw_psi != nullptr)
if (this->__kspw_psi != nullptr && GlobalV::precision_flag == "single")
{
this->__kspw_psi = nullptr;
delete reinterpret_cast<psi::Psi<std::complex<double>, Device>*>(this->__kspw_psi);
}

if (this->__kspw_psi == nullptr)
{
this->__kspw_psi = GlobalV::precision_flag == "single"
? new psi::Psi<std::complex<double>, Device>(this->kspw_psi[0])
: reinterpret_cast<psi::Psi<std::complex<double>, Device>*>(this->kspw_psi);
}
// Refresh __kspw_psi
this->__kspw_psi = GlobalV::precision_flag == "single"
? new psi::Psi<std::complex<double>, Device>(this->kspw_psi[0])
: reinterpret_cast<psi::Psi<std::complex<double>, Device>*>(this->kspw_psi);
ss.cal_stress(stress,
GlobalC::ucell,
this->pw_rhod,
Expand Down
Loading