orx_parallel/parameters/
params.rs

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