CUDA from scratch

Five classic GPU kernels — vector addition, matrix multiplication, reduction, convolution and histogram — each written naively first, then optimised step by step.

A GPU is not a faster CPU. It is a machine that does the same small operation on thousands of pieces of data at once, and most of the skill is in feeding it: getting data into the right kind of memory, and making neighbouring threads touch neighbouring addresses.

This course works through five problems. Each one starts with the obvious implementation, then improves it — and the interesting part is always why the faster version is faster, which is almost never about arithmetic.

Prerequisites

  • Comfortable C or C++: pointers, arrays, manual allocation.
  • An NVIDIA GPU with the CUDA toolkit installed. Check with nvcc --version.
  • No prior GPU experience assumed.

The environment these were written on

Component Version
OS Ubuntu 20.04.6 LTS
GPU NVIDIA GTX 2080 Ti
CUDA 11.8.0
cuDNN 8.8.0.121-11.8

Nothing here depends on that exact setup; any reasonably recent toolkit works.

Building and running

Every lesson is a single self-contained .cu file. Compile and run it directly:

nvcc -o program 01vec_add_baseline.cu
./program

Add -O3 for optimised builds, and -lcublas when a lesson uses cuBLAS. Most programs verify their own output and print Completed Successfully, so a silent assertion failure means the kernel is wrong.

The five ideas

The CUDA memory hierarchy as a pyramid: registers private to one thread at the top, then shared memory and L1 for one block, the L2 cache shared by every block, global GPU memory for every thread, and host CPU memory across the PCIe bus at the bottom. Each tier is tagged with the lessons that use it. The CUDA memory hierarchy as a pyramid: registers private to one thread at the top, then shared memory and L1 for one block, the L2 cache shared by every block, global GPU memory for every thread, and host CPU memory across the PCIe bus at the bottom. Each tier is tagged with the lessons that use it.
Each tier is faster, smaller and closer to the threads than the one below it. Almost every optimisation in this course is about moving work up this pyramid.

The lessons are ordered so each one introduces exactly one new part of the memory hierarchy — global memory, then pinned and unified memory, then shared memory, then constant memory, then atomics.

Lessons

  1. 1 Vector additionThe "hello world" of CUDA — and a tour of four different ways to get data onto the GPU.
  2. 2 Matrix multiplicationGoing 2D, then using shared memory as a hand-managed cache to cut global memory traffic.
  3. 3 Sum reductionTurning a whole array into one number — and four rounds of fixing the ways that goes wrong.
  4. 4 ConvolutionSliding a mask over an array, and meeting constant memory — the read-only cache built for exactly this.
  5. 5 HistogramWhat to do when thousands of threads want to increment the same counter — atomics, and the privatisation trick that rescues them.

Browse this course's code on GitHub →