17. Multiprocessor Systems and Hardware Accelerators
As computing scale increases, the idealized model of a single, uniform pool of memory and a set of identical cores gives way to more complex architectures. Large-scale systems are characterized by Non-Uniform Memory Access (NUMA) topologies, complex cache coherency protocols, and the tight integration of specialized hardware accelerators for Machine Learning (ML) and High-Performance Computing (HPC). These hardware realities impose unique constraints on operating system design, runtime libraries, and systems software.
17.1 NUMA Topologies and Locality Optimization
In a NUMA architecture, the system is composed of multiple processor sockets or chiplets, each possessing its own local memory controller and associated Dynamic Random-Access Memory (DRAM) (Figure 17-1). While any core can access any physical address, latency and bandwidth depend strongly on physical distance between the executing core and the target memory controller. Accessing local DRAM is fast, whereas traversing the inter-socket interconnect (such as Intel Ultra Path Interconnect (UPI), AMD Infinity Fabric, or Apple UltraFusion) incurs a measurable latency and bandwidth penalty.
To preserve locality, operating systems combine two policies: processor affinity (scheduling threads onto cores belonging to the socket that holds their working set) and first-touch placement (allocating physical frames on the socket of the thread that first faults and writes a newly mapped page, rather than the thread that merely called mmap()).
17.2 Cache Coherence and the False Sharing Phenomenon
Hardware-level cache coherence guarantees that all cores observe a consistent value for every memory address, typically via variants of the Modified-Exclusive-Shared-Invalid (MESI) or Modified-Owned-Exclusive-Shared-Invalid (MOESI) state machine (Figure 17-2). Each 64-byte cache line in a core’s private cache carries a coherence state: Invalid (not present), Shared (clean copy potentially held by multiple cores), Exclusive (clean copy held by this core alone), or Modified (dirty copy owned exclusively by this core). Before a core can write to a Shared line, it must broadcast a Read-For-Ownership (RFO) invalidation across the interconnect, forcing every sibling core to mark its copy Invalid.
This cache-line granularity creates false sharing (Figure 17-3): when two threads update logically independent variables that happen to reside within the same 64-byte cache line, every store invalidates the peer core’s cache line, causing the line to bounce continuously across the interconnect and degrading throughput by an order of magnitude. Padding concurrently written fields to cache-line boundaries (alignas(64) in C++20; #[repr(align(64))] in Rust) eliminates the contention completely, as verified in Listings 17-1 and 17-2.
Listing 17-1. Two counters in one cache line and in two (C++20). (code/ch17/false_sharing.cpp)
// ch17/false_sharing.cpp: two hot counters, one cache line versus two.
// The counters use atomic increments: the standard pattern for shared counters.
#include <atomic>
#include <chrono>
#include <cstdint>
#include <cstdio>
#include <thread>
struct Unpadded {
alignas(64) std::atomic<uint64_t> a{0};
std::atomic<uint64_t> b{0}; // shares a's cache line
};
struct Padded {
alignas(64) std::atomic<uint64_t> a{0};
alignas(64) std::atomic<uint64_t> b{0}; // each field owns a line
};
template <typename C>
double run(C& c) {
constexpr uint64_t kIters = 40'000'000;
auto t0 = std::chrono::steady_clock::now();
std::thread t1([&] {
for (uint64_t i = 0; i < kIters; ++i) {
c.a.fetch_add(1, std::memory_order_relaxed);
}
});
std::thread t2([&] {
for (uint64_t i = 0; i < kIters; ++i) {
c.b.fetch_add(1, std::memory_order_relaxed);
}
});
t1.join();
t2.join();
auto t1e = std::chrono::steady_clock::now();
return std::chrono::duration<double, std::milli>(t1e - t0).count();
}
int main() {
Unpadded u;
Padded p;
run(u); // warmup
run(p);
double tu = run(u);
double tp = run(p);
printf("unpadded (shared line): %7.1f ms\n", tu);
printf("padded (separate) : %7.1f ms\n", tp);
printf("ratio : %5.2fx\n", tu / tp);
printf("(unpadded %llu, padded %llu)\n",
static_cast<unsigned long long>(u.a.load()),
static_cast<unsigned long long>(p.a.load()));
}
Output 17-1. One run on the verification machine.
unpadded (shared line): 1745.7 ms
padded (separate) : 340.6 ms
ratio : 5.13x
(unpadded 80000000, padded 80000000)
Listing 17-2. The same experiment in Rust, with atomic counters. (code/ch17/false_sharing.rs)
// ch17/false_sharing.rs: the same experiment with atomic counters.
use std::sync::atomic::{AtomicU64, Ordering};
use std::thread;
use std::time::Instant;
#[repr(C, align(64))]
struct Unpadded {
a: AtomicU64,
b: AtomicU64, // shares a's cache line
}
#[repr(C, align(64))]
struct Padded {
a: AtomicU64,
pad: [u64; 7],
b: AtomicU64, // 64 bytes after a: its own line
}
fn bench(a: &AtomicU64, b: &AtomicU64) -> f64 {
const ITERS: u64 = 40_000_000;
let t0 = Instant::now();
thread::scope(|s| {
s.spawn(|| {
for _ in 0..ITERS {
a.fetch_add(1, Ordering::Relaxed);
}
});
s.spawn(|| {
for _ in 0..ITERS {
b.fetch_add(1, Ordering::Relaxed);
}
});
});
t0.elapsed().as_secs_f64() * 1000.0
}
fn main() {
let u = Unpadded { a: AtomicU64::new(0), b: AtomicU64::new(0) };
let p = Padded { a: AtomicU64::new(0), pad: [0; 7], b: AtomicU64::new(0) };
bench(&u.a, &u.b); // warmup
bench(&p.a, &p.b);
let tu = bench(&u.a, &u.b);
let tp = bench(&p.a, &p.b);
println!("unpadded (shared line): {tu:7.1} ms");
println!("padded (separate) : {tp:7.1} ms");
println!("ratio : {:5.2}x", tu / tp);
println!(
"(unpadded {}, padded {})",
u.a.load(Ordering::Relaxed),
p.a.load(Ordering::Relaxed)
);
}
Output 17-2.
unpadded (shared line): 1748.2 ms
padded (separate) : 337.5 ms
ratio : 5.18x
(unpadded 80000000, padded 80000000)
Hardware cache coherence guarantees correctness, not performance. Independent variables written concurrently must be padded to 64-byte cache line boundaries (alignas(64)); otherwise, hardware invalidation traffic serializes execution cores across the memory bus.
Run Listing 17-1 (code/ch17/false_sharing.cpp) or Listing 17-2 (code/ch17/false_sharing.rs) and compare elapsed time between the packed struct (both counters on one 64-byte cache line) and the alignas(64) padded struct. Notice how separating the two counters by 64 bytes eliminates MESI RFO ping-ponging and speeds up execution several-fold.
17.3 Hardware Accelerators: Discrete Architecture vs. Unified Memory Architecture (UMA)
Beyond general-purpose processors, modern servers increasingly rely on specialized hardware accelerators (such as Graphics Processing Units (GPUs), Neural Processing Units (NPUs), and cryptographic Application-Specific Integrated Circuits (ASICs)) to execute ML and HPC workloads across two distinct memory architectures.
17.3.1 The Discrete Accelerator Model (PCIe + Dedicated VRAM)
In traditional enterprise servers (such as x86 hosts paired with discrete Peripheral Component Interconnect Express (PCIe) GPUs), the host processor and accelerator inhabit separate physical memory domains: general-purpose Double Data Rate (DDR4/DDR5) DRAM on the host, and High Bandwidth Memory (HBM) or Graphics Double Data Rate (GDDR) Video Random-Access Memory (VRAM) soldered onto the accelerator card. Executing a workload requires Direct Memory Access (DMA) transfers across PCIe: the operating system pins host pages (mlock or kernel page pinning) so the virtual memory subsystem cannot relocate frames mid-transfer, copies inputs across the PCIe bus (32 to 64 GB/s on PCIe 4.0/5.0, far slower than on-die memory), runs the kernel on the GPU, and copies results back to host DRAM.
17.3.2 Unified Memory Architecture (UMA) on Apple Silicon
Apple Silicon servers and high-performance Systems-on-Chip (SoCs) integrate CPU cores, GPU cores, Neural Engine (NPU), and media engines onto a single silicon package sharing one wide memory pool (exceeding 800 GB/s on Ultra-tier parts). Because every compute unit addresses the same physical memory through a shared System Level Cache (SLC), PCIe copies and host pinning vanish: a daemon can ingest network packets on the CPU and pass the buffer pointer directly to the GPU or NPU with sub-microsecond dispatch latency.
17.3.3 Heterogeneous Cache Coherency and Memory Barriers
While UMA eliminates physical memory copies, CPUs and accelerators maintain distinct local caches: CPUs optimize for low-latency coherent Level 1/Level 2 caches, whereas GPUs and NPUs rely on wide streaming write-combining buffers and tile caches. Without explicit synchronization, an accelerator can read stale data still sitting in dirty CPU cache lines, or the CPU can read stale cached bytes after the accelerator finishes. Systems runtimes on UMA platforms therefore enforce explicit cache flushes and invalidations alongside hardware memory fences and command-queue completion events (such as Apple Metal command buffers and shared events) to establish strict happens-before visibility across heterogeneous compute units.
In Unified Memory Architectures, “zero-copy” does not mean “zero-synchronization.” Forgetting to flush CPU write buffers or neglecting GPU completion fences leads to silent, non-deterministic data corruption that cannot be caught by standard CPU memory sanitizers.
- NUMA systems enforce non-uniform access latencies; schedulers must optimize for socket affinity and first-touch memory allocation.
- False sharing occurs when concurrent threads write distinct variables within the same 64-byte cache line; align concurrently written data to 64 bytes (
alignas(64)). - Discrete accelerators are constrained by PCIe DMA transfer latency, host memory pinning, and separate memory pools.
- Apple Silicon Unified Memory Architecture (UMA) provides zero-copy memory sharing across CPU, GPU, and NPU cores, reaching sub-microsecond accelerator dispatch.
- Systems programming for UMA requires explicit management of cache flushes, memory barriers, and synchronization events across heterogeneous compute cores.