Communication without a matching receive
In point-to-point communication both sides take part: one sends, the other receives. One-sided communication, or remote memory access (RMA), separates the two. A process makes part of its memory available as a window, and other processes read from it or write into it directly. The target does not post a receive.
This fits problems where the target doesn’t know in advance who will access its data or when, for example distributed hash tables, work queues and irregular graph algorithms. On networks with RDMA support, such as InfiniBand, these operations can be carried out by the network adapter without involving the target CPU.
Windows
MPI_Win win;
MPI_Win_create(base, size_in_bytes, disp_unit, MPI_INFO_NULL, comm, &win);
...
MPI_Win_free(&win);
Creating a window is collective. A process that doesn’t expose memory passes size 0. The displacement unit (usually the size of one element) lets remote processes address the window by element index rather than by byte offset. Two newer constructors are often a better choice:
MPI_Win_allocatelets MPI allocate the memory, which can be faster on some networks.MPI_Win_allocate_sharedcreates memory that processes on the same node can read and write with ordinary loads and stores.
The three operations
| Call | Effect |
|---|---|
MPI_Put |
write local data into the target’s window |
MPI_Get |
read data from the target’s window |
MPI_Accumulate |
combine local data with the target’s data using an operation such as MPI_SUM |
All three are non-blocking: when the call returns, the data has not necessarily moved. They only become complete and visible at a synchronisation point.
Synchronisation with fences
The simplest synchronisation is MPI_Win_fence, a collective call on the window. The operations between two fences form an access epoch. After the second fence, all of them are complete everywhere:
MPI_Win_fence(0, nwin);
if (rank != 0) MPI_Get(&n, 1, MPI_LONG, 0, 0, 1, MPI_LONG, nwin);
MPI_Win_fence(0, nwin); /* only now is n valid on every rank */
MPI_Barrier is not a substitute for a fence. It synchronises processes, but it doesn’t complete RMA operations.
Example: pi with one-sided operations
10_rma_pi.c is the pi program from chapter 3, rewritten so that rank 0 never takes part in a send or receive. It exposes n and pi in two windows. The other ranks fetch n with MPI_Get, compute their part of the sum, and add it into pi with MPI_Accumulate:
MPI_Win_fence(0, piwin);
MPI_Accumulate(&mypi, 1, MPI_DOUBLE, 0, 0, 1, MPI_DOUBLE, MPI_SUM, piwin);
MPI_Win_fence(0, piwin);
pi = 3.1415926535896861, error = 1.07e-13 (from 4 processes)
Several processes update the same location at the same time here, and that is fine for MPI_Accumulate, because MPI applies accumulates to the same location atomically. Doing the same with MPI_Put would be an error: concurrent puts to one location give an undefined result.
For a real program this example is overkill, since MPI_Reduce does the same in one line. It does show the shape of every RMA code: window, epoch, operations, epoch.
Finer-grained synchronisation
Fences synchronise every process in the window. Two other mechanisms involve fewer processes:
- Post-start-complete-wait (
MPI_Win_post,MPI_Win_start,MPI_Win_complete,MPI_Win_wait) synchronises only a chosen group of origins with a group of targets, for example the neighbours in a stencil. - Passive target (
MPI_Win_lock/MPI_Win_unlock, orMPI_Win_lock_allwithMPI_Win_flush) lets an origin access a target that does nothing at all. This is the mode for truly irregular access. MPI 3.0 added atomic operations for it, such asMPI_Fetch_and_opandMPI_Compare_and_swap, which are the building blocks of distributed counters and queues.
Exercises
- Replace
MPI_AccumulatewithMPI_Putinto one element per rank, so that rank 0’s window holds an array of partial results that it adds up itself. - Implement a shared work counter: rank 0 holds an integer in a window, and every rank repeatedly uses
MPI_Fetch_and_opwith passive-target locks to take the next task number until all tasks are done. - Time the pi program with
MPI_Reduceand with RMA on 2, 8 and 32 processes. Which is faster on your system?