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
// SPDX-License-Identifier: AGPL-3.0-or-later
//! Coulomb Force Calculation (f64)
//!
//! **Physics**: Electrostatic interactions between charged particles
//! **Formula**: F = k * `q_i` * `q_j` / r² * r̂
//! **Use Case**: Ions, proteins, charged molecules, nuclei
//!
//! **Deep Debt Compliance**:
//! - ✅ Pure WGSL shader (f64)
//! - ✅ Zero unsafe code
//! - ✅ Capability-based dispatch
//! - ✅ Agnostic (no hardcoded constants)
//!
//! **Precision**: f64 is critical for:
//! - Large systems where small forces accumulate
//! - Nuclear physics (fine structure constant precision)
//! - Long timescale simulations

use crate::device::WgpuDevice;
use crate::device::capabilities::WORKGROUP_SIZE_1D;
use crate::error::{BarracudaError, Result};
use std::sync::Arc;

#[cfg(test)]
mod cpu_reference;

#[cfg(test)]
mod tests;

#[repr(C)]
#[derive(Copy, Clone, bytemuck::Pod, bytemuck::Zeroable)]
struct CoulombParams {
    n_particles: u32,
    _pad0: u32,
    coulomb_constant: f64,
    cutoff_radius: f64,
    cutoff_radius_sq: f64,
    softening: f64,
}

/// Shared GPU buffers for Coulomb calculations.
struct CoulombBuffers {
    pos: wgpu::Buffer,
    charges: wgpu::Buffer,
    forces: wgpu::Buffer,
    params: wgpu::Buffer,
}

impl CoulombBuffers {
    fn new(
        dev: &WgpuDevice,
        positions: &[f64],
        charges: &[f64],
        k: f64,
        cutoff: f64,
        eps: f64,
    ) -> Self {
        let n = charges.len();
        let pos = dev
            .device
            .create_buffer_init(&wgpu::util::BufferInitDescriptor {
                label: Some("Coulomb f64 Positions"),
                contents: bytemuck::cast_slice(positions),
                usage: wgpu::BufferUsages::STORAGE,
            });

        let charges_buf = dev
            .device
            .create_buffer_init(&wgpu::util::BufferInitDescriptor {
                label: Some("Coulomb f64 Charges"),
                contents: bytemuck::cast_slice(charges),
                usage: wgpu::BufferUsages::STORAGE,
            });

        let forces = dev.device.create_buffer(&wgpu::BufferDescriptor {
            label: Some("Coulomb f64 Forces"),
            size: (n * 3 * std::mem::size_of::<f64>()) as u64,
            usage: wgpu::BufferUsages::STORAGE | wgpu::BufferUsages::COPY_SRC,
            mapped_at_creation: false,
        });

        let params = dev
            .device
            .create_buffer_init(&wgpu::util::BufferInitDescriptor {
                label: Some("Coulomb f64 Params"),
                contents: bytemuck::cast_slice(&[CoulombParams {
                    n_particles: n as u32,
                    _pad0: 0,
                    coulomb_constant: k,
                    cutoff_radius: cutoff,
                    cutoff_radius_sq: cutoff * cutoff,
                    softening: eps,
                }]),
                usage: wgpu::BufferUsages::UNIFORM,
            });

        Self {
            pos,
            charges: charges_buf,
            forces,
            params,
        }
    }
}

/// Map a GPU buffer back to CPU as `Vec<f64>`.
fn read_f64_via_staging(
    dev: &WgpuDevice,
    encoder: &mut wgpu::CommandEncoder,
    src: &wgpu::Buffer,
    count: usize,
    label: &str,
) -> (wgpu::Buffer, usize) {
    let size = (count * std::mem::size_of::<f64>()) as u64;
    let staging = dev.device.create_buffer(&wgpu::BufferDescriptor {
        label: Some(label),
        size,
        usage: wgpu::BufferUsages::MAP_READ | wgpu::BufferUsages::COPY_DST,
        mapped_at_creation: false,
    });
    encoder.copy_buffer_to_buffer(src, 0, &staging, 0, size);
    (staging, count)
}

/// f64 Coulomb force calculation operation
///
/// Computes electrostatic forces between all particle pairs.
/// Uses softened potential to avoid singularities.
pub struct CoulombForceF64 {
    device: Arc<WgpuDevice>,
}

impl CoulombForceF64 {
    /// Create new Coulomb f64 force calculation
    /// # Errors
    /// Returns [`Err`] if device initialization fails.
    pub fn new(device: Arc<WgpuDevice>) -> Result<Self> {
        Ok(Self { device })
    }

