44#ifndef __ADS_MPIExecutor
45 #define __ADS_MPIExecutor
48#if defined( ADS_WITH_MPI )
58#ifndef OMPI_SKIP_MPICXX
59 #define OMPI_SKIP_MPICXX 1
61#ifndef MPICH_SKIP_MPICXX
62 #define MPICH_SKIP_MPICXX 1
84inline void mpiCheck(
int code ,
const char * what )
86 if( code != MPI_SUCCESS )
87 throw( std::runtime_error( std::string(
"MPI: " ) + what +
" failed" ) );
92MPI_Datatype mpiType(
void )
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 ) )
98 else if constexpr(
sizeof( T ) ==
sizeof( double ) )
101 return( MPI_LONG_DOUBLE );
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 );
111 return( MPI_INT64_T );
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 );
121 return( MPI_UINT64_T );
138 int level = MPI_THREAD_FUNNELED ) {
140 MPI_Initialized( &done );
143 detail::mpiCheck( MPI_Init_thread( &argc , &argv ,
level , &f_level ) ,
158 [[nodiscard]]
int level(
void )
const {
return( f_level ); }
161 bool f_owner =
false;
162 int f_level = MPI_THREAD_SINGLE;
177 MPI_Initialized( &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" );
184 [[nodiscard]] std::size_t
rank(
void )
const {
185 return( std::size_t( f_rank ) );
189 [[nodiscard]] std::size_t
size(
void )
const {
190 return( std::size_t( f_size ) );
194 [[nodiscard]] MPI_Comm
comm(
void )
const {
return( f_comm ); }
199 void run( F && f )
const {
205 detail::mpiCheck( MPI_Barrier( f_comm ) ,
"MPI_Barrier" );
233 MPI_Wait( &f_req , MPI_STATUS_IGNORE );
253 detail::mpiCheck( MPI_Iallreduce( &f_in , &f_out , 1 ,
254 detail::mpiType< T >() ,
255 MPI_MIN , f_comm , &f_req ) ,
263 [[nodiscard]] std::optional< T >
test(
void ) {
265 return( std::nullopt );
267 detail::mpiCheck( MPI_Test( &f_req , &flag , MPI_STATUS_IGNORE ) ,
270 return( std::nullopt );
279 detail::mpiCheck( MPI_Wait( &f_req , MPI_STATUS_IGNORE ) ,
"MPI_Wait" );
285 [[nodiscard]]
bool inFlight(
void )
const {
return( f_flight ); }
289 MPI_Request f_req = MPI_REQUEST_NULL;
292 bool f_flight =
false;
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