Lecture 05 · Vectorized Execution on CPUs
The whole deck on two pages.
Page numbers are the numbers printed on the slides of ../lectures/Lecture_05_Vectorization.pdf (from slide 26 on, PDF page + 1).
SIMD basics
- Scalar (SISD): one operation on one item; SIMD: one operation on a vector of items pp.4–5
- Builds on pipelining, superscalar and OoO; an ISA extension p.3
- x86: MMX, SSE, AVX · Arm: NEON, SVE p.13
| Extension | Registers | Width | fp32 lanes | p. |
|---|---|---|---|---|
| SSE (1999) | — | 128 bit | 4 | 14 |
| AVX2 | 16 | 256 bit | 8 | 15 |
| AVX-512 | 32 | 512 bit | 16 | 15 |
- Instruction classes: load/store; arithmetic (ADD, SUB, MUL, DIV, SQRT, MAX, MIN); bitwise; compare (==, <, …) and shuffle; conversion; cache control (store bypassing the cache) pp.16–17
Three ways to vectorize
| Option | How | Catch | p. |
|---|---|---|---|
| automatic | compiler rewrites simple loops | simple loops are rare; aliasing blocks it | 18–20 |
| hints | restrict (distinct arrays), #pragma ivdep (ignore dependencies) |
restrict not in C++ standard; ivdep correctness is on you | 21–23 |
| intrinsics | _mm256_load_ps, _mm256_mul_ps, _mm256_store_ps |
potentially not portable | 24, 26 |
Dependencies and fixes
- Vectorizable only if steps are independent: no RAW or WAR hazards p.30
- SAXPY X[i] = a·X[i] + Y[i]: fine if X and Y don't overlap pp.31–32
- Sparse SAXPY X[K[i]] … Y[M[i]]: vectorizable only if no index collisions pp.33–34
- Explicit SAXPY: broadcast a → load 8 of X, Y → multiply, add → store p.36
- Dot product: loop-carried dependency on sum → partial sums, then one SIMD reduce pp.37–40
- Remainder: split_point = n − (n % 4); scalar tail runs < 4 times pp.41–43
- Branch in loop prevents vectorization → predication: X[i] = ((X[i] < 50) + 1) * X[i] pp.45–47
- SIMD branches: compare → mask → AND;
_mm256_blendv_epi8,_mm256_blend_epi32, masked load/store pp.49–50
Where it pays
- DB operations: scans, joins, sorting, aggregation p.27
- Column filter → bit vector: 8.4× dense, 2.7× sparse, TPC-H Q6 end-to-end 1.4× p.28
| 2010 (i7-960) | 2026 (Ryzen 9 9950X) | |
|---|---|---|
| Peak fp32 GFLOP/s | 102 | ~5 100 |
| DRAM bandwidth | 32 GB/s | ~90 GB/s |
| Bandwidth needed for peak | ~1.2 TB/s | ~61 TB/s |
| DRAM too slow by | ~38× | ~680× |
p.52
- Peak only with data in cache; GPUs use HBM against the memory wall p.52
- Advice: intrinsics only for non-trivial code; otherwise compiler-friendly code + pragmas; best: domain libraries p.53
Exam-style questions
The deck has no exam-style slide. The lesson's written section has four practice questions.