Unit 1 of 10 · about 4 hours · explain, implement
Host, device, kernels, index mapping and explicit device memory
Every kernel starts with a mapping and an ownership boundary. Getting those right lets later experiments change performance without changing the operation.
You'll be able to
- Map one- and two-dimensional launches to distinct output elements, including edge threads.
- Implement explicit device allocation, input copies, a kernel launch, result validation and cleanup.
- Explain which work runs on the host and why a successful launch is not evidence of completion.
Covers
Read
In this order, only the parts listed
0 of 2 done
- PMPP, 4th edition: kernels, copies and multidimensional mappingProgramming Massively Parallel Processors, 4th edition - Chapters 2 to 6
Sections 2.2 to 2.6 and 3.1 to 3.2
- CUDA Programming Guide 13.4.2: explicit memory and error handlingCUDA Programming Guide
Sections 2.1.3.2, 2.1.4 and 2.1.7.1 to 2.1.7.2
Focus on
- A grid contains blocks; each block contains threads. Compute row and column separately, then use the storage layout to form the element offset.
- Allocation and copy sizes are bytes, not element counts. Keep ownership of each allocation on the host and release it on every error path.
- Kernel launch is asynchronous. Check
cudaGetLastError()after launching and check the completion operation before consuming results.
Common mistakes · 3
- Dereferencing an ordinary
cudaMallocdevice pointer on the CPU. Passing the pointer to a runtime API is not the same as accessing its pointee. - Using the grid width instead of the tensor row stride to flatten a two-dimensional address.
- Treating a successful launch check as proof that the kernel has finished without an execution error.
Try it yourself
Closed-book, before checking anything
a[i] = float(i) and b[i] = 1.0f. Use four blocks of 256 threads, explicit allocation and copies, and a bounds check. All 1003 results must equal float(i + 1) exactly; 21 launched threads must perform no memory access. Check every CUDA return value and free all allocations.Quiz yourself
Answer first, then mark it. Missed ones go to Review.
0 got · 0 missed
1.Why does the 1003-element launch need a guard even though the grid covers the input?Answer
Four blocks contain 1024 threads. The last 21 have indices outside the allocation; covering the input does not make those addresses valid.
2.Does using a two-dimensional grid make the input tensor two-dimensional in memory?Answer
No. Launch coordinates and storage layout are separate. The kernel must compute an address from the tensor's actual layout.
3.What must happen before the host checks the output?Answer
Kernel writes must complete and the results must be accessible to the host. In this explicit-memory program, check synchronization and the device-to-host copy before reading the host buffer.
You're done when
Your vector-add baseline passes lengths 0, 1, 31, 32, 33 and 1003, with zero length handled on the host, and you can draw a rectangular launch's address map without running it.
Only when you can do this without notes.
Leads to
Notes
Resources · 2 for this unit, 9 more in the section
- Programming Massively Parallel Processors, 4th edition - Chapters 2 to 6Wen-mei Hwu, David Kirk and Izzat El Hajj · primary
Read Chapters 2 to 6: data-parallel kernels, multidimensional grids, compute architecture and scheduling, memory architecture and data locality, and performance considerations. Work the chapter exercises on warps, occupancy, memory and coalescing, and predict three altered launch or address patterns before running them.
- CUDA Programming GuideNVIDIA · required
Read the Basics and Programming Model chapters plus the sections on memory, synchronization, hardware implementation and compute capabilities used by your kernels. Record the toolkit documentation version alongside every claim.
Also in this section
- MIT 6.S894 Accelerated Computing (Fall 2025) - Lectures 2 to 5 and Labs 2 and 3MIT · required
Use Lecture 2 (Throughput Processors), Lectures 3 and 4 (Memory, Parts 1 and 2) and Lecture 5 with its continuation (scratchpads, bank conflicts, register shuffles, overlap) for why the hardware looks as it does. Do Lab 2 (Massively-Parallel Mandelbrot, with its FMA-latency and warp-scheduler probes) and Lab 3 (Wave Simulation, with its coalesced-load and memory-latency probes) from the public lab repositories, built with nvcc for your own GPU instead of the course service. The labs were written for an RTX 4000 Ada, so recompute any SM count or bandwidth the questions assume.
- CUDA C++ Best Practices GuideNVIDIA · required
Study Assess, Parallelize, Optimize, Deploy; Getting the Right Answer; memory optimization; execution configuration; instruction optimization; and deployment compatibility. Convert recommendations into hypotheses, not universal rules.
- Parallel Thread Execution ISANVIDIA · reference
Read the PTX purpose, machine model, state spaces, memory consistency and target directives. Compile two tiny kernels, map source to PTX and SASS, and explain what PTX does and does not guarantee across devices.
- GPU MODE Foundations LecturesGPU MODE · support
Use Lectures 2, 3, 4, 5, 8, 37 and 41 for PMPP review, CUDA setup, architecture, optimization checklists, SASS and documentation reading. Reproduce only the examples that test a prediction from this stage.
- TensorTonic Language Modeling from Scratch - GPU analysis problemsTensorTonic · support
Solve GPU Occupancy Calculator, Shared-Memory Bank Conflict Analyzer and Global-Memory Coalescing Counter after the matching units, and check each answer against your own derivation.
- AI Performance Engineering - GPU FundamentalsWafer · reference
A curated index. Use Section 1 only to find a CUDA, PTX or architecture source the other entries lack.
- NVIDIA CUDA Pro Tips: grid-stride loops and vectorized memory accessNVIDIA · required
Read the grid-stride SAXPY loop and its scalability and debugging discussion (April 22, 2013), plus the scalar-copy SASS and vector-type alignment discussion in Increase Performance with Vectorized Memory Access (August 4, 2025). Skip the latter article's full vector-copy kernels: their tail-thread condition can omit remainders. These fill the pool's grid-stride and vector-width coverage gaps.
- CUDA Binary Utilities 13.4NVIDIA · reference
Sections 1.1 to 1.2, 2.1.1 to 2.1.3, 3.1.1 and 3.1.4 for cubin extraction, resource reports and line-correlated SASS. The existing compiler and PTX references lack a worked binary-tool workflow.
- PyTorch custom C++/CUDA operators and CUDA semanticsPyTorch · required
Custom C++ and CUDA Operators: build system, backend registration, Testing an operator and mutable schema; CUDA semantics 2.14: CUDA streams. These fill the native tensor-boundary gap in the CUDA-only pool. Stop before fake-kernel and autograd implementation; harden the tutorial's CUDA example for empty inputs, same-device checks, device guards, indexing width and launch errors.