Skip to content

Commit 937e119

Browse files
authored
Merge pull request #5857 from ye-luo/introduce-intra-group-comm
Replace group-leader Communicator with inter-group Communicator
2 parents 21cf24e + 56344c5 commit 937e119

7 files changed

Lines changed: 50 additions & 86 deletions

File tree

src/Message/Communicate.cpp

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ Communicate::~Communicate() = default;
4848
//exclusive: MPI or Serial
4949
#ifdef HAVE_MPI
5050

51-
// in_comm needs to be mutable to be duplicated
51+
// in_comm needs to be mutable to be duplicated
5252
Communicate::Communicate(mpi3::communicator& in_comm) : d_groupid(0), d_ngroups(1), comm{in_comm.duplicate()}
5353
{
5454
myMPI = comm.get();
@@ -66,7 +66,7 @@ Communicate::Communicate(mpi3::communicator&& in_comm) : d_groupid(0), d_ngroups
6666
Communicate::Communicate(const Communicate& in_comm, int nparts)
6767
{
6868
std::vector<int> nplist(nparts + 1);
69-
int p = FairDivideLow(in_comm.rank(), in_comm.size(), nparts, nplist); //group
69+
const int p = FairDivideLow(in_comm.rank(), in_comm.size(), nparts, nplist); //group
7070
// comm is mutable member
7171
comm = in_comm.comm.split(p, in_comm.rank());
7272
myMPI = comm.get();
@@ -75,17 +75,13 @@ Communicate::Communicate(const Communicate& in_comm, int nparts)
7575
d_ncontexts = comm.size();
7676
d_groupid = p;
7777
d_ngroups = nparts;
78-
// create a communicator among group leaders.
79-
80-
nplist.pop_back();
81-
mpi3::communicator leader_comm = in_comm.comm.subcomm(nplist);
82-
if (isGroupLeader())
83-
GroupLeaderComm = std::make_unique<Communicate>(leader_comm);
84-
else
85-
GroupLeaderComm.reset();
78+
79+
// create an inter group communicator
80+
inter_group_comm_ = std::make_unique<Communicate>(in_comm.comm.split(comm.rank(), in_comm.rank()));
8681
}
8782

88-
Communicate Communicate::NodeComm() const {
83+
Communicate Communicate::NodeComm() const
84+
{
8985
// comm is mutable member
9086
return Communicate{comm.split_shared()};
9187
}
@@ -107,7 +103,7 @@ void Communicate::abort() const { comm.abort(1); }
107103
void Communicate::barrier() const { comm.barrier(); }
108104
#else
109105

110-
Communicate Communicate::NodeComm() const {return Communicate{};}
106+
Communicate Communicate::NodeComm() const { return Communicate{}; }
111107

112108
void Communicate::finalize() {}
113109

@@ -119,9 +115,7 @@ void Communicate::cleanupMessage(void*) {}
119115

120116
Communicate::Communicate(const Communicate& in_comm, int nparts)
121117
: myMPI(MPI_COMM_NULL), d_mycontext(0), d_ncontexts(1), d_groupid(0)
122-
{
123-
GroupLeaderComm = std::make_unique<Communicate>();
124-
}
118+
{ inter_group_comm_ = std::make_unique<Communicate>(); }
125119
#endif // !HAVE_MPI
126120

127121
void Communicate::barrier_and_abort(const std::string& msg) const

src/Message/Communicate.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -215,12 +215,12 @@ class Communicate : public CommunicatorTraits
215215
int d_groupid;
216216
/// Total number of groups in the parent communicator
217217
int d_ngroups;
218-
/// Group Leader Communicator
219-
std::unique_ptr<Communicate> GroupLeaderComm;
218+
/// inter group communicator
219+
std::unique_ptr<Communicate> inter_group_comm_;
220220

221221
public:
222222
// Avoid public access to unique_ptr.
223-
Communicate* getGroupLeaderComm() { return GroupLeaderComm.get(); }
223+
Communicate& getInterGroupComm() const { return *inter_group_comm_; }
224224

225225
#ifdef HAVE_MPI
226226
/// mpi3 communicator wrapper

src/Message/tests/test_communciate.cpp

Lines changed: 21 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -24,19 +24,11 @@ TEST_CASE("test_communicate_split_one", "[message]")
2424
REQUIRE(c2->size() == c->size());
2525
REQUIRE(c2->rank() == c->rank());
2626

27-
if (c->rank() == 0)
28-
{
29-
REQUIRE(c2->isGroupLeader() == true);
30-
auto GroupLeaderComm = c2->getGroupLeaderComm();
31-
REQUIRE(GroupLeaderComm != nullptr);
32-
REQUIRE(GroupLeaderComm->size() == 1);
33-
REQUIRE(GroupLeaderComm->rank() == 0);
34-
}
35-
else
36-
{
37-
REQUIRE(c2->isGroupLeader() == false);
38-
REQUIRE(c2->getGroupLeaderComm() == nullptr);
39-
}
27+
REQUIRE(c2->isGroupLeader() == (c->rank() == 0));
28+
29+
auto& inter_group_comm = c2->getInterGroupComm();
30+
REQUIRE(inter_group_comm.size() == 1);
31+
REQUIRE(inter_group_comm.rank() == 0);
4032

4133
std::string real_name = c->getName();
4234

@@ -66,37 +58,23 @@ TEST_CASE("test_communicate_split_two", "[message]")
6658
int new_group_id = c->rank() < midpoint ? 0 : 1;
6759
int new_rank = c->rank();
6860
if (c->rank() >= midpoint)
69-
{
7061
new_rank -= midpoint;
71-
}
62+
7263
// Adjust for odd size - the last group has the extra process
7364
if (c->size() % 2 == 1)
74-
{
7565
new_size[1] = new_size[1] + 1;
76-
}
66+
7767
REQUIRE(c2->size() == new_size[new_group_id]);
7868
REQUIRE(c2->rank() == new_rank);
7969

80-
if (c->rank() == 0 || c->rank() == midpoint)
81-
{
82-
REQUIRE(c2->isGroupLeader() == true);
83-
auto GroupLeaderComm = c2->getGroupLeaderComm();
84-
REQUIRE(GroupLeaderComm != nullptr);
85-
REQUIRE(GroupLeaderComm->size() == 2);
86-
if (c->rank() == 0)
87-
{
88-
REQUIRE(GroupLeaderComm->rank() == 0);
89-
}
90-
else
91-
{
92-
REQUIRE(GroupLeaderComm->rank() == 1);
93-
}
94-
}
70+
REQUIRE(c2->isGroupLeader() == (c->rank() == 0 || c->rank() == midpoint));
71+
72+
auto& inter_group_comm = c2->getInterGroupComm();
73+
REQUIRE(inter_group_comm.size() == (c->rank() < midpoint * 2 ? 2 : 1));
74+
if (c->rank() < midpoint * 2)
75+
REQUIRE(inter_group_comm.rank() == new_group_id);
9576
else
96-
{
97-
REQUIRE(c2->isGroupLeader() == false);
98-
REQUIRE(c2->getGroupLeaderComm() == nullptr);
99-
}
77+
REQUIRE(inter_group_comm.rank() == 0);
10078
}
10179
}
10280

