Loading [MathJax]/jax/output/HTML-CSS/config.js
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 >
static std::vector< T > BaseLib::MPI::allgather ( std::vector< T > const & vector,
Mpi const & mpi )
static

Definition at line 115 of file MPI.h.

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

References BaseLib::MPI::Mpi::communicator, mpiType(), 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 104 of file MPI.h.

105{
106 std::vector<T> result(mpi.size);
107
108 MPI_Allgather(&value, 1, mpiType<T>(), result.data(), 1, mpiType<T>(),
109 mpi.communicator);
110
111 return result;
112}

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

Referenced by allgatherv(), MeshLib::computeNumberOfRegularBaseNodesAtRank(), MeshLib::computeNumberOfRegularHigherOrderNodesAtRank(), MeshLib::computeRegularBaseNodeGlobalNodeIDsOfSubDomainPartition(), MeshLib::IO::getPartitionInfo(), MeshLib::IO::getTopologyType(), 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:104
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().

◆ anyOf()

static 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 191 of file MPI.h.

195#endif
196)
197{
198#ifdef USE_PETSC
199 return allreduce(val, MPI_LOR, mpi);
200#else
201 return val;
202#endif
203}
MPI_Comm OGS_COMM_WORLD
Definition MPI.cpp:15

References OGS_COMM_WORLD.

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

◆ mpiType()

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

Definition at line 70 of file MPI.h.

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

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

Variable Documentation

◆ OGS_COMM_WORLD