GPU Programming

Why does the CUDA kernel above include "if (i < n)"?

__global__ void add(float *c, const float *a,
                    const float *b, int n) {
    int i = blockIdx.x * blockDim.x + threadIdx.x;
    if (i < n)
        c[i] = a[i] + b[i];
}
  1. ASo that surplus threads do not read or write beyond the array when there are more threads than elements
  2. BTo keep the sums from exceeding n
  3. CTo double the GPU's speed
Show answer and explanation

Answer: A. So that surplus threads do not read or write beyond the array when there are more threads than elements

Threads are launched in whole blocks, so unless the element count is a multiple of the block size, there are more threads than elements. Writing without checking the index would corrupt memory beyond the allocated arrays or fault on an illegal access. The "i < n" check is an almost universal pattern in GPU kernels.

  • A. Correct. It handles the surplus from rounding up the block count.
  • B. It checks the index range, not the size of the values.
  • C. It is for correctness, not speed.
Practice this topic Back to topic

More GPU Programming questions

  1. On NVIDIA GPUs, what is the unit of 32 threads bundled together to execute the same instruction in lockstep called?
  2. How does the GPU SIMT (single instruction, multiple threads) model differ from CPU SIMD instructions?
  3. Within one warp, a conditional branch sends half the threads to the if path and half to the else path (warp divergence). How is this generally executed?
  4. Both (1) and (2) above make half the threads run A and the other half run B. Which is correct about warp efficiency (32 threads per warp)?
  5. Which describes memory coalescing on a GPU?