Skip to content
LeetQuant
E11 High-Performance Computing

E11 · High-Performance Computing · Lesson 2 of 4

GPU Computing

The GPU execution model (SIMT, warps, divergence, memory hierarchy, occupancy), how to decide with Amdahl's law and transfer costs whether a GPU will help, and why it suits Monte Carlo and large linear algebra but not a microsecond trading decision.

17 min

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 if and others take the else, 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

LevelScopeTypical size (order of magnitude)Notes
Registersone threadup to 255 32-bit registers per threadfastest; the per-SM register file is shared by all resident threads
Shared memory / L1one block, on one SMtens to a couple of hundred KB per SMprogrammer-managed scratchpad for data reused within a block
L2 cachewhole GPUtens of MB
Global memory (HBM or GDDR)whole GPUtens of GB1–3+ TB/s on data-centre GPUs, but hundreds of cycles of latency
Host memory over PCIeCPU sideabout 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 O(n3)O(n^3) work on O(n2)O(n^2) data, so arithmetic per byte grows with nn 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 ff of the run time can be accelerated by a factor ss, and the rest 1−f1 - f cannot, the overall speed-up is

S=1(1−f)+f/s.S = \frac{1}{(1 - f) + f / s}.

As s→∞s \to \infty, S→1/(1−f)S \to 1 / (1 - f). With f=0.95f = 0.95 the ceiling is 20× however fast the GPU is; with f=0.9f = 0.9 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 solution

(a) Ignoring transfers. The parallel fraction is f=1.9/2.0=0.95f = 1.9 / 2.0 = 0.95 and s=40s = 40:

S=10.05+0.95/40=10.05+0.02375=10.07375≈13.6.S = \frac{1}{0.05 + 0.95 / 40} = \frac{1}{0.05 + 0.02375} = \frac{1}{0.07375} \approx 13.6.

Equivalently, the new time is 0.1+1.9/40=0.1+0.0475=0.14750.1 + 1.9 / 40 = 0.1 + 0.0475 = 0.1475 s, and 2.0/0.1475≈13.62.0 / 0.1475 \approx 13.6.

(b) Including transfers. Copy in: 4/25=0.164 / 25 = 0.16 s. Copy out: 0.04/25=0.00160.04 / 25 = 0.0016 s. New time:

0.1+0.0475+0.16+0.0016=0.3091 s,S=2.0/0.3091≈6.5.0.1 + 0.0475 + 0.16 + 0.0016 = 0.3091 \text{ s}, \qquad S = 2.0 / 0.3091 \approx 6.5.

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 k+1k+1 moves while block kk 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 solution

(a) Per element on a CPU: 0.95×100+0.05×300=1100.95 \times 100 + 0.05 \times 300 = 110 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 1−0.9532≈0.8061 - 0.95^{32} \approx 0.806; the cheap path by some lane with probability 1−0.0532≈11 - 0.05^{32} \approx 1. Expected warp time:

100×1+300×0.806≈342 cycles,100 \times 1 + 300 \times 0.806 \approx 342 \text{ cycles},

about 342/110≈3.1342 / 110 \approx 3.1 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: 65,536/64=1,02465{,}536 / 64 = 1{,}024, which is 1,024/32=321{,}024 / 32 = 32 warps, so occupancy is 32/64=50%32 / 64 = 50\%. With 40 registers: 65,536/40=1,63865{,}536 / 40 = 1{,}638 threads, so 51 whole warps, about 51/64≈80%51 / 64 \approx 80\% (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 10810^8 floats that start in host memory:

StepBytesRate (typical)Time
Copy x,yx, y to GPU and yy back over PCIe1.2 GB25 GB/sabout 48 ms
Kernel on the GPU1.2 GB2 TB/sabout 0.6 ms
Same loop on the CPU, memory-bound1.2 GB50 GB/sabout 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).