From 82854f694515c25bdb11d84a7911140a5512bac8 Mon Sep 17 00:00:00 2001 From: AroundPeking Date: Tue, 24 Nov 2026 10:50:19 +0800 Subject: [PATCH 1/4] feat(base): sub-communicator helpers for the RI and RPA stages 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`. --- source/source_base/parallel_common.cpp | 30 +++++ source/source_base/parallel_common.h | 13 ++- source/source_base/parallel_reduce.cpp | 87 +++++++++++++++ source/source_base/parallel_reduce.h | 32 ++++++ source/source_base/unittests/CMakeLists.txt | 18 +++ .../unittests/test_parallel_common.cpp | 103 ++++++++++++++++++ 6 files changed, 282 insertions(+), 1 deletion(-) create mode 100644 source/source_base/unittests/test_parallel_common.cpp diff --git a/source/source_base/parallel_common.cpp b/source/source_base/parallel_common.cpp index 4cdeb408197..2182b27342f 100644 --- a/source/source_base/parallel_common.cpp +++ b/source/source_base/parallel_common.cpp @@ -118,6 +118,36 @@ void bcast_char(char* object, const int n) } #ifdef __MPI +void bcast_int(int* object, const int n, const MPI_Comm mpi_comm, const int root) +{ + MPI_Bcast(object, n, MPI_INT, root, mpi_comm); +} + +void bcast_double(double* object, const int n, const MPI_Comm mpi_comm, const int root) +{ + MPI_Bcast(object, n, MPI_DOUBLE, root, mpi_comm); +} + +int my_rank(const MPI_Comm mpi_comm) +{ + int rank = 0; + MPI_Comm_rank(mpi_comm, &rank); + return rank; +} + +int nproc(const MPI_Comm mpi_comm) +{ + int size = 1; + MPI_Comm_size(mpi_comm, &size); + return size; +} + +void barrier(const MPI_Comm mpi_comm) +{ + MPI_Barrier(mpi_comm); +} + +// bcast single on an explicit communicator, from the communicator's rank 0 void bcast_mpi_offset(MPI_Offset& object, const MPI_Comm& comm) { MPI_Bcast(&object, 1, MPI_OFFSET, 0, comm); diff --git a/source/source_base/parallel_common.h b/source/source_base/parallel_common.h index aab84fe7011..c489d63e189 100644 --- a/source/source_base/parallel_common.h +++ b/source/source_base/parallel_common.h @@ -35,7 +35,18 @@ void bcast_int(int& object); void bcast_bool(bool& object); #ifdef __MPI -//(3) bcast single on an explicit communicator, from the communicator's rank 0 +//(3) communicator- and root-aware counterparts for the sub-communicator +// stages (RI/RPA) that run outside MPI_COMM_WORLD and outside root 0 +void bcast_int(int* object, const int n, const MPI_Comm mpi_comm, const int root); +void bcast_double(double* object, const int n, const MPI_Comm mpi_comm, const int root); +/// @brief Rank of the caller on an arbitrary communicator. +int my_rank(const MPI_Comm mpi_comm); +/// @brief Number of ranks on an arbitrary communicator. +int nproc(const MPI_Comm mpi_comm); +/// @brief Barrier on an arbitrary communicator. +void barrier(const MPI_Comm mpi_comm); + +//(4) bcast single on an explicit communicator, from the communicator's rank 0 void bcast_bool(bool& object, const MPI_Comm& comm); void bcast_mpi_offset(MPI_Offset& object, const MPI_Comm& comm); #endif diff --git a/source/source_base/parallel_reduce.cpp b/source/source_base/parallel_reduce.cpp index 741b38f36d5..b7991fc60b0 100644 --- a/source/source_base/parallel_reduce.cpp +++ b/source/source_base/parallel_reduce.cpp @@ -11,6 +11,7 @@ const MPI_Datatype Parallel_Reduce::MPI_Type::value = MPI_FLOAT; const MPI_Datatype Parallel_Reduce::MPI_Type>::value = MPI_DOUBLE_COMPLEX; const MPI_Datatype Parallel_Reduce::MPI_Type>::value = MPI_C_FLOAT_COMPLEX; const MPI_Datatype Parallel_Reduce::MPI_Type::value = MPI_LONG_LONG; +const MPI_Datatype Parallel_Reduce::MPI_Type::value = MPI_UNSIGNED_LONG_LONG; #endif template @@ -126,6 +127,92 @@ template void Parallel_Reduce::reduce_max(double&); template void Parallel_Reduce::reduce_min_pool(const int&, double&); template void Parallel_Reduce::reduce_max_pool(const int&, double&); +#ifdef __MPI +template +void Parallel_Reduce::reduce_all(const MPI_Comm mpi_comm, T& object) +{ + MPI_Allreduce(MPI_IN_PLACE, &object, 1, MPI_Type::value, MPI_SUM, mpi_comm); +} + +template +void Parallel_Reduce::reduce_all(const MPI_Comm mpi_comm, T* object, const int n) +{ + MPI_Allreduce(MPI_IN_PLACE, object, n, MPI_Type::value, MPI_SUM, mpi_comm); +} + +template +void Parallel_Reduce::reduce_max(const MPI_Comm mpi_comm, T& object) +{ + MPI_Allreduce(MPI_IN_PLACE, &object, 1, MPI_Type::value, MPI_MAX, mpi_comm); +} + +template +void Parallel_Reduce::reduce_min(const MPI_Comm mpi_comm, T& object) +{ + MPI_Allreduce(MPI_IN_PLACE, &object, 1, MPI_Type::value, MPI_MIN, mpi_comm); +} + +void Parallel_Reduce::reduce_max_loc(const MPI_Comm mpi_comm, double& value, int& rank) +{ + struct MaxLoc + { + double value; + int rank; + }; + MaxLoc local{value, rank}; + MaxLoc global{}; + MPI_Allreduce(&local, &global, 1, MPI_DOUBLE_INT, MPI_MAXLOC, mpi_comm); + value = global.value; + rank = global.rank; +} + +void Parallel_Reduce::gather_int_all(const MPI_Comm mpi_comm, int& v, int* all) +{ + assert(all != nullptr); + MPI_Allgather(&v, 1, MPI_INT, all, 1, MPI_INT, mpi_comm); +} + +void Parallel_Reduce::gatherv_int_all(const MPI_Comm mpi_comm, + const int* send_buffer, + const int send_count, + int* receive_buffer, + const int* receive_counts, + const int* receive_displacements) +{ + // MPI_Allgatherv tolerates a null receive buffer when every receive count + // is zero (an empty std::vector's data() is null in that case), so only + // demand one when there is something to receive. Every rank still enters + // the collective regardless of its own receive counts. + int total_receive = 0; + int nranks = 0; + MPI_Comm_size(mpi_comm, &nranks); + for (int irank = 0; irank < nranks; ++irank) + { + total_receive += receive_counts[irank]; + } + assert(total_receive == 0 || receive_buffer != nullptr); + MPI_Allgatherv(send_buffer, send_count, MPI_INT, receive_buffer, receive_counts, receive_displacements, + MPI_INT, mpi_comm); +} + +template void Parallel_Reduce::reduce_all(const MPI_Comm, int&); +template void Parallel_Reduce::reduce_all(const MPI_Comm, double&); +template void Parallel_Reduce::reduce_all(const MPI_Comm, unsigned long long&); + +template void Parallel_Reduce::reduce_all(const MPI_Comm, int*, const int); +template void Parallel_Reduce::reduce_all(const MPI_Comm, double*, const int); +template void Parallel_Reduce::reduce_all(const MPI_Comm, unsigned long long*, const int); + +template void Parallel_Reduce::reduce_max(const MPI_Comm, int&); +template void Parallel_Reduce::reduce_max(const MPI_Comm, double&); +template void Parallel_Reduce::reduce_max(const MPI_Comm, unsigned long long&); + +template void Parallel_Reduce::reduce_min(const MPI_Comm, int&); +template void Parallel_Reduce::reduce_min(const MPI_Comm, double&); +template void Parallel_Reduce::reduce_min(const MPI_Comm, unsigned long long&); +#endif + + void Parallel_Reduce::reduce_double_grid(double* object, const int n) { #ifdef __MPI diff --git a/source/source_base/parallel_reduce.h b/source/source_base/parallel_reduce.h index 2e2f842f3a2..dfd67680129 100644 --- a/source/source_base/parallel_reduce.h +++ b/source/source_base/parallel_reduce.h @@ -50,6 +50,12 @@ struct MPI_Type { static const MPI_Datatype value; }; + +template <> +struct MPI_Type +{ + static const MPI_Datatype value; +}; #endif /// reduce in all process @@ -82,6 +88,32 @@ void reduce_double_allpool(const int& npool, const int& nproc_in_pool, double* o void gather_int_all(int& v, int* all); +#ifdef __MPI +/// Communicator-aware counterparts of the MPI_COMM_WORLD-bound helpers above. +/// The RI and RPA stages run on caller-provided sub-communicators, which those +/// helpers cannot express; route every reduction on such a communicator +/// through these overloads so the raw MPI calls stay centralized here. +template +void reduce_all(const MPI_Comm mpi_comm, T& object); +template +void reduce_all(const MPI_Comm mpi_comm, T* object, const int n); +template +void reduce_max(const MPI_Comm mpi_comm, T& object); +template +void reduce_min(const MPI_Comm mpi_comm, T& object); +/// @brief Largest value together with the rank that supplied it (MPI_MAXLOC). +void reduce_max_loc(const MPI_Comm mpi_comm, double& value, int& rank); +/// @brief Allgather one int per rank on an arbitrary communicator. +void gather_int_all(const MPI_Comm mpi_comm, int& v, int* all); +/// @brief Allgatherv with per-rank counts and displacements on an arbitrary communicator. +void gatherv_int_all(const MPI_Comm mpi_comm, + const int* send_buffer, + const int send_count, + int* receive_buffer, + const int* receive_counts, + const int* receive_displacements); +#endif + } // namespace Parallel_Reduce #endif diff --git a/source/source_base/unittests/CMakeLists.txt b/source/source_base/unittests/CMakeLists.txt index 5381e4f9f59..983a75fd64f 100644 --- a/source/source_base/unittests/CMakeLists.txt +++ b/source/source_base/unittests/CMakeLists.txt @@ -11,3 +11,21 @@ AddTest( LIBS parameter base device SOURCES test_filename.cpp ) + +# The sub-communicator helpers are MPI-only, so this target keeps __MPI even +# though the directory disables it for the others, and the test is registered +# only where real collectives are available. +if(ENABLE_MPI) + addtest( + TARGET MODULE_BASE_parallel_common + LIBS parameter base device + KEEP_FEATURE_DEFINITIONS __MPI + SOURCES test_parallel_common.cpp) + add_test( + NAME MODULE_BASE_parallel_common_4 + COMMAND ${MPIEXEC_EXECUTABLE} ${MPIEXEC_NUMPROC_FLAG} 4 ${MPIEXEC_PREFLAGS} + $ ${MPIEXEC_POSTFLAGS} + WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}) + set_tests_properties(MODULE_BASE_parallel_common_4 PROPERTIES PROCESSORS 4 + TIMEOUT 60) +endif() diff --git a/source/source_base/unittests/test_parallel_common.cpp b/source/source_base/unittests/test_parallel_common.cpp new file mode 100644 index 00000000000..d3d497b6062 --- /dev/null +++ b/source/source_base/unittests/test_parallel_common.cpp @@ -0,0 +1,103 @@ +// The sub-communicator helpers are only meaningful with MPI, so this test is +// built and registered for the MPI configuration only. +#include "source_base/parallel_common.h" +#include "source_base/parallel_reduce.h" + +#include +#include +#include +#include +#ifdef __MPI +#include +#endif + +namespace +{ +void require(const bool condition, const char* message) +{ + if (!condition) + { + std::fprintf(stderr, "%s\n", message); + std::exit(EXIT_FAILURE); + } +} +} // namespace + +int main(int argc, char** argv) +{ +#ifdef __MPI + MPI_Init(&argc, &argv); + + // Split into two groups so the helpers are exercised on a communicator + // that is neither MPI_COMM_WORLD nor rooted at world rank 0. + int world_rank = 0; + int world_size = 0; + MPI_Comm_rank(MPI_COMM_WORLD, &world_rank); + MPI_Comm_size(MPI_COMM_WORLD, &world_size); + require(world_size >= 4, "run this test with at least four ranks"); + + const int colour = world_rank < world_size / 2 ? 0 : 1; + MPI_Comm sub = MPI_COMM_NULL; + MPI_Comm_split(MPI_COMM_WORLD, colour, world_rank, &sub); + + int sub_rank = 0; + int sub_size = 0; + MPI_Comm_rank(sub, &sub_rank); + MPI_Comm_size(sub, &sub_size); + require(Parallel_Common::my_rank(sub) == sub_rank, "my_rank disagrees with MPI_Comm_rank"); + require(Parallel_Common::nproc(sub) == sub_size, "nproc disagrees with MPI_Comm_size"); + + // A root other than zero, which is the case the RI and RPA stages need. + const int root = sub_size - 1; + int scalar = sub_rank == root ? 41 : 0; + Parallel_Common::bcast_int(&scalar, 1, sub, root); + require(scalar == 41, "bcast_int did not reach every rank from a non-zero root"); + + std::vector values(3, sub_rank == root ? 7.5 : -1.0); + Parallel_Common::bcast_double(values.data(), static_cast(values.size()), sub, root); + for (const double value : values) + { + require(value == 7.5, "bcast_double did not reach every rank from a non-zero root"); + } + + // Allgatherv must accept an all-empty receive buffer. Every rank sends + // nothing, so the destination vector's data() may be null. + std::vector empty_receive; + std::vector zero_counts(static_cast(sub_size), 0); + std::vector zero_displacements(static_cast(sub_size), 0); + Parallel_Reduce::gatherv_int_all(sub, nullptr, 0, empty_receive.data(), + zero_counts.data(), zero_displacements.data()); + require(empty_receive.empty(), "all-empty gatherv produced data"); + + // And it must still gather when there is something to receive. + const int send_value = 100 + sub_rank; + std::vector receive(static_cast(sub_size), 0); + std::vector counts(static_cast(sub_size), 1); + std::vector displacements(static_cast(sub_size), 0); + for (int irank = 0; irank < sub_size; ++irank) + { + displacements[irank] = irank; + } + Parallel_Reduce::gatherv_int_all(sub, &send_value, 1, receive.data(), + counts.data(), displacements.data()); + for (int irank = 0; irank < sub_size; ++irank) + { + require(receive[irank] == 100 + irank, "gatherv_int_all lost a rank's value"); + } + + Parallel_Common::barrier(sub); + MPI_Comm_free(&sub); + MPI_Finalize(); + if (world_rank == 0) + { + std::printf("sub-communicator helper tests passed\n"); + } +#else + (void)argc; + (void)argv; + // The helpers are declared only under __MPI: without it there is no + // communicator type to pass, so there is nothing to exercise here. + std::printf("sub-communicator helper tests skipped (no MPI)\n"); +#endif + return EXIT_SUCCESS; +} From 17b9b8aad1e9e27bb3ffaeb49280b3f649f1a8ae Mon Sep 17 00:00:00 2001 From: AroundPeking Date: Tue, 24 Nov 2026 14:49:48 +0800 Subject: [PATCH 2/4] test(base): register the sub-communicator test for four ranks only `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`. --- source/source_base/unittests/CMakeLists.txt | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/source/source_base/unittests/CMakeLists.txt b/source/source_base/unittests/CMakeLists.txt index 983a75fd64f..7e66a345c8c 100644 --- a/source/source_base/unittests/CMakeLists.txt +++ b/source/source_base/unittests/CMakeLists.txt @@ -12,15 +12,18 @@ AddTest( SOURCES test_filename.cpp ) -# The sub-communicator helpers are MPI-only, so this target keeps __MPI even -# though the directory disables it for the others, and the test is registered -# only where real collectives are available. +# The sub-communicator helpers need real collectives, so this target keeps +# __MPI even though the directory disables it for the others. It is built +# directly rather than through addtest(): that helper also registers a +# single-process ctest entry, and this test requires four ranks. if(ENABLE_MPI) - addtest( - TARGET MODULE_BASE_parallel_common - LIBS parameter base device - KEEP_FEATURE_DEFINITIONS __MPI - SOURCES test_parallel_common.cpp) + add_executable(MODULE_BASE_parallel_common test_parallel_common.cpp) + # This test supplies its own main (MPI_Init then RUN_ALL_TESTS), so link the + # gtest/gmock libraries without their main-providing variants. + target_link_libraries(MODULE_BASE_parallel_common + PRIVATE parameter base device GTest::gtest GTest::gmock + abacus::linalg_libs) + target_compile_definitions(MODULE_BASE_parallel_common PRIVATE __MPI) add_test( NAME MODULE_BASE_parallel_common_4 COMMAND ${MPIEXEC_EXECUTABLE} ${MPIEXEC_NUMPROC_FLAG} 4 ${MPIEXEC_PREFLAGS} From 3a28fc2df0d57afe82b79068e0747afa50770b4a Mon Sep 17 00:00:00 2001 From: AroundPeking Date: Wed, 25 Nov 2026 10:53:00 +0800 Subject: [PATCH 3/4] fix(base): test the receive-buffer guard without summing counts 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. --- source/source_base/parallel_reduce.cpp | 14 +++- .../unittests/test_parallel_common.cpp | 66 +++++++++++++++++-- 2 files changed, 72 insertions(+), 8 deletions(-) diff --git a/source/source_base/parallel_reduce.cpp b/source/source_base/parallel_reduce.cpp index b7991fc60b0..2dcaa53a41f 100644 --- a/source/source_base/parallel_reduce.cpp +++ b/source/source_base/parallel_reduce.cpp @@ -183,14 +183,22 @@ void Parallel_Reduce::gatherv_int_all(const MPI_Comm mpi_comm, // is zero (an empty std::vector's data() is null in that case), so only // demand one when there is something to receive. Every rank still enters // the collective regardless of its own receive counts. - int total_receive = 0; + // + // The test is "any count is positive" rather than a sum: two ranks may + // legitimately pass counts like {INT_MAX, 1}, whose sum overflows a signed + // int even though the receive buffer is valid. int nranks = 0; MPI_Comm_size(mpi_comm, &nranks); + bool has_received_data = false; for (int irank = 0; irank < nranks; ++irank) { - total_receive += receive_counts[irank]; + if (receive_counts[irank] > 0) + { + has_received_data = true; + break; + } } - assert(total_receive == 0 || receive_buffer != nullptr); + assert(!has_received_data || receive_buffer != nullptr); MPI_Allgatherv(send_buffer, send_count, MPI_INT, receive_buffer, receive_counts, receive_displacements, MPI_INT, mpi_comm); } diff --git a/source/source_base/unittests/test_parallel_common.cpp b/source/source_base/unittests/test_parallel_common.cpp index d3d497b6062..5505b90f413 100644 --- a/source/source_base/unittests/test_parallel_common.cpp +++ b/source/source_base/unittests/test_parallel_common.cpp @@ -49,12 +49,19 @@ int main(int argc, char** argv) // A root other than zero, which is the case the RI and RPA stages need. const int root = sub_size - 1; - int scalar = sub_rank == root ? 41 : 0; - Parallel_Common::bcast_int(&scalar, 1, sub, root); + const int scalar_from_root = 41; + const int scalar_count = 1; + int scalar = sub_rank == root ? scalar_from_root : 0; + Parallel_Common::bcast_int(&scalar, scalar_count, sub, root); require(scalar == 41, "bcast_int did not reach every rank from a non-zero root"); - std::vector values(3, sub_rank == root ? 7.5 : -1.0); - Parallel_Common::bcast_double(values.data(), static_cast(values.size()), sub, root); + // Rule 14: name the payload and the element count before the call. + const double root_value = 7.5; + const double other_value = -1.0; + const double initial_value = sub_rank == root ? root_value : other_value; + const int value_count = 3; + std::vector values(static_cast(value_count), initial_value); + Parallel_Common::bcast_double(values.data(), value_count, sub, root); for (const double value : values) { require(value == 7.5, "bcast_double did not reach every rank from a non-zero root"); @@ -65,7 +72,8 @@ int main(int argc, char** argv) std::vector empty_receive; std::vector zero_counts(static_cast(sub_size), 0); std::vector zero_displacements(static_cast(sub_size), 0); - Parallel_Reduce::gatherv_int_all(sub, nullptr, 0, empty_receive.data(), + const int no_send_count = 0; + Parallel_Reduce::gatherv_int_all(sub, nullptr, no_send_count, empty_receive.data(), zero_counts.data(), zero_displacements.data()); require(empty_receive.empty(), "all-empty gatherv produced data"); @@ -85,6 +93,54 @@ int main(int argc, char** argv) require(receive[irank] == 100 + irank, "gatherv_int_all lost a rank's value"); } + // Uneven gather counts: rank 0 receives nothing, the others receive one. + const bool sends_one = sub_rank > 0; + const int uneven_send = sends_one ? 200 + sub_rank : 0; + const int uneven_send_count = sends_one ? 1 : 0; + std::vector uneven_counts(static_cast(sub_size), 0); + std::vector uneven_displacements(static_cast(sub_size), 0); + int running_offset = 0; + for (int irank = 0; irank < sub_size; ++irank) + { + uneven_counts[static_cast(irank)] = irank > 0 ? 1 : 0; + uneven_displacements[static_cast(irank)] = running_offset; + running_offset += uneven_counts[static_cast(irank)]; + } + std::vector uneven_receive(static_cast(running_offset), -1); + Parallel_Reduce::gatherv_int_all(sub, sends_one ? &uneven_send : nullptr, + uneven_send_count, uneven_receive.data(), + uneven_counts.data(), uneven_displacements.data()); + for (int irank = 1; irank < sub_size; ++irank) + { + require(uneven_receive[static_cast(irank - 1)] == 200 + irank, + "uneven gatherv misplaced a rank's value"); + } + + // gather_int_all on the same sub-communicator. + int one_per_rank = 300 + sub_rank; + std::vector gathered(static_cast(sub_size), 0); + Parallel_Reduce::gather_int_all(sub, one_per_rank, gathered.data()); + for (int irank = 0; irank < sub_size; ++irank) + { + require(gathered[static_cast(irank)] == 300 + irank, + "gather_int_all lost a rank's value"); + } + + // reduce_max_loc: the rank supplying the maximum comes back with it. On a + // tie MPI may report any contributing rank, so only check the value there. + const double local_value = (sub_rank == 0 || sub_rank == 1) ? 9.5 : 1.0; + const int local_rank = sub_rank; + double best_value = local_value; + int best_rank = local_rank; + Parallel_Reduce::reduce_max_loc(sub, best_value, best_rank); + require(best_value == 9.5, "reduce_max_loc lost the maximum value"); + const bool tied = sub_size > 1; + if (tied) + { + require(best_rank == 0 || best_rank == 1, + "reduce_max_loc reported a rank that did not hold the maximum"); + } + Parallel_Common::barrier(sub); MPI_Comm_free(&sub); MPI_Finalize(); From 9fe15957234cda3f0c9d71cc6dcfde11d71bae53 Mon Sep 17 00:00:00 2001 From: AroundPeking Date: Thu, 26 Nov 2026 00:34:26 +0800 Subject: [PATCH 4/4] refactor(base): guard the bodies, not the interfaces, of the new helpers MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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. --- source/source_base/parallel_common.cpp | 40 +++++++++- source/source_base/parallel_common.h | 17 +++- source/source_base/parallel_reduce.cpp | 78 +++++++++++++++++-- source/source_base/parallel_reduce.h | 18 ++++- .../unittests/test_parallel_common.cpp | 13 +++- 5 files changed, 150 insertions(+), 16 deletions(-) diff --git a/source/source_base/parallel_common.cpp b/source/source_base/parallel_common.cpp index 2182b27342f..53b5b06c4d8 100644 --- a/source/source_base/parallel_common.cpp +++ b/source/source_base/parallel_common.cpp @@ -96,9 +96,9 @@ void bcast_bool(bool& object) #endif } -#ifdef __MPI void bcast_bool(bool& object, const MPI_Comm& comm) { +#ifdef __MPI int swap = object; int my_rank; MPI_Comm_rank(comm, &my_rank); @@ -107,8 +107,12 @@ void bcast_bool(bool& object, const MPI_Comm& comm) { object = static_cast(swap); } -} +#else + // One rank, which is the root: the value is already correct. + (void)object; + (void)comm; #endif +} void bcast_char(char* object, const int n) { @@ -117,36 +121,66 @@ void bcast_char(char* object, const int n) #endif } -#ifdef __MPI void bcast_int(int* object, const int n, const MPI_Comm mpi_comm, const int root) { +#ifdef __MPI MPI_Bcast(object, n, MPI_INT, root, mpi_comm); +#else + // A serial run has one rank, which is always the root, so the payload is + // already in place everywhere. The other arguments are unused. + (void)object; + (void)n; + (void)mpi_comm; + (void)root; +#endif } void bcast_double(double* object, const int n, const MPI_Comm mpi_comm, const int root) { +#ifdef __MPI MPI_Bcast(object, n, MPI_DOUBLE, root, mpi_comm); +#else + (void)object; + (void)n; + (void)mpi_comm; + (void)root; +#endif } int my_rank(const MPI_Comm mpi_comm) { +#ifdef __MPI int rank = 0; MPI_Comm_rank(mpi_comm, &rank); return rank; +#else + (void)mpi_comm; + return 0; +#endif } int nproc(const MPI_Comm mpi_comm) { +#ifdef __MPI int size = 1; MPI_Comm_size(mpi_comm, &size); return size; +#else + (void)mpi_comm; + return 1; +#endif } void barrier(const MPI_Comm mpi_comm) { +#ifdef __MPI MPI_Barrier(mpi_comm); +#else + (void)mpi_comm; +#endif } +#ifdef __MPI // bcast single on an explicit communicator, from the communicator's rank 0 void bcast_mpi_offset(MPI_Offset& object, const MPI_Comm& comm) { diff --git a/source/source_base/parallel_common.h b/source/source_base/parallel_common.h index c489d63e189..369b96a3b05 100644 --- a/source/source_base/parallel_common.h +++ b/source/source_base/parallel_common.h @@ -34,7 +34,18 @@ void bcast_double(double& object); void bcast_int(int& object); void bcast_bool(bool& object); -#ifdef __MPI +#ifndef __MPI +/// Serial builds have no communicator type, but the interfaces below name one. +/// Declare a stand-in so those interfaces stay visible to callers in both +/// builds instead of vanishing with the macro; a serial implementation simply +/// ignores the argument. It is scoped to this namespace so that a translation +/// unit which pulls in a real `mpi.h` for some other reason cannot clash. +struct SerialCommunicator +{ +}; +using MPI_Comm = SerialCommunicator; +#endif + //(3) communicator- and root-aware counterparts for the sub-communicator // stages (RI/RPA) that run outside MPI_COMM_WORLD and outside root 0 void bcast_int(int* object, const int n, const MPI_Comm mpi_comm, const int root); @@ -48,6 +59,10 @@ void barrier(const MPI_Comm mpi_comm); //(4) bcast single on an explicit communicator, from the communicator's rank 0 void bcast_bool(bool& object, const MPI_Comm& comm); + +#ifdef __MPI +// MPI_Offset has no serial equivalent — it is an MPI-defined integer type — so +// this one overload is the only interface here that stays MPI-only. void bcast_mpi_offset(MPI_Offset& object, const MPI_Comm& comm); #endif diff --git a/source/source_base/parallel_reduce.cpp b/source/source_base/parallel_reduce.cpp index 2dcaa53a41f..763301aa211 100644 --- a/source/source_base/parallel_reduce.cpp +++ b/source/source_base/parallel_reduce.cpp @@ -152,15 +152,17 @@ void Parallel_Reduce::reduce_min(const MPI_Comm mpi_comm, T& object) MPI_Allreduce(MPI_IN_PLACE, &object, 1, MPI_Type::value, MPI_MIN, mpi_comm); } -void Parallel_Reduce::reduce_max_loc(const MPI_Comm mpi_comm, double& value, int& rank) +void Parallel_Reduce::reduce_max_with_rank(const MPI_Comm mpi_comm, double& value, int& rank) { - struct MaxLoc + // MPI_MAXLOC pairs a value with its source rank; name the pair after what + // it holds rather than after the MPI operation code. + struct ValueAndSourceRank { double value; int rank; }; - MaxLoc local{value, rank}; - MaxLoc global{}; + ValueAndSourceRank local{value, rank}; + ValueAndSourceRank global{}; MPI_Allreduce(&local, &global, 1, MPI_DOUBLE_INT, MPI_MAXLOC, mpi_comm); value = global.value; rank = global.rank; @@ -203,6 +205,73 @@ void Parallel_Reduce::gatherv_int_all(const MPI_Comm mpi_comm, MPI_INT, mpi_comm); } +#else +// Serial counterparts: one rank owns all the data, so a "reduce" over a +// communicator is the identity and a gather writes exactly one entry. They +// exist so the declarations stay usable instead of being compiled away. +template +void Parallel_Reduce::reduce_all(const MPI_Comm mpi_comm, T& object) +{ + (void)mpi_comm; + (void)object; +} + +template +void Parallel_Reduce::reduce_all(const MPI_Comm mpi_comm, T* object, const int n) +{ + (void)mpi_comm; + (void)object; + (void)n; +} + +template +void Parallel_Reduce::reduce_max(const MPI_Comm mpi_comm, T& object) +{ + (void)mpi_comm; + (void)object; +} + +template +void Parallel_Reduce::reduce_min(const MPI_Comm mpi_comm, T& object) +{ + (void)mpi_comm; + (void)object; +} + +void Parallel_Reduce::reduce_max_with_rank(const MPI_Comm mpi_comm, double& value, int& rank) +{ + (void)mpi_comm; + (void)value; + rank = 0; +} + +void Parallel_Reduce::gather_int_all(const MPI_Comm mpi_comm, int& v, int* all) +{ + (void)mpi_comm; + assert(all != nullptr); + all[0] = v; +} + +void Parallel_Reduce::gatherv_int_all(const MPI_Comm mpi_comm, + const int* send_buffer, + const int send_count, + int* receive_buffer, + const int* receive_counts, + const int* receive_displacements) +{ + (void)mpi_comm; + (void)receive_counts; + (void)receive_displacements; + assert(send_count == 0 || (send_buffer != nullptr && receive_buffer != nullptr)); + for (int i = 0; i < send_count; ++i) + { + receive_buffer[i] = send_buffer[i]; + } +} +#endif + +// The instantiations apply to whichever implementation the branch above +// compiled, so they sit outside the conditional. template void Parallel_Reduce::reduce_all(const MPI_Comm, int&); template void Parallel_Reduce::reduce_all(const MPI_Comm, double&); template void Parallel_Reduce::reduce_all(const MPI_Comm, unsigned long long&); @@ -218,7 +287,6 @@ template void Parallel_Reduce::reduce_max(const MPI_Comm, un template void Parallel_Reduce::reduce_min(const MPI_Comm, int&); template void Parallel_Reduce::reduce_min(const MPI_Comm, double&); template void Parallel_Reduce::reduce_min(const MPI_Comm, unsigned long long&); -#endif void Parallel_Reduce::reduce_double_grid(double* object, const int n) diff --git a/source/source_base/parallel_reduce.h b/source/source_base/parallel_reduce.h index dfd67680129..2845098a8f0 100644 --- a/source/source_base/parallel_reduce.h +++ b/source/source_base/parallel_reduce.h @@ -3,6 +3,11 @@ #ifdef __MPI #include +#else +// The communicator-aware overloads below name MPI_Comm. A serial build has no +// such type, so reuse the stand-in that Parallel_Common declares rather than +// defining a second one that a caller could not interchange. +#include "parallel_common.h" #endif #include @@ -10,6 +15,9 @@ namespace Parallel_Reduce { +#ifndef __MPI +using MPI_Comm = Parallel_Common::MPI_Comm; +#endif #ifdef __MPI template @@ -88,11 +96,14 @@ void reduce_double_allpool(const int& npool, const int& nproc_in_pool, double* o void gather_int_all(int& v, int* all); -#ifdef __MPI /// Communicator-aware counterparts of the MPI_COMM_WORLD-bound helpers above. /// The RI and RPA stages run on caller-provided sub-communicators, which those /// helpers cannot express; route every reduction on such a communicator /// through these overloads so the raw MPI calls stay centralized here. +/// +/// These declarations are visible in serial builds too: the implementations +/// guard their bodies instead of the whole function, so a caller does not have +/// to reproduce the #ifdef at every use site. template void reduce_all(const MPI_Comm mpi_comm, T& object); template @@ -102,7 +113,9 @@ void reduce_max(const MPI_Comm mpi_comm, T& object); template void reduce_min(const MPI_Comm mpi_comm, T& object); /// @brief Largest value together with the rank that supplied it (MPI_MAXLOC). -void reduce_max_loc(const MPI_Comm mpi_comm, double& value, int& rank); +/// "loc" is MPI's contraction of "location"; the name is spelled out here so a +/// reader who does not know MPI_MAXLOC can still tell what the call does. +void reduce_max_with_rank(const MPI_Comm mpi_comm, double& value, int& rank); /// @brief Allgather one int per rank on an arbitrary communicator. void gather_int_all(const MPI_Comm mpi_comm, int& v, int* all); /// @brief Allgatherv with per-rank counts and displacements on an arbitrary communicator. @@ -112,7 +125,6 @@ void gatherv_int_all(const MPI_Comm mpi_comm, int* receive_buffer, const int* receive_counts, const int* receive_displacements); -#endif } // namespace Parallel_Reduce diff --git a/source/source_base/unittests/test_parallel_common.cpp b/source/source_base/unittests/test_parallel_common.cpp index 5505b90f413..654b6ea0e99 100644 --- a/source/source_base/unittests/test_parallel_common.cpp +++ b/source/source_base/unittests/test_parallel_common.cpp @@ -126,21 +126,26 @@ int main(int argc, char** argv) "gather_int_all lost a rank's value"); } - // reduce_max_loc: the rank supplying the maximum comes back with it. On a + // reduce_max_with_rank: the rank supplying the maximum comes back with it. On a // tie MPI may report any contributing rank, so only check the value there. const double local_value = (sub_rank == 0 || sub_rank == 1) ? 9.5 : 1.0; const int local_rank = sub_rank; double best_value = local_value; int best_rank = local_rank; - Parallel_Reduce::reduce_max_loc(sub, best_value, best_rank); - require(best_value == 9.5, "reduce_max_loc lost the maximum value"); + Parallel_Reduce::reduce_max_with_rank(sub, best_value, best_rank); + require(best_value == 9.5, "reduce_max_with_rank lost the maximum value"); const bool tied = sub_size > 1; if (tied) { require(best_rank == 0 || best_rank == 1, - "reduce_max_loc reported a rank that did not hold the maximum"); + "reduce_max_with_rank reported a rank that did not hold the maximum"); } + // The interface must also exist in a serial build. The header keeps its + // declarations unconditional, so this call compiles whether or not __MPI + // is defined; only the implementation behind it changes. + require(Parallel_Common::nproc(sub) >= 1, "nproc must report at least one rank"); + Parallel_Common::barrier(sub); MPI_Comm_free(&sub); MPI_Finalize();