Skip to main content

Crate moirai_parallel

Crate moirai_parallel 

Source
Expand description

Synchronous data-parallel primitives — Moirai’s rayon-replacement surface.

This crate is the parallel domain (throughput over data), distinct from the concurrent domain (moirai-async, async tasks/IO). All operations here are fully synchronous (no async, no .await), so they are safe inside pure compute kernels without async contagion, and operate on borrowed slices with in-place mutation (zero-copy).

§Selecting an execution strategy

Strategy is a zero-sized ExecutionPolicy type (Sequential, Parallel, Adaptive) chosen at compile time, so every form below monomorphizes with no dynamic dispatch:

  • Extension traits (the surface) — slice.par() / slice.par_mut() return Adaptive handles, then for_each / enumerate / map_collect / map_reduce:
    use moirai_parallel::{ParallelSlice, ParallelSliceMut};
    let v: Vec<u64> = (0..1000).collect();
    let sum = v.par().map_reduce(0, |&x| x, |a, b| a + b);   // auto-routes
    let mut m = v.clone();
    m.par_mut().for_each(|x| *x += 1);
  • *_with::<P> free functions — a low-level override that pins the policy via turbofish (for_each_with::<Sequential>(&data, f)), for the rare case that needs to force sequential (determinism / nested regions) or parallel. Most code should just use .par().

Because Adaptive is itself a zero-sized policy, .par() is a fully monomorphized, zero-cost abstraction that parallelizes only at or above ADAPTIVE_PARALLEL_THRESHOLD and runs sequentially below it — the parallel/sequential decision is automatic, with nothing to designate.

These data-parallel ops are synchronous (they return values, not futures), but they run on the same unified hybrid scheduler as async work (moirai_executor::global) — not a separate pool. A .par() worker task can therefore spawn or drive async work (moirai::global().spawn_async/ block_on) on that same runtime, so parallel processing and asynchronous tasks compose within one process. The sync return shape here is a property of the operation, not an isolation boundary.

Re-exports§

pub use ops::ChunkBuffersError;
pub use ops::Scope;
pub use ops::UNIT_TASK_BYTES;
pub use ops::enumerate_mut_with;
pub use ops::enumerate_with;
pub use ops::fold_reduce_with;
pub use ops::for_each_chunk_buffers_mut_enumerated_with;
pub use ops::for_each_chunk_mut_enumerated_with;
pub use ops::for_each_chunk_mut_with;
pub use ops::for_each_chunk_mut_with_state;
pub use ops::for_each_chunk_pair_mut_enumerated_with;
pub use ops::for_each_chunk_quad_mut_enumerated_with;
pub use ops::for_each_chunk_triple_mut_enumerated_with;
pub use ops::for_each_index_with;
pub use ops::for_each_mut_with;
pub use ops::for_each_unit_task_many_mut_with;
pub use ops::for_each_unit_task_mut_with;
pub use ops::for_each_unit_task_pair_mut_with;
pub use ops::for_each_unit_task_range_with;
pub use ops::for_each_unit_task_triple_mut_with;
pub use ops::for_each_with;
pub use ops::join;
pub use ops::join_with;
pub use ops::map_collect_index_with;
pub use ops::map_collect_mut_with;
pub use ops::map_collect_with;
pub use ops::map_reduce_with;
pub use ops::reduce_index_with;
pub use ops::scope;
pub use ops::units_per_task;

Modules§

melinoe_ext
Parallel partitioning drivers for branded Melinoe cell slices.
ops
Synchronous data-parallel operators and free functions. Synchronous data-parallel operators over the unified scheduler.

Structs§

Adaptive
Run in parallel only for inputs at or above ADAPTIVE_PARALLEL_THRESHOLD.
AdaptiveWithThreshold
Run in parallel only for inputs at or above the custom threshold N.
ParMut
A mutable parallel view of a slice bound to execution policy P.
ParRef
A read-only parallel view of a slice bound to execution policy P.
Parallel
Always run in parallel on the shared work-stealing pool.
Sequential
Always run sequentially (single thread, no scheduling).
WorkBytes
Run in parallel only for an operation that moves at least N bytes.

Constants§

ADAPTIVE_PARALLEL_THRESHOLD
Element count at or above which Adaptive chooses parallel execution.

Traits§

ExecutionPolicy
Compile-time strategy selector for the data-parallel operations in this crate.
ParallelSlice
Extension trait providing an adaptive parallel view over &[T].
ParallelSliceMut
Extension trait providing an adaptive mutable parallel view over &mut [T].