IMPORTANT: To view this page as Markdown, append `.md` to the URL (e.g. /get-started.md). For the complete documentation index, see llms.txt.
Skip to main content
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

block_reduce

def block_reduce[ReduceFnType: def[dtype: DType, width: SIMDLength](SIMD[dtype, width], SIMD[dtype, width]) -> SIMD[dtype, width] & RegisterPassable & ImplicitlyCopyable, //, BLOCK_SIZE: Int, dtype: DType, simd_width: SIMDLength](val: SIMD[dtype, simd_width], init: Scalar[dtype], reduce_fn: ReduceFnType) -> Scalar[dtype]

Performs a block-level reduction of a single SIMD value across all threads in a GPU thread block using warp-level primitives and shared memory.

Parameters:

  • ​ReduceFnType (def[dtype: DType, width: SIMDLength](SIMD[dtype, width], SIMD[dtype, width]) -> SIMD[dtype, width] & RegisterPassable & ImplicitlyCopyable): The type of reduce_fn (inferred).
  • ​BLOCK_SIZE (Int): The number of threads per block.
  • ​dtype (DType): The data type of the elements.
  • ​simd_width (SIMDLength): The SIMD vector width.

Args:

  • ​val (SIMD[dtype, simd_width]): The per-thread SIMD value to reduce.
  • ​init (Scalar[dtype]): The identity value for the reduction.
  • ​reduce_fn (ReduceFnType): The binary reduction function.

Returns:

Scalar[dtype]: The reduced scalar result (valid on thread 0).

def block_reduce[ReduceFnType: def[dtype: DType, width: SIMDLength, reduction_idx: Int](SIMD[dtype, width], SIMD[dtype, width]) -> SIMD[dtype, width] & RegisterPassable & ImplicitlyCopyable, //, BLOCK_SIZE: Int, num_reductions: Int, dtype: DType, simd_width: SIMDLength](val: StaticTuple[SIMD[dtype, simd_width], num_reductions], init: StaticTuple[Scalar[dtype], num_reductions], reduce_fn: ReduceFnType) -> StaticTuple[Scalar[dtype], num_reductions]

Performs a block-level reduction of multiple fused SIMD values across all threads in a GPU thread block using warp shuffles and shared memory.

Parameters:

  • ​ReduceFnType (def[dtype: DType, width: SIMDLength, reduction_idx: Int](SIMD[dtype, width], SIMD[dtype, width]) -> SIMD[dtype, width] & RegisterPassable & ImplicitlyCopyable): The type of reduce_fn (inferred).
  • ​BLOCK_SIZE (Int): The number of threads per block.
  • ​num_reductions (Int): The number of fused reductions to perform.
  • ​dtype (DType): The data type of the elements.
  • ​simd_width (SIMDLength): The SIMD vector width.

Args:

Returns:

StaticTuple[Scalar[dtype], num_reductions]: The reduced scalar results (valid on thread 0).

Was this page helpful?