use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ParallelPolicy {
#[serde(default)]
pub max_threads: Option<usize>,
#[serde(default = "ParallelPolicy::default_utility_threshold")]
pub utility_threshold: usize,
#[serde(default = "ParallelPolicy::default_postprocess_pixel_threshold")]
pub postprocess_pixel_threshold: usize,
}
impl ParallelPolicy {
pub fn new() -> Self {
Self::default()
}
pub fn with_max_threads(mut self, max_threads: Option<usize>) -> Self {
self.max_threads = max_threads;
self
}
pub fn with_postprocess_pixel_threshold(mut self, threshold: usize) -> Self {
self.postprocess_pixel_threshold = threshold;
self
}
pub fn with_utility_threshold(mut self, threshold: usize) -> Self {
self.utility_threshold = threshold;
self
}
pub fn install_global_thread_pool(&self) -> Result<bool, rayon::ThreadPoolBuildError> {
if let Some(num_threads) = self.max_threads {
rayon::ThreadPoolBuilder::new()
.num_threads(num_threads)
.build_global()?;
Ok(true)
} else {
Ok(false)
}
}
fn default_utility_threshold() -> usize {
4 }
fn default_postprocess_pixel_threshold() -> usize {
8_000
}
}
impl Default for ParallelPolicy {
fn default() -> Self {
Self {
max_threads: None,
utility_threshold: Self::default_utility_threshold(),
postprocess_pixel_threshold: Self::default_postprocess_pixel_threshold(),
}
}
}