use crate::core::errors::OCRError;
#[derive(Debug, Clone, Default)]
pub struct OrtGlobalThreadPoolOptions {
intra_threads: Option<usize>,
inter_threads: Option<usize>,
allow_spinning: Option<bool>,
}
impl OrtGlobalThreadPoolOptions {
pub fn new() -> Self {
Self::default()
}
pub fn with_intra_threads(mut self, threads: usize) -> Self {
self.intra_threads = Some(threads);
self
}
pub fn with_inter_threads(mut self, threads: usize) -> Self {
self.inter_threads = Some(threads);
self
}
pub fn with_spin_control(mut self, allow: bool) -> Self {
self.allow_spinning = Some(allow);
self
}
pub fn commit(self) -> Result<bool, OCRError> {
let mut options = ort::environment::GlobalThreadPoolOptions::default();
if let Some(threads) = self.intra_threads {
options = options.with_intra_threads(threads).map_err(config_error)?;
}
if let Some(threads) = self.inter_threads {
options = options.with_inter_threads(threads).map_err(config_error)?;
}
if let Some(allow) = self.allow_spinning {
options = options.with_spin_control(allow).map_err(config_error)?;
}
Ok(ort::init().with_global_thread_pool(options).commit())
}
}
fn config_error(error: ort::Error) -> OCRError {
OCRError::ConfigError {
message: format!("Failed to configure ONNX Runtime global thread pool: {error}"),
}
}