primal-barracuda 0.4.0

barraCuda — BARrier-free Rust Abstracted Cross-platform Unified Dimensional Algebra. Vendor-agnostic GPU compute via WGSL.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
// SPDX-License-Identifier: AGPL-3.0-or-later
//! Benchmark suite for empirically determining CPU/GPU dispatch thresholds
//!
//! This module provides tools to benchmark operations at various input sizes
//! and find the crossover point where GPU becomes faster than CPU.
//!
//! # Algorithm
//!
//! For each operation:
//! 1. Generate test data at exponentially increasing sizes
//! 2. Benchmark CPU implementation (multiple iterations, take median)
//! 3. Benchmark GPU implementation (multiple iterations, take median)
//! 4. Find crossover point where GPU becomes faster
//! 5. Add safety margin for threshold recommendation
//!
//! # Example
//!
//! ```ignore
//! use barracuda::dispatch::benchmark::{BenchmarkSuite, BenchmarkConfig};
//!
//! let config = BenchmarkConfig::default();
//! let suite = BenchmarkSuite::new(config);
//!
//! // Benchmark specific operation
//! let result = suite.benchmark_operation("matmul")?;
//! tracing::info!("Optimal threshold for matmul: {}", result.optimal_threshold);
//!
//! // Benchmark all operations
//! let results = suite.run_all()?;
//! tracing::info!("{}", results.summary());
//! ```

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};

/// Configuration for benchmarking
#[derive(Debug, Clone)]
pub struct BenchmarkConfig {
    /// Sizes to test (exponential growth)
    pub sizes: Vec<usize>,
    /// Number of warmup iterations
    pub warmup_iterations: usize,
    /// Number of timed iterations
    pub timed_iterations: usize,
    /// Safety margin multiplier for threshold (e.g., 1.5 = 50% above crossover)
    pub safety_margin: f64,
    /// Minimum speedup required to prefer GPU (e.g., 1.2 = GPU must be 20% faster)
    pub min_speedup: f64,
    /// Maximum time per benchmark (seconds)
    pub max_time_per_benchmark: f64,
}

impl Default for BenchmarkConfig {
    fn default() -> Self {
        Self {
            // Exponential sizes: 16, 32, 64, 128, 256, 512, 1024, 2048, 4096
            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 {
    /// Create config with custom sizes
    #[must_use]
    pub fn with_sizes(mut self, sizes: Vec<usize>) -> Self {
        self.sizes = sizes;
        self
    }

    /// Set number of iterations
    #[must_use]
    pub fn with_iterations(mut self, warmup: usize, timed: usize) -> Self {
        self.warmup_iterations = warmup;
        self.timed_iterations = timed;
        self
    }

    /// Quick benchmark (fewer iterations)
    #[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,
        }
    }

    /// Thorough benchmark (more sizes and iterations)
    #[must_use]
    pub fn thorough() -> Self {
        Self {
            sizes: (3..=14).map(|i| 1 << i).collect(), // 8 to 16384
            warmup_iterations: 5,
            timed_iterations: 20,
            safety_margin: 1.3,
            min_speedup: 1.1,
            max_time_per_benchmark: 60.0,
        }
    }
}

/// Result of benchmarking a single operation
#[derive(Debug, Clone)]
pub struct OperationBenchmark {
    /// Operation name
    pub operation: String,
    /// Size at each measurement point
    pub sizes: Vec<usize>,
    /// CPU time (median) at each size
    pub cpu_times: Vec<Duration>,
    /// GPU time (median) at each size
    pub gpu_times: Vec<Duration>,
    /// Speedup (CPU/GPU) at each size
    pub speedups: Vec<f64>,
    /// Whether GPU was available for benchmarking
    pub gpu_available: bool,
}

impl OperationBenchmark {
    /// Find the crossover point where GPU becomes faster
    #[must_use]
    pub fn crossover_index(&self, min_speedup: f64) -> Option<usize> {
        self.speedups.iter().position(|&s| s >= min_speedup)
    }

    /// Get the crossover size
    #[must_use]
    pub fn crossover_size(&self, min_speedup: f64) -> Option<usize> {
        self.crossover_index(min_speedup).map(|i| self.sizes[i])
    }

    /// Compute optimal threshold with safety margin
    #[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), // GPU never faster - use maximum tested size
            |size| ((size as f64) / safety_margin).max(1.0) as usize,
        )
    }

    /// Get maximum speedup observed
    pub fn max_speedup(&self) -> f64 {
        self.speedups.iter().copied().fold(0.0, f64::max)
    }

    /// Summary string
    #[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" }
        )
    }
}

/// Result of threshold determination
#[derive(Debug, Clone)]
pub struct ThresholdResult {
    /// Operation name
    pub operation: String,
    /// Recommended threshold
    pub threshold: usize,
    /// Crossover size (where GPU became faster)
    pub crossover_size: Option<usize>,
    /// Maximum speedup observed
    pub max_speedup: f64,
    /// Confidence (based on consistency of speedup trend)
    pub confidence: f64,
}

