Skip to content

Commit bee21a3

Browse files
author
abacus_fixer
committed
refactor(klist): extract MPI k-point pack/unpack into KListIO
Move the this-free flatten/scatter loops in K_Vectors::mpi_k into KListIO::pack_kpts (rank-0 -> contiguous broadcast buffers) and KListIO::unpack_kpts (broadcast buffers -> this pool's k-point slice). mpi_k keeps the broadcast calls and the renew/distribute orchestration. pack_kpts sizes the kvec_c_full copy from the vector length so the full grid is handled independently of nkstot. Verified: cmake --build build_max_para_test -j30 (exit 0, __MPI on) and ctest -R "MODULE_CELL_(klist|reciprocal_grid|qlist)" -> 5/5 (covers the para1/para4 mpi_k paths).
1 parent 3eaeb98 commit bee21a3

3 files changed

Lines changed: 115 additions & 32 deletions

File tree

source/source_cell/klist.cpp

Lines changed: 23 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -727,20 +727,17 @@ void K_Vectors::mpi_k(std::ofstream& ofs_running)
727727
// collect and process in rank 0
728728
if (GlobalV::MY_RANK == 0)
729729
{
730-
for (int ik = 0; ik < this->nkstot; ik++)
731-
{
732-
isk_aux[ik] = this->isk[ik];
733-
wk_aux[ik] = this->wk[ik];
734-
kvec_c_aux[3 * ik] = this->kvec_c[ik].x;
735-
kvec_c_aux[3 * ik + 1] = this->kvec_c[ik].y;
736-
kvec_c_aux[3 * ik + 2] = this->kvec_c[ik].z;
737-
kvec_d_aux[3 * ik] = this->kvec_d[ik].x;
738-
kvec_d_aux[3 * ik + 1] = this->kvec_d[ik].y;
739-
kvec_d_aux[3 * ik + 2] = this->kvec_d[ik].z;
740-
kvec_c_full_aux[3 * ik] = this->kvec_c_full[ik].x;
741-
kvec_c_full_aux[3 * ik + 1] = this->kvec_c_full[ik].y;
742-
kvec_c_full_aux[3 * ik + 2] = this->kvec_c_full[ik].z;
743-
}
730+
KListIO::pack_kpts(this->isk,
731+
this->wk,
732+
this->kvec_c,
733+
this->kvec_d,
734+
this->kvec_c_full,
735+
this->nkstot,
736+
isk_aux,
737+
wk_aux,
738+
kvec_c_aux,
739+
kvec_d_aux,
740+
kvec_c_full_aux);
744741
}
745742

746743
// broadcast k point data to all processors
@@ -755,24 +752,18 @@ void K_Vectors::mpi_k(std::ofstream& ofs_running)
755752
this->renew(this->nks * this->spin_mult);
756753

757754
// distribute
758-
int k_index = 0;
759-
760-
for (int i = 0; i < this->nks; i++)
761-
{
762-
// 3 is because each k point has three value:kx, ky, kz
763-
k_index = i + this->para_k.startk_pool[GlobalV::MY_POOL];
764-
this->kvec_c[i].x = kvec_c_aux[k_index * 3];
765-
this->kvec_c[i].y = kvec_c_aux[k_index * 3 + 1];
766-
this->kvec_c[i].z = kvec_c_aux[k_index * 3 + 2];
767-
this->kvec_d[i].x = kvec_d_aux[k_index * 3];
768-
this->kvec_d[i].y = kvec_d_aux[k_index * 3 + 1];
769-
this->kvec_d[i].z = kvec_d_aux[k_index * 3 + 2];
770-
this->kvec_c_full[i].x = kvec_c_full_aux[k_index * 3];
771-
this->kvec_c_full[i].y = kvec_c_full_aux[k_index * 3 + 1];
772-
this->kvec_c_full[i].z = kvec_c_full_aux[k_index * 3 + 2];
773-
this->wk[i] = wk_aux[k_index];
774-
this->isk[i] = isk_aux[k_index];
775-
}
755+
KListIO::unpack_kpts(isk_aux,
756+
wk_aux,
757+
kvec_c_aux,
758+
kvec_d_aux,
759+
kvec_c_full_aux,
760+
this->nks,
761+
this->para_k.startk_pool[GlobalV::MY_POOL],
762+
this->isk,
763+
this->wk,
764+
this->kvec_c,
765+
this->kvec_d,
766+
this->kvec_c_full);
776767

777768
#ifdef __EXX
778769
if (ModuleSymmetry::Symmetry::symm_flag == 1)

source/source_cell/klist_io.cpp

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,4 +178,67 @@ void build_kstars(const std::vector<ModuleBase::Vector3<double>>& kvec_d,
178178
}
179179
}
180180

