Debian Codegraph @

src/parallel/mod.rs

raw symbols SCIP
1 //! Run computations in parallel, or not based the `parallel` feature toggle.
2 //!
3 //! ### `in_parallel`(…)
4 //!
5 //! The [`in_parallel(…)`][in_parallel()] is the typical fan-out-fan-in mode of parallelism, with thread local storage
6 //! made available to a `consume(…)` function to process input. The result is sent to the [`Reduce`] running in the calling
7 //! thread to aggregate the results into a single output, which is returned by [`in_parallel()`].
8 //!
9 //! Interruptions can be achieved by letting the reducers [`feed(…)`][Reduce::feed()] method fail.
10 //!
11 //! It gets a boost in usability as it allows threads to borrow variables from the stack, most commonly the repository itself
12 //! or the data to work on.
13 //!
14 //! This mode of operation doesn't lend itself perfectly to being wrapped for `async` as it appears like a single long-running
15 //! operation which runs as fast as possible, which is cancellable only by merit of stopping the input or stopping the output
16 //! aggregation.
17 //!
18 //! ### `reduce::Stepwise`
19 //!
20 //! The [`Stepwise`][reduce::Stepwise] iterator works exactly as [`in_parallel()`] except that the processing of the output produced by
21 //! `consume(I, &mut State) -> O` is made accessible by the `Iterator` trait's `next()` method. As produced work is not
22 //! buffered, the owner of the iterator controls the progress made.
23 //!
24 //! Getting the final output of the [`Reduce`] is achieved through the consuming [`Stepwise::finalize()`][reduce::Stepwise::finalize()] method, which
25 //! is functionally equivalent to calling [`in_parallel()`].
26 //!
27 //! In an `async` context this means that progress is only made each time `next()` is called on the iterator, while merely dropping
28 //! the iterator will wind down the computation without any result.
29 //!
30 //! #### Maintaining Safety
31 //!
32 //! In order to assure that threads don't outlive the data they borrow because their handles are leaked, we enforce
33 //! the `'static` lifetime for its inputs, making it less intuitive to use. It is, however, possible to produce
34 //! suitable input iterators as long as they can hold something on the heap.
35 #[cfg(feature = "parallel")]
36 mod in_parallelrust-analyzer cargo gix-features 0.48.0 parallel/in_parallel/;
37 #[cfg(feature = "parallel")]
38 pub use in_parallelrust-analyzer cargo gix-features 0.48.0 parallel/in_parallel/::{
39 build_threadrust-analyzer cargo gix-features 0.48.0 parallel/in_parallel/build_thread()., in_parallelrust-analyzer cargo gix-features 0.48.0 parallel/in_parallel/in_parallel()., in_parallel_with_finalizerust-analyzer cargo gix-features 0.48.0 parallel/in_parallel/in_parallel_with_finalize()., in_parallel_with_slicerust-analyzer cargo gix-features 0.48.0 parallel/in_parallel/in_parallel_with_slice()., joinrust-analyzer cargo gix-features 0.48.0 parallel/in_parallel/join()., threadsrust-analyzer cargo gix-features 0.48.0 parallel/in_parallel/threads()., Scoperust-analyzer cargo gix-features 0.48.0 parallel/in_parallel/Scope#,
40 };
41
42 mod serialrust-analyzer cargo gix-features 0.48.0 parallel/serial/;
43 #[cfg(not(feature = "parallel"))]
44 pub use serialrust-analyzer cargo gix-features 0.48.0 parallel/serial/::{build_thread, in_parallelrust-analyzer cargo gix-features 0.48.0 parallel/serial/in_parallel()., in_parallel_with_finalize, in_parallel_with_slice, join, threads, Scope};
45
46 mod in_orderrust-analyzer cargo gix-features 0.48.0 parallel/in_order/;
47 pub use in_orderrust-analyzer cargo gix-features 0.48.0 parallel/in_order/::{InOrderIterrust-analyzer cargo gix-features 0.48.0 parallel/in_order/InOrderIter#, SequenceIdrust-analyzer cargo gix-features 0.48.0 parallel/in_order/SequenceId#};
48
49 mod eager_iterrust-analyzer cargo gix-features 0.48.0 parallel/eager_iter/;
50 pub use eager_iterrust-analyzer cargo gix-features 0.48.0 parallel/eager_iter/::{EagerIterrust-analyzer cargo gix-features 0.48.0 parallel/eager_iter/EagerIter#, EagerIterIfrust-analyzer cargo gix-features 0.48.0 parallel/eager_iter/EagerIterIf#};
51
52 /// A no-op returning the input _(`desired_chunk_size`, `Some(thread_limit)`, `thread_limit)_ used
53 /// when the `parallel` feature toggle is not set.
54 #[cfg(not(feature = "parallel"))]
55 pub fn optimize_chunk_size_and_thread_limit(
56 desired_chunk_size: usize,
57 _num_items: Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<usize>,
58 thread_limit: Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<usize>,
59 _available_threads: Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<usize>,
60 ) -> (usize, Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<usize>, usize) {
61 (desired_chunk_size, thread_limit, num_threadsrust-analyzer cargo gix-features 0.48.0 parallel/num_threads().(thread_limit))
62 }
63
64 /// Return the 'optimal' _(`size of chunks`, `amount of threads as Option`, `amount of threads`)_ to use in [`in_parallel()`] for the given
65 /// `desired_chunk_size`, `num_items`, `thread_limit` and `available_threads`.
66 ///
67 /// * `desired_chunk_size` is the amount of items per chunk you think should be used.
68 /// * `num_items` is the total amount of items in the iteration, if `Some`.
69 /// Otherwise this knowledge will not affect the output of this function.
70 /// * `thread_limit` is the amount of threads to use at most, if `Some`.
71 /// Otherwise this knowledge will not affect the output of this function.
72 /// * `available_threads` is the total amount of threads available, if `Some`.
73 /// Otherwise the actual amount of available threads is determined by querying the system.
74 ///
75 /// `Note` that this implementation is available only if the `parallel` feature toggle is set.
76 #[cfg(feature = "parallel")]
77 pub fn optimize_chunk_size_and_thread_limitrust-analyzer cargo gix-features 0.48.0 parallel/optimize_chunk_size_and_thread_limit().(
78 desired_chunk_size: usize,
79 num_items: Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<usize>,
80 thread_limit: Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<usize>,
81 available_threads: Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<usize>,
82 ) -> (usize, Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<usize>, usize) {
83 let available_threads =
84 available_threads.unwrap_or_elserust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap_or_else().(|| stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::threadrust-analyzer cargo std https://github.com/rust-lang/rust/library/std thread/::available_parallelismrust-analyzer cargo std https://github.com/rust-lang/rust/library/std thread/functions/available_parallelism().().map_orrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]map_or().(1, Intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/Into#::intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().));
85 let available_threads = thread_limit.map_orrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]map_or().(available_threads, |l| if l rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]eq().==rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]eq(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]eq().0 { available_threads } else { l });
86
87 let (lower, upper) = (50, 1000);
88 let (chunk_size, thread_limit) = num_items.map_orrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]map_or().(
89 {
90 let chunk_size = if available_threads rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]eq().==rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]eq(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]eq().1 {
91 desired_chunk_size
92 } else if desired_chunk_size rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]lt().<rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]lt(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]lt().lower {
93 lower
94 } else {
95 desired_chunk_size.minrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Ord#min().(upper)
96 };
97 (chunk_size, available_threads)
98 },
99 |num_items| {
100 let desired_chunks_per_thread_at_least = 2;
101 let items = num_items;
102 let chunk_size = (items rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Div<Self>`]div()./rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Div<Self>`]div(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Div<Self>`]div().(available_threads rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Mul<Self>`]mul().*rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Mul<Self>`]mul(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Mul<Self>`]mul().desired_chunks_per_thread_at_least)).clamprust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][Ord]clamp().(1, upper);
103 let num_chunks = items rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Div<Self>`]div()./rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Div<Self>`]div(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Div<Self>`]div().chunk_size;
104 let thread_limit = if num_chunks rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]le().<=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]le(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]le().available_threads {
105 (num_chunks rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Div<Self>`]div()./rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Div<Self>`]div(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Div<Self>`]div().desired_chunks_per_thread_at_least).maxrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Ord#max().(1)
106 } else {
107 available_threads
108 };
109 (chunk_size, thread_limit)
110 },
111 );
112 (chunk_size, Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(thread_limit), thread_limit)
113 }
114
115 /// Always returns 1, available when the `parallel` feature toggle is unset.
116 #[cfg(not(feature = "parallel"))]
117 pub fn num_threads(_thread_limit: Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<usize>) -> usize {
118 1
119 }
120
121 /// Returns the amount of threads the system can effectively use as the amount of its logical cores.
122 ///
123 /// Only available with the `parallel` feature toggle set.
124 #[cfg(feature = "parallel")]
125 pub fn num_threadsrust-analyzer cargo gix-features 0.48.0 parallel/num_threads().(thread_limit: Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<usize>) -> usize {
126 let logical_cores = stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::threadrust-analyzer cargo std https://github.com/rust-lang/rust/library/std thread/::available_parallelismrust-analyzer cargo std https://github.com/rust-lang/rust/library/std thread/functions/available_parallelism().().map_orrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]map_or().(1, Intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/Into#::intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().);
127 thread_limit.map_orrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]map_or().(logical_cores, |l| if l rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]eq().==rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]eq(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]eq().0 { logical_cores } else { l })
128 }
129
130 /// Run [`in_parallel()`] only if the given `condition()` returns true when eagerly evaluated.
131 ///
132 /// For parameters, see the documentation of [`in_parallel()`]
133 #[cfg(feature = "parallel")]
134 pub fn in_parallel_ifrust-analyzer cargo gix-features 0.48.0 parallel/in_parallel_if().<I, S, O, R>(
135 condition: impl FnOncerust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/function/FnOnce#() -> bool,
136 input: impl Iteratorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#<Itemrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#Item# = I> + Sendrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Send#,
137 thread_limit: Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<usize>,
138 new_thread_state: impl FnOncerust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/function/FnOnce#(usize) -> S + Sendrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Send# + Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone#,
139 consume: impl FnMutrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/function/FnMut#(I, &mut S) -> O + Sendrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Send# + Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone#,
140 reducer: R,
141 ) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<<R as Reducerust-analyzer cargo gix-features 0.48.0 parallel/reduce/Reduce#>::Outputrust-analyzer cargo gix-features 0.48.0 parallel/reduce/Reduce#Output#, <R as Reducerust-analyzer cargo gix-features 0.48.0 parallel/reduce/Reduce#>::Errorrust-analyzer cargo gix-features 0.48.0 parallel/reduce/Reduce#Error#>
142 where
143 R: Reducerust-analyzer cargo gix-features 0.48.0 parallel/reduce/Reduce#<Inputrust-analyzer cargo gix-features 0.48.0 parallel/reduce/Reduce#Input# = O>,
144 I: Sendrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Send#,
145 O: Sendrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Send#,
146 {
147 if num_threadsrust-analyzer cargo gix-features 0.48.0 parallel/num_threads().(thread_limit) rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]gt().>rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]gt(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]gt().1 && condition() {
148 in_parallelrust-analyzer cargo gix-features 0.48.0 parallel/in_parallel/in_parallel().(input, thread_limit, new_thread_state, consume, reducer)
149 } else {
150 serialrust-analyzer cargo gix-features 0.48.0 parallel/serial/::in_parallelrust-analyzer cargo gix-features 0.48.0 parallel/serial/in_parallel().(input, thread_limit, new_thread_state, consume, reducer)
151 }
152 }
153
154 /// Run [`in_parallel()`] only if the given `condition()` returns true when eagerly evaluated.
155 ///
156 /// For parameters, see the documentation of [`in_parallel()`]
157 ///
158 /// Note that the non-parallel version is equivalent to [`in_parallel()`].
159 #[cfg(not(feature = "parallel"))]
160 pub fn in_parallel_if<I, S, O, R>(
161 _condition: impl FnOncerust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/function/FnOnce#() -> bool,
162 input: impl Iteratorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#<Itemrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#Item# = I>,
163 thread_limit: Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<usize>,
164 new_thread_state: impl FnOncerust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/function/FnOnce#(usize) -> S,
165 consume: impl FnMutrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/function/FnMut#(I, &mut S) -> O,
166 reducer: R,
167 ) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<<R as Reducerust-analyzer cargo gix-features 0.48.0 parallel/reduce/Reduce#>::Outputrust-analyzer cargo gix-features 0.48.0 parallel/reduce/Reduce#Output#, <R as Reducerust-analyzer cargo gix-features 0.48.0 parallel/reduce/Reduce#>::Errorrust-analyzer cargo gix-features 0.48.0 parallel/reduce/Reduce#Error#>
168 where
169 R: Reducerust-analyzer cargo gix-features 0.48.0 parallel/reduce/Reduce#<Inputrust-analyzer cargo gix-features 0.48.0 parallel/reduce/Reduce#Input# = O>,
170 I: Sendrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Send#,
171 O: Sendrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Send#,
172 {
173 serialrust-analyzer cargo gix-features 0.48.0 parallel/serial/::in_parallelrust-analyzer cargo gix-features 0.48.0 parallel/serial/in_parallel().(input, thread_limit, new_thread_state, consume, reducer)
174 }
175
176 ///
177 pub mod reducerust-analyzer cargo gix-features 0.48.0 parallel/reduce/;
178 pub use reducerust-analyzer cargo gix-features 0.48.0 parallel/reduce/::Reducerust-analyzer cargo gix-features 0.48.0 parallel/reduce/Reduce#;