Skip to content

Commit 6086c29

Browse files
committed
mpi: made MPI-error-checking macro an inline function
this is a little more C++-like and IMHO a little nicer Signed-off-by: Torbjörn Klatt <[email protected]>
1 parent 7b9095d commit 6086c29

File tree

3 files changed

+35
-20
lines changed

3 files changed

+35
-20
lines changed

include/pfasst/mpi_communicator.hpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,19 @@ namespace pfasst
4747
static MPIError from_code(const int err_code);
4848
};
4949

50+
/**
51+
* checks MPI error code
52+
*
53+
* In case @p err_code is not `MPI_SUCCESS` this throws MPIError with the error code looked up
54+
* to a descriptive string as defined by the MPI implementation.
55+
*/
56+
inline static void check_mpi_error(const int err_code)
57+
{
58+
if (err_code != MPI_SUCCESS) {
59+
throw MPIError::from_code(err_code);
60+
}
61+
}
62+
5063

5164
// forward declare for MPICommunicator
5265
class MPIStatus;

src/pfasst/encap/vector_impl.hpp

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,6 @@
33

44
#include "pfasst/encap/vector.hpp"
55

6-
#ifdef WITH_MPI
7-
#define CHKMPIERR(err) if (err != MPI_SUCCESS) { throw MPIError::from_code(err); }
8-
#endif
9-
106
namespace pfasst
117
{
128
namespace encap
@@ -45,7 +41,7 @@ namespace pfasst
4541
if (this->send_request != MPI_REQUEST_NULL) {
4642
MPI_Status stat;
4743
int err = MPI_Wait(&(this->send_request), &stat);
48-
CHKMPIERR(err);
44+
check_mpi_error(err);
4945
}
5046
assert(this->recv_request == MPI_REQUEST_NULL);
5147
assert(this->send_request == MPI_REQUEST_NULL);
@@ -191,13 +187,13 @@ namespace pfasst
191187
if (mpi.rank() == 0) { return; }
192188

193189
if (this->recv_request != MPI_REQUEST_NULL) {
194-
throw MPIError();
190+
throw MPIError("a previous recieve request is still open");
195191
}
196192

197193
int src = (mpi.rank() - 1) % mpi.size();
198194
int err = MPI_Irecv(this->data(), sizeof(scalar) * this->size(), MPI_CHAR,
199195
src, tag, mpi.comm, &this->recv_request);
200-
CHKMPIERR(err);
196+
check_mpi_error(err);
201197
}
202198

203199
template<typename scalar, typename time>
@@ -214,11 +210,12 @@ namespace pfasst
214210
int src = (mpi.rank() - 1) % mpi.size();
215211
err = MPI_Recv(this->data(), sizeof(scalar) * this->size(), MPI_CHAR,
216212
src, tag, mpi.comm, &stat);
217-
CHKMPIERR(err);
213+
check_mpi_error(err);
218214
} else {
219215
if (this->recv_request != MPI_REQUEST_NULL) {
220216
CLOG(DEBUG, "Encap") << "waiting on last recv request";
221-
err = MPI_Wait(&(this->recv_request), &stat); CHKMPIERR(err);
217+
err = MPI_Wait(&(this->recv_request), &stat);
218+
check_mpi_error(err);
222219
CLOG(DEBUG, "Encap") << "waiting done: " << stat;
223220
}
224221
}
@@ -237,24 +234,23 @@ namespace pfasst
237234

238235
if (blocking) {
239236
err = MPI_Send(this->data(), sizeof(scalar) * this->size(), MPI_CHAR, dest, tag, mpi.comm);
240-
CHKMPIERR(err);
237+
check_mpi_error(err);
241238
} else {
242239
err = MPI_Wait(&(this->send_request), &stat);
243-
CHKMPIERR(err);
240+
check_mpi_error(err);
244241
err = MPI_Isend(this->data(), sizeof(scalar) * this->size(), MPI_CHAR,
245242
dest, tag, mpi.comm, &(this->send_request));
246-
CHKMPIERR(err);
243+
check_mpi_error(err);
247244
}
248-
249245
}
250246

251247
template<typename scalar, typename time>
252248
void VectorEncapsulation<scalar, time>::broadcast(ICommunicator* comm)
253249
{
254250
auto& mpi = as_mpi(comm);
255251
int err = MPI_Bcast(this->data(), sizeof(scalar) * this->size(), MPI_CHAR,
256-
comm->size()-1, mpi.comm); CHKMPIERR(err);
257-
CHKMPIERR(err);
252+
comm->size()-1, mpi.comm);
253+
check_mpi_error(err);
258254
}
259255
#endif
260256

src/pfasst/mpi_communicator_impl.hpp

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ namespace pfasst
2020
{
2121
char err_str[MPI_MAX_ERROR_STRING];
2222
int err_len = 0;
23-
MPI_Error_string(err_code, err_str, &err_len);
23+
int err = MPI_Error_string(err_code, err_str, &err_len);
24+
check_mpi_error(err);
2425
return MPIError("MPI Error: " + string(err_str, err_len) + " (code=" + to_string(err_code) + ")");
2526
}
2627

@@ -40,7 +41,8 @@ namespace pfasst
4041
MPI_Comm_rank(this->comm, &(this->_rank));
4142
int len = 0;
4243
char buff[MPI_MAX_OBJECT_NAME];
43-
MPI_Comm_get_name(this->comm, buff, &len);
44+
int err = MPI_Comm_get_name(this->comm, buff, &len);
45+
check_mpi_error(err);
4446
if (len == 0) {
4547
this->_name = string("world");
4648
} else {
@@ -109,7 +111,7 @@ namespace pfasst
109111
int dest_rank = (mpi->rank() + 1) % mpi->size();
110112

111113
int err = MPI_Send(&iconverged, sizeof(int), MPI_INT, dest_rank, 1, mpi->comm);
112-
if (err != MPI_SUCCESS) { throw MPIError::from_code(err); }
114+
check_mpi_error(err);
113115
}
114116

115117
void MPIStatus::recv()
@@ -127,7 +129,7 @@ namespace pfasst
127129
int iconverged;
128130
int src_rank = (mpi->rank() - 1) % mpi->size();
129131
int err = MPI_Recv(&iconverged, sizeof(iconverged), MPI_INT, src_rank, 1, mpi->comm, &stat);
130-
if (err != MPI_SUCCESS) { throw MPIError::from_code(err); }
132+
check_mpi_error(err);
131133

132134
converged.at(mpi->rank() - 1) = (iconverged == IStatus::CONVERGED) ? true : false;
133135
}
@@ -142,9 +144,13 @@ MAKE_LOGGABLE(MPI_Status, mpi_status, os)
142144
&& mpi_status.MPI_ERROR == MPI_SUCCESS) {
143145
os << "MPI_Status(empty)";
144146
} else {
147+
char err_str[MPI_MAX_ERROR_STRING];
148+
int err_len = 0;
149+
int err = MPI_Error_string(mpi_status.MPI_ERROR, err_str, &err_len);
150+
pfasst::mpi::check_mpi_error(err);
145151
os << "MPI_Status(source=" << to_string(mpi_status.MPI_SOURCE) << ", "
146152
<< "tag=" << to_string(mpi_status.MPI_TAG) << ", "
147-
<< "error=" << to_string(mpi_status.MPI_ERROR) << ")";
153+
<< "error=" << string(err_str, err_len) << ")";
148154
}
149155
return os;
150156
}

0 commit comments

Comments
 (0)