3 releases (1 stable)

new 2026.2.0 Feb 5, 2026
0.0.65 Jan 14, 2026
0.0.64 Jan 6, 2026

#8 in #fold

Download history 43/week @ 2026-01-08 673/week @ 2026-01-15 1247/week @ 2026-01-22 1135/week @ 2026-01-29

3,098 downloads per month
Used in 26 crates (14 directly)

MIT/Apache

44KB
343 lines

commonware-parallel

Crates.io Docs.rs

Parallelize fold operations with pluggable execution strategies.

Status

Stability varies by primitive. See README for details.


lib.rs:

Parallelize fold operations with pluggable execution strategies..

This crate provides the Strategy trait, which abstracts over sequential and parallel execution of fold operations. This allows algorithms to be written once and executed either sequentially or in parallel depending on the chosen strategy.

Overview

The core abstraction is the Strategy trait, which provides several operations:

Core Operations:

  • fold: Reduces a collection to a single value
  • fold_init: Like fold, but with per-partition initialization

Convenience Methods:

Two implementations are provided:

  • Sequential: Executes operations sequentially on the current thread (works in no_std)
  • Rayon: Executes operations in parallel using a rayon thread pool (requires std)

Features

  • std (default): Enables the Rayon strategy backed by rayon

When the std feature is disabled, only Sequential is available, making this crate suitable for no_std environments.

Example

The main benefit of this crate is writing algorithms that can switch between sequential and parallel execution:

use commonware_parallel::{Strategy, Sequential};

fn sum_of_squares(strategy: &impl Strategy, data: &[i64]) -> i64 {
    strategy.fold(
        data,
        || 0i64,
        |acc, &x| acc + x * x,
        |a, b| a + b,
    )
}

let strategy = Sequential;
let data = vec![1, 2, 3, 4, 5];
let result = sum_of_squares(&strategy, &data);
assert_eq!(result, 55); // 1 + 4 + 9 + 16 + 25

Dependencies

~2.6–4MB
~88K SLoC