Skip to content

Commit c63f50c

Browse files
author
dyzheng
committed
fix: auto-load k-point to GPU in PAGED_GPU fix_k/fix_b/fix_kb
When device_memory_mode defaults to 'paged' (auto mode on GPU PW), fix_k() returned CPU buffer pointer for k-points not yet loaded, causing psiToRho and other GPU consumers to read CPU memory instead of GPU memory. This produced wrong charge density and incorrect total energy for multi-k-point GPU tests (003, 078, etc.). Fix: make current_k_gpu_ mutable, call load_k_to_gpu in fix_k/fix_b/ fix_kb when the requested k-point differs from the loaded one. Non-PAGED modes are unaffected (load_k_to_gpu is a no-op for ALL_GPU).
1 parent 7f107b3 commit c63f50c

2 files changed

Lines changed: 20 additions & 9 deletions

File tree

source/source_psi/psi.cpp

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -536,16 +536,13 @@ void Psi<T, Device>::fix_k(const int ik) const
536536
if (storage_mode_ == PsiStorageMode::PAGED_GPU)
537537
{
538538
this->psi_bias = 0;
539-
if (this->current_k_gpu_ == ik)
539+
if (this->current_k_gpu_ != ik)
540540
{
541-
// This k-point is loaded on GPU by load_k_to_gpu
542-
this->psi_current = const_cast<T*>(this->psi);
541+
const_cast<Psi*>(this)->load_k_to_gpu(ik);
543542
}
544543
else
545544
{
546-
// Read from CPU full buffer (psi_cpu_ has all k-points)
547-
this->psi_current = const_cast<T*>(psi_cpu_
548-
+ static_cast<size_t>(ik) * this->nbands * this->nbasis);
545+
this->psi_current = const_cast<T*>(this->psi);
549546
}
550547
return;
551548
}
@@ -572,7 +569,14 @@ void Psi<T, Device>::fix_b(const int ib) const
572569
if (storage_mode_ == PsiStorageMode::PAGED_GPU)
573570
{
574571
this->psi_bias = 0;
575-
this->psi_current = const_cast<T*>(this->psi);
572+
if (this->current_k_gpu_ != this->current_k)
573+
{
574+
const_cast<Psi*>(this)->load_k_to_gpu(this->current_k);
575+
}
576+
else
577+
{
578+
this->psi_current = const_cast<T*>(this->psi);
579+
}
576580
return;
577581
}
578582

@@ -604,7 +608,14 @@ void Psi<T, Device>::fix_kb(const int ik, const int ib) const
604608
if (storage_mode_ == PsiStorageMode::PAGED_GPU)
605609
{
606610
this->psi_bias = 0;
607-
this->psi_current = const_cast<T*>(this->psi);
611+
if (this->current_k_gpu_ != ik)
612+
{
613+
const_cast<Psi*>(this)->load_k_to_gpu(ik);
614+
}
615+
else
616+
{
617+
this->psi_current = const_cast<T*>(this->psi);
618+
}
608619
return;
609620
}
610621

source/source_psi/psi.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ class Psi
190190
bool psi_cpu_owned_ = true;
191191
T* psi_gpu_buffer_ = nullptr;
192192
T* psi_gpu_transfer_buffer_ = nullptr;
193-
int current_k_gpu_ = -1;
193+
mutable int current_k_gpu_ = -1;
194194

195195
#ifdef __DSP
196196
using delete_memory_op = base_device::memory::delete_memory_op_mt<T, Device>;

0 commit comments

Comments
 (0)