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
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
// SPDX-License-Identifier: AGPL-3.0-or-later
//! `BarraCuda` vs CUDA Benchmarking Framework
//!
//! **Purpose**: Compare `BarraCuda` performance against CUDA across hardware
//!
//! This module provides comprehensive benchmarking tools to:
//! - Compare `BarraCuda` (WGSL/WebGPU) vs CUDA performance
//! - Test across different hardware (NVIDIA, AMD, Intel, Apple)
//! - Measure operation throughput, latency, and efficiency
//! - Generate performance reports and visualizations
//!
//! **Deep Debt Principles**:
//! - ✅ Fair comparison (same algorithms, same precision)
//! - ✅ Hardware diversity (test on all available GPUs)
//! - ✅ Reproducible (fixed random seeds, warm-up runs)
//! - ✅ Comprehensive (cover all operation types)

use crate::error::Result;
use std::time::Duration;

/// Benchmark harness — warm-up, measurement, and statistical aggregation.
pub mod harness;
/// GPU operation benchmarks for every supported compute primitive.
pub mod operations;
/// Benchmark report generation — CSV, JSON, and human-readable output.
pub mod report;

/// Benchmark configuration
#[derive(Debug, Clone)]
pub struct BenchmarkConfig {
    /// Number of warm-up iterations (to prime caches)
    pub warmup_iterations: usize,

    /// Number of measurement iterations
    pub measurement_iterations: usize,

    /// Minimum benchmark duration (ensures statistically significant results)
    pub min_duration: Duration,

    /// Enable CUDA comparison (requires CUDA installation)
    pub compare_cuda: bool,

    /// Precision (FP32, FP16, etc.)
    pub precision: Precision,

    /// Random seed (for reproducibility)
    pub random_seed: u64,
}

/// Default minimum benchmark measurement duration (seconds).
const DEFAULT_MIN_BENCH_DURATION_SECS: u64 = 5;

impl Default for BenchmarkConfig {
    fn default() -> Self {
        Self {
            warmup_iterations: 10,
            measurement_iterations: 100,
            min_duration: Duration::from_secs(DEFAULT_MIN_BENCH_DURATION_SECS),
            compare_cuda: true,
            precision: Precision::FP32,
            random_seed: 42,
        }
    }
}

/// Precision for benchmarks.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Precision {
    /// 16-bit floating point
    FP16,
    /// 32-bit floating point
    FP32,
    /// 64-bit floating point
    FP64,
    /// 8-bit integer
    INT8,
}

/// Benchmark result for a single operation
#[derive(Debug, Clone)]
pub struct BenchmarkResult {
    /// Operation name
    pub operation: String,

    /// Hardware name (e.g., "NVIDIA RTX 4090", "AMD RX 7900 XTX")
    pub hardware: String,

    /// Framework (`BarraCuda` or CUDA)
    pub framework: Framework,

    /// Median execution time
    pub median_time: Duration,

    /// Mean execution time
    pub mean_time: Duration,

    /// Standard deviation
    pub std_dev: Duration,

    /// Minimum time
    pub min_time: Duration,

    /// Maximum time
    pub max_time: Duration,

    /// Throughput (operations/second)
    pub throughput: f64,

    /// Memory bandwidth utilized (GB/s)
    pub bandwidth_gbps: f64,

    /// TFLOPS achieved
    pub tflops: f64,
}

/// Framework identifier for benchmark comparison.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Framework {
    /// `BarraCuda` (WGSL/WebGPU)
    BarraCuda,
    /// Native CUDA
    CUDA,
    /// `PyTorch` with CUDA backend
    PyTorchCUDA,
    /// TensorFlow with CUDA backend
    TensorFlowCUDA,
}

impl std::fmt::Display for Framework {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::BarraCuda => write!(f, "BarraCuda"),
            Self::CUDA => write!(f, "CUDA"),
            Self::PyTorchCUDA => write!(f, "PyTorch+CUDA"),
            Self::TensorFlowCUDA => write!(f, "TensorFlow+CUDA"),
        }
    }
}

