Skip to content

Commit cfa57b4

Browse files
committed
Use GEMM for PPCG projections
1 parent ffa2f72 commit cfa57b4

1 file changed

Lines changed: 37 additions & 23 deletions

File tree

source/source_hsolver/ppcg/diago_ppcg_ops.hpp

Lines changed: 37 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,9 @@ void DiagoPPCG<T, Device>::project_against(
194194
if (basis_cols.empty() || x_cols.empty())
195195
return;
196196

197+
std::vector<T> x_l;
197198
std::vector<T> sx_l;
199+
copy_cols(x.data(), x_cols, x_l);
198200
copy_cols(sx.data(), x_cols, sx_l);
199201

200202
const int nbasis = static_cast<int>(basis_cols.size());
@@ -210,39 +212,51 @@ void DiagoPPCG<T, Device>::project_against(
210212
}
211213

212214
std::vector<T> basis_l;
215+
std::vector<T> sbasis_l;
213216
const T* basis_data = basis;
217+
const T* sbasis_data = sbasis;
214218
if (!contiguous_basis)
215219
{
216220
copy_cols(basis, basis_cols, basis_l);
221+
copy_cols(sbasis, basis_cols, sbasis_l);
217222
basis_data = basis_l.data();
223+
sbasis_data = sbasis_l.data();
218224
}
219225

220226
std::vector<T> coeff(nbasis * nx, T(0));
221227
gram(basis_data, sx_l.data(), nbasis, nx, coeff, nbasis);
222228

223-
#ifdef _OPENMP
224-
#pragma omp parallel for schedule(static) if (n_dim_ * nx > 4096)
225-
#endif
226-
for (int jc = 0; jc < nx; ++jc)
227-
{
228-
const int c = x_cols[jc];
229-
T* xc = x.data() + c * ld_psi_;
230-
T* sxc = sx.data() + c * ld_psi_;
231-
for (int ib = 0; ib < nbasis; ++ib)
232-
{
233-
const int bc = basis_cols[ib];
234-
const T cproj = coeff[ib + jc * nbasis];
235-
if (std::abs(cproj) <= std::numeric_limits<Real>::epsilon())
236-
continue;
237-
const T* bb = basis + bc * ld_psi_;
238-
const T* sb = sbasis + bc * ld_psi_;
239-
for (int ig = 0; ig < n_dim_; ++ig)
240-
{
241-
xc[ig] -= bb[ig] * cproj;
242-
sxc[ig] -= sb[ig] * cproj;
243-
}
244-
}
245-
}
229+
const T minus_one = T(-1);
230+
const T one = T(1);
231+
ModuleBase::gemm_op<T, Device>()('N',
232+
'N',
233+
n_dim_,
234+
nx,
235+
nbasis,
236+
&minus_one,
237+
basis_data,
238+
ld_psi_,
239+
coeff.data(),
240+
nbasis,
241+
&one,
242+
x_l.data(),
243+
ld_psi_);
244+
ModuleBase::gemm_op<T, Device>()('N',
245+
'N',
246+
n_dim_,
247+
nx,
248+
nbasis,
249+
&minus_one,
250+
sbasis_data,
251+
ld_psi_,
252+
coeff.data(),
253+
nbasis,
254+
&one,
255+
sx_l.data(),
256+
ld_psi_);
257+
258+
scatter_cols(x.data(), x_cols, x_l);
259+
scatter_cols(sx.data(), x_cols, sx_l);
246260
}
247261

248262
// =============================================================================

0 commit comments

Comments
 (0)