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
// SPDX-License-Identifier: AGPL-3.0-or-later

//! GPU-resident transport observables — batched VACF + stress-tensor ACF.
//!
//! **Absorbed from**: hotSpring v0.64 (Feb 2026)
//!
//! ## Architecture
//!
//! During MD production, velocity snapshots are stored in a single flat GPU
//! buffer ([`GpuVelocityRing`]). After production, the batched VACF shader
//! computes C(lag) for each lag in ONE dispatch (iterating over all time
//! origins inside the shader), then reduces to a scalar via
//! [`crate::ops::sum_reduce_f64`]. Total GPU round-trips: `n_lag × 2`.
//!
//! ## Shaders
//!
//! | File | Entry Point | Purpose |
//! |------|-------------|---------|
//! | `vacf_batch_f64.wgsl` | `main` | Batched C(lag) across all origins |
//! | `stress_virial_f64.wgsl` | `main` | Per-particle `σ_xy` for Green-Kubo viscosity |
//!
//! ## Deep Debt Compliance
//!
//! - WGSL shader-first (separate .wgsl files)
//! - Full f64 precision
//! - Zero unsafe code
//! - Capability-based: CPU fallback when no GPU available

use crate::device::WgpuDevice;
use crate::device::capabilities::WORKGROUP_SIZE_COMPACT;
use bytemuck::{Pod, Zeroable};
use std::sync::Arc;

const VACF_BATCH_SHADER: &str =
    include_str!("../../../shaders/md/vacf_batch_per_particle_f64.wgsl");
const STRESS_VIRIAL_SHADER: &str =
    include_str!("../../../shaders/md/stress_virial_per_particle_f64.wgsl");

// ─── Batched VACF ────────────────────────────────────────────────────────────

#[repr(C)]
#[derive(Copy, Clone, Pod, Zeroable)]
struct VacfBatchParams {
    n: u32,
    n_frames: u32,
    lag: u32,
    stride: u32,
}

/// Batched VACF GPU pipeline — computes C(lag) in a single dispatch per lag.
///
/// Replaces `n_frames` individual dispatches with ONE dispatch that iterates
/// over all valid time origins inside the shader.
pub struct VacfBatchGpu {
    device: Arc<WgpuDevice>,
    pipeline: wgpu::ComputePipeline,
    bgl: wgpu::BindGroupLayout,
}

impl VacfBatchGpu {
    /// Create a batched VACF GPU pipeline.
    #[must_use]
    pub fn new(device: Arc<WgpuDevice>) -> Self {
        let module = device.compile_shader_f64(VACF_BATCH_SHADER, Some("vacf_batch_f64"));

        let bgl = device
            .device
            .create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
                label: Some("VacfBatch BGL"),
                entries: &[
                    storage_bgl_entry(0, true),
                    storage_bgl_entry(1, false),
                    uniform_bgl_entry(2),
                ],
            });

        let layout = device
            .device
            .create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
                label: Some("VacfBatch Layout"),
                bind_group_layouts: &[&bgl],
                immediate_size: 0,
            });

        let pipeline = device
            .device
            .create_compute_pipeline(&wgpu::ComputePipelineDescriptor {
                label: Some("VacfBatch Pipeline"),
                layout: Some(&layout),
                module: &module,
                entry_point: Some("main"),
                compilation_options: Default::default(),
                cache: None,
            });

        Self {
            device,
            pipeline,
            bgl,
        }
    }

    /// Compute the per-particle VACF contribution for a single `lag` value.
    ///
    /// `vel_ring_buf` must hold `[n_frames × n_particles × 3]` f64 values.
    /// Returns a buffer of `[n_particles]` f64 values (per-particle C(lag)),
    /// ready for reduction via `ReduceScalarPipeline`.
    pub fn dispatch(
        &self,
        vel_ring_buf: &wgpu::Buffer,
        out_buf: &wgpu::Buffer,
        n_particles: u32,
        n_frames: u32,
        lag: u32,
    ) {
        let stride = n_particles * 3;
        let params = VacfBatchParams {
            n: n_particles,
            n_frames,
            lag,
            stride,
        };
        let params_buf = self
            .device
            .device
            .create_buffer_init(&wgpu::util::BufferInitDescriptor {
                label: Some("VacfBatch Params"),
                contents: bytemuck::bytes_of(&params),
                usage: wgpu::BufferUsages::UNIFORM,
            });

        let bg = self
            .device
            .device
            .create_bind_group(&wgpu::BindGroupDescriptor {
                label: Some("VacfBatch BG"),
                layout: &self.bgl,
                entries: &[
                    wgpu::BindGroupEntry {
                        binding: 0,
                        resource: vel_ring_buf.as_entire_binding(),
                    },
                    wgpu::BindGroupEntry {
                        binding: 1,
                        resource: out_buf.as_entire_binding(),
                    },
                    wgpu::BindGroupEntry {
                        binding: 2,
                        resource: params_buf.as_entire_binding(),
                    },
                ],
            });

        let mut encoder = self
            .device
            .create_encoder_guarded(&wgpu::CommandEncoderDescriptor {
                label: Some("VacfBatch"),
            });
        {
            let mut pass = encoder.begin_compute_pass(&wgpu::ComputePassDescriptor::default());
            pass.set_pipeline(&self.pipeline);
            pass.set_bind_group(0, Some(&bg), &[]);
            pass.dispatch_workgroups(n_particles.div_ceil(WORKGROUP_SIZE_COMPACT), 1, 1);
        }
        self.device.submit_commands(Some(encoder.finish()));
    }
}

