gix_features/parallel/in_parallel.rs
1use std::sync::atomic::{AtomicBool, AtomicIsize, AtomicUsize, Ordering};
2
3use crate::parallel::{Reduce, num_threads};
4
5/// A scope to start threads within.
6pub type Scope<'scope, 'env> = std::thread::Scope<'scope, 'env>;
7
8/// Runs `left` and `right` in parallel, returning their output when both are done.
9pub fn join<O1: Send, O2: Send>(left: impl FnOnce() -> O1 + Send, right: impl FnOnce() -> O2 + Send) -> (O1, O2) {
10 std::thread::scope(|s| {
11 let left = std::thread::Builder::new()
12 .name("gitoxide.join.left".into())
13 .spawn_scoped(s, left)
14 .expect("valid name");
15 let right = std::thread::Builder::new()
16 .name("gitoxide.join.right".into())
17 .spawn_scoped(s, right)
18 .expect("valid name");
19 (left.join().unwrap(), right.join().unwrap())
20 })
21}
22
23/// Runs `f` with a scope to be used for spawning threads that will not outlive the function call.
24/// That way it's possible to handle threads without needing the 'static lifetime for data they interact with.
25///
26/// Note that the threads should not rely on actual parallelism as threading might be turned off entirely, hence should not
27/// connect each other with channels as deadlock would occur in single-threaded mode.
28pub fn threads<'env, F, R>(f: F) -> R
29where
30 F: for<'scope> FnOnce(&'scope std::thread::Scope<'scope, 'env>) -> R,
31{
32 std::thread::scope(f)
33}
34
35/// Create a builder for threads which allows them to be spawned into a scope and configured prior to spawning.
36pub fn build_thread() -> std::thread::Builder {
37 std::thread::Builder::new()
38}
39
40/// Read items from `input` and `consume` them in multiple threads,
41/// whose output is collected by a `reducer`. Its task is to
42/// aggregate these outputs into the final result returned by this function, with the benefit of not having to be thread-safe.
43///
44/// * if `thread_limit` is `Some`, then the given number of threads will be used. If `None`, all logical cores will be used.
45/// * `new_thread_state(thread_number) -> State` produces thread-local state once per thread to be passed to `consume`
46/// * `consume(Item, &mut State) -> Output` produces an output given an input obtained by `input` along with mutable state initially
47/// created by `new_thread_state(…)`.
48/// * For `reducer`, see the [`Reduce`] trait
49pub fn in_parallel<I, S, O, R>(
50 input: impl Iterator<Item = I> + Send,
51 thread_limit: Option<usize>,
52 new_thread_state: impl FnOnce(usize) -> S + Send + Clone,
53 consume: impl FnMut(I, &mut S) -> O + Send + Clone,
54 mut reducer: R,
55) -> Result<<R as Reduce>::Output, <R as Reduce>::Error>
56where
57 R: Reduce<Input = O>,
58 I: Send,
59 O: Send,
60{
61 let num_threads = num_threads(thread_limit);
62 std::thread::scope(move |s| {
63 let receive_result = {
64 let (send_input, receive_input) = crossbeam_channel::bounded::<I>(num_threads);
65 let (send_result, receive_result) = crossbeam_channel::bounded::<O>(num_threads);
66 for thread_id in 0..num_threads {
67 std::thread::Builder::new()
68 .name(format!("gitoxide.in_parallel.produce.{thread_id}"))
69 .spawn_scoped(s, {
70 let send_result = send_result.clone();
71 let receive_input = receive_input.clone();
72 let new_thread_state = new_thread_state.clone();
73 let mut consume = consume.clone();
74 move || {
75 let mut state = new_thread_state(thread_id);
76 for item in receive_input {
77 if send_result.send(consume(item, &mut state)).is_err() {
78 break;
79 }
80 }
81 }
82 })
83 .expect("valid name");
84 }
85 std::thread::Builder::new()
86 .name("gitoxide.in_parallel.feed".into())
87 .spawn_scoped(s, move || {
88 for item in input {
89 if send_input.send(item).is_err() {
90 break;
91 }
92 }
93 })
94 .expect("valid name");
95 receive_result
96 };
97
98 for item in receive_result {
99 drop(reducer.feed(item)?);
100 }
101 reducer.finalize()
102 })
103}
104
105/// Read items from `input` and `consume` them in multiple threads,
106/// whose output is collected by a `reducer`. Its task is to
107/// aggregate these outputs into the final result returned by this function with the benefit of not having to be thread-safe.
108/// Call `finalize` to finish the computation, once per thread, if there was no error sending results earlier.
109///
110/// * if `thread_limit` is `Some`, then the given number of threads will be used. If `None`, all logical cores will be used.
111/// * `new_thread_state(thread_number) -> State` produces thread-local state once per thread to be passed to `consume`
112/// * `consume(Item, &mut State) -> Output` produces an output given an input obtained by `input` along with mutable state initially
113/// created by `new_thread_state(…)`.
114/// * `finalize(State) -> Output` is called to potentially process remaining work that was placed in `State`.
115/// * For `reducer`, see the [`Reduce`] trait
116pub fn in_parallel_with_finalize<I, S, O, R>(
117 input: impl Iterator<Item = I> + Send,
118 thread_limit: Option<usize>,
119 new_thread_state: impl FnOnce(usize) -> S + Send + Clone,
120 consume: impl FnMut(I, &mut S) -> O + Send + Clone,
121 finalize: impl FnOnce(S) -> O + Send + Clone,
122 mut reducer: R,
123) -> Result<<R as Reduce>::Output, <R as Reduce>::Error>
124where
125 R: Reduce<Input = O>,
126 I: Send,
127 O: Send,
128{
129 let num_threads = num_threads(thread_limit);
130 std::thread::scope(move |s| {
131 let receive_result = {
132 let (send_input, receive_input) = crossbeam_channel::bounded::<I>(num_threads);
133 let (send_result, receive_result) = crossbeam_channel::bounded::<O>(num_threads);
134 for thread_id in 0..num_threads {
135 std::thread::Builder::new()
136 .name(format!("gitoxide.in_parallel.produce.{thread_id}"))
137 .spawn_scoped(s, {
138 let send_result = send_result.clone();
139 let receive_input = receive_input.clone();
140 let new_thread_state = new_thread_state.clone();
141 let mut consume = consume.clone();
142 let finalize = finalize.clone();
143 move || {
144 let mut state = new_thread_state(thread_id);
145 let mut can_send = true;
146 for item in receive_input {
147 if send_result.send(consume(item, &mut state)).is_err() {
148 can_send = false;
149 break;
150 }
151 }
152 if can_send {
153 send_result.send(finalize(state)).ok();
154 }
155 }
156 })
157 .expect("valid name");
158 }
159 std::thread::Builder::new()
160 .name("gitoxide.in_parallel.feed".into())
161 .spawn_scoped(s, move || {
162 for item in input {
163 if send_input.send(item).is_err() {
164 break;
165 }
166 }
167 })
168 .expect("valid name");
169 receive_result
170 };
171
172 for item in receive_result {
173 drop(reducer.feed(item)?);
174 }
175 reducer.finalize()
176 })
177}
178
179/// An experiment to have fine-grained per-item parallelization with built-in aggregation via thread state.
180/// This is only good for operations where near-random access isn't detrimental, so it's not usually great
181/// for file-io as it won't make use of sorted inputs well.
182/// Note that `periodic` is not guaranteed to be called in case other threads come up first and finish too fast.
183/// `consume(&mut item, &mut stat, &Scope, &threads_available, &should_interrupt)` is called for performing the actual computation.
184/// Note that `threads_available` should be decremented to start a thread that can steal your own work (as stored in `item`),
185/// which allows callees to implement their own work-stealing in case the work is distributed unevenly.
186/// Work stealing should only start after having processed at least one item to give all threads naturally operating on the slice
187/// some time to start. Starting threads while slice-workers are still starting up would lead to over-allocation of threads,
188/// which is why the number of threads left may turn negative. Once threads are started and stopped, be sure to adjust
189/// the thread-count accordingly.
190// TODO: better docs
191pub fn in_parallel_with_slice<I, S, R, E>(
192 input: &mut [I],
193 thread_limit: Option<usize>,
194 new_thread_state: impl FnOnce(usize) -> S + Send + Clone,
195 consume: impl FnMut(&mut I, &mut S, &AtomicIsize, &AtomicBool) -> Result<(), E> + Send + Clone,
196 mut periodic: impl FnMut() -> Option<std::time::Duration> + Send,
197 state_to_rval: impl FnOnce(S) -> R + Send + Clone,
198) -> Result<Vec<R>, E>
199where
200 I: Send,
201 E: Send,
202 R: Send,
203{
204 let num_threads = num_threads(thread_limit);
205 let mut results = Vec::with_capacity(num_threads);
206 let stop_everything = &AtomicBool::default();
207 let a_consumer_failed = &AtomicBool::default();
208 let index = &AtomicUsize::default();
209 let threads_left = &AtomicIsize::new(num_threads as isize);
210
211 std::thread::scope({
212 move |s| {
213 std::thread::Builder::new()
214 .name("gitoxide.in_parallel_with_slice.watch-interrupts".into())
215 .spawn_scoped(s, {
216 move || loop {
217 if stop_everything.load(Ordering::Relaxed) {
218 break;
219 }
220
221 match periodic() {
222 Some(duration) => std::thread::sleep(duration),
223 None => {
224 stop_everything.store(true, Ordering::Relaxed);
225 break;
226 }
227 }
228 }
229 })
230 .expect("valid name");
231
232 let input_len = input.len();
233 struct Input<I>(*mut I)
234 where
235 I: Send;
236
237 // SAFETY: I is Send, and we only use the pointer for creating new
238 // pointers (within the input slice) from the threads.
239 #[expect(unsafe_code)]
240 unsafe impl<I> Send for Input<I> where I: Send {}
241
242 let threads: Vec<_> = (0..num_threads)
243 .map(|thread_id| {
244 std::thread::Builder::new()
245 .name(format!("gitoxide.in_parallel_with_slice.produce.{thread_id}"))
246 .spawn_scoped(s, {
247 let new_thread_state = new_thread_state.clone();
248 let state_to_rval = state_to_rval.clone();
249 let mut consume = consume.clone();
250 let input = Input(input.as_mut_ptr());
251 move || {
252 let _ = &input;
253 threads_left.fetch_sub(1, Ordering::SeqCst);
254 let mut state = new_thread_state(thread_id);
255 let res = (|| {
256 while let Ok(input_index) =
257 index.fetch_update(Ordering::SeqCst, Ordering::SeqCst, |x| {
258 (x < input_len).then_some(x + 1)
259 })
260 {
261 if stop_everything.load(Ordering::Relaxed) {
262 break;
263 }
264 // SAFETY: our atomic counter for `input_index` is only ever incremented, yielding
265 // each item exactly once.
266 let item = {
267 #[expect(unsafe_code)]
268 unsafe {
269 &mut *input.0.add(input_index)
270 }
271 };
272 if let Err(err) = consume(item, &mut state, threads_left, stop_everything) {
273 // The first consumer to fail is the one that caused the stop - every
274 // error thereafter is a mere reaction to the stop signal, like a
275 // bail-out on interrupt, and must not mask the causal error.
276 let is_cause = !a_consumer_failed.swap(true, Ordering::SeqCst);
277 stop_everything.store(true, Ordering::Relaxed);
278 return Err((err, is_cause));
279 }
280 }
281 Ok(state_to_rval(state))
282 })();
283 threads_left.fetch_add(1, Ordering::SeqCst);
284 res
285 }
286 })
287 .expect("valid name")
288 })
289 .collect();
290 for thread in threads {
291 match thread.join() {
292 Ok(Ok(rval)) => {
293 results.push(rval);
294 }
295 Ok(Err((err, is_cause))) => {
296 // is-cause is race-free, and must be set on the first error.
297 if is_cause {
298 return Err(err);
299 }
300 }
301 Err(err) => {
302 // a panic happened, stop the world gracefully (even though we panic later)
303 stop_everything.store(true, Ordering::Relaxed);
304 std::panic::resume_unwind(err);
305 }
306 }
307 }
308
309 stop_everything.store(true, Ordering::Relaxed);
310 Ok(results)
311 }
312 })
313}