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
// SPDX-License-Identifier: AGPL-3.0-or-later
//! Universal Substrate Cache Hierarchy
//!
//! **RUNTIME DISCOVERY, NOT VENDOR HARDCODING**
//!
//! This module discovers cache hierarchies via runtime probing, not hardcoded
//! vendor-specific values. The silicon tells us what it can do.
//!
//! # Design Principles
//!
//! 1. **Probe, don't assume** — Run bandwidth microbenchmarks to find cache boundaries
//! 2. **Adaptive discovery** — Bandwidth inflection points reveal cache levels
//! 3. **Conservative defaults** — If probing fails, use safe generic values
//! 4. **Zero vendor hardcoding** — No "if AMD then 128MB" patterns
//!
//! # Usage
//!
//! ```no_run
//! use barracuda::device::{SubstrateMemoryHierarchy, CacheAwareTiler};
//! use barracuda::prelude::WgpuDevice;
//!
//! # async fn example() -> barracuda::error::Result<()> {
//! let device = WgpuDevice::new().await?;
//! // Discover cache hierarchy via probing
//! let hierarchy = SubstrateMemoryHierarchy::probe(&device).await;
//!
//! // Or use quick estimation from adapter limits
//! let hierarchy = SubstrateMemoryHierarchy::estimate(&device);
//!
//! // Tile workloads to fit cache
//! let tiler = CacheAwareTiler::new(hierarchy);
//! let config = tiler.optimal_tile_size(1024, 8, 3.0);
//! # Ok(())
//! # }
//! ```

use crate::device::capabilities::WORKGROUP_SIZE_1D;
use crate::device::{SubstrateType, WgpuDevice};
use std::time::Instant;
use wgpu::util::DeviceExt;

// ── Device memory estimates (conservative defaults; probing refines) ─────────

const DISCRETE_GPU_CACHE_ESTIMATE: u64 = 32 * 1024 * 1024;
const DISCRETE_GPU_BW_GBS: f64 = 500.0;
const DISCRETE_GPU_VRAM_ESTIMATE: u64 = 16 * 1024 * 1024 * 1024;

const INTEGRATED_GPU_CACHE_ESTIMATE: u64 = 16 * 1024 * 1024;
const INTEGRATED_GPU_BW_GBS: f64 = 100.0;
const INTEGRATED_GPU_VRAM_ESTIMATE: u64 = 8 * 1024 * 1024 * 1024;

const CPU_CACHE_ESTIMATE: u64 = 32 * 1024 * 1024;
const CPU_BW_GBS: f64 = 50.0;
const CPU_VRAM_ESTIMATE: u64 = 64 * 1024 * 1024 * 1024;

const FALLBACK_CACHE_ESTIMATE: u64 = 8 * 1024 * 1024;
const FALLBACK_BW_GBS: f64 = 100.0;
const FALLBACK_VRAM_ESTIMATE: u64 = 8 * 1024 * 1024 * 1024;

/// Bandwidth sample point for cache probing
#[derive(Debug, Clone)]
struct BandwidthSample {
    size_bytes: u64,
    bandwidth_gbs: f64,
}

/// Cache level descriptor — discovered at runtime
#[derive(Debug, Clone)]
pub struct CacheLevel {
    /// Cache level name (L1, L2, L3, Cache, etc.)
    pub name: &'static str,

    /// Size in bytes (discovered via probing)
    pub size_bytes: u64,

    /// Observed bandwidth in GB/s
    pub bandwidth_gbs: f64,

    /// Whether this level is shared across compute units
    pub shared: bool,
}

/// Main memory specification
#[derive(Debug, Clone)]
pub struct MainMemory {
    /// Size in bytes (from adapter limits)
    pub size_bytes: u64,

    /// Observed or theoretical bandwidth in GB/s
    pub bandwidth_gbs: f64,
}

/// Discovered memory hierarchy for a substrate
#[derive(Debug, Clone)]
pub struct SubstrateMemoryHierarchy {
    /// Substrate identifier
    pub substrate_name: String,

    /// Substrate type (GPU, CPU, NPU)
    pub substrate_type: SubstrateType,

    /// Discovered cache levels (smallest/fastest to largest/slowest)
    pub cache_levels: Vec<CacheLevel>,

    /// Main memory (DRAM/VRAM)
    pub main_memory: MainMemory,

