GPU Programming

What is the advantage of writing a kernel with a loop like the one above (a grid-stride loop)?

for (int i = blockIdx.x * blockDim.x + threadIdx.x;
     i < n;
     i += blockDim.x * gridDim.x)
    y[i] = a * x[i] + y[i];
  1. AUsing the loop prevents memory coalescing
  2. BEven with fewer threads than elements, each thread handles several elements spaced by the total thread count, covering every element
  3. COnly one thread runs
Show answer and explanation

Answer: B. Even with fewer threads than elements, each thread handles several elements spaced by the total thread count, covering every element

Because each thread advances its index by the total thread count (blockDim.x × gridDim.x), every element is processed no matter how many threads are launched. The kernel can be launched with a block count suited to the GPU's number of SMs, reducing thread-creation overhead. In each iteration adjacent threads handle adjacent elements, so coalescing is preserved.

  • A. In each iteration adjacent threads read adjacent elements, so accesses coalesce.
  • B. Correct. Grid size is decoupled from element count.
  • C. All threads run.
Practice this topic Back to topic

More GPU Programming questions

  1. For what kind of access does CUDA constant memory perform especially well?
  2. In general, how do GPU caches compare with CPU caches?
  3. Which is an example of a parallel-programming framework for GPUs that also works on GPUs other than NVIDIA's?
  4. On a GPU you want to compact only the elements meeting a condition into another array (each thread outputs 0 or 1 items). Which parallel computation is commonly used to determine each thread's write position?
  5. Which assumption must be respected when writing GPU kernels?