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
// SPDX-License-Identifier: AGPL-3.0-or-later
//! Multi-Kernel Pipeline — GPU Operation Chaining Without CPU Round-Trips
//!
//! Enables chaining GPU operations where the output buffer of one operation
//! becomes the input buffer of the next, without CPU involvement.
//!
//! **Problem**: Traditional pattern requires CPU readback between dependent ops:
//!
//! ```text
//! Current:  H-build → [readback] → eigensolve → [readback] → BCS → [readback] → density
//! ```
//!
//! **Solution**: Chain ops with buffer handles, single GPU submit:
//!
//! ```text
//! Target:   H-build ──────────────> eigensolve ────────────> BCS ────────────> density
//!                    (GPU buffer)              (GPU buffer)       (GPU buffer)
//! ```
//!
//! # Example
//!
//! ```rust,ignore
//! let pipeline = PipelineBuilder::new(&device)
//!     .create_buffer("input", BufferSpec::f64(1000))
//!     .create_buffer("intermediate", BufferSpec::f64(500))
//!     .create_buffer("output", BufferSpec::f64(100))
//!     .add_stage(Stage {
//!         name: "transform",
//!         pipeline: &transform_pipeline,
//!         bind_group_layout: &transform_bgl,
//!         inputs: &["input"],
//!         outputs: &["intermediate"],
//!         workgroups: (4, 1, 1),
//!     })
//!     .add_stage(Stage {
//!         name: "reduce",
//!         pipeline: &reduce_pipeline,
//!         bind_group_layout: &reduce_bgl,
//!         inputs: &["intermediate"],
//!         outputs: &["output"],
//!         workgroups: (1, 1, 1),
//!     })
//!     .build()?;
//!
//! // Upload initial data
//! pipeline.write_buffer("input", &input_data)?;
//!
//! // Execute all stages with single GPU submit
//! pipeline.execute()?;
//!
//! // Read final result
//! let result = pipeline.read_buffer("output")?;
//! ```
//!
//! # Deep Debt Principles
//!
//! - Zero CPU↔GPU round-trips during pipeline execution
//! - Safe Rust (no unsafe code)
//! - Composable stages
//! - Capability-based buffer management

pub mod batched_stateful;
pub mod gpu_view;
pub mod reduce;
pub mod stateful;

pub use gpu_view::{GpuView, GpuViewElement, GpuViewF32, GpuViewF64, GpuViewU32};
pub use reduce::ReduceScalarPipeline;
pub use stateful::{PipelineStage, StatefulPipeline, WaterBalanceState};

use crate::device::WgpuDevice;
use crate::error::{BarracudaError, Result};
use crate::utils::chunk_to_array;
use bytemuck;
use std::collections::HashMap;
use std::sync::Arc;

/// Buffer specification for pipeline allocation
#[derive(Debug, Clone)]
pub struct BufferSpec {
    /// Size in bytes
    pub size: u64,
    /// Buffer usage flags
    pub usage: wgpu::BufferUsages,
    /// Label for debugging
    pub label: Option<String>,
}

impl BufferSpec {
    /// Create a buffer spec for f64 array
    #[must_use]
    pub fn f64(count: usize) -> Self {
        Self {
            size: (count * 8) as u64,
            usage: wgpu::BufferUsages::STORAGE
                | wgpu::BufferUsages::COPY_SRC
                | wgpu::BufferUsages::COPY_DST,
            label: None,
        }
    }

    /// Create a buffer spec for f32 array
    #[must_use]
    pub fn f32(count: usize) -> Self {
        Self {
            size: (count * 4) as u64,
            usage: wgpu::BufferUsages::STORAGE
                | wgpu::BufferUsages::COPY_SRC
                | wgpu::BufferUsages::COPY_DST,
            label: None,
        }
    }

    /// Create a buffer spec for u32 array
    #[must_use]
    pub fn u32(count: usize) -> Self {
        Self {
            size: (count * 4) as u64,
            usage: wgpu::BufferUsages::STORAGE
                | wgpu::BufferUsages::COPY_SRC
                | wgpu::BufferUsages::COPY_DST,
            label: None,
        }
    }

    /// Create a buffer spec with explicit size
    #[must_use]
    pub fn bytes(size: u64) -> Self {
        Self {
            size,
            usage: wgpu::BufferUsages::STORAGE
                | wgpu::BufferUsages::COPY_SRC
                | wgpu::BufferUsages::COPY_DST,
            label: None,
        }
    }

    /// Set label for debugging
    #[must_use]
    pub fn with_label(mut self, label: impl Into<String>) -> Self {
        self.label = Some(label.into());
        self
    }

