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
// SPDX-License-Identifier: AGPL-3.0-or-later
//! `WrightFisherF32` — GPU-vectorized Wright-Fisher population genetics.
//!
//! Simulates allele frequency evolution under selection + genetic drift for
//! `n_pops × n_loci` (population, locus) pairs in parallel on the GPU.
//!
//! Each generation:
//! 1. **Selection**: `p' = p * (1+s) / (p * (1+s) + (1-p))`
//! 2. **Drift**: Binomial(`2N`, `p'`) via sequential PRNG sampling
//!
//! Uses xoshiro128** PRNG seeded per-thread from a state buffer.
//!
//! Absorbed from neuralSpring `metalForge/shaders/wright_fisher_step.wgsl`
//! (Papers 024/025 — pangenome selection, meta-population dynamics).

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

#[repr(C)]
#[derive(Copy, Clone, Pod, Zeroable)]
struct WrightFisherParams {
    n_pops: u32,
    n_loci: u32,
    two_n: u32,
    _pad: u32,
}

/// Configuration for a Wright-Fisher simulation.
pub struct WrightFisherConfig {
    /// Number of populations.
    pub n_pops: u32,
    /// Number of loci per population.
    pub n_loci: u32,
    /// Diploid population size (2N total gene copies).
    pub two_n: u32,
}

/// GPU-vectorized Wright-Fisher drift + selection simulation.
///
/// Each GPU thread handles one (population, locus) pair per generation.
/// Call [`simulate_generation`](Self::simulate_generation) repeatedly for
/// multi-generation trajectories.
pub struct WrightFisherF32 {
    device: Arc<WgpuDevice>,
    config: WrightFisherConfig,
}

