Skip to content
Merged
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
259 changes: 155 additions & 104 deletions source/source_hsolver/diago_dav_subspace.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,12 @@

#include "source_hsolver/kernels/hegvd_op.h"
#include "source_hsolver/diag_hs_para.h"
#include "source_hsolver/kernels/bpcg_kernel_op.h" // normalize_op, precondition_op, apply_eigenvalues_op
#include "source_hsolver/kernels/bpcg_kernel_op.h" // precondition_op

#include <cmath>
#include <iomanip>
#include <limits>
#include <sstream>
#include <vector>

#ifdef __MPI
Expand Down Expand Up @@ -122,7 +126,8 @@ int Diago_DavSubspace<T, Device>::diag_once(const HSOperator<T, Device>& op,
T* psi_in,
const int psi_in_dmax,
Real* eigenvalue_in_hsolver,
const std::vector<double>& ethr_band)
const std::vector<double>& ethr_band,
const int ik)
{
ModuleBase::timer::start("Diago_DavSubspace", "diag_once");
const int matrix_ld = std::max(this->dim, 1);
Expand Down Expand Up @@ -186,38 +191,43 @@ int Diago_DavSubspace<T, Device>::diag_once(const HSOperator<T, Device>& op,
this->spsi,
this->vcc,
unconv.data(),
&eigenvalue_iter);
&eigenvalue_iter,
ethr_band,
ik);

this->cal_elem(this->dim,
nbase,
this->notconv,
this->psi_in_iter,
this->spsi,
this->hpsi,
this->hcc,
this->scc);

this->diag_zhegvx(nbase, this->n_band, this->hcc, this->scc, this->nbase_x, &eigenvalue_iter, this->vcc);

// check convergence and update eigenvalues
ModuleBase::timer::start("Diago_DavSubspace", "check_update");

this->notconv = 0;
for (int m = 0; m < this->n_band; m++)
if (this->notconv > 0)
{
convflag[m] = (std::abs(eigenvalue_iter[m] - eigenvalue_in_hsolver[m]) < ethr_band[m]);

if (!convflag[m])
this->cal_elem(this->dim,
nbase,
this->notconv,
this->psi_in_iter,
this->spsi,
this->hpsi,
this->hcc,
this->scc);

this->diag_zhegvx(nbase, this->n_band, this->hcc, this->scc, this->nbase_x, &eigenvalue_iter, this->vcc);

// check convergence and update eigenvalues
ModuleBase::timer::start("Diago_DavSubspace", "check_update");

this->notconv = 0;
for (int m = 0; m < this->n_band; m++)
{
unconv[this->notconv] = m;
this->notconv++;
convflag[m] = (std::abs(eigenvalue_iter[m] - eigenvalue_in_hsolver[m]) < ethr_band[m]);

if (!convflag[m])
{
unconv[this->notconv] = m;
this->notconv++;
}

eigenvalue_in_hsolver[m] = eigenvalue_iter[m];
}

eigenvalue_in_hsolver[m] = eigenvalue_iter[m];
ModuleBase::timer::end("Diago_DavSubspace", "check_update");
}

ModuleBase::timer::end("Diago_DavSubspace", "check_update");

