Skip to content

Commit 750ee17

Browse files
committed
MPI: use MPI_Bcast instead of Allreduce for gamma sync
Replace MPI_Allreduce(SUM) with MPI_Bcast from rank 0. Bcast is correct for both: - Gamma-only (all ranks have same data, Bcast distributes rank-0 result) - Distributed multi-k (only rank 0 has k-point 0 data, Bcast provides it) This avoids the nproc division ambiguity in the Allreduce approach.
1 parent 65c9c79 commit 750ee17

1 file changed

Lines changed: 9 additions & 41 deletions

File tree

source/source_lcao/module_deltap/deltap_wannier.cpp

Lines changed: 9 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1562,56 +1562,24 @@ void DeltaP::compute_gamma_scf(const UnitCell& ucell,
15621562
MPI_Comm_size(comm, &nproc);
15631563
if (nproc > 1)
15641564
{
1565-
// Gamma-only (single k-point replicated): each rank computed the same
1566-
// gamma values independently. Sum and divide by nproc for consensus.
1567-
// Multi-k (distributed k-points): partial contributions from each rank
1568-
// combine via sum. Both cases are handled by MPI_Allreduce(SUM) followed
1569-
// by division check (for Gamma-only, all ranks have identical data and
1570-
// the sum = nproc × correct value).
1571-
int nks = (psi != nullptr) ? psi->get_nk() : 1;
1572-
bool gamma_only = (nks == 1);
1573-
1565+
// Broadcast gamma results from rank 0 to all ranks.
1566+
// This handles both Gamma-only (all ranks have same data)
1567+
// and distributed multi-k (only rank 0 has correct k-point-0 data).
15741568
int nat = static_cast<int>(results_.gamma_I.size());
15751569
for (int iat = 0; iat < nat; ++iat)
15761570
{
15771571
for (int dir = 0; dir < 3; ++dir)
15781572
{
1579-
double gamma_val = results_.gamma_I[iat][dir];
1580-
double gamma_raw_val = results_.gamma_I_raw[iat][dir];
1581-
double P_val = results_.P_I[iat][dir];
1582-
double r_val = results_.r_elec_center[iat][dir];
1583-
MPI_Allreduce(MPI_IN_PLACE, &gamma_val, 1, MPI_DOUBLE, MPI_SUM, comm);
1584-
MPI_Allreduce(MPI_IN_PLACE, &gamma_raw_val, 1, MPI_DOUBLE, MPI_SUM, comm);
1585-
MPI_Allreduce(MPI_IN_PLACE, &P_val, 1, MPI_DOUBLE, MPI_SUM, comm);
1586-
MPI_Allreduce(MPI_IN_PLACE, &r_val, 1, MPI_DOUBLE, MPI_SUM, comm);
1587-
if (gamma_only)
1588-
{
1589-
double inv_nproc = 1.0 / nproc;
1590-
gamma_val *= inv_nproc;
1591-
gamma_raw_val *= inv_nproc;
1592-
P_val *= inv_nproc;
1593-
r_val *= inv_nproc;
1594-
}
1595-
results_.gamma_I[iat][dir] = gamma_val;
1596-
results_.gamma_I_raw[iat][dir] = gamma_raw_val;
1597-
results_.P_I[iat][dir] = P_val;
1598-
results_.r_elec_center[iat][dir] = r_val;
1573+
MPI_Bcast(&results_.gamma_I[iat][dir], 1, MPI_DOUBLE, 0, comm);
1574+
MPI_Bcast(&results_.gamma_I_raw[iat][dir], 1, MPI_DOUBLE, 0, comm);
1575+
MPI_Bcast(&results_.P_I[iat][dir], 1, MPI_DOUBLE, 0, comm);
1576+
MPI_Bcast(&results_.r_elec_center[iat][dir], 1, MPI_DOUBLE, 0, comm);
15991577
}
16001578
}
16011579
for (int iat = 0; iat < nat; ++iat)
1602-
{
1603-
double w_val = results_.smo_weight_sum[iat];
1604-
MPI_Allreduce(MPI_IN_PLACE, &w_val, 1, MPI_DOUBLE, MPI_SUM, comm);
1605-
if (gamma_only) w_val /= nproc;
1606-
results_.smo_weight_sum[iat] = w_val;
1607-
}
1580+
MPI_Bcast(&results_.smo_weight_sum[iat], 1, MPI_DOUBLE, 0, comm);
16081581
for (int dir = 0; dir < 3; ++dir)
1609-
{
1610-
double ptot = results_.P_total[dir];
1611-
MPI_Allreduce(MPI_IN_PLACE, &ptot, 1, MPI_DOUBLE, MPI_SUM, comm);
1612-
if (gamma_only) ptot /= nproc;
1613-
results_.P_total[dir] = ptot;
1614-
}
1582+
MPI_Bcast(&results_.P_total[dir], 1, MPI_DOUBLE, 0, comm);
16151583
}
16161584
}
16171585
#endif

0 commit comments

Comments
 (0)