    /// Computed optimal tile size
    pub optimal_tile_bytes: u64,

    /// Was this hierarchy probed or estimated?
    pub probed: bool,
}

impl SubstrateMemoryHierarchy {
    /// Quick estimation from device info (no probing)
    ///
    /// Uses device type to estimate cache hierarchy.
    /// This is fast but less accurate than probing.
    #[must_use]
    pub fn estimate(device: &WgpuDevice) -> Self {
        let info = device.adapter_info();

        // Determine substrate type from device info
        let substrate_type = Self::classify_substrate(info);

        // Estimate based on device type, NOT vendor
        // These are conservative defaults — actual probing will refine them
        let (cache_size, main_bw, vram_size) = match info.device_type {
            wgpu::DeviceType::DiscreteGpu => {
                // Discrete GPUs: conservative 32MB cache estimate
                // Real sizes vary from 4MB (old) to 128MB (Infinity Cache)
                // Probing will discover actual boundaries
                (
                    DISCRETE_GPU_CACHE_ESTIMATE,
                    DISCRETE_GPU_BW_GBS,
                    DISCRETE_GPU_VRAM_ESTIMATE,
                )
            }
            wgpu::DeviceType::IntegratedGpu => {
                // iGPUs share system memory, smaller effective cache
                (
                    INTEGRATED_GPU_CACHE_ESTIMATE,
                    INTEGRATED_GPU_BW_GBS,
                    INTEGRATED_GPU_VRAM_ESTIMATE,
                )
            }
            wgpu::DeviceType::Cpu => {
                // CPUs have L3 cache, typically 8-64MB
                (CPU_CACHE_ESTIMATE, CPU_BW_GBS, CPU_VRAM_ESTIMATE)
            }
            _ => {
                // Unknown device type, use conservative defaults
                (
                    FALLBACK_CACHE_ESTIMATE,
                    FALLBACK_BW_GBS,
                    FALLBACK_VRAM_ESTIMATE,
                )
            }
        };

        Self {
            substrate_name: info.name.clone(),
            substrate_type,
            cache_levels: vec![CacheLevel {
                name: "Cache",
                size_bytes: cache_size,
                bandwidth_gbs: main_bw * 2.0, // Cache is typically faster
                shared: true,
            }],
            main_memory: MainMemory {
                size_bytes: vram_size,
                bandwidth_gbs: main_bw,
            },
            optimal_tile_bytes: cache_size / 2, // Target 50% cache utilization
            probed: false,
        }
    }

    /// Probe cache hierarchy via bandwidth microbenchmarks
    ///
    /// This is slower but much more accurate than estimation.
    /// Runs bandwidth tests at increasing data sizes and detects
    /// inflection points that indicate cache boundaries.
    pub async fn probe(device: &WgpuDevice) -> Self {
        let info = device.adapter_info();
        let substrate_type = Self::classify_substrate(info);

        // Probe sizes from 4KB to 512MB (powers of 4)
        let probe_sizes: Vec<u64> = vec![
            4 * 1024,          // 4 KB
            16 * 1024,         // 16 KB
            64 * 1024,         // 64 KB
            256 * 1024,        // 256 KB
            1024 * 1024,       // 1 MB
            4 * 1024 * 1024,   // 4 MB
            16 * 1024 * 1024,  // 16 MB
            64 * 1024 * 1024,  // 64 MB
            256 * 1024 * 1024, // 256 MB
            512 * 1024 * 1024, // 512 MB
        ];

        // Run bandwidth probes
        let mut samples: Vec<BandwidthSample> = Vec::new();
        for &size in &probe_sizes {
            if let Some(bw) = Self::probe_bandwidth(device, size).await {
                samples.push(BandwidthSample {
                    size_bytes: size,
                    bandwidth_gbs: bw,
                });
            }
        }

        // Need at least 3 samples to find inflection points
        if samples.len() < 3 {
            return Self::estimate(device);
        }

        // Find inflection points where bandwidth drops significantly (>20%)
        let mut cache_levels = Vec::new();
        let mut last_cache_bw = samples[0].bandwidth_gbs;

        for pair in samples.windows(2) {
            let (prev, cur) = (&pair[0], &pair[1]);
            let drop_ratio = cur.bandwidth_gbs / last_cache_bw;

            if drop_ratio < 0.8 {
                let level_name = match cache_levels.len() {
                    0 => "L2",
                    1 => "L3",
                    _ => "Cache",
                };

                cache_levels.push(CacheLevel {
                    name: level_name,
                    size_bytes: prev.size_bytes,
                    bandwidth_gbs: last_cache_bw,
                    shared: cache_levels.is_empty(),
                });

                last_cache_bw = cur.bandwidth_gbs;
            }
        }

        // If no inflection points found, use the highest bandwidth point
        if cache_levels.is_empty() {
            let max_sample = samples.iter().max_by(|a, b| {
                a.bandwidth_gbs
                    .partial_cmp(&b.bandwidth_gbs)
                    .unwrap_or(std::cmp::Ordering::Equal)
            });

            if let Some(sample) = max_sample {
                cache_levels.push(CacheLevel {
                    name: "Cache",
                    size_bytes: sample.size_bytes,
                    bandwidth_gbs: sample.bandwidth_gbs,
                    shared: true,
                });
            }
        }

        // VRAM bandwidth from largest probe that succeeded
        let main_bw = samples.last().map_or(100.0, |s| s.bandwidth_gbs);
        let main_memory = MainMemory {
            size_bytes: device.device().limits().max_buffer_size,
            bandwidth_gbs: main_bw,
        };

        // Optimal tile targets ~70% of largest cache
        let optimal_tile = cache_levels
            .last()
            .map_or(16 * 1024 * 1024, |c| (c.size_bytes as f64 * 0.7) as u64);

        Self {
            substrate_name: info.name.clone(),
            substrate_type,
            cache_levels,
            main_memory,
            optimal_tile_bytes: optimal_tile,
            probed: true,
        }
    }

