use std::collections::HashMap;
use std::sync::{Arc, OnceLock};
static GLOBAL_CONFIG: OnceLock<DispatchConfig> = OnceLock::new();
#[derive(Debug, Clone)]
pub struct DispatchConfig {
thresholds: HashMap<Arc<str>, usize>,
gpu_available: bool,
force_cpu: bool,
force_gpu: bool,
}
impl Default for DispatchConfig {
fn default() -> Self {
Self {
thresholds: default_thresholds(),
gpu_available: check_gpu_available(),
force_cpu: false,
force_gpu: false,
}
}
}
impl DispatchConfig {
#[must_use]
pub fn new() -> Self {
Self::default()
}
#[must_use]
pub fn with_thresholds(thresholds: HashMap<Arc<str>, usize>) -> Self {
Self {
thresholds,
..Self::default()
}
}
#[must_use]
pub fn force_cpu(mut self) -> Self {
self.force_cpu = true;
self.force_gpu = false;
self
}
#[must_use]
pub fn force_gpu(mut self) -> Self {
self.force_gpu = true;
self.force_cpu = false;
self
}
pub fn set_threshold(&mut self, operation: impl Into<Arc<str>>, threshold: usize) {
self.thresholds.insert(operation.into(), threshold);
}
#[must_use]
pub fn threshold(&self, operation: &str) -> usize {
self.thresholds
.get(operation)
.copied()
.unwrap_or(DEFAULT_THRESHOLD)
}
#[must_use]
pub fn should_use_gpu(&self, input_size: usize, operation: &str) -> bool {
if self.force_cpu {
return false;
}
if self.force_gpu && self.gpu_available {
return true;
}
if !self.gpu_available {
return false;
}
let threshold = self.threshold(operation);
input_size >= threshold
}
#[must_use]
pub fn has_gpu(&self) -> bool {
self.gpu_available
}
}
pub const DEFAULT_THRESHOLD: usize = 1024;
const HMM_FORWARD_THRESHOLD: usize = 5000;
fn default_thresholds() -> HashMap<Arc<str>, usize> {
let mut m = HashMap::new();
m.insert(Arc::from("erf"), 512); m.insert(Arc::from("erfc"), 512); m.insert(Arc::from("gamma"), 256); m.insert(Arc::from("lgamma"), 256); m.insert(Arc::from("digamma"), 256); m.insert(Arc::from("bessel_j0"), 512); m.insert(Arc::from("bessel_j1"), 512); m.insert(Arc::from("bessel_i0"), 512); m.insert(Arc::from("bessel_k0"), 512);
m.insert(Arc::from("matmul"), 64); m.insert(Arc::from("frobenius_norm"), 4096);
m.insert(Arc::from("transpose"), 4096);
m.insert(Arc::from("cholesky"), 128); m.insert(Arc::from("eigh"), 128); m.insert(Arc::from("lu"), 128); m.insert(Arc::from("qr"), 128); m.insert(Arc::from("svd"), 128); m.insert(Arc::from("solve"), 128); m.insert(Arc::from("tridiagonal"), 256);
m.insert(Arc::from("cdist"), 200); m.insert(Arc::from("pdist"), 200); m.insert(Arc::from("cosine_similarity"), 256);
m.insert(Arc::from("fft"), 1024); m.insert(Arc::from("ifft"), 1024); m.insert(Arc::from("dct"), 1024);
m.insert(Arc::from("sum"), 4096); m.insert(Arc::from("mean"), 4096);
m.insert(Arc::from("variance"), 4096);
m.insert(Arc::from("l2_distance"), 4096);
m.insert(Arc::from("max"), 4096);
m.insert(Arc::from("min"), 4096);
m.insert(Arc::from("argmax"), 4096);
m.insert(Arc::from("argmin"), 4096);
m.insert(Arc::from("softmax"), 2048);
m.insert(Arc::from("gelu"), 2048);
m.insert(Arc::from("relu"), 2048);
m.insert(Arc::from("sigmoid"), 2048);
m.insert(Arc::from("tanh"), 2048);
m.insert(Arc::from("exp"), 2048);
m.insert(Arc::from("log"), 2048);
m.insert(Arc::from("sqrt"), 2048);
m.insert(Arc::from("sin"), 2048);
m.insert(Arc::from("cos"), 2048);
m.insert(Arc::from("rbf_kernel"), 200); m.insert(Arc::from("surrogate_predict"), 100); m.insert(Arc::from("surrogate_train"), 200);
m.insert(Arc::from("hmm"), HMM_FORWARD_THRESHOLD);
m
}
fn check_gpu_available() -> bool {
crate::runtime::tokio_block_on(crate::device::WgpuDevice::new()).is_ok()
}
pub fn global_config() -> &'static DispatchConfig {
GLOBAL_CONFIG.get_or_init(DispatchConfig::default)
}
pub trait Dispatch {
fn dispatch_config(&self) -> &DispatchConfig;
fn should_use_gpu(&self, operation: &str) -> bool;
fn dispatch_size(&self) -> usize;
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum DispatchTarget {
Cpu,
Gpu,
}
impl DispatchTarget {
#[must_use]
pub fn is_cpu(self) -> bool {
matches!(self, Self::Cpu)
}
#[must_use]
pub fn is_gpu(self) -> bool {
matches!(self, Self::Gpu)
}
}
#[must_use]
pub fn dispatch_for(operation: &str, input_size: usize) -> DispatchTarget {
if global_config().should_use_gpu(input_size, operation) {
DispatchTarget::Gpu
} else {
DispatchTarget::Cpu
}
}
#[must_use]
pub const fn pairwise_substrate(n_items: usize, item_dim: usize) -> DispatchTarget {
let n_pairs = n_items.saturating_mul(n_items.saturating_sub(1)) / 2;
let estimated_work = n_pairs * item_dim;
if estimated_work > 500_000 {
DispatchTarget::Gpu
} else {
DispatchTarget::Cpu
}
}
#[must_use]
pub const fn batch_fitness_substrate(pop_size: usize, genome_len: usize) -> DispatchTarget {
let total_work = pop_size * genome_len;
if total_work > 50_000 {
DispatchTarget::Gpu
} else {
DispatchTarget::Cpu
}
}
#[must_use]
pub const fn ode_substrate(n_systems: usize, n_steps: usize) -> DispatchTarget {
let total_work = n_systems * n_steps;
if total_work > 10_000 {
DispatchTarget::Gpu
} else {
DispatchTarget::Cpu
}
}
#[must_use]
pub const fn hmm_substrate(n_states: usize, n_observations: usize) -> DispatchTarget {
let total_work = n_states * n_observations;
if total_work > HMM_FORWARD_THRESHOLD {
DispatchTarget::Gpu
} else {
DispatchTarget::Cpu
}
}
#[must_use]
pub const fn spatial_substrate(grid_cells: usize) -> DispatchTarget {
if grid_cells > 4_000 {
DispatchTarget::Gpu
} else {
DispatchTarget::Cpu
}
}
#[must_use]
pub fn dispatch_with_config(
config: &DispatchConfig,
operation: &str,
input_size: usize,
) -> DispatchTarget {
if config.should_use_gpu(input_size, operation) {
DispatchTarget::Gpu
} else {
DispatchTarget::Cpu
}
}
#[must_use]
pub fn dispatch_with_transfer_cost(
operation: &str,
input_size: usize,
data_bytes: usize,
bandwidth: crate::unified_hardware::BandwidthTier,
) -> DispatchTarget {
let config = global_config();
if !config.should_use_gpu(input_size, operation) {
return DispatchTarget::Cpu;
}
let cost = bandwidth.transfer_cost();
let transfer_us =
cost.estimated_us(data_bytes) + crate::unified_hardware::GPU_DISPATCH_OVERHEAD_US;
let threshold = config.threshold(operation);
let compute_advantage_us = input_size.saturating_sub(threshold) as f64 * 0.001;
if compute_advantage_us > transfer_us {
DispatchTarget::Gpu
} else {
DispatchTarget::Cpu
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_default_config() {
let config = DispatchConfig::default();
assert_eq!(config.threshold("matmul"), 64);
assert_eq!(config.threshold("erf"), 512);
assert_eq!(config.threshold("cdist"), 200);
assert_eq!(config.threshold("unknown_op"), DEFAULT_THRESHOLD);
}
#[test]
fn test_force_cpu() {
let config = DispatchConfig::default().force_cpu();
assert!(!config.should_use_gpu(1_000_000, "matmul"));
assert!(!config.should_use_gpu(1_000_000, "erf"));
}
#[test]
fn test_size_threshold() {
let config = DispatchConfig::default();
if config.has_gpu() {
assert!(!config.should_use_gpu(32, "matmul")); assert!(config.should_use_gpu(128, "matmul")); }
}
#[test]
fn test_custom_threshold() {
let mut config = DispatchConfig::default();
config.set_threshold("custom_op", 500);
assert_eq!(config.threshold("custom_op"), 500);
}
#[test]
fn test_dispatch_target() {
assert!(DispatchTarget::Cpu.is_cpu());
assert!(!DispatchTarget::Cpu.is_gpu());
assert!(DispatchTarget::Gpu.is_gpu());
assert!(!DispatchTarget::Gpu.is_cpu());
}
#[test]
fn test_dispatch_for() {
let target = dispatch_for("matmul", 10);
assert!(target.is_cpu());
}
#[test]
fn test_force_gpu_and_cpu_mutual_exclusion() {
let config = DispatchConfig::default().force_gpu().force_cpu();
assert!(!config.should_use_gpu(1_000_000, "matmul"));
let config = DispatchConfig::default().force_cpu().force_gpu();
if config.has_gpu() {
assert!(config.should_use_gpu(1, "matmul"));
}
}
#[test]
fn test_with_thresholds_custom_map() {
let mut thresholds = HashMap::new();
thresholds.insert(Arc::from("custom_fft"), 256_usize);
thresholds.insert(Arc::from("custom_sum"), 8192_usize);
let config = DispatchConfig::with_thresholds(thresholds);
assert_eq!(config.threshold("custom_fft"), 256);
assert_eq!(config.threshold("custom_sum"), 8192);
assert_eq!(config.threshold("unknown"), DEFAULT_THRESHOLD);
}
#[test]
fn test_dispatch_with_config() {
let config = DispatchConfig::default().force_cpu();
let target = dispatch_with_config(&config, "matmul", 1_000_000);
assert!(target.is_cpu());
}
#[test]
fn test_dispatch_with_config_gpu_available() {
let config = DispatchConfig::default();
if config.has_gpu() {
let target = dispatch_with_config(&config, "matmul", 1_000_000);
assert!(target.is_gpu());
}
}
#[test]
fn test_dispatch_with_transfer_cost_small_input_stays_cpu() {
let target = dispatch_with_transfer_cost(
"matmul",
10,
10 * 4,
crate::unified_hardware::BandwidthTier::PciE4x16,
);
assert!(target.is_cpu(), "small input should stay on CPU");
}
#[test]
fn test_dispatch_with_transfer_cost_shared_memory() {
let config = DispatchConfig::default();
if config.has_gpu() {
let target = dispatch_with_transfer_cost(
"matmul",
200,
200 * 4,
crate::unified_hardware::BandwidthTier::SharedMemory,
);
assert!(target.is_cpu());
}
}
#[test]
fn test_dispatch_with_transfer_cost_regression_basic_dispatch() {
let config = DispatchConfig::default();
if config.has_gpu() {
let basic = dispatch_for("erf", 100);
let transfer_aware = dispatch_with_transfer_cost(
"erf",
100,
100 * 4,
crate::unified_hardware::BandwidthTier::PciE4x16,
);
assert_eq!(basic, transfer_aware);
}
}
#[test]
fn test_all_default_threshold_operations() {
let config = DispatchConfig::default();
assert_eq!(config.threshold("erf"), 512);
assert_eq!(config.threshold("erfc"), 512);
assert_eq!(config.threshold("gamma"), 256);
assert_eq!(config.threshold("lgamma"), 256);
assert_eq!(config.threshold("digamma"), 256);
assert_eq!(config.threshold("bessel_j0"), 512);
assert_eq!(config.threshold("bessel_j1"), 512);
assert_eq!(config.threshold("bessel_i0"), 512);
assert_eq!(config.threshold("bessel_k0"), 512);
assert_eq!(config.threshold("cholesky"), 128);
assert_eq!(config.threshold("eigh"), 128);
assert_eq!(config.threshold("lu"), 128);
assert_eq!(config.threshold("qr"), 128);
assert_eq!(config.threshold("svd"), 128);
assert_eq!(config.threshold("solve"), 128);
assert_eq!(config.threshold("tridiagonal"), 256);
assert_eq!(config.threshold("cdist"), 200);
assert_eq!(config.threshold("pdist"), 200);
assert_eq!(config.threshold("cosine_similarity"), 256);
assert_eq!(config.threshold("fft"), 1024);
assert_eq!(config.threshold("ifft"), 1024);
assert_eq!(config.threshold("dct"), 1024);
assert_eq!(config.threshold("sum"), 4096);
assert_eq!(config.threshold("mean"), 4096);
assert_eq!(config.threshold("max"), 4096);
assert_eq!(config.threshold("min"), 4096);
assert_eq!(config.threshold("argmax"), 4096);
assert_eq!(config.threshold("argmin"), 4096);
assert_eq!(config.threshold("relu"), 2048);
assert_eq!(config.threshold("sigmoid"), 2048);
assert_eq!(config.threshold("tanh"), 2048);
assert_eq!(config.threshold("exp"), 2048);
assert_eq!(config.threshold("log"), 2048);
assert_eq!(config.threshold("sqrt"), 2048);
assert_eq!(config.threshold("sin"), 2048);
assert_eq!(config.threshold("cos"), 2048);
assert_eq!(config.threshold("rbf_kernel"), 200);
assert_eq!(config.threshold("surrogate_predict"), 100);
assert_eq!(config.threshold("surrogate_train"), 200);
}
#[test]
fn test_boundary_conditions() {
let config = DispatchConfig::default();
if config.has_gpu() {
assert!(!config.should_use_gpu(63, "matmul"));
assert!(config.should_use_gpu(64, "matmul"));
assert!(config.should_use_gpu(65, "matmul"));
}
}
#[test]
fn test_zero_size_input() {
let target = dispatch_for("matmul", 0);
assert!(target.is_cpu());
}
#[test]
fn test_new_equals_default() {
let new_config = DispatchConfig::new();
let default_config = DispatchConfig::default();
assert_eq!(
new_config.threshold("matmul"),
default_config.threshold("matmul")
);
assert_eq!(new_config.has_gpu(), default_config.has_gpu());
}
#[test]
fn test_pairwise_substrate_small_uses_cpu() {
assert_eq!(pairwise_substrate(20, 500), DispatchTarget::Cpu);
}
#[test]
fn test_pairwise_substrate_large_uses_gpu() {
assert_eq!(pairwise_substrate(200, 1000), DispatchTarget::Gpu);
}
#[test]
fn test_batch_fitness_substrate_small_uses_cpu() {
assert_eq!(batch_fitness_substrate(100, 10), DispatchTarget::Cpu);
}
#[test]
fn test_batch_fitness_substrate_large_uses_gpu() {
assert_eq!(batch_fitness_substrate(50_000, 64), DispatchTarget::Gpu);
}
#[test]
fn test_ode_substrate_small_uses_cpu() {
assert_eq!(ode_substrate(10, 100), DispatchTarget::Cpu);
}
#[test]
fn test_ode_substrate_large_uses_gpu() {
assert_eq!(ode_substrate(1000, 2000), DispatchTarget::Gpu);
}
#[test]
fn test_hmm_substrate_small_uses_cpu() {
assert_eq!(hmm_substrate(3, 100), DispatchTarget::Cpu);
}
#[test]
fn test_hmm_substrate_large_uses_gpu() {
assert_eq!(hmm_substrate(3, 5000), DispatchTarget::Gpu);
}
#[test]
fn test_spatial_substrate_small_uses_cpu() {
assert_eq!(spatial_substrate(100), DispatchTarget::Cpu);
}
#[test]
fn test_spatial_substrate_large_uses_gpu() {
assert_eq!(spatial_substrate(10_000), DispatchTarget::Gpu);
}
}