Skip to main content

fold_reduce

Function fold_reduce 

Source
pub fn fold_reduce<T, I, F, M>(items: I, fold_fn: F, merge_fn: M) -> T
where T: Send + Default, I: ParallelIterator, F: Fn(T, I::Item) -> T + Sync + Send, M: Fn(T, T) -> T + Sync + Send,
Expand description

Execute parallel fold-reduce with custom accumulator.

This is the most general form of parallel aggregation:

  1. Each thread gets its own accumulator (created by T::default)
  2. fold_fn processes items into thread-local accumulators
  3. merge_fn combines accumulators from different threads

ยงExample

use grafeo_core::execution::parallel::fold::fold_reduce;
use rayon::prelude::*;

let items = vec![1, 2, 3, 4, 5];
let sum: i32 = fold_reduce(
    items.into_par_iter(),
    |acc: i32, item| acc + item,
    |a, b| a + b,
);
assert_eq!(sum, 15i32);