Skip to content

feat(base): sub-communicator helpers for the RI and RPA stages - #8370

Open
AroundPeking wants to merge 6 commits into
deepmodeling:developfrom
AroundPeking:split/1-base-parallel
Open

AroundPeking wants to merge 6 commits into
deepmodeling:developfrom
AroundPeking:split/1-base-parallel

Conversation

@AroundPeking

@AroundPeking AroundPeking commented Nov 24, 2026 •

Copy link
Copy Markdown

What

RI and RPA run on sub-communicators that are neither MPI_COMM_WORLD nor rooted at rank 0.
Parallel_Common only had entry points for the global communicator, so these stages had to
pick the root by hand.

This adds the communicator-aware counterparts:

new meaning
bcast_int(int*, n, comm, root) broadcast an array from an explicit root
bcast_double(double*, n, comm, root) same for doubles
my_rank(comm) rank of the caller on comm
nproc(comm) size of comm
barrier(comm) barrier on comm

and two gathering helpers on the same communicator: gather_int_all and gatherv_int_all.

MPI-only

Every one of these is declared and defined inside #ifdef __MPI. A serial build has no
communicator 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_all accepts 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. The guard therefore asks
only 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 collective
regardless 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_WORLD into two sub-communicators and covers, on that
communicator:

  • my_rank / nproc against MPI_Comm_rank / MPI_Comm_size
  • bcast_int and bcast_double from a non-zero root
  • gatherv_int_all with an all-empty receive buffer, with one element per rank, and with an
    uneven distribution where rank 0 contributes nothing
  • gather_int_all
  • reduce_max_loc, including the case where two ranks tie for the maximum

It requires four ranks, so it is registered only as an mpirun -np 4 ctest entry.

Dependencies

None. This is the base of a series; the next PR adds the EXX symmetry export that uses
my_rank/nproc/barrier on sub-communicators.

@ABACUS-PR-BOT ABACUS-PR-BOT left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread source/source_base/parallel_reduce.cpp Resolved
Comment thread source/source_base/parallel_reduce.cpp Resolved
Comment thread source/source_base/parallel_common.h Resolved
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`.
`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`.
@mohanchen mohanchen added Refactor Refactor ABACUS codes EXX/lr-TDDFT/RPA/BSE/GW Related to beyond DFT methods labels Nov 24, 2026

@ABACUS-PR-BOT ABACUS-PR-BOT left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread source/source_base/parallel_reduce.cpp Resolved
AroundPeking and others added 2 commits November 25, 2026 10:53
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.
@mohanchen mohanchen added the GPU & DCU & HPC GPU and DCU and HPC related any issues label Nov 25, 2026

@kirk0830 kirk0830 left a comment •

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread source/source_base/parallel_common.cpp Resolved
Comment thread source/source_base/parallel_reduce.cpp Resolved
Comment thread source/source_base/parallel_reduce.cpp Resolved
@MrLi000001

Copy link
Copy Markdown
Collaborator

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.

  1. Medium: make the expected values differ between the two sub-communicators
    Files: source/source_base/unittests/test_parallel_common.cpp:52-67 and :129-141
    Within each broadcast test, the two sub-communicators reuse identical payload values: both groups use 41 for bcast_int, and both groups use 7.5 for bcast_double. Consequently, an incorrect implementation that broadcasts on MPI_COMM_WORLD could still pass these assertions: every rank would receive the value supplied by world rank 1, which is the same value expected by the test.
    The reduce_max_loc case has a similar weakness because both groups use the same local values and accept the same result ranks. It therefore does not reliably distinguish a sub-communicator reduction from a world-communicator reduction.
    Please derive the payloads and expected results from colour, so that the two groups have different values. The gather inputs and expected values should likewise be group-dependent where practical. This would make the test fail if an implementation accidentally routes a collective through MPI_COMM_WORLD.
  2. Medium: add runtime coverage for the new reduction overloads
    File: source/source_base/parallel_reduce.cpp:132-153 and :206-220
    The PR adds communicator-aware reduce_all, reduce_max, and reduce_min overloads, including unsigned long long support, but the new test only calls reduce_max_loc. As written, the test does not verify that the other reduction overloads produce correct results on a split communicator.
    Please add focused checks for:
  • scalar and array reduce_all;
  • reduce_max and reduce_min;
  • the unsigned long long scalar and array instantiations;
  • group-dependent inputs and expected results, so the tests also verify communicator routing.
  1. Low: move the conditional expression out of the argument list
    File: source/source_base/unittests/test_parallel_common.cpp:110
    The following call contains a conditional expression in its argument list, contrary to AGENTS.md rule 14:
    Parallel_Reduce::gatherv_int_all(sub, sends_one ? &uneven_send : nullptr,
    uneven_send_count, uneven_receive.data(),
    uneven_counts.data(), uneven_displacements.data());
    Please name the pointer first and then pass the named variable:
    const int* uneven_send_buffer = sends_one ? &uneven_send : nullptr;
    Parallel_Reduce::gatherv_int_all(sub, uneven_send_buffer,
    uneven_send_count, uneven_receive.data(),
    uneven_counts.data(), uneven_displacements.data());
    Overall
    The scope is suitably isolated: this PR introduces the MPI-only APIs and their focused test without changing existing RI/RPA call sites. I would prefer to see the first two test issues addressed before merge, because communicator routing is the central behavior introduced by this PR.

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.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

EXX/lr-TDDFT/RPA/BSE/GW Related to beyond DFT methods GPU & DCU & HPC GPU and DCU and HPC related any issues Refactor Refactor ABACUS codes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants