mod operations;
use crate::device::WgpuDevice;
use crate::error::{BarracudaError, Result};
use operations::{generate_test_data, run_cpu_operation, run_gpu_operation};
use std::collections::HashMap;
use std::sync::Arc;
use std::time::{Duration, Instant};
#[derive(Debug, Clone)]
pub struct BenchmarkConfig {
pub sizes: Vec<usize>,
pub warmup_iterations: usize,
pub timed_iterations: usize,
pub safety_margin: f64,
pub min_speedup: f64,
pub max_time_per_benchmark: f64,
}
impl Default for BenchmarkConfig {
fn default() -> Self {
Self {
sizes: (4..=12).map(|i| 1 << i).collect(),
warmup_iterations: 3,
timed_iterations: 10,
safety_margin: 1.5,
min_speedup: 1.2,
max_time_per_benchmark: 30.0,
}
}
}
impl BenchmarkConfig {
#[must_use]
pub fn with_sizes(mut self, sizes: Vec<usize>) -> Self {
self.sizes = sizes;
self
}
#[must_use]
pub fn with_iterations(mut self, warmup: usize, timed: usize) -> Self {
self.warmup_iterations = warmup;
self.timed_iterations = timed;
self
}
#[must_use]
pub fn quick() -> Self {
Self {
sizes: vec![32, 64, 128, 256, 512, 1024],
warmup_iterations: 1,
timed_iterations: 5,
safety_margin: 1.5,
min_speedup: 1.2,
max_time_per_benchmark: 10.0,
}
}
#[must_use]
pub fn thorough() -> Self {
Self {
sizes: (3..=14).map(|i| 1 << i).collect(), warmup_iterations: 5,
timed_iterations: 20,
safety_margin: 1.3,
min_speedup: 1.1,
max_time_per_benchmark: 60.0,
}
}
}
#[derive(Debug, Clone)]
pub struct OperationBenchmark {
pub operation: String,
pub sizes: Vec<usize>,
pub cpu_times: Vec<Duration>,
pub gpu_times: Vec<Duration>,
pub speedups: Vec<f64>,
pub gpu_available: bool,
}
impl OperationBenchmark {
#[must_use]
pub fn crossover_index(&self, min_speedup: f64) -> Option<usize> {
self.speedups.iter().position(|&s| s >= min_speedup)
}
#[must_use]
pub fn crossover_size(&self, min_speedup: f64) -> Option<usize> {
self.crossover_index(min_speedup).map(|i| self.sizes[i])
}
#[must_use]
pub fn optimal_threshold(&self, min_speedup: f64, safety_margin: f64) -> usize {
self.crossover_size(min_speedup).map_or_else(
|| *self.sizes.last().unwrap_or(&1024), |size| ((size as f64) / safety_margin).max(1.0) as usize,
)
}
pub fn max_speedup(&self) -> f64 {
self.speedups.iter().copied().fold(0.0, f64::max)
}
#[must_use]
pub fn summary(&self, min_speedup: f64, safety_margin: f64) -> String {
let threshold = self.optimal_threshold(min_speedup, safety_margin);
let crossover = self.crossover_size(min_speedup);
let max_speedup = self.max_speedup();
format!(
"{:<15} threshold: {:>6} crossover: {:>6} max_speedup: {:.2}x gpu: {}",
self.operation,
threshold,
crossover.map_or_else(|| "N/A".to_string(), |s| s.to_string()),
max_speedup,
if self.gpu_available { "yes" } else { "no" }
)
}
}
#[derive(Debug, Clone)]
pub struct ThresholdResult {
pub operation: String,
pub threshold: usize,
pub crossover_size: Option<usize>,
pub max_speedup: f64,
pub confidence: f64,
}
#[derive(Debug, Clone)]
pub struct BenchmarkResult {
pub operations: HashMap<String, OperationBenchmark>,
pub config: BenchmarkConfig,
pub total_time: Duration,
pub gpu_name: Option<String>,
}
impl BenchmarkResult {
#[must_use]
pub fn optimal_thresholds(&self) -> HashMap<Arc<str>, usize> {
let mut thresholds = HashMap::new();
for (name, bench) in &self.operations {
let threshold =
bench.optimal_threshold(self.config.min_speedup, self.config.safety_margin);
thresholds.insert(Arc::from(name.as_str()), threshold);
}
thresholds
}
#[must_use]
pub fn threshold_results(&self) -> Vec<ThresholdResult> {
self.operations
.iter()
.map(|(name, bench)| {
let threshold =
bench.optimal_threshold(self.config.min_speedup, self.config.safety_margin);
let crossover = bench.crossover_size(self.config.min_speedup);
let max_speedup = bench.max_speedup();
let confidence = if max_speedup > 2.0 {
1.0 } else if max_speedup > 1.5 {
0.8
} else if max_speedup > 1.2 {
0.6
} else if max_speedup > 1.0 {
0.4
} else {
0.2 };
ThresholdResult {
operation: name.clone(),
threshold,
crossover_size: crossover,
max_speedup,
confidence,
}
})
.collect()
}
#[must_use]
pub fn summary(&self) -> String {
let mut lines = Vec::new();
lines.push(
"═══════════════════════════════════════════════════════════════════".to_string(),
);
lines.push(" DISPATCH BENCHMARK RESULTS".to_string());
lines.push(
"═══════════════════════════════════════════════════════════════════".to_string(),
);
if let Some(gpu) = &self.gpu_name {
lines.push(format!("GPU: {gpu}"));
} else {
lines.push("GPU: Not available".to_string());
}
lines.push(format!("Total time: {:.2}s", self.total_time.as_secs_f64()));
lines.push(format!("Sizes tested: {:?}", self.config.sizes));
lines.push(String::new());
lines.push(
"─────────────────────────────────────────────────────────────────────".to_string(),
);
lines.push(format!(
"{:<15} {:>10} {:>10} {:>12} {:>6}",
"Operation", "Threshold", "Crossover", "Max Speedup", "GPU"
));
lines.push(
"─────────────────────────────────────────────────────────────────────".to_string(),
);
let mut results: Vec<_> = self.threshold_results();
results.sort_by(|a, b| a.operation.cmp(&b.operation));
for result in &results {
lines.push(format!(
"{:<15} {:>10} {:>10} {:>11.2}x {:>6}",
result.operation,
result.threshold,
result
.crossover_size
.map_or_else(|| "N/A".to_string(), |s| s.to_string()),
result.max_speedup,
if result.confidence > 0.5 {
"high"
} else {
"low"
}
));
}
lines.push(
"═══════════════════════════════════════════════════════════════════".to_string(),
);
lines.join("\n")
}
}
pub struct BenchmarkSuite {
config: BenchmarkConfig,
gpu_available: bool,
gpu_name: Option<String>,
gpu_device: Option<Arc<WgpuDevice>>,
}
impl BenchmarkSuite {
#[must_use]
pub fn new(config: BenchmarkConfig) -> Self {
let (gpu_available, gpu_name, gpu_device) = check_gpu();
Self {
config,
gpu_available,
gpu_name,
gpu_device,
}
}
#[must_use]
pub fn has_gpu(&self) -> bool {
self.gpu_available
}
pub fn benchmark_operation(&self, operation: &str) -> Result<OperationBenchmark> {
let mut sizes = Vec::new();
let mut cpu_times = Vec::new();
let mut gpu_times = Vec::new();
let mut speedups = Vec::new();
let start = Instant::now();
for &size in &self.config.sizes {
if start.elapsed().as_secs_f64() > self.config.max_time_per_benchmark {
break;
}
let cpu_time = self.benchmark_cpu(operation, size)?;
let gpu_time = if self.gpu_available {
self.benchmark_gpu(operation, size)?
} else {
let simulated_speedup: f64 = match operation {
"matmul" => 0.02, "exp" | "log" | "sqrt" => 0.01, _ => 0.1, };
cpu_time.mul_f64(simulated_speedup.max(0.01))
};
let speedup = cpu_time.as_secs_f64() / gpu_time.as_secs_f64().max(1e-9);
sizes.push(size);
cpu_times.push(cpu_time);
gpu_times.push(gpu_time);
speedups.push(speedup);
}
Ok(OperationBenchmark {
operation: operation.to_string(),
sizes,
cpu_times,
gpu_times,
speedups,
gpu_available: self.gpu_available,
})
}
fn benchmark_cpu(&self, operation: &str, size: usize) -> Result<Duration> {
let data = generate_test_data(operation, size);
for _ in 0..self.config.warmup_iterations {
run_cpu_operation(operation, &data, self.gpu_device.as_ref())?;
}
let mut times = Vec::with_capacity(self.config.timed_iterations);
for _ in 0..self.config.timed_iterations {
let start = Instant::now();
run_cpu_operation(operation, &data, self.gpu_device.as_ref())?;
times.push(start.elapsed());
}
times.sort();
Ok(times[times.len() / 2])
}
fn benchmark_gpu(&self, operation: &str, size: usize) -> Result<Duration> {
if !self.gpu_available {
return Err(BarracudaError::Internal("GPU not available".to_string()));
}
let data = generate_test_data(operation, size);
for _ in 0..self.config.warmup_iterations {
run_gpu_operation(operation, &data, self.gpu_device.as_ref())?;
}
let mut times = Vec::with_capacity(self.config.timed_iterations);
for _ in 0..self.config.timed_iterations {
let start = Instant::now();
run_gpu_operation(operation, &data, self.gpu_device.as_ref())?;
times.push(start.elapsed());
}
times.sort();
Ok(times[times.len() / 2])
}
pub fn run_common(&self) -> Result<BenchmarkResult> {
let operations = vec!["matmul", "erf", "exp", "sum", "cdist"];
self.run_operations(&operations)
}
pub fn run_operations(&self, operations: &[&str]) -> Result<BenchmarkResult> {
let start = Instant::now();
let mut results = HashMap::new();
for &op in operations {
match self.benchmark_operation(op) {
Ok(bench) => {
results.insert(op.to_string(), bench);
}
Err(e) => {
tracing::warn!("Failed to benchmark {}: {}", op, e);
}
}
}
Ok(BenchmarkResult {
operations: results,
config: self.config.clone(),
total_time: start.elapsed(),
gpu_name: self.gpu_name.clone(),
})
}
pub fn run_all(&self) -> Result<BenchmarkResult> {
let operations = vec![
"erf",
"gamma",
"bessel_j0",
"matmul",
"cholesky",
"eigh",
"lu",
"qr",
"svd",
"solve",
"cdist",
"exp",
"log",
"sqrt",
"sin",
"cos",
"sum",
"max",
"rbf_kernel",
];
self.run_operations(&operations)
}
}
fn check_gpu() -> (bool, Option<String>, Option<Arc<crate::device::WgpuDevice>>) {
match crate::runtime::tokio_block_on(crate::device::WgpuDevice::new()) {
Ok(device) => {
let name = device.adapter_info().name.clone();
(true, Some(name), Some(Arc::new(device)))
}
Err(_) => (false, None, None),
}
}
#[cfg(test)]
mod tests;