    fn wgsl_shader() -> &'static str {
        include_str!("coulomb_f64.wgsl")
    }

    /// Execute Coulomb force calculation
    /// # Arguments
    /// * `positions` - Particle positions [N*3] (x,y,z interleaved)
    /// * `charges` - Particle charges [N]
    /// * `coulomb_constant` - Coulomb constant k (default: 1.0)
    /// * `cutoff_radius` - Cutoff distance (default: infinity)
    /// * `softening` - Softening parameter (default: 1e-10)
    /// # Returns
    /// Force vectors [N*3] containing force for each particle
    /// # Errors
    /// Returns [`Err`] if `positions.len() != 3 * charges.len()`, or if buffer allocation, GPU dispatch, or buffer readback fails (e.g. device lost).
    pub fn compute_forces(
        &self,
        positions: &[f64],
        charges: &[f64],
        coulomb_constant: Option<f64>,
        cutoff_radius: Option<f64>,
        softening: Option<f64>,
    ) -> Result<Vec<f64>> {
        let n = charges.len();
        if positions.len() != n * 3 {
            return Err(BarracudaError::InvalidInput {
                message: format!(
                    "Position length {} != 3 * charges length {}",
                    positions.len(),
                    n * 3
                ),
            });
        }

        self.compute_gpu(
            positions,
            charges,
            coulomb_constant.unwrap_or(1.0),
            cutoff_radius.unwrap_or(f64::INFINITY),
            softening.unwrap_or(1e-10),
            "coulomb_f64",
        )
    }

    /// Compute forces with potential energy output
    /// # Errors
    /// Returns [`Err`] if `positions.len() != 3 * charges.len()`, or if buffer allocation, GPU dispatch, or buffer readback fails (e.g. device lost).
    pub fn compute_forces_and_energy(
        &self,
        positions: &[f64],
        charges: &[f64],
        coulomb_constant: Option<f64>,
        cutoff_radius: Option<f64>,
        softening: Option<f64>,
    ) -> Result<(Vec<f64>, Vec<f64>)> {
        let n = charges.len();
        if positions.len() != n * 3 {
            return Err(BarracudaError::InvalidInput {
                message: format!(
                    "Position length {} != 3 * charges length {}",
                    positions.len(),
                    n * 3
                ),
            });
        }

        let k = coulomb_constant.unwrap_or(1.0);
        let cutoff = cutoff_radius.unwrap_or(f64::INFINITY);
        let eps = softening.unwrap_or(1e-10);

        self.compute_gpu_with_energy(positions, charges, k, cutoff, eps)
    }

    fn compute_gpu(
        &self,
        positions: &[f64],
        charges: &[f64],
        k: f64,
        cutoff: f64,
        eps: f64,
        entry_point: &str,
    ) -> Result<Vec<f64>> {
        let n = charges.len();
        let dev = &self.device;
        let bufs = CoulombBuffers::new(dev, positions, charges, k, cutoff, eps);

        let bgl = dev
            .device
            .create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
                label: Some("Coulomb f64 BGL"),
                entries: &(0..4u32)
                    .map(|i| wgpu::BindGroupLayoutEntry {
                        binding: i,
                        visibility: wgpu::ShaderStages::COMPUTE,
                        ty: wgpu::BindingType::Buffer {
                            ty: match i {
                                3 => wgpu::BufferBindingType::Uniform,
                                2 => wgpu::BufferBindingType::Storage { read_only: false },
                                _ => wgpu::BufferBindingType::Storage { read_only: true },
                            },
                            has_dynamic_offset: false,
                            min_binding_size: None,
                        },
                        count: None,
                    })
                    .collect::<Vec<_>>(),
            });

        let bind_bufs: [&wgpu::Buffer; 4] = [&bufs.pos, &bufs.charges, &bufs.forces, &bufs.params];
        let bind_group = dev.device.create_bind_group(&wgpu::BindGroupDescriptor {
            label: Some("Coulomb f64 Bind Group"),
            layout: &bgl,
            entries: &bind_bufs
                .iter()
                .enumerate()
                .map(|(i, b)| wgpu::BindGroupEntry {
                    binding: i as u32,
                    resource: b.as_entire_binding(),
                })
                .collect::<Vec<_>>(),
        });

        let shader = dev.compile_shader_f64(Self::wgsl_shader(), Some("Coulomb f64"));
        let pl = dev
            .device
            .create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
                label: None,
                bind_group_layouts: &[&bgl],
                immediate_size: 0,
            });
        let pipeline = dev
            .device
            .create_compute_pipeline(&wgpu::ComputePipelineDescriptor {
                label: Some("Coulomb f64 Pipeline"),
                layout: Some(&pl),
                module: &shader,
                entry_point: Some(entry_point),
                cache: None,
                compilation_options: Default::default(),
            });

        let mut encoder = dev.create_encoder_guarded(&wgpu::CommandEncoderDescriptor {
            label: Some("Coulomb f64 Encoder"),
        });
        {
            let mut pass = encoder.begin_compute_pass(&wgpu::ComputePassDescriptor::default());
            pass.set_pipeline(&pipeline);
            pass.set_bind_group(0, Some(&bind_group), &[]);
            pass.dispatch_workgroups((n as u32).div_ceil(WORKGROUP_SIZE_1D), 1, 1);
        }

        let (staging, count) =
            read_f64_via_staging(dev, &mut encoder, &bufs.forces, n * 3, "Coulomb Staging");
        dev.submit_commands(Some(encoder.finish()));
        dev.map_staging_buffer::<f64>(&staging, count)
    }

    fn compute_gpu_with_energy(
        &self,
        positions: &[f64],
        charges: &[f64],
        k: f64,
        cutoff: f64,
        eps: f64,
    ) -> Result<(Vec<f64>, Vec<f64>)> {
        let n = charges.len();
        let dev = &self.device;
        let bufs = CoulombBuffers::new(dev, positions, charges, k, cutoff, eps);

        let energy_buf = dev.device.create_buffer(&wgpu::BufferDescriptor {
            label: Some("Coulomb f64 Energy"),
            size: std::mem::size_of_val(charges) as u64,
            usage: wgpu::BufferUsages::STORAGE | wgpu::BufferUsages::COPY_SRC,
            mapped_at_creation: false,
        });

        let bgl = dev
            .device
            .create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
                label: Some("Coulomb f64 Energy BGL"),
                entries: &(0..5u32)
                    .map(|i| wgpu::BindGroupLayoutEntry {
                        binding: i,
                        visibility: wgpu::ShaderStages::COMPUTE,
                        ty: wgpu::BindingType::Buffer {
                            ty: match i {
                                3 => wgpu::BufferBindingType::Uniform,
                                0 | 1 => wgpu::BufferBindingType::Storage { read_only: true },
                                _ => wgpu::BufferBindingType::Storage { read_only: false },
                            },
                            has_dynamic_offset: false,
                            min_binding_size: None,
                        },
                        count: None,
                    })
                    .collect::<Vec<_>>(),
            });

        let bind_bufs: [&wgpu::Buffer; 5] = [
            &bufs.pos,
            &bufs.charges,
            &bufs.forces,
            &bufs.params,
            &energy_buf,
        ];
        let bind_group = dev.device.create_bind_group(&wgpu::BindGroupDescriptor {
            label: Some("Coulomb f64 Energy Bind Group"),
            layout: &bgl,
            entries: &bind_bufs
                .iter()
                .enumerate()
                .map(|(i, b)| wgpu::BindGroupEntry {
                    binding: i as u32,
                    resource: b.as_entire_binding(),
                })
                .collect::<Vec<_>>(),
        });

        let shader = dev.compile_shader_f64(Self::wgsl_shader(), Some("Coulomb f64 Energy"));
        let pl = dev
            .device
            .create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
                label: None,
                bind_group_layouts: &[&bgl],
                immediate_size: 0,
            });
        let pipeline = dev
            .device
            .create_compute_pipeline(&wgpu::ComputePipelineDescriptor {
                label: Some("Coulomb f64 Energy Pipeline"),
                layout: Some(&pl),
                module: &shader,
                entry_point: Some("coulomb_with_energy_f64"),
                cache: None,
                compilation_options: Default::default(),
            });

        let mut encoder = dev.create_encoder_guarded(&wgpu::CommandEncoderDescriptor {
            label: Some("Coulomb f64 Energy Encoder"),
        });
        {
            let mut pass = encoder.begin_compute_pass(&wgpu::ComputePassDescriptor::default());
            pass.set_pipeline(&pipeline);
            pass.set_bind_group(0, Some(&bind_group), &[]);
            pass.dispatch_workgroups((n as u32).div_ceil(WORKGROUP_SIZE_1D), 1, 1);
        }

        let (forces_stg, forces_count) = read_f64_via_staging(
            dev,
            &mut encoder,
            &bufs.forces,
            n * 3,
            "Coulomb Forces Staging",
        );
        let (energy_stg, energy_count) =
            read_f64_via_staging(dev, &mut encoder, &energy_buf, n, "Coulomb Energy Staging");
        dev.submit_commands(Some(encoder.finish()));

        let forces = dev.map_staging_buffer::<f64>(&forces_stg, forces_count)?;
        let energies = dev.map_staging_buffer::<f64>(&energy_stg, energy_count)?;
        Ok((forces, energies))
    }
}