OAOzden Akinci
Writing / MPI, Parallel programming

The MPI exchange that works on a small mesh and hangs on a large one

A simulation runs fine on the test case. You give it the production mesh, and it stops: no error, no crash, all processes at 100% CPU, nothing moving. Very often the cause is four lines of point-to-point communication that were never safe in the first place. They just happened to work while the messages were small.

This post is based on a lecture I gave on point-to-point communication. I ran every example for this article with Open MPI 4.1.

The pattern

Two processes exchange a buffer. Each one sends first, then receives:

if (rank == 0) {
    MPI_Send(a, count, MPI_DOUBLE, 1, 17, MPI_COMM_WORLD);
    MPI_Recv(b, count, MPI_DOUBLE, 1, 19, MPI_COMM_WORLD, &status);
} else if (rank == 1) {
    MPI_Send(a, count, MPI_DOUBLE, 0, 19, MPI_COMM_WORLD);
    MPI_Recv(b, count, MPI_DOUBLE, 0, 17, MPI_COMM_WORLD, &status);
}

It looks symmetric and harmless, and in a halo exchange it is exactly what most people write first. Whether it finishes depends on something the MPI standard deliberately leaves open: whether MPI_Send returns before the matching receive has been posted.

Eager and rendezvous

MPI_Send is the standard mode send. The standard allows an implementation to do either of two things:

  • Eager: copy a small message into a buffer, either locally or on the receiver, and return at once. The sender can carry on, and the data waits until the receive is posted.
  • Rendezvous: for larger messages, wait until the receiver has posted the matching receive, then transfer the data straight into the user’s buffer. The send behaves like a synchronous send.

The switch point is the eager limit. Below it, both ranks’ sends return, both receives are then posted, and the exchange completes. Above it, both ranks sit inside MPI_Send waiting for a receive that the other rank will only post after its own send returns. That is a deadlock.

Measuring where it breaks

The exercise from the lecture takes the message length on the command line. On one node with two ranks, using Open MPI’s shared-memory transport:

count = 505 doubles (4,040 bytes)   completes
count = 506 doubles (4,048 bytes)   hangs

The shared-memory transport’s default eager limit is 4,096 bytes (btl_vader_eager_limit), and part of that is taken by the message header. Raise the limit and the cliff moves with it:

# limit raised to 64 KiB
mpirun --mca btl_vader_eager_limit 65536 -np 2 ./dependBuffer -c 8000  # OK
mpirun --mca btl_vader_eager_limit 65536 -np 2 ./dependBuffer -c 8192  # hangs

The exact number depends on the MPI library, the transport and the version. Other libraries have their own defaults and settings, for example I_MPI_EAGER_THRESHOLD in Intel MPI. Across nodes the limit is different again from within a node. So a code that has always worked can start hanging after a library upgrade, on a different interconnect, or when a job is spread over more nodes. Tuning the eager limit only moves the problem somewhere else.

Finding unsafe code quickly

Temporarily replace MPI_Send with MPI_Ssend. A synchronous send never completes before the matching receive has started, so it behaves as if the eager limit were zero:

MPI_Ssend, count = 1   hangs

If your code survives a run with MPI_Ssend, its point-to-point pattern does not depend on buffering. It is a cheap test to add to a debug build.

Three safe ways to write the exchange

All three versions below swap ten million doubles (80 MB) between two ranks without a problem. The full program is linked at the end of this post.

1. Break the symmetry. One rank sends first, the other receives first. This is simple and always safe, but in a ring or a grid you have to choose the order carefully, for example by sending first on even ranks and receiving first on odd ones.

if (rank == 0) {
    MPI_Send(out, count, MPI_DOUBLE, other, 0, MPI_COMM_WORLD);
    MPI_Recv(in,  count, MPI_DOUBLE, other, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
} else {
    MPI_Recv(in,  count, MPI_DOUBLE, other, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
    MPI_Send(out, count, MPI_DOUBLE, other, 0, MPI_COMM_WORLD);
}

2. Use MPI_Sendrecv. The library pairs the send and the receive for you. This is the clearest choice for exchanges with a fixed partner, and it also works with MPI_PROC_NULL at the edges of a domain.

MPI_Sendrecv(out, count, MPI_DOUBLE, other, 0,
             in,  count, MPI_DOUBLE, other, 0,
             MPI_COMM_WORLD, MPI_STATUS_IGNORE);

3. Post non-blocking operations, then wait. Post the receive and the send, do any work that does not touch the buffers, then wait for both. This scales naturally to many neighbours and is where overlap of communication and computation starts.

MPI_Request req[2];
MPI_Irecv(in,  count, MPI_DOUBLE, other, 0, MPI_COMM_WORLD, &req[0]);
MPI_Isend(out, count, MPI_DOUBLE, other, 0, MPI_COMM_WORLD, &req[1]);
/* ... computation that does not read 'in' or write 'out' ... */
MPI_Waitall(2, req, MPI_STATUSES_IGNORE);

The rule that comes with non-blocking calls: after MPI_Isend the send buffer must not be modified, and after MPI_Irecv the receive buffer must not be read, until the request has completed with MPI_Wait, MPI_Waitall or a successful MPI_Test.

What about the other send modes?

  • MPI_Bsend copies the message into a buffer you attach with MPI_Buffer_attach, so the send always returns quickly. It does avoid the deadlock, but you have to size the buffer for the largest burst of messages, and every message is copied once more.
  • MPI_Rsend is only correct if the matching receive is guaranteed to be posted already. It is rarely worth the risk in application code.

Summary

  • Never rely on MPI_Send returning before the matching receive is posted. Small test cases hide the problem.
  • Test with MPI_Ssend to expose patterns that only work because of buffering.
  • For exchanges, use MPI_Sendrecv or non-blocking operations with MPI_Waitall.

Code: exchange.c (all three safe versions) and dependBuffer.c (the exercise for finding the eager limit).

All writing