impl WrightFisherF32 {
    fn wgsl_shader() -> &'static str {
        include_str!("../shaders/science/wright_fisher_step_f32.wgsl")
    }

    /// Create a new Wright-Fisher simulation.
    ///
    /// # Errors
    ///
    /// Returns an error if `n_pops == 0`, `n_loci == 0`, or `two_n == 0`.
    pub fn new(device: Arc<WgpuDevice>, config: WrightFisherConfig) -> Result<Self> {
        if config.n_pops == 0 || config.n_loci == 0 {
            return Err(BarracudaError::InvalidInput {
                message: "WrightFisherF32: n_pops and n_loci must be > 0".into(),
            });
        }
        if config.two_n == 0 {
            return Err(BarracudaError::InvalidInput {
                message: "WrightFisherF32: two_n must be > 0".into(),
            });
        }
        Ok(Self { device, config })
    }

    /// Run one generation of Wright-Fisher selection + drift.
    ///
    /// # Arguments
    ///
    /// * `freq_in` — Current allele frequencies, length `n_pops × n_loci`, in `[0, 1]`.
    /// * `selection` — Selection coefficients per locus, length `n_loci`.
    ///   `s > 0` = advantageous, `s = 0` = neutral, `s < 0` = deleterious.
    /// * `prng_state` — Mutable xoshiro128** state, length `4 × n_pops × n_loci`.
    ///   Updated in-place each generation.
    ///
    /// Returns new allele frequencies of length `n_pops × n_loci`.
    ///
    /// # Errors
    ///
    /// Returns an error if input lengths are wrong or GPU dispatch fails.
    pub fn simulate_generation(
        &self,
        freq_in: &[f32],
        selection: &[f32],
        prng_state: &mut Vec<u32>,
    ) -> Result<Vec<f32>> {
        let total = (self.config.n_pops as usize) * (self.config.n_loci as usize);
        if freq_in.len() != total {
            return Err(BarracudaError::InvalidInput {
                message: format!(
                    "freq_in length {} != n_pops*n_loci = {total}",
                    freq_in.len()
                ),
            });
        }
        if selection.len() != self.config.n_loci as usize {
            return Err(BarracudaError::InvalidInput {
                message: format!(
                    "selection length {} != n_loci = {}",
                    selection.len(),
                    self.config.n_loci
                ),
            });
        }
        let expected_prng = total * 4;
        if prng_state.len() != expected_prng {
            return Err(BarracudaError::InvalidInput {
                message: format!(
                    "prng_state length {} != 4*n_pops*n_loci = {expected_prng}",
                    prng_state.len()
                ),
            });
        }

        let dev = &self.device;
        let params = WrightFisherParams {
            n_pops: self.config.n_pops,
            n_loci: self.config.n_loci,
            two_n: self.config.two_n,
            _pad: 0,
        };

        let freq_buf = dev
            .device
            .create_buffer_init(&wgpu::util::BufferInitDescriptor {
                label: Some("WF freq_in"),
                contents: bytemuck::cast_slice(freq_in),
                usage: wgpu::BufferUsages::STORAGE,
            });
        let sel_buf = dev
            .device
            .create_buffer_init(&wgpu::util::BufferInitDescriptor {
                label: Some("WF selection"),
                contents: bytemuck::cast_slice(selection),
                usage: wgpu::BufferUsages::STORAGE,
            });
        let out_buf = dev.device.create_buffer(&wgpu::BufferDescriptor {
            label: Some("WF freq_out"),
            size: (total * std::mem::size_of::<f32>()) as u64,
            usage: wgpu::BufferUsages::STORAGE | wgpu::BufferUsages::COPY_SRC,
            mapped_at_creation: false,
        });
        let prng_buf = dev
            .device
            .create_buffer_init(&wgpu::util::BufferInitDescriptor {
                label: Some("WF prng_state"),
                contents: bytemuck::cast_slice(prng_state),
                usage: wgpu::BufferUsages::STORAGE | wgpu::BufferUsages::COPY_SRC,
            });
        let param_buf = dev
            .device
            .create_buffer_init(&wgpu::util::BufferInitDescriptor {
                label: Some("WF params"),
                contents: bytemuck::bytes_of(&params),
                usage: wgpu::BufferUsages::UNIFORM,
            });

        let bgl = dev
            .device
            .create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
                label: Some("WF BGL"),
                entries: &[
                    bgl_storage_ro(0),
                    bgl_storage_ro(1),
                    bgl_storage_rw(2),
                    bgl_storage_rw(3),
                    bgl_uniform(4),
                ],
            });

        let bg = dev.device.create_bind_group(&wgpu::BindGroupDescriptor {
            label: Some("WF BG"),
            layout: &bgl,
            entries: &[
                wgpu::BindGroupEntry {
                    binding: 0,
                    resource: freq_buf.as_entire_binding(),
                },
                wgpu::BindGroupEntry {
                    binding: 1,
                    resource: sel_buf.as_entire_binding(),
                },
                wgpu::BindGroupEntry {
                    binding: 2,
                    resource: out_buf.as_entire_binding(),
                },
                wgpu::BindGroupEntry {
                    binding: 3,
                    resource: prng_buf.as_entire_binding(),
                },
                wgpu::BindGroupEntry {
                    binding: 4,
                    resource: param_buf.as_entire_binding(),
                },
            ],
        });

        let shader = dev
            .device
            .create_shader_module(wgpu::ShaderModuleDescriptor {
                label: Some("WF Shader"),
                source: wgpu::ShaderSource::Wgsl(Self::wgsl_shader().into()),
            });

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

        let wg = (total as u32).div_ceil(WORKGROUP_SIZE_1D);
        let mut encoder = dev.create_encoder_guarded(&wgpu::CommandEncoderDescriptor {
            label: Some("WF Encoder"),
        });
        {
            let mut pass = encoder.begin_compute_pass(&wgpu::ComputePassDescriptor {
                label: Some("WF Pass"),
                timestamp_writes: None,
            });
            pass.set_pipeline(&pipeline);
            pass.set_bind_group(0, Some(&bg), &[]);
            pass.dispatch_workgroups(wg, 1, 1);
        }
        dev.submit_commands(Some(encoder.finish()));

        let freq_out = crate::utils::read_buffer(dev, &out_buf, total)?;
        let new_prng = crate::utils::read_buffer_u32(dev, &prng_buf, expected_prng)?;
        *prng_state = new_prng;

        Ok(freq_out)
    }
}

/// Seed xoshiro128** state for `n` threads using `SplitMix32`.
///
/// Each thread gets 4 independent u32 state values.
#[must_use]
pub fn seed_xoshiro_state(base_seed: u32, n: usize) -> Vec<u32> {
    let mut state = Vec::with_capacity(n * 4);
    for i in 0..n {
        let mut s = base_seed.wrapping_add(i as u32);
        for _ in 0..4 {
            s = splitmix32(s);
            state.push(s);
        }
    }
    state
}

