Two ranks must exchange their boundary arrays. Each sends first and then receives. The program works for a small array, then hangs for a larger one. Nothing about the mathematics changed. What changed was an assumption that a send would always find enough buffering to return.
This chapter extends the separate-address-space model with three precise questions: which receive matches a message, when may a buffer be reused, and can the calls make progress without circular waiting?
1. An envelope identifies the conversation
A message has a payload and an envelope: source, destination, tag and communicator context. Ranks are interpreted within the communicator. A receive's source is the sender's rank, not the destination argument the sender used.
/* Executed on rank 0; the communicator has at least two ranks. */
int a = 42;
MPI_Send(&a, 1, MPI_INT, 1, 999, MPI_COMM_WORLD);
/* Executed on rank 1. */
int b;
MPI_Status status;
MPI_Recv(&b, 1, MPI_INT, 0, 999, MPI_COMM_WORLD, &status);
The count is elements, not bytes. Corresponding basic C datatypes include MPI_CHAR, MPI_INT, MPI_LONG, MPI_FLOAT and MPI_DOUBLE. Sender and receiver must supply compatible type signatures; receive capacity must be sufficient. Derived datatypes permit different memory layouts, but are not needed here.
MPI_ANY_SOURCE and MPI_ANY_TAG broaden matching; status.MPI_SOURCE and status.MPI_TAG identify the actual match. Use MPI_STATUS_IGNORE when status is unnecessary. Wildcards are convenient but can introduce nondeterministic choice between available messages.
Within an ordered single-threaded sender, MPI's nonovertaking rule constrains messages that could match the same receive. It is not a global arrival-order guarantee across sources or distinct tags.
A correct payload is insufficient when its envelope belongs to a different conversation. Try a matching and a mismatched tag while holding the participants and context fixed.
A value is waiting to arrive. Change its tag and predict whether this posted receive can accept it.
Play, pause, or scrub to inspect a moment.
Paused. A matching envelope is travelling toward the posted receive.
2. Blocking means a local completion condition
| Send mode | Blocking / nonblocking | What the mode commits to |
|---|---|---|
| Standard | MPI_Send / MPI_Isend | May buffer or wait for a matching receive; do not rely on buffering |
| Synchronous | MPI_Ssend / MPI_Issend | Send completion requires a matching receive to have started receiving |
| Buffered | MPI_Bsend / MPI_Ibsend | Uses sufficient attached buffering; buffer sizing is the application's obligation |
| Ready | MPI_Rsend / MPI_Irsend | Valid only if the matching receive is already posted |
A completed blocking send permits reuse of the send buffer. It does not generally mean the receiver has returned from its receive. A completed receive makes the received data available locally. Buffered sends are not guaranteed instantaneous, and insufficient attached space is an error rather than a portable fallback. Ready sends require a proof of the precondition, not a guess about timing.
3. Many senders, one receiver
The original square-of-rank example separates computation from communication:
#include <stdio.h>
#include <mpi.h>
int main(int argc, char **argv) {
int rank, p, a;
MPI_Init(&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Comm_size(MPI_COMM_WORLD, &p);
if (rank != 0) {
a = rank * rank;
MPI_Send(&a, 1, MPI_INT, 0, 999, MPI_COMM_WORLD);
} else {
for (int src = 1; src < p; ++src) {
MPI_Recv(&a, 1, MPI_INT, src, 999,
MPI_COMM_WORLD, MPI_STATUS_IGNORE);
printf("Rank %d sent %d\n", src, a);
}
}
MPI_Finalize();
return 0;
}
Assume ranks are small enough that rank*rank fits in int. Compile with mpicc squares.c -o squares and launch with mpiexec -np 4 ./squares. Rank zero requests sources in increasing order; a later sender may wait, but there is no cycle because rank zero eventually posts each required receive. With one rank, the receive loop is empty.
4. Draw the wait-for cycle
For exactly two ranks, partner=1-rank identifies the peer:
MPI_Send(a, n, MPI_FLOAT, partner, 999, MPI_COMM_WORLD);
MPI_Recv(b, n, MPI_FLOAT, partner, 999,
MPI_COMM_WORLD, MPI_STATUS_IGNORE);
If each standard send waits for a matching receive, neither rank reaches its receive. The implementation has not changed MPI_Send into another API mode; standard-mode semantics simply permit this lack of buffering.
For a pair, ordering one rank receive-then-send and the other send-then-receive breaks the cycle. A more direct expression is:
MPI_Sendrecv(a, n, MPI_FLOAT, partner, 999,
b, n, MPI_FLOAT, partner, 999,
MPI_COMM_WORLD, MPI_STATUS_IGNORE);
MPI_Sendrecv combines the two operations so this matching exchange need not depend on buffering. It is not an atomic transaction, nor does it repair mismatched ranks, tags or counts. Send and receive buffers must be disjoint; MPI_Sendrecv_replace is a different API for a shared buffer.
In a ring, send to (rank+1)%p and receive from (rank+p-1)%p. All ranks can call MPI_Sendrecv with these neighbours and compatible tags. The same pattern will implement Cartesian halo transfers.
Watch the two schedules unfold. The important change is the availability of a matching receive, rather than the apparent speed of a small test message.
Both ranks want to exchange a value. Watch which required receive is reachable under each call schedule.
Play, pause, or scrub to inspect a moment.
Paused. Without buffering, each send can wait for the other rank's later receive. The resulting cycle prevents progress.
5. Initiation and completion can be separate
MPI_Isend and MPI_Irecv initiate operations and return requests. Later wait/test calls establish completion. This can break blocking-order cycles if all needed operations are posted before waiting, and can expose useful overlap. Immediately waiting for an unmatchable send before posting its receive reproduces the dependency problem.
The nonblocking send documentation makes buffer lifetime central: do not modify the send data before completion; do not access incoming data before its receive completes. “Fire and forget” is an unsafe analogy. Advanced MPI develops the full overlap pattern.
Exercises
- Turn the square-sender example into a ring using
MPI_Sendrecv; check the received value against the source rank. - For a two-rank experiment, replace sends with
MPI_Ssendin the unsafe ordering. Use a timeout and explain the wait-for graph rather than treating a hang as a performance measurement. - Explain why a standard send returning does not prove the receiver has used the value.
- Make a table of source, destination, tag and communicator for each message in your exchange before writing code.
Point-to-point operations describe individual edges. Collectives express common communication patterns as a group operation.