Skip to main content

laddu_extensions/
optimize.rs

1//! Optimization helpers and algorithm integrations for likelihood terms.
2
3/// Callback and observer integration used by optimization algorithms.
4pub mod callbacks;
5/// Bindings and adapters for optimization algorithms from `ganesh`.
6pub mod ganesh;
7
8pub use callbacks::LikelihoodTermObserver;
9use laddu_core::{LadduResult, ThreadPoolManager};
10
11/// A wrapper for the requested thread-count policy used by optimization callbacks.
12#[derive(Clone, Copy, Debug)]
13pub struct MaybeThreadPool {
14    requested_threads: Option<usize>,
15}
16
17impl MaybeThreadPool {
18    /// Crate a new thread pool with the given number of threads. This is typically used as
19    /// user-data for [`ganesh`] optimizations.
20    pub fn new(num_threads: usize) -> Self {
21        Self {
22            requested_threads: Some(num_threads),
23        }
24    }
25
26    /// Run the given operation on the current thread pool.
27    pub fn install<R: Send>(&self, op: impl FnOnce() -> LadduResult<R> + Send) -> LadduResult<R> {
28        ThreadPoolManager::shared().install(self.requested_threads, op)?
29    }
30}