/// Benchmark comparison result
#[derive(Debug, Clone)]
pub struct ComparisonResult {
    /// Operation name
    pub operation: String,

    /// Hardware name
    pub hardware: String,

    /// `BarraCuda` result
    pub barracuda: BenchmarkResult,

    /// CUDA result
    pub cuda: Option<BenchmarkResult>,

    /// Speedup (positive = `BarraCuda` faster, negative = CUDA faster)
    /// 2.0 = `BarraCuda` is 2x faster
    /// -2.0 = CUDA is 2x faster
    pub speedup: f64,

    /// Parity percentage (100% = same speed, >100% = `BarraCuda` faster)
    pub parity_percent: f64,
}

impl ComparisonResult {
    /// Create comparison from `BarraCuda` and optional CUDA results.
    #[must_use]
    pub fn new(barracuda: BenchmarkResult, cuda: Option<BenchmarkResult>) -> Self {
        let operation = barracuda.operation.clone();
        let hardware = barracuda.hardware.clone();

        let (speedup, parity_percent) = if let Some(ref cuda_result) = cuda {
            let barracuda_secs = barracuda.median_time.as_secs_f64();
            let cuda_secs = cuda_result.median_time.as_secs_f64();

            let speedup = cuda_secs / barracuda_secs;
            let parity = (cuda_secs / barracuda_secs) * 100.0;

            (speedup, parity)
        } else {
            (0.0, 100.0)
        };

        Self {
            operation,
            hardware,
            barracuda,
            cuda,
            speedup,
            parity_percent,
        }
    }

    /// Check if `BarraCuda` achieves target parity (e.g., 90%)
    #[must_use]
    pub fn achieves_parity(&self, target_percent: f64) -> bool {
        self.parity_percent >= target_percent
    }
}

/// Benchmark suite for all operations (`BarraCuda` vs CUDA).
pub struct BenchmarkSuite {
    config: BenchmarkConfig,
    results: Vec<ComparisonResult>,
}

impl BenchmarkSuite {
    /// Create new benchmark suite
    #[must_use]
    pub fn new(config: BenchmarkConfig) -> Self {
        Self {
            config,
            results: Vec::new(),
        }
    }

    /// Run all benchmarks
    /// # Errors
    /// Returns [`Err`] if hardware discovery fails, or if any benchmark
    /// operation fails (e.g. device creation, GPU dispatch).
    pub async fn run_all(&mut self) -> Result<()> {
        tracing::info!("Starting BarraCuda vs CUDA Benchmark Suite");

        // Discover hardware
        let hardware = self.discover_hardware().await?;
        tracing::info!("Discovered {} compute device(s)", hardware.len());
        for hw in &hardware {
            tracing::info!("  {hw}");
        }

        // Run operation benchmarks
        self.benchmark_matrix_operations().await?;
        self.benchmark_activations().await?;
        self.benchmark_reductions().await?;
        self.benchmark_convolutions().await?;

        Ok(())
    }

    async fn discover_hardware(&self) -> Result<Vec<String>> {
        use crate::device::WgpuDevice;

        let mut hardware = vec!["CPU".to_string()];

        // Try to discover GPU via wgpu
        if let Ok(device) = WgpuDevice::new().await {
            let info = device.adapter_info();
            hardware.push(format!("{} ({:?})", info.name, info.device_type));
        } else {
            // No GPU available, CPU-only mode
        }

        Ok(hardware)
    }

    async fn benchmark_matrix_operations(&mut self) -> Result<()> {
        tracing::info!("Matrix Operations");

        // Small sizes that complete quickly for CI/testing
        let quick_sizes: &[(usize, usize, usize)] =
            &[(128, 128, 128), (256, 256, 256), (512, 512, 512)];

        for &(m, n, k) in quick_sizes {
            let (barracuda, cuda) = operations::benchmark_matmul(&self.config, m, n, k).await?;
            self.results.push(ComparisonResult::new(barracuda, cuda));
        }

        Ok(())
    }

