Debian Codegraph @

src/parallel/serial.rs

raw symbols SCIP
1 use craterust-analyzer cargo gix-features 0.48.0 crate/::parallelrust-analyzer cargo gix-features 0.48.0 parallel/::Reducerust-analyzer cargo gix-features 0.48.0 parallel/reduce/Reduce#;
2
3 #[cfg(not(feature = "parallel"))]
4 mod not_parallel {
5 use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::syncrust-analyzer cargo std https://github.com/rust-lang/rust/library/std sync/::atomicrust-analyzer cargo core https://github.com/rust-lang/rust/library/core sync/atomic/::{AtomicBoolrust-analyzer cargo core https://github.com/rust-lang/rust/library/core sync/atomic/AtomicBool#, AtomicIsizerust-analyzer cargo core https://github.com/rust-lang/rust/library/core sync/atomic/AtomicIsize#};
6
7 /// Runs `left` and then `right`, one after another, returning their output when both are done.
8 pub fn join<O1, O2>(left: impl FnOncerust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/function/FnOnce#() -> O1, right: impl FnOncerust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/function/FnOnce#() -> O2) -> (O1, O2) {
9 (left(), right())
10 }
11
12 /// A scope for spawning threads.
13 pub struct Scope<'scope, 'env: 'scope> {
14 _scope: stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::markerrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/::PhantomDatarust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/PhantomData#<&'scope mut &'scope ()>,
15 _env: stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::markerrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/::PhantomDatarust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/PhantomData#<&'env mut &'env ()>,
16 }
17
18 pub struct ThreadBuilder;
19
20 /// Create a builder for threads which allows them to be spawned into a scope and configured prior to spawning.
21 pub fn build_thread() -> ThreadBuilder {
22 ThreadBuilder
23 }
24
25 #[allow(unsafe_code)]
26 unsafe impl Syncrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Sync# for Scope<'_, '_> {}
27
28 impl ThreadBuilder {
29 pub fn name(self, _new: Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#) -> Self {
30 selfrust-analyzer cargo gix-features 0.48.0 parallel/serial/
31 }
32 pub fn spawn_scoped<'scope, 'env, F, T>(
33 &self,
34 scope: &'scope Scope<'scope, 'env>,
35 f: F,
36 ) -> stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Resultrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Result#<ScopedJoinHandle<'scope, T>>
37 where
38 F: FnOncerust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/function/FnOnce#() -> T + 'scope,
39 T: 'scope,
40 {
41 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(scope.spawn(f))
42 }
43 }
44
45 impl<'scope> Scope<'scope, '_> {
46 /// Provided with this scope, let `f` start new threads that live within it.
47 pub fn spawn<F, T>(&'scope self, f: F) -> ScopedJoinHandle<'scope, T>
48 where
49 F: FnOncerust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/function/FnOnce#() -> T + 'scope,
50 T: 'scope,
51 {
52 ScopedJoinHandle {
53 result: f(),
54 _marker: Defaultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core default/Default#::default(),
55 }
56 }
57 }
58
59 /// Runs `f` with a scope to be used for spawning threads that will not outlive the function call.
60 /// Note that this implementation will run the spawned functions immediately.
61 pub fn threads<'env, F, R>(f: F) -> R
62 where
63 F: for<'scope> FnOncerust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/function/FnOnce#(&'scope Scope<'scope, 'env>) -> R,
64 {
65 f(&Scope {
66 _scope: Defaultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core default/Default#::default(),
67 _env: Defaultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core default/Default#::default(),
68 })
69 }
70
71 /// A handle that can be used to join its scoped thread.
72 ///
73 /// This struct is created by the [`Scope::spawn`] method and the
74 /// [`ScopedThreadBuilder::spawn`] method.
75 pub struct ScopedJoinHandle<'scope, T> {
76 /// Holds the result of the inner closure.
77 result: T,
78 _marker: stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::markerrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/::PhantomDatarust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/PhantomData#<&'scope mut &'scope ()>,
79 }
80
81 impl<T> ScopedJoinHandle<'_, T> {
82 pub fn join(self) -> 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/::Resultrust-analyzer cargo std https://github.com/rust-lang/rust/library/std thread/Result#<T> {
83 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(selfrust-analyzer cargo gix-features 0.48.0 parallel/serial/.result)
84 }
85 pub fn is_finished(&self) -> bool {
86 true
87 }
88 }
89
90 /// An experiment to have fine-grained per-item parallelization with built-in aggregation via thread state.
91 /// This is only good for operations where near-random access isn't detrimental, so it's not usually great
92 /// for file-io as it won't make use of sorted inputs well.
93 // TODO: better docs
94 pub fn in_parallel_with_slice<I, S, R, E>(
95 input: &mut [I],
96 _thread_limit: Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<usize>,
97 new_thread_state: impl FnOncerust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/function/FnOnce#(usize) -> S + Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone#,
98 mut consume: impl FnMutrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/function/FnMut#(&mut I, &mut S, &AtomicIsize, &AtomicBool) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<(), E> + Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone#,
99 mut periodic: impl FnMutrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/function/FnMut#() -> Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::timerust-analyzer cargo std https://github.com/rust-lang/rust/library/std time/::Durationrust-analyzer cargo core https://github.com/rust-lang/rust/library/core time/Duration#>,
100 state_to_rval: impl FnOncerust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/function/FnOnce#(S) -> R + Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone#,
101 ) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<R>, E> {
102 let mut state = new_thread_state(0);
103 let should_interrupt = &AtomicBool::default();
104 let threads_left = &AtomicIsize::default();
105 for item in input {
106 consume(item, &mut state, threads_left, should_interrupt)?;
107 if periodic().is_none() {
108 break;
109 }
110 }
111 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![state_to_rval(state)])
112 }
113 }
114
115 #[cfg(not(feature = "parallel"))]
116 pub use not_parallel::{build_thread, in_parallel_with_slice, join, threads, Scope};
117
118 /// Read items from `input` and `consume` them in a single thread, producing an output to be collected by a `reducer`,
119 /// whose task it is to aggregate these outputs into the final result returned by this function.
120 ///
121 /// * `new_thread_state(thread_number) -> State` produces thread-local state once per thread to be passed to `consume`
122 /// * `consume(Item, &mut State) -> Output` produces an output given an input along with mutable state.
123 /// * For `reducer`, see the [`Reduce`] trait
124 /// * `thread_limit` has no effect as everything is run on the main thread, but is present to keep the signature
125 /// similar to the parallel version.
126 ///
127 /// **This serial version performing all calculations on the current thread.**
128 pub fn in_parallelrust-analyzer cargo gix-features 0.48.0 parallel/serial/in_parallel().<I, S, O, R>(
129 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>,
130 _thread_limit: Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<usize>,
131 new_thread_state: impl FnOncerust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/function/FnOnce#(usize) -> S,
132 mut consume: impl FnMutrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/function/FnMut#(I, &mut S) -> O,
133 mut reducer: R,
134 ) -> 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#>
135 where
136 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>,
137 {
138 let mut state = new_thread_state(0);
139 for item in input {
140 droprust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/drop().(reducer.feedrust-analyzer cargo gix-features 0.48.0 parallel/reduce/Reduce#feed().(consume(item, &mut state))?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().);
141 }
142 reducer.finalizerust-analyzer cargo gix-features 0.48.0 parallel/reduce/Reduce#finalize().()
143 }
144
145 /// Read items from `input` and `consume` them in multiple threads,
146 /// whose output is collected by a `reducer`. Its task is to
147 /// aggregate these outputs into the final result returned by this function with the benefit of not having to be thread-safe.
148 /// Call `finalize` to finish the computation, once per thread, if there was no error sending results earlier.
149 ///
150 /// * if `thread_limit` is `Some`, the given number of threads will be used. If `None`, all logical cores will be used.
151 /// * `new_thread_state(thread_number) -> State` produces thread-local state once per thread to be passed to `consume`
152 /// * `consume(Item, &mut State) -> Output` produces an output given an input obtained by `input` along with mutable state initially
153 /// created by `new_thread_state(…)`.
154 /// * `finalize(State) -> Output` is called to potentially process remaining work that was placed in `State`.
155 /// * For `reducer`, see the [`Reduce`] trait
156 #[cfg(not(feature = "parallel"))]
157 pub fn in_parallel_with_finalize<I, S, O, R>(
158 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>,
159 _thread_limit: Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<usize>,
160 new_thread_state: impl FnOncerust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/function/FnOnce#(usize) -> S,
161 mut consume: impl FnMutrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/function/FnMut#(I, &mut S) -> O,
162 finalize: impl FnOncerust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/function/FnOnce#(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#,
163 mut reducer: R,
164 ) -> 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#>
165 where
166 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>,
167 {
168 let mut state = new_thread_state(0);
169 for item in input {
170 droprust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/drop().(reducer.feed(consume(item, &mut state))?);
171 }
172 reducer.feed(finalize(state))?;
173 reducer.finalize()
174 }