Parallelism on the GPU
How GPUs expose massive parallelism (SIMT) and how to reason about, measure and map parallel work to threads, warps and SMs for good performance.
Drafted with Aria, reviewed by the AiCanCode.org team · Updated . How we write and check our notes. Spotted an error? Use Give Feedback at the bottom of the page.
Why it matters
GPUs give very high computational throughput by running many operations in parallel. An engineer uses GPU parallelism to map heavy data-parallel or regular-task-parallel problems (graphics, dense linear algebra, image processing, neural nets) to thousands of lightweight threads so the device achieves high throughput and hides memory latency.
Key ideas
Start from first principles: a processor gives results by executing instructions. A GPU increases throughput by executing many instructions in parallel across many simple cores.
Parallelism: executing multiple operations or tasks at the same time. Contrast with serial execution where operations run one after another.
GPU (graphics processing unit): a many-core device designed for high throughput via parallel execution units (SMs / compute units). I will use the NVIDIA terminology SM (streaming multiprocessor) for examples; refer to "GPU architecture and its evolution" for more.
Thread: the smallest unit of execution that runs a sequence of instructions. In CUDA, a thread is lightweight and executes as part of a warp (the warp is the scheduling/issue unit).
Warp (definition): a fixed-size group of threads that execute the same instruction stream under the Single-Instruction, Multiple-Thread model. On current NVIDIA devices the warp size is 32 threads (verify for new architectures). Draw: a block of 32 small boxes labelled thread 0..31 inside a larger box labelled Warp; draw an arrow from Warp to an ALU labelled "single instruction".
SIMT (Single-Instruction, Multiple-Thread): a programming model where threads in a warp execute the same instruction, but each thread has its own registers and logical control flow; hardware issues instructions per warp. On modern NVIDIA GPUs (Volta and newer), each thread also has its own program counter (independent thread scheduling). SIMT is like SIMD but with per-thread control flow and memory.
Data parallelism: running the same operation on different data elements simultaneously. Example: add two arrays elementwise.
Task parallelism: running different tasks (different code) in parallel when they are independent.
Divergence: when threads in the same warp follow different control-flow paths (if/else). Divergence forces serialized execution of different paths and reduces throughput. The penalty is confined within a warp; different warps can take different branches without affecting each other.
Latency hiding: GPUs hide memory or instruction latency by switching from a stalled warp to another ready warp. To hide latency you need many active warps per SM.
Occupancy (definition): the ratio of active warps (or threads) on an SM to the hardware maximum; higher occupancy improves the ability to hide latency but does not guarantee best performance.
Coalesced memory access: arranging threads' memory requests so they form few aligned, wide transactions to global memory at warp granularity. Coalescing reduces effective memory traffic and increases bandwidth.
Speedup (definition): ratio of serial execution time to parallel execution time for the same problem.
Exact laws commonly used in exam answers:
Amdahl's law (exact statement and derivation): Assume a program has a fraction p of execution time that is perfectly parallelisable and a fraction (1-p) that is strictly serial. If we run the parallel portion on N identical processors and the serial portion on one processor, then the overall speedup S(N) is: Step 1: Serial time = T = T_serial + T_parallel. Step 2: Let T_serial = (1-p)T and T_parallel = pT. Step 3: Parallel execution time = (1-p)T + (pT)/N. Step 4: Speedup S(N) = T / [ (1-p)T + (pT)/N ] = 1 / [ (1-p) + p/N ]. Assumptions: p measured on the original serial execution; parallel portion scales perfectly with N; communication/synchronisation overhead ignored.
Gustafson's law (exact statement and derivation): Assume the parallel portion scales with the number of processors so we fix execution time and grow problem size. Let p be the fraction of runtime that would be parallel on a single processor. For N processors, scaled speedup S(N) = (1-p) + p·N. Derivation: total scaled work = T_serial + N·T_parallel_single = (1-p)T + N·pT, normalized by original T gives S. Assumptions: scaled problem, parallel portion grows with N, serial cost constant. Also known as weak scaling; by contrast, Amdahl's law describes strong scaling.
Thread mapping and hierarchy (what to draw):
- Draw a rectangle labelled GPU. Inside draw 4 boxes labelled SM0..SM3. Inside one SM draw several smaller rectangles labelled Warp arrays of 32 threads. Draw a grid of thread blocks above the SMs with arrows showing blocks are scheduled onto SMs.
- Label: Grid -> Blocks -> Threads. This illustrates the CUDA execution hierarchy (refer to "CUDA threads, blocks, grids and warps" for full details).
Derive why divergence hurts: if k threads in a warp take path A and the rest take path B, the warp executes path A for the threads taking A while masking others, then executes path B, serialising the two paths. If times are tA and tB then effective warp time is tA+tB, larger than max(tA,tB) needed for fully lock-step execution.
Formulas
S(N) = 1 / ((1 - p) + p / N) — Amdahl's law. p: parallel fraction (dimensionless), N: number of processors/threads/SMs.
S_scaled(N) = (1 - p) + p * N — Gustafson's law. p: parallel fraction on single processor, N: processors.
occupancy = (active warps per SM) / (max warps per SM) — occupancy ratio (dimensionless). active warps per SM: integer; max warps per SM: hardware constant (e.g., 64 on many NVIDIA GPUs when max resident threads/SM = 2048 and warp size = 32; always check your GPU's compute capability).
warp_size = 32 (typical on NVIDIA) — number of threads in a warp (threads). Verify for new GPUs.
Remember this CUDA syntax:
__global__ void kernelName(args) — kernel declaration (runs on device, callable from host).
kernelName<<<gridDim, blockDim>>>(args) — kernel launch syntax where gridDim and blockDim are integers or dim3.
threadIdx.x, blockIdx.x, blockDim.x, gridDim.x — thread and block indices and dimensions used to map work to threads.
Worked examples
Numerical using Amdahl's law Problem: A program has 90% of its work parallelisable (p = 0.9). We run on N = 10 processors. Compute speedup using Amdahl's law. Step 1: Write formula: S(N) = 1 / ( (1-p) + p/N ). Step 2: Substitute numbers: S(10) = 1 / ( (1 - 0.9) + 0.9 / 10 ). Step 3: Compute inside: (1 - 0.9) = 0.1; 0.9 / 10 = 0.09; sum = 0.19. Step 4: S(10) = 1 / 0.19 = 5.26315789. Final answer: Speedup ≈ 5.263 (dimensionless).
Simple CUDA program: vector addition (full program). This is an example of mapping data-parallel work to threads. Compile with nvcc.
#include <stdio.h>
#include <cuda_runtime.h>
__global__ void vecAdd(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];
}
int main() {
int N = 8;
size_t sz = N * sizeof(float);
float hA[8] = {0,1,2,3,4,5,6,7};
float hB[8] = {10,11,12,13,14,15,16,17};
float hC[8];
float *dA, *dB, *dC;
cudaMalloc((void**)&dA, sz); cudaMalloc((void**)&dB, sz); cudaMalloc((void**)&dC, sz);
cudaMemcpy(dA, hA, sz, cudaMemcpyHostToDevice);
cudaMemcpy(dB, hB, sz, cudaMemcpyHostToDevice);
int threadsPerBlock = 4;
int blocksPerGrid = (N + threadsPerBlock - 1) / threadsPerBlock;
vecAdd<<<blocksPerGrid, threadsPerBlock>>>(dA, dB, dC, N);
cudaMemcpy(hC, dC, sz, cudaMemcpyDeviceToHost);
for (int i = 0; i < N; ++i) printf("C[%d] = %.1f\n", i, hC[i]);
cudaFree(dA); cudaFree(dB); cudaFree(dC);
return 0;
}
Sample input: none (program uses built-in arrays). Exact output when run (lines in this order): C[0] = 10.0 C[1] = 12.0 C[2] = 14.0 C[3] = 16.0 C[4] = 18.0 C[5] = 20.0 C[6] = 22.0 C[7] = 24.0
Explain: Each CUDA thread computes one element. threadsPerBlock=4 and blocksPerGrid=2 gives total 8 threads; mapping i = blockIdx.x*blockDim.x + threadIdx.x covers indices 0..7.
Common mistakes
- Forgetting the bounds check if (i < N) in kernels; this causes out-of-bounds memory writes for non-divisible grid sizes.
- Ignoring divergence cost: writing code with heavy branch divergence inside warps (per-element conditionals) without restructuring.
- Not coalescing global memory accesses: scattered loads/stores cause extra memory transactions and slow down kernels. Also, choosing block sizes that are not multiples of the warp size can underutilize hardware.
- Applying Amdahl's law blindly without checking communication, synchronisation and memory contention overhead.
For your exam
Types of questions:
- Definitions and short notes (define SIMT, warp, occupancy, divergence).
- Derivations (derive Amdahl's law, Gustafson's law) — show assumptions clearly.
- Numerical problems (compute speedup with given p and N, occupancy calculation using given resources).
- Programs or pseudocode (write a CUDA kernel for vector add, map 2D data to threads).
- Diagrams (draw GPU–SM–warp–thread hierarchy and label indices).
What a full-marks answer includes:
- Precise definitions with bold term names and assumptions stated.
- Step-by-step derivations showing algebraic manipulation and the assumptions used (e.g., perfect parallel scaling).
- For programs: correct kernel declaration, mapping from thread/block indices to data indices, bounds checks and sample kernel launch parameters.
- For numericals: show formulas used, substitute numbers, compute intermediate steps and give final numeric answer with units or dimensionless label.
Quick check
- Define a warp.
- State Amdahl's law formula for speedup.
- Why does divergence reduce performance in SIMT? Answers: 1) A warp is a fixed-size group of threads that execute the same instruction stream (typical size 32 on NVIDIA). 2) S(N) = 1 / ((1 - p) + p / N). 3) Divergence causes threads in a warp to take different branches; the warp must serialise branch paths, increasing effective execution time.
Finished this topic? Mark it so your progress, study plan and readiness keep up.
Stuck on something here?