IMPORTANT: To view this page as Markdown, append `.md` to the URL (e.g. /max/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. /max/get-started.md).

Mojo struct

ReduceProduct

struct ReduceProduct[dtype: DType, W: Int = simd_width_of[dtype]()]

Product reduction monoid: (self, x) -> self * x.

Single-field state: acc: SIMD[dtype, W]. accumulate is one expression β€” acc *= Self.pad[...](val). The horizontal collapse to scalar happens once at reduce, in acc[0]. No hardware-fast Reducer.product, so join_parallel falls through to the generic combiner after the SIMD collapse.

Parameters​

  • ​dtype (DType): The accumulator dtype.
  • ​W (Int): SIMD width of the lane-wise accumulator. Defaults to the target's simd_width_of[dtype].

Fields​

  • ​acc (SIMD[dtype, W]): Lane-wise SIMD accumulator. reduce reduces to scalar (placed in acc[0]); bodies read acc[0].

Implemented traits​

AnyType, Copyable, ImplicitlyCopyable, ImplicitlyDeletable, Movable, ReduceOp, RegisterPassable, TrivialRegisterPassable

comptime members​

Single​

comptime Single = ReduceProduct[dtype, Int(1)]

width​

comptime width = W

Methods​

__init__​

def __init__() -> Self

Identity: acc = 1_W.

__getitem__​

def __getitem__(self, j: Int) -> Self.Single

Returns lane j as a width-1 monoid.

Returns:

Self.Single

__setitem__​

def __setitem__(mut self, j: Int, s: ReduceProduct[dtype, Int(1)])

Writes width-1 monoid s into lane j.

accumulate​

def accumulate[val_dtype: DType, w: Int](mut self, val: SIMD[val_dtype, w], idx: SIMD[DType.int64, w] = 0)

Folds a SIMD tile into acc lane-wise. Partial tiles get identity-padded (1) via Self.pad.

Parameters:

  • ​val_dtype (DType): The tile's source dtype.
  • ​w (Int): SIMD width of the tile.

Args:

join​

def join(mut self, other: Self)

Sequential combine: lane-wise product.

Args:

  • ​other (Self): The state to combine into self.

reduce​

def reduce(self) -> Self.Single

Multiplies the W lane partials via the reduce_mul intrinsic.

Overrides the default lane-fold so the vectorized horizontal product is emitted explicitly rather than reconstructed from a scalar chain by the optimizer (a measured CPU win; GPU-neutral).

Returns:

Self.Single: A width-1 ReduceProduct holding the product in acc[0].

join_parallel​

def join_parallel[R: Reducer](mut self, reducer: R)

Cross-thread combine. No hardware-fast scalar product, so this goes through reducer.generic. Runs after reduce, so acc[0] already holds the within-thread product; reduce a width-1 ReduceProduct (1 uint32 word) so the combine takes the register shuffle-butterfly (<= _WARP_SHUFFLE_MAX_WORDS) rather than the shmem tree a wider state falls into, then splat the result back.

Parameters:

  • ​R (Reducer): The parallel scalar reducer.

Args:

  • ​reducer (R): The reducer instance.