|
35 | 35 | #[cfg(feature = "parallel")] |
36 | 36 | mod in_parallel; |
37 | 37 | #[cfg(feature = "parallel")] |
38 | | -pub use in_parallel::{build_thread, in_parallel, in_parallel_with_slice, join, threads}; |
| 38 | +pub use in_parallel::{build_thread, in_parallel, in_parallel_chunks, in_parallel_with_slice, join, threads}; |
39 | 39 |
|
40 | 40 | mod serial; |
41 | 41 | #[cfg(not(feature = "parallel"))] |
42 | | -pub use serial::{build_thread, in_parallel, in_parallel_with_slice, join, threads}; |
| 42 | +pub use serial::{build_thread, in_parallel, in_parallel_chunks, in_parallel_with_slice, join, threads}; |
43 | 43 |
|
44 | 44 | mod in_order; |
45 | 45 | pub use in_order::{InOrderIter, SequenceId}; |
@@ -128,6 +128,53 @@ pub fn num_threads(thread_limit: Option<usize>) -> usize { |
128 | 128 | .unwrap_or(logical_cores) |
129 | 129 | } |
130 | 130 |
|
| 131 | +/// Run [`in_parallel()`] only if the given `condition()` returns true when eagerly evaluated. |
| 132 | +/// |
| 133 | +/// For parameters, see the documentation of [`in_parallel()`] |
| 134 | +#[cfg(feature = "parallel")] |
| 135 | +pub fn in_parallel_chunks_if<'a, I, S, O, R>( |
| 136 | + condition: impl FnOnce() -> bool, |
| 137 | + input: &'a mut [I], |
| 138 | + chunk_size: usize, |
| 139 | + thread_limit: Option<usize>, |
| 140 | + new_thread_state: impl Fn(usize) -> S + Send + Clone, |
| 141 | + consume: impl Fn(&'a mut I, &mut S) -> Option<O> + Send + Clone, |
| 142 | + reducer: R, |
| 143 | +) -> Result<<R as Reduce>::Output, <R as Reduce>::Error> |
| 144 | +where |
| 145 | + R: Reduce<Input = O>, |
| 146 | + I: Send, |
| 147 | + O: Send, |
| 148 | +{ |
| 149 | + if num_threads(thread_limit) > 1 && condition() { |
| 150 | + in_parallel_chunks(input, chunk_size, thread_limit, new_thread_state, consume, reducer) |
| 151 | + } else { |
| 152 | + serial::in_parallel_chunks(input, chunk_size, thread_limit, new_thread_state, consume, reducer) |
| 153 | + } |
| 154 | +} |
| 155 | + |
| 156 | +/// Run [`in_parallel()`] only if the given `condition()` returns true when eagerly evaluated. |
| 157 | +/// |
| 158 | +/// For parameters, see the documentation of [`in_parallel()`] |
| 159 | +/// |
| 160 | +/// Note that the non-parallel version is equivalent to [`in_parallel()`]. |
| 161 | +#[cfg(not(feature = "parallel"))] |
| 162 | +pub fn in_parallel_chunks_if<'a, I, S, O, R>( |
| 163 | + _condition: impl FnOnce() -> bool, |
| 164 | + input: &'a mut [I], |
| 165 | + chunk_size: usize, |
| 166 | + thread_limit: Option<usize>, |
| 167 | + new_thread_state: impl Fn(usize) -> S + Send + Clone, |
| 168 | + consume: impl Fn(&'a mut I, &mut S) -> Option<O> + Send + Clone, |
| 169 | + reducer: R, |
| 170 | +) -> Result<<R as Reduce>::Output, <R as Reduce>::Error> |
| 171 | +where |
| 172 | + R: Reduce<Input = O>, |
| 173 | + I: Send, |
| 174 | + O: Send, |
| 175 | +{ |
| 176 | + serial::in_parallel_chunks(input, chunk_size, thread_limit, new_thread_state, consume, reducer) |
| 177 | +} |
131 | 178 | /// Run [`in_parallel()`] only if the given `condition()` returns true when eagerly evaluated. |
132 | 179 | /// |
133 | 180 | /// For parameters, see the documentation of [`in_parallel()`] |
|
0 commit comments