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

map_reduce

def map_reduce[simd_width: SIMDLength, dtype: DType, acc_type: DType, origins_gen: OriginSet, input_gen_fn: def[dtype: DType, width: Int](Int) capturing thin -> SIMD[dtype, width], origins_vec: OriginSet, reduce_vec_to_vec_fn: def[acc_type: DType, dtype: DType, width: SIMDLength](SIMD[acc_type, width], SIMD[dtype, width]) capturing thin -> SIMD[acc_type, width], reduce_vec_to_scalar_fn: def[dtype: DType, width: SIMDLength](SIMD[dtype, width]) thin -> Scalar[dtype]](dst: Span[Scalar[dtype]], init: Scalar[acc_type]) -> Scalar[acc_type]

Stores the result of calling input_gen_fn in dst and simultaneously reduce the result using a custom reduction function.

Parameters:

  • ​simd_width (SIMDLength): The vector width for the computation.
  • ​dtype (DType): The buffer elements dtype.
  • ​acc_type (DType): The dtype of the reduction accumulator.
  • ​origins_gen (OriginSet): The OriginSet of captured arguments by the input_gen_fn.
  • ​input_gen_fn (def[dtype: DType, width: Int](Int) capturing thin -> SIMD[dtype, width]): A function that generates inputs to reduce.
  • ​origins_vec (OriginSet): The OriginSet of captured arguments by the reduce_vec_to_vec_fn.
  • ​reduce_vec_to_vec_fn (def[acc_type: DType, dtype: DType, width: SIMDLength](SIMD[acc_type, width], SIMD[dtype, width]) capturing thin -> SIMD[acc_type, width]): A mapping function. This function is used to combine (accumulate) two chunks of input data: e.g. we load two 8xfloat32 vectors of elements and need to reduce them into a single 8xfloat32 vector.
  • ​reduce_vec_to_scalar_fn (def[dtype: DType, width: SIMDLength](SIMD[dtype, width]) thin -> Scalar[dtype]): A reduction function. This function is used to reduce a vector to a scalar. E.g. when we got 8xfloat32 vector and want to reduce it to an float32 scalar.

Args:

Returns:

Scalar[acc_type]: The computed reduction value.

def map_reduce[InputGenFnType: def[dtype: DType, width: Int](Int) -> SIMD[dtype, width], ReduceVecToVecFnType: def[acc_type: DType, dtype: DType, width: SIMDLength](SIMD[acc_type, width], SIMD[dtype, width]) -> SIMD[acc_type, width], OutputFnType: def[dtype_: DType, width: SIMDLength, alignment: Int](Int, SIMD[dtype_, width]) -> None, //, simd_width: SIMDLength, dtype: DType, acc_type: DType, reduce_vec_to_scalar_fn: def[dtype: DType, width: SIMDLength](SIMD[dtype, width]) thin -> Scalar[dtype]](input_gen_fn: InputGenFnType, reduce_vec_to_vec_fn: ReduceVecToVecFnType, output_fn: OutputFnType, length: Int, init: Scalar[acc_type]) -> Scalar[acc_type]

Performs a vectorized map-reduce operation over a sequence.

Parameters:

  • ​InputGenFnType (def[dtype: DType, width: Int](Int) -> SIMD[dtype, width]): The type of input_gen_fn (inferred).
  • ​ReduceVecToVecFnType (def[acc_type: DType, dtype: DType, width: SIMDLength](SIMD[acc_type, width], SIMD[dtype, width]) -> SIMD[acc_type, width]): The type of reduce_vec_to_vec_fn (inferred).
  • ​OutputFnType (def[dtype_: DType, width: SIMDLength, alignment: Int](Int, SIMD[dtype_, width]) -> None): The type of output_fn (inferred).
  • ​simd_width (SIMDLength): The SIMD vector width to use.
  • ​dtype (DType): The data type of the input elements.
  • ​acc_type (DType): The data type of the accumulator.
  • ​reduce_vec_to_scalar_fn (def[dtype: DType, width: SIMDLength](SIMD[dtype, width]) thin -> Scalar[dtype]): Function that reduces a final vector to a scalar.

Args:

  • ​input_gen_fn (InputGenFnType): Function that generates input values at each index.
  • ​reduce_vec_to_vec_fn (ReduceVecToVecFnType): Function that reduces a vector into the accumulator.
  • ​output_fn (OutputFnType): Function to output intermediate results.
  • ​length (Int): The number of elements to process.
  • ​init (Scalar[acc_type]): The initial accumulator value.

Returns:

Scalar[acc_type]: The final reduced scalar value.

Was this page helpful?