// ─── Stress virial ───────────────────────────────────────────────────────────

/// GPU stress tensor `σ_xy` operator for Green-Kubo viscosity.
///
/// Per-particle `σ_xy` = m·vx·vy + Σ_{j≠i} (`F_ij_x` · `r_ij_y)/(2r`)
/// Output is `[N]` f64 values, reduced via `ReduceScalarPipeline`.
pub struct StressVirialGpu {
    device: Arc<WgpuDevice>,
    pipeline: wgpu::ComputePipeline,
    bgl: wgpu::BindGroupLayout,
}

impl StressVirialGpu {
    /// Create a stress-virial GPU pipeline.
    #[must_use]
    pub fn new(device: Arc<WgpuDevice>) -> Self {
        let module = device.compile_shader_f64(STRESS_VIRIAL_SHADER, Some("stress_virial_f64"));

        let bgl = device
            .device
            .create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
                label: Some("StressVirial BGL"),
                entries: &[
                    storage_bgl_entry(0, true),  // positions
                    storage_bgl_entry(1, true),  // velocities
                    storage_bgl_entry(2, false), // out
                    storage_bgl_entry(3, true),  // params
                ],
            });

        let layout = device
            .device
            .create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
                label: Some("StressVirial Layout"),
                bind_group_layouts: &[&bgl],
                immediate_size: 0,
            });

        let pipeline = device
            .device
            .create_compute_pipeline(&wgpu::ComputePipelineDescriptor {
                label: Some("StressVirial Pipeline"),
                layout: Some(&layout),
                module: &module,
                entry_point: Some("main"),
                compilation_options: Default::default(),
                cache: None,
            });

        Self {
            device,
            pipeline,
            bgl,
        }
    }

    /// Dispatch the stress-virial kernel.
    ///
    /// `pos_buf`:  `[N×3]` f64 — particle positions
    /// `vel_buf`:  `[N×3]` f64 — particle velocities
    /// `out_buf`:  `[N]`   f64 — per-particle `σ_xy` contribution
    /// `params`:   simulation parameters packed as `[8]` f64
    pub fn dispatch(
        &self,
        pos_buf: &wgpu::Buffer,
        vel_buf: &wgpu::Buffer,
        out_buf: &wgpu::Buffer,
        params_buf: &wgpu::Buffer,
        n_particles: u32,
    ) {
        let bg = self
            .device
            .device
            .create_bind_group(&wgpu::BindGroupDescriptor {
                label: Some("StressVirial BG"),
                layout: &self.bgl,
                entries: &[
                    wgpu::BindGroupEntry {
                        binding: 0,
                        resource: pos_buf.as_entire_binding(),
                    },
                    wgpu::BindGroupEntry {
                        binding: 1,
                        resource: vel_buf.as_entire_binding(),
                    },
                    wgpu::BindGroupEntry {
                        binding: 2,
                        resource: out_buf.as_entire_binding(),
                    },
                    wgpu::BindGroupEntry {
                        binding: 3,
                        resource: params_buf.as_entire_binding(),
                    },
                ],
            });

        let mut encoder = self
            .device
            .create_encoder_guarded(&wgpu::CommandEncoderDescriptor {
                label: Some("StressVirial"),
            });
        {
            let mut pass = encoder.begin_compute_pass(&wgpu::ComputePassDescriptor::default());
            pass.set_pipeline(&self.pipeline);
            pass.set_bind_group(0, Some(&bg), &[]);
            pass.dispatch_workgroups(n_particles.div_ceil(WORKGROUP_SIZE_COMPACT), 1, 1);
        }
        self.device.submit_commands(Some(encoder.finish()));
    }
}

