Skip to content
BIASED

Vectors, SIMD and GPUs

SIMD registers, RVV and strip mining, vectorization requirements, GPU warps/SIMT, the roofline model $\min(P, I\cdot B)$.

SIMD

DLP: one instruction, many elements — saves scalar fetch/decode/scheduling. SIMD packs a fixed count (four floats in 128 bits). Compilers vectorize loops without loop-carried dependences or aliasing (restrict helps). M1 saxpy: 31 vs 6 GFLOP/s from cache; 8.2 vs 3.4 from memory.

Notation. The running example is saxpy, yi←a xi+yiy_i ≤ftarrow a x_i + y_iyi​←axi​+yi​ for i=0,…,n−1i = 0, \dots, n - 1i=0,…,n−1, on 32-bit floating-point numbers (“single-precision aaa times xxx plus yyy”). For the RISC-V Vector extension, VLEN is the number of bits in one vector register, SEW the width of one element in bits, LMUL the number of registers grouped into one operand, and VLMAX=LMUL⋅VLEN/SEWVLMAX = LMUL · VLEN/SEWVLMAX=LMUL⋅VLEN/SEW the largest number of elements one instruction can process; vl\mathit{vl}vl is the number it actually processes, and AVL the number the program still has to process. For the roofline, PPP is a machine’s peak arithmetic rate in FLOP/s, BBB its memory bandwidth in bytes/s, and III the arithmetic intensity of a computation: floating-point operations per byte moved to or from memory.

Worked example. Saxpy on 128-bit registers handles four elements per iteration: load four xxx values, load four yyy values, one fused multiply-add for all four, store four yyy values, then the loop overhead. That is about eight instructions per four elements, two per element instead of the scalar loop’s nine.

The roofline of the A100 and the kernels of the worked example:

RVV and GPUs

RVV: vsetvli grants \mathit{vl}≤VLMAX=LMUL·VLEN/SEW; strip mining loops until nothing remains — length not frozen in the ISA.

GPUs: thousands of threads in warps of 32 (SIMT), hide memory latency by switching warps; want coalesced accesses and low divergence.

Worked example (measured). The saxpy program of the Code section, one thread on an Apple M1 (macOS 26.5, Apple clang 21), with 2282^{28}228 element updates in total. With n=4,096n = 4{,}096n=4,096 the two arrays take 32 KB and stay in the 128 KB first-level cache; with n=224n = 2^{24}n=224 they take 128 MB and come from main memory on every pass. One run of each build gave, in GFLOP/s (two operations per element):

Worked example. With VLEN = 128, SEW = 32 and LMUL = 1, VLMAX=128/32=4VLMAX = 128/32 = 4VLMAX=128/32=4. For n=10, a=2, x=(1,2,…,10)x = (1, 2, \dots, 10)x=(1,2,…,10) and every yi=10y_i = 10yi​=10:

Nine instructions per element, of which two do the arithmetic and three move data; the other four are loop overhead. Every one of them is fetched, decoded and, on the out-of-order core of the instruction-level parallelism note, renamed, queued, woken up and retired. A four-wide core needs at least 9/4=2.259/4 = 2.259/4=2.25 cycles per element even if no instruction ever waits. The work per element does not change from one element to the next, and nothing in the hardware takes advantage of that.

Roofline

Roofline: attainable FLOP/s =\min(P, I· B), ridge I^=P/B (≈12.5 FLOP/byte on A100 FP32). Short vector ops → memory roof; blocked matrix product → compute roof. SIMD. One instruction, many elements — amortizes fetch/decode/schedule. A 128-bit register holds four floats or sixteen bytes; AVX-512 packs sixteen floats. Compilers vectorize loops without loop-carried dependences or aliasing (restrict helps). Measured M1 saxpy: 31 vs 6 GFLOP/s from cache; 8.2 vs 3.4 from memory — SIMD helps most when data already hit. RVV. vsetvli grants \mathit{vl}\≤\VLMAX=\LMUL\·\VLEN/\SEW; strip-mining loops until nothing remains — vector length is not frozen in the ISA, so the same binary scales across implementations. GPUs. Thousands of threads in warps of 32 (SIMT) hide memory latency by switching warps; want coalesced accesses and low divergence (threads in a warp taking the same branch). Occupancy trades registers/shared memory per thread against how many warps can stay resident. Roofline. Attainable FLOP/s =\min(P, I\· B) with intensity I in FLOP/byte; ridge I^=P/B (≈12.5 FLOP/byte on A100 FP32). Short vector ops sit under the memory roof; blocked matrix product climbs toward the compute roof. Profiling should report both achieved GFLOP/s and bytes moved, not only time. Strip-mining with RVV looks like: ask for vl, process vl elements, advance pointers by vl, repeat until the remainder is 0. The same binary runs on a core with VLEN=128 and one with VLEN=512; only the number of iterations changes. Predicated / masked operations let you handle the tail without a scalar cleanup loop, which matters when the leftover is a large fraction of a short array. GPU occupancy: if each thread uses too many registers, fewer warps fit on the SM and latency hiding collapses — a kernel can be memory-latency bound even with perfect coalescing. Prefer structures-of-arrays when warps should load adjacent floats; arrays-of-structures often force gather instructions. Mixed precision (fp16/bf16 compute, fp32 accumulate) raises arithmetic intensity on tensor cores while keeping the roofline’s compute roof in reach. Next: Making programs fast — measure, then change order and blocking. Worked example. For n=106n = 10^6n=106 and blocks of 256 threads, the launch creates ⌈106/256⌉=3,907\lceil 10^6/256 \rceil = 3{,}907⌈106/256⌉=3,907 blocks, 3,907⋅256=1,000,1923{,}907 · 256 = 1{,}000{,}1923,907⋅256=1,000,192 threads and 3,907⋅8=31,2563{,}907 · 8 = 31{,}2563,907⋅8=31,256 warps. The test i < n switches off the last 192 threads; they all fall in the last block, in its last six warps, so only those warps have masked lanes and no warp diverges in a way that costs a second pass over useful work. Worked example. For the A100 with B=1.555 TB/s, the ridge point is 19.5/1.555=12.519.5/1.555 = 12.519.5/1.555=12.5 FLOP/byte for single-precision arithmetic and 312/1.555=200.6312/1.555 = 200.6312/1.555=200.6 for the 16-bit tensor cores. The machine was shared with other

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.