---
title: Vector and SIMD processing
heading: Vector instructions, lanes and chimes, strip mining, memory banks, and SIMD extensions
eyebrow: S4 · Lesson 28 · Vector and SIMD processing
xp: 50
---

::: hook
When a program applies the same operation to thousands of elements, fetching and decoding one instruction per element wastes most of the effort. A vector instruction says "add these 64 pairs" once, and the hardware streams the elements through deeply pipelined units, several lanes at a time. Vector supercomputers from Cray pioneered it; today every CPU has SIMD extensions (SSE, AVX, NEON, the RISC-V V extension) and GPUs build on the same idea. Vectors make data parallelism cheap in energy and instruction bandwidth, as long as the vectors are long enough to pay off the startup cost and the memory system can feed them.
:::

::: play
- Cycles per element for a vector loop over n elements, in Hennessy and Patterson's model: $T_n = \lceil n/\text{MVL} \rceil(T_\text{loop} + T_\text{start}) + nT_\text{chime}$, with MVL the maximum vector length, $T_\text{loop}$ = 15 cycles of scalar loop overhead per strip, $T_\text{start}$ the pipeline startup per strip, and $T_\text{chime}$ the number of convoys (one cycle per element each).
- Short vectors are dominated by startup: 8 elements cost 11 cycles each. Long vectors approach $T_\text{chime} + (T_\text{loop} + T_\text{start})/\text{MVL}$.
- The sawtooth comes from strip mining: one element past a multiple of MVL starts a whole new strip.
:::
:::

```sim plot
view: graph
x: n = 1..2048 log
y: c = 0..20
slider: M = 8..256 = 64 log
slider: S = 10..100 = 49 cycles
slider: C = 1..5 step 1 = 3
plot: (ceil(n/M - 0.0001)*(15 + S) + n*C)/n | samples: 600 | label: cycles per element
readout: limit for long vectors = C + (15 + S)/M
```

::: predict
## A 16-core processor runs at 3 GHz, and each core has two AVX-512 fused multiply-add units working on double-precision numbers. What's its peak double-precision rate?

- [ ] 96 GFLOPS
- [ ] 384 GFLOPS
- [x] 1536 GFLOPS
- [ ] 3072 GFLOPS

::: reveal
1536 GFLOPS: 16 cores × 3 GHz × 2 units × 8 lanes (512 bits of 64-bit doubles) × 2 operations per fused multiply-add. A scalar core issuing one FMA per cycle would reach 96. Getting near the peak takes long vectors, aligned data and enough memory bandwidth, which the roofline model (lesson 31) quantifies; many real codes reach a small fraction of it.
:::
:::

::: nameit
## Vector execution

- A vector register holds up to MVL elements; a **vector-length register** handles shorter vectors, and **mask registers** handle conditionals.
- **Lanes:** the element operations of one instruction are spread over L parallel pipelines, so a vector of n elements takes about n/L cycles per unit.
- **Chaining** forwards results element by element to a dependent vector instruction, so a multiply and the add that uses it overlap.
- A **convoy** is a set of vector instructions that can execute together (no structural hazards); a **chime** is the time to stream one vector through one convoy. A loop with m convoys takes about m chimes, m·n/L cycles, plus startup.

## Strip mining

Vectors longer than MVL run in strips of MVL (the first or last shorter): $\lceil n/\text{MVL} \rceil$ strips, each paying loop overhead and pipeline startup.

## Memory

Vector loads need many words per cycle, supplied by many independent **banks**. A stride of s words over b banks (s dividing b) uses only b/s of them, so strided access can conflict; gather and scatter instructions handle indirect access (sparse data) at lower bandwidth.

## SIMD extensions

Short fixed-width vectors in a CPU: a W-bit register holds $W/e$ elements of e bits (AVX-512: 16 floats or 8 doubles). Peak rate = cores × clock × SIMD units × lanes × 2 for FMA.
:::

::: example
## A loop over 1000 elements runs on a machine with MVL = 64, $T_\text{start}$ = 49, $T_\text{loop}$ = 15 and 3 chimes. How many cycles, and how does that compare to 8 and 64 elements?

::: solution
1. **Strips.**
   $$\lceil 1000/64 \rceil = 16\ \text{strips}$$
