Skip to content

Commit 1995a0a

Browse files
committed
Reduce PPCG temporary allocations
1 parent 4314ebb commit 1995a0a

2 files changed

Lines changed: 32 additions & 11 deletions

File tree

source/source_hsolver/ppcg/diago_ppcg_diag.hpp

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,11 @@ double DiagoPPCG<T, Device>::diag(const HPsiFunc& hpsi_func,
8080
apply_s_current(psi_in, spsi_.data(), ncol);
8181
record_residual(0, "initial_rr");
8282

83+
std::vector<T> w_active;
84+
std::vector<T> hw_active;
85+
std::vector<int> cols;
86+
SmallSubspace subspace;
87+
8388
while (!active_cols.empty() && iter <= maxiter_)
8489
{
8590
const int nact = static_cast<int>(active_cols.size());
@@ -91,10 +96,9 @@ double DiagoPPCG<T, Device>::diag(const HPsiFunc& hpsi_func,
9196
project_against(psi_in, spsi_.data(), all_cols, w_, sw_, active_cols);
9297

9398
// Apply H to the search direction.
94-
std::vector<T> w_active;
9599
copy_cols(w_.data(), active_cols, w_active);
96100
force_g0_real(w_active.data(), nact);
97-
std::vector<T> hw_active(ld_psi_ * nact, T(0));
101+
hw_active.assign(ld_psi_ * nact, T(0));
98102
scatter_cols(w_.data(), active_cols, w_active);
99103
apply_h(hpsi_func, w_active.data(), hw_active.data(), nact);
100104
scatter_cols(hw_.data(), active_cols, hw_active);
@@ -117,10 +121,9 @@ double DiagoPPCG<T, Device>::diag(const HPsiFunc& hpsi_func,
117121
{
118122
const int i0 = isb * sbsize_;
119123
const int l = std::min(sbsize_, nact - i0);
120-
std::vector<int> cols(active_cols.begin() + i0,
121-
active_cols.begin() + i0 + l);
124+
cols.assign(active_cols.begin() + i0,
125+
active_cols.begin() + i0 + l);
122126

123-
SmallSubspace subspace;
124127
build_small_subspace(psi_in, cols, use_p_now, subspace);
125128
solve_small_generalized((use_p_now ? 3 : 2) * l, subspace);
126129
update_one_block(psi_in, cols, l, use_p_now, subspace);
@@ -166,11 +169,13 @@ double DiagoPPCG<T, Device>::diag(const HPsiFunc& hpsi_func,
166169
update_polak_ribiere(grad, p, grad_old_, z_old_, beta_denom_, prec);
167170

168171
// CG iteration loop.
172+
std::vector<T> hp;
173+
std::vector<T> sp;
169174
while (iter <= maxiter_)
170175
{
171176
// Apply H and S to search direction.
172-
std::vector<T> hp(ld_psi_ * ncol, T(0));
173-
std::vector<T> sp(ld_psi_ * ncol, T(0));
177+
hp.assign(ld_psi_ * ncol, T(0));
178+
sp.assign(ld_psi_ * ncol, T(0));
174179
apply_h(hpsi_func, p.data(), hp.data(), ncol);
175180
apply_s_current(p.data(), sp.data(), ncol);
176181

source/source_hsolver/ppcg/diago_ppcg_ops.hpp

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -146,8 +146,8 @@ void DiagoPPCG<T, Device>::copy_cols(const T* src,
146146
const std::vector<int>& cols,
147147
std::vector<T>& dst) const
148148
{
149-
dst.assign(ld_psi_ * cols.size(), T(0));
150149
const int ncols = static_cast<int>(cols.size());
150+
dst.resize(ld_psi_ * ncols);
151151
#ifdef _OPENMP
152152
#pragma omp parallel for schedule(static) if (ld_psi_ * ncols > 4096)
153153
#endif
@@ -194,15 +194,31 @@ void DiagoPPCG<T, Device>::project_against(
194194
if (basis_cols.empty() || x_cols.empty())
195195
return;
196196

197-
std::vector<T> basis_l;
198197
std::vector<T> sx_l;
199-
copy_cols(basis, basis_cols, basis_l);
200198
copy_cols(sx.data(), x_cols, sx_l);
201199

202200
const int nbasis = static_cast<int>(basis_cols.size());
203201
const int nx = static_cast<int>(x_cols.size());
202+
bool contiguous_basis = true;
203+
for (int i = 0; i < nbasis; ++i)
204+
{
205+
if (basis_cols[i] != i)
206+
{
207+
contiguous_basis = false;
208+
break;
209+
}
210+
}
211+
212+
std::vector<T> basis_l;
213+
const T* basis_data = basis;
214+
if (!contiguous_basis)
215+
{
216+
copy_cols(basis, basis_cols, basis_l);
217+
basis_data = basis_l.data();
218+
}
219+
204220
std::vector<T> coeff(nbasis * nx, T(0));
205-
gram(basis_l.data(), sx_l.data(), nbasis, nx, coeff, nbasis);
221+
gram(basis_data, sx_l.data(), nbasis, nx, coeff, nbasis);
206222

207223
#ifdef _OPENMP
208224
#pragma omp parallel for schedule(static) if (n_dim_ * nx > 4096)

0 commit comments

Comments
 (0)