// ─── GPU velocity ring buffer ────────────────────────────────────────────────

/// GPU-resident velocity ring buffer backed by a single flat wgpu buffer.
///
/// Layout: `vel_flat[snapshot_idx × stride + particle × 3 + component]`
/// where `stride = n_particles × 3`.
///
/// During MD production, velocity snapshots are GPU→GPU copied into sequential
/// slots. After production, the flat buffer is passed directly to
/// [`VacfBatchGpu`] for correlation.
pub struct GpuVelocityRing {
    /// Flat GPU buffer holding all velocity snapshots
    pub flat_buf: wgpu::Buffer,
    /// Number of snapshot slots in the ring
    pub n_slots: usize,
    /// Current write index (ring position)
    pub write_idx: usize,
    /// Total snapshots stored (capped at `n_slots`)
    pub total_stored: usize,
    /// Number of particles per snapshot
    pub n_particles: usize,
    /// Stride per snapshot (`n_particles` * 3)
    pub stride: usize,
}

impl GpuVelocityRing {
    /// Create a velocity ring buffer for the given particle count and slot count.
    #[must_use]
    pub fn new(device: &WgpuDevice, n_particles: usize, n_slots: usize) -> Self {
        let stride = n_particles * 3;
        let total_f64 = n_slots * stride;
        let total_bytes = (total_f64 * 8) as u64;

        let flat_buf = device.device.create_buffer(&wgpu::BufferDescriptor {
            label: Some("vel_ring_flat"),
            size: total_bytes.max(8),
            usage: wgpu::BufferUsages::STORAGE
                | wgpu::BufferUsages::COPY_DST
                | wgpu::BufferUsages::COPY_SRC,
            mapped_at_creation: false,
        });

        Self {
            flat_buf,
            n_slots,
            write_idx: 0,
            total_stored: 0,
            n_particles,
            stride,
        }
    }

    /// Copy a velocity snapshot from `src_buf` into the next ring slot.
    ///
    /// `src_buf` must hold `[N×3]` f64 values (the current velocity array).
    pub fn push_snapshot(&mut self, encoder: &mut wgpu::CommandEncoder, src_buf: &wgpu::Buffer) {
        let offset_bytes = (self.write_idx * self.stride * 8) as u64;
        let size_bytes = (self.stride * 8) as u64;

        encoder.copy_buffer_to_buffer(src_buf, 0, &self.flat_buf, offset_bytes, size_bytes);

        self.write_idx = (self.write_idx + 1) % self.n_slots;
        self.total_stored = (self.total_stored + 1).min(self.n_slots);
    }

    /// Number of snapshots currently stored.
    #[must_use]
    pub fn stored(&self) -> usize {
        self.total_stored
    }
}

// ─── Helpers ─────────────────────────────────────────────────────────────────

fn storage_bgl_entry(binding: u32, read_only: bool) -> wgpu::BindGroupLayoutEntry {
    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,
    }
}

fn uniform_bgl_entry(binding: u32) -> wgpu::BindGroupLayoutEntry {
    wgpu::BindGroupLayoutEntry {
        binding,
        visibility: wgpu::ShaderStages::COMPUTE,
        ty: wgpu::BindingType::Buffer {
            ty: wgpu::BufferBindingType::Uniform,
            has_dynamic_offset: false,
            min_binding_size: None,
        },
        count: None,
    }
}

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

    #[test]
    fn test_velocity_ring_creation() {
        let Some(device) = test_pool::get_test_device_if_f64_gpu_available_sync() else {
            return;
        };
        let ring = GpuVelocityRing::new(&device, 100, 500);
        assert_eq!(ring.n_slots, 500);
        assert_eq!(ring.stride, 300);
        assert_eq!(ring.stored(), 0);
    }

    #[test]
    fn test_vacf_batch_pipeline_creation() {
        let Some(device) = test_pool::get_test_device_if_f64_gpu_available_sync() else {
            return;
        };
        let _pipeline = VacfBatchGpu::new(device);
    }

    #[test]
    fn test_stress_virial_pipeline_creation() {
        let Some(device) = test_pool::get_test_device_if_f64_gpu_available_sync() else {
            return;
        };
        let _pipeline = StressVirialGpu::new(device);
    }
}