laddu-core 0.18.0

Core of the laddu library
Documentation
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
398
399
400
401
402
403
404
//! Shared thread-pool manager for APIs that accept a per-call thread count.

#[cfg(feature = "rayon")]
use std::sync::Arc;
use std::sync::{
    atomic::{AtomicUsize, Ordering},
    OnceLock,
};

#[cfg(feature = "rayon")]
use parking_lot::RwLock;

#[cfg(feature = "rayon")]
use crate::LadduError;
use crate::LadduResult;

static GLOBAL_THREAD_COUNT: AtomicUsize = AtomicUsize::new(0);

/// Shared thread-execution mode used by both [`ThreadPoolManager`] and
/// [`ExecutionContext`](crate::execution_context::ExecutionContext).
#[derive(Debug, Clone, Default)]
pub(crate) enum ThreadExecutor {
    /// Run work on the caller thread / ambient global Rayon context.
    #[default]
    Ambient,
    /// Run work on a dedicated Rayon pool.
    #[cfg(feature = "rayon")]
    Dedicated(Arc<rayon::ThreadPool>),
}

impl ThreadExecutor {
    /// Create a dedicated executor with `n_threads`.
    #[cfg(feature = "rayon")]
    pub(crate) fn dedicated(n_threads: usize) -> LadduResult<Self> {
        if n_threads == 0 {
            return Err(LadduError::ExecutionContextError {
                reason: "Dedicated thread pool size must be >= 1".into(),
            });
        }

        Ok(Self::Dedicated(Arc::new(
            rayon::ThreadPoolBuilder::new()
                .num_threads(n_threads)
                .build()?,
        )))
    }

    /// Execute work using this executor.
    #[cfg(feature = "rayon")]
    pub(crate) fn install<R: Send>(&self, op: impl FnOnce() -> R + Send) -> R {
        match self {
            Self::Ambient => op(),
            Self::Dedicated(pool) => pool.install(op),
        }
    }

    /// Execute work using this executor.
    #[allow(dead_code)]
    #[cfg(not(feature = "rayon"))]
    pub(crate) fn install<R>(&self, op: impl FnOnce() -> R) -> R {
        op()
    }
}

/// Shared manager for per-call Rayon thread-pool reuse.
///
/// This manager is intended for APIs that accept an optional thread count on each call.
/// Requests with `None` or `Some(0)` use the configured global default. When that default is `0`,
/// work falls back to the ambient/global Rayon behavior. Positive thread counts reuse one cached
/// dedicated pool for the most recently requested size.
#[derive(Debug, Default)]
pub struct ThreadPoolManager {
    #[cfg(feature = "rayon")]
    dedicated_pool: RwLock<Option<(usize, ThreadExecutor)>>,
}

impl ThreadPoolManager {
    /// Return the process-wide shared pool manager.
    pub fn shared() -> &'static Self {
        static THREAD_POOL_MANAGER: OnceLock<ThreadPoolManager> = OnceLock::new();
        THREAD_POOL_MANAGER.get_or_init(Self::default)
    }

    /// Set the process-global default thread count used by omitted or zero-valued requests.
    ///
    /// A value of `0` resets the default to the ambient/global Rayon behavior.
    pub fn set_global_thread_count(n_threads: usize) {
        GLOBAL_THREAD_COUNT.store(n_threads, Ordering::Relaxed);
    }

    /// Return the process-global default thread count used by omitted or zero-valued requests.
    ///
    /// Returns `None` when the default is the ambient/global Rayon behavior.
    pub fn global_thread_count() -> Option<usize> {
        Self::normalize_thread_request(Some(GLOBAL_THREAD_COUNT.load(Ordering::Relaxed)))
    }

    /// Resolve an optional thread request against the process-global default.
    ///
    /// `None` and `Some(0)` both use the configured global default. Positive thread counts bypass
    /// the global default and are returned directly.
    pub fn resolve_thread_request(requested_threads: Option<usize>) -> Option<usize> {
        match requested_threads {
            None | Some(0) => Self::global_thread_count(),
            Some(n_threads) => Some(n_threads),
        }
    }

    /// Execute work using the requested thread-count policy.
    ///
    /// `None` and `Some(0)` both use the configured global default. Positive thread counts reuse a
    /// cached dedicated pool of that size.
    #[cfg(feature = "rayon")]
    pub fn install<R: Send>(
        &self,
        requested_threads: Option<usize>,
        op: impl FnOnce() -> R + Send,
    ) -> LadduResult<R> {
        match Self::resolve_thread_request(requested_threads) {
            Some(n_threads) => Ok(self.executor_for_threads(n_threads)?.install(op)),
            None => Ok(ThreadExecutor::default().install(op)),
        }
    }

    /// Execute work using the requested thread-count policy.
    ///
    /// Without Rayon, all work runs on the caller thread and the requested thread count is
    /// ignored.
    #[cfg(not(feature = "rayon"))]
    pub fn install<R>(
        &self,
        _requested_threads: Option<usize>,
        op: impl FnOnce() -> R,
    ) -> LadduResult<R> {
        Ok(op())
    }

    fn normalize_thread_request(requested_threads: Option<usize>) -> Option<usize> {
        requested_threads.filter(|&n_threads| n_threads > 0)
    }

    #[cfg(feature = "rayon")]
    fn executor_for_threads(&self, n_threads: usize) -> LadduResult<ThreadExecutor> {
        if let Some((cached_threads, executor)) = &*self.dedicated_pool.read() {
            if *cached_threads == n_threads {
                return Ok(executor.clone());
            }
        }

        let executor = ThreadExecutor::dedicated(n_threads)?;
        let mut dedicated_pool = self.dedicated_pool.write();
        *dedicated_pool = Some((n_threads, executor.clone()));
        Ok(executor)
    }
}