    /// Add uniform usage (for parameter buffers)
    #[must_use]
    pub fn with_uniform(mut self) -> Self {
        self.usage |= wgpu::BufferUsages::UNIFORM;
        self
    }
}

/// Stage definition for a compute pipeline stage
pub struct Stage {
    /// Stage name for debugging
    pub name: String,
    /// Compute pipeline (shared ownership)
    pub pipeline: Arc<wgpu::ComputePipeline>,
    /// Bind group layout (shared ownership)
    pub bind_group_layout: Arc<wgpu::BindGroupLayout>,
    /// Input buffer names (in binding order)
    pub inputs: Vec<String>,
    /// Output buffer names (in binding order, after inputs)
    pub outputs: Vec<String>,
    /// Workgroup dispatch dimensions
    pub workgroups: (u32, u32, u32),
}

impl Stage {
    /// Create a new stage
    pub fn new(
        name: impl Into<String>,
        pipeline: Arc<wgpu::ComputePipeline>,
        bind_group_layout: Arc<wgpu::BindGroupLayout>,
    ) -> Self {
        Self {
            name: name.into(),
            pipeline,
            bind_group_layout,
            inputs: Vec::new(),
            outputs: Vec::new(),
            workgroups: (1, 1, 1),
        }
    }

    /// Add input buffer names
    #[must_use]
    pub fn with_inputs(mut self, inputs: &[&str]) -> Self {
        self.inputs = inputs
            .iter()
            .map(std::string::ToString::to_string)
            .collect();
        self
    }

    /// Add output buffer names
    #[must_use]
    pub fn with_outputs(mut self, outputs: &[&str]) -> Self {
        self.outputs = outputs
            .iter()
            .map(std::string::ToString::to_string)
            .collect();
        self
    }

    /// Set workgroup dimensions
    #[must_use]
    pub fn with_workgroups(mut self, x: u32, y: u32, z: u32) -> Self {
        self.workgroups = (x, y, z);
        self
    }
}

/// A stage ready for execution (with pre-built bind group)
struct CompiledStage {
    name: String,
    pipeline: Arc<wgpu::ComputePipeline>,
    bind_group: wgpu::BindGroup,
    workgroups: (u32, u32, u32),
}

/// Builder for constructing multi-kernel pipelines
pub struct PipelineBuilder {
    device: Arc<WgpuDevice>,
    buffer_specs: HashMap<String, BufferSpec>,
    stages: Vec<Stage>,
}

impl PipelineBuilder {
    /// Create a new pipeline builder
    #[must_use]
    pub fn new(device: Arc<WgpuDevice>) -> Self {
        Self {
            device,
            buffer_specs: HashMap::new(),
            stages: Vec::new(),
        }
    }

    /// Create a named buffer for the pipeline
    #[must_use]
    pub fn create_buffer(mut self, name: &str, spec: BufferSpec) -> Self {
        self.buffer_specs.insert(name.to_string(), spec);
        self
    }

    /// Add a compute stage to the pipeline
    #[must_use]
    pub fn add_stage(mut self, stage: Stage) -> Self {
        self.stages.push(stage);
        self
    }

    /// Build the pipeline
    /// # Errors
    /// Returns [`Err`] if any stage references an unknown input or output buffer
    /// name. Buffer allocation is infallible in wgpu (OOM surfaces as device lost).
    pub fn build(self) -> Result<ComputePipeline> {
        // Allocate all buffers
        let mut buffers = HashMap::new();
        for (name, spec) in &self.buffer_specs {
            let label = spec
                .label
                .clone()
                .unwrap_or_else(|| format!("Pipeline:{name}"));
            let buffer = self.device.device.create_buffer(&wgpu::BufferDescriptor {
                label: Some(&label),
                size: spec.size,
                usage: spec.usage,
                mapped_at_creation: false,
            });
            buffers.insert(name.clone(), Arc::new(buffer));
        }

        // Build stages with bind groups
        let mut compiled_stages = Vec::new();
        for stage in self.stages {
            // Validate all buffers exist
            for input in &stage.inputs {
                if !buffers.contains_key(input) {
                    return Err(BarracudaError::InvalidInput {
                        message: format!(
                            "Stage '{}' references unknown input buffer '{}'",
                            stage.name, input
                        ),
                    });
                }
            }
            for output in &stage.outputs {
                if !buffers.contains_key(output) {
                    return Err(BarracudaError::InvalidInput {
                        message: format!(
                            "Stage '{}' references unknown output buffer '{}'",
                            stage.name, output
                        ),
                    });
                }
            }

            // Build bind group entries
            let mut entries = Vec::new();
            let mut binding = 0u32;

            // Inputs first
            for input in &stage.inputs {
                entries.push(wgpu::BindGroupEntry {
                    binding,
                    resource: buffers[input].as_entire_binding(),
                });
                binding += 1;
            }

            // Then outputs
            for output in &stage.outputs {
                entries.push(wgpu::BindGroupEntry {
                    binding,
                    resource: buffers[output].as_entire_binding(),
                });
                binding += 1;
            }

            let bind_group = self
                .device
                .device
                .create_bind_group(&wgpu::BindGroupDescriptor {
                    label: Some(&format!("Pipeline:{}:BG", stage.name)),
                    layout: &stage.bind_group_layout,
                    entries: &entries,
                });

            compiled_stages.push(CompiledStage {
                name: stage.name,
                pipeline: stage.pipeline,
                bind_group,
                workgroups: stage.workgroups,
            });
        }

        Ok(ComputePipeline {
            device: self.device,
            buffers,
            stages: compiled_stages,
        })
    }
}

