Crate commonware_parallel

Crate commonware_parallel 

Source
Expand description

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

Structs§

Rayon
A parallel execution strategy backed by a rayon thread pool.
Sequential
A sequential execution strategy.

Traits§

Strategy
A strategy for executing fold operations.

Type Aliases§

ThreadPool
A clone-able wrapper around a rayon-compatible thread pool.