orx_parallel/runner/
parallel_runner.rs

1use super::{computation_kind::ComputationKind, thread_runner::ThreadRunner};
2use crate::parameters::Params;
3use orx_concurrent_iter::ConcurrentIter;
4
5/// A parallel runner which is responsible for taking a computation defined as a composition
6/// of iterator methods, spawns threads, shares tasks and returns the result of the parallel
7/// execution.
8pub trait ParallelRunner: Sized + Sync + 'static {
9    /// Data shared to the thread runners.
10    type SharedState: Send + Sync;
11
12    /// Thread runner that is responsible for executing the tasks allocated to a thread.
13    type ThreadRunner: ThreadRunner<SharedState = Self::SharedState>;
14
15    /// Creates a new parallel runner for the given computation `kind`, parallelization `params`
16    /// and `initial_input_len`.
17    fn new(kind: ComputationKind, params: Params, initial_input_len: Option<usize>) -> Self;
18
19    /// Creates an initial shared state.
20    fn new_shared_state(&self) -> Self::SharedState;
21
22    /// Returns true if it is beneficial to spawn a new thread provided that:
23    ///
24    /// * `num_spawned` threads are already been spawned, and
25    /// * `shared_state` is the current parallel execution state.
26    fn do_spawn_new<I>(
27        &self,
28        num_spawned: usize,
29        shared_state: &Self::SharedState,
30        iter: &I,
31    ) -> bool
32    where
33        I: ConcurrentIter;
34
35    /// Creates a new thread runner provided that the current parallel execution state is
36    /// `shared_state`.
37    fn new_thread_runner(&self, shared_state: &Self::SharedState) -> Self::ThreadRunner;
38}