Lesson 8: Grid-Stride Loops & Arbitrary N
So far we assumed one thread handles one element, with if (i < n) guarding the bounds. But what happens when n is larger than the number of threads we launched? With a million elements and only 262,144 threads, half the array is never computed. The fix is a grid-stride loop: each thread starts at it
Imagine 100 chairs to arrange but only 10 helpers. Instead of each one arranging a single chair and going home, every helper starts at their chair and jumps 10 chairs ahead each time: 1, 11, 21... so 10 people cover all 100 chairs.
- grid-stride loop
- A pattern where each thread handles several elements, jumping between them by the size of the whole grid until the index passes n.
- stride
- The distance the index jumps forward: blockDim.x * gridDim.x, the total number of threads in the grid.
- gridDim
- The number of blocks in the grid. gridDim.x * blockDim.x gives the total threads along one dimension.
- arbitrary N
- An input of any size, even larger than the thread count. A grid-stride loop covers it without changing the launch configuration.