Skip to content

Commit 52ee22f

Browse files
author
abacus_fixer
committed
refactor(klist): extract EXX k-star broadcast into KListIO::bcast_kstars
Move the rank-0 -> all kstars broadcast loop out of K_Vectors::mpi_k into a this-free KListIO helper. The wrappers compile to no-ops in serial builds, so the helper is guarded only by __EXX at the call site like the original block.
1 parent 7f34127 commit 52ee22f

3 files changed

Lines changed: 45 additions & 27 deletions

File tree

source/source_cell/klist.cpp

Lines changed: 3 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -784,34 +784,10 @@ void K_Vectors::mpi_k(std::ofstream& ofs_running, const int my_rank, const int m
784784
this->kvec_c_full);
785785

786786
#ifdef __EXX
787+
// bcast kstars (rank 0 holds the filled maps; other ranks rebuild them)
787788
if (ModuleSymmetry::Symmetry::symm_flag == 1)
788-
{ // bcast kstars
789-
this->kstars.resize(this->nkstot);
790-
for (int ikibz = 0; ikibz < this->nkstot; ++ikibz)
791-
{
792-
int starsize = this->kstars[ikibz].size();
793-
Parallel_Common::bcast_int(starsize);
794-
auto ks = this->kstars[ikibz].begin();
795-
for (int ik = 0; ik < starsize; ++ik)
796-
{
797-
int isym = 0;
798-
ModuleBase::Vector3<double> ks_vec(0, 0, 0);
799-
if (my_rank == 0)
800-
{
801-
isym = ks->first;
802-
ks_vec = ks->second;
803-
++ks;
804-
}
805-
Parallel_Common::bcast_int(isym);
806-
Parallel_Common::bcast_double(ks_vec.x);
807-
Parallel_Common::bcast_double(ks_vec.y);
808-
Parallel_Common::bcast_double(ks_vec.z);
809-
if (my_rank != 0)
810-
{
811-
this->kstars[ikibz].insert(std::make_pair(isym, ks_vec));
812-
}
813-
}
814-
}
789+
{
790+
KListIO::bcast_kstars(this->kstars, this->nkstot, my_rank);
815791
}
816792
#endif
817793
} // END SUBROUTINE mpi_k

source/source_cell/klist_io.cpp

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
#include "source_base/formatter.h"
1010
#include "source_base/global_function.h"
11+
#include "source_base/parallel_common.h"
1112
#include "source_cell/reciprocal_grid.h"
1213

1314
#include <sstream>
@@ -269,4 +270,36 @@ void unpack_kpts(const std::vector<int>& isk_aux,
269270
}
270271
}
271272

273+
void bcast_kstars(std::vector<std::map<int, ModuleBase::Vector3<double>>>& kstars,
274+
const int nkstot,
275+
const int my_rank)
276+
{
277+
kstars.resize(nkstot);
278+
for (int ikibz = 0; ikibz < nkstot; ++ikibz)
279+
{
280+
int starsize = kstars[ikibz].size();
281+
Parallel_Common::bcast_int(starsize);
282+
auto ks = kstars[ikibz].begin();
283+
for (int ik = 0; ik < starsize; ++ik)
284+
{
285+
int isym = 0;
286+
ModuleBase::Vector3<double> ks_vec(0, 0, 0);
287+
if (my_rank == 0)
288+
{
289+
isym = ks->first;
290+
ks_vec = ks->second;
291+
++ks;
292+
}
293+
Parallel_Common::bcast_int(isym);
294+
Parallel_Common::bcast_double(ks_vec.x);
295+
Parallel_Common::bcast_double(ks_vec.y);
296+
Parallel_Common::bcast_double(ks_vec.z);
297+
if (my_rank != 0)
298+
{
299+
kstars[ikibz].insert(std::make_pair(isym, ks_vec));
300+
}
301+
}
302+
}
303+
}
304+
272305
} // namespace KListIO

source/source_cell/klist_io.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,15 @@ void pack_kpts(const std::vector<int>& isk,
7979
std::vector<double>& kvec_d_aux,
8080
std::vector<double>& kvec_c_full_aux);
8181

82+
/// Broadcast the EXX k-stars (one (symmetry-index, k-vector) map per IBZ
83+
/// k-point) from `my_rank == 0` to every process. Rank 0 holds the filled
84+
/// maps; other ranks resize and rebuild them from the broadcast. MPI
85+
/// wrappers are compiled as no-ops without __MPI, so the call is safe in
86+
/// serial builds (it simply leaves the rank-0 maps untouched).
87+
void bcast_kstars(std::vector<std::map<int, ModuleBase::Vector3<double>>>& kstars,
88+
int nkstot,
89+
int my_rank);
90+
8291
/// Scatter the broadcast buffers into this pool's k-point slice, starting at
8392
/// global index `startk`. this-free; mirrors pack_kpts after the broadcast.
8493
void unpack_kpts(const std::vector<int>& isk_aux,

0 commit comments

Comments
 (0)