2. **1000 elements.**
   $$\begin{aligned} T &= 16 \times (15 + 49) + 1000 \times 3 \\ &= 4024\ \text{cycles} \end{aligned}$$
   which is 4.02 per element.
3. **64 elements.**
   $$\begin{aligned} T &= 1 \times 64 + 192 \\ &= 256\ \text{cycles} \end{aligned}$$
   which is 4 per element.
4. **8 elements.**
   $$\begin{aligned} T &= 64 + 24 \\ &= 88\ \text{cycles} \end{aligned}$$
   which is 11 per element: short vectors waste most of their time starting up.

**Answer:** 4024 cycles for 1000 elements (4.02 per element), against 4 per element for 64 and 11 per element for 8.
:::
:::

::: example
## A vector unit reads from 16 memory banks, one word per bank per access. How many banks does a stride of 1, 2 and 8 words use, and how does that limit bandwidth?

::: solution
1. **Banks used.** Stride 1 uses all 16 banks, stride 2 uses 8 (every other bank), and stride 8 uses 2.
2. **Bandwidth.** If each bank can deliver one word every 4 cycles, the words per cycle are:
   $$\begin{aligned} \text{stride 1: } 16/4 &= 4 \\ \text{stride 8: } 2/4 &= 0.5 \end{aligned}$$
3. **The fix.** Padding array rows to an odd length moves column accesses onto all the banks.

**Answer:** strides of 1, 2 and 8 use 16, 8 and 2 banks; at one word per bank every 4 cycles, that's 4 words per cycle at stride 1 but only 0.5 at stride 8.
:::
:::

::: example
## A 16-core processor at 2.5 GHz has two AVX-512 fused multiply-add units per core. What is its peak double-precision rate?

::: solution
1. **Operations per unit per cycle.** Each 512-bit unit holds 8 doubles, and an FMA counts as 2 operations.
2. **Peak rate.**
   $$\begin{aligned} R &= 16 \times 2.5\ \text{GHz} \\ &\quad \times 2 \times 8 \times 2 \\ &= 1.28\ \text{TFLOP/s} \end{aligned}$$
3. **Reaching it.** It needs every core vectorized, both units busy every cycle and data arriving fast enough, which the roofline model (lesson 31) weighs.

**Answer:** the peak is 1.28 TFLOP/s in double precision.
:::
:::

::: check
given: n = 100 | 500 | 1500; M = 32 | 64 | 128
answer: ceil(n/M - 0.0001)
tolerance: 0

## A loop over {n} elements runs on a vector machine with a maximum vector length of {M}. How many strips does strip mining create?

::: right
Right. $\lceil n/\text{MVL} \rceil$ = {answer}.
:::

::: misses
- [floor(n/M + 0.0001)] Round up: the leftover elements need a strip too.
- [any] Not yet. Strips of at most MVL elements.
:::
:::

::: check
given: n = 200 | 800; M = 64 | 128; L = 10 | 15; S = 30 | 60; C = 2 | 4
answer: ceil(n/M - 0.0001)*(L + S) + n*C
tolerance: 0.5%

## A vector loop of {n} elements: MVL {M}, loop overhead {L} and startup {S} cycles per strip, {C} chimes. How many cycles does it take?

::: right
Right. $\lceil n/\text{MVL} \rceil(T_\text{loop} + T_\text{start}) + nT_\text{chime}$ = {answer}.
:::

::: misses
- [n*C] Each strip also pays overhead and startup.
- [any] Not yet. Strips × per-strip cost, plus elements × chimes.
:::
:::

::: check
given: W = 128 | 256 | 512; e = 8 | 16 | 32 | 64
answer: W/e
tolerance: 0

## How many {e}-bit elements fit in a {W}-bit SIMD register?

::: right
Right. {answer} lanes.
:::

::: misses
- [any] Not yet. Register width over element width.
:::
:::

::: check
given: c = 4 | 16 | 64; f = 2 | 3.5; u = 1 | 2; l = 4 | 8 | 16
answer: c*f*u*l*2
unit: GFLOPS
tolerance: 0.5%

## A chip has {c} cores at {f} GHz, each with {u} FMA unit(s) of {l} lanes. What's its peak rate, counting an FMA as two operations?

::: right
Right. {answer} GFLOPS.
:::

::: misses
- [c*f*u*l] A fused multiply-add counts as two floating-point operations.
- [any] Not yet. Multiply every factor, times 2 for FMA.
:::
:::

