S442 of 55
0 XP
  1. Hook
  2. Play
  3. Predict
  4. Name it
  5. Check
  6. Explain

S4 · Lesson 42 · Accelerator lab

Roofline analysis, a systolic array's cycle model and GPU occupancy, in Python

Choosing or designing an accelerator starts with three quick models: the roofline says whether a kernel is limited by arithmetic or by memory; a cycle model of a systolic array says how much of its peak a matrix multiply of a given shape can use; and an occupancy calculator says how many warps a GPU kernel keeps resident, which decides whether memory latency is hidden. This lab writes all three and applies them to the shapes that come up in practice.

  • The curve is the utilization of an n × n weight-stationary systolic array on a matrix multiply with M rows streamed per tile, M/(M+n+2(n−1)), counting n cycles to load each tile's weights and 2(n−1) to fill and drain.
  • Small batches waste most of a big array: with M = 64 a 256 × 256 array is busy under 10% of the time.
  • The lab adds tiling over the other two dimensions, which can waste more.

Predict

A 256 × 256 systolic array multiplies a 64 × 256 matrix by a 256 × 256 weight matrix: one tile, 64 rows streamed. Loading the tile's weights takes 256 cycles and filling and draining 510. What fraction of its peak does it use?

Name it

Roofline

For a kernel of W operations moving Q bytes on a machine with peak P and bandwidth B: intensity I=W/Q, attainable rate min⁡(P,BI), and time W/min⁡(P,BI); memory-bound if BI<P.

Systolic tiling

An M × K by K × N multiply on an n × n weight-stationary array needs ⌈K/n⌉⌈N/n⌉ weight tiles. Each takes n cycles to load, then M cycles to stream the rows plus 2(n−1) to fill and drain:

cycles=⌈Kn⌉⌈Nn⌉(n+M+2(n−1)),utilization=MKNn2⋅cycles

GPU occupancy

A block of T threads is ⌈T/32⌉ warps. An SM fits as many blocks as its registers, its shared memory, its block slots and its warp slots all allow:

blocks=min⁡(⌊R32rwb⌋,⌊Ss⌋,Bmax,⌊Wmaxwb⌋)

for r registers per thread, s bytes of shared memory per block and wb warps per block; occupancy is resident warps over Wmax.

Check

Write roofline(P, B, ops, nbytes): with P in GFLOP/s and B in GB/s, return (attainable GFLOP/s, 'memory' or 'compute', run time in seconds).

Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.

Check

Write systolic(M, K, N, n): return (cycles, utilization) for an M × K by K × N multiply on an n × n weight-stationary array, by the tiling model.

Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.

Check

Write occupancy(regs, smem, threads, R=65536, S=102400, Wmax=64, Bmax=32): the fraction of an SM's warp slots a kernel fills, for regs registers per thread, smem bytes of shared memory per block (0 for none) and threads per block.

Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.

Explain why

Why can a systolic array that matches a layer's weight matrix exactly still run at a small fraction of its peak?