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 bool anyOf (bool const val, Mpi const &mpi=Mpi{OGS_COMM_WORLD})

Variables

MPI_Comm OGS_COMM_WORLD = MPI_COMM_WORLD

Function Documentation

◆ allgather() [1/2]

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

Definition at line 109 of file MPI.h.

110{
111 std::size_t const size = vector.size();
112 // Flat in memory over all ranks;
113 std::vector<T> result(mpi.size * size);
114
115 MPI_Allgather(vector.data(), size, mpiType<T>(), result.data(), size,
116 mpiType<T>(), mpi.communicator);
117
118 return result;
119}
constexpr MPI_Datatype mpiType()
Definition MPI.h:64

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

◆ allgather() [2/2]

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

◆ allgatherv()

template<typename T>
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 158 of file MPI.h.

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

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

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

◆ allreduce() [1/2]

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

Definition at line 131 of file MPI.h.

133{
134 std::size_t const size = vector.size();
135 std::vector<T> result(vector.size());
136
137 MPI_Allreduce(vector.data(), result.data(), size, mpiType<T>(), mpi_op,
138 mpi.communicator);
139 return result;
140}

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

◆ allreduce() [2/2]

◆ allreduceInplace()

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

Definition at line 143 of file MPI.h.

146{
147 MPI_Allreduce(MPI_IN_PLACE,
148 vector.data(),
149 vector.size(),
150 mpiType<T>(),
151 mpi_op,
152 mpi.communicator);
153}

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

Referenced by MeshLib::computeGhostBaseNodeGlobalNodeIDsOfSubDomainPartition().

◆ anyOf()

bool BaseLib::MPI::anyOf ( bool const val,
Mpi const & mpi = Mpi{OGS_COMM_WORLD} )
inlinestatic

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

Definition at line 185 of file MPI.h.

189#endif
190)
191{
192#ifdef USE_PETSC
193 return allreduce(val, MPI_LOR, mpi);
194#else
195 return val;
196#endif
197}
MPI_Comm OGS_COMM_WORLD
Definition MPI.cpp:9
static T allreduce(T const &value, MPI_Op const &mpi_op, Mpi const &mpi)
Definition MPI.h:122

References OGS_COMM_WORLD.

Referenced by ProcessLib::AssemblyMixin< Process >::assemble(), ProcessLib::AssemblyMixin< Process >::assembleWithJacobian(), and NumLib::NonlinearSolver< NonlinearSolverTag::Newton >::solve().

◆ mpiType()

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

Definition at line 64 of file MPI.h.

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

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

Variable Documentation

◆ OGS_COMM_WORLD