18 #ifndef GEOS_COMMON_MPIWRAPPER_HPP_
19 #define GEOS_COMMON_MPIWRAPPER_HPP_
24 #if defined(GEOSX_USE_MPI)
26 #define MPI_PARAM( x ) x
28 #define MPI_PARAM( x )
31 #define MPI_COMM_NULL ((MPI_Comm)0x04000000)
32 #define MPI_COMM_WORLD ((MPI_Comm)0x44000000)
33 #define MPI_COMM_SELF ((MPI_Comm)0x40000000)
36 typedef int MPI_Datatype;
37 #define MPI_CHAR ((MPI_Datatype)0x4c000101)
38 #define MPI_SIGNED_CHAR ((MPI_Datatype)0x4c000118)
39 #define MPI_UNSIGNED_CHAR ((MPI_Datatype)0x4c000102)
40 #define MPI_BYTE ((MPI_Datatype)0x4c00010d)
41 #define MPI_WCHAR ((MPI_Datatype)0x4c00040e)
42 #define MPI_SHORT ((MPI_Datatype)0x4c000203)
43 #define MPI_UNSIGNED_SHORT ((MPI_Datatype)0x4c000204)
44 #define MPI_INT ((MPI_Datatype)0x4c000405)
45 #define MPI_UNSIGNED ((MPI_Datatype)0x4c000406)
46 #define MPI_LONG ((MPI_Datatype)0x4c000807)
47 #define MPI_UNSIGNED_LONG ((MPI_Datatype)0x4c000808)
48 #define MPI_FLOAT ((MPI_Datatype)0x4c00040a)
49 #define MPI_DOUBLE ((MPI_Datatype)0x4c00080b)
50 #define MPI_LONG_DOUBLE ((MPI_Datatype)0x4c00100c)
51 #define MPI_LONG_LONG_INT ((MPI_Datatype)0x4c000809)
52 #define MPI_UNSIGNED_LONG_LONG ((MPI_Datatype)0x4c000819)
53 #define MPI_LONG_LONG MPI_LONG_LONG_INT
57 #define MPI_MAX (MPI_Op)(0x58000001)
58 #define MPI_MIN (MPI_Op)(0x58000002)
59 #define MPI_SUM (MPI_Op)(0x58000003)
60 #define MPI_PROD (MPI_Op)(0x58000004)
61 #define MPI_LAND (MPI_Op)(0x58000005)
62 #define MPI_BAND (MPI_Op)(0x58000006)
63 #define MPI_LOR (MPI_Op)(0x58000007)
64 #define MPI_BOR (MPI_Op)(0x58000008)
65 #define MPI_LXOR (MPI_Op)(0x58000009)
66 #define MPI_BXOR (MPI_Op)(0x5800000a)
67 #define MPI_MINLOC (MPI_Op)(0x5800000b)
68 #define MPI_MAXLOC (MPI_Op)(0x5800000c)
69 #define MPI_REPLACE (MPI_Op)(0x5800000d)
70 #define MPI_NO_OP (MPI_Op)(0x5800000e)
73 #define MPI_UNDEFINED (-32766)
74 #define MPI_STATUS_IGNORE (MPI_Status *)1
75 #define MPI_STATUSES_IGNORE (MPI_Status *)1
76 #define MPI_REQUEST_NULL ((MPI_Request)0x2c000000)
77 typedef int MPI_Request;
80 #define MPI_INFO_NULL (MPI_Info)(0x60000000)
90 #define MPI_CHECK_ERROR( error ) ((void) error)
92 #define MPI_CHECK_ERROR( error ) GEOS_ERROR_IF_NE( error, MPI_SUCCESS );
140 static void barrier( MPI_Comm
const & MPI_PARAM( comm )=
MPI_COMM_GEOSX );
142 static int cartCoords( MPI_Comm comm,
int rank,
int maxdims,
int coords[] );
144 static int cartCreate( MPI_Comm comm_old,
int ndims,
const int dims[],
const int periods[],
145 int reorder, MPI_Comm * comm_cart );
147 static int cartRank( MPI_Comm comm,
const int coords[] );
149 static void commFree( MPI_Comm & comm );
151 static int commRank( MPI_Comm
const & MPI_PARAM( comm )=
MPI_COMM_GEOSX );
153 static int commSize( MPI_Comm
const & MPI_PARAM( comm )=
MPI_COMM_GEOSX );
155 static bool commCompare( MPI_Comm
const & comm1, MPI_Comm
const & comm2 );
157 static bool initialized();
159 static int init(
int * argc,
char * * * argv );
161 static void finalize();
163 static MPI_Comm commDup( MPI_Comm
const comm );
165 static MPI_Comm commSplit( MPI_Comm
const comm,
int color,
int key );
167 static int test( MPI_Request * request,
int * flag,
MPI_Status * status );
169 static int testAny(
int count, MPI_Request array_of_requests[],
int * idx,
int * flags,
MPI_Status array_of_statuses[] );
171 static int testSome(
int count, MPI_Request array_of_requests[],
int * outcount,
int array_of_indices[],
MPI_Status array_of_statuses[] );
173 static int testAll(
int count, MPI_Request array_of_requests[],
int * flags,
MPI_Status array_of_statuses[] );
195 static int checkAny(
int count, MPI_Request array_of_requests[],
int * idx,
int * flag,
MPI_Status array_of_statuses[] );
206 static int checkAll(
int count, MPI_Request array_of_requests[],
int * flag,
MPI_Status array_of_statuses[] );
208 static int wait( MPI_Request * request,
MPI_Status * status );
210 static int waitAny(
int count, MPI_Request array_of_requests[],
int * indx,
MPI_Status array_of_statuses[] );
212 static int waitSome(
int count, MPI_Request array_of_requests[],
int * outcount,
int array_of_indices[],
MPI_Status array_of_statuses[] );
214 static int waitAll(
int count, MPI_Request array_of_requests[],
MPI_Status array_of_statuses[] );
216 static double wtime(
void );
229 MPI_Request array_of_requests[],
231 std::function< MPI_Request (
int ) > func );
243 MPI_Request array_of_requests[],
245 std::function< MPI_Request (
int ) > func );
260 std::vector< std::tuple< MPI_Request *,
MPI_Status *, std::function< MPI_Request (
int ) > > >
const & phases );
276 std::vector< std::tuple< MPI_Request *,
MPI_Status *, std::function< MPI_Request (
int ) > > >
const & phases );
279 #if !defined(GEOSX_USE_MPI)
280 static std::map< int, std::pair< int, void * > > & getTagToPointersMap()
282 static std::map< int, std::pair< int, void * > > tagToPointers;
283 return tagToPointers;
304 template<
typename T_SEND,
typename T_RECV >
323 template<
typename T_SEND,
typename T_RECV >
337 template<
typename T >
340 template<
typename T >
354 template<
typename T >
365 template<
typename T >
376 template<
typename T >
389 template<
typename T >
390 static int reduce( T
const * sendbuf, T * recvbuf,
int count, MPI_Op op,
int root, MPI_Comm comm =
MPI_COMM_GEOSX );
400 template<
typename T >
411 template<
typename T >
415 template<
typename T >
416 static int scan( T
const * sendbuf, T * recvbuf,
int count, MPI_Op op, MPI_Comm comm );
418 template<
typename T >
419 static int exscan( T
const * sendbuf, T * recvbuf,
int count, MPI_Op op, MPI_Comm comm );
429 template<
typename T >
430 static int bcast( T * buffer,
int count,
int root, MPI_Comm comm );
439 template<
typename T >
454 template<
typename TS,
typename TR >
455 static int gather( TS
const *
const sendbuf,
476 template<
typename TS,
typename TR >
480 const int * recvcounts,
492 template<
typename T >
494 int MPI_PARAM( source ),
496 MPI_Comm MPI_PARAM( comm ),
499 template<
typename T >
501 int MPI_PARAM( dest ),
503 MPI_Comm MPI_PARAM( comm ),
504 MPI_Request * MPI_PARAM( request ) );
516 template<
typename T >
522 MPI_Request * request );
534 template<
typename T >
535 static int iSend( T
const *
const buf,
540 MPI_Request * request );
549 template<
typename U,
typename T >
557 template<
typename T >
566 template<
typename T >
574 template<
typename T >
583 template<
typename T >
591 template<
typename T >
600 template<
typename T >
607 template<
typename T,
typename ENABLE =
void >
608 struct MpiTypeImpl {};
610 #define ADD_MPI_TYPE_MAP( T, MPI_T ) \
611 template<> struct MpiTypeImpl< T > { static MPI_Datatype get() { return MPI_T; } }
613 ADD_MPI_TYPE_MAP(
float, MPI_FLOAT );
614 ADD_MPI_TYPE_MAP(
double, MPI_DOUBLE );
616 ADD_MPI_TYPE_MAP(
char, MPI_CHAR );
617 ADD_MPI_TYPE_MAP(
signed char, MPI_SIGNED_CHAR );
618 ADD_MPI_TYPE_MAP(
unsigned char, MPI_UNSIGNED_CHAR );
620 ADD_MPI_TYPE_MAP(
int, MPI_INT );
621 ADD_MPI_TYPE_MAP(
long int, MPI_LONG );
622 ADD_MPI_TYPE_MAP(
long long int, MPI_LONG_LONG );
624 ADD_MPI_TYPE_MAP(
unsigned int, MPI_UNSIGNED );
625 ADD_MPI_TYPE_MAP(
unsigned long int, MPI_UNSIGNED_LONG );
626 ADD_MPI_TYPE_MAP(
unsigned long long int, MPI_UNSIGNED_LONG_LONG );
628 #undef ADD_MPI_TYPE_MAP
630 template<
typename T >
631 struct MpiTypeImpl< T, std::enable_if_t< std::is_enum< T >::value > >
633 static MPI_Datatype get() {
return MpiTypeImpl< std::underlying_type_t< T > >::get(); }
636 template<
typename T >
637 MPI_Datatype getMpiType()
639 return MpiTypeImpl< T >::get();
665 GEOS_ERROR(
"Unsupported reduction operation" );
670 template<
typename T_SEND,
typename T_RECV >
673 T_RECV *
const recvbuf,
675 MPI_Comm MPI_PARAM( comm ) )
678 return MPI_Allgather( sendbuf, sendcount, internal::getMpiType< T_SEND >(),
679 recvbuf, recvcount, internal::getMpiType< T_RECV >(),
682 static_assert( std::is_same< T_SEND, T_RECV >::value,
683 "MpiWrapper::allgather() for serial run requires send and receive buffers are of the same type" );
685 std::copy( sendbuf, sendbuf + sendcount, recvbuf )
690 template<
typename T_SEND,
typename T_RECV >
693 T_RECV *
const recvbuf,
696 MPI_Comm MPI_PARAM( comm ) )
699 return MPI_Allgatherv( sendbuf, sendcount, internal::getMpiType< T_SEND >(),
700 recvbuf, recvcounts, displacements, internal::getMpiType< T_RECV >(),
703 static_assert( std::is_same< T_SEND, T_RECV >::value,
704 "MpiWrapper::allgatherv() for serial run requires send and receive buffers are of the same type" );
706 std::copy( sendbuf, sendbuf + sendcount, recvbuf )
712 template<
typename T >
716 int const mpiSize = commSize( comm );
717 allValues.resize( mpiSize );
719 MPI_Datatype
const MPI_TYPE = internal::getMpiType< T >();
721 MPI_Allgather( &myValue, 1, MPI_TYPE, allValues.data(), 1, MPI_TYPE, comm );
724 allValues.resize( 1 );
725 allValues[0] = myValue;
729 template<
typename T >
731 array1d< T > & allValues,
732 MPI_Comm MPI_PARAM( comm ) )
734 int const sendSize = LvArray::integerConversion< int >( sendValues.size() );
736 int const mpiSize = commSize( comm );
737 allValues.resize( mpiSize * sendSize );
738 return MPI_Allgather( sendValues.data(),
740 internal::getMpiType< T >(),
743 internal::getMpiType< T >(),
747 allValues.resize( sendSize );
750 allValues[a] = sendValues[a];
756 template<
typename T >
760 MPI_Op
const MPI_PARAM( op ),
761 MPI_Comm
const MPI_PARAM( comm ) )
764 MPI_Datatype
const mpiType = internal::getMpiType< T >();
765 return MPI_Allreduce( sendbuf == recvbuf ? MPI_IN_PLACE : sendbuf, recvbuf, count, mpiType, op, comm );
767 if( sendbuf != recvbuf )
769 memcpy( recvbuf, sendbuf, count *
sizeof( T ) );
775 template<
typename T >
779 MPI_Op
const MPI_PARAM( op ),
781 MPI_Comm
const MPI_PARAM( comm ) )
784 MPI_Datatype
const mpiType = internal::getMpiType< T >();
785 return MPI_Reduce( sendbuf == recvbuf ? MPI_IN_PLACE : sendbuf, recvbuf, count, mpiType, op, root, comm );
787 if( sendbuf != recvbuf )
789 memcpy( recvbuf, sendbuf, count *
sizeof( T ) );
795 template<
typename T >
796 int MpiWrapper::scan( T
const *
const sendbuf,
799 MPI_Op MPI_PARAM( op ),
800 MPI_Comm MPI_PARAM( comm ) )
803 return MPI_Scan( sendbuf, recvbuf, count, internal::getMpiType< T >(), op, comm );
805 memcpy( recvbuf, sendbuf, count*
sizeof(T) );
810 template<
typename T >
811 int MpiWrapper::exscan( T
const *
const MPI_PARAM( sendbuf ),
814 MPI_Op MPI_PARAM( op ),
815 MPI_Comm MPI_PARAM( comm ) )
818 return MPI_Exscan( sendbuf, recvbuf, count, internal::getMpiType< T >(), op, comm );
820 memset( recvbuf, 0, count*
sizeof(T) );
825 template<
typename T >
827 int MPI_PARAM( count ),
828 int MPI_PARAM( root ),
829 MPI_Comm MPI_PARAM( comm ) )
832 return MPI_Bcast( buffer, count, internal::getMpiType< T >(), root, comm );
839 template<
typename T >
840 void MpiWrapper::broadcast( T & MPI_PARAM( value ),
int MPI_PARAM( srcRank ), MPI_Comm MPI_PARAM( comm ) )
843 MPI_Bcast( &value, 1, internal::getMpiType< T >(), srcRank, comm );
849 void MpiWrapper::broadcast< string >(
string & MPI_PARAM( value ),
850 int MPI_PARAM( srcRank ),
851 MPI_Comm MPI_PARAM( comm ) )
854 int size = LvArray::integerConversion< int >( value.size() );
856 value.resize( size );
857 MPI_Bcast(
const_cast< char *
>( value.data() ), size, internal::getMpiType< char >(), srcRank, comm );
861 template<
typename TS,
typename TR >
866 int MPI_PARAM( root ),
867 MPI_Comm MPI_PARAM( comm ) )
870 return MPI_Gather( sendbuf, sendcount, internal::getMpiType< TS >(),
871 recvbuf, recvcount, internal::getMpiType< TR >(),
874 static_assert( std::is_same< TS, TR >::value,
875 "MpiWrapper::gather() for serial run requires send and receive buffers are of the same type" );
876 std::size_t const sendBufferSize = sendcount *
sizeof(TS);
877 std::size_t const recvBufferSize = recvcount *
sizeof(TR);
878 GEOS_ERROR_IF_NE_MSG( sendBufferSize, recvBufferSize,
"size of send buffer and receive buffer are not equal" );
879 memcpy( recvbuf, sendbuf, sendBufferSize );
884 template<
typename TS,
typename TR >
888 const int * recvcounts,
889 const int * MPI_PARAM( displs ),
890 int MPI_PARAM( root ),
891 MPI_Comm MPI_PARAM( comm ) )
894 return MPI_Gatherv( sendbuf, sendcount, internal::getMpiType< TS >(),
895 recvbuf, recvcounts, displs, internal::getMpiType< TR >(),
898 static_assert( std::is_same< TS, TR >::value,
899 "MpiWrapper::gather() for serial run requires send and receive buffers are of the same type" );
900 std::size_t const sendBufferSize = sendcount *
sizeof(TS);
901 std::size_t const recvBufferSize = recvcounts[0] *
sizeof(TR);
902 GEOS_ERROR_IF_NE_MSG( sendBufferSize, recvBufferSize,
"size of send buffer and receive buffer are not equal" );
903 memcpy( recvbuf, sendbuf, sendBufferSize );
908 template<
typename T >
911 int MPI_PARAM( source ),
913 MPI_Comm MPI_PARAM( comm ),
914 MPI_Request * MPI_PARAM( request ) )
918 "Attempting to use an MPI_Request that is still in use." );
919 return MPI_Irecv( buf, count, internal::getMpiType< T >(), source, tag, comm, request );
921 std::map< int, std::pair< int, void * > > & pointerMap = getTagToPointersMap();
922 std::map< int, std::pair< int, void * > >::iterator iPointer = pointerMap.find( tag );
924 if( iPointer==pointerMap.end() )
926 pointerMap.insert( {tag, {1, buf} } );
931 "Tag does is assigned, but pointer was not set by iSend." );
932 memcpy( buf, iPointer->second.second, count*
sizeof(T) );
933 pointerMap.erase( iPointer );
939 template<
typename T >
940 int MpiWrapper::recv( array1d< T > & buf,
941 int MPI_PARAM( source ),
943 MPI_Comm MPI_PARAM( comm ),
949 MPI_Probe( source, tag, comm, &status );
950 MPI_Get_count( &status, MPI_CHAR, &count );
953 buf.resize( count /
sizeof( T ) );
955 return MPI_Recv(
reinterpret_cast< char *
>( buf.data() ),
968 template<
typename T >
969 int MpiWrapper::iSend( arrayView1d< T >
const & buf,
970 int MPI_PARAM( dest ),
972 MPI_Comm MPI_PARAM( comm ),
973 MPI_Request * MPI_PARAM( request ) )
977 "Attempting to use an MPI_Request that is still in use." );
978 return MPI_Isend(
reinterpret_cast< void const *
>( buf.data() ),
979 buf.size() *
sizeof( T ),
991 template<
typename T >
992 int MpiWrapper::iSend( T
const *
const buf,
994 int MPI_PARAM( dest ),
996 MPI_Comm MPI_PARAM( comm ),
997 MPI_Request * MPI_PARAM( request ) )
1001 "Attempting to use an MPI_Request that is still in use." );
1002 return MPI_Isend( buf, count, internal::getMpiType< T >(), dest, tag, comm, request );
1004 std::map< int, std::pair< int, void * > > & pointerMap = getTagToPointersMap();
1005 std::map< int, std::pair< int, void * > >::iterator iPointer = pointerMap.find( tag );
1007 if( iPointer==pointerMap.end() )
1009 pointerMap.insert( {tag, {0,
const_cast< T *
>(buf)}
1015 "Tag does is assigned, but pointer was not set by iRecv." );
1016 memcpy( iPointer->second.second, buf, count*
sizeof(T) );
1017 pointerMap.erase( iPointer );
1023 template<
typename U,
typename T >
1028 #ifdef GEOSX_USE_MPI
1029 U
const convertedValue = value;
1030 int const error = MPI_Exscan( &convertedValue, &localResult, 1, internal::getMpiType< U >(), MPI_SUM, comm );
1031 MPI_CHECK_ERROR( error );
1033 if( commRank() == 0 )
1042 template<
typename T >
1050 template<
typename T >
1057 template<
typename T >
1063 template<
typename T >
1069 template<
typename T >
1075 template<
typename T >
1081 template<
typename T >
1087 template<
typename T >
1094 template<
typename T >
1102 template<
typename T >
#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_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_GEOSX
Global MPI communicator used by GEOSX.
std::size_t size_t
Unsigned size type.
GEOSX_LOCALINDEX_TYPE localIndex
Local index type (for indexing objects within an MPI partition).
Array< T, 1 > array1d
Alias for 1D array.
static int allgatherv(T_SEND const *sendbuf, int sendcount, T_RECV *recvbuf, int *recvcounts, int *displacements, MPI_Comm comm)
Strongly typed wrapper around MPI_Allgatherv.
static int bcast(T *buffer, int count, int root, MPI_Comm comm)
Strongly typed wrapper around MPI_Bcast.
static MPI_Op getMpiOp(Reduction const op)
Returns an MPI_Op associated with our strongly typed Reduction enum.
static int activeWaitSomeCompletePhase(const int participants, std::vector< std::tuple< MPI_Request *, MPI_Status *, std::function< MPI_Request(int) > > > const &phases)
static int checkAll(int count, MPI_Request array_of_requests[], int *flag, MPI_Status array_of_statuses[])
static int activeWaitOrderedCompletePhase(const int participants, std::vector< std::tuple< MPI_Request *, MPI_Status *, std::function< MPI_Request(int) > > > const &phases)
static int gather(TS const *const sendbuf, int sendcount, TR *const recvbuf, int recvcount, int root, MPI_Comm comm)
Strongly typed wrapper around MPI_Gather().
static int gatherv(TS const *const sendbuf, int sendcount, TR *const recvbuf, const int *recvcounts, const int *displs, int root, MPI_Comm comm)
Strongly typed wrapper around MPI_Gatherv.
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)
static int reduce(T const *sendbuf, T *recvbuf, int count, MPI_Op op, int root, MPI_Comm comm=MPI_COMM_GEOSX)
Strongly typed wrapper around MPI_Reduce.
static void allGather(T const myValue, array1d< T > &allValues, MPI_Comm comm=MPI_COMM_GEOSX)
Convenience function for MPI_Allgather.
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 allgather(T_SEND const *sendbuf, int sendcount, T_RECV *recvbuf, int recvcount, MPI_Comm comm)
Strongly typed wrapper around MPI_Allgather.
static T sum(T const &value, MPI_Comm comm=MPI_COMM_GEOSX)
Convenience function for a MPI_Allreduce using a MPI_SUM operation.
static int activeWaitSome(const int count, MPI_Request array_of_requests[], MPI_Status array_of_statuses[], std::function< MPI_Request(int) > func)
static T max(T const &value, MPI_Comm comm=MPI_COMM_GEOSX)
Convenience function for a MPI_Allreduce using a MPI_MAX operation.
static T min(T const &value, MPI_Comm comm=MPI_COMM_GEOSX)
Convenience function for a MPI_Allreduce using a MPI_MIN operation.
static int checkAny(int count, MPI_Request array_of_requests[], int *idx, int *flag, MPI_Status array_of_statuses[])
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 void broadcast(T &value, int srcRank=0, MPI_Comm comm=MPI_COMM_GEOSX)
Convenience function for MPI_Broadcast.
static int allReduce(T const *sendbuf, T *recvbuf, int count, MPI_Op op, MPI_Comm comm=MPI_COMM_GEOSX)
Strongly typed wrapper around MPI_Allreduce.
static U prefixSum(T const value, MPI_Comm comm=MPI_COMM_GEOSX)
Compute exclusive prefix sum and full sum.
static int nodeCommSize()
Compute the number of ranks allocated on the same node.