#![allow(clippy::missing_errors_doc)]
use crate::backend::{Backend, BackendRef};
use crate::dispatch_profile::DispatchProfile;
use crate::foundation::Variant;
use crate::sched::JobPlan;
use crate::sched::adaptive_backend::{
active_backend_id, migrate_backend, resolve_active_backend,
};
use crate::sched::adaptive_profile::{
WorkloadClass, active_dispatch_profile, migrate_dispatch_profile,
migrate_workload_class,
};
use crate::sched::k_gating::KGating;
use crate::sched::workload_shape::WorkloadShape;
pub struct AdaptiveDispatcher {
shape: WorkloadShape,
variant: Variant,
k_outer: u8,
use_smt: bool,
explicit_class: Option<WorkloadClass>,
}
impl Default for AdaptiveDispatcher {
fn default() -> Self {
Self::new()
}
}
impl AdaptiveDispatcher {
pub fn new() -> Self {
Self {
shape: WorkloadShape::WorkSteal {
n_consumers: num_cpus(),
batch_size: 256,
},
variant: Variant::Faithful,
k_outer: 4,
use_smt: false,
explicit_class: None,
}
}
#[inline]
pub fn with_shape(mut self, shape: WorkloadShape) -> Self {
self.shape = shape;
self
}
#[inline]
pub fn with_variant(mut self, variant: Variant) -> Self {
self.variant = variant;
self
}
#[inline]
pub fn with_k_outer(mut self, k_outer: u8) -> Self {
self.k_outer = k_outer;
self
}
#[inline]
pub fn with_smt(mut self) -> Self {
self.use_smt = true;
self
}
fn build_plan(&self, batch_size: u32) -> JobPlan {
let profile = match self.explicit_class {
Some(c) => c.to_dispatch_profile(),
None => active_dispatch_profile(),
};
let mut plan = JobPlan::set_profile_with(
self.k_outer,
batch_size,
profile,
self.explicit_class.is_some(),
)
.with_variant(self.variant)
.with_workload_shape(self.shape);
if self.use_smt {
plan = plan.with_smt();
}
plan
}
pub fn execute_cooperative<R>(
self,
closures: Vec<Box<dyn FnOnce() -> R + Send>>,
) -> Vec<R>
where
R: Send + 'static,
{
let plan = self.build_plan(closures.len() as u32);
crate::sched::cooperative_join_n_flat(&plan, closures)
}
pub fn execute_cooperative_mailbox<R>(
self,
closures: Vec<Box<dyn FnOnce() -> R + Send>>,
) -> Vec<R>
where
R: Send + 'static,
{
let plan = self
.build_plan(closures.len() as u32)
.with_mailbox_routing(true);
crate::sched::cooperative::cooperative_join_n_flat_mailbox(&plan, closures)
}
#[track_caller]
pub fn execute_for_each<T, F>(self, items: &mut [T], op: F)
where
T: Send,
F: Fn(&mut [T]) + Sync + Send,
{
let plan = self.build_plan(items.len() as u32);
crate::sched::par_iter::for_each_chunk(&plan, items, op);
}
pub fn execute_streaming<R, F>(self, op: F) -> R
where
F: FnOnce() -> R + Send,
R: Send,
{
op()
}
pub fn execute_indexed<F>(self, count: u32, work: F) -> bool
where
F: Fn(u32) + Send + Sync,
{
let (backend, fell_back) = resolve_active_backend();
backend.dispatch_parallel_for(count, &work);
fell_back
}
pub fn migrate_k_gating(&self, gating: KGating) {
let arena = crate::sched::arena::global_local_arena();
arena.migrate_all_workers_k_gating(gating);
}
#[inline]
pub fn migrate_dispatch_profile(&self, profile: DispatchProfile) {
migrate_dispatch_profile(profile);
}
#[inline]
pub fn migrate_workload_class(&self, class: WorkloadClass) {
migrate_workload_class(class);
}
#[inline]
pub fn active_dispatch_profile(&self) -> DispatchProfile {
active_dispatch_profile()
}
#[inline]
pub fn migrate_backend(&self, backend: Backend) {
migrate_backend(backend);
}
#[inline]
pub fn active_backend_id(&self) -> Backend {
active_backend_id()
}
#[inline]
pub fn resolve_active_backend(&self) -> (BackendRef, bool) {
resolve_active_backend()
}
#[inline]
pub fn with_workload_class(mut self, class: WorkloadClass) -> Self {
self.explicit_class = Some(class);
self
}
}
fn num_cpus() -> u32 {
std::thread::available_parallelism()
.map(|n| n.get() as u32)
.unwrap_or(4)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn default_dispatcher_constructs() {
let d = AdaptiveDispatcher::new();
assert_eq!(d.k_outer, 4);
assert!(!d.use_smt);
assert_eq!(d.variant, Variant::Faithful);
}
#[test]
fn builder_chains() {
let d = AdaptiveDispatcher::new()
.with_shape(WorkloadShape::ProducerFast { burst: 32 })
.with_variant(Variant::Fast)
.with_k_outer(8)
.with_smt();
assert_eq!(d.k_outer, 8);
assert!(d.use_smt);
assert_eq!(d.variant, Variant::Fast);
assert!(matches!(d.shape, WorkloadShape::ProducerFast { burst: 32 }));
}
#[test]
fn execute_streaming_runs_inline() {
let d = AdaptiveDispatcher::new()
.with_shape(WorkloadShape::Streaming);
let r = d.execute_streaming(|| 42u32);
assert_eq!(r, 42);
}
#[test]
fn execute_cooperative_round_trips_caller_order() {
let d = AdaptiveDispatcher::new()
.with_shape(WorkloadShape::ProducerFast { burst: 8 });
let closures: Vec<Box<dyn FnOnce() -> u32 + Send>> =
(0..8u32).map(|i| Box::new(move || i) as _).collect();
let results = d.execute_cooperative(closures);
assert_eq!(results, (0..8u32).collect::<Vec<_>>());
}
#[test]
fn execute_for_each_in_place_updates_items() {
let d = AdaptiveDispatcher::new()
.with_shape(WorkloadShape::WorkSteal { n_consumers: 4, batch_size: 64 });
let mut items: Vec<u32> = (0..256).collect();
d.execute_for_each(&mut items, |slice| {
for x in slice.iter_mut() {
*x = x.wrapping_mul(2);
}
});
for (i, x) in items.iter().enumerate() {
assert_eq!(*x, (i as u32).wrapping_mul(2));
}
}
}