Parallelism & Multicore
Instruction-, data- and thread-level parallelism; Flynn's taxonomy; SIMD; multicore and NUMA; memory consistency; and Amdahl's and Gustafson's laws.
Since single-core speed stopped growing quickly (around 2005), extra performance comes from doing more things at once. Hardware offers parallelism at several levels, and software has to be written to use it.
Levels of parallelism
| Level | What runs in parallel | Who finds it |
|---|---|---|
| Instruction-level (ILP) | Independent instructions in one thread | Hardware (pipelining, superscalar, out-of-order) |
| Data-level (DLP) | The same operation on many data elements | Compiler / programmer (SIMD, GPUs) |
| Thread-level (TLP) | Independent threads or processes | Programmer / OS (multicore, SMT) |
| Request-level | Independent requests | Architecture (servers, clusters) |
Flynn's taxonomy
| Single data | Multiple data | |
|---|---|---|
| Single instruction | SISD — a classic scalar CPU | SIMD — vector units, GPUs (SIMT) |
| Multiple instruction | MISD — rare | MIMD — multicore CPUs, clusters |
SIMD: one instruction, many values
A 256-bit AVX2 register holds 8 floats; one vaddps adds 8 pairs at once. Compilers auto-vectorise simple loops; libraries like NumPy and BLAS are built on SIMD.
Vectorisation-friendly code has contiguous arrays, no dependencies between iterations, few branches (or branches that can be turned into masks), and no aliasing between arrays.
// Java's Vector API (incubating) gives explicit SIMD:
var species = FloatVector.SPECIES_256;
for (int i = 0; i < n; i += species.length()) {
var va = FloatVector.fromArray(species, a, i);
var vb = FloatVector.fromArray(species, b, i);
va.add(vb).intoArray(c, i);
}
// HotSpot's JIT also auto-vectorises simple loops.import numpy as np
c = a + b # one call; the loop runs in C using SIMD
# vs a Python loop: ~100x slower due to interpreter overhead// WebAssembly SIMD (v128) brings 128-bit SIMD to the browser;
// in plain JS, typed arrays + tight loops let the JIT do its best.
for (let i = 0; i < n; i++) c[i] = a[i] + b[i];Multicore and shared memory
Cores communicate through shared memory, kept consistent by cache coherence. On multi-socket servers, each socket has its own memory: NUMA (non-uniform memory access). Remote memory is perhaps 1.5–2× slower, so the OS and runtimes try to allocate memory on the node where the thread runs.
Memory consistency models
Coherence guarantees a single location looks consistent. Consistency defines the order in which writes to different locations become visible to other cores.
- Sequential consistency — all cores see one interleaving of all operations. Intuitive but slow; no real CPU provides it by default.
- x86 (TSO) — fairly strong: only a store followed by a load of a different location may appear reordered (because of store buffers).
- ARM / RISC-V — weak: many reorderings are allowed unless you use fences or acquire/release instructions.
That's why concurrent code must use proper synchronisation (locks, atomics, volatile in Java) rather than plain variables. Languages define their own memory models on top: the Java memory model and C++ std::atomic.
Amdahl's law
If a fraction p of a program can be parallelised over N cores:
| Parallel fraction p | 4 cores | 16 cores | ∞ cores |
|---|---|---|---|
| 50% | 1.6× | 1.9× | 2× |
| 90% | 3.1× | 6.4× | 10× |
| 99% | 3.9× | 13.9× | 100× |
The serial part dominates: with 10% serial work you can never exceed 10×.
Gustafson's law offers the optimistic counterpoint: in practice we use more cores to solve bigger problems in the same time, and the parallel part usually grows with the problem size, so scaled speed-up stays close to N.
Costs of parallelism
- Synchronisation — locks and atomics cost tens to hundreds of cycles, and contention serialises threads.
- Coherence traffic — shared writes bounce cache lines; false sharing.
- Load imbalance — the slowest thread determines completion time.
- Memory bandwidth — all cores share it, so memory-bound code stops scaling early.
Software side of the story: Threads, Synchronization, Java concurrency.
- Parallelism exists at the instruction, data, thread and request levels.
- SIMD applies one instruction to many values; NumPy and BLAS rely on it.
- Multicore shares memory via coherence; NUMA makes memory location matter.
- Weak memory models reorder operations; use proper synchronisation.
- Amdahl: the serial fraction caps speed-up; Gustafson: bigger problems scale better.