Warp (subgroup) operations
Threads execute in hardware groups — warps (NVIDIA), waves (AMD), SIMD-groups (Apple) — that can exchange
registers without touching shared memory. kernel.warp exposes the portable subset:
kernel.warp.size, kernel.warp.lane_id, and
kernel.warp.shuffle_down(v, n) (the shuffle offset is a compile-time literal). The canonical use
is a register-only tree reduction:
use system.gpu
use system.collections.array
gpu fn warp_reduce_sum(input Array<int, 32>, dst out Array<int, 1>)
let lane = kernel.warp.lane_id
var v = input[lane]
v = v + kernel.warp.shuffle_down(v, 16)
v = v + kernel.warp.shuffle_down(v, 8)
v = v + kernel.warp.shuffle_down(v, 4)
v = v + kernel.warp.shuffle_down(v, 2)
v = v + kernel.warp.shuffle_down(v, 1)
if lane == 0
dst[0] = v
fn main()
gpu let input = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32]
gpu var dst = Array<int,1>()
warp_reduce_sum(input, dst).launch(Dim3(1, 1, 1), Dim3(32, 1, 1))
let result = dst
println(f'{result[0]}')
Output: 528 — the sum of 1..32, reduced in five shuffle steps with no shared memory and no
barrier. Warp ops require adapter subgroup support. A device without it refuses the kernel before dispatch
rather than miscomputing.