Ch29 Discussion

Discussion Guide

Opening prompt (5 min). "You have a numerical loop that's too slow. Before you know anything else, what is the first thing you change?" Let them propose the clever things (SIMD, unrolling), then reveal that the answer is almost always "check the loop order and turn on -O3" — and that you should have profiled first. Surfaces the gap between optimization folklore and discipline.

Discussion questions: 1. The chapter says most numerical kernels are memory-bound, so "optimization is mostly about moving less data, not doing less arithmetic." Why is this counterintuitive, and how does the roofline model make it precise? What would a compute-bound kernel look like, and how does the advice flip? 2. Cache blocking transformed matrix multiply (CS-02) but did nothing for the heat stencil (CS-01). Explain the difference using arithmetic intensity in one sentence. Give the general rule for when tiling is worth trying. 3. do concurrent is "a promise the compiler trusts but does not check." When is a construct that trusts you without checking a good design, and when is it dangerous? Compare to intent (checked) and restrict in C (also an unchecked promise). 4. You spent a week hand-blocking and vectorizing a matrix multiply and reached 15% of peak; dgemm reaches 80% out of the box. Was the week wasted? Argue both sides, then state what you actually learned that a call dgemm alone would not have taught you. 5. Knuth: "premature optimization is the root of all evil… yet we should not pass up our opportunities in that critical 3%." How do you identify the 3%? What is the cost of optimizing the other 97%, in a scientific code that must be maintained for a decade (Part IX)?

Mini group activity (20 min): "Which rung of the ladder?" Hand each pair a set of kernel cards and have them label each: (a) name the first optimization to try, (b) predict whether cache blocking helps, (c) say whether it is memory- or compute-bound. Suggested cards:

Kernel First optimization? Blocking helps? Bound?
2D heat stencil sweep loop order + fusion no memory
dense $n\times n$ matrix multiply loop order, then block yes compute
y = a*x + y (saxpy) over a huge vector loop order (already unit stride) / vectorize no memory
element-wise exp(x(:)) over a huge array vectorize (elemental) no memory (or compute if exp is heavy)
small $8\times8$ matmul in an inner loop inline; maybe just matmul no (fits in cache) compute

Groups fill the table, then defend one row each. Debrief on the pattern: the first move is almost always loop order + vectorize; blocking is the exception (high reuse); and "call the library" wins for the standard shapes. This makes the abstract ladder concrete and hand-owned — the whole chapter's judgment in twenty minutes.