Suppose five array elements are assigned to eight GPU threads. The last three threads have no element to process, but they still run. A boundary check, or bounds check, tests an index before access so those threads can skip the work. section 2.12
For a nonnegative index idx and an array of n elements, the valid indices are 0 through n−1. Put the computation and its array accesses inside if (idx < n). Pass the actual n to the kernel: a fixed limit can silently leave elements unprocessed when the input grows. section 2.12 section 2.23
Rounding up a launch
With 1,000 elements and 256 threads per block, four blocks provide 1,024 threads. Compute each thread’s global index as blockIdx.x * blockDim.x + threadIdx.x. The check if (idx < n) lets indices 0–999 do the work and keeps indices 1,000–1,023 from accessing the array. section 3.7 section 3.16
__global__ void doubleKernel(int* data, int n) { #A
int idx = blockIdx.x * blockDim.x + threadIdx.x;
data[idx] = data[idx] * 2; #B
}
__global__ void doubleKernel(int* data, int n) { #C
int idx = blockIdx.x * blockDim.x + threadIdx.x;
if (idx < n) { #D
data[idx] = data[idx] * 2;
}
}Compare the unguarded multi-block kernel with the version that checks the global index before doubling an element. section 3.7
A missing check can cause a crash, garbage results, or silent memory corruption. NVIDIA’s compute-sanitizer can detect invalid memory accesses at runtime, but guarding the access prevents them. A kernel guard also cannot fix host arrays that were allocated too small; host and device allocations must both cover the input. section 2.12 section 2.23
Check each index that an operation actually uses:
- In a scan, tid − stride can be negative even when tid itself is valid. Guard the addition with if (tid >= stride); otherwise carry the current value forward. section 6.17
- In a stencil, a valid center element can still lack a neighbor. Check each neighbor against the relevant edge before reading it. One averaging approach uses only the neighbors that exist. section 9.13 section 9.2
- When loading a scan’s shared buffer, threads beyond the input can store zero instead of reading input. They must still reach the same __syncthreads() barrier as the other threads. section 6.10
Rounding up may leave some threads idle and cause a little branch divergence at the boundary. The check is still essential: correctness comes before that small inefficiency. section 3.7



