Search

Search papers and lessons

GPU Architecture

Level 2 · 10 min read

A GPU is a chip with thousands of small cores that run the same instruction on many threads at once, which suits work made of many identical operations.

On this page

  • Colour = topic mix
  • Systems
  • ReadReady to readNot yet

TL;DR

A graphics processing unit, or GPU, is a chip built from many small arithmetic cores that work in groups. Each group runs one instruction at a time on many threads at once, so the chip does a very large number of identical operations per second. It solves the problem of doing huge batches of simple, similar arithmetic, such as shading pixels or multiplying matrices, far faster than a few large general-purpose cores could.

Builds on: Thread · Leads to: GPU Memory Hierarchy

Where it's used

Training neural networks. Training a large language model is mostly matrix multiplication. A single training run on thousands of GPUs can take weeks, and the speed of each GPU's arithmetic units decides how much a run costs.

Real-time graphics. A game frame at 1080p has about two million pixels, and each pixel needs a colour computed from lighting and textures. The same small program runs for every pixel, which is exactly the pattern a GPU is built for. The name comes from this use.

Scientific simulation. Weather models, molecular dynamics and fluid solvers update millions of grid points or particles with the same formula at each time step. Each point is an independent thread of work, so the whole grid maps well onto the chip.

How it works

A toy GPU will show the structure. Real chips are much bigger, but the rules are the same. Our toy has 2 streaming multiprocessors (SMs), each a block of cores that can run one group of threads at a time. The group is called a warp. In this toy a warp has 4 threads. Real NVIDIA warps have 32.

Job: add two arrays of 8 numbers, so c[i] = a[i] + b[i] for i from 0 to 7.

  1. Launch threads. The program starts 8 threads, one per element. Each thread knows its own index i. Check: thread 5 computes c[5] and no other element.
  2. Group into warps. The 8 threads are split into 2 warps of 4: warp 0 holds threads 0 to 3, warp 1 holds threads 4 to 7. Check: 8 threads divided by 4 per warp gives 2 warps.
  3. Assign warps to SMs. SM 0 takes warp 0 and SM 1 takes warp 1. Both SMs now work at the same time. Check: with 2 SMs and 2 warps, neither SM is idle.
  4. Issue one instruction to the whole warp. Each SM issues "load a[i]" once, and all 4 threads in its warp execute it on their own data. This is the key idea, called SIMT (single instruction, multiple threads). Check: one instruction issue does 4 loads, one per thread.
  5. Load, add and store. The warp then issues "load b[i]", "add", and "store c[i]", each once for all 4 threads. Check: 4 additions happen in the time the SM takes to issue one add.
  6. Hide the wait. A load from memory can take many cycles. While warp 0 waits for its data, the SM switches to another resident warp that is ready. With enough warps, the arithmetic units stay busy during the waits. Check: an SM with only one warp would sit idle during the wait, but one with several warps does not.

The result: all 8 sums are computed by 2 instruction streams running in parallel, with each instruction covering 4 threads. The chip spends its transistors on arithmetic rather than on the complex control logic a CPU uses to run one thread fast.

flowchart TB
  K[kernel launch: 8 threads] --> W0[warp 0: threads 0-3]
  K --> W1[warp 1: threads 4-7]
  W0 --> S0[SM 0]
  W1 --> S1[SM 1]
  S0 --> I0[one instruction issued to 4 threads]
  S1 --> I1[one instruction issued to 4 threads]

The math

Step 1 in symbols: threads and warps

Each thread has a global index

i=b⋅T+ti = b \cdot T + t

where b is the index of its block, T is the number of threads per block, and t is the thread's index inside its block. The number of warps needed for n threads, with warp size w, is

W=⌈nw⌉W = \left\lceil \frac{n}{w} \right\rceil

The ceiling symbol means round up. A partly filled last warp still takes a full warp slot.

Peak throughput: how many operations per second

A chip with S SMs, each with C arithmetic cores, running at clock rate f cycles per second, and doing k floating-point operations per core per cycle, has a peak throughput of

P=S⋅C⋅k⋅fP = S \cdot C \cdot k \cdot f

A fused multiply-add counts as k = 2 operations, one multiply and one add. The formula gives the best case, where every core is busy every cycle. Real programs fall short of it when warps wait for memory, which is the subject of GPU Memory Hierarchy.

Step 4 in symbols: the warp's instruction

If the instruction is an addition, the SM performs one add per thread of the warp, so a warp of w threads does w additions for each issue. Over all W warps of an array of n elements, the number of additions is n, and the number of issues is W, which is about n divided by w.

Worked examples

Example
Tiny: 8 threads with warps of 4

Take n = 8 threads and warp size w = 4.

  1. Warps needed: W = ceil(8 / 4) = 2.
  2. Warp 0 covers threads 0, 1, 2, 3 and warp 1 covers threads 4, 5, 6, 7.
  3. Each warp issues "add" once. Warp 0 does 4 additions in one issue, and warp 1 does 4 in one issue.
  4. Total additions: 4 plus 4 = 8, which equals n.
  5. Interpretation: 8 additions needed only 2 warp-level instruction issues per step, so the instruction stream was 4 times shorter than a one-thread-at-a-time loop.
Example
Standard: 1000 threads with real warps of 32