/// An executable multi-kernel compute pipeline
pub struct ComputePipeline {
    device: Arc<WgpuDevice>,
    buffers: HashMap<String, Arc<wgpu::Buffer>>,
    stages: Vec<CompiledStage>,
}

impl ComputePipeline {
    /// Write data to a pipeline buffer
    /// # Errors
    /// Returns [`Err`] if the buffer name is unknown.
    pub fn write_f64(&self, name: &str, data: &[f64]) -> Result<()> {
        let buffer = self
            .buffers
            .get(name)
            .ok_or_else(|| BarracudaError::InvalidInput {
                message: format!("Unknown buffer: {name}"),
            })?;

        let bytes: &[u8] = bytemuck::cast_slice(data);
        self.device.queue.write_buffer(buffer, 0, bytes);
        Ok(())
    }

    /// Write f32 data to a pipeline buffer
    /// # Errors
    /// Returns [`Err`] if the buffer name is unknown.
    pub fn write_f32(&self, name: &str, data: &[f32]) -> Result<()> {
        let buffer = self
            .buffers
            .get(name)
            .ok_or_else(|| BarracudaError::InvalidInput {
                message: format!("Unknown buffer: {name}"),
            })?;

        let bytes: &[u8] = bytemuck::cast_slice(data);
        self.device.queue.write_buffer(buffer, 0, bytes);
        Ok(())
    }

    /// Write raw bytes to a pipeline buffer
    /// # Errors
    /// Returns [`Err`] if the buffer name is unknown.
    pub fn write_bytes(&self, name: &str, data: &[u8]) -> Result<()> {
        let buffer = self
            .buffers
            .get(name)
            .ok_or_else(|| BarracudaError::InvalidInput {
                message: format!("Unknown buffer: {name}"),
            })?;

        self.device.queue.write_buffer(buffer, 0, data);
        Ok(())
    }

    /// Execute all pipeline stages with a single GPU submit
    /// # Errors
    /// Returns [`Err`] if command encoder creation fails, submit fails, or the
    /// device is lost (e.g. during poll).
    pub fn execute(&self) -> Result<()> {
        let mut encoder = self
            .device
            .create_encoder_guarded(&wgpu::CommandEncoderDescriptor {
                label: Some("Pipeline Encoder"),
            });

        for stage in &self.stages {
            let mut pass = encoder.begin_compute_pass(&wgpu::ComputePassDescriptor {
                label: Some(&stage.name),
                timestamp_writes: None,
            });
            pass.set_pipeline(&stage.pipeline);
            pass.set_bind_group(0, Some(&stage.bind_group), &[]);
            pass.dispatch_workgroups(stage.workgroups.0, stage.workgroups.1, stage.workgroups.2);
        }

        self.device.submit_commands(Some(encoder.finish()));
        Ok(())
    }

