Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 66 additions & 2 deletions source/source_base/parallel_common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -107,8 +107,12 @@ void bcast_bool(bool& object, const MPI_Comm& comm)
{
object = static_cast<bool>(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)
{
Expand All @@ -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);
Comment thread
AroundPeking marked this conversation as resolved.
Resolved
#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);
Expand Down
30 changes: 28 additions & 2 deletions source/source_base/parallel_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Comment thread
AroundPeking marked this conversation as resolved.
Resolved
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

Expand Down
163 changes: 163 additions & 0 deletions source/source_base/parallel_reduce.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const MPI_Datatype Parallel_Reduce::MPI_Type<float>::value = MPI_FLOAT;
const MPI_Datatype Parallel_Reduce::MPI_Type<std::complex<double>>::value = MPI_DOUBLE_COMPLEX;
const MPI_Datatype Parallel_Reduce::MPI_Type<std::complex<float>>::value = MPI_C_FLOAT_COMPLEX;
const MPI_Datatype Parallel_Reduce::MPI_Type<long long>::value = MPI_LONG_LONG;
const MPI_Datatype Parallel_Reduce::MPI_Type<unsigned long long>::value = MPI_UNSIGNED_LONG_LONG;
#endif

template <typename T>
Expand Down Expand Up @@ -126,6 +127,168 @@ template void Parallel_Reduce::reduce_max<double>(double&);
template void Parallel_Reduce::reduce_min_pool<double>(const int&, double&);
template void Parallel_Reduce::reduce_max_pool<double>(const int&, double&);

#ifdef __MPI
Comment thread
AroundPeking marked this conversation as resolved.
Resolved
template <typename T>
void Parallel_Reduce::reduce_all(const MPI_Comm mpi_comm, T& object)
{
MPI_Allreduce(MPI_IN_PLACE, &object, 1, MPI_Type<T>::value, MPI_SUM, mpi_comm);
}

template <typename T>
void Parallel_Reduce::reduce_all(const MPI_Comm mpi_comm, T* object, const int n)
{
MPI_Allreduce(MPI_IN_PLACE, object, n, MPI_Type<T>::value, MPI_SUM, mpi_comm);
}

template <typename T>
void Parallel_Reduce::reduce_max(const MPI_Comm mpi_comm, T& object)
{
MPI_Allreduce(MPI_IN_PLACE, &object, 1, MPI_Type<T>::value, MPI_MAX, mpi_comm);
}

template <typename T>
void Parallel_Reduce::reduce_min(const MPI_Comm mpi_comm, T& object)
{
MPI_Allreduce(MPI_IN_PLACE, &object, 1, MPI_Type<T>::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 <typename T>
void Parallel_Reduce::reduce_all(const MPI_Comm mpi_comm, T& object)
{
(void)mpi_comm;
(void)object;
}

template <typename T>
void Parallel_Reduce::reduce_all(const MPI_Comm mpi_comm, T* object, const int n)
{
(void)mpi_comm;
(void)object;
(void)n;
}

template <typename T>
void Parallel_Reduce::reduce_max(const MPI_Comm mpi_comm, T& object)
{
(void)mpi_comm;
(void)object;
}

template <typename T>
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<int>(const MPI_Comm, int&);
Comment thread
AroundPeking marked this conversation as resolved.
Resolved
template void Parallel_Reduce::reduce_all<double>(const MPI_Comm, double&);
template void Parallel_Reduce::reduce_all<unsigned long long>(const MPI_Comm, unsigned long long&);

template void Parallel_Reduce::reduce_all<int>(const MPI_Comm, int*, const int);
template void Parallel_Reduce::reduce_all<double>(const MPI_Comm, double*, const int);
template void Parallel_Reduce::reduce_all<unsigned long long>(const MPI_Comm, unsigned long long*, const int);

template void Parallel_Reduce::reduce_max<int>(const MPI_Comm, int&);
template void Parallel_Reduce::reduce_max<double>(const MPI_Comm, double&);
template void Parallel_Reduce::reduce_max<unsigned long long>(const MPI_Comm, unsigned long long&);

template void Parallel_Reduce::reduce_min<int>(const MPI_Comm, int&);
template void Parallel_Reduce::reduce_min<double>(const MPI_Comm, double&);
template void Parallel_Reduce::reduce_min<unsigned long long>(const MPI_Comm, unsigned long long&);


void Parallel_Reduce::reduce_double_grid(double* object, const int n)
{
#ifdef __MPI
Expand Down
44 changes: 44 additions & 0 deletions source/source_base/parallel_reduce.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,21 @@

#ifdef __MPI
#include <mpi.h>
#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 <cassert>
#include <complex>

namespace Parallel_Reduce
{
#ifndef __MPI
using MPI_Comm = Parallel_Common::MPI_Comm;
#endif

#ifdef __MPI
template <typename T>
Expand Down Expand Up @@ -50,6 +58,12 @@ struct MPI_Type<long long>
{
static const MPI_Datatype value;
};

template <>
struct MPI_Type<unsigned long long>
{
static const MPI_Datatype value;
};
#endif

/// reduce in all process
Expand Down Expand Up @@ -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 <typename T>
void reduce_all(const MPI_Comm mpi_comm, T& object);
template <typename T>
void reduce_all(const MPI_Comm mpi_comm, T* object, const int n);
template <typename T>
void reduce_max(const MPI_Comm mpi_comm, T& object);
template <typename T>
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
Loading
Loading