fn splitmix32(mut z: u32) -> u32 {
    z = z.wrapping_add(0x9E37_79B9);
    z = (z ^ (z >> 15)).wrapping_mul(0x85EB_CA6B);
    z = (z ^ (z >> 13)).wrapping_mul(0xC2B2_AE35);
    z ^ (z >> 16)
}

fn bgl_storage_ro(idx: u32) -> wgpu::BindGroupLayoutEntry {
    wgpu::BindGroupLayoutEntry {
        binding: idx,
        visibility: wgpu::ShaderStages::COMPUTE,
        ty: wgpu::BindingType::Buffer {
            ty: wgpu::BufferBindingType::Storage { read_only: true },
            has_dynamic_offset: false,
            min_binding_size: None,
        },
        count: None,
    }
}

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

fn bgl_uniform(idx: u32) -> wgpu::BindGroupLayoutEntry {
    wgpu::BindGroupLayoutEntry {
        binding: idx,
        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::*;

    #[test]
    fn seed_state_length() {
        let state = seed_xoshiro_state(42, 10);
        assert_eq!(state.len(), 40);
    }

    #[test]
    fn seed_state_nonzero() {
        let state = seed_xoshiro_state(0, 100);
        assert!(state.iter().any(|&s| s != 0), "all zeros is degenerate");
    }

    #[test]
    fn seed_state_deterministic() {
        let a = seed_xoshiro_state(123, 50);
        let b = seed_xoshiro_state(123, 50);
        assert_eq!(a, b);
    }

    #[tokio::test]
    async fn wright_fisher_neutral_drift() {
        let device = crate::device::test_pool::get_test_device().await;
        let n_pops = 4;
        let n_loci = 8;
        let total = n_pops * n_loci;
        let wf = WrightFisherF32::new(
            device,
            WrightFisherConfig {
                n_pops: n_pops as u32,
                n_loci: n_loci as u32,
                two_n: 100,
            },
        )
        .unwrap();

        let freq_in = vec![0.5_f32; total];
        let selection = vec![0.0_f32; n_loci];
        let mut prng = seed_xoshiro_state(42, total);

        let freq_out = wf
            .simulate_generation(&freq_in, &selection, &mut prng)
            .unwrap();

        assert_eq!(freq_out.len(), total);
        for &f in &freq_out {
            assert!((0.0..=1.0).contains(&f), "frequency {f} out of [0,1]");
        }
    }

    #[tokio::test]
    async fn wright_fisher_strong_selection() {
        let device = crate::device::test_pool::get_test_device().await;
        let n_pops = 10;
        let n_loci = 1;
        let total = n_pops * n_loci;
        let wf = WrightFisherF32::new(
            device,
            WrightFisherConfig {
                n_pops: n_pops as u32,
                n_loci: n_loci as u32,
                two_n: 1000,
            },
        )
        .unwrap();

        let freq_in = vec![0.5_f32; total];
        let selection = vec![0.5_f32; n_loci]; // very strong positive selection
        let mut prng = seed_xoshiro_state(99, total);

        let freq_out = wf
            .simulate_generation(&freq_in, &selection, &mut prng)
            .unwrap();

        // With s=0.5 and 2N=1000, most frequencies should increase from 0.5
        let mean_freq: f32 = freq_out.iter().sum::<f32>() / freq_out.len() as f32;
        assert!(
            mean_freq > 0.55,
            "strong selection should increase frequency; mean = {mean_freq}"
        );
    }

    #[tokio::test]
    async fn wright_fisher_fixation() {
        let device = crate::device::test_pool::get_test_device().await;
        let wf = WrightFisherF32::new(
            device,
            WrightFisherConfig {
                n_pops: 1,
                n_loci: 1,
                two_n: 20,
            },
        )
        .unwrap();

        let selection = vec![0.0_f32]; // neutral
        let mut prng = seed_xoshiro_state(7, 1);
        let mut freq = vec![0.5_f32];

        // Validate dispatch works — run a few generations, check output is valid
        for _ in 0..5 {
            freq = wf
                .simulate_generation(&freq, &selection, &mut prng)
                .unwrap();
        }

        assert!(
            (0.0..=1.0).contains(&freq[0]),
            "frequency must be in [0, 1], got {}",
            freq[0]
        );
    }
}