Throughput machines, not fast cores
A CPU core is built to finish one thread's instructions as soon as possible: large caches, branch prediction, out-of-order execution. A GPU is built to finish a huge number of similar, independent calculations per second: thousands of simple lanes, little cache per lane, and latency hidden by always having other work ready. The objective of this lesson is to reason about which of those two shapes a quant workload has, and to estimate the speed-up, including the cost of moving data, before anyone writes a kernel.
SIMT and warps
You write a kernel: a function that one thread runs for one element. You launch it over a grid of blocks, each of many threads. The hardware groups threads into warps of 32 (NVIDIA; AMD calls them wavefronts, 64 lanes on its data-centre GPUs) that execute the same instruction at the same time on different data. This is SIMT, single instruction, multiple threads: it is SIMD with the lanes presented as threads.
// CUDA (illustrative; requires nvcc and an NVIDIA GPU)
__global__ void saxpy(int n, float a, const float* x, float* y) {
int i = blockIdx.x * blockDim.x + threadIdx.x; // this thread's element
if (i < n) y[i] = a * x[i] + y[i];
}
// launch: saxpy<<<(n + 255) / 256, 256>>>(n, a, d_x, d_y);
Two consequences follow from all 32 lanes sharing one instruction stream:
- Divergence. If some lanes of a warp take an
ifand others take theelse, the warp runs both paths one after the other, with the inactive lanes masked off. A warp's time is the sum of the paths any of its lanes take. - Coalescing. When the 32 lanes read 32 consecutive 4-byte words, the hardware serves the warp with one 128-byte memory transaction. If they read scattered addresses, it needs up to 32 transactions. Consecutive threads should touch consecutive addresses: structure of arrays again.
Memory hierarchy
| Level | Scope | Typical size (order of magnitude) | Notes |
|---|---|---|---|
| Registers | one thread | up to 255 32-bit registers per thread | fastest; the per-SM register file is shared by all resident threads |
| Shared memory / L1 | one block, on one SM | tens to a couple of hundred KB per SM | programmer-managed scratchpad for data reused within a block |
| L2 cache | whole GPU | tens of MB | |
| Global memory (HBM or GDDR) | whole GPU | tens of GB | 1–3+ TB/s on data-centre GPUs, but hundreds of cycles of latency |
| Host memory over PCIe | CPU side | about 25–32 GB/s for PCIe 4.0 x16, about double for PCIe 5.0 |
An SM (streaming multiprocessor) is the GPU's equivalent of a core. The bandwidth gap between the last two rows, roughly 100×, is the single most important number in deciding whether to use a GPU.
Occupancy and latency hiding
A global-memory load takes hundreds of cycles. The GPU does not wait: each SM keeps many warps resident and, every cycle, issues from one that is ready. Occupancy is resident warps divided by the SM's maximum. It is limited by whichever resource runs out first: registers per thread, shared memory per block, or threads per block. Higher occupancy hides more latency, but it is a means, not a goal: a kernel with good data reuse in registers can run fast at modest occupancy.
When a GPU helps
- Monte Carlo pricing and risk. Millions of independent paths, the same arithmetic on each, a reduction at the end. Each thread needs its own random stream (counter-based generators such as Philox, used by cuRAND, are designed for this).
- Large dense linear algebra. Matrix multiply does work on data, so arithmetic per byte grows with and the GPU's compute is fully used. Covariance matrices for thousands of assets, PCA, large least squares.
- ML training and batch inference. Mostly matrix multiplies over large batches.
- Large grids: PDE solvers and scenario grids where every cell is updated the same way.
When it does not
- Latency-critical, small work. Reacting to one order-book update has microseconds of budget. Launching a kernel costs on the order of 5 µs, and copying inputs and results over PCIe adds a few more. The CPU finishes the whole decision before the GPU starts.
- Branchy, irregular code. Order matching, parsing exchange messages, tree walks: divergence and scattered memory access waste most of the lanes.
- Little work per byte moved. If the data starts in host memory and the kernel does a few operations per element, the PCIe copy dominates (worked example below).
- Inherently serial chains, such as a path-dependent recursion that cannot be split.
Amdahl's law
If a fraction of the run time can be accelerated by a factor , and the rest cannot, the overall speed-up is
As , . With the ceiling is 20× however fast the GPU is; with it is 10×. For a GPU the "unaccelerated" part must include everything that does not run on the device: setup on the host, the copies across PCIe, and combining results.
Worked example
a risk job with and without the transfer
A nightly scenario-risk job takes 2.0 s on the CPU: 0.1 s of serial setup and 1.9 s of perfectly parallel scenario evaluation. A GPU runs the scenario evaluation 40× faster. The inputs are 4 GB in host memory and must be copied to the GPU; the results are 40 MB and come back. Assume an effective PCIe rate of 25 GB/s. What is the speed-up (a) ignoring transfers, (b) including them?
Show solutionHide solution
(a) Ignoring transfers. The parallel fraction is and :
Equivalently, the new time is s, and .
(b) Including transfers. Copy in: s. Copy out: s. New time:
The copy-in alone takes more than three times as long as the GPU computation. What to say next in an interview: keep the inputs resident on the GPU between runs if they change slowly (market data snapshots, the scenario set), generate random numbers on the device instead of copying them, overlap copies with computation using streams so that block moves while block computes, and reduce the bytes (single precision where the error budget allows).
Worked example
divergence and occupancy
(a) In a kernel, each thread independently takes a cheap path costing 100 cycles with probability 0.95, and an expensive path costing 300 cycles with probability 0.05. On a CPU, the average cost per element is the expectation. What is the expected cost of a 32-lane warp, and how much worse is it than the per-element average?
(b) An SM allows at most 64 resident warps (2,048 threads) and has 65,536 32-bit registers (these are NVIDIA A100 limits). A kernel uses 64 registers per thread. Ignoring allocation granularity and other limits, what is its maximum occupancy? What if it used 40 registers?
Show solutionHide solution
(a) Per element on a CPU: cycles.
A warp pays for each path that at least one of its 32 lanes takes. The expensive path is taken by some lane with probability ; the cheap path by some lane with probability . Expected warp time:
about times the per-element average. A rare branch is not rare at the warp level. Fixes: sort or bin the work so that elements taking the same path sit in the same warps, or make the expensive path branch-free.
(b) Threads that fit by registers: , which is warps, so occupancy is . With 40 registers: threads, so 51 whole warps, about (real allocation granularity lowers this somewhat). Whether the extra occupancy speeds the kernel up depends on whether it was waiting on memory; forcing fewer registers can also cause spills to slow local memory.
Arithmetic intensity: the quick test
Divide floating-point operations by bytes moved. saxpy does 2 flops for 12 bytes. Run on floats that start in host memory:
| Step | Bytes | Rate (typical) | Time |
|---|---|---|---|
| Copy to GPU and back over PCIe | 1.2 GB | 25 GB/s | about 48 ms |
| Kernel on the GPU | 1.2 GB | 2 TB/s | about 0.6 ms |
| Same loop on the CPU, memory-bound | 1.2 GB | 50 GB/s | about 24 ms |
The GPU kernel is 40× faster than the CPU loop, and the whole GPU job is 2× slower, because the data had to cross PCIe. A Monte Carlo pricer is the opposite case: a few bytes of parameters in, millions of paths of arithmetic, one number out per instrument. Ask "where does the data live and how many operations per byte?" before "how many cores does the GPU have?".
Knowledge check
In one warp, 16 lanes take the if-branch (cost 200 cycles) and 16 take the else-branch (cost 300 cycles). Roughly how long does the warp take for this code?
Knowledge check
90% of a job's run time is perfectly parallel and the rest is serial. A GPU makes the parallel part effectively infinitely fast. What is the best possible overall speed-up?
Knowledge check
Which workload is the weakest candidate for a GPU?
Key takeaways
- A GPU runs warps of 32 threads in lockstep (SIMT). Divergent branches cost the sum of the paths; scattered memory access breaks coalescing.
- Memory: registers, shared memory, L2, then global memory at TB/s, then host memory at tens of GB/s across PCIe. The PCIe gap often decides the answer.
- Occupancy (resident warps over the maximum) hides memory latency and is limited by registers, shared memory and block size.
- Use Amdahl's law with the copies counted as serial time: 95% parallel and 40× on that part gives 13.6× before transfers, 6.5× after copying 4 GB.
- GPUs suit Monte Carlo, large linear algebra and ML training; they do not suit microsecond, branchy, per-event decisions.
Practice problems
Finished reading?
Answer the 3 knowledge checks to complete this lesson (0 done).