@@ -108,24 +86,14 @@ TEST_CASE("test_communicate_split_four", "[message]")
10886
{
10987
auto c2 = std::make_unique<Communicate>(*c, 4);
11088

111-
REQUIRE(c2->size() == c->size() / 4);
112-
int group_size = c->size() / 4;
113-
int new_rank = c->rank() % group_size;
89+
const int group_size = c->size() / 4;
90+
const int new_rank = c->rank() % group_size;
91+
REQUIRE(c2->size() == group_size);
11492
REQUIRE(c2->rank() == new_rank);
115-
116-
if (new_rank == 0)
117-
{
118-
REQUIRE(c2->isGroupLeader() == true);
119-
auto GroupLeaderComm = c2->getGroupLeaderComm();
120-
REQUIRE(GroupLeaderComm != nullptr);
121-
REQUIRE(GroupLeaderComm->size() == 4);
122-
REQUIRE(GroupLeaderComm->rank() == c->rank() / group_size);
123-
}
124-
else
125-
{
126-
REQUIRE(c2->isGroupLeader() == false);
127-
REQUIRE(c2->getGroupLeaderComm() == nullptr);
128-
}
93+
REQUIRE(c2->isGroupLeader() == (new_rank == 0));
94+
auto& inter_group_comm = c2->getInterGroupComm();
95+
REQUIRE(inter_group_comm.size() == 4);
96+
REQUIRE(inter_group_comm.rank() == c->rank() / group_size);
12997
}
13098
}
13199

src/QMCWaveFunctions/BsplineFactory/HybridRepCenterOrbitals.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,19 +51,19 @@ bool AtomicOrbitals<ST>::write_splines(hdf_archive& h5f)
5151
}
5252

5353
template<typename ST>
54-
void HybridRepCenterOrbitals<ST>::bcast_atomic_tables(Communicate* comm)
54+
void HybridRepCenterOrbitals<ST>::bcast_atomic_tables(Communicate& comm)
5555
{
5656
for (int ic = 0; ic < AtomicCenters.size(); ic++)
57-
AtomicCenters[ic].bcast_tables(*comm);
57+
AtomicCenters[ic].bcast_tables(comm);
5858
}
5959

