What is Grid-stride loop?
Writing a kernel as a loop in which each thread processes elements spaced by the total thread count, decoupling launch size from element count.
Japanese name: グリッドストライドループ
Writing a kernel as a loop in which each thread processes elements spaced by the total thread count, decoupling launch size from element count.
Japanese name: グリッドストライドループ
The typical form is for (int i = blockIdx.x * blockDim.x + threadIdx.x; i < n; i += blockDim.x * gridDim.x). Each thread starts from its own global index and processes elements while advancing by the total number of threads in the grid. If there are more threads than elements, the loop runs zero or one times; if there are fewer, it runs many times. In every iteration adjacent threads handle adjacent elements, so global memory accesses remain coalesced.
Because the launch size is decoupled from the element count, the kernel can be launched with, for example, a few times as many blocks as there are SMs, filling the GPU appropriately. Per-thread setup work, such as initializing shared memory or loading constants, is reused across several elements. In reductions such as sums, a common pattern is for each thread to accumulate a partial sum in the loop before the results are combined within the block. Launching with a single block of a single thread turns the same kernel into a serial loop, which is handy for checking results.
In the one-thread-per-element style, the grid is sized to the number of elements and if (i < n) excludes the out-of-range threads. By contrast, if each thread is given a contiguous range, with thread 0 processing elements 0–99 and thread 1 elements 100–199, adjacent threads read distant addresses at the same moment, so the accesses are not coalesced and performance suffers. In a grid-stride loop, threads take elements at intervals of the total thread count, so this problem does not arise.
Grid-stride loop appears in 1 practice question across 1 topic, including GPU Programming.
As an Amazon Associate I earn from qualifying purchases.
This page was drafted with AI and fact-checked by a separate AI review before publication. Our Editorial policy explains how we write and check content. If you spot an error, please tell us on our Contact page.