Skip to main content

orx_parallel/parameters/
params.rs

1use super::{chunk_size::ChunkSize, iteration_order::IterationOrder, num_threads::NumThreads};
2use crate::Par;
3
4/// Parameters of a parallel computation.
5#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
6pub struct Params {
7    /// Number of threads to be used in the parallel computation.
8    ///
9    /// See [`NumThreads`] for details.
10    pub num_threads: NumThreads,
11    /// Chunk size to be used in the parallel computation.
12    ///
13    /// See [`ChunkSize`] for details.
14    pub chunk_size: ChunkSize,
15    /// Ordering of outputs of the parallel computation that is important when the outputs
16    /// are collected into a collection.
17    ///
18    /// See [`IterationOrder`] for details.
19    pub iteration_order: IterationOrder,
20}
21
22impl Params {
23    /// Crates parallel computation parameters for the given configurations.
24    pub fn new(
25        num_threads: impl Into<NumThreads>,
26        chunk_size: impl Into<ChunkSize>,
27        iteration_order: IterationOrder,
28    ) -> Self {
29        Self {
30            num_threads: num_threads.into(),
31            chunk_size: chunk_size.into(),
32            iteration_order,
33        }
34    }
35
36    /// Returns true if number of threads is set to 1.
37    ///
38    /// Note that in this case the computation will be executed sequentially using regular iterators.
39    pub fn is_sequential(self) -> bool {
40        self.num_threads.is_sequential()
41    }
42
43    /// Applies itself to the provided parallel computation `par` and returns it back.
44    pub fn apply<P: Par>(&self, par: P) -> P {
45        par.num_threads(self.num_threads)
46            .chunk_size(self.chunk_size)
47            .iteration_order(self.iteration_order)
48    }
49
50    // helpers
51
52    pub(crate) fn with_num_threads(self, num_threads: impl Into<NumThreads>) -> Self {
53        Self {
54            num_threads: num_threads.into(),
55            chunk_size: self.chunk_size,
56            iteration_order: self.iteration_order,
57        }
58    }
59
60    pub(crate) fn with_chunk_size(self, chunk_size: impl Into<ChunkSize>) -> Self {
61        Self {
62            num_threads: self.num_threads,
63            chunk_size: chunk_size.into(),
64            iteration_order: self.iteration_order,
65        }
66    }
67
68    pub(crate) fn with_collect_ordering(self, iteration_order: IterationOrder) -> Self {
69        Self {
70            num_threads: self.num_threads,
71            chunk_size: self.chunk_size,
72            iteration_order,
73        }
74    }
75}