Skip to content
BIASED

Making programs fast

Wall/CPU time, repeats and profilers, what `-O2` does and what blocks it, algorithm vs constant factor, matmul 26× via loop order and blocking.

How to measure

Do not guess. Wall time (monotonic clock) vs CPU time (clock/getrusage): waiting is wall without CPU; threads can make CPU > wall. Repeat, report min/median, drop warm-up. Profilers find the hotspot; rdcycle/rdinstret give cycles and CPI.

Worked example (simulated). A fully associative cache of 8 KiB with 128-byte lines, 64 lines in all, and n=128, so that one column of BBB touches 128 lines and cannot stay. The Code section’s simulator counts the misses per multiply-add: 1.070 for i, j, k, 0.0635 for i, k, j (the 1/161/161/16 of row kkk of BBB), and 0.0083 for blocks of b=16. The blocked count has a short explanation: for one block of AAA, all n/bn/bn/b blocks of BBB and CCC in its row pass through the cache while the block of AAA stays, so each b3b^3b3 multiply-adds load two blocks of b2/16b^2/16b2/16 lines, 2/(16b)=0.00782/(16b) = 0.00782/(16b)=0.0078 misses per multiply-add. With b=32 three blocks need 24 KiB, more than the cache, and the count jumps back to 0.066.

Worked example. Suppose a program spends 3.871 s in the baseline matrix product and 1 s on everything else (a hypothetical figure). Then f=3.871/4.871=0.795f = 3.871/4.871 = 0.795f=3.871/4.871=0.795. Replacing the product by the blocked -O2 version makes that part s=3.871/0.147=26.3s = 3.871/0.147 = 26.3s=3.871/0.147=26.3 times faster, and the whole program

The compiler and blockers

-O2: registers, inlining, constant folding, hoist, strength reduction, unroll, vectorize. Blockers: aliasing, opaque calls/side effects, non-associative FP. Help with restrict, fewer opaque calls, careful flags — not by hoping.

The block size should let the blocks in use fit in the cache. Three blocks of doubles take 3⋅8b23 \cdot 8 b^23⋅8b2 bytes: 96 KiB for b=64, which fits in the M1’s 128 KiB level-1 data cache, and 384 KiB for b=128, which does not. Measured at -O2, in GFLOP/s: 12.75 for b=16, 13.61 for b=32, 14.57 for b=64, then 9.50 for b=128 and 10.09 for b=256. The best block, b=64, took 0.147 s against 0.175 s for the unblocked i, k, j, a gain of 1.19. The gain is modest at n=1024, most likely because all of BBB, 8 MiB, fits in the M1’s 12 MiB level-2 cache, so i, k, j already finds what it reuses there; blocking matters more for larger matrices and for machines with smaller caches, and the same idea, applied at the level of registers and of each cache, is what tuned libraries are built from.

The previous note added the last of the three kinds of parallelism a processor offers: several numbers per instruction, next to several instructions per cycle and several cores, all fed by the cache hierarchy. None of it helps a program that is not written and compiled to use it. The first note of the course already saw this: the same matrix product, the same instructions in three loop orders, took 1.5 s in one order and 6.4 s in another. This note turns that observation into a method. Measure first, and measure properly. Let the compiler do what it can, and know what stops it. Prefer a better algorithm when there is one. Then fix the memory access pattern. Matrix multiplication is the running example, and Amdahl’s law says when to stop.

Matmul: order and blocking

Algorithm first; then constants from memory order and SIMD. For C=AB in C: loop order i,k,j, then blocking so three b× b tiles fit in cache. M1: 0.55 → 14.6 GFLOP/s (~26×); tuned library 255. Amdahl for opts: S=1/((1-f)+f/s) — optimize the large f, re-profile. Measure first. Wall time (monotonic clock) vs CPU time (clock/getrusage): waiting is wall without CPU; threads can make CPU > wall. Repeat, report min/median, drop warm-up. Profilers (perf, Instruments) find the hotspot; rdcycle/rdinstret on RISC-V give cycles and CPI for a region. Compiler. -O2 enables register allocation, inlining, constant folding, hoist, strength reduction, unroll, vectorize. Blockers: aliasing, opaque calls/side effects, non-associative FP. Help with restrict, Link-Time Optimization, fewer opaque calls — not by hoping. Read the assembly: if the hot loop still spills to the stack, fix that before micro-tuning. Matmul case study. Algorithm first; then constants from memory order and SIMD. For C=AB in C: loop order i,k,j (streaming B’s row in cache), then blocking so three b\× b tiles fit in L1/L2. M1 numbers in the notes: 0.55 → 14.6 GFLOP/s (~26×) after order+block; a tuned library reaches ~255. Amdahl for opts: S=1/((1-f)+f/s) — optimize the large f, then re-profile; a 10× gain on a 5% hotspot is only ~1.05× overall. A practical loop: (1) measure wall and CPU, (2) profile to a function or line, (3) inspect assembly and cache counters (perf stat), (4) change one thing — algorithm, layout, compiler flag, blocking factor — (5) re-measure. Changing three things at once teaches nothing. Microbenchmarks must outlive the cache warm-up and defeat dead-code elimination (consume results, DoNotOptimize barriers). Roofline again: if you are at 5% of peak FLOP/s with intensity below the ridge, do not buy a fatter FPU — cut bytes moved. If you are already past the ridge, algorithmic FLOP reduction or a better ISA (FMA, SIMD width) matters. The DSA topic is what happens when even that is not enough and the whole graph moves to specialized silicon. Next: Domain-specific architectures — TPUs when general cores are not enough. Worked example. 20 instructions per element at -O0 against 14/16=0.87514/16 = 0.87514/16=0.875 at -O2 is 23 times fewer instructions for a long array, before counting the cheaper instructions and the absent stack traffic. Setup of every measurement in this note. One Apple M1 (8 GB, macOS 26.5), Apple clang 21.0.0, one thread, n=1024 doubles (8 MiB per matrix), the minimum of five timings unless stated. The M1 reports 128-byte cache lines and a 128 KiB level-1 data cache per performance core (sysctl hw.cachelinesize hw.perflevel0.l1dcachesize). The machine was shared with other jobs, and repeated runs differ by 10–20 %, so only ratios that are well

Check your understanding

The same questions as in Pulse. Answers are shared between modes.

Full notes

Answers are saved on the server and tied to this browser until you sign in.