/// Result of running all benchmarks
#[derive(Debug, Clone)]
pub struct BenchmarkResult {
    /// Per-operation benchmarks
    pub operations: HashMap<String, OperationBenchmark>,
    /// Config used
    pub config: BenchmarkConfig,
    /// Total benchmark time
    pub total_time: Duration,
    /// GPU device name (if available)
    pub gpu_name: Option<String>,
}

impl BenchmarkResult {
    /// Get optimal thresholds for all operations
    #[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
    }

    /// Get threshold results with confidence
    #[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();

                // Confidence based on how clear the crossover is
                let confidence = if max_speedup > 2.0 {
                    1.0 // Very clear GPU advantage
                } 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 // GPU rarely faster
                };

                ThresholdResult {
                    operation: name.clone(),
                    threshold,
                    crossover_size: crossover,
                    max_speedup,
                    confidence,
                }
            })
            .collect()
    }

    /// Human-readable summary
    #[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")
    }
}

/// Benchmark suite for dispatch threshold determination
pub struct BenchmarkSuite {
    config: BenchmarkConfig,
    gpu_available: bool,
    gpu_name: Option<String>,
    /// GPU device (when available) for ops that require it (solve, etc.)
    gpu_device: Option<Arc<WgpuDevice>>,
}

impl BenchmarkSuite {
    /// Create a new benchmark suite
    #[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,
        }
    }

    /// Check if GPU is available
    #[must_use]
    pub fn has_gpu(&self) -> bool {
        self.gpu_available
    }

    /// Benchmark a specific operation
    /// # Errors
    /// Returns [`Err`] if CPU or GPU operation execution fails (e.g., unknown
    /// operation, GPU not available when required, buffer allocation failure).
    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 {
            // Check time limit
            if start.elapsed().as_secs_f64() > self.config.max_time_per_benchmark {
                break;
            }

            // Benchmark CPU
            let cpu_time = self.benchmark_cpu(operation, size)?;

            // Benchmark GPU (if available)
            // When GPU is unavailable, we simulate GPU timing based on typical
            // CPU/GPU ratios. This allows the benchmark suite to still determine
            // optimal dispatch thresholds on CPU-only systems.
            let gpu_time = if self.gpu_available {
                self.benchmark_gpu(operation, size)?
            } else {
                // Simulated GPU time: faster than CPU for large workloads
                // This reflects typical GPU behavior without actual hardware
                let simulated_speedup: f64 = match operation {
                    "matmul" => 0.02,               // GPU ~50x faster for large matmul
                    "exp" | "log" | "sqrt" => 0.01, // ~100x for elementwise
                    _ => 0.1,                       // ~10x default
                };
                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,
        })
    }

    /// Benchmark CPU implementation
    fn benchmark_cpu(&self, operation: &str, size: usize) -> Result<Duration> {
        // Generate test data
        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])
    }

    /// Benchmark GPU implementation
    fn benchmark_gpu(&self, operation: &str, size: usize) -> Result<Duration> {
        if !self.gpu_available {
            return Err(BarracudaError::Internal("GPU not available".to_string()));
        }

        // Generate test data
        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])
    }

    /// Run benchmarks for common operations
    /// # Errors
    /// Returns [`Err`] if any operation benchmark fails (see
    /// [`benchmark_operation`](Self::benchmark_operation)).
    pub fn run_common(&self) -> Result<BenchmarkResult> {
        let operations = vec!["matmul", "erf", "exp", "sum", "cdist"];

        self.run_operations(&operations)
    }

    /// Run benchmarks for specified operations
    /// # Errors
    /// Returns [`Err`] if any operation benchmark fails (errors are logged but
    /// do not stop the run; this returns `Ok` with partial results).
    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) => {
                    // Log error but continue with other operations
                    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(),
        })
    }

    /// Run all benchmarks
    /// # Errors
    /// Returns [`Err`] if any operation benchmark fails (see
    /// [`run_operations`](Self::run_operations)).
    pub fn run_all(&self) -> Result<BenchmarkResult> {
        let operations = vec![
            // Special functions
            "erf",
            "gamma",
            "bessel_j0",
            // Linear algebra
            "matmul",
            "cholesky",
            "eigh",
            "lu",
            "qr",
            "svd",
            "solve",
            // Distance
            "cdist",
            // Element-wise
            "exp",
            "log",
            "sqrt",
            "sin",
            "cos",
            // Reductions
            "sum",
            "max",
            // Surrogate
            "rbf_kernel",
        ];

        self.run_operations(&operations)
    }
}

/// Probe GPU availability.
///
/// Uses `WgpuDevice::new()` for a consistent probe rather than duplicating
/// low-level wgpu setup code.
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;