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