ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
MPIExecutor.h
Go to the documentation of this file.
1/*--------------------------------------------------------------------------*/
2/*--------------------------- File MPIExecutor.h ---------------------------*/
3/*--------------------------------------------------------------------------*/
40/*--------------------------------------------------------------------------*/
41/*----------------------------- DEFINITIONS --------------------------------*/
42/*--------------------------------------------------------------------------*/
43
44#ifndef __ADS_MPIExecutor
45 #define __ADS_MPIExecutor
46/* self-identification: #endif constant */
47
48#if defined( ADS_WITH_MPI )
49
50/*--------------------------------------------------------------------------*/
51/*------------------------------ INCLUDES ----------------------------------*/
52/*--------------------------------------------------------------------------*/
53
54#include "ADS/Contract.h"
55
56// the C interface of MPI only: the bindings of C++, removed from the
57// standard, do not compile cleanly
58#ifndef OMPI_SKIP_MPICXX
59 #define OMPI_SKIP_MPICXX 1
60#endif
61#ifndef MPICH_SKIP_MPICXX
62 #define MPICH_SKIP_MPICXX 1
63#endif
64#include <mpi.h>
65
66#include <cstddef>
67#include <cstdint>
68#include <optional>
69#include <stdexcept>
70#include <string>
71#include <type_traits>
72
73/*--------------------------------------------------------------------------*/
74/*--------------------------- NAMESPACE ------------------------------------*/
75/*--------------------------------------------------------------------------*/
76
77namespace ADS
78{
79
80namespace detail
81{
82
84inline void mpiCheck( int code , const char * what )
85{
86 if( code != MPI_SUCCESS )
87 throw( std::runtime_error( std::string( "MPI: " ) + what + " failed" ) );
88 }
89
91template< class T >
92MPI_Datatype mpiType( void )
93{
94 static_assert( std::is_arithmetic_v< T > , "an arithmetic type" );
95 if constexpr( std::is_floating_point_v< T > ) {
96 if constexpr( sizeof( T ) == sizeof( float ) )
97 return( MPI_FLOAT );
98 else if constexpr( sizeof( T ) == sizeof( double ) )
99 return( MPI_DOUBLE );
100 else
101 return( MPI_LONG_DOUBLE );
102 }
103 else if constexpr( std::is_signed_v< T > ) {
104 if constexpr( sizeof( T ) == 1 )
105 return( MPI_INT8_T );
106 else if constexpr( sizeof( T ) == 2 )
107 return( MPI_INT16_T );
108 else if constexpr( sizeof( T ) == 4 )
109 return( MPI_INT32_T );
110 else
111 return( MPI_INT64_T );
112 }
113 else {
114 if constexpr( sizeof( T ) == 1 )
115 return( MPI_UINT8_T );
116 else if constexpr( sizeof( T ) == 2 )
117 return( MPI_UINT16_T );
118 else if constexpr( sizeof( T ) == 4 )
119 return( MPI_UINT32_T );
120 else
121 return( MPI_UINT64_T );
122 }
123 }
124
125} // end( namespace detail )
126
127/*--------------------------------------------------------------------------*/
128/*-------------------------- CLASS MPIEnvironment --------------------------*/
129/*--------------------------------------------------------------------------*/
130
133{
134 public:
137 MPIEnvironment( int & argc , char **& argv ,
138 int level = MPI_THREAD_FUNNELED ) {
139 int done = 0;
140 MPI_Initialized( &done );
141 if( done )
142 return;
143 detail::mpiCheck( MPI_Init_thread( &argc , &argv , level , &f_level ) ,
144 "MPI_Init_thread" );
145 f_owner = true;
146 }
147
148 MPIEnvironment( const MPIEnvironment & ) = delete;
153 if( f_owner )
154 MPI_Finalize();
155 }
156
158 [[nodiscard]] int level( void ) const { return( f_level ); }
159
160 private:
161 bool f_owner = false;
162 int f_level = MPI_THREAD_SINGLE;
163 }; // end( class MPIEnvironment )
164
165/*--------------------------------------------------------------------------*/
166/*--------------------------- CLASS MPIExecutor ----------------------------*/
167/*--------------------------------------------------------------------------*/
168
171{
172 public:
175 explicit MPIExecutor( MPI_Comm comm = MPI_COMM_WORLD ) : f_comm( comm ) {
176 int done = 0;
177 MPI_Initialized( &done );
178 ADS_PRE( done );
179 detail::mpiCheck( MPI_Comm_rank( comm , &f_rank ) , "MPI_Comm_rank" );
180 detail::mpiCheck( MPI_Comm_size( comm , &f_size ) , "MPI_Comm_size" );
181 }
182
184 [[nodiscard]] std::size_t rank( void ) const {
185 return( std::size_t( f_rank ) );
186 }
187
189 [[nodiscard]] std::size_t size( void ) const {
190 return( std::size_t( f_size ) );
191 }
192
194 [[nodiscard]] MPI_Comm comm( void ) const { return( f_comm ); }
195
198 template< class F >
199 void run( F && f ) const {
200 f( rank() );
201 }
202
204 void barrier( void ) const {
205 detail::mpiCheck( MPI_Barrier( f_comm ) , "MPI_Barrier" );
206 }
207
208 private:
209 MPI_Comm f_comm;
210 int f_rank = 0;
211 int f_size = 1;
212 }; // end( class MPIExecutor )
213
214/*--------------------------------------------------------------------------*/
215/*-------------------------- CLASS DistributedMin --------------------------*/
216/*--------------------------------------------------------------------------*/
217
220template< class T >
222{
223 public:
225 explicit DistributedMin( MPI_Comm comm = MPI_COMM_WORLD ) : f_comm( comm ) { }
227 DistributedMin( const DistributedMin & ) = delete;
232 if( f_flight )
233 MPI_Wait( &f_req , MPI_STATUS_IGNORE );
234 }
235
249 bool start( T v ) {
250 if( f_flight )
251 return( false );
252 f_in = v;
253 detail::mpiCheck( MPI_Iallreduce( &f_in , &f_out , 1 ,
254 detail::mpiType< T >() ,
255 MPI_MIN , f_comm , &f_req ) ,
256 "MPI_Iallreduce" );
257 f_flight = true;
258 return( true );
259 }
260
263 [[nodiscard]] std::optional< T > test( void ) {
264 if( ! f_flight )
265 return( std::nullopt );
266 int flag = 0;
267 detail::mpiCheck( MPI_Test( &f_req , &flag , MPI_STATUS_IGNORE ) ,
268 "MPI_Test" );
269 if( ! flag )
270 return( std::nullopt );
271 f_flight = false;
272 return( f_out );
273 }
274
277 T wait( void ) {
278 ADS_PRE( f_flight );
279 detail::mpiCheck( MPI_Wait( &f_req , MPI_STATUS_IGNORE ) , "MPI_Wait" );
280 f_flight = false;
281 return( f_out );
282 }
283
285 [[nodiscard]] bool inFlight( void ) const { return( f_flight ); }
286
287 private:
288 MPI_Comm f_comm;
289 MPI_Request f_req = MPI_REQUEST_NULL;
290 T f_in{};
291 T f_out{};
292 bool f_flight = false;
293 }; // end( class DistributedMin )
294
295/*--------------------------------------------------------------------------*/
296
297} // end( namespace ADS )
298
299#endif /* ADS_WITH_MPI */
300
301/*--------------------------------------------------------------------------*/
302
303#endif /* MPIExecutor.h included */
304
305/*--------------------------------------------------------------------------*/
306/*------------------------ End File MPIExecutor.h --------------------------*/
307/*--------------------------------------------------------------------------*/
The contracts of ADS: the preconditions, the postconditions and the invariants of the operations,...
~DistributedMin()
waits for the reduction in flight, if any
Definition MPIExecutor.h:231
T wait(void)
the minimum of the reduction in flight, waiting for it
Definition MPIExecutor.h:277
DistributedMin(MPI_Comm comm=MPI_COMM_WORLD)
the minima over the processes of comm
Definition MPIExecutor.h:225
std::optional< T > test(void)
the minimum of the last reduction if it has completed, nothing if it is still in flight or none was s...
Definition MPIExecutor.h:263
bool start(T v)
starts the reduction of the value of this process, unless one is in flight, and tells whether it did
Definition MPIExecutor.h:249
bool inFlight(void) const
whether a reduction is in flight
Definition MPIExecutor.h:285
DistributedMin(const DistributedMin &)=delete
no copy, since MPI writes into the object
DistributedMin & operator=(const DistributedMin &)=delete
no copy, since MPI writes into the object
~MPIEnvironment()
MPI ended by MPI_Finalize(), if this object started it.
Definition MPIExecutor.h:152
MPIEnvironment(int &argc, char **&argv, int level=MPI_THREAD_FUNNELED)
MPI started by MPI_Init_thread() at the level level (funneled by default), unless it already was.
Definition MPIExecutor.h:137
int level(void) const
the level of threads that MPI provides, if this object started it
Definition MPIExecutor.h:158
MPIEnvironment(const MPIEnvironment &)=delete
no copy, since MPI ends once
MPIEnvironment & operator=(const MPIEnvironment &)=delete
no copy, since MPI ends once
void run(F &&f) const
the call f( rank ), the worker of this process; every process calls it
Definition MPIExecutor.h:199
std::size_t rank(void) const
the rank of the process
Definition MPIExecutor.h:184
void barrier(void) const
returns when all the processes have called it
Definition MPIExecutor.h:204
std::size_t size(void) const
the number of the processes
Definition MPIExecutor.h:189
MPIExecutor(MPI_Comm comm=MPI_COMM_WORLD)
the executor of the processes of comm
Definition MPIExecutor.h:175
MPI_Comm comm(void) const
the communicator
Definition MPIExecutor.h:194
#define ADS_PRE(cond)
[check]
Definition Contract.h:121
Definition ADT.h:96