Parallelism & Multicore

Instruction-, data- and thread-level parallelism; Flynn's taxonomy; SIMD; multicore and NUMA; memory consistency; and Amdahl's and Gustafson's laws.

Advanced 3 min readLesson 14 of 15

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

LevelWhat runs in parallelWho finds it
Instruction-level (ILP)Independent instructions in one threadHardware (pipelining, superscalar, out-of-order)
Data-level (DLP)The same operation on many data elementsCompiler / programmer (SIMD, GPUs)
Thread-level (TLP)Independent threads or processesProgrammer / OS (multicore, SMT)
Request-levelIndependent requestsArchitecture (servers, clusters)

Flynn's taxonomy

Single dataMultiple data
Single instructionSISD — a classic scalar CPUSIMD — vector units, GPUs (SIMT)
Multiple instructionMISD — rareMIMD — 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.

a0+b00a1+b11a2+b22a3+b33a4+b44a5+b55a6+b66a7+b77
One AVX instruction: eight additions.

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.

Multicore and shared memory

Core 0L1 + L2Core 1L1 + L2Core 2L1 + L2Core 3L1 + L2Shared L3 + coherence fabricMemory controllers → DRAM

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:

Speedup(N)=1(1−p)+pN\text{Speedup}(N) = \frac{1}{(1 - p) + \dfrac{p}{N}}
Parallel fraction p4 cores16 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.

Key takeaways
  • 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.