diff --git a/source/source_base/parallel_common.cpp b/source/source_base/parallel_common.cpp index 4cdeb40819..53b5b06c4d 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,7 +121,67 @@ void bcast_char(char* object, const int n) #endif } +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) { 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 aab84fe701..369b96a3b0 100644 --- a/source/source_base/parallel_common.h +++ b/source/source_base/parallel_common.h @@ -34,9 +34,35 @@ void bcast_double(double& object); 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 +#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); +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); + +#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 741b38f36d..763301aa21 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,168 @@ 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_with_rank(const MPI_Comm mpi_comm, double& value, int& rank) +{ + // 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; + }; + ValueAndSourceRank local{value, rank}; + ValueAndSourceRank 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. + // + // 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) + { + if (receive_counts[irank] > 0) + { + has_received_data = true; + break; + } + } + 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); +} + +#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&); + +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&); + + 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 2e2f842f3a..2845098a8f 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 @@ -50,6 +58,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 +96,36 @@ void reduce_double_allpool(const int& npool, const int& nproc_in_pool, double* o void gather_int_all(int& v, int* all); +/// 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 +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). +/// "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. +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); + } // namespace Parallel_Reduce #endif diff --git a/source/source_base/unittests/CMakeLists.txt b/source/source_base/unittests/CMakeLists.txt index 5381e4f9f5..7e66a345c8 100644 --- a/source/source_base/unittests/CMakeLists.txt +++ b/source/source_base/unittests/CMakeLists.txt @@ -11,3 +11,24 @@ AddTest( LIBS parameter base device SOURCES test_filename.cpp ) + +# 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) + 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} + $ ${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 0000000000..654b6ea0e9 --- /dev/null +++ b/source/source_base/unittests/test_parallel_common.cpp @@ -0,0 +1,164 @@ +// 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; + 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"); + + // 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"); + } + + // 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); + 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"); + + // 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"); + } + + // 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_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_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_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(); + 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; +}