if ((this->notconv == 0) || (nbase + this->notconv + 1 > this->nbase_x) || (dav_iter == this->iter_nmax))
{
ModuleBase::timer::start("Diago_DavSubspace", "last");
Expand Down Expand Up @@ -287,24 +297,28 @@ template <typename T, typename Device>
void Diago_DavSubspace<T, Device>::cal_grad(const HSOperator<T, Device>& op,
const int& dim,
const int& nbase,
const int& notconv,
int& notconv,
T* psi_iter,
T* hpsi,
T* spsi,
T* vcc,
const int* unconv,
std::vector<Real>* eigenvalue_iter)
int* unconv,
const std::vector<Real>* eigenvalue_iter,
const std::vector<double>& ethr_band,
const int ik)
{
ModuleBase::timer::start("Diago_DavSubspace", "cal_grad");
const int matrix_ld = std::max(this->dim, 1);

for (size_t i = 0; i < notconv; i++)
// Keep the full Ritz solution intact for output and restart, including when
// every residual vanishes and there is no subsequent reduced solve.
T* active_vcc = nullptr;
resmem_complex_op()(active_vcc, this->nbase_x * notconv);
std::vector<Real> active_eigenvalues(this->nbase_x, 0);
for (int i = 0; i < notconv; ++i)
{
if (unconv[i] != i)
{
syncmem_complex_op()(vcc + i * this->nbase_x, vcc + unconv[i] * this->nbase_x, nbase);
(*eigenvalue_iter)[i] = (*eigenvalue_iter)[unconv[i]];
}
syncmem_complex_op()(active_vcc + i * this->nbase_x, vcc + unconv[i] * this->nbase_x, nbase);
active_eigenvalues[i] = (*eigenvalue_iter)[unconv[i]];
}

if (notconv > 1){
Expand All @@ -322,7 +336,7 @@ void Diago_DavSubspace<T, Device>::cal_grad(const HSOperator<T, Device>& op,
this->one,
hpsi,
matrix_ld,
vcc,
active_vcc,
this->nbase_x,
this->zero,
psi_iter + (nbase) * this->dim,
Expand All @@ -341,7 +355,7 @@ void Diago_DavSubspace<T, Device>::cal_grad(const HSOperator<T, Device>& op,
this->one, // alpha
hpsi, // A dim * nbase
matrix_ld, // LDA: if(N) max(1,m)
vcc, // X nbase
active_vcc, // X nbase
1, // incx
this->zero, // beta
psi_iter + (nbase) * this->dim, // Y dim
Expand All @@ -351,15 +365,16 @@ void Diago_DavSubspace<T, Device>::cal_grad(const HSOperator<T, Device>& op,


// Eigenvalues operation section
Real* e_temp_hd = eigenvalue_iter->data();
Real* e_temp_hd = active_eigenvalues.data();
if (this->device == base_device::GpuDevice)
{
syncmem_var_h2d_op()(this->d_eigenvalue, eigenvalue_iter->data(), this->nbase_x);
syncmem_var_h2d_op()(this->d_eigenvalue, active_eigenvalues.data(), this->nbase_x);
e_temp_hd = this->d_eigenvalue;
}

// vcc = - vcc * eigenvalue
ModuleBase::matrix_mul_vector_op<T, Device>()(nbase, notconv, vcc, this->nbase_x, e_temp_hd, -1.0, vcc, this->nbase_x);
// active_vcc = - active_vcc * eigenvalue
ModuleBase::matrix_mul_vector_op<T, Device>()(
nbase, notconv, active_vcc, this->nbase_x, e_temp_hd, -1.0, active_vcc, this->nbase_x);

if (notconv > 1){

Expand All @@ -376,7 +391,7 @@ void Diago_DavSubspace<T, Device>::cal_grad(const HSOperator<T, Device>& op,
this->one,
spsi,
matrix_ld,
vcc,
active_vcc,
this->nbase_x,
this->one,
psi_iter + nbase * this->dim,
Expand All @@ -394,18 +409,91 @@ void Diago_DavSubspace<T, Device>::cal_grad(const HSOperator<T, Device>& op,
this->one, // alpha
spsi, // A dim * nbase
matrix_ld, // LDA: if(N) max(1,m)
vcc, // X nbase
active_vcc, // X nbase
1, // incx
this->one, // beta
psi_iter + nbase * this->dim, // Y dim
1 // incy
);
}

delmem_complex_op()(active_vcc);

// Deflation is only a safeguard for numerically tiny residuals. A loose
// energy-change tolerance (e.g. for unoccupied bands) must not skip an
// ordinary Davidson expansion. Account for the arithmetic precision while
// never increasing the requested band tolerance.
const auto residual_threshold = [&](const int band)
{
const Real energy_scale = std::max(Real(1), std::abs((*eigenvalue_iter)[band]));
const double tiny_norm = std::max(1.0e-12, double(std::numeric_limits<Real>::epsilon() * energy_scale));
return std::min(ethr_band[band], tiny_norm);
};

const auto breakdown = [&](const int band, const Real residual, const Real direction,
const char* reason)
{
std::ostringstream message;
message << std::setprecision(std::numeric_limits<Real>::max_digits10)
<< "Diago_DavSubspace::cal_grad: " << reason
<< "; band=" << band + 1 << ", k-point=";
if (ik >= 0)
{
message << ik + 1 << " (1-based, pool-local k index)";
}
else
{
message << "n/a (operator without single-k context)";
}
message << ", eigenvalue=" << (*eigenvalue_iter)[band]
<< ", residual_norm=" << residual
<< ", residual_threshold=" << residual_threshold(band)
<< ", band_threshold=" << ethr_band[band]
<< ", direction_norm=" << direction
<< ", direction_min=" << std::numeric_limits<Real>::min()
<< ". The correction cannot be normalized safely.";
ModuleBase::WARNING_QUIT("Diago_DavSubspace::cal_grad", message.str());
};

// The Ritz vectors are S-normalized. Check the unpreconditioned residual
Comment thread
mohanchen marked this conversation as resolved.
Resolved
// before deciding that a correction is unnecessary.
std::vector<Real> residual_norm(notconv, 0);
int active = 0;
for (int i = 0; i < notconv; ++i)
{
T* residual = psi_iter + (nbase + i) * dim;
const Real norm = std::sqrt(ModuleBase::dot_real_op<T, Device>()(
dim, residual, residual, this->diag_comm.nproc > 1));
Comment thread
mohanchen marked this conversation as resolved.
Resolved
const int band = unconv[i];
if (!std::isfinite(norm))
{
breakdown(band, norm, std::numeric_limits<Real>::quiet_NaN(), "nonfinite eigen-residual");
}
if (norm <= residual_threshold(band))
{
continue;
}
if (active != i)
{
syncmem_complex_op()(psi_iter + (nbase + active) * dim, residual, dim);
}
residual_norm[active] = norm;
active_eigenvalues[active] = (*eigenvalue_iter)[band];
unconv[active] = band;
++active;
}
notconv = active;
if (notconv == 0)
{
ModuleBase::timer::end("Diago_DavSubspace", "cal_grad");
return;
}

// Precondition section
#if defined(__CUDA) || defined(__ROCM)
if (this->device == base_device::GpuDevice)
{
syncmem_var_h2d_op()(this->d_eigenvalue, active_eigenvalues.data(), notconv);
precondition_op<T, Device>()(this->dim,
psi_iter,
nbase,
Expand All @@ -421,70 +509,21 @@ void Diago_DavSubspace<T, Device>::cal_grad(const HSOperator<T, Device>& op,
nbase,
notconv,
this->precondition.data(),
(*eigenvalue_iter).data());
active_eigenvalues.data());
}

// Normalize section
#if defined(__CUDA) || defined(__ROCM)
if (this->device == base_device::GpuDevice)
// Measure and validate before division. The same typed operations serve CPU
// and GPU, and each norm includes all basis partitions in the pool.
for (int i = 0; i < notconv; ++i)
{
Real* psi_norm = nullptr;
resmem_real_op()(psi_norm, notconv);
setmem_real_op()(psi_norm, 0.0, notconv);

normalize_op<T, Device>()(this->dim,
psi_iter,
nbase,
notconv,
psi_norm);

// Check for zero norms (GPU path: copy norms from device to host)
Real* psi_norm_host = nullptr;
resmem_real_h_op()(psi_norm_host, notconv);
syncmem_var_d2h_op()(psi_norm_host, psi_norm, notconv);
for (int i = 0; i < notconv; i++)
T* direction = psi_iter + (nbase + i) * dim;
const Real norm = std::sqrt(ModuleBase::dot_real_op<T, Device>()(
dim, direction, direction, this->diag_comm.nproc > 1));
if (!std::isfinite(norm) || norm < std::numeric_limits<Real>::min())
{
if (psi_norm_host[i] <= 1.0e-12)
{
std::cout << "Diago_DavSubspace::cal_grad: psi_norm <= 0 for band " << i << std::endl;
std::cout << "This may be due to npwx < nbands: the number of plane waves is less than" << std::endl;
std::cout << "the number of bands, leading to a rank-deficient problem." << std::endl;
std::cout << "Please increase ecutwfc or reduce nbands." << std::endl;
delmem_real_h_op()(psi_norm_host);
delmem_real_op()(psi_norm);
ModuleBase::WARNING_QUIT("cal_grad", "psi_norm <= 0");
}
breakdown(unconv[i], residual_norm[i], norm, "unusable preconditioned direction");
}
delmem_real_h_op()(psi_norm_host);
delmem_real_op()(psi_norm);
}
else
#endif
{
Real* psi_norm = nullptr;
resmem_real_h_op()(psi_norm, notconv);
setmem_real_h_op()(psi_norm, 0.0, notconv);

normalize_op<T, Device>()(this->dim,
psi_iter,
nbase,
notconv,
psi_norm);

// Check for zero norms (CPU path)
for (int i = 0; i < notconv; i++)
{
if (psi_norm[i] <= 1.0e-12)
{
std::cout << "Diago_DavSubspace::cal_grad: psi_norm <= 0 for band " << i << std::endl;
std::cout << "This may be due to npwx < nbands: the number of plane waves is less than" << std::endl;
std::cout << "the number of bands, leading to a rank-deficient problem." << std::endl;
std::cout << "Please increase ecutwfc or reduce nbands." << std::endl;
delmem_real_h_op()(psi_norm);
ModuleBase::WARNING_QUIT("cal_grad", "psi_norm <= 0");
}
}
delmem_real_h_op()(psi_norm);
ModuleBase::vector_div_constant_op<T, Device>()(dim, direction, direction, norm);
}

// update hpsi[:, nbase:nbase+notconv]
Expand Down Expand Up @@ -844,6 +883,18 @@ int Diago_DavSubspace<T, Device>::diag(const HSOperator<T, Device>& op,
Real* eigenvalue_in_hsolver,
const std::vector<double>& ethr_band,
const bool& scf_type)
{
return this->diag(op, psi_in, psi_in_dmax, eigenvalue_in_hsolver, ethr_band, scf_type, -1);
}

template <typename T, typename Device>
int Diago_DavSubspace<T, Device>::diag(const HSOperator<T, Device>& op,
T* psi_in,
const int psi_in_dmax,
Real* eigenvalue_in_hsolver,
const std::vector<double>& ethr_band,
const bool& scf_type,
const int ik)
{
/// record the times of trying iterative diagonalization
this->notconv = 0;
Expand All @@ -854,7 +905,7 @@ int Diago_DavSubspace<T, Device>::diag(const HSOperator<T, Device>& op,
do
{

sum_iter += this->diag_once(op, psi_in, psi_in_dmax, eigenvalue_in_hsolver, ethr_band);
sum_iter += this->diag_once(op, psi_in, psi_in_dmax, eigenvalue_in_hsolver, ethr_band, ik);

++ntry;

Expand Down
Loading
Loading