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 valuetry_fold: Likefold, but stops applying the fold operation after failuresfold_init: Likefold, but with per-partition initializationsort_by: Sorts a slice with a comparator
Convenience Methods:
map_collect_vec: Maps elements and collects into aVectry_map_collect_vec: Maps fallible operations and collects into aResult<Vec<_>, _>map_init_collect_vec: Likemap_collect_vecwith per-partition initializationmap_partition_collect_vec: Maps elements, collecting successful results and tracking indices of filtered elements
Two implementations are provided:
Sequential: Executes operations sequentially on the current thread (works inno_std)Rayon: Adaptively executes collection operations serially or with 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§
- Manual
- A strategy wrapper for manually partitioned work.
- 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.