    async fn benchmark_activations(&mut self) -> Result<()> {
        tracing::info!("Activation Functions");

        let activation_ops = ["ReLU", "GELU", "SiLU", "Sigmoid", "Tanh"];
        let sizes = [10_000, 100_000, 1_000_000];

        for op in &activation_ops {
            for &size in &sizes {
                let result = operations::benchmark_activation(&self.config, op, size).await?;
                // No CUDA comparison for activations yet
                self.results.push(ComparisonResult::new(result, None));
            }
        }

        Ok(())
    }

    async fn benchmark_reductions(&mut self) -> Result<()> {
        tracing::info!("Reduction Operations");

        // Reduction benchmark using sum operation
        let sizes = [10_000usize, 100_000, 1_000_000, 10_000_000];

        for &size in &sizes {
            let mut times = Vec::new();
            let data: Vec<f32> = (0..size).map(|i| (i % 1000) as f32 * 0.001).collect();

            // Warmup
            for _ in 0..self.config.warmup_iterations {
                let _: f32 = data.iter().sum();
            }

            // Measurement
            for _ in 0..self.config.measurement_iterations {
                let start = std::time::Instant::now();
                let _sum: f32 = data.iter().sum();
                times.push(start.elapsed());
            }

            times.sort();
            let median_time = times[times.len() / 2];
            let min_time = times[0];
            let max_time = times[times.len() - 1];
            let sum: Duration = times.iter().sum();
            let mean_time = sum / times.len() as u32;

            let mean_nanos = mean_time.as_nanos() as f64;
            let variance: f64 = times
                .iter()
                .map(|t| {
                    let diff = t.as_nanos() as f64 - mean_nanos;
                    diff * diff
                })
                .sum::<f64>()
                / times.len() as f64;
            let std_dev = Duration::from_nanos(variance.sqrt() as u64);

            let throughput = size as f64 / median_time.as_secs_f64();
            let bandwidth_gbps = (size as f64 * 4.0) / median_time.as_secs_f64() / 1e9;

            let result = BenchmarkResult {
                operation: format!("Sum [size={size}]"),
                hardware: "CPU".to_string(),
                framework: Framework::BarraCuda,
                median_time,
                min_time,
                max_time,
                mean_time,
                std_dev,
                throughput,
                bandwidth_gbps,
                tflops: throughput / 1e12,
            };

            self.results.push(ComparisonResult::new(result, None));
        }

        Ok(())
    }

    async fn benchmark_convolutions(&mut self) -> Result<()> {
        tracing::info!("Convolution Operations");

        // Basic 1D convolution benchmark
        let input_sizes = [1024usize, 4096, 16_384];
        let kernel_size = 7;

        for &input_size in &input_sizes {
            let mut times = Vec::new();
            let input: Vec<f32> = (0..input_size).map(|i| (i % 256) as f32 / 256.0).collect();
            let kernel: Vec<f32> = (0..kernel_size)
                .map(|i| i as f32 / kernel_size as f32)
                .collect();

            // Warmup
            for _ in 0..self.config.warmup_iterations {
                let _ = cpu_conv1d(&input, &kernel);
            }

            // Measurement
            for _ in 0..self.config.measurement_iterations {
                let start = std::time::Instant::now();
                let _ = cpu_conv1d(&input, &kernel);
                times.push(start.elapsed());
            }

            times.sort();
            let median_time = times[times.len() / 2];
            let min_time = times[0];
            let max_time = times[times.len() - 1];
            let sum: Duration = times.iter().sum();
            let mean_time = sum / times.len() as u32;

            let mean_nanos = mean_time.as_nanos() as f64;
            let variance: f64 = times
                .iter()
                .map(|t| {
                    let diff = t.as_nanos() as f64 - mean_nanos;
                    diff * diff
                })
                .sum::<f64>()
                / times.len() as f64;
            let std_dev = Duration::from_nanos(variance.sqrt() as u64);

            let output_size = input_size - kernel_size + 1;
            let flops = (output_size * kernel_size) as f64; // MACs
            let throughput = flops / median_time.as_secs_f64();
            let bandwidth_gbps = ((input_size + kernel_size + output_size) as f64 * 4.0)
                / median_time.as_secs_f64()
                / 1e9;

            let result = BenchmarkResult {
                operation: format!("Conv1D [in={input_size}, k={kernel_size}]"),
                hardware: "CPU".to_string(),
                framework: Framework::BarraCuda,
                median_time,
                min_time,
                max_time,
                mean_time,
                std_dev,
                throughput,
                bandwidth_gbps,
                tflops: throughput / 1e12,
            };

            self.results.push(ComparisonResult::new(result, None));
        }

        Ok(())
    }

