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 valuefold_init: Likefold, but with per-partition initialization
Convenience Methods:
map_collect_vec: Maps elements and collects into aVecmap_init_collect_vec: Likemap_collect_vecwith per-partition initialization
Two implementations are provided:
Sequential: Executes operations sequentially on the current thread (works inno_std)Rayon: Executes operations in parallel using arayonthread pool (requiresstd)
§Features
std(default): Enables theRayonstrategy 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 + 25Structs§
- 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§
- Thread
Pool - A clone-able wrapper around a rayon-compatible thread pool.