Take n = 1000 threads and warp size w = 32.

  1. Warps needed: W = ceil(1000 / 32) = ceil(31.25) = 32.
  2. The first 31 warps are full, each with 32 threads: 31 times 32 = 992 threads.
  3. The last warp holds the remaining 1000 minus 992 = 8 threads. It still occupies a full warp slot, so 24 of its 32 lanes are idle.
  4. Efficiency of this launch: 1000 threads out of 32 times 32 = 1024 lanes, or about 97.7 percent of the lanes do work.
  5. Interpretation: rounding the thread count up to a multiple of 32 costs little here. A launch of 33 threads would be much worse, with 2 warps and only 33 of 64 lanes working.
Example
Realistic: peak FP32 throughput of an NVIDIA A100

The NVIDIA A100 has S = 108 SMs, each with C = 64 FP32 cores, and a boost clock of f = 1.41 GHz. Take k = 2 for a fused multiply-add.

  1. Total cores: 108 times 64 = 6,912.
  2. Operations per cycle across the chip: 6,912 times 2 = 13,824.
  3. Operations per second: 13,824 times 1.41 times 10 to the power 9 = about 1.95 times 10 to the power 13.
  4. Convert: 1.95 times 10 to the power 13 operations per second is about 19.5 teraFLOPS.
  5. Interpretation: 19.5 teraFLOPS is the ceiling for FP32 math. Any kernel that delivers less is either waiting on memory or not keeping all its warps busy. The number matches NVIDIA's published FP32 figure for this chip.

Try it: A made-up chip has 10 SMs, each with 64 cores, 2 operations per core per cycle, and a clock of 1 GHz. What is its peak throughput in FLOPS? Answer: P = 10 times 64 times 2 times 1 times 10 to the power 9 = 1.28 times 10 to the power 12, or 1.28 teraFLOPS.

Common mistakes and tradeoffs

It is tempting to think a GPU is just a CPU with more cores. In fact each GPU core is simple and slow on its own, and the chip is fast only because thousands of threads run the same instruction together. A CPU core runs one thread fast with large caches and branch prediction, while a GPU trades that per-thread speed for total throughput.

It is tempting to think that every thread in a warp can take its own path freely. In fact a warp issues one instruction at a time. When threads in a warp take different branches of an if, the hardware runs each branch in turn with the other threads masked off, an effect called branch divergence. The cost is roughly the sum of the branches, not the longest one alone.

It is tempting to think a GPU is always faster. In fact it wins only when there are many independent operations and the data stays on the chip long enough to be reused. A small job, or one that copies data to and from the host for every step, can run slower on the GPU than on the CPU because of transfer and launch overhead.

When to use it: large, regular, data-parallel work such as matrix multiplication, image processing and simulation on a grid.

When not to use it: tasks with little parallelism, many unpredictable branches, or frequent small transfers between CPU and GPU.

Alternatives: multi-core CPUs with vector instructions (SIMD) for medium-sized parallel work, and specialised accelerators such as TPUs for matrix-heavy workloads.

History

Early graphics hardware in the 1980s and 1990s drew pixels with fixed-function circuits, with no general programmability. In the late 1990s and early 2000s, programmable shaders let developers run small programs per vertex and per pixel, and researchers began to use the same chips for general arithmetic, a practice called GPGPU. In 2006 NVIDIA released CUDA, a programming model that let ordinary C-like code run on the chip's many threads without disguising it as graphics. The SIMT design with warps became the basis for the chips that later trained deep neural networks at scale, and the tensor cores of NVIDIA's 2017 Volta generation added dedicated matrix units on top.

Recap

  • A GPU is built from streaming multiprocessors, each running groups of threads called warps.
  • A warp of 32 threads issues one instruction at a time, so all threads in it do the same operation on their own data (SIMT).
  • When a warp waits for memory, the SM switches to another ready warp, which keeps the arithmetic units busy.
  • Peak throughput is the number of cores times operations per cycle times clock rate; the realistic speed depends on memory traffic.
  • Read next: GPU Memory Hierarchy for why memory decides the real speed. Recent chips also add dedicated matrix units, called tensor cores, which run small matrix products in one step.

Check yourself

QuestionWhy does a GPU issue one instruction to a whole warp instead of one instruction per thread?Show answer

A warp is a group of threads that share one instruction stream. Issuing once for many threads means the control logic is shared, so the chip can afford many arithmetic cores. Each thread still works on its own data, which is what makes the design SIMT.

QuestionA launch has 500 threads and the warp size is 32. How many warps are needed, and how many lanes are idle in the last warp?Show answer

ceil(500 / 32) = ceil(15.625) = 16 warps. The first 15 warps hold 15 times 32 = 480 threads, so the last warp holds 20 threads and has 32 minus 20 = 12 idle lanes.

QuestionWhy can a branch in a warp make the code slower than the same work without the branch?Show answer

The warp runs one instruction at a time. If some threads take the if-branch and others take the else-branch, the hardware runs both paths in sequence, masking off the threads that are not on each path. The warp therefore pays for both branches.

QuestionA chip has 20 SMs, each with 64 cores, 2 operations per core per cycle, and a 1.5 GHz clock. What is its peak FP32 throughput, and why is a real kernel usually below it?Show answer

P = 20 times 64 times 2 times 1.5 times 10 to the power 9 = 3.84 times 10 to the power 12, or 3.84 teraFLOPS. A real kernel is usually below this because warps wait for memory, and the arithmetic units sit idle during those waits.

Sources