22
33#include < iostream>
44#include < math.h>
5- #include < mpi.h>
65
76using namespace std ;
87
@@ -33,98 +32,132 @@ template<typename T> void mpi_send(int rank, int count, MPI_Datatype type, const
3332 cout << rank << " ->" << dest << " : Sending " << printer (val) << " ." << endl;
3433 MPI_Send (val, count, type, dest, tag, MPI_COMM_WORLD);
3534}
36- template <typename T> T mpi_recvValue (int rank, MPI_Datatype type, int source, int tag, bool debug, auto printer) {
35+ template <typename T> T mpi_recvValue (int rank, MPI_Datatype type, int source, int tag, MPI_Status* status, bool debug, auto printer) {
3736 T val;
38- MPI_Recv (&val, 1 , type, source, tag, MPI_COMM_WORLD, MPI_STATUS_IGNORE );
37+ MPI_Recv (&val, 1 , type, source, tag, MPI_COMM_WORLD, status );
3938 if (debug)
4039 cout << source << " ->" << rank << " : Received " << printer (val) << " ." << endl;
4140 return val;
4241}
43- template <typename T> bool mpi_tryRecvValue (int rank, MPI_Datatype type, int source, int tag, T& result, bool debug, auto printer) {
42+ template <typename T> bool mpi_tryRecvValue (int rank, MPI_Datatype type, int source, int tag, MPI_Status* status, T& result, bool debug, auto printer) {
4443 int flag;
45- MPI_Iprobe (source, tag, MPI_COMM_WORLD, &flag, MPI_STATUS_IGNORE );
44+ MPI_Iprobe (source, tag, MPI_COMM_WORLD, &flag, status );
4645 if (flag) {
47- MPI_Recv (&result, 1 , type, source, tag, MPI_COMM_WORLD, MPI_STATUS_IGNORE );
46+ MPI_Recv (&result, 1 , type, source, tag, MPI_COMM_WORLD, status );
4847 if (debug)
4948 cout << source << " ->" << rank << " : Received " << printer (result) << " ." << endl;
5049 }
5150 return flag;
5251}
53- template <typename T, size_t N> array<T, N> mpi_recvArray (int rank, MPI_Datatype type, int source, int tag, bool debug, auto printer) {
52+ template <typename T, size_t N> array<T, N> mpi_recvArray (int rank, MPI_Datatype type, int source, int tag, MPI_Status* status, bool debug, auto printer) {
5453 array<T, N> arr;
55- MPI_Recv (arr.data (), N, type, source, tag, MPI_COMM_WORLD, MPI_STATUS_IGNORE );
54+ MPI_Recv (arr.data (), N, type, source, tag, MPI_COMM_WORLD, status );
5655 if (debug)
5756 cout << source << " ->" << rank << " : Received " << printer (arr) << " ." << endl;
5857 return arr;
5958}
60- template <typename T, size_t N> bool mpi_tryRecvArray (int rank, MPI_Datatype type, int source, int tag, array<T, N>& result, bool debug, auto printer) {
59+ template <typename T, size_t N> bool mpi_tryRecvArray (int rank, MPI_Datatype type, int source, int tag, MPI_Status* status, array<T, N>& result, bool debug, auto printer) {
6160 int flag;
62- MPI_Iprobe (source, tag, MPI_COMM_WORLD, &flag, MPI_STATUS_IGNORE );
61+ MPI_Iprobe (source, tag, MPI_COMM_WORLD, &flag, status );
6362 if (flag) {
64- MPI_Recv (result.data (), N, type, source, tag, MPI_COMM_WORLD, MPI_STATUS_IGNORE );
63+ MPI_Recv (result.data (), N, type, source, tag, MPI_COMM_WORLD, status );
6564 if (debug)
6665 cout << source << " ->" << rank << " : Received " << printer (result) << " ." << endl;
6766 }
6867 return flag;
6968}
70- template <typename T> ManagedArray<T> mpi_recvDynArray (int rank, MPI_Datatype type, int source, int tag, bool debug, auto printer) {
71- MPI_Status status_recv ;
72- MPI_Probe (source, tag, MPI_COMM_WORLD, &status_recv );
69+ template <typename T> ManagedArray<T> mpi_recvDynArray (int rank, MPI_Datatype type, int source, int tag, MPI_Status* status, bool debug, auto printer) {
70+ MPI_Status status_probe ;
71+ MPI_Probe (source, tag, MPI_COMM_WORLD, &status_probe );
7372 int size;
74- MPI_Get_count (&status_recv , type, &size);
73+ MPI_Get_count (&status_probe , type, &size);
7574 if (debug)
7675 cout << source << " ->" << rank << " : Will receive " << size << " elements." << endl;
7776 ManagedArray<T> arr (size);
78- MPI_Recv (arr.data , size, type, source, tag, MPI_COMM_WORLD, MPI_STATUS_IGNORE );
77+ MPI_Recv (arr.data , size, type, source, tag, MPI_COMM_WORLD, status );
7978 if (debug)
8079 cout << source << " ->" << rank << " : Received " << printer (arr) << " ." << endl;
8180 return arr;
8281}
83- template <typename T> bool mpi_tryRecvDynArray (int rank, MPI_Datatype type, int source, int tag, ManagedArray<T>& result, bool debug, auto printer) {
82+ template <typename T> bool mpi_tryRecvDynArray (int rank, MPI_Datatype type, int source, int tag, MPI_Status* status, ManagedArray<T>& result, bool debug, auto printer) {
8483 int flag;
85- MPI_Status status_recv ;
86- MPI_Iprobe (source, tag, MPI_COMM_WORLD, &flag, &status_recv );
84+ MPI_Status status_probe ;
85+ MPI_Iprobe (source, tag, MPI_COMM_WORLD, &flag, &status_probe );
8786 if (flag) {
8887 int size;
89- MPI_Get_count (&status_recv , type, &size);
88+ MPI_Get_count (&status_probe , type, &size);
9089 if (debug)
9190 cout << source << " ->" << rank << " : Will receive " << size << " elements." << endl;
9291 result.alloc (size);
93- MPI_Recv (result.data , size, type, source, tag, MPI_COMM_WORLD, MPI_STATUS_IGNORE );
92+ MPI_Recv (result.data , size, type, source, tag, MPI_COMM_WORLD, status );
9493 if (debug)
9594 cout << source << " ->" << rank << " : Received " << printer (result) << " ." << endl;
9695 }
9796 return flag;
9897}
9998
100- enum mpi_tag : int {
101- mpi_tag_unspecified = 0 , // not used by any helper function
102- mpi_tag_bool,
103- mpi_tag_string,
104- mpi_tag_uint64,
105- mpi_tag_uint64_pair
106- };
99+ void FctHelper::mpi_sendBool (int rank, const bool b, int dest, int tag, bool debug) {
100+ char c (b);
101+ mpi_send (rank, 1 , MPI_CHAR, &c, dest, tag, debug, [](const char * x) { return *x ? " true" : " false" ; });
102+ }
103+
104+ bool FctHelper::mpi_recvBool (int rank, int source, int tag, MPI_Status* optOut_status, bool debug) {
105+ return mpi_recvValue<char >(rank, MPI_CHAR, source, tag, optOut_status, debug, [](const char x) { return x ? " true" : " false" ; });
106+ }
107+
108+ bool FctHelper::mpi_tryRecvBool (int rank, int source, bool & result, int tag, MPI_Status* optOut_status, bool debug) {
109+ char c;
110+ if (mpi_tryRecvValue (rank, MPI_CHAR, source, tag, optOut_status, c, debug, [](const char x) { return x ? " true" : " false" ; })) {
111+ result = c;
112+ return true ;
113+ }
114+ return false ;
115+ }
116+
117+ void FctHelper::mpi_sendInt (int rank, const int num, int dest, int tag, bool debug) {
118+ mpi_send (rank, 1 , MPI_INT, &num, dest, tag, debug, [](const int * x) { return to_string (*x); });
119+ }
120+
121+ int FctHelper::mpi_recvInt (int rank, int source, int tag, MPI_Status* optOut_status, bool debug) {
122+ return mpi_recvValue<int >(rank, MPI_INT, source, tag, optOut_status, debug, [](const int x) { return to_string (x); });
123+ }
124+
125+ bool FctHelper::mpi_tryRecvInt (int rank, int source, int & result, int tag, MPI_Status* optOut_status, bool debug) {
126+ return mpi_tryRecvValue (rank, MPI_INT, source, tag, optOut_status, result, debug, [](const int x) { return to_string (x); });
127+ }
128+
129+ void FctHelper::mpi_sendUint64 (int rank, const uint64_t num, int dest, int tag, bool debug) {
130+ mpi_send (rank, 1 , MPI_UNSIGNED_LONG_LONG, &num, dest, tag, debug, [](const uint64_t * x) { return to_string (*x); });
131+ }
132+
133+ uint64_t FctHelper::mpi_recvUint64 (int rank, int source, int tag, MPI_Status* optOut_status, bool debug) {
134+ return mpi_recvValue<uint64_t >(rank, MPI_UNSIGNED_LONG_LONG, source, tag, optOut_status, debug, [](const uint64_t x) { return to_string (x); });
135+ }
107136
108- void FctHelper::mpi_sendString (int rank, const string& s, int dest, bool debug) {
137+ bool FctHelper::mpi_tryRecvUint64 (int rank, int source, uint64_t & result, int tag, MPI_Status* optOut_status, bool debug) {
138+ return mpi_tryRecvValue (rank, MPI_UNSIGNED_LONG_LONG, source, tag, optOut_status, result, debug, [](const uint64_t x) { return to_string (x); });
139+ }
140+
141+ void FctHelper::mpi_sendString (int rank, const string& s, int dest, int tag, bool debug) {
109142 // Actually send s.size() + 1 chars, since s.c_str() is null-terminated.
110- mpi_send (rank, static_cast <int >(s.size () + 1 ), MPI_CHAR, s.c_str (), dest, mpi_tag_string , debug, [&](const char * x) {
143+ mpi_send (rank, static_cast <int >(s.size () + 1 ), MPI_CHAR, s.c_str (), dest, tag , debug, [&](const char * x) {
111144 stringstream ss;
112145 ss << " \" " << x << " \" (length " << s.length () << " )" ;
113146 return ss.str ();
114147 });
115148}
116149
117- string FctHelper::mpi_recvString (int rank, int source, bool debug) {
118- return mpi_recvDynArray<char >(rank, MPI_CHAR, source, mpi_tag_string , debug, [&](const ManagedArray<char >& x) {
150+ string FctHelper::mpi_recvString (int rank, int source, int tag, MPI_Status* optOut_status, bool debug) {
151+ return mpi_recvDynArray<char >(rank, MPI_CHAR, source, tag, optOut_status , debug, [&](const ManagedArray<char >& x) {
119152 stringstream ss;
120153 ss << " \" " << x.data << " \" (length " << string (x.data ).length () << " )" ;
121154 return ss.str ();
122155 }).data ;
123156}
124157
125- bool FctHelper::mpi_tryRecvString (int rank, int source, string& result, bool debug) {
158+ bool FctHelper::mpi_tryRecvString (int rank, int source, string& result, int tag, MPI_Status* optOut_status, bool debug) {
126159 ManagedArray<char > arr;
127- if (mpi_tryRecvDynArray (rank, MPI_CHAR, source, mpi_tag_string , arr, debug, [&](const ManagedArray<char >& x) {
160+ if (mpi_tryRecvDynArray (rank, MPI_CHAR, source, tag, optOut_status , arr, debug, [&](const ManagedArray<char >& x) {
128161 stringstream ss;
129162 ss << " \" " << x.data << " \" (length " << string (x.data ).length () << " )" ;
130163 return ss.str ();
@@ -135,50 +168,20 @@ bool FctHelper::mpi_tryRecvString(int rank, int source, string& result, bool deb
135168 return false ;
136169}
137170
138- void FctHelper::mpi_sendBool (int rank, const bool b, int dest, bool debug) {
139- char c (b);
140- mpi_send (rank, 1 , MPI_CHAR, &c, dest, mpi_tag_bool, debug, [](const char * x) { return *x ? " true" : " false" ; });
141- }
142-
143- bool FctHelper::mpi_recvBool (int rank, int source, bool debug) {
144- return mpi_recvValue<char >(rank, MPI_CHAR, source, mpi_tag_bool, debug, [](const char x) { return x ? " true" : " false" ; });
145- }
146-
147- bool FctHelper::mpi_tryRecvBool (int rank, int source, bool & result, bool debug) {
148- char c;
149- if (mpi_tryRecvValue (rank, MPI_CHAR, source, mpi_tag_bool, c, debug, [](const char x) { return x ? " true" : " false" ; })) {
150- result = c;
151- return true ;
152- }
153- return false ;
154- }
155-
156- void FctHelper::mpi_sendUint64 (int rank, const uint64_t num, int dest, bool debug) {
157- mpi_send (rank, 1 , MPI_LONG_LONG_INT, &num, dest, mpi_tag_uint64, debug, [](const uint64_t * x) { return to_string (*x); });
158- }
159-
160- uint64_t FctHelper::mpi_recvUint64 (int rank, int source, bool debug) {
161- return mpi_recvValue<uint64_t >(rank, MPI_LONG_LONG_INT, source, mpi_tag_uint64, debug, [](const uint64_t x) { return to_string (x); });
162- }
163-
164- bool FctHelper::mpi_tryRecvUint64 (int rank, int source, uint64_t & result, bool debug) {
165- return mpi_tryRecvValue (rank, MPI_LONG_LONG_INT, source, mpi_tag_uint64, result, debug, [](const uint64_t x) { return to_string (x); });
166- }
167-
168- void FctHelper::mpi_sendUint64Pair (int rank, const array<uint64_t , 2 >& arr, int dest, bool debug) {
169- mpi_send (rank, 2 , MPI_LONG_LONG_INT, arr.data (), dest, mpi_tag_uint64_pair, debug, [](const uint64_t * x) {
171+ void FctHelper::mpi_sendPairUint64 (int rank, const array<uint64_t , 2 >& arr, int dest, int tag, bool debug) {
172+ mpi_send (rank, 2 , MPI_UNSIGNED_LONG_LONG, arr.data (), dest, tag, debug, [](const uint64_t * x) {
170173 return " (" + to_string (x[0 ]) + " , " + to_string (x[1 ]) + " )" ;
171174 });
172175}
173176
174- array<uint64_t , 2 > FctHelper::mpi_recvUint64Pair (int rank, int source, bool debug) {
175- return mpi_recvArray<uint64_t , 2 >(rank, MPI_LONG_LONG_INT , source, mpi_tag_uint64_pair , debug, [](const array<uint64_t , 2 >& x) {
177+ array<uint64_t , 2 > FctHelper::mpi_recvPairUint64 (int rank, int source, int tag, MPI_Status* optOut_status , bool debug) {
178+ return mpi_recvArray<uint64_t , 2 >(rank, MPI_UNSIGNED_LONG_LONG , source, tag, optOut_status , debug, [](const array<uint64_t , 2 >& x) {
176179 return " (" + to_string (x[0 ]) + " , " + to_string (x[1 ]) + " )" ;
177180 });
178181}
179182
180- bool FctHelper::mpi_tryRecvUint64Pair (int rank, int source, array<uint64_t , 2 >& result, bool debug) {
181- return mpi_tryRecvArray (rank, MPI_LONG_LONG_INT , source, mpi_tag_uint64_pair , result, debug, [](const array<uint64_t , 2 >& x) {
183+ bool FctHelper::mpi_tryRecvPairUint64 (int rank, int source, array<uint64_t , 2 >& result, int tag, MPI_Status* optOut_status , bool debug) {
184+ return mpi_tryRecvArray (rank, MPI_UNSIGNED_LONG_LONG , source, tag, optOut_status , result, debug, [](const array<uint64_t , 2 >& x) {
182185 return " (" + to_string (x[0 ]) + " , " + to_string (x[1 ]) + " )" ;
183186 });
184187}
0 commit comments