When you launch a kernel with kernel<<<blocks, threads>>>(),
CUDA creates a grid of blocks, and each block holds a team of threads.
Every thread computes one global index to find its own piece of data:
i = blockIdx.x * blockDim.x + threadIdx.x. Click any thread below to see exactly how
it maps onto the data array — and what the bounds check if (i < n) is for.
float d_x[n])The takeaway: you launch more threads than data elements
whenever n isn't an exact multiple of the block size — here the last block has
threads that run off the end of the array. That is why every CUDA kernel starts with
if (i < n) return; (or wraps its work in if (i < n)): the extra
threads must do nothing, or they corrupt memory past the array.