OAOzden Akinci
MPI lecture notes / Chapter 2 of 7

Point-to-point communication

Sending and receiving messages, the message envelope, the four send modes, deadlocks, non-blocking communication and measuring latency and bandwidth.

Send and receive

int MPI_Send(const void *buf, int count, MPI_Datatype type,
             int dest, int tag, MPI_Comm comm);
int MPI_Recv(void *buf, int count, MPI_Datatype type,
             int source, int tag, MPI_Comm comm, MPI_Status *status);

The data is described by buffer, count and datatype, for example 100 elements of MPI_DOUBLE, never by a number of bytes. The rest is the envelope: source, destination, tag and communicator. A receive matches a message only if all four fit. Two wildcards help on the receiving side:

  • MPI_ANY_SOURCE accepts a message from any rank.
  • MPI_ANY_TAG accepts any tag.

After a wildcard receive, the MPI_Status tells you what actually arrived: status.MPI_SOURCE, status.MPI_TAG, and the element count via MPI_Get_count. The receive buffer may be larger than the message, but not smaller. A message that doesn’t fit is an error.

Messages between the same pair of processes on the same communicator are non-overtaking: if rank 0 sends two messages to rank 1 that both match a receive, they arrive in the order they were sent.

Four send modes

Mode Blocking Non-blocking Returns when
Standard MPI_Send MPI_Isend the buffer can be reused. MPI may have copied the message, or it may have waited for the receiver.
Synchronous MPI_Ssend MPI_Issend the matching receive has started
Buffered MPI_Bsend MPI_Ibsend the message has been copied into a buffer you attached with MPI_Buffer_attach
Ready MPI_Rsend MPI_Irsend as standard, but only correct if the receive is already posted

In practice you use the standard mode and design your communication so that it would also work with the synchronous one. The reason is the topic of the next section.

Deadlock

A deadlock happens when processes wait for each other in a cycle. The classic case is two ranks that both receive first:

/* rank 0 */                        /* rank 1 */
MPI_Recv(... from 1 ...);           MPI_Recv(... from 0 ...);
MPI_Send(... to 1 ...);             MPI_Send(... to 0 ...);

Neither receive can complete, so neither send is ever reached. A subtler version, where both ranks send first, works for small messages and hangs for large ones. Whether MPI_Send returns before the receive is posted depends on an internal threshold, the eager limit. This article measures where that limit is and shows how to find such code.

The three safe ways to exchange data between two ranks are in 03_exchange.c:

  1. Break the symmetry: one side sends first, the other receives first.
  2. MPI_Sendrecv, which pairs the send and the receive internally.
  3. Non-blocking calls followed by MPI_Waitall.

Non-blocking communication

A non-blocking call starts an operation and returns at once with an MPI_Request. You complete it later:

MPI_Request req[2];
MPI_Irecv(&incoming, 1, MPI_INT, left,  0, MPI_COMM_WORLD, &req[0]);
MPI_Isend(&passing,  1, MPI_INT, right, 0, MPI_COMM_WORLD, &req[1]);
/* work that touches neither 'incoming' nor 'passing' */
MPI_Waitall(2, req, MPI_STATUSES_IGNORE);
  • MPI_Wait and MPI_Waitall block until the operations complete.
  • MPI_Test and MPI_Testall check without blocking, which is useful inside a loop that does other work.
  • Until an operation has completed, you may not modify a send buffer or read a receive buffer.

Non-blocking calls solve two problems: they make deadlock-free communication patterns easy to write, and they let communication overlap with computation. 04_ring.c passes values around a ring of any size this way, so every rank ends up with the sum of all contributions:

$ mpirun -np 5 ./04_ring
rank 0: sum of rank^2 over all ranks = 30
rank 1: sum of rank^2 over all ranks = 30
...

A plain blocking version of this ring, where every rank first sends to the right and then receives from the left, is exactly the pattern that deadlocks once the messages are large.

Checking for a message: MPI_Probe

Sometimes the receiver doesn’t know how large a message is. MPI_Probe waits for a matching message without receiving it. You then read its size from the status and allocate the buffer:

MPI_Status st;
int n;
MPI_Probe(0, 0, MPI_COMM_WORLD, &st);
MPI_Get_count(&st, MPI_DOUBLE, &n);
double *buf = malloc(n * sizeof(double));
MPI_Recv(buf, n, MPI_DOUBLE, 0, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);

In multithreaded programs, use MPI_Mprobe and MPI_Mrecv instead, so that another thread cannot receive the probed message in between.

How fast is a message?

02_pingpong.c sends a message back and forth between two ranks and reports the time for one direction. On one node, through shared memory, it gives a typical picture:

     bytes   latency [us] bandwidth [MB/s]
         1           0.41            2.5
      1024           0.75         1361.6
     65536          12.67         5171.7
   4194304         436.90         9600.2

Small messages cost a nearly fixed latency. Large messages are limited by bandwidth. Between nodes, latency over InfiniBand is a few microseconds, and over plain Ethernet tens of microseconds. That is why one large message is almost always cheaper than many small ones: if you have several pieces of data for the same neighbour, pack them together.

Exercises

  1. Change MPI_Send to MPI_Ssend in 03_exchange.c with the order method. Does it still complete? Why?
  2. Write the ring from 04_ring.c with MPI_Sendrecv instead of non-blocking calls.
  3. Run 02_pingpong with the two ranks on different nodes, for example with srun --nodes=2 --ntasks-per-node=1. At what message size does bandwidth reach half of its maximum?