::: check
given: b = 8 | 16 | 32; s = 2 | 4 | 8
answer: b/s
tolerance: 0

## A memory has {b} interleaved banks. How many banks does a stride of {s} words use?

::: right
Right. b/s = {answer}.
:::

::: misses
- [b] A stride that shares a factor with the bank count keeps hitting the same banks.
- [any] Not yet. Every s-th bank, wrapping around.
:::
:::

::: check
given: m = 2 | 3 | 4; n = 64 | 128; L = 2 | 4 | 8
answer: m*n/L
tolerance: 0.5%

## A loop has {m} convoys over vectors of {n} elements on a machine with {L} lane(s). Ignoring startup, how many cycles does it take?

::: right
Right. $mn/L$ = {answer}.
:::

::: misses
- [m*n] Lanes divide the per-convoy time.
- [any] Not yet. Chimes × elements ÷ lanes.
:::
:::

::: check
kind: parts
given: n = 100 | 1000 | 5000; M = 32 | 64; S = 30 | 49; C = 2 | 3 | 4

## A vector loop over {n} elements runs with MVL = {M}, 15 cycles of loop overhead and {S} of startup per strip, and {C} chimes.

::: part
answer: ceil(n/M - 1e-9)
tolerance: 0

### How many strips does it take?

::: misses
- [any] $\lceil n/\text{MVL}\rceil$.
:::
:::

::: part
answer: ceil(n/M - 1e-9)*(15 + S) + n*C
unit: cycles
tolerance: 0.5%

### How many cycles does it take?

::: misses
- [n*C] Each strip pays loop overhead and startup.
- [any] $\lceil n/\text{MVL}\rceil(T_\text{loop} + T_\text{start}) + nT_\text{chime}$.
:::
:::
:::

::: check
kind: parts
given: b = 16 | 32; s = 1 | 2 | 4 | 8

## A vector unit reads from {b} memory banks with a stride of {s} words.

::: part
answer: b/s
unit: banks
tolerance: 0

### How many banks does it use?

::: misses
- [any] $b/s$ when s divides b.
:::
:::

::: part
answer: 1/s
tolerance: 0.5%

### What fraction of the unit-stride bandwidth does it get?

::: misses
- [any] Banks used over banks available.
:::
:::
:::

::: check
kind: derivation
vars: c = 1..128, f = 1e9..5e9, u = 1..4, W = 128..1024, e = 8..64

## A processor has c cores at clock f, each with u FMA units of W bits operating on e-bit elements.

::: step
answer: W/e

### How many elements does each unit process per instruction?
:::

::: step
answer: c*f*u*(W/e)*2

### What is the peak rate in operations per second?
:::

::: right
Right. Halving the element width doubles the peak, which is why machine learning moved to 16- and 8-bit formats.
:::
:::

::: explain
## Why are vector instructions more energy-efficient than the equivalent scalar loop?

::: model
In a scalar loop, every element operation pays for fetching, decoding, renaming, scheduling and committing its own instructions, plus the loop's address and branch instructions, and that overhead costs far more energy than the arithmetic itself. A vector instruction pays that overhead once for many elements: one fetch and decode describes dozens of operations, and the hardware knows in advance that they're independent, so it can stream them through the units without any dependence checking. Memory accesses are described as whole regular patterns too, which lets the hardware fetch them efficiently. More of the energy goes into useful arithmetic, which is why GPUs, AI accelerators and every modern CPU lean on vectors.
:::
:::

::: practice

::: check
given: n = 300 | 2000; M = 64 | 256
answer: ceil(n/M - 0.0001)
tolerance: 0

## Strips for {n} elements, MVL {M}?

::: misses
- [any] $\lceil n/\text{MVL} \rceil$.
:::
:::

::: check
given: c = 8 | 32; f = 2.5 | 3; u = 2; l = 8 | 16
answer: c*f*u*l*2
unit: GFLOPS
tolerance: 0.5%

## Peak: {c} cores, {f} GHz, {u} FMA units of {l} lanes?

::: misses
- [any] Product × 2.
:::
:::

::: check
given: m = 3 | 5; n = 64 | 256; L = 2 | 8
answer: m*n/L
tolerance: 0.5%

## Cycles: {m} convoys, {n} elements, {L} lanes?

::: misses
- [any] $mn/L$.
:::
:::
:::
