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.

Advanced 3 min readLesson 15 of 15

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.

CPU
  • 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
GPU
  • 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

Grid (one kernel launch)Block 0shared memoryBlock 1shared memoryBlock …runs on any SMthreads → warps of 32
C++
// 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);
MemoryScopeSpeed
RegistersPer threadFastest
Shared memoryPer block (programmer-managed scratchpad)Very fast (~L1)
L2 cacheWhole GPUFast
Global memory (HBM/GDDR)Whole GPUHigh bandwidth, high latency
Host memory over PCIeCPU sideSlow — 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

AcceleratorIdeaExamples
Tensor coresHardware units doing small matrix multiply-accumulates (e.g. 4×4) per instruction, in fp16/bf16/fp8NVIDIA since Volta
TPUA large systolic array where data flows through a grid of multiply-accumulate cellsGoogle TPU
NPULow-power inference engines in phones and laptopsApple Neural Engine, Qualcomm Hexagon
FPGAReconfigurable hardware for custom pipelinesNetworking, low-latency trading
ASICFixed-function chips; maximum efficiency for one taskVideo 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.

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