GPUs & Accelerators
Why GPUs have thousands of simple cores, how SIMT, warps and memory coalescing work, the CUDA programming model, and TPUs/NPUs for AI.
A CPU is designed to run one thread very fast: big caches, branch prediction and out-of-order logic. A GPU is designed to run thousands of threads at once, spending its transistors on arithmetic units instead. For massively parallel work like graphics, matrix multiplication and neural networks, that trade-off gives 10–100× more throughput.
- 8–128 powerful cores
- Large caches, branch prediction, out-of-order execution
- Optimised for latency: finish one task quickly
- Great for branchy, serial, general-purpose code
- Thousands of simple lanes grouped into streaming multiprocessors (SMs)
- Small caches, huge register files, very high memory bandwidth (HBM: 3+ TB/s)
- Optimised for throughput: finish many tasks
- Great for data-parallel arithmetic
SIMT: single instruction, multiple threads
GPU threads are grouped into warps (32 threads on NVIDIA) that execute the same instruction in lockstep on different data. To the programmer they look like independent threads; to the hardware they're a wide SIMD unit.
Hiding latency with parallelism
GPUs don't avoid memory latency with big caches; they hide it. Each SM holds many warps' state at once, and when one warp waits on memory, the scheduler switches to another ready warp in a single cycle (no context-switch cost). This needs many threads in flight, which is why GPU kernels launch millions of threads.
The CUDA programming model
// Add two vectors on the GPU: one thread per element
__global__ void add(const float* a, const float* b, float* c, int n) {
int i = blockIdx.x * blockDim.x + threadIdx.x;
if (i < n) c[i] = a[i] + b[i];
}
// Host: copy data to the GPU, launch ceil(n / 256) blocks of 256 threads
add<<<(n + 255) / 256, 256>>>(d_a, d_b, d_c, n);| Memory | Scope | Speed |
|---|---|---|
| Registers | Per thread | Fastest |
| Shared memory | Per block (programmer-managed scratchpad) | Very fast (~L1) |
| L2 cache | Whole GPU | Fast |
| Global memory (HBM/GDDR) | Whole GPU | High bandwidth, high latency |
| Host memory over PCIe | CPU side | Slow — minimise transfers |
Performance rules of thumb
- Coalesce memory accesses: neighbouring threads should read neighbouring addresses so a warp's 32 loads merge into a few wide transactions.
- Reuse data in shared memory (tiling), exactly like cache blocking on CPUs.
- Keep the GPU busy: launch enough threads, overlap transfers with compute, and avoid round trips to the CPU.
- Arithmetic intensity (FLOPs per byte moved) decides whether a kernel is compute-bound or memory-bound (the roofline model). Matrix multiplication is compute-bound; element-wise operations are memory-bound, which is why ML frameworks fuse them.
Accelerators for AI
| Accelerator | Idea | Examples |
|---|---|---|
| Tensor cores | Hardware units doing small matrix multiply-accumulates (e.g. 4×4) per instruction, in fp16/bf16/fp8 | NVIDIA since Volta |
| TPU | A large systolic array where data flows through a grid of multiply-accumulate cells | Google TPU |
| NPU | Low-power inference engines in phones and laptops | Apple Neural Engine, Qualcomm Hexagon |
| FPGA | Reconfigurable hardware for custom pipelines | Networking, low-latency trading |
| ASIC | Fixed-function chips; maximum efficiency for one task | Video codecs, crypto mining |
When to use a GPU
Good fit: large, regular, data-parallel work — linear algebra, deep learning, image and video processing, simulations, rendering, some databases and analytics.
Poor fit: small inputs (transfer overhead dominates), branchy or irregular logic, strongly sequential algorithms, and anything dominated by I/O.
- CPUs optimise latency; GPUs optimise throughput with thousands of simple lanes.
- SIMT: warps of 32 threads execute in lockstep; divergence serialises paths.
- GPUs hide memory latency by switching between many resident warps.
- CUDA: grid → blocks (with shared memory) → threads; coalesce accesses, reuse data, minimise PCIe transfers.
- Tensor cores, TPUs and NPUs specialise in matrix maths for AI.