181+
void pack_kpts(const std::vector<int>& isk,
182+
const std::vector<double>& wk,
183+
const std::vector<ModuleBase::Vector3<double>>& kvec_c,
184+
const std::vector<ModuleBase::Vector3<double>>& kvec_d,
185+
const std::vector<ModuleBase::Vector3<double>>& kvec_c_full,
186+
const int nkstot,
187+
std::vector<int>& isk_aux,
188+
std::vector<double>& wk_aux,
189+
std::vector<double>& kvec_c_aux,
190+
std::vector<double>& kvec_d_aux,
191+
std::vector<double>& kvec_c_full_aux)
192+
{
193+
for (int ik = 0; ik < nkstot; ik++)
194+
{
195+
isk_aux[ik] = isk[ik];
196+
wk_aux[ik] = wk[ik];
197+
kvec_c_aux[3 * ik] = kvec_c[ik].x;
198+
kvec_c_aux[3 * ik + 1] = kvec_c[ik].y;
199+
kvec_c_aux[3 * ik + 2] = kvec_c[ik].z;
200+
kvec_d_aux[3 * ik] = kvec_d[ik].x;
201+
kvec_d_aux[3 * ik + 1] = kvec_d[ik].y;
202+
kvec_d_aux[3 * ik + 2] = kvec_d[ik].z;
203+
}
204+
const int nkstot_full = static_cast<int>(kvec_c_full.size());
205+
for (int ik = 0; ik < nkstot_full; ik++)
206+
{
207+
kvec_c_full_aux[3 * ik] = kvec_c_full[ik].x;
208+
kvec_c_full_aux[3 * ik + 1] = kvec_c_full[ik].y;
209+
kvec_c_full_aux[3 * ik + 2] = kvec_c_full[ik].z;
210+
}
211+
}
212+
213+
void unpack_kpts(const std::vector<int>& isk_aux,
214+
const std::vector<double>& wk_aux,
215+
const std::vector<double>& kvec_c_aux,
216+
const std::vector<double>& kvec_d_aux,
217+
const std::vector<double>& kvec_c_full_aux,
218+
const int nks,
219+
const int startk,
220+
std::vector<int>& isk,
221+
std::vector<double>& wk,
222+
std::vector<ModuleBase::Vector3<double>>& kvec_c,
223+
std::vector<ModuleBase::Vector3<double>>& kvec_d,
224+
std::vector<ModuleBase::Vector3<double>>& kvec_c_full)
225+
{
226+
for (int i = 0; i < nks; i++)
227+
{
228+
// 3 is because each k point has three value:kx, ky, kz
229+
const int k_index = i + startk;
230+
kvec_c[i].x = kvec_c_aux[k_index * 3];
231+
kvec_c[i].y = kvec_c_aux[k_index * 3 + 1];
232+
kvec_c[i].z = kvec_c_aux[k_index * 3 + 2];
233+
kvec_d[i].x = kvec_d_aux[k_index * 3];
234+
kvec_d[i].y = kvec_d_aux[k_index * 3 + 1];
235+
kvec_d[i].z = kvec_d_aux[k_index * 3 + 2];
236+
kvec_c_full[i].x = kvec_c_full_aux[k_index * 3];
237+
kvec_c_full[i].y = kvec_c_full_aux[k_index * 3 + 1];
238+
kvec_c_full[i].z = kvec_c_full_aux[k_index * 3 + 2];
239+
wk[i] = wk_aux[k_index];
240+
isk[i] = isk_aux[k_index];
241+
}
242+
}
243+
181244
} // namespace KListIO

source/source_cell/klist_io.h

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,35 @@ void build_kstars(const std::vector<ModuleBase::Vector3<double>>& kvec_d,
5151
double epsilon,
5252
const std::function<bool(double, double)>& equal,
5353
std::vector<std::map<int, ModuleBase::Vector3<double>>>& kstars);
54+
55+
/// Flatten k-point arrays into contiguous MPI buffers (x,y,z interleaved).
56+
/// this-free; used on rank 0 before broadcasting in K_Vectors::mpi_k.
57+
void pack_kpts(const std::vector<int>& isk,
58+
const std::vector<double>& wk,
59+
const std::vector<ModuleBase::Vector3<double>>& kvec_c,
60+
const std::vector<ModuleBase::Vector3<double>>& kvec_d,
61+
const std::vector<ModuleBase::Vector3<double>>& kvec_c_full,
62+
int nkstot,
63+
std::vector<int>& isk_aux,
64+
std::vector<double>& wk_aux,
65+
std::vector<double>& kvec_c_aux,
66+
std::vector<double>& kvec_d_aux,
67+
std::vector<double>& kvec_c_full_aux);
68+
69+
/// Scatter the broadcast buffers into this pool's k-point slice, starting at
70+
/// global index `startk`. this-free; mirrors pack_kpts after the broadcast.
71+
void unpack_kpts(const std::vector<int>& isk_aux,
72+
const std::vector<double>& wk_aux,
73+
const std::vector<double>& kvec_c_aux,
74+
const std::vector<double>& kvec_d_aux,
75+
const std::vector<double>& kvec_c_full_aux,
76+
int nks,
77+
int startk,
78+
std::vector<int>& isk,
79+
std::vector<double>& wk,
80+
std::vector<ModuleBase::Vector3<double>>& kvec_c,
81+
std::vector<ModuleBase::Vector3<double>>& kvec_d,
82+
std::vector<ModuleBase::Vector3<double>>& kvec_c_full);
5483
} // namespace KListIO
5584

5685
#endif // KLIST_IO_H

0 commit comments

Comments
 (0)