warp icon indicating copy to clipboard operation
warp copied to clipboard

Optimize tiled CUDA launches by reading blockIdx.x directly

Open adenzler-nvidia opened this issue 7 months ago • 3 comments

For tiled CUDA kernel launches, blockIdx.x already encodes the tile index and threadIdx.x the intra-tile position. The current codegen computes the linear thread index and then divides by block_dim() to recover the tile coordinate, which is redundant on the GPU side.

A dedicated codegen path for tiled CUDA kernels could read blockIdx.x directly instead of performing the linear /= block_dim() division and the multiply+add to reconstruct the linear index. The CPU path would continue using the current approach since it does not have an equivalent of blockIdx.

This came up during review of GH-1270 (templated launch_bounds_t), where the tiled launch coordinate computation was preserved as-is to limit scope.

adenzler-nvidia avatar Apr 13 '26 12:04 adenzler-nvidia

Hey @adenzler-nvidia, if this is an open issue and no one is actively working on it can I be assigned this issue?

aneangel avatar May 29 '26 17:05 aneangel

@aneangel @shi-eric I'd be happy to work on this if no one is already implementing it.

One detail I'd like to preserve is the logical tile-index semantics in both the normal and grid-stride launch paths. In particular, I don't think this should be a mechanical replacement with a fixed blockIdx.x, since a grid-stride block may process more than one logical tile, and the multidimensional launch path also needs to preserve the existing coordinate mapping.

0z5a avatar Sep 19 '26 08:09 0z5a

One thing I would preserve before simplifying this to blockIdx.x is the distinction between physical CUDA blocks and logical tile indices.

For a one-block-per-tile launch they coincide, but a grid-stride tiled launch can have one CUDA block process multiple logical tiles.

So the optimized path probably needs something closer to:

logical_tile = blockIdx.x + iteration * gridDim.x

rather than treating blockIdx.x itself as the final tile coordinate.

The multidimensional launch mapping also needs to consume that logical tile index before decomposing coordinates.

A useful regression matrix would cover:

  • ordinary 1-D tiled launch;
  • grid-stride launch with more logical tiles than blocks;
  • multidimensional tile dimensions;
  • CPU path unchanged.

I would benchmark only after those semantics are locked down, since saving the divide is not useful if it changes logical indexing.

kvnloo avatar Sep 27 '26 21:09 kvnloo