For the complete documentation index, see llms.txt. Markdown versions of all pages are available by appending .md to any URL (e.g. /get-started.md).
Mojo function
lane_group_reduce
def lane_group_reduce[val_type: DType, simd_width: SIMDLength, FuncType: def[dtype: DType, width: SIMDLength](SIMD[dtype, width], SIMD[dtype, width]) -> SIMD[dtype, width] & RegisterPassable & ImplicitlyCopyable, //, shuffle: def[dtype: DType, simd_width: SIMDLength](val: SIMD[dtype, simd_width], offset: UInt32) thin -> SIMD[dtype, simd_width], num_lanes: Int, *, stride: Int = Int(1)](val: SIMD[val_type, simd_width], func: FuncType) -> SIMD[val_type, simd_width]
Performs a generic warp-level reduction operation using shuffle operations.
This function implements a parallel reduction across threads in a warp using a butterfly pattern. It allows customizing both the shuffle operation and reduction function.
Example:
from max.gpu.primitives.warp import lane_group_reduce, shuffle_down
# Compute sum across 16 threads using shuffle down
def add[dtype: DType, width: SIMDLength](x: SIMD[dtype, width], y: SIMD[dtype, width]) -> SIMD[dtype, width]:
return x + y
var val = SIMD[.float32, 16](42.0)
var result = lane_group_reduce[shuffle_down, num_lanes=16](val, add)Parameters:
- val_type (
DType): The data type of the SIMD elements (e.g. float32, int32). - simd_width (
SIMDLength): The number of elements in the SIMD vector. - FuncType (
def[dtype: DType, width: SIMDLength](SIMD[dtype, width], SIMD[dtype, width]) -> SIMD[dtype, width]&RegisterPassable&ImplicitlyCopyable): The type of the reduction closure. - shuffle (
def[dtype: DType, simd_width: SIMDLength](val: SIMD[dtype, simd_width], offset: UInt32) thin -> SIMD[dtype, simd_width]): A function that performs the warp shuffle operation. Takes a SIMD value and offset and returns the shuffled result. - num_lanes (
Int): The number of lanes in a group. The reduction is done within each group. Must be a power of 2. - stride (
Int): The stride between lanes participating in the reduction.
Args:
- val (
SIMD[val_type, simd_width]): The SIMD value to reduce. Each lane contributes its value. - func (
FuncType): A binary function that combines two SIMD values during reduction. This defines the reduction operation (e.g. add, max, min).
Returns:
SIMD[val_type, simd_width]: A SIMD value containing the reduction result.