Chapter 32 — Teaching Notes
One-line purpose. Teach Fortran's native parallel model — images, coarrays, coindexing, synchronization, and the 2018 collectives — well enough that students can decompose a structured-grid solver across images with a correct halo exchange, and understand why the same idea reappears (in other clothes) as OpenMP and MPI.
Key ideas to emphasize
avsa[q]is the whole model. The single most important distinction: an unbracketed reference is always the local copy; a coindexed referencea[q]reaches another image (and may be a network message). Students who blur this writea = a + 1expecting a shared increment and are baffled. Drill it early.- Distributed memory, not shared. Each image has a private copy of every coarray. This is the opposite of the OpenMP mental model coming next chapter — say so explicitly, repeatedly. Coarrays are MPI's model wearing nicer syntax, not OpenMP's.
- Correctness = write →
sync all→ read. Frame every correct example as an instance of this. Teach segments: races live in unordered segments; a synchronization orders them. The Find-the-Bug and Case Study 1 are built to make this visceral. -fcoarray=singlevalidates logic, NOT synchronization. This is the trap that ships bugs (Case Study 1). One image cannot race, so a green single-image test says nothing about your syncs. Make students internalize that a passing-fcoarray=singlerun is necessary but not sufficient.- Collectives are the safe path.
co_sum/co_max/co_broadcastdo the common reductions correctly and in one line; hand-rolled coindexed reductions are where races breed. Teach the collectives as the default and the raw coindexing as the thing they save you from. - Coarrays are Fortran's alone. The threshold concept — parallelism declared on a variable, not imported — is the transferable "aha." It is also the concrete payoff of "Fortran is not dead / modern Fortran is a modern language."
Misconceptions to preempt
- "All images share one array." (No — each has its own private copy;
ais local,a[q]is remote.) - "A coarray is automatically synchronized." (No — you must synchronize remote access yourself.)
- "It passed on my laptop, so it's correct." (
-fcoarray=singlehides all races.) - "
sync allbetween the code makes any order fine." (Only if the sync straddles the write and the read; position matters — Case Study 1, Fault 2.) - "co_sum is called by one image." (No — every image must call a collective.)
- "Teams work in gfortran." (Standard, but weakly supported today — verify or use ifx.)
- "Cut the plate any way you like." (Cut along columns so halos are contiguous — column-major matters again.)
A live demonstration (8–10 minutes)
Requires OpenCoarrays (or Intel). Put code/example-02-remote-access.f90 on the projector. Run it
-fcoarray=single (one line) then cafrun -n 4 (four lines). Then delete the sync all and rerun on 4
images several times — on a loaded machine it will occasionally print a wrong/garbage square, visibly
demonstrating the race. Restore the sync; it is solid again. If OpenCoarrays is unavailable, do the same as a
trace on the board: show how, without the barrier, image 1 can reach sq[3] before image 3 has written it.
Then run example-03-collectives.f90 on 4 images and show co_sum = 10, co_max = 4 — the whole reduction
in one line.
Class-time budget (~50 min)
- 8 min: images and SPMD;
this_image/num_images;real :: a[*](§32.1); the threshold concept. - 10 min: codimensions and coindexed access
a[q]; local vs remote; the remote-access example (§32.2). - 12 min: synchronization — segments,
sync all, the race,sync images,critical(§32.3); the live demo. - 8 min: collectives
co_sum/co_max/co_broadcast; teams + the honest support caveat (§32.4). - 6 min: building/running —
-fcoarray=singlevs OpenCoarrays; when coarrays beat MPI/OpenMP (§32.5). - 6 min: the Project Checkpoint — plate strips, halo exchange, why the
32needs the halo.
Prerequisites to review
One slide each: the Chapter 24 five-point stencil and two-array (current/next) update — the checkpoint reuses it verbatim; Chapter 5 column-major order — the reason halos are columns; Chapter 8 modules / explicit interfaces — needed for coarray dummy arguments (spaced review); Chapter 31's Amdahl estimate and the data-parallel-stencil / sequential-time-loop split — the plan this chapter executes.
Connections
Forward: Chapter 33 (OpenMP) parallelizes the same stencil with shared-memory threads — contrast the mental
models directly; Chapter 34 (MPI) does this very halo exchange with explicit messages and formalizes domain
decomposition, ghost cells, and non-blocking overlap; Chapter 38 (capstone) runs the parallel solver and
measures scaling against the Chapter 31 estimate. The co_sum ↔ reduction ↔ MPI_Allreduce correspondence is
worth previewing so the next two chapters feel like translation. Chapter 39 revisits coarrays for the future
(its spaced review targets Ch. 32).