    /// Probe bandwidth at a specific buffer size
    async fn probe_bandwidth(device: &WgpuDevice, size_bytes: u64) -> Option<f64> {
        let elements = (size_bytes / 4) as usize; // f32 = 4 bytes

        // Skip if buffer would exceed device limits
        if size_bytes > device.device().limits().max_buffer_size / 3 {
            return None;
        }

        let shader_src = r"
@group(0) @binding(0) var<storage, read> a: array<f32>;
@group(0) @binding(1) var<storage, read> b: array<f32>;
@group(0) @binding(2) var<storage, read_write> output: array<f32>;

@compute @workgroup_size(256)
fn main(@builtin(global_invocation_id) global_id: vec3<u32>) {
    let idx = global_id.x;
    if (idx >= arrayLength(&output)) {
        return;
    }
    output[idx] = a[idx] + b[idx];
}
";

        let wgpu_device = device.device();

        // Create test buffers
        let data: Vec<f32> = (0..elements).map(|i| i as f32 * 0.001).collect();

        let buf_a = wgpu_device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
            label: Some("probe_a"),
            contents: bytemuck::cast_slice(&data),
            usage: wgpu::BufferUsages::STORAGE,
        });

        let buf_b = wgpu_device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
            label: Some("probe_b"),
            contents: bytemuck::cast_slice(&data),
            usage: wgpu::BufferUsages::STORAGE,
        });

        let buf_out = wgpu_device.create_buffer(&wgpu::BufferDescriptor {
            label: Some("probe_out"),
            size: size_bytes,
            usage: wgpu::BufferUsages::STORAGE,
            mapped_at_creation: false,
        });

        let shader = wgpu_device.create_shader_module(wgpu::ShaderModuleDescriptor {
            label: Some("probe_shader"),
            source: wgpu::ShaderSource::Wgsl(shader_src.into()),
        });

        let bgl_entry = |binding: u32, read_only: bool| wgpu::BindGroupLayoutEntry {
            binding,
            visibility: wgpu::ShaderStages::COMPUTE,
            ty: wgpu::BindingType::Buffer {
                ty: wgpu::BufferBindingType::Storage { read_only },
                has_dynamic_offset: false,
                min_binding_size: None,
            },
            count: None,
        };
        let bgl = wgpu_device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
            label: None,
            entries: &[bgl_entry(0, true), bgl_entry(1, true), bgl_entry(2, false)],
        });

        let bufs = [&buf_a, &buf_b, &buf_out];
        let bind_group = wgpu_device.create_bind_group(&wgpu::BindGroupDescriptor {
            label: None,
            layout: &bgl,
            entries: &bufs
                .iter()
                .enumerate()
                .map(|(i, b)| wgpu::BindGroupEntry {
                    binding: i as u32,
                    resource: b.as_entire_binding(),
                })
                .collect::<Vec<_>>(),
        });

        let pl = wgpu_device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
            label: None,
            bind_group_layouts: &[&bgl],
            immediate_size: 0,
        });
        let pipeline = wgpu_device.create_compute_pipeline(&wgpu::ComputePipelineDescriptor {
            label: None,
            layout: Some(&pl),
            module: &shader,
            entry_point: Some("main"),
            cache: None,
            compilation_options: Default::default(),
        });

        let workgroups = (elements as u32).div_ceil(WORKGROUP_SIZE_1D).min(65_535);

        // Hold WgpuDevice::lock() for all submit/poll via submit_and_poll_inner
        let run_pass = || {
            let mut enc =
                wgpu_device.create_command_encoder(&wgpu::CommandEncoderDescriptor::default());
            {
                let mut p = enc.begin_compute_pass(&wgpu::ComputePassDescriptor::default());
                p.set_pipeline(&pipeline);
                p.set_bind_group(0, Some(&bind_group), &[]);
                p.dispatch_workgroups(workgroups, 1, 1);
            }
            device.submit_and_poll_inner(Some(enc.finish()));
        };

        // Warmup: submit 3 command buffers, then poll once
        let mut warmup_buffers = Vec::with_capacity(3);
        for _ in 0..3 {
            let mut enc =
                wgpu_device.create_command_encoder(&wgpu::CommandEncoderDescriptor::default());
            {
                let mut p = enc.begin_compute_pass(&wgpu::ComputePassDescriptor::default());
                p.set_pipeline(&pipeline);
                p.set_bind_group(0, Some(&bind_group), &[]);
                p.dispatch_workgroups(workgroups, 1, 1);
            }
            warmup_buffers.push(enc.finish());
        }
        device.submit_and_poll_inner(warmup_buffers);

        let iterations = 10;
        let start = Instant::now();
        for _ in 0..iterations {
            run_pass();
        }

        let elapsed = start.elapsed();
        let time_per_op = elapsed.as_secs_f64() / iterations as f64;

        // Bandwidth: (read A + read B + write C) = 3 * size
        let bytes = size_bytes * 3;
        let bandwidth_gbs = bytes as f64 / time_per_op / 1e9;

        Some(bandwidth_gbs)
    }

    /// Discover cache hierarchy (alias for estimate)
    #[must_use]
    pub fn discover(device: &WgpuDevice) -> Self {
        Self::estimate(device)
    }

    /// Classify substrate type from wgpu `DeviceType` (vendor-agnostic).
    fn classify_substrate(info: &wgpu::AdapterInfo) -> SubstrateType {
        match info.device_type {
            wgpu::DeviceType::DiscreteGpu => SubstrateType::DiscreteGpu,
            wgpu::DeviceType::IntegratedGpu => SubstrateType::IntegratedGpu,
            wgpu::DeviceType::Cpu => SubstrateType::Cpu,
            _ => SubstrateType::Other,
        }
    }

    /// Get the largest cache level
    #[must_use]
    pub fn largest_cache(&self) -> Option<&CacheLevel> {
        self.cache_levels.iter().max_by_key(|c| c.size_bytes)
    }

    /// Total cache capacity
    #[must_use]
    pub fn total_cache_bytes(&self) -> u64 {
        self.cache_levels.iter().map(|c| c.size_bytes).sum()
    }
}

