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

S4 · Lesson 28 · Vector and SIMD processing

Vector instructions, lanes and chimes, strip mining, memory banks, and SIMD extensions

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.

  • Cycles per element for a vector loop over n elements, in Hennessy and Patterson's model: Tn=⌈n/MVL⌉(Tloop+Tstart)+nTchime, with MVL the maximum vector length, Tloop = 15 cycles of scalar loop overhead per strip, Tstart the pipeline startup per strip, and Tchime the number of convoys (one cycle per element each).
  • Short vectors are dominated by startup: 8 elements cost 11 cycles each. Long vectors approach Tchime+(Tloop+Tstart)/MVL.
  • The sawtooth comes from strip mining: one element past a multiple of MVL starts a whole new strip.

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?

Name it

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): ⌈n/MVL⌉ 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.

Worked example

A loop over 1000 elements runs on a machine with MVL = 64, Tstart = 49, Tloop = 15 and 3 chimes. How many cycles, and how does that compare to 8 and 64 elements?

Show the solution
  1. Strips. ⌈1000/64⌉=16strips
  2. 1000 elements. T=16×(15+49)+1000×3=4024cycles which is 4.02 per element.
  3. 64 elements. T=1×64+192=256cycles which is 4 per element.
  4. 8 elements. T=64+24=88cycles 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.

Worked 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?

Show the 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: stride 1: 16/4=4stride 8: 2/4=0.5
  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.

Worked 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?

Show the solution
  1. Operations per unit per cycle. Each 512-bit unit holds 8 doubles, and an FMA counts as 2 operations.
  2. Peak rate. R=16×2.5GHz×2×8×2=1.28TFLOP/s
  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

A loop over 500 elements runs on a vector machine with a maximum vector length of 32. How many strips does strip mining create?

Check

A vector loop of 200 elements: MVL 64, loop overhead 15 and startup 60 cycles per strip, 2 chimes. How many cycles does it take?

Check

How many 8-bit elements fit in a 256-bit SIMD register?

Check

A chip has 16 cores at 2 GHz, each with 2 FMA unit(s) of 16 lanes. What's its peak rate, counting an FMA as two operations?

Check

A memory has 16 interleaved banks. How many banks does a stride of 2 words use?

Check

A loop has 3 convoys over vectors of 64 elements on a machine with 8 lane(s). Ignoring startup, how many cycles does it take?

Check

A vector loop over 1000 elements runs with MVL = 32, 15 cycles of loop overhead and 49 of startup per strip, and 4 chimes.

(a)

How many strips does it take?

(b)

How many cycles does it take?

Check

A vector unit reads from 16 memory banks with a stride of 1 words.

(a)

How many banks does it use?

(b)

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

Check

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

Step 1

How many elements does each unit process per instruction?

Type it the way you would write it, like 3sin(2πft) or 3*sin(2*pi*f*t). Angles can be in radians or carry a degree sign, like 45°.

Explain why

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

Practice

Keep practicing

11 kinds of problem on this topic, dealt one at a time with new numbers every time, for as long as you like.