Skip to main content
This example reduces a large array to a single sum using a two-level approach: SIMD group reductions within each threadgroup, followed by a second pass to combine threadgroup results.

Strategy

Level 1: threadgroup reduction kernel

Level 2: final reduction

Once all threadgroup sums are in PartialOut, run a second pass. For small PartialOut arrays (< 256 elements), a single threadgroup is sufficient:

Full dispatch

Notes

  • SIMD group size is hardware-fixed at 32. The threadgroup size should be a multiple of 32.
  • This two-pass approach works for any N that is a multiple of threads_per_tg.
  • For padded inputs (N not a power of 2), use load_if with a bounds mask in the first pass.

See also