/// Cache residency status
#[derive(Debug, Clone)]
pub enum CacheResidency {
    /// Data fits in cache
    Resident {
        /// Cache level name (e.g. "L2", "L3")
        cache_level: &'static str,
        /// Fraction of cache utilized (0.0–1.0)
        utilization: f64,
    },
    /// Data exceeds cache, DRAM-bound
    DramBound {
        /// Bytes that overflow the largest cache
        overflow_bytes: u64,
    },
}

/// Tile configuration for cache-aware execution
#[derive(Debug, Clone)]
pub struct TileConfig {
    /// Elements per tile
    pub tile_elements: usize,
    /// Bytes per tile
    pub tile_bytes: u64,
    /// Number of tiles
    pub num_tiles: usize,
    /// Target cache level
    pub target_cache: &'static str,
    /// Expected bandwidth
    pub expected_bandwidth_gbs: f64,
}

/// Cache-aware workload tiler
pub struct CacheAwareTiler {
    hierarchy: SubstrateMemoryHierarchy,
}

impl CacheAwareTiler {
    /// Create a tiler for a substrate
    #[must_use]
    pub fn new(hierarchy: SubstrateMemoryHierarchy) -> Self {
        Self { hierarchy }
    }

    /// Compute optimal tile size
    ///
    /// # Arguments
    /// * `total_bytes` - Total data size
    /// * `element_size` - Bytes per element
    /// * `read_write_ratio` - Data reuse factor (e.g., 3.0 for A*B+C)
    #[must_use]
    pub fn optimal_tile_size(
        &self,
        total_bytes: u64,
        element_size: usize,
        read_write_ratio: f64,
    ) -> TileConfig {
        let working_set_per_element = (element_size as f64 * read_write_ratio) as u64;

        // Find largest cache that can fit useful tiles
        for cache in self.hierarchy.cache_levels.iter().rev() {
            // Target 70% utilization for headroom
            let usable_cache = (cache.size_bytes as f64 * 0.7) as u64;
            let elements_per_tile = usable_cache / working_set_per_element.max(1);

            if elements_per_tile >= 1024 {
                let tile_bytes = elements_per_tile * element_size as u64;
                let num_tiles = total_bytes.div_ceil(tile_bytes) as usize;

                return TileConfig {
                    tile_elements: elements_per_tile as usize,
                    tile_bytes,
                    num_tiles,
                    target_cache: cache.name,
                    expected_bandwidth_gbs: cache.bandwidth_gbs,
                };
            }
        }

        // Fall back to single pass (DRAM-bound)
        let total_elements = (total_bytes / element_size as u64) as usize;
        TileConfig {
            tile_elements: total_elements,
            tile_bytes: total_bytes,
            num_tiles: 1,
            target_cache: "DRAM",
            expected_bandwidth_gbs: self.hierarchy.main_memory.bandwidth_gbs,
        }
    }

