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:
run: Chooses between serial and parallel operation bodiesrun_batches: Runs an operation over batches of its inputrun_tiles: Runs an operation over tiles of several passes over the same inputfold: 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 comparatorsort_unstable_by: Likesort_by, but without preserving the order of equal elements
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 a Commonware thread pool (requiresstd)
§Features
std(default): Enables theRayonstrategy and its thread pooltest-utils: Enablesmocks, strategies that pin or explore a test’s execution shapes
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 + 25Modules§
- rayon
- A work-stealing pool of worker threads.
Structs§
- Batches
- Batches supplied for one invocation of
Strategy::run_batches. - Grid
- A grid that
Strategy::run_tilescan cut, made ofrowspasses over the run’s columns. Finishing a tile costs a fixedtile_costcells. - Rayon
- A parallel execution strategy backed by a Commonware thread pool.
- Sequential
- A sequential execution strategy.
- Tiles
- Tiles supplied for one invocation of
Strategy::run_tiles.
Traits§
- Strategy
- A strategy for executing fold operations.
Type Aliases§
- Thread
Pool - A shared
rayon::ThreadPool.