pub mod scheduling;
pub mod threshold;
pub mod workload;
pub use scheduling::{optimize_scheduling, work_stealing_scheduler, SchedulingStrategy};
pub use threshold::{
adaptive_threshold, get_optimal_threshold, set_global_threshold, ParallelizationThreshold,
};
pub use workload::{partition_workload, WorkloadPartitioning};
pub fn optimize_parallel_computation(
array_size: usize,
element_cost: f64,
scheduling: SchedulingStrategy,
) -> usize {
let threshold = adaptive_threshold(array_size, element_cost);
if array_size <= threshold {
return 1;
}
let num_threads = scirs2_core::parallel_ops::num_threads();
optimize_scheduling(array_size, element_cost, scheduling, num_threads)
}
#[repr(align(64))]
#[derive(Debug, Clone, Copy)]
pub struct ParallelConfig {
pub use_parallel: bool,
pub min_parallel_size: usize,
pub chunk_size: usize,
pub max_threads: Option<usize>,
pub scheduling_strategy: SchedulingStrategy,
}
impl Default for ParallelConfig {
fn default() -> Self {
Self {
use_parallel: true,
min_parallel_size: 1000, chunk_size: 250, max_threads: None, scheduling_strategy: SchedulingStrategy::Adaptive,
}
}
}
impl ParallelConfig {
pub fn new() -> Self {
Self::default()
}
pub fn optimized(array_size: usize, element_cost: f64) -> Self {
let threshold = adaptive_threshold(array_size, element_cost);
let chunk_size = (threshold / 4).max(100);
Self {
use_parallel: array_size >= threshold,
min_parallel_size: threshold,
chunk_size,
max_threads: None,
scheduling_strategy: SchedulingStrategy::Adaptive,
}
}
pub fn with_parallel(mut self, use_parallel: bool) -> Self {
self.use_parallel = use_parallel;
self
}
pub fn with_min_size(mut self, min_size: usize) -> Self {
self.min_parallel_size = min_size;
self
}
pub fn with_chunk_size(mut self, chunk_size: usize) -> Self {
self.chunk_size = chunk_size;
self
}
pub fn with_max_threads(mut self, max_threads: usize) -> Self {
self.max_threads = Some(max_threads);
self
}
pub fn with_scheduling(mut self, strategy: SchedulingStrategy) -> Self {
self.scheduling_strategy = strategy;
self
}
pub fn should_parallelize(&self, array_size: usize) -> bool {
self.use_parallel && array_size >= self.min_parallel_size
}
pub fn optimal_threads(&self, array_size: usize, element_cost: f64) -> usize {
if !self.should_parallelize(array_size) {
return 1;
}
let available_threads = scirs2_core::parallel_ops::num_threads();
let mut optimal = optimize_scheduling(
array_size,
element_cost,
self.scheduling_strategy,
available_threads,
);
if let Some(max) = self.max_threads {
optimal = optimal.min(max);
}
optimal
}
}