use scirs2_core::parallel_ops::{
current_num_threads, IndexedParallelIterator, ParallelIterator, ThreadPool, ThreadPoolBuilder,
};
use scirs2_core::random::prelude::*;
use std::sync::{Arc, OnceLock};
use super::types::SciRS2ParallelContext;
static SHARED_THREAD_POOL: OnceLock<Arc<ThreadPool>> = OnceLock::new();
pub fn shared_thread_pool() -> Arc<ThreadPool> {
Arc::clone(SHARED_THREAD_POOL.get_or_init(|| {
let pool = ThreadPoolBuilder::new()
.num_threads(current_num_threads())
.build()
.unwrap_or_else(|_| {
ThreadPoolBuilder::new()
.build()
.expect("fallback thread pool creation should succeed")
});
Arc::new(pool)
}))
}
impl Default for SciRS2ParallelContext {
fn default() -> Self {
let thread_pool = shared_thread_pool();
Self {
num_threads: thread_pool.current_num_threads(),
thread_pool,
numa_aware: true,
}
}
}