← Back to Home

Session 12 — CUDA & the Modern GPU Stack

Real CUDA C programs plus one browser demo. The .cu files need an actual NVIDIA GPU and the CUDA toolkit — they can't run in a browser or on a Mac. Open them to view the code, then run them on Google Colab with a free GPU (see "How to run" below). This is the last session of Unit IV.

▶  Open the Interactive Slides

4. CUDA Thread Indexing, Interactive browser demo

Click a thread to watch i = blockIdx.x*blockDim.x + threadIdx.x map onto the data array — and see the extra "off the end" threads that the if (i < n) bounds check skips. Runs in-browser — show it live.

1. Vector Add / SAXPY view CUDA code

The "Hello World" of CUDA: y = a*x + y, one thread per element. The full five-step pattern — cudaMalloc, cudaMemcpy H2D, kernel launch, cudaMemcpy D2H, cudaFree — with the launch config and bounds check.

2. Parallel Reduction — Lab 5 view CUDA code

Sum a million numbers on the GPU with a tree reduction in shared memory: each block halves its active threads every step with __syncthreads(), then partials are combined. The canonical cooperative GPU pattern.

3. Image Grayscale — Lab 3 view CUDA code

Image processing as pure data parallelism: one thread per pixel on a synthetic 512×512 image, RGB → grayscale, using a 2D grid of 2D blocks.

How to run these

The .cu files need an NVIDIA GPU + CUDA toolkit. Easiest for everyone: Google Colab — set Runtime → Change runtime type → GPU, then run !nvcc 01-vector-add.cu -o out && ./out in a cell.

Own an NVIDIA GPU? Locally: nvcc 01-vector-add.cu -o vadd && ./vadd. macOS has no CUDA at all — use Colab. Full walkthrough in README.md.

This closes Unit IV (Programming Models & Tools): OpenMP (S10) → MPI (S11) → CUDA (S12). Unit V turns to where all this power gets used.