20 #ifndef GEOS_COMMON_MPIWRAPPER_HPP_
21 #define GEOS_COMMON_MPIWRAPPER_HPP_
29 #if defined(GEOS_USE_MPI)
31 #define MPI_PARAM( x ) x
33 #define MPI_PARAM( x )
36 #define MPI_COMM_NULL ((MPI_Comm)0x04000000)
37 #define MPI_COMM_WORLD ((MPI_Comm)0x44000000)
38 #define MPI_COMM_SELF ((MPI_Comm)0x40000000)
41 typedef int MPI_Datatype;
42 #define MPI_CHAR ((MPI_Datatype)0x4c000101)
43 #define MPI_SIGNED_CHAR ((MPI_Datatype)0x4c000118)
44 #define MPI_UNSIGNED_CHAR ((MPI_Datatype)0x4c000102)
45 #define MPI_BYTE ((MPI_Datatype)0x4c00010d)
46 #define MPI_C_BOOL ((MPI_Datatype)0x4c00013f)
47 #define MPI_WCHAR ((MPI_Datatype)0x4c00040e)
48 #define MPI_SHORT ((MPI_Datatype)0x4c000203)
49 #define MPI_UNSIGNED_SHORT ((MPI_Datatype)0x4c000204)
50 #define MPI_INT ((MPI_Datatype)0x4c000405)
51 #define MPI_UNSIGNED ((MPI_Datatype)0x4c000406)
52 #define MPI_LONG ((MPI_Datatype)0x4c000807)
53 #define MPI_UNSIGNED_LONG ((MPI_Datatype)0x4c000808)
54 #define MPI_FLOAT ((MPI_Datatype)0x4c00040a)
55 #define MPI_DOUBLE ((MPI_Datatype)0x4c00080b)
56 #define MPI_LONG_DOUBLE ((MPI_Datatype)0x4c00100c)
57 #define MPI_LONG_LONG_INT ((MPI_Datatype)0x4c000809)
58 #define MPI_UNSIGNED_LONG_LONG ((MPI_Datatype)0x4c000819)
59 #define MPI_LONG_LONG MPI_LONG_LONG_INT
63 #define MPI_MAX (MPI_Op)(0x58000001)
64 #define MPI_MIN (MPI_Op)(0x58000002)
65 #define MPI_SUM (MPI_Op)(0x58000003)
66 #define MPI_PROD (MPI_Op)(0x58000004)
67 #define MPI_LAND (MPI_Op)(0x58000005)
68 #define MPI_BAND (MPI_Op)(0x58000006)
69 #define MPI_LOR (MPI_Op)(0x58000007)
70 #define MPI_BOR (MPI_Op)(0x58000008)
71 #define MPI_LXOR (MPI_Op)(0x58000009)
72 #define MPI_BXOR (MPI_Op)(0x5800000a)
73 #define MPI_MINLOC (MPI_Op)(0x5800000b)
74 #define MPI_MAXLOC (MPI_Op)(0x5800000c)
75 #define MPI_REPLACE (MPI_Op)(0x5800000d)
76 #define MPI_NO_OP (MPI_Op)(0x5800000e)
79 #define MPI_UNDEFINED (-32766)
80 #define MPI_STATUS_IGNORE (MPI_Status *)1
81 #define MPI_STATUSES_IGNORE (MPI_Status *)1
82 #define MPI_REQUEST_NULL ((MPI_Request)0x2c000000)
83 typedef int MPI_Request;
86 #define MPI_INFO_NULL (MPI_Info)(0x60000000)
96 #define MPI_CHECK_ERROR( error ) ((void) error)
98 #define MPI_CHECK_ERROR( error ) GEOS_ERROR_IF_NE( error, MPI_SUCCESS );
154 template<
typename FIRST,
typename SECOND >
177 static void barrier( MPI_Comm
const & MPI_PARAM( comm )=
MPI_COMM_GEOS );
179 static int cartCoords( MPI_Comm comm,
int rank,
int maxdims,
int coords[] );
181 static int cartCreate( MPI_Comm comm_old,
int ndims,
const int dims[],
const int periods[],
182 int reorder, MPI_Comm * comm_cart );
184 static int cartRank( MPI_Comm comm,
const int coords[] );
186 static void commFree( MPI_Comm & comm );
188 static int commRank( MPI_Comm
const & MPI_PARAM( comm )=
MPI_COMM_GEOS );
190 static int commSize( MPI_Comm
const & MPI_PARAM( comm )=
MPI_COMM_GEOS );
192 static bool commCompare( MPI_Comm
const & comm1, MPI_Comm
const & comm2 );
194 static bool initialized();
196 static int init(
int * argc,
char * * * argv );
204 static MPI_Comm commDup( MPI_Comm
const comm );
206 static MPI_Comm commSplit( MPI_Comm
const comm,
int color,
int key );
208 static int test( MPI_Request * request,
int * flag,
MPI_Status * status );
210 static int testAny(
int count, MPI_Request array_of_requests[],
int * idx,
int * flags,
MPI_Status array_of_statuses[] );
212 static int testSome(
int count, MPI_Request array_of_requests[],
int * outcount,
int array_of_indices[],
MPI_Status array_of_statuses[] );
214 static int testAll(
int count, MPI_Request array_of_requests[],
int * flags,
MPI_Status array_of_statuses[] );
236 static int checkAny(
int count, MPI_Request array_of_requests[],
int * idx,
int * flag,
MPI_Status array_of_statuses[] );
247 static int checkAll(
int count, MPI_Request array_of_requests[],
int * flag,
MPI_Status array_of_statuses[] );
249 static int wait( MPI_Request * request,
MPI_Status * status );
251 static int waitAny(
int count, MPI_Request array_of_requests[],
int * indx,
MPI_Status array_of_statuses[] );
253 static int waitSome(
int count, MPI_Request array_of_requests[],
int * outcount,
int array_of_indices[],
MPI_Status array_of_statuses[] );
255 static int waitAll(
int count, MPI_Request array_of_requests[],
MPI_Status array_of_statuses[] );
257 static double wtime(
void );
270 MPI_Request array_of_requests[],
272 std::function< MPI_Request (
int ) > func );
284 MPI_Request array_of_requests[],
286 std::function< MPI_Request (
int ) > func );
301 stdVector< std::tuple< MPI_Request *,
MPI_Status *, std::function< MPI_Request (
int ) > > >
const & phases );
317 stdVector< std::tuple< MPI_Request *,
MPI_Status *, std::function< MPI_Request (
int ) > > >
const & phases );
320 #if !defined(GEOS_USE_MPI)
321 static std::map< int, std::pair< int, void * > > & getTagToPointersMap()
323 static std::map< int, std::pair< int, void * > > tagToPointers;
324 return tagToPointers;
345 template<
typename T_SEND,
typename T_RECV >
364 template<
typename T_SEND,
typename T_RECV >
378 template<
typename T >
381 template<
typename T >
386 template<
typename T >
399 template<
typename T >
410 template<
typename T >
421 template<
typename SRC_CONTAINER_TYPE,
typename DST_CONTAINER_TYPE >
433 template<
typename SRC_CONTAINER_TYPE,
typename DST_CONTAINER_TYPE >
434 static void allReduce( SRC_CONTAINER_TYPE
const & src, DST_CONTAINER_TYPE & dst,
int const count,
Reduction const op, MPI_Comm
const comm );
445 template<
typename FIRST,
typename SECOND, PairReduction OP >
459 template<
typename FIRST,
typename SECOND,
typename CONTAINER, PairReduction OP >
472 template<
typename T >
473 static int reduce( T
const * sendbuf, T * recvbuf,
int count, MPI_Op op,
int root, MPI_Comm comm =
MPI_COMM_GEOS );
483 template<
typename T >
494 template<
typename T >
498 template<
typename T >
499 static int scan( T
const * sendbuf, T * recvbuf,
int count, MPI_Op op, MPI_Comm comm =
MPI_COMM_GEOS );
501 template<
typename T >
502 static int exscan( T
const * sendbuf, T * recvbuf,
int count, MPI_Op op, MPI_Comm comm =
MPI_COMM_GEOS );
512 template<
typename T >
522 template<
typename T >
537 template<
typename TS,
typename TR >
538 static int gather( TS
const *
const sendbuf,
556 template<
typename T,
typename DST_CONTAINER,
557 typename = std::enable_if_t<
558 std::is_trivially_copyable_v< T > &&
559 std::is_same_v< decltype(std::declval< DST_CONTAINER >().size()),
std::size_t > &&
560 std::is_same_v< decltype(std::declval< DST_CONTAINER >().data()), T * > > >
562 DST_CONTAINER & destValuesBuffer,
580 template<
typename TS,
typename TR >
584 const int * recvcounts,
602 template<
typename TS,
typename TR >
624 template<
typename TS,
typename TR >
626 const int * sendcounts,
642 template<
typename T >
644 int MPI_PARAM( source ),
646 MPI_Comm MPI_PARAM( comm ),
649 template<
typename T >
651 int MPI_PARAM( dest ),
653 MPI_Comm MPI_PARAM( comm ),
654 MPI_Request * MPI_PARAM( request ) );
666 template<
typename T >
672 MPI_Request * request );
683 template<
typename T >
684 static int send( T
const *
const buf,
700 template<
typename T >
701 static int iSend( T
const *
const buf,
706 MPI_Request * request );
715 template<
typename U,
typename T >
723 template<
typename T >
732 template<
typename T >
740 template<
typename T >
749 template<
typename T >
758 template<
typename FIRST,
typename SECOND >
767 template<
typename FIRST,
typename SECOND,
typename CONTAINER >
775 template<
typename T >
784 template<
typename T >
793 template<
typename FIRST,
typename SECOND >
802 template<
typename FIRST,
typename SECOND,
typename CONTAINER >
816 template<
typename T >
817 static int allReduce( T
const * sendbuf, T * recvbuf,
int count, MPI_Op op, MPI_Comm comm =
MPI_COMM_GEOS );
824 struct ManagedResources
827 std::set< MPI_Op > m_mpiOps;
830 std::set< MPI_Datatype > m_mpiTypes;
843 ManagedResources & getManagedResources();
845 template<
typename T,
typename ENABLE =
void >
846 struct MpiTypeImpl {};
848 #define ADD_MPI_TYPE_MAP( T, MPI_T ) \
849 template<> struct MpiTypeImpl< T > { static MPI_Datatype get() { return MPI_T; } }
851 ADD_MPI_TYPE_MAP(
float, MPI_FLOAT );
852 ADD_MPI_TYPE_MAP(
double, MPI_DOUBLE );
854 ADD_MPI_TYPE_MAP(
char, MPI_CHAR );
855 ADD_MPI_TYPE_MAP(
signed char, MPI_SIGNED_CHAR );
856 ADD_MPI_TYPE_MAP(
unsigned char, MPI_UNSIGNED_CHAR );
858 ADD_MPI_TYPE_MAP(
int, MPI_INT );
859 ADD_MPI_TYPE_MAP(
long int, MPI_LONG );
860 ADD_MPI_TYPE_MAP(
long long int, MPI_LONG_LONG );
862 ADD_MPI_TYPE_MAP(
unsigned int, MPI_UNSIGNED );
863 ADD_MPI_TYPE_MAP(
unsigned long int, MPI_UNSIGNED_LONG );
864 ADD_MPI_TYPE_MAP(
unsigned long long int, MPI_UNSIGNED_LONG_LONG );
866 #undef ADD_MPI_TYPE_MAP
868 template<
typename T >
869 struct MpiTypeImpl< T, std::enable_if_t< std::is_enum< T >::value > >
871 static MPI_Datatype get() {
return MpiTypeImpl< std::underlying_type_t< T > >::get(); }
875 struct MpiTypeImpl< bool * >
877 static MPI_Datatype get()
885 template<
typename T >
886 MPI_Datatype getMpiType()
888 return MpiTypeImpl< T >::get();
891 template<
typename FIRST,
typename SECOND >
892 MPI_Datatype getMpiPairType()
894 static_assert(
"no default implementation, please add a template specialization and add it in the \"testMpiWrapper\" unit test." );
897 template<> MPI_Datatype getMpiPairType< int, int >();
898 template<> MPI_Datatype getMpiPairType< long int, int >();
899 template<> MPI_Datatype getMpiPairType< long int, long int >();
900 template<> MPI_Datatype getMpiPairType< long long int, long long int >();
901 template<> MPI_Datatype getMpiPairType< float, int >();
902 template<> MPI_Datatype getMpiPairType< double, int >();
903 template<> MPI_Datatype getMpiPairType< double, long int >();
904 template<> MPI_Datatype getMpiPairType< double, long long int >();
905 template<> MPI_Datatype getMpiPairType< double, double >();
908 template<
typename FIRST,
typename SECOND, MpiWrapper::PairReduction OP >
909 MPI_Op getMpiPairReductionOp()
911 static auto const createOpHolder = [] () {
912 using PAIR_T = MpiWrapper::PairType< FIRST, SECOND >;
914 auto const customOpFunc =
915 [](
void * invec,
void * inoutvec,
int * len, MPI_Datatype * )
917 for(
int i = 0; i < *len; ++i )
919 PAIR_T & in =
static_cast< PAIR_T *
>(invec)[i];
920 PAIR_T & inout =
static_cast< PAIR_T *
>(inoutvec)[i];
923 if( std::tie( in.first, in.second ) < std::tie( inout.first, inout.second ) )
928 if( std::tie( in.first, in.second ) > std::tie( inout.first, inout.second ) )
937 internal::getManagedResources().m_mpiOps.emplace( mpiOp );
941 static MPI_Op mpiOp{ createOpHolder() };
976 GEOS_ERROR(
"Unsupported reduction operation" );
981 template<
typename T_SEND,
typename T_RECV >
984 T_RECV *
const recvbuf,
986 MPI_Comm MPI_PARAM( comm ) )
989 return MPI_Allgather( sendbuf, sendcount, internal::getMpiType< T_SEND >(),
990 recvbuf, recvcount, internal::getMpiType< T_RECV >(),
993 static_assert( std::is_same< T_SEND, T_RECV >::value,
994 "MpiWrapper::allgather() for serial run requires send and receive buffers are of the same type" );
996 std::copy( sendbuf, sendbuf + sendcount, recvbuf )
1001 template<
typename T_SEND,
typename T_RECV >
1004 T_RECV *
const recvbuf,
1006 int * displacements,
1007 MPI_Comm MPI_PARAM( comm ) )
1010 return MPI_Allgatherv( sendbuf, sendcount, internal::getMpiType< T_SEND >(),
1011 recvbuf, recvcounts, displacements, internal::getMpiType< T_RECV >(),
1014 static_assert( std::is_same< T_SEND, T_RECV >::value,
1015 "MpiWrapper::allgatherv() for serial run requires send and receive buffers are of the same type" );
1017 std::copy( sendbuf, sendbuf + sendcount, recvbuf )
1023 template<
typename T >
1027 int const mpiSize = commSize( comm );
1028 allValues.resize( mpiSize );
1030 MPI_Datatype
const MPI_TYPE = internal::getMpiType< T >();
1032 MPI_Allgather( &myValue, 1, MPI_TYPE, allValues.data(), 1, MPI_TYPE, comm );
1035 allValues.resize( 1 );
1036 allValues[0] = myValue;
1040 template<
typename T >
1042 array1d< T > & allValues,
1043 MPI_Comm MPI_PARAM( comm ) )
1045 int const sendSize = LvArray::integerConversion< int >( sendValues.size() );
1047 int const mpiSize = commSize( comm );
1048 allValues.resize( mpiSize * sendSize );
1049 return MPI_Allgather( sendValues.data(),
1051 internal::getMpiType< T >(),
1054 internal::getMpiType< T >(),
1058 allValues.resize( sendSize );
1061 allValues[a] = sendValues[a];
1067 template<
typename T >
1068 int MpiWrapper::allGatherv( arrayView1d< T const >
const & sendValues,
1069 array1d< T > & allValues,
1070 MPI_Comm MPI_PARAM( comm ) )
1072 int const sendSize = LvArray::integerConversion< int >( sendValues.size() );
1074 int const mpiSize = commSize( comm );
1075 array1d< int > counts;
1077 array1d< int > displs( mpiSize + 1 );
1078 std::partial_sum( counts.begin(), counts.end(), displs.begin() + 1 );
1079 allValues.resize( displs.back() );
1080 return MPI_Allgatherv( sendValues.data(),
1082 internal::getMpiType< T >(),
1086 internal::getMpiType< T >(),
1090 allValues.resize( sendSize );
1093 allValues[a] = sendValues[a];
1099 template<
typename T >
1103 MPI_Op
const MPI_PARAM( op ),
1104 MPI_Comm
const MPI_PARAM( comm ) )
1107 MPI_Datatype
const mpiType = internal::getMpiType< T >();
1108 return MPI_Allreduce( sendbuf == recvbuf ? MPI_IN_PLACE : sendbuf, recvbuf, count, mpiType, op, comm );
1110 if( sendbuf != recvbuf )
1112 memcpy( recvbuf, sendbuf, count *
sizeof( T ) );
1118 template<
typename T >
1122 MPI_Op
const MPI_PARAM( op ),
1124 MPI_Comm
const MPI_PARAM( comm ) )
1127 MPI_Datatype
const mpiType = internal::getMpiType< T >();
1128 return MPI_Reduce( sendbuf == recvbuf ? MPI_IN_PLACE : sendbuf, recvbuf, count, mpiType, op, root, comm );
1130 if( sendbuf != recvbuf )
1132 memcpy( recvbuf, sendbuf, count *
sizeof( T ) );
1138 template<
typename T >
1139 int MpiWrapper::scan( T
const *
const sendbuf,
1142 MPI_Op MPI_PARAM( op ),
1143 MPI_Comm MPI_PARAM( comm ) )
1146 return MPI_Scan( sendbuf, recvbuf, count, internal::getMpiType< T >(), op, comm );
1148 memcpy( recvbuf, sendbuf, count*
sizeof(T) );
1153 template<
typename T >
1154 int MpiWrapper::exscan( T
const *
const MPI_PARAM( sendbuf ),
1157 MPI_Op MPI_PARAM( op ),
1158 MPI_Comm MPI_PARAM( comm ) )
1161 return MPI_Exscan( sendbuf, recvbuf, count, internal::getMpiType< T >(), op, comm );
1163 memset( recvbuf, 0, count*
sizeof(T) );
1168 template<
typename T >
1170 int MPI_PARAM( count ),
1171 int MPI_PARAM( root ),
1172 MPI_Comm MPI_PARAM( comm ) )
1175 return MPI_Bcast( buffer, count, internal::getMpiType< T >(), root, comm );
1182 template<
typename T >
1183 void MpiWrapper::broadcast( T & MPI_PARAM( value ),
int MPI_PARAM( srcRank ), MPI_Comm MPI_PARAM( comm ) )
1186 MPI_Bcast( &value, 1, internal::getMpiType< T >(), srcRank, comm );
1192 void MpiWrapper::broadcast< string >(
string & MPI_PARAM( value ),
1193 int MPI_PARAM( srcRank ),
1194 MPI_Comm MPI_PARAM( comm ) )
1197 int size = LvArray::integerConversion< int >( value.size() );
1199 value.resize( size );
1200 MPI_Bcast(
const_cast< char *
>( value.data() ), size, internal::getMpiType< char >(), srcRank, comm );
1204 template<
typename TS,
typename TR >
1209 int MPI_PARAM( root ),
1210 MPI_Comm MPI_PARAM( comm ) )
1213 return MPI_Gather( sendbuf, sendcount, internal::getMpiType< TS >(),
1214 recvbuf, recvcount, internal::getMpiType< TR >(),
1217 static_assert( std::is_same< TS, TR >::value,
1218 "MpiWrapper::gather() for serial run requires send and receive buffers are of the same type" );
1219 std::size_t const sendBufferSize = sendcount *
sizeof(TS);
1220 std::size_t const recvBufferSize = recvcount *
sizeof(TR);
1221 GEOS_ERROR_IF_NE_MSG( sendBufferSize, recvBufferSize,
"size of send buffer and receive buffer are not equal" );
1222 memcpy( recvbuf, sendbuf, sendBufferSize );
1227 template<
typename T,
typename DST_CONTAINER,
typename >
1229 DST_CONTAINER & destValuesBuffer,
1231 MPI_Comm MPI_PARAM( comm ) )
1233 if( commRank() == 0 )
1235 "Receive buffer is not large enough to contain the values to receive." );
1237 return MPI_Gather( &value,
sizeof( T ), internal::getMpiType< uint8_t >(),
1238 destValuesBuffer.data(),
sizeof( T ), internal::getMpiType< uint8_t >(),
1241 memcpy( destValuesBuffer.data(), &value, sendBufferSize );
1246 template<
typename TS,
typename TR >
1250 const int * recvcounts,
1251 const int * MPI_PARAM( displs ),
1252 int MPI_PARAM( root ),
1253 MPI_Comm MPI_PARAM( comm ) )
1256 return MPI_Gatherv( sendbuf, sendcount, internal::getMpiType< TS >(),
1257 recvbuf, recvcounts, displs, internal::getMpiType< TR >(),
1260 static_assert( std::is_same< TS, TR >::value,
1261 "MpiWrapper::gather() for serial run requires send and receive buffers are of the same type" );
1262 std::size_t const sendBufferSize = sendcount *
sizeof(TS);
1263 std::size_t const recvBufferSize = recvcounts[0] *
sizeof(TR);
1264 GEOS_ERROR_IF_NE_MSG( sendBufferSize, recvBufferSize,
"size of send buffer and receive buffer are not equal" );
1265 memcpy( recvbuf, sendbuf, sendBufferSize );
1271 template<
typename TS,
typename TR >
1276 int MPI_PARAM( root ),
1277 MPI_Comm MPI_PARAM( comm ))
1280 return MPI_Scatter( sendbuf, sendcount, internal::getMpiType< TS >(),
1281 recvbuf, recvcount, internal::getMpiType< TR >(),
1284 static_assert( std::is_same< TS, TR >::value,
1285 "MpiWrapper::scatter() for serial run requires send and receive buffers are of the same type" );
1286 std::size_t const sendBufferSize = sendcount *
sizeof(TS);
1287 std::size_t const recvBufferSize = recvcount *
sizeof(TR);
1288 GEOS_ERROR_IF_NE_MSG( sendBufferSize, recvBufferSize,
"size of send buffer and receive buffer are not equal" );
1289 memcpy( recvbuf, sendbuf, sendBufferSize );
1294 template<
typename TS,
typename TR >
1296 const int * sendcounts,
1297 const int * MPI_PARAM( displs ),
1300 int MPI_PARAM( root ),
1301 MPI_Comm MPI_PARAM( comm ))
1304 return MPI_Scatterv( sendbuf, sendcounts, displs, internal::getMpiType< TS >(),
1305 recvbuf, recvcount, internal::getMpiType< TR >(),
1308 static_assert( std::is_same< TS, TR >::value,
1309 "MpiWrapper::scatterv() for serial run requires send and receive buffers are of the same type" );
1310 std::size_t const sendBufferSize = sendcounts *
sizeof(TS);
1311 std::size_t const recvBufferSize = recvcount *
sizeof(TR);
1312 GEOS_ERROR_IF_NE_MSG( sendBufferSize, recvBufferSize,
"size of send buffer and receive buffer are not equal" );
1313 memcpy( recvbuf, sendbuf, sendBufferSize );
1320 template<
typename T >
1323 int MPI_PARAM( source ),
1325 MPI_Comm MPI_PARAM( comm ),
1326 MPI_Request * MPI_PARAM( request ) )
1330 "Attempting to use an MPI_Request that is still in use." );
1331 return MPI_Irecv( buf, count, internal::getMpiType< T >(), source, tag, comm, request );
1333 std::map< int, std::pair< int, void * > > & pointerMap = getTagToPointersMap();
1334 std::map< int, std::pair< int, void * > >::iterator iPointer = pointerMap.find( tag );
1336 if( iPointer==pointerMap.end() )
1338 pointerMap.insert( {tag, {1, buf} } );
1343 "Tag does is assigned, but pointer was not set by iSend." );
1344 memcpy( buf, iPointer->second.second, count*
sizeof(T) );
1345 pointerMap.erase( iPointer );
1351 template<
typename T >
1352 int MpiWrapper::recv( array1d< T > & buf,
1353 int MPI_PARAM( source ),
1355 MPI_Comm MPI_PARAM( comm ),
1361 MPI_Probe( source, tag, comm, &status );
1362 MPI_Get_count( &status, MPI_CHAR, &count );
1365 buf.resize( count /
sizeof( T ) );
1367 return MPI_Recv(
reinterpret_cast< char *
>( buf.data() ),
1380 template<
typename T >
1381 int MpiWrapper::iSend( arrayView1d< T >
const & buf,
1382 int MPI_PARAM( dest ),
1384 MPI_Comm MPI_PARAM( comm ),
1385 MPI_Request * MPI_PARAM( request ) )
1389 "Attempting to use an MPI_Request that is still in use." );
1390 return MPI_Isend(
reinterpret_cast< void const *
>( buf.data() ),
1391 buf.size() *
sizeof( T ),
1403 template<
typename T >
1411 return MPI_Send( buf, count, internal::getMpiType< T >(), dest, tag, comm );
1417 template<
typename T >
1418 int MpiWrapper::iSend( T
const *
const buf,
1420 int MPI_PARAM( dest ),
1422 MPI_Comm MPI_PARAM( comm ),
1423 MPI_Request * MPI_PARAM( request ) )
1427 "Attempting to use an MPI_Request that is still in use." );
1428 return MPI_Isend( buf, count, internal::getMpiType< T >(), dest, tag, comm, request );
1430 std::map< int, std::pair< int, void * > > & pointerMap = getTagToPointersMap();
1431 std::map< int, std::pair< int, void * > >::iterator iPointer = pointerMap.find( tag );
1433 if( iPointer==pointerMap.end() )
1435 pointerMap.insert( {tag, {0,
const_cast< T *
>(buf)}
1441 "Tag does is assigned, but pointer was not set by iRecv." );
1442 memcpy( iPointer->second.second, buf, count*
sizeof(T) );
1443 pointerMap.erase( iPointer );
1449 template<
typename U,
typename T >
1455 U
const convertedValue = value;
1456 int const error = MPI_Exscan( &convertedValue, &localResult, 1, internal::getMpiType< U >(), MPI_SUM, comm );
1457 MPI_CHECK_ERROR( error );
1459 if( commRank() == 0 )
1468 template<
typename T >
1476 template<
typename T >
1483 template<
typename SRC_CONTAINER_TYPE,
typename DST_CONTAINER_TYPE >
1487 "The type in the source container must be trivially copyable." );
1489 "The type in the destination container must be trivially copyable." );
1492 "Source and destination containers must have the same value type." );
1498 template<
typename SRC_CONTAINER_TYPE,
typename DST_CONTAINER_TYPE >
1502 "The type in the source container must be trivially copyable." );
1504 "The type in the destination container must be trivially copyable." );
1507 "Source and destination containers must have the same value type." );
1509 allReduce( src.data(), dst.data(), LvArray::integerConversion< int >( src.size() ),
getMpiOp( op ), comm );
1512 template<
typename T >
1518 template<
typename T >
1524 template<
typename T >
1530 template<
typename T >
1536 template<
typename T >
1542 template<
typename T >
1549 template<
typename T >
1557 template<
typename T >
1564 template<
typename FIRST,
typename SECOND, MpiWrapper::PairReduction const OP >
1569 auto const type = internal::getMpiPairType< FIRST, SECOND >();
1570 auto const mpiOp = internal::getMpiPairReductionOp< FIRST, SECOND, OP >();
1572 MPI_Allreduce( MPI_IN_PLACE, &pair, 1, type, mpiOp, comm );
1579 template<
typename FIRST,
typename SECOND,
typename CONTAINER, MpiWrapper::PairReduction const OP >
1584 std::function< PAIR_T( PAIR_T, PAIR_T ) >
const getMin = []( PAIR_T
const & a, PAIR_T
const & b ) {
1585 return ( std::tie( a.first, a.second ) < std::tie( b.first, b.second ) ) ? a : b;
1587 std::function< PAIR_T( PAIR_T, PAIR_T ) >
const getMax = []( PAIR_T
const & a, PAIR_T
const & b ) {
1588 return ( std::tie( a.first, a.second ) > std::tie( b.first, b.second ) ) ? a : b;
1590 PAIR_T
const defaultPair{
1591 OP ==
PairReduction::Min ? std::numeric_limits< FIRST >::max() : std::numeric_limits< FIRST >::lowest(),
1592 OP ==
PairReduction::Min ? std::numeric_limits< SECOND >::max() : std::numeric_limits< SECOND >::lowest()
1595 PAIR_T pair = std::accumulate( pairs.begin(), pairs.end(), defaultPair,
1597 return allReduce< FIRST, SECOND, OP >( pair, comm );
1600 template<
typename FIRST,
typename SECOND >
1602 {
return allReduce< FIRST, SECOND, PairReduction::Min >( pair, comm ); }
1604 template<
typename FIRST,
typename SECOND,
typename CONTAINER >
1606 {
return allReduce< FIRST, SECOND, CONTAINER, PairReduction::Min >( pairs, comm ); }
1608 template<
typename FIRST,
typename SECOND >
1610 {
return allReduce< FIRST, SECOND, PairReduction::Max >( pair, comm ); }
1612 template<
typename FIRST,
typename SECOND,
typename CONTAINER >
1614 {
return allReduce< FIRST, SECOND, CONTAINER, PairReduction::Max >( pairs, comm ); }
#define GEOS_ERROR(msg)
Raise a hard error and terminate the program.
#define GEOS_ERROR_IF(EXP, msg)
Conditionally raise a hard error and terminate the program.
#define GEOS_ERROR_IF_NE(lhs, rhs)
Raise a hard error if two values are not equal.
#define GEOS_ASSERT_GE(lhs, rhs)
Assert that one value compares greater than or equal to the other in debug builds.
#define GEOS_ERROR_IF_LT_MSG(lhs, rhs, msg)
Raise a hard error if one value compares less than the other.
#define GEOS_ERROR_IF_NE_MSG(lhs, rhs, msg)
Raise a hard error if two values are not equal.
#define GEOS_ASSERT_EQ(lhs, rhs)
Assert that two values compare equal in debug builds.
Lightweight non-owning wrapper over a contiguous range of elements.
constexpr T * data() const noexcept
constexpr size_type size() const noexcept
ArrayView< T, 1 > arrayView1d
Alias for 1D array view.
int MPI_COMM_GEOS
Global MPI communicator used by GEOSX.
GEOS_LOCALINDEX_TYPE localIndex
Local index type (for indexing objects within an MPI partition).
std::size_t size_t
Unsigned size type.
Array< T, 1 > array1d
Alias for 1D array.
internal::StdVectorWrapper< T, Allocator, USE_STD_CONTAINER_BOUNDS_CHECKING > stdVector
static MPI_Op getMpiOp(Reduction const op)
Returns an MPI_Op associated with our strongly typed Reduction enum.
static int checkAll(int count, MPI_Request array_of_requests[], int *flag, MPI_Status array_of_statuses[])
static int allgatherv(T_SEND const *sendbuf, int sendcount, T_RECV *recvbuf, int *recvcounts, int *displacements, MPI_Comm comm=MPI_COMM_GEOS)
Strongly typed wrapper around MPI_Allgatherv.
static int check(MPI_Request *request, int *flag, MPI_Status *status)
static int activeWaitAny(const int count, MPI_Request array_of_requests[], MPI_Status array_of_statuses[], std::function< MPI_Request(int) > func)
@ Max
Max pair first value.
@ Min
Min pair first value.
static int activeWaitSomeCompletePhase(const int participants, stdVector< std::tuple< MPI_Request *, MPI_Status *, std::function< MPI_Request(int) > > > const &phases)
static int iSend(T const *const buf, int count, int dest, int tag, MPI_Comm comm, MPI_Request *request)
Strongly typed wrapper around MPI_Isend()
static int bcast(T *buffer, int count, int root, MPI_Comm comm=MPI_COMM_GEOS)
Strongly typed wrapper around MPI_Bcast.
static void allGather(T const myValue, array1d< T > &allValues, MPI_Comm comm=MPI_COMM_GEOS)
Convenience function for MPI_Allgather.
static T max(T const &value, MPI_Comm comm=MPI_COMM_GEOS)
Convenience function for a MPI_Allreduce using a MPI_MAX operation.
static int allgather(T_SEND const *sendbuf, int sendcount, T_RECV *recvbuf, int recvcount, MPI_Comm comm=MPI_COMM_GEOS)
Strongly typed wrapper around MPI_Allgather.
static int activeWaitSome(const int count, MPI_Request array_of_requests[], MPI_Status array_of_statuses[], std::function< MPI_Request(int) > func)
static int send(T const *const buf, int count, int dest, int tag, MPI_Comm comm)
Strongly typed wrapper around MPI_Send()
static U prefixSum(T const value, MPI_Comm comm=MPI_COMM_GEOS)
Compute exclusive prefix sum and full sum.
static int scatter(TS const *const sendbuf, int sendcount, TR *const recvbuf, int recvcount, int root, MPI_Comm comm=MPI_COMM_GEOS)
Strongly typed wrapper around MPI_Scatter.
static int gather(T const &value, DST_CONTAINER &destValuesBuffer, int root, MPI_Comm comm=MPI_COMM_GEOS)
Strongly typed wrapper around MPI_Gather().
static T allReduce(T const &value, Reduction const op, MPI_Comm comm=MPI_COMM_GEOS)
Convenience wrapper for the MPI_Allreduce function.
static int checkAny(int count, MPI_Request array_of_requests[], int *idx, int *flag, MPI_Status array_of_statuses[])
static int gather(TS const *const sendbuf, int sendcount, TR *const recvbuf, int recvcount, int root, MPI_Comm comm=MPI_COMM_GEOS)
Strongly typed wrapper around MPI_Gather().
static int scatterv(TS const *const sendbuf, const int *sendcounts, const int *displs, TR *const recvbuf, int recvcount, int root, MPI_Comm comm=MPI_COMM_GEOS)
Strongly typed wrapper around MPI_Scatterv.
static int iRecv(T *const buf, int count, int source, int tag, MPI_Comm comm, MPI_Request *request)
Strongly typed wrapper around MPI_Irecv()
static T sum(T const &value, MPI_Comm comm=MPI_COMM_GEOS)
Convenience function for a MPI_Allreduce using a MPI_SUM operation.
static int activeWaitOrderedCompletePhase(const int participants, stdVector< std::tuple< MPI_Request *, MPI_Status *, std::function< MPI_Request(int) > > > const &phases)
static T min(T const &value, MPI_Comm comm=MPI_COMM_GEOS)
Convenience function for a MPI_Allreduce using a MPI_MIN operation.
static int nodeCommSize()
Compute the number of ranks allocated on the same node.
static int gatherv(TS const *const sendbuf, int sendcount, TR *const recvbuf, const int *recvcounts, const int *displs, int root, MPI_Comm comm=MPI_COMM_GEOS)
Strongly typed wrapper around MPI_Gatherv.
static void finalize()
Free MPI managed resources, then call MPI_Finalize(). Please note that once called,...
static void broadcast(T &value, int srcRank=0, MPI_Comm comm=MPI_COMM_GEOS)
Convenience function for MPI_Broadcast.
static int reduce(T const *sendbuf, T *recvbuf, int count, MPI_Op op, int root, MPI_Comm comm=MPI_COMM_GEOS)
Strongly typed wrapper around MPI_Reduce.
Trait to retrieve the value_type or ValueType of a type T.