Log inStart free
LESSON 27.C2 · 6 STAGES · 2 CONTROLS · 3 CHECKSPRO · PREVIEW

The GEMM optimization ladder

Watch one 64 × 64 matmul climb six rungs, from 62 to 712 GFLOP/s.


Pro lesson. Stage 1 is free to watch. Pro opens all 6 stages, 2 controls and 3 checks, in this lesson and every other.

Go Pro · $8.25/mo
EXPLAINER · 27.C2 · GEMM BAYRUNG 1 OF 6 · 8,448 NS · 62 GFLOP/S
01
Stage 1 of 6 · 64 GEMVs
Run C = A · B as 64 GEMVs, one per column of B. Each GEMV reads all of A again.
Explain it simply

You see A, B and C in HBM, the GPU's main memory. A GEMV multiplies A by one column of B, so 64 columns need 64 GEMVs. Each one reads all of A again. The math units sit idle most of the time, waiting for bytes.

Rung 1 of 6: 64 GEMVs take 8,448 ns, 62 GFLOP/s. HBM sets the time.

bytes and copiesmath and speedstalls and waitsA, peaklatency, idle time
540,672 B from HBMeach A value read 64 timesHBM64 B per nslimit: HBMshared memorynot usedCUDA corespeak 512 GFLOP/s12% of peakABCachieved GFLOP/sCUDA peak 51262 GFLOP/s8,448 nsrung 1 of 6 · 64 GEMVswhole GPU · rung 1: 8,448 nsHBMmath02,0004,0006,0001 · 64 GEMVs27.092 · Naive GEMM27.103 · Shared tiles27.104 · Swizzle27.115 · Double buffer27.126 · Tensor Cores27.13t = 64 · max(8,448 B / 64, 8,192 / 512) = 8,448 ns · 62 GFLOP/smemory 8,448 ns · math 1,024 ns540,672 B from HBMeach A value read 64 timesHBM64 B per nslimit: HBMshared memorynot usedCUDA corespeak 512 GFLOP/s12% of peakABCachieved GFLOP/sCUDA peak 51262 GFLOP/s8,448 nsrung 1 of 6 · 64 GEMVswhole GPU · rung 1: 8,448 nsHBMmath02,0004,0006,0001 · 64 GEMVs27.092 · Naive GEMM27.103 · Shared tiles27.104 · Swizzle27.115 · Double buffer27.126 · Tensor Cores27.13t = 64 · max(8,448 B / 64, 8,192 / 512) = 8,448 ns · 62 GFLOP/smemory 8,448 ns · math 1,024 ns
Toy: a 64 × 64 FP16 matmul on a 4-SM toy GPU
STAGE 1 / 6
MATH · STAGE 1HOVER A SYMBOL OR A SHAPE
t=64⋅max⁡ ⁣(QBW, FP)\sy{t}{out}{t} = 64 \cdot \max\!\left(\frac{\sy{q}{data}{Q}}{\sy{bw}{param}{BW}},\ \frac{\sy{f}{out}{F}}{\sy{pk}{param}{P}}\right)
t=64⋅max⁡ ⁣(8,44864,8,192512)=8,448 ns  ⇒  524,2888,448=62 GFLOP/st = 64 \cdot \max\!\left(\tfrac{8{,}448}{64}, \tfrac{8{,}192}{512}\right) = 8{,}448\ \text{ns}\;\Rightarrow\; \tfrac{524{,}288}{8{,}448} = 62\ \text{GFLOP/s}

Each GEMV takes the longer of its memory time and its math time.

Common mix-ups

MYTHOne GEMM is always faster than 64 GEMVs.

The fix, and 1 more mix-up with the diagram to show them, come with Pro.

Predict, then reveal
3 CHECKS · PRO
Q01With a 128 ns copy latency, does double buffering save more time than with 32 ns?

Make your guess, then test it in the diagram. All 3 checks come with Pro.

PRO LESSON · YOU WATCHED STAGE 1 OF 6
Unlock the full explainer
  • 5 more stages and 2 live controls
  • 2 mix-up cards and 3 predict-then-reveal checks
  • Watch it as a narrated video, in three voices
  • Every Pro lesson and capstone · 656 in total

$99 a year, or $15 a month. Cancel any time.