1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
use std::num::NonZeroUsize;
use crate::{BatchDecision, BatchPolicy, BatchQuery, ExecError, Pool};
/// Parallelism granted to an operation by [`Exec::install`].
///
/// # Examples
///
/// ```
/// assert_eq!(tcontract_exec::Par::Seq.threads(), 1);
/// ```
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Par {
/// Run serially on the current thread.
Seq,
/// Up to this many workers of the pool the closure runs in.
Threads(NonZeroUsize),
}
impl Par {
/// Worker count (1 for `Seq`).
pub fn threads(self) -> usize {
match self {
Par::Seq => 1,
Par::Threads(n) => n.get(),
}
}
}
/// Execution context passed to every operation that may run in parallel.
///
/// # Examples
///
/// ```
/// use tcontract_exec::Exec;
/// assert_eq!(Exec::serial().budget(), 1);
/// assert!(!Exec::serial().is_worker());
/// ```
#[derive(Clone, Copy)]
#[non_exhaustive]
pub enum Exec<'a> {
/// Everything on the calling thread, with call-local scratch. Carries the
/// batch policy so a forced strategy is still resolved at one thread.
Serial {
/// The batch lane policy this context applies.
policy: BatchPolicy,
},
/// Everything on the calling thread, with caller-owned reusable scratch.
SerialWithWorkspace {
/// The caller-owned storage.
workspace: &'a dyn crate::WorkspaceProvider,
/// The batch lane policy this context applies.
policy: BatchPolicy,
},
/// A borrowed pool with a thread budget `<= pool.size()`.
Rayon {
/// The borrowed pool.
pool: &'a Pool<'a>,
/// Most threads an operation may occupy.
budget: NonZeroUsize,
/// The batch lane policy this context applies.
policy: BatchPolicy,
},
}
// A `&dyn WorkspaceProvider` is not `Debug`, so the derive cannot cover the
// serial-with-workspace variant. The provider is an anonymous implementation
// detail here, so only the variant name is shown.
impl std::fmt::Debug for Exec<'_> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Exec::Serial { .. } => f.write_str("Serial"),
Exec::SerialWithWorkspace { .. } => f.write_str("SerialWithWorkspace(..)"),
Exec::Rayon {
pool,
budget,
policy,
} => f
.debug_struct("Rayon")
.field("pool", pool)
.field("budget", budget)
.field("policy", policy)
.finish(),
}
}
}
impl<'a> Exec<'a> {
/// The serial context.
pub const fn serial() -> Exec<'static> {
Exec::Serial {
policy: crate::batch::DEFAULT_BATCH_POLICY,
}
}
/// Serial execution with caller-owned reusable storage.
pub fn serial_with_workspace(workspace: &'a dyn crate::WorkspaceProvider) -> Self {
Exec::SerialWithWorkspace {
workspace,
policy: crate::batch::DEFAULT_BATCH_POLICY,
}
}
/// Use `pool` with its full size as budget and the default batch policy.
pub fn rayon(pool: &'a Pool<'a>) -> Self {
let budget = NonZeroUsize::new(pool.size()).unwrap_or(NonZeroUsize::MIN);
Exec::Rayon {
pool,
budget,
policy: BatchPolicy::default(),
}
}
/// Limit the budget; values above the pool size are clamped.
///
/// # Errors
///
/// [`ExecError::ZeroBudget`] when `max_threads == 0`.
///
/// # Examples
///
/// ```
/// use tcontract_exec::{Exec, ExecError};
/// assert_eq!(Exec::serial().with_budget(0).unwrap_err(), ExecError::ZeroBudget);
/// ```
pub fn with_budget(self, max_threads: usize) -> Result<Self, ExecError> {
let max = NonZeroUsize::new(max_threads).ok_or(ExecError::ZeroBudget)?;
Ok(match self {
Exec::Serial { .. } | Exec::SerialWithWorkspace { .. } => self,
Exec::Rayon { pool, .. } => {
let cap = NonZeroUsize::new(pool.size()).unwrap_or(NonZeroUsize::MIN);
Exec::Rayon {
pool,
budget: max.min(cap),
policy: *self.batch_policy(),
}
}
})
}
/// The batch lane policy this context applies.
///
/// A serial context has one thread but still carries a policy, so a forced
/// strategy is resolved (and can be refused) at one thread as well.
///
/// # Examples
///
/// ```
/// use tcontract_exec::{BatchPolicy, BatchStrategy, Exec};
/// assert_eq!(Exec::serial().batch_policy().strategy, BatchStrategy::Auto);
/// let tp = rayon::ThreadPoolBuilder::new().num_threads(2).build().unwrap();
/// let pool = tcontract_exec::Pool::borrow(&tp);
/// let exec = Exec::rayon(&pool).with_batch_policy(BatchPolicy::new(BatchStrategy::Outer));
/// assert_eq!(exec.batch_policy().strategy, BatchStrategy::Outer);
/// ```
pub fn batch_policy(&self) -> &BatchPolicy {
match self {
Exec::Serial { policy } | Exec::SerialWithWorkspace { policy, .. } => policy,
Exec::Rayon { policy, .. } => policy,
}
}
/// Return this context with a different batch lane policy.
///
/// A serial context has one thread, so the policy cannot change its lane
/// count, but it is preserved: a forced strategy is still honoured (or
/// refused with a typed error) at one thread instead of silently running
/// `Auto`.
///
/// # Examples
///
/// ```
/// use tcontract_exec::{BatchPolicy, BatchStrategy, Exec};
/// let exec = Exec::serial().with_batch_policy(BatchPolicy::new(BatchStrategy::Outer));
/// assert_eq!(exec.batch_policy().strategy, BatchStrategy::Outer);
/// ```
#[must_use]
pub fn with_batch_policy(self, policy: BatchPolicy) -> Self {
match self {
Exec::Serial { .. } => Exec::Serial { policy },
Exec::SerialWithWorkspace { workspace, .. } => {
Exec::SerialWithWorkspace { workspace, policy }
}
Exec::Rayon { pool, budget, .. } => Exec::Rayon {
pool,
budget,
policy,
},
}
}
/// Decide the batch route for `query` on this context's budget and policy.
///
/// This is the one decision function every operation that owns batch lanes
/// calls; see [`crate::batch`]. Whether a *route* can honour the returned
/// decision is the route's own preflight (see
/// [`crate::batch::BatchDecision`]'s route contract); the decision itself
/// is arithmetic and never fails.
///
/// # Examples
///
/// ```
/// use tcontract_exec::{BatchQuery, Exec};
/// let d = Exec::serial().batch_decision(&BatchQuery::new(4, 1000.0));
/// assert_eq!((d.lanes, d.item_threads), (1, 1));
/// ```
pub fn batch_decision(&self, query: &BatchQuery) -> BatchDecision {
// INVARIANT: `Exec` budgets are `NonZeroUsize` by construction.
let budget = NonZeroUsize::new(self.budget()).unwrap_or(NonZeroUsize::MIN);
crate::batch::batch_decision(self.batch_policy(), budget, query)
}
/// Most threads an operation may occupy (1 for a serial context).
pub fn budget(&self) -> usize {
match self {
Exec::Serial { .. } | Exec::SerialWithWorkspace { .. } => 1,
Exec::Rayon { budget, .. } => budget.get(),
}
}
/// Storage the operations on this context may share, or none.
///
/// A pool lends its own arena to every operation that runs on it; a serial
/// context owns nothing, so a caller that wants reuse keeps a provider of
/// its own and lends it through [`Exec::serial_with_workspace`].
pub fn workspace(&self) -> Option<&'a dyn crate::WorkspaceProvider> {
match self {
Exec::Serial { .. } => None,
Exec::SerialWithWorkspace { workspace, .. } => Some(*workspace),
Exec::Rayon { pool, .. } => Some(pool.workspace()),
}
}
/// Whether the calling thread is a worker of this context's pool.
pub fn is_worker(&self) -> bool {
match self {
Exec::Serial { .. } | Exec::SerialWithWorkspace { .. } => false,
Exec::Rayon { pool, .. } => pool.is_worker(),
}
}
fn width(&self, k: usize) -> usize {
k.clamp(1, self.budget())
}
/// Run `op` with `min(k, budget)` threads of parallelism.
///
/// Width one runs `op(Par::Seq)` inline on the caller. Otherwise `op`
/// runs inside the pool, which is entered once, or in place when the
/// caller is already one of its workers.
pub fn install<R: Send>(&self, k: usize, op: impl FnOnce(Par) -> R + Send) -> R {
let k = self.width(k);
match (self, NonZeroUsize::new(k)) {
(Exec::Rayon { pool, .. }, Some(n)) if k > 1 => {
if pool.is_worker() {
pool.count_inline();
op(Par::Threads(n))
} else {
pool.count_entry();
pool.tp().install(|| op(Par::Threads(n)))
}
}
_ => op(Par::Seq),
}
}
/// Barrier-free partition: runs `f(i)` exactly once for each `i < k`,
/// on at most `budget` workers in contiguous lanes.
///
/// Tasks are not guaranteed to run concurrently, so `f` must never wait
/// on another index.
///
/// # Examples
///
/// ```
/// use std::sync::atomic::{AtomicUsize, Ordering};
/// let n = AtomicUsize::new(0);
/// tcontract_exec::Exec::serial().for_each_partition(5, &|_| {
/// n.fetch_add(1, Ordering::Relaxed);
/// });
/// assert_eq!(n.into_inner(), 5);
/// ```
pub fn for_each_partition(&self, k: usize, f: &(dyn Fn(usize) + Sync)) {
if k == 0 {
return;
}
let lanes = self.width(k);
if lanes == 1 {
(0..k).for_each(f);
return;
}
self.install(lanes, |_| {
rayon::scope(|s| {
for lane in 0..lanes {
s.spawn(move |_| {
let lo = lane * k / lanes;
let hi = (lane + 1) * k / lanes;
(lo..hi).for_each(f);
});
}
})
});
}
/// Co-scheduled SPMD execution: `f(t)` for every `t < width` runs
/// concurrently on distinct workers, so `f` may use barriers counting
/// `width` participants.
///
/// Width one runs `f(0)` inline. On a pool the whole pool is dispatched
/// (Rayon `broadcast`); workers with index `>= width` return at once.
/// Broadcasts through one [`Pool`] wrapper are serialized (use one
/// wrapper per `ThreadPool`). A broadcast waits until every worker reaches
/// a scheduling point, so a worker busy in another host's long job delays
/// it. A panic in a barrier-free `f` propagates to the caller after every
/// participant has finished; if `f` panics before a barrier the other
/// participants wait at it forever, so barrier-bearing `f` must not panic.
///
/// # Errors
///
/// [`ExecError::Unavailable`] when co-scheduling cannot be guaranteed
/// (`Serial` with `width > 1`, or the caller is already a worker of the
/// pool, where an outer job may hold workers at a barrier); nothing runs
/// then, and the caller uses its barrier-free variant.
/// [`ExecError::WidthExceedsPool`] when `width` exceeds the pool, and
/// [`ExecError::WidthExceedsBudget`] when it exceeds the budget (the
/// active width is capped by the budget even though the whole pool is
/// dispatched).
///
/// # Examples
///
/// ```
/// use tcontract_exec::{Exec, ExecError};
/// assert_eq!(Exec::serial().broadcast(2, &|_| {}), Err(ExecError::Unavailable));
/// ```
pub fn broadcast(&self, width: usize, f: &(dyn Fn(usize) + Sync)) -> Result<(), ExecError> {
if width == 0 {
return Ok(());
}
match self {
Exec::Serial { .. } | Exec::SerialWithWorkspace { .. } => {
if width == 1 {
f(0);
Ok(())
} else {
Err(ExecError::Unavailable)
}
}
Exec::Rayon { pool, budget, .. } => {
let size = pool.size();
if width > size {
return Err(ExecError::WidthExceedsPool { width, pool: size });
}
if width > budget.get() {
return Err(ExecError::WidthExceedsBudget {
width,
budget: budget.get(),
});
}
if pool.is_worker() {
return Err(ExecError::Unavailable);
}
if width == 1 {
f(0);
return Ok(());
}
// INVARIANT: the SPMD mutex serializes broadcasts on this pool, so
// no worker ever holds two barrier-bearing jobs; it guards no data,
// so a poisoned lock (panic in an earlier `f`) is safe to reuse.
let _guard = pool
.spmd
.lock()
.unwrap_or_else(std::sync::PoisonError::into_inner);
pool.count_broadcast();
pool.tp().broadcast(|ctx| {
let t = ctx.index();
if t < width {
f(t)
}
});
Ok(())
}
}
}
/// Partition width for work estimated at `serial_ns` on one thread,
/// minimizing the [`WidthPolicy`](crate::WidthPolicy) cost model within the budget.
///
/// # Examples
///
/// ```
/// use tcontract_exec::{Exec, WidthPolicy};
/// assert_eq!(Exec::serial().width_for(1e9, &WidthPolicy::default()), 1);
/// ```
pub fn width_for(&self, serial_ns: f64, policy: &crate::WidthPolicy) -> usize {
crate::width::width_for(serial_ns, policy, self.budget())
}
}