OGS
BaseLib::MPI Namespace Reference

Classes

struct  Mpi
 
struct  Setup
 

Functions

template<typename T >
constexpr MPI_Datatype mpiType ()
 
template<typename T >
static std::vector< T > allgather (T const &value, Mpi const &mpi)
 
template<typename T >
static std::vector< T > allgather (std::vector< T > const &vector, Mpi const &mpi)
 
template<typename T >
static T allreduce (T const &value, MPI_Op const &mpi_op, Mpi const &mpi)
 
template<typename T >
static std::vector< T > allreduce (std::vector< T > const &vector, MPI_Op const &mpi_op, Mpi const &mpi)
 
template<typename T >
static void allreduceInplace (std::vector< T > &vector, MPI_Op const &mpi_op, Mpi const &mpi)
 
template<typename T >
static std::vector< int > allgatherv (std::span< T > const send_buffer, std::vector< std::remove_const_t< T > > &receive_buffer, Mpi const &mpi)
 
static int reduceMin (int const val)
 

Function Documentation

◆ allgather() [1/2]

template<typename T >
static std::vector< T > BaseLib::MPI::allgather ( std::vector< T > const & vector,
Mpi const & mpi )
static

Definition at line 113 of file MPI.h.

114{
115 std::size_t const size = vector.size();
116 // Flat in memory over all ranks;
117 std::vector<T> result(mpi.size * size);
118
119 std::copy_n(vector.begin(), size, &result[mpi.rank * size]);
120
121 MPI_Allgather(&result[mpi.rank * size], size, mpiType<T>(), result.data(),
122 size, mpiType<T>(), mpi.communicator);
123
124 return result;
125}

References BaseLib::MPI::Mpi::communicator, mpiType(), BaseLib::MPI::Mpi::rank, and BaseLib::MPI::Mpi::size.

◆ allgather() [2/2]

template<typename T >
static std::vector< T > BaseLib::MPI::allgather ( T const & value,
Mpi const & mpi )
static

Definition at line 100 of file MPI.h.

101{
102 std::vector<T> result(mpi.size);
103
104 result[mpi.rank] = value;
105
106 MPI_Allgather(&result[mpi.rank], 1, mpiType<T>(), result.data(), 1,
107 mpiType<T>(), mpi.communicator);
108
109 return result;
110}

References BaseLib::MPI::Mpi::communicator, mpiType(), BaseLib::MPI::Mpi::rank, and BaseLib::MPI::Mpi::size.

Referenced by allgatherv(), MeshLib::computeNumberOfRegularBaseNodesAtRank(), MeshLib::computeNumberOfRegularHigherOrderNodesAtRank(), MeshLib::computeRegularBaseNodeGlobalNodeIDsOfSubDomainPartition(), MeshLib::IO::getPartitionInfo(), and MeshLib::IO::NodePartitionedMeshReader::newMesh().

◆ allgatherv()

template<typename T >
static std::vector< int > BaseLib::MPI::allgatherv ( std::span< T > const send_buffer,
std::vector< std::remove_const_t< T > > & receive_buffer,
Mpi const & mpi )
static

Allgather for variable data. Returns offsets in the receive buffer. The receive buffer is resized to accommodate the gathered data.

Definition at line 164 of file MPI.h.

168{
169 // Determine the number of elements to send
170 int const size = static_cast<int>(send_buffer.size());
171
172 // Gather sizes from all ranks
173 std::vector<int> const sizes = allgather(size, mpi);
174
175 // Compute offsets based on counts
176 std::vector<int> const offsets = BaseLib::sizesToOffsets(sizes);
177
178 // Resize receive buffer to hold all gathered data
179 receive_buffer.resize(offsets.back());
180
181 MPI_Allgatherv(send_buffer.data(), size, mpiType<T>(),
182 receive_buffer.data(), sizes.data(), offsets.data(),
183 mpiType<T>(), mpi.communicator);
184
185 return offsets;
186}
static std::vector< T > allgather(T const &value, Mpi const &mpi)
Definition MPI.h:100
std::vector< ranges::range_value_t< R > > sizesToOffsets(R const &sizes)
Definition Algorithm.h:283

References allgather(), BaseLib::MPI::Mpi::communicator, mpiType(), and BaseLib::sizesToOffsets().

Referenced by MeshLib::computeGhostBaseNodeGlobalNodeIDsOfSubDomainPartition(), and MathLib::PETScVector::getGlobalVector().

◆ allreduce() [1/2]

template<typename T >
static std::vector< T > BaseLib::MPI::allreduce ( std::vector< T > const & vector,
MPI_Op const & mpi_op,
Mpi const & mpi )
static

Definition at line 137 of file MPI.h.

139{
140 std::size_t const size = vector.size();
141 std::vector<T> result(vector.size());
142
143 MPI_Allreduce(vector.data(), result.data(), size, mpiType<T>(), mpi_op,
144 mpi.communicator);
145 return result;
146}

References BaseLib::MPI::Mpi::communicator, and mpiType().

◆ allreduce() [2/2]

◆ allreduceInplace()

template<typename T >
static void BaseLib::MPI::allreduceInplace ( std::vector< T > & vector,
MPI_Op const & mpi_op,
Mpi const & mpi )
static

Definition at line 149 of file MPI.h.

152{
153 MPI_Allreduce(MPI_IN_PLACE,
154 vector.data(),
155 vector.size(),
156 mpiType<T>(),
157 mpi_op,
158 mpi.communicator);
159}

References BaseLib::MPI::Mpi::communicator, and mpiType().

Referenced by MeshLib::computeGhostBaseNodeGlobalNodeIDsOfSubDomainPartition().

◆ mpiType()

template<typename T >
MPI_Datatype BaseLib::MPI::mpiType ( )
constexpr

Definition at line 66 of file MPI.h.

67{
68 using U = std::remove_const_t<T>;
69 if constexpr (std::is_same_v<U, bool>)
70 {
71 return MPI_C_BOOL;
72 }
73 if constexpr (std::is_same_v<U, char>)
74 {
75 return MPI_CHAR;
76 }
77 if constexpr (std::is_same_v<U, double>)
78 {
79 return MPI_DOUBLE;
80 }
81 if constexpr (std::is_same_v<U, float>)
82 {
83 return MPI_FLOAT;
84 }
85 if constexpr (std::is_same_v<U, int>)
86 {
87 return MPI_INT;
88 }
89 if constexpr (std::is_same_v<U, std::size_t>)
90 {
91 return MPI_UNSIGNED_LONG;
92 }
93 if constexpr (std::is_same_v<U, unsigned int>)
94 {
95 return MPI_UNSIGNED;
96 }
97}

Referenced by allgather(), allgather(), allgatherv(), allreduce(), allreduce(), and allreduceInplace().

◆ reduceMin()

static int BaseLib::MPI::reduceMin ( int const val)
inlinestatic

The reduction is implemented transparently for with and without MPI. In the latter case the input value is returned.

Definition at line 191 of file MPI.h.

192{
193#ifdef USE_PETSC
194 return allreduce(val, MPI_MIN, Mpi{MPI_COMM_WORLD});
195#else
196 return val;
197#endif
198}
static T allreduce(T const &value, MPI_Op const &mpi_op, Mpi const &mpi)
Definition MPI.h:128

References allreduce().

Referenced by NumLib::NonlinearSolver< NonlinearSolverTag::Newton >::solve().