    /// Read f64 data from a pipeline buffer
    /// # Errors
    /// Returns [`Err`] if the buffer name is unknown, if buffer mapping fails
    /// (e.g. device lost), or if the mapping channel is closed before completion.
    /// # Panics
    /// Panics if the buffer size is not a multiple of 8 bytes (internal invariant
    /// violation; should not occur with correctly-sized f64 buffers).
    pub fn read_f64(&self, name: &str) -> Result<Vec<f64>> {
        let buffer = self
            .buffers
            .get(name)
            .ok_or_else(|| BarracudaError::InvalidInput {
                message: format!("Unknown buffer: {name}"),
            })?;

        let size = buffer.size() as usize;
        let staging = self.device.device.create_buffer(&wgpu::BufferDescriptor {
            label: Some("Pipeline Read Staging"),
            size: size as u64,
            usage: wgpu::BufferUsages::MAP_READ | wgpu::BufferUsages::COPY_DST,
            mapped_at_creation: false,
        });

        let mut encoder = self
            .device
            .create_encoder_guarded(&wgpu::CommandEncoderDescriptor {
                label: Some("Pipeline Read"),
            });
        encoder.copy_buffer_to_buffer(buffer, 0, &staging, 0, size as u64);
        self.device.submit_commands(Some(encoder.finish()));

        let slice = staging.slice(..);
        let (sender, receiver) = std::sync::mpsc::channel();
        slice.map_async(wgpu::MapMode::Read, move |result| {
            let _ = sender.send(result);
        });
        self.device.poll_safe()?;
        receiver
            .recv()
            .map_err(|_| BarracudaError::execution_failed("GPU buffer mapping channel closed"))?
            .map_err(|e| BarracudaError::execution_failed(e.to_string()))?;

        let data = slice.get_mapped_range();
        let result: Vec<f64> = data
            .chunks_exact(8)
            .map(|chunk| chunk_to_array::<8>(chunk).map(f64::from_le_bytes))
            .collect::<Result<Vec<_>>>()?;
        drop(data);
        staging.unmap();

        Ok(result)
    }

    /// Read f32 data from a pipeline buffer
    /// # Errors
    /// Returns [`Err`] if the buffer name is unknown, if buffer mapping fails
    /// (e.g. device lost), or if the mapping channel is closed before completion.
    /// # Panics
    /// Panics if the buffer size is not a multiple of 4 bytes (internal invariant
    /// violation; should not occur with correctly-sized f32 buffers).
    pub fn read_f32(&self, name: &str) -> Result<Vec<f32>> {
        let buffer = self
            .buffers
            .get(name)
            .ok_or_else(|| BarracudaError::InvalidInput {
                message: format!("Unknown buffer: {name}"),
            })?;

        let size = buffer.size() as usize;
        let staging = self.device.device.create_buffer(&wgpu::BufferDescriptor {
            label: Some("Pipeline Read Staging"),
            size: size as u64,
            usage: wgpu::BufferUsages::MAP_READ | wgpu::BufferUsages::COPY_DST,
            mapped_at_creation: false,
        });

        let mut encoder = self
            .device
            .create_encoder_guarded(&wgpu::CommandEncoderDescriptor {
                label: Some("Pipeline Read"),
            });
        encoder.copy_buffer_to_buffer(buffer, 0, &staging, 0, size as u64);
        self.device.submit_commands(Some(encoder.finish()));

        let slice = staging.slice(..);
        let (sender, receiver) = std::sync::mpsc::channel();
        slice.map_async(wgpu::MapMode::Read, move |result| {
            let _ = sender.send(result);
        });
        self.device.poll_safe()?;
        receiver
            .recv()
            .map_err(|_| BarracudaError::execution_failed("GPU buffer mapping channel closed"))?
            .map_err(|e| BarracudaError::execution_failed(e.to_string()))?;

        let data = slice.get_mapped_range();
        let result: Vec<f32> = data
            .chunks_exact(4)
            .map(|chunk| chunk_to_array::<4>(chunk).map(f32::from_le_bytes))
            .collect::<Result<Vec<_>>>()?;
        drop(data);
        staging.unmap();

        Ok(result)
    }

    /// Get a buffer reference (for use with external bind groups)
    #[must_use]
    pub fn buffer(&self, name: &str) -> Option<&wgpu::Buffer> {
        self.buffers.get(name).map(std::convert::AsRef::as_ref)
    }

    /// Get stage count
    #[must_use]
    pub fn stage_count(&self) -> usize {
        self.stages.len()
    }

    /// Get buffer count
    #[must_use]
    pub fn buffer_count(&self) -> usize {
        self.buffers.len()
    }
}

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

    // Note: Full integration tests require actual compute pipelines.
    // These tests verify the builder API and buffer management.

    #[test]
    fn test_buffer_spec_f64() {
        let spec = BufferSpec::f64(100);
        assert_eq!(spec.size, 800); // 100 * 8 bytes
    }

    #[test]
    fn test_buffer_spec_f32() {
        let spec = BufferSpec::f32(100);
        assert_eq!(spec.size, 400); // 100 * 4 bytes
    }

    #[test]
    fn test_buffer_spec_with_label() {
        let spec = BufferSpec::f64(10).with_label("my_buffer");
        assert_eq!(spec.label, Some("my_buffer".to_string()));
    }

    #[test]
    fn test_buffer_spec_with_uniform() {
        let spec = BufferSpec::f64(10).with_uniform();
        assert!(spec.usage.contains(wgpu::BufferUsages::UNIFORM));
        assert!(spec.usage.contains(wgpu::BufferUsages::STORAGE));
    }
}