I am trying to find out in what cases a potentially blocking boost mpi "send" will actually block and causes a deadlock.
#include <boost/mpi.hpp>
#include <iostream>
int main(int argc, char *argv[])
{
boost::mpi::environment env{argc, argv};
boost::mpi::communicator world;
if (world.rank() == 0)
{
char buffer[14];
const char *c = "Hello, world from 1!";
world.send(1, 1, c, 13);
std::cout << "1--11111\n";
world.send(1, 1, c, 13);
std::cout << "1--22222\n";
world.recv(1, 1, buffer, 13);
std::cout << "1--33333\n";
world.recv(1, 1, buffer, 13);
std::cout << "1--44444\n";
buffer[13] = '\0';
std::cout << buffer << "11 \n";
}
else
{
char buffer[14];
const char *c = "Hello, world from 2!";
world.send(0, 1, c, 13);
std::cout << "2--11111\n";
world.send(0, 1, c, 13);
std::cout << "2--22222\n";
world.recv(0, 1, buffer, 13);
std::cout << "2--33333\n";
world.recv(0, 1, buffer, 13);
std::cout << "2--44444\n";
buffer[13] = '\0';
std::cout << buffer << "22 \n";
}
}
but it runs just fine and the with this order:
2--11111
2--22222
1--11111
1--22222
1--33333
1--44444
Hello, world 11
2--33333
2--44444
Hello, world 22
I will be grateful if someone could give me a scenario in which I have actually deadlock. How does the potentially blocking boost mpi works?
Thank you.
Implementations may or may not complete MPI_Send
before the recieve is posted. Your code is not a correct MPI application. It may deadlock with a specific message size, on a specific network, during full moon while it's raining.
In practice it mostly depends on the message size as implementations commonly send small messages to preallocated buffers. There may be a tuning parameter depending on your implementation.
MPI describes several communication modes. The default is blocking1 or standard mode, as described above. A synchronous send (MPI_Ssend
) must wait for the receive to be posted. This will guarantee a deadlock.
A buffered send (MPI_Bsend
) must complete even if the recieve is not posted. In addition to the modes, there are nonblocking or immediate calls that will always complete locally, but may not copy the data yet. Both will guarantee no deadlock. However only the latter is supported by boost.MPI.
1: blocking in the terminology only means that the call blocks until the send buffer can be reused. Either because it was sent, or because it was buffered locally. Also take a look at the standard.