    /// Get all results
    #[must_use]
    pub fn results(&self) -> &[ComparisonResult] {
        &self.results
    }

    /// Generate summary report
    #[must_use]
    pub fn summary(&self) -> BenchmarkSummary {
        let total_ops = self.results.len();
        let ops_with_parity_90 = self
            .results
            .iter()
            .filter(|r| r.achieves_parity(90.0))
            .count();
        let ops_with_parity_95 = self
            .results
            .iter()
            .filter(|r| r.achieves_parity(95.0))
            .count();
        let ops_with_parity_98 = self
            .results
            .iter()
            .filter(|r| r.achieves_parity(98.0))
            .count();

        let mean_parity = if total_ops > 0 {
            self.results.iter().map(|r| r.parity_percent).sum::<f64>() / total_ops as f64
        } else {
            0.0
        };

        BenchmarkSummary {
            total_operations: total_ops,
            ops_at_90_parity: ops_with_parity_90,
            ops_at_95_parity: ops_with_parity_95,
            ops_at_98_parity: ops_with_parity_98,
            mean_parity_percent: mean_parity,
        }
    }
}

/// Benchmark summary statistics.
#[derive(Debug, Clone)]
pub struct BenchmarkSummary {
    /// Total operations benchmarked
    pub total_operations: usize,
    /// Operations achieving ≥90% parity
    pub ops_at_90_parity: usize,
    /// Operations achieving ≥95% parity
    pub ops_at_95_parity: usize,
    /// Operations achieving ≥98% parity
    pub ops_at_98_parity: usize,
    /// Mean parity percentage
    pub mean_parity_percent: f64,
}

impl std::fmt::Display for BenchmarkSummary {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        writeln!(f, "📊 Benchmark Summary")?;
        writeln!(f, "━━━━━━━━━━━━━━━━━━━━━━━━━━━━")?;
        writeln!(f, "Total Operations: {}", self.total_operations)?;
        writeln!(
            f,
            "≥90% Parity: {} ({:.1}%)",
            self.ops_at_90_parity,
            (self.ops_at_90_parity as f64 / self.total_operations as f64) * 100.0
        )?;
        writeln!(
            f,
            "≥95% Parity: {} ({:.1}%)",
            self.ops_at_95_parity,
            (self.ops_at_95_parity as f64 / self.total_operations as f64) * 100.0
        )?;
        writeln!(
            f,
            "≥98% Parity: {} ({:.1}%)",
            self.ops_at_98_parity,
            (self.ops_at_98_parity as f64 / self.total_operations as f64) * 100.0
        )?;
        writeln!(f, "Mean Parity: {:.2}%", self.mean_parity_percent)?;
        Ok(())
    }
}

