Guarding Against Out-of-Range
The if (i < n) bounds check.
You Often Launch Too Many
Thread counts come in fixed block sizes, so you almost always launch a few extra threads beyond your array length. Those spares need handling.
What Goes Wrong
An extra thread computes an index past the end of the array. If it writes there, it touches memory it does not own, causing a silent out-of-bounds bug.
All lessons in this course
- The Classic Index Formula
- Guarding Against Out-of-Range
- Rounding Up the Block Count
- Grid-Stride Loops