← Back to Home

Session 10 — OpenMP in Depth

Unit IV, and the code gets real. Work-sharing, scheduling, data scoping, and Lab 1 — Matrix Multiplication. 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. schedule() Visualizer browser demo

Colour-codes 32 loop iterations by which thread ran them, for static / dynamic / guided on even vs uneven work. Toggle to uneven on static and watch one thread get stuck with the heavy tail. Runs in-browser — show it live.

3. Matrix Multiplication Lab 1 view C code

Serial vs #pragma omp parallel for, timed with omp_get_wtime. Checks the two results match, then prints speedup and efficiency. Run it at 1, 4, 8 threads and watch the same code get 3–5× faster.

1. Loop Scheduling view C code

The same unbalanced loop run three ways — static, dynamic, guided — printing which thread ran each iteration. See scheduling with your own eyes.

2. Sections (task parallelism) view C code

Three different jobs run at once with #pragma omp sections, plus single. The kitchen-with-three-cooks pattern — different from the split-a-loop data parallelism of the others.

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 -O2 -fopenmp 03-matrix-multiply.c -o matmul && ./matmul. Change threads with OMP_NUM_THREADS=4 ./matmul. On Compiler Explorer (godbolt.org) add -O2 -fopenmp. Full instructions and the macOS libomp note are in README.md.

Unit IV — Programming Models & Tools. Next up: MPI (Session 11) for many machines, then CUDA (Session 12) for the GPU.