/// CPU 1D convolution for benchmarking
#[inline(never)]
fn cpu_conv1d(input: &[f32], kernel: &[f32]) -> Vec<f32> {
    let n = input.len();
    let k = kernel.len();
    if k > n {
        return vec![];
    }

    let output_size = n - k + 1;
    let mut output = vec![0.0f32; output_size];

    for i in 0..output_size {
        let mut sum = 0.0f32;
        for j in 0..k {
            sum = input[i + j].mul_add(kernel[j], sum);
        }
        output[i] = sum;
    }

    output
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::time::Duration;

    fn make_result(op: &str, hw: &str, framework: Framework, median_ms: f64) -> BenchmarkResult {
        let median = Duration::from_secs_f64(median_ms / 1000.0);
        BenchmarkResult {
            operation: op.to_string(),
            hardware: hw.to_string(),
            framework,
            median_time: median,
            mean_time: median,
            std_dev: Duration::from_millis(1),
            min_time: median,
            max_time: median,
            throughput: 1000.0 / median_ms,
            bandwidth_gbps: 0.0,
            tflops: 0.0,
        }
    }

    #[test]
    fn test_benchmark_config_default() {
        let config = BenchmarkConfig::default();
        assert_eq!(config.warmup_iterations, 10);
        assert_eq!(config.measurement_iterations, 100);
        assert_eq!(config.precision, Precision::FP32);
    }

    #[test]
    fn test_framework_display() {
        assert_eq!(Framework::BarraCuda.to_string(), "BarraCuda");
        assert_eq!(Framework::CUDA.to_string(), "CUDA");
    }

    #[test]
    fn test_comparison_result_new_with_cuda() {
        let barracuda = make_result("MatMul", "CPU", Framework::BarraCuda, 10.0);
        let cuda = make_result("MatMul", "CPU", Framework::CUDA, 5.0);
        let cmp = ComparisonResult::new(barracuda, Some(cuda));
        // CUDA is 2x faster: cuda_secs/barracuda_secs = 5/10 = 0.5, parity = 50%
        assert!((cmp.speedup - 0.5).abs() < 1e-6);
        assert!((cmp.parity_percent - 50.0).abs() < 1e-6);
    }

    #[test]
    fn test_comparison_result_new_without_cuda() {
        let barracuda = make_result("MatMul", "CPU", Framework::BarraCuda, 10.0);
        let cmp = ComparisonResult::new(barracuda, None);
        assert_eq!(cmp.parity_percent, 100.0);
        assert_eq!(cmp.speedup, 0.0);
    }

    #[test]
    fn test_achieves_parity() {
        let barracuda = make_result("MatMul", "CPU", Framework::BarraCuda, 10.0);
        let cuda_95 = make_result("MatMul", "CPU", Framework::CUDA, 9.5);
        let cuda_80 = make_result("MatMul", "CPU", Framework::CUDA, 8.0);
        let cmp_95 = ComparisonResult::new(barracuda.clone(), Some(cuda_95));
        let cmp_80 = ComparisonResult::new(barracuda, Some(cuda_80));
        assert!(cmp_95.achieves_parity(90.0));
        assert!(!cmp_80.achieves_parity(90.0));
    }

    #[test]
    fn test_benchmark_suite_new_and_summary() {
        let config = BenchmarkConfig::default();
        let suite = BenchmarkSuite::new(config);
        let summary = suite.summary();
        assert_eq!(summary.total_operations, 0);
        assert_eq!(summary.ops_at_90_parity, 0);
        assert_eq!(summary.ops_at_95_parity, 0);
        assert_eq!(summary.ops_at_98_parity, 0);
        assert!((summary.mean_parity_percent - 0.0).abs() < 1e-6);
    }

    #[test]
    fn test_benchmark_summary_display() {
        let summary = BenchmarkSummary {
            total_operations: 5,
            ops_at_90_parity: 4,
            ops_at_95_parity: 3,
            ops_at_98_parity: 2,
            mean_parity_percent: 92.5,
        };
        let s = summary.to_string();
        assert!(s.contains("Benchmark Summary"));
        assert!(s.contains("Total Operations: 5"));
        assert!(s.contains("≥90% Parity"));
        assert!(s.contains("Mean Parity: 92.50%"));
    }

    #[test]
    fn test_precision_variants() {
        assert_eq!(Precision::FP16, Precision::FP16);
        assert_eq!(Precision::FP32, Precision::FP32);
        assert_eq!(Precision::FP64, Precision::FP64);
        assert_eq!(Precision::INT8, Precision::INT8);
        assert!(Precision::FP32 != Precision::FP64);
    }

    #[test]
    fn test_cpu_conv1d() {
        let input = vec![1.0f32, 2.0, 3.0, 4.0, 5.0];
        let kernel = vec![1.0f32, 0.0, 1.0];
        let out = super::cpu_conv1d(&input, &kernel);
        assert_eq!(out.len(), 3);
        assert!((out[0] - 4.0).abs() < 1e-6);
        assert!((out[1] - 6.0).abs() < 1e-6);
        assert!((out[2] - 8.0).abs() < 1e-6);
    }
}