#[cfg(test)]
mod tests {
    #[cfg(feature = "rayon")]
    use super::ThreadExecutor;
    use super::ThreadPoolManager;

    use std::sync::{Mutex, MutexGuard};

    #[cfg(feature = "rayon")]
    use std::sync::Arc;

    static GLOBAL_THREAD_COUNT_TEST_GUARD: Mutex<()> = Mutex::new(());

    struct GlobalThreadCountReset {
        previous: Option<usize>,
        _guard: MutexGuard<'static, ()>,
    }

    impl GlobalThreadCountReset {
        fn new(n_threads: usize) -> Self {
            let guard = GLOBAL_THREAD_COUNT_TEST_GUARD
                .lock()
                .expect("global thread-count test guard should not be poisoned");
            let previous = ThreadPoolManager::global_thread_count();
            ThreadPoolManager::set_global_thread_count(n_threads);
            Self {
                previous,
                _guard: guard,
            }
        }
    }

    impl Drop for GlobalThreadCountReset {
        fn drop(&mut self) {
            ThreadPoolManager::set_global_thread_count(self.previous.unwrap_or(0));
        }
    }

    #[cfg(feature = "rayon")]
    #[test]
    fn thread_pool_manager_reuses_cached_pool_for_same_thread_count() {
        let manager = ThreadPoolManager::default();
        let first_pool = match manager
            .executor_for_threads(2)
            .expect("pool for two threads should build")
        {
            ThreadExecutor::Dedicated(pool) => pool,
            ThreadExecutor::Ambient => panic!("executor should be dedicated"),
        };
        let second_pool = match manager
            .executor_for_threads(2)
            .expect("pool for two threads should be cached")
        {
            ThreadExecutor::Dedicated(pool) => pool,
            ThreadExecutor::Ambient => panic!("executor should be dedicated"),
        };
        assert!(Arc::ptr_eq(&first_pool, &second_pool));
    }

    #[cfg(feature = "rayon")]
    #[test]
    fn thread_pool_manager_separates_distinct_thread_counts() {
        let manager = ThreadPoolManager::default();
        let two_thread_pool = match manager
            .executor_for_threads(2)
            .expect("pool for two threads should build")
        {
            ThreadExecutor::Dedicated(pool) => pool,
            ThreadExecutor::Ambient => panic!("executor should be dedicated"),
        };
        let three_thread_pool = match manager
            .executor_for_threads(3)
            .expect("pool for three threads should build")
        {
            ThreadExecutor::Dedicated(pool) => pool,
            ThreadExecutor::Ambient => panic!("executor should be dedicated"),
        };
        assert!(!Arc::ptr_eq(&two_thread_pool, &three_thread_pool));
    }

    #[cfg(feature = "rayon")]
    #[test]
    fn thread_pool_manager_replaces_cached_pool_when_thread_count_changes() {
        let manager = ThreadPoolManager::default();
        let first_two_thread_pool = match manager
            .executor_for_threads(2)
            .expect("pool for two threads should build")
        {
            ThreadExecutor::Dedicated(pool) => pool,
            ThreadExecutor::Ambient => panic!("executor should be dedicated"),
        };
        manager
            .executor_for_threads(3)
            .expect("pool for three threads should replace the cache");
        let second_two_thread_pool = match manager
            .executor_for_threads(2)
            .expect("pool for two threads should rebuild after cache replacement")
        {
            ThreadExecutor::Dedicated(pool) => pool,
            ThreadExecutor::Ambient => panic!("executor should be dedicated"),
        };
        assert!(!Arc::ptr_eq(
            &first_two_thread_pool,
            &second_two_thread_pool
        ));
    }

