Repository navigation
feat(base): sub-communicator helpers for the RI and RPA stages - #8370
AroundPeking wants to merge 6 commits into
Conversation
ABACUS-PR-BOT
left a comment
There was a problem hiding this comment.
The new variable-count gather rejects a valid all-empty receive buffer when assertions are enabled. The new APIs also need focused tests and an accurate MPI-only description.
Static source review at a734ea4; no build or MPI test was run.
RI and RPA run on sub-communicators that are neither MPI_COMM_WORLD nor rooted at rank 0, so the existing `Parallel_Common` entry points would broadcast from the wrong rank on a communicator that cannot be reached from `GlobalV`. Add the communicator-aware counterparts: - `bcast_int` / `bcast_double` over an explicit length and root - `Parallel_Common::my_rank`, `nproc` and `barrier` on an arbitrary communicator - `Parallel_Reduce::gather_int_all` and `gatherv_int_all`, which the RI stages need to collect per-rank block counts All of them are declared under `__MPI` only: without MPI there is no communicator type to pass, so the serial build keeps the existing MPI_COMM_WORLD wrappers and gains nothing here. The PR description says the same. `gatherv_int_all` must accept an all-empty receive buffer. MPI_Allgatherv is valid when every receive count is zero, and an empty std::vector's data() is null in that case, so the guard now demands a buffer only when the total receive count is positive. Every rank still enters the collective regardless of its own counts. The focused test covers a split communicator, a non-zero root, an all-empty gatherv and a populated one. It keeps `__MPI` despite the directory-wide disable, and it is registered only under `ENABLE_MPI`.
a734ea4 to
82854f6
Compare
`addtest()` also registers a single-process ctest entry, and this test needs four ranks: it exits with a failure when `MPI_Comm_size` is below four. The `Module_Base Unittests` step therefore failed on a run that never had a chance to pass. Build the executable directly and register only the `mpirun -np 4` entry. Its own `main` runs `MPI_Init` before `RUN_ALL_TESTS`, so link `GTest::gtest` rather than `GTest::gtest_main` to avoid a duplicate `main`.
ABACUS-PR-BOT
left a comment
There was a problem hiding this comment.
The all-empty receive-buffer case is corrected in source. The new emptiness check can still overflow for large valid count layouts; please use a boolean predicate.
Static review only; no builds or runtime tests were run.
Two review points on the all-empty gatherv guard.
The guard summed the receive counts in an `int`, which can overflow: two ranks
may legitimately pass `{INT_MAX, 1}`, whose sum is negative even though the
receive buffer is valid. Test "any count is positive" with an early exit
instead, so no arithmetic is performed on the counts and every rank still
enters the collective.
Rule 14: the test now names the broadcast payload, the element count and the
zero send count before the calls instead of computing them inline. While
there, cover the cases the older helper tests do not reach: an uneven gather
where rank 0 contributes nothing, `gather_int_all` on the split communicator,
and `reduce_max_loc` including the tie case.
There was a problem hiding this comment.
I suggest change to enclose the function body instead of the whole functions declaration and definition with #ifdef pre-compile spec to keep the calling graph stable, otherwise, the caller should handle with the non-mpi and mpi cases everywhere, which makes the code dirty.
|
Thanks for splitting the communicator-aware helpers into a focused base PR. The API direction looks reasonable for the upcoming RI/RPA changes, and I did not find a clear correctness error in the new production implementations. I have two test-coverage concerns and one minor style comment.
|
Review point: wrapping a whole function in `#ifdef __MPI` makes the interface itself disappear in a serial build, so every caller has to reproduce the guard or fall back to a different code path. The rest of this file already does it the other way round — `bcast_string` and `bcast_char` keep one signature and guard only the MPI calls inside. Do the same here: - `bcast_int`, `bcast_double`, `my_rank`, `nproc`, `barrier`, `bcast_bool` and the `Parallel_Reduce` communicator overloads are now declared unconditionally and provide a one-rank implementation when `__MPI` is off. - `parallel_common.h` declares a namespaced `MPI_Comm` stand-in for serial builds. It sits in the namespace, not at global scope, so a translation unit that also pulls in a real `mpi.h` cannot collide with it. - `parallel_reduce.h` reuses that stand-in instead of defining a second one, so the two headers always agree on the type. `bcast_mpi_offset` stays MPI-only: `MPI_Offset` is an MPI-defined integer type with no serial counterpart, so its declaration has to follow the macro. Also rename `reduce_max_loc` to `reduce_max_with_rank` and its `MaxLoc` pair to `ValueAndSourceRank`. "loc" is MPI's contraction of "location" and there is no other place to explain it; the new names say what the call returns. Verified in both configurations: `parallel_common.cpp` and `parallel_reduce.cpp` compile with and without `__MPI`, and the four-rank test passes.
What
RI and RPA run on sub-communicators that are neither
MPI_COMM_WORLDnor rooted at rank 0.Parallel_Commononly had entry points for the global communicator, so these stages had topick the root by hand.
This adds the communicator-aware counterparts:
bcast_int(int*, n, comm, root)bcast_double(double*, n, comm, root)my_rank(comm)commnproc(comm)commbarrier(comm)command two gathering helpers on the same communicator:
gather_int_allandgatherv_int_all.MPI-only
Every one of these is declared and defined inside
#ifdef __MPI. A serial build has nocommunicator type to pass, so the serial path deliberately gains nothing here and a future caller
still needs its own MPI guard. There is no serial fallback.
gatherv_int_allaccepts an all-empty receive buffer:MPI_Allgathervis valid when every receivecount is zero, and an empty
std::vector'sdata()is null in that case. The guard therefore asksonly that the buffer be non-null when at least one count is positive — it does not sum the counts,
because
{INT_MAX, 1}is a legal input whose sum would overflow. Every rank enters the collectiveregardless of its own counts.
Scope
No callers yet. This lands the API only, so it can be reviewed and merged on its own. The RI
and RPA call sites arrive in the follow-up changes.
Verification
The focused test splits
MPI_COMM_WORLDinto two sub-communicators and covers, on thatcommunicator:
my_rank/nprocagainstMPI_Comm_rank/MPI_Comm_sizebcast_intandbcast_doublefrom a non-zero rootgatherv_int_allwith an all-empty receive buffer, with one element per rank, and with anuneven distribution where rank 0 contributes nothing
gather_int_allreduce_max_loc, including the case where two ranks tie for the maximumIt requires four ranks, so it is registered only as an
mpirun -np 4ctest entry.Dependencies
None. This is the base of a series; the next PR adds the EXX symmetry export that uses
my_rank/nproc/barrieron sub-communicators.