liveBook · Manning

Out-of-bounds array access

As explained in Grokking GPU Programming

An out-of-bounds access is an attempt to read or write outside the valid range of a data structure or memory allocation.

An out-of-bounds access happens when a thread reads or writes beyond an array’s valid indices. If an array has five elements but eight threads try to access one element each, threads with indices 5, 6, and 7 access data that does not exist. section 2.12

Before doing the work, check that each thread’s index is below the element count: if (idx < n). Without this guard, a GPU program may fail, silently corrupt memory, or return incorrect results. NVIDIA’s compute-sanitizer can detect memory-access errors at runtime. section 2.12 section 3.16

How Grokking GPU Programming covers it

How deep it goes
Demonstrates the index guard and describes runtime failures, silent corruption, and incorrect results.
Examples
Uses five array elements and eight threads to show invalid indices 5, 6, and 7.
What you can do after
Diagnose/debug with it by checking idx < n before each thread performs its computation.
How it connects
Connects CUDA thread indexing with bounds checking and GPU memory corruption.

Also explained in

Compare how 2 books explain out-of-bounds array access →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Grokking GPU Programming. The section links point to the parts of the book it draws on.