Lecture 06 · Concurrency and Parallelism on CPUs
The whole deck on two pages.
Page numbers are the numbers printed on the slides of ../lectures/Lecture_06_ConcurrencyParallelism.pdf.
Parallelism taxonomy
| Level | Mechanism | Extracted by | Coordinated via | Bounded by |
|---|---|---|---|---|
| Instruction | pipelining, superscalar, OoO | hardware | registers | hazards (RAW/WAR), branches |
| SIMD | AVX/SSE | compiler / intrinsics | predication, masks | dependencies, memory wall |
| Thread | threads on 10s of cores | programmer | locks, atomics, messages | Amdahl, sync overhead |
| Thread across NUMA | threads across sockets | programmer + OS | same locks + topology awareness | remote memory latency |
| Distributed | processes on many machines | programmer + frameworks | message passing | network, failures, consistency |
p.5
- Coordination cost grows with granularity, like the memory pyramid p.6
Amdahl's law
T_new = T(1 − p) + T·p/s Speedup = 1 / ((1 − p) + p/s) limit = 1 / (1 − p)- p = parallel fraction, s = parallelism factor p.8; 50/75/90/95% parallel → max 2/4/10/20× p.9
- Disk example: 1 000 × 4 KB at 1 GB/s = 4 ms I/O + 0.16 ms scan; 32 cores → 1.04× pp.10, 18
- Fix: read only age (4 of 64 B) → 0.25 ms, 1 core 10.1×, 32 cores 16.3×; overlap: T = max(T_io, T_scan); cache 4 MB in RAM pp.17–18
OS concepts
- Threads share an address space, cheaper switches; one crash may kill the process; no scaling across machines p.20
- Reschedule on: exit, block on I/O/semaphore, thread creation, I/O or clock interrupt p.21
- Context switch: pipeline flush, registers, maybe TLB flush, lost cache locality; CFS/EEVDF p.21
- System calls: orders of magnitude slower than function calls; not getting faster pp.22–23
Synchronization
- Processes: messages or shared memory (IPC); threads: shared variables p.25
- Guaranteed: cache-line atomic load/store, order of dependent instructions; rest may interleave p.27
| Kind | Examples | Waiting | p. |
|---|---|---|---|
| software / OS | semaphore, mutex | sleep | 28 |
| hardware | CAS, fetch-and-add, atomic logic ops | spin | 28 |
| fences | order w.r.t. earlier loads | — | 28 |
| futex | test-and-set in user space, kernel only on contention | sleep after failing | 31 |
- Rare writes → all lock types perform similarly p.30; futex: up to 100s of thousands ops/s p.31
Patterns
- RW lock: concurrent readers, exclusive writers; long writers block readers p.33
- Copy-on-write: lockless reads, writer copies + atomic pointer swap; pointer may return to an old value p.34
- Pipeline: stages + circular FIFO buffers, batches; speed = slowest stage; balance per-stage parallelism pp.35, 37
- NUMA: locking is latency-sensitive, batches bandwidth-sensitive; compute-heavy apps still gain from more threads p.38
Exam-style questions from the deck p.40
- What do a loop-carried dependency and a branching instruction have in common for a superscalar, pipelined CPU?
- T/F: compiler hints, used correctly, can express the same level of detail as intrinsics.
- Can
for x: for y in 0..16: sum[y] += A[x*16+y]be vectorized? Why or why not? - A software pipeline with one thread per stage needs no synchronization. Why or why not?
- The disk + parallel compute example.