← Back to Home

Session 03 — Parallel Programming Models

Our first real code. These are C + OpenMP programs plus one browser demo. The .c files can't run inside a browser — open them to view the code, then run them via OnlineGDB or a local compiler (see "How to run" below).

▶  Open the Interactive Slides

4. Fork-Join, Animated browser demo

Watch one master thread fork into a team of workers during a parallel region, then join back to one. The mental model behind every OpenMP program. Runs in-browser — show it live.

1. Hello, OpenMP view C code

Your first parallel program. Add one #pragma and every thread says hello. The print order is non-deterministic — the first "aha".

2. Parallel Sum (the right way) view C code

Sum a 100-million-element array with parallel for reduction(+:sum). Correct and fast, every single run.

3. Race Condition (the wrong way) view C code

The same sum with the reduction removed. Run it several times and watch the wrong answer change — a live data race. Fixed by example 2.

How to run these

The .c files need an OpenMP compiler. Easiest in class: paste into OnlineGDB (language C) and press Run — OpenMP is already on, zero install.

Locally: gcc -fopenmp 01-hello-openmp.c -o hello && ./hello. Set threads with OMP_NUM_THREADS=4 ./hello. On Compiler Explorer (godbolt.org) add -fopenmp. Full instructions and the macOS note are in README.md.

This is the last session of Unit I. MPI and CUDA join OpenMP in Unit IV.