Skip to content

Commit c996775

Browse files
committed
Fix(paged): resolve Psi PAGED_GPU mode issues with psi_prepare, operator(), force, and stress
- psi_prepare: in PAGED_GPU mode, fix_k returns CPU pointer when k-point is not loaded, causing cudaMemcpy H2D with invalid CPU destination. Fix by copying init data to kspw_psi CPU backing store before load_k_to_gpu. - psi::operator()(ik,iband,ibasis): PAGED_GPU always returned CPU pointer, but GPU callers (force/stress via CUBLAS) need GPU pointers. Fix by returning GPU buffer pointer when current_k_gpu_ matches requested ik. - forces_nl, stress_nl, stress_kin: add ensure_k_on_gpu(ik) call before accessing psi via operator() in k-point loops.
1 parent 2de77d2 commit c996775

5 files changed

Lines changed: 18 additions & 3 deletions

File tree

source/source_psi/psi.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -628,8 +628,12 @@ T& Psi<T, Device>::operator()(const int ikb1, const int ikb2, const int ibasis)
628628

629629
if (storage_mode_ == PsiStorageMode::PAGED_GPU)
630630
{
631-
// PAGED_GPU: GPU buffer is single-k, read from CPU full buffer (k_first layout)
631+
// PAGED_GPU: GPU buffer is single-k, return from GPU if loaded, else CPU
632632
assert(psi_cpu_ != nullptr);
633+
if (this->current_k_gpu_ == ikb1)
634+
{
635+
return this->psi[ikb2 * this->nbasis + ibasis];
636+
}
633637
return const_cast<T*>(psi_cpu_)[(ikb1 * this->nbands + ikb2) * this->nbasis + ibasis];
634638
}
635639

source/source_psi/psi_prepare.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,9 @@ void PSIPrepare<T, Device>::initialize_psi(Psi<std::complex<double>>* psi,
178178
{
179179
if (kspw_psi->get_storage_mode() == psi::PsiStorageMode::PAGED_GPU)
180180
{
181-
syncmem_h2d_op()(psi_device->get_pointer(), psi_cpu->get_pointer(), nbands_start * nbasis);
181+
T* dst_cpu = kspw_psi->get_cpu_pointer(ik);
182+
std::memcpy(dst_cpu, psi_cpu->get_pointer(), sizeof(T) * nbands_start * nbasis);
183+
kspw_psi->load_k_to_gpu(ik);
182184
}
183185
else
184186
{

source/source_pw/module_pwdft/forces_nl.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ void Forces<FPTYPE, Device>::cal_force_nl(ModuleBase::matrix& forcenl,
3737
const int max_nbands = wg.nc;
3838
for (int ik = 0; ik < nks; ik++) // loop k points
3939
{
40+
// Ensure k-point is loaded to GPU for PAGED_GPU mode
41+
const_cast<psi::Psi<std::complex<FPTYPE>, Device>*>(psi_in)->ensure_k_on_gpu(ik);
42+
4043
// skip zero weights to speed up
4144
int nbands_occ = wg.nc;
4245
while (wg(ik, nbands_occ - 1) == 0.0)

source/source_pw/module_pwdft/stress_kin.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,12 @@ void Stress_Func<FPTYPE, Device>::stress_kin(ModuleBase::matrix& sigma,
1818

1919
this->ucell = &ucell_in;
2020

21-
hamilt::FS_Kin_tools<FPTYPE, Device> kin_tool(*this->ucell, p_kv, wfc_basis, wg);
21+
hamilt::FS_Kin_tools<FPTYPE, Device> kin_tool(*this->ucell, p_kv, wfc_basis, wg);
2222
for (int ik = 0; ik < wfc_basis->nks; ++ik)
2323
{
24+
// Ensure k-point is loaded to GPU for PAGED_GPU mode
25+
const_cast<psi::Psi<std::complex<FPTYPE>, Device>*>(psi_in)->ensure_k_on_gpu(ik);
26+
2427
int nbands_occ = wg.nc;
2528
while (wg(ik, nbands_occ - 1) == 0.0)
2629
{

source/source_pw/module_pwdft/stress_nl.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ void Stress_Func<FPTYPE, Device>::stress_nl(ModuleBase::matrix& sigma,
3939
const int max_nbands = wg.nc;
4040
for (int ik = 0; ik < nks; ik++) // loop k points
4141
{
42+
// Ensure k-point is loaded to GPU for PAGED_GPU mode
43+
const_cast<psi::Psi<std::complex<FPTYPE>, Device>*>(psi_in)->ensure_k_on_gpu(ik);
44+
4245
// skip zero weights to speed up
4346
int nbands_occ = wg.nc;
4447
while (wg(ik, nbands_occ - 1) == 0.0)

0 commit comments

Comments
 (0)