In my application I use MPI to distribute jobs in a master-slave fashion. A job is given to a slave and then results are collected. In a multi-threaded version of this program, there was a potential deadlock when all processors simultaneously try to Send
(blocking), because there was no matching Recv
. I came with up with a solution which seems to work, but I would like to have the guarantee (other than by testing it another ten thousand times).
The safety of my program is certain, if this little code is guaranteed to be -- provided a conforming implementation. (obviously this works for two processors only and is not intended for more):
#include <cassert>
#include "mpi.h"
int main()
{
MPI::Init();
int ns[] = {-1, -1};
int rank = MPI::COMM_WORLD.Get_rank();
ns[rank] = rank;
MPI::Request request = MPI::COMM_WORLD.Isend(&rank, sizeof(int), MPI::BYTE, 1 - rank, 0);
MPI::COMM_WORLD.Recv(&ns[1 - rank], sizeof(int), MPI::BYTE, 1 - rank, 0);
request.Wait();
assert( ns[0] == 0 );
assert( ns[1] == 1 );
MPI::Finalize();
}
So my question is: Is the interleaving of an Isend
with a Recv
until I call Wait
on the Request
returned by Isend
a well-defined safe thing in MPI?
(Disclaimer: This piece of code is not designed to be exception-safe or particularly beautiful. It's for demo purposes only)