6060
template<typename ST>
61-
void HybridRepCenterOrbitals<ST>::gather_atomic_tables(Communicate* comm, const std::vector<int>& offset)
61+
void HybridRepCenterOrbitals<ST>::gather_atomic_tables(Communicate& comm, const std::vector<int>& offset)
6262
{
63-
if (comm->size() == 1)
63+
if (comm.size() == 1)
6464
return;
6565
for (int ic = 0; ic < AtomicCenters.size(); ic++)
66-
AtomicCenters[ic].gather_tables(*comm, offset);
66+
AtomicCenters[ic].gather_tables(comm, offset);
6767
}
6868

6969
template<typename ST>

src/QMCWaveFunctions/BsplineFactory/HybridRepCenterOrbitals.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -550,8 +550,8 @@ class HybridRepCenterOrbitals
550550
<< "for the atomic radial splines in hybrid orbital representation" << std::endl;
551551
}
552552

553-
void bcast_atomic_tables(Communicate* comm);
554-
void gather_atomic_tables(Communicate* comm, const std::vector<int>& offset);
553+
void bcast_atomic_tables(Communicate& comm);
554+
void gather_atomic_tables(Communicate& comm, const std::vector<int>& offset);
555555

556556
inline void flush_zero()
557557
{

src/QMCWaveFunctions/BsplineFactory/HybridRepSetReader.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,7 @@ std::unique_ptr<SPOSet> HybridRepSetReader<ST>::create_spline_set(const std::str
280280
{
281281
Timer now;
282282
SplineUtils<ST>::bcast(multi_splines, *myComm);
283-
hybrid_center_orbs.bcast_atomic_tables(myComm);
283+
hybrid_center_orbs.bcast_atomic_tables(*myComm);
284284
app_log() << " Time to bcast the table = " << now.elapsed() << std::endl;
285285
}
286286
return bspline;
@@ -751,19 +751,20 @@ void HybridRepSetReader<ST>::initialize_hybrid_pio_gather(const int spin,
751751
myComm->barrier();
752752
if (band_group_comm.isGroupLeader())
753753
{
754+
auto& group_leader_comm = band_group_comm.getInterGroupComm();
754755
Timer now;
755756
if (use_duplex_splines_)
756757
{
757758
std::vector<int> offset(band_groups.size());
758759
for (int i = 0; i < offset.size(); i++)
759760
offset[i] = band_groups[i] * 2;
760-
SplineUtils<ST>::gatherv(multi_splines, offset, *band_group_comm.getGroupLeaderComm());
761-
multi_atomic_splines.gather_atomic_tables(band_group_comm.getGroupLeaderComm(), offset);
761+
SplineUtils<ST>::gatherv(multi_splines, offset, group_leader_comm);
762+
multi_atomic_splines.gather_atomic_tables(group_leader_comm, offset);
762763
}
763764
else
764765
{
765-
SplineUtils<ST>::gatherv(multi_splines, band_groups, *band_group_comm.getGroupLeaderComm());
766-
multi_atomic_splines.gather_atomic_tables(band_group_comm.getGroupLeaderComm(), band_groups);
766+
SplineUtils<ST>::gatherv(multi_splines, band_groups, group_leader_comm);
767+
multi_atomic_splines.gather_atomic_tables(group_leader_comm, band_groups);
767768
}
768769

769770
app_log() << " Time to gather the table = " << now.elapsed() << std::endl;

src/QMCWaveFunctions/BsplineFactory/SplineSetReader.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,7 @@ void SplineSetReader<ST>::initialize_spline_pio_gather(const int spin,
176176
const std::vector<BandInfo>& cur_bands = bandgroup.myBands;
177177
if (band_group_comm.isGroupLeader())
178178
{
179+
auto& group_leader_comm = band_group_comm.getInterGroupComm();
179180
h5f.open(mybuilder->H5FileName, H5F_ACC_RDONLY);
180181
for (int iorb = iorb_first; iorb < iorb_last; iorb++)
181182
{
@@ -193,17 +194,17 @@ void SplineSetReader<ST>::initialize_spline_pio_gather(const int spin,
193194
}
194195

195196
{
196-
band_group_comm.getGroupLeaderComm()->barrier();
197+
group_leader_comm.barrier();
197198
Timer now;
198199
if (use_duplex_splines_)
199200
{
200201
std::vector<int> offset(band_groups.size());
201202
for (int i = 0; i < offset.size(); i++)
202203
offset[i] = band_groups[i] * 2;
203-
SplineUtils<ST>::gatherv(multi_splines, offset, *band_group_comm.getGroupLeaderComm());
204+
SplineUtils<ST>::gatherv(multi_splines, offset, group_leader_comm);
204205
}
205206
else
206-
SplineUtils<ST>::gatherv(multi_splines, band_groups, *band_group_comm.getGroupLeaderComm());
207+
SplineUtils<ST>::gatherv(multi_splines, band_groups, group_leader_comm);
207208
app_log() << " Time to gather the table = " << now.elapsed() << std::endl;
208209
}
209210
}

0 commit comments

Comments
 (0)