PX457 · Course contents

MPI: Introduction

An OpenMP thread can read a shared array element once the appropriate ordering is established. A process on another node cannot dereference that element's address. The change from shared to distributed memory is therefore a change in the algorithm's obligations: data ownership and data transfer become explicit.

MPI, the Message Passing Interface, is a standard for communication between processes. Implementations include Open MPI and MPICH. It can be used across nodes or between processes on the same node; “MPI” does not imply that every message crosses a network.

1. Same program, separate state

Most examples use SPMD, single program, multiple data. Every process starts the same executable, but rank-dependent control flow and different inputs give it different work. SPMD is a programming pattern; MIMD describes independently progressing instruction streams. The two are compatible.

ModelWorkerOrdinary storageHow another worker obtains a value
OpenMP hereThread in one processShared or private according to scopeShared loads, with correct ordering
MPI hereProcessSeparate address spaceExplicit communication

If every rank declares int a = 0, there are PP independent objects called a. Setting rank zero's a to 42 does not update any other rank's object. Later MPI facilities can expose shared-memory or remote-memory windows, but those are explicit mechanisms beyond this introductory model.

A communicator supplies an ordered group of processes and a communication context. In our intracommunicator examples, a rank is an integer r∈{0,…,P−1}r\in\{0,\ldots,P-1\} identifying a process within that communicator. The same process can have a different rank in another communicator. MPI_COMM_WORLD initially includes all processes in the World-model job.

One name does not imply one object
rank 0a = 42rank 1a = 0rank 2a = 0explicit message
All ranks run the same source, but each owns a separate a. A message copies a value into a receiver's object; it does not turn the objects into one shared variable. The drawing is logical and also applies to ranks placed on one node. ↗

Make the separation literal: changing one object should leave the other two untouched. A message is an additional event, not an automatic consequence of assignment.

A message copies; it does not share

Rank zero changes its local object. Watch what must happen before another rank can see the value.

Play, pause, or scrub to inspect a moment.

rank 0 assigns a = 42rank 0a = 0rank 1a = 0rank 2a = 0copy the value; keep distinct objects

Paused. Local values are (0, 0, 0). The receiving rank owns its own copy.

Three distinct address spaces each own a variable a. A schematic send carries rank zero's 42 to rank one's storage; it creates no pointer alias and does not update rank two. ↗

2. Initialise, identify, compute, finalise

The World-model program begins with MPI_Init (or the threaded form introduced later) and ends with MPI_Finalize. Initialise before communication or communicator queries. Some inquiry routines are legal before initialisation or after finalisation; the rule is not literally “no MPI function may be called”.

#include <stdio.h>
#include <stdlib.h>
#include <mpi.h>

int main(int argc, char **argv) {
    int rank, p;
    MPI_Init(&argc, &argv);
    MPI_Comm_rank(MPI_COMM_WORLD, &rank);
    MPI_Comm_size(MPI_COMM_WORLD, &p);
    printf("Hello PX457 from rank %d of %d\n", rank, p);
    MPI_Finalize();
    return EXIT_SUCCESS;
}

The addresses &argc,&argv permit the implementation to process command-line arguments. The communicator queries return this process's rank and the group size. Every process prints once; output may be interleaved or reordered by the launcher. A plausible order is ranks 2, 0, 1, 3, but none is promised.

MPI_Finalize is collective over the relevant connected World-model processes. Complete pending communications first. It does not mean that all ranks' standard-output text will appear neatly sorted, and no further World-model communication may follow it.

3. Compilation and placement are separate choices

mpicc hello.c -o hello
mpiexec -np 4 ./hello

mpicc is a compiler wrapper: it supplies MPI headers and libraries to a configured underlying compiler, which is not necessarily GCC. mpiexec requests four processes; scheduler integration, allocation and affinity determine where they run. On a managed cluster, follow the site's launcher and batch-system instructions rather than assuming a login node is an execution resource.

Four ranks on one node are useful for correctness tests. They do not reproduce the network costs of four nodes. A scaling report must state ranks per node, threads per rank, binding and problem partition as well as total process count.

Errors have a policy as well as a return code

MPI routines commonly return an integer error code. Many communication errors invoke a default error handler that aborts rather than returning control normally. An application can select MPI_ERRORS_RETURN on a communicator and inspect returned codes, but MPI does not guarantee that every erroneous program can recover and continue correctly. Do not treat an unchecked failure as a valid result or assume a returned error repairs inconsistent collective participation.

The Open MPI initialisation documentation distinguishes the World model used here from the newer Sessions model. This course's examples retain the init–work–finalise structure rather than mixing the two.

4. Ownership comes before communication

For a simple divisible array length N=PnN=Pn, rank rr can own global indices rn,…,(r+1)n−1rn,\ldots,(r+1)n-1. Every index has one owner, so independent elementwise work requires no communication during the kernel. A stencil is different: the first owned element may need its neighbour from another rank. That value becomes a halo or ghost value: a local copy of remotely owned data.

This is the distributed-memory version of the dependency question in OpenMP. We still draw producer–consumer edges. Now some edges must be implemented by messages.

A decomposition becomes a communication problem exactly where a calculation needs a value owned by another rank. Compare an independent update with a nearest-neighbour stencil on the same partition.

An ownership boundary creates a halo

Split twelve points over three ranks. Now trace the neighbour values needed by a stencil at each partition edge.

Play, pause, or scrub to inspect a moment.

R00123BC·R14567··R2891011·BCimmutable old values → ghost copies

Paused. Four directed copies serve the two internal partition boundaries. Original ownership is unchanged.

Each rank owns four immutable old values. The stencil copies only adjacent remote values into ghost slots. Physical outer boundaries are not periodic in this drawing and require their own boundary conditions. ↗

Exercises and continuation

  1. Modify the hello program so each rank prints its own local a, after only rank zero changes its value. Predict the result first.
  2. For N=12,P=3N=12,P=3, list each rank's indices and the remotely owned values needed by a nearest-neighbour stencil.
  3. Compile and launch with the site's supported commands. Record placement separately from process count.

Point-to-point communication supplies the operations that carry a value across an ownership boundary, together with the completion rules that make reusing its buffer safe.