    #[test]
    fn thread_pool_manager_treats_zero_threads_as_global_fallback() {
        let _reset = GlobalThreadCountReset::new(0);
        let manager = ThreadPoolManager::default();
        let value = manager
            .install(Some(0), || 17usize)
            .expect("global fallback install should succeed");
        assert_eq!(value, 17);
        #[cfg(feature = "rayon")]
        assert!(manager.dedicated_pool.read().is_none());
    }

    #[cfg(feature = "rayon")]
    #[test]
    fn thread_pool_manager_reuses_cached_pool_across_many_short_installs() {
        let manager = ThreadPoolManager::default();
        let first_pool = match manager
            .executor_for_threads(2)
            .expect("pool for two threads should build")
        {
            ThreadExecutor::Dedicated(pool) => pool,
            ThreadExecutor::Ambient => panic!("executor should be dedicated"),
        };

        let total = (0usize..128)
            .map(|index| {
                let value = manager
                    .install(Some(2), || index + 1)
                    .expect("repeated short install should succeed");
                let cached_pool = match manager
                    .executor_for_threads(2)
                    .expect("pool for two threads should remain cached")
                {
                    ThreadExecutor::Dedicated(pool) => pool,
                    ThreadExecutor::Ambient => panic!("executor should be dedicated"),
                };
                assert!(Arc::ptr_eq(&first_pool, &cached_pool));
                value
            })
            .sum::<usize>();

        assert_eq!(total, (1usize..=128).sum::<usize>());
    }

    #[cfg(feature = "rayon")]
    #[test]
    fn thread_pool_manager_uses_global_default_for_omitted_and_zero_requests() {
        let _reset = GlobalThreadCountReset::new(2);
        let manager = ThreadPoolManager::default();

        manager
            .install(None, || 17usize)
            .expect("omitted thread request should succeed");
        let omitted_pool = match manager
            .dedicated_pool
            .read()
            .as_ref()
            .expect("global default should create a dedicated pool")
            .1
            .clone()
        {
            ThreadExecutor::Dedicated(pool) => pool,
            ThreadExecutor::Ambient => panic!("executor should be dedicated"),
        };
        assert_eq!(
            manager
                .dedicated_pool
                .read()
                .as_ref()
                .map(|(n_threads, _)| *n_threads),
            Some(2)
        );

        manager
            .install(Some(0), || 23usize)
            .expect("zero thread request should use the global default");
        let zero_pool = match manager
            .dedicated_pool
            .read()
            .as_ref()
            .expect("global default should remain cached")
            .1
            .clone()
        {
            ThreadExecutor::Dedicated(pool) => pool,
            ThreadExecutor::Ambient => panic!("executor should be dedicated"),
        };
        assert!(Arc::ptr_eq(&omitted_pool, &zero_pool));
    }

    #[cfg(feature = "rayon")]
    #[test]
    fn explicit_thread_request_overrides_global_default() {
        let _reset = GlobalThreadCountReset::new(2);
        let manager = ThreadPoolManager::default();

        manager
            .install(Some(3), || 19usize)
            .expect("explicit thread request should succeed");
        assert_eq!(
            manager
                .dedicated_pool
                .read()
                .as_ref()
                .map(|(n_threads, _)| *n_threads),
            Some(3)
        );

        manager
            .install(None, || 29usize)
            .expect("omitted thread request should fall back to the global default");
        assert_eq!(
            manager
                .dedicated_pool
                .read()
                .as_ref()
                .map(|(n_threads, _)| *n_threads),
            Some(2)
        );
    }

    #[test]
    fn resolve_thread_request_prioritizes_explicit_positive_values() {
        let _reset = GlobalThreadCountReset::new(2);

        assert_eq!(ThreadPoolManager::resolve_thread_request(None), Some(2));
        assert_eq!(ThreadPoolManager::resolve_thread_request(Some(0)), Some(2));
        assert_eq!(ThreadPoolManager::resolve_thread_request(Some(3)), Some(3));
    }

    #[test]
    fn set_global_thread_count_zero_resets_to_ambient_behavior() {
        let _reset = GlobalThreadCountReset::new(3);
        assert_eq!(ThreadPoolManager::global_thread_count(), Some(3));

        ThreadPoolManager::set_global_thread_count(0);
        assert_eq!(ThreadPoolManager::global_thread_count(), None);
        assert_eq!(ThreadPoolManager::resolve_thread_request(None), None);
        assert_eq!(ThreadPoolManager::resolve_thread_request(Some(0)), None);
    }
}