    /// Check if data fits in cache
    #[must_use]
    pub fn is_cache_resident(&self, data_bytes: u64) -> CacheResidency {
        for cache in &self.hierarchy.cache_levels {
            if data_bytes <= cache.size_bytes {
                return CacheResidency::Resident {
                    cache_level: cache.name,
                    utilization: data_bytes as f64 / cache.size_bytes as f64,
                };
            }
        }

        let largest = self
            .hierarchy
            .cache_levels
            .last()
            .map_or(0, |c| c.size_bytes);

        CacheResidency::DramBound {
            overflow_bytes: data_bytes.saturating_sub(largest),
        }
    }

    /// Predict effective bandwidth
    #[must_use]
    pub fn predict_bandwidth(&self, data_bytes: u64) -> f64 {
        match self.is_cache_resident(data_bytes) {
            CacheResidency::Resident { cache_level, .. } => self
                .hierarchy
                .cache_levels
                .iter()
                .find(|c| c.name == cache_level)
                .map_or(self.hierarchy.main_memory.bandwidth_gbs, |c| {
                    c.bandwidth_gbs
                }),
            CacheResidency::DramBound { .. } => {
                // Expect ~80% of theoretical DRAM bandwidth
                self.hierarchy.main_memory.bandwidth_gbs * 0.8
            }
        }
    }
}

impl std::fmt::Display for SubstrateMemoryHierarchy {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        writeln!(
            f,
            "Memory Hierarchy: {} ({})",
            self.substrate_name,
            if self.probed { "probed" } else { "estimated" }
        )?;
        writeln!(f, "  Type: {:?}", self.substrate_type)?;
        for cache in &self.cache_levels {
            writeln!(
                f,
                "  {}: {} MB @ {:.0} GB/s",
                cache.name,
                cache.size_bytes / 1024 / 1024,
                cache.bandwidth_gbs
            )?;
        }
        writeln!(
            f,
            "  DRAM: {:.0} GB/s (theoretical)",
            self.main_memory.bandwidth_gbs
        )?;
        writeln!(
            f,
            "  Optimal tile: {} MB",
            self.optimal_tile_bytes / 1024 / 1024
        )
    }
}

#[cfg(test)]
#[path = "cache_hierarchy_tests.rs"]
mod tests;