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];
- AUsing the loop prevents memory coalescing
- BEven with fewer threads than elements, each thread handles several elements spaced by the total thread count, covering every element
- 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.