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
// SPDX-License-Identifier: AGPL-3.0-or-later
//! Broyden Mixing GPU Implementation (f64)
//!
//! GPU-accelerated vector mixing for SCF convergence.
//! Uses WGSL shaders for f64 precision on all GPU hardware.

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

/// Mixing parameters for SCF solvers
#[derive(Debug, Clone)]
pub struct MixingParams {
    /// Mixing parameter α (typically 0.3-0.7)
    pub alpha: f64,
    /// Optional minimum value (for non-negative quantities like density)
    pub clamp_min: Option<f64>,
    /// Optional maximum value
    pub clamp_max: Option<f64>,
    /// Number of warmup iterations with linear mixing before Broyden
    pub n_warmup: usize,
}

impl Default for MixingParams {
    fn default() -> Self {
        Self {
            alpha: 0.4,
            clamp_min: None,
            clamp_max: None,
            n_warmup: 3,
        }
    }
}

/// Linear mixer for simple damped iteration
///
/// `x_new` = (1-α)·x_old + `α·x_computed`
pub struct LinearMixer {
    device: Arc<WgpuDevice>,
    pipeline: wgpu::ComputePipeline,
    bind_group_layout: wgpu::BindGroupLayout,
    vec_dim: usize,
    params: MixingParams,
}

impl LinearMixer {
    /// Create a new linear mixer
    /// # Errors
    /// Returns [`Err`] if `vec_dim` is zero, or if shader compilation or pipeline
    /// creation fails.
    pub fn new(device: Arc<WgpuDevice>, vec_dim: usize, params: MixingParams) -> Result<Self> {
        if vec_dim == 0 {
            return Err(crate::error::BarracudaError::invalid_op(
                "LinearMixer",
                "vec_dim must be > 0 (zero-length buffers are invalid for GPU compute)",
            ));
        }
        let shader_source = include_str!("../../shaders/mixing/broyden_f64.wgsl");
        let shader_module = device.compile_shader_f64(shader_source, Some("linear_mixer_shader"));

        let bind_group_layout =
            device
                .device()
                .create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
                    label: Some("linear_mixer_bgl"),
                    entries: &[
                        // params uniform
                        wgpu::BindGroupLayoutEntry {
                            binding: 0,
                            visibility: wgpu::ShaderStages::COMPUTE,
                            ty: wgpu::BindingType::Buffer {
                                ty: wgpu::BufferBindingType::Uniform,
                                has_dynamic_offset: false,
                                min_binding_size: None,
                            },
                            count: None,
                        },
                        // old_vec
                        wgpu::BindGroupLayoutEntry {
                            binding: 1,
                            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,
                        },
                        // computed_vec
                        wgpu::BindGroupLayoutEntry {
                            binding: 2,
                            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,
                        },
                        // output_vec
                        wgpu::BindGroupLayoutEntry {
                            binding: 3,
                            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,
                        },
                    ],
                });

        let pipeline_layout =
            device
                .device()
                .create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
                    label: Some("linear_mixer_layout"),
                    bind_group_layouts: &[&bind_group_layout],
                    immediate_size: 0,
                });

        let pipeline = device
            .device()
            .create_compute_pipeline(&wgpu::ComputePipelineDescriptor {
                label: Some("linear_mixer_pipeline"),
                layout: Some(&pipeline_layout),
                module: &shader_module,
                entry_point: Some("mix_linear"),
                cache: None,
                compilation_options: Default::default(),
            });

        Ok(Self {
            device,
            pipeline,
            bind_group_layout,
            vec_dim,
            params,
        })
    }

    /// Mix two vectors: `x_new` = (1-α)·x_old + `α·x_computed`
    /// # Errors
    /// Returns [`Err`] if vector lengths do not match `vec_dim`, buffer allocation
    /// fails, GPU dispatch fails, or buffer mapping/readback fails (e.g. device lost).
    pub async fn mix(&self, x_old: &[f64], x_computed: &[f64]) -> Result<Vec<f64>> {
        if x_old.len() != self.vec_dim || x_computed.len() != self.vec_dim {
            return Err(BarracudaError::InvalidInput {
                message: format!(
                    "Vector dimension mismatch: expected {}, got {} and {}",
                    self.vec_dim,
                    x_old.len(),
                    x_computed.len()
                ),
            });
        }

        // Create uniform params buffer
        #[repr(C)]
        #[derive(Copy, Clone, bytemuck::Pod, bytemuck::Zeroable)]
        struct LinearParamsGpu {
            vec_dim: u32,
            _pad0: u32,
            _pad1: u32,
            _pad2: u32,
            alpha: f64,
            clamp_min: f64,
            clamp_max: f64,
        }

        let params_data = LinearParamsGpu {
            vec_dim: self.vec_dim as u32,
            _pad0: 0,
            _pad1: 0,
            _pad2: 0,
            alpha: self.params.alpha,
            clamp_min: self.params.clamp_min.unwrap_or(-1e308),
            clamp_max: self.params.clamp_max.unwrap_or(1e308),
        };

        let params_buffer =
            self.device
                .device()
                .create_buffer_init(&wgpu::util::BufferInitDescriptor {
                    label: Some("linear_params"),
                    contents: bytemuck::bytes_of(&params_data),
                    usage: wgpu::BufferUsages::UNIFORM,
                });

        let old_buffer =
            self.device
                .device()
                .create_buffer_init(&wgpu::util::BufferInitDescriptor {
                    label: Some("old_vec"),
                    contents: bytemuck::cast_slice(x_old),
                    usage: wgpu::BufferUsages::STORAGE,
                });

        let computed_buffer =
            self.device
                .device()
                .create_buffer_init(&wgpu::util::BufferInitDescriptor {
                    label: Some("computed_vec"),
                    contents: bytemuck::cast_slice(x_computed),
                    usage: wgpu::BufferUsages::STORAGE,
                });

        let output_buffer = self.device.device().create_buffer(&wgpu::BufferDescriptor {
            label: Some("output_vec"),
            size: (self.vec_dim * std::mem::size_of::<f64>()) as u64,
            usage: wgpu::BufferUsages::STORAGE | wgpu::BufferUsages::COPY_SRC,
            mapped_at_creation: false,
        });

        let bind_group = self
            .device
            .device()
            .create_bind_group(&wgpu::BindGroupDescriptor {
                label: Some("linear_mixer_bg"),
                layout: &self.bind_group_layout,
                entries: &[
                    wgpu::BindGroupEntry {
                        binding: 0,
                        resource: params_buffer.as_entire_binding(),
                    },
                    wgpu::BindGroupEntry {
                        binding: 1,
                        resource: old_buffer.as_entire_binding(),
                    },
                    wgpu::BindGroupEntry {
                        binding: 2,
                        resource: computed_buffer.as_entire_binding(),
                    },
                    wgpu::BindGroupEntry {
                        binding: 3,
                        resource: output_buffer.as_entire_binding(),
                    },
                ],
            });

        // Dispatch
        let mut encoder = self
            .device
            .create_encoder_guarded(&wgpu::CommandEncoderDescriptor { label: None });
        {
            let mut pass = encoder.begin_compute_pass(&wgpu::ComputePassDescriptor {
                label: Some("linear_mix"),
                timestamp_writes: None,
            });
            pass.set_pipeline(&self.pipeline);
            pass.set_bind_group(0, Some(&bind_group), &[]);
            pass.dispatch_workgroups(
                self.vec_dim.div_ceil(WORKGROUP_SIZE_1D as usize) as u32,
                1,
                1,
            );
        }

        // Read back
        let staging_buffer = self.device.device().create_buffer(&wgpu::BufferDescriptor {
            label: Some("staging"),
            size: (self.vec_dim * std::mem::size_of::<f64>()) as u64,
            usage: wgpu::BufferUsages::MAP_READ | wgpu::BufferUsages::COPY_DST,
            mapped_at_creation: false,
        });
        encoder.copy_buffer_to_buffer(
            &output_buffer,
            0,
            &staging_buffer,
            0,
            (self.vec_dim * std::mem::size_of::<f64>()) as u64,
        );
        self.device.submit_commands(Some(encoder.finish()));

        self.device
            .map_staging_buffer::<f64>(&staging_buffer, self.vec_dim)
    }
}

/// Broyden mixer with history for accelerated SCF convergence
///
/// Modified Broyden II algorithm:
/// x_{n+1} = `x_n` + `α·r_n` - `Σ_m` `γ_m·(Δx_m` + `α·Δr_m`)
pub struct BroydenMixer {
    device: Arc<WgpuDevice>,
    linear_mixer: LinearMixer,
    vec_dim: usize,
    max_history: usize,
    params: MixingParams,
    // History storage (CPU side - small linear algebra done on CPU)
    dx_history: Vec<Vec<f64>>,
    df_history: Vec<Vec<f64>>,
    iteration: usize,
}

impl BroydenMixer {
    /// Create a new Broyden mixer
    /// # Arguments
    /// * `device` - GPU device
    /// * `vec_dim` - Dimension of vectors to mix
    /// * `max_history` - Maximum number of history vectors (typically 5-10)
    /// * `params` - Mixing parameters
    /// # Errors
    /// Returns [`Err`] if `vec_dim` is zero, or if the underlying linear mixer
    /// creation fails (shader compilation, pipeline creation).
    pub fn new(
        device: Arc<WgpuDevice>,
        vec_dim: usize,
        max_history: usize,
        params: MixingParams,
    ) -> Result<Self> {
        let linear_mixer = LinearMixer::new(device.clone(), vec_dim, params.clone())?;

        Ok(Self {
            device,
            linear_mixer,
            vec_dim,
            max_history,
            params,
            dx_history: Vec::with_capacity(max_history),
            df_history: Vec::with_capacity(max_history),
            iteration: 0,
        })
    }

    /// GPU device handle (for future GPU-accelerated Broyden update).
    #[must_use]
    pub fn device(&self) -> &Arc<WgpuDevice> {
        &self.device
    }

    /// Dimension of the vectors being mixed.
    #[must_use]
    pub fn vec_dim(&self) -> usize {
        self.vec_dim
    }

    /// Reset the mixer for a new SCF calculation
    pub fn reset(&mut self) {
        self.dx_history.clear();
        self.df_history.clear();
        self.iteration = 0;
    }

    /// Perform one mixing step
    /// During warmup (first `n_warmup` iterations), uses linear mixing.
    /// After warmup, uses full Broyden with history.
    /// # Arguments
    /// * `x_old` - Input from previous iteration
    /// * `x_new` - Output from `F(x_old)`
    /// # Returns
    /// Mixed vector x_{n+1}
    /// # Errors
    /// Returns [`Err`] if vector lengths do not match, buffer allocation fails,
    /// GPU dispatch fails, buffer readback fails, or the Broyden gamma computation
    /// fails (e.g. device lost).
    pub async fn mix(&mut self, x_old: &[f64], x_new: &[f64]) -> Result<Vec<f64>> {
        self.iteration += 1;

        // Compute residual: r = x_new - x_old
        let residual: Vec<f64> = x_new.iter().zip(x_old).map(|(a, b)| a - b).collect();

        // Use linear mixing during warmup
        if self.iteration <= self.params.n_warmup || self.dx_history.is_empty() {
            let result = self.linear_mixer.mix(x_old, x_new).await?;

            // Store history for next iteration (if we have a previous iteration)
            if self.iteration > 1 && self.dx_history.len() < self.max_history {
                // During warmup, the Jacobian inverse approximation has
                // insufficient rank for useful Broyden updates — linear
                // mixing provides better conditioning until enough
                // (x, residual) pairs populate the history.
            }

            return Ok(result);
        }

        // Full Broyden mixing
        // Compute γ coefficients on CPU (small matrix operations)
        let gammas = self.compute_broyden_gammas(&residual)?;

        // Apply Broyden update on GPU
        let result = self.broyden_update_gpu(x_old, &residual, &gammas).await?;

        // Update history
        self.update_history(x_old, &result, &residual);

        Ok(result)
    }

    /// Compute Broyden γ coefficients via least-squares on CPU.
    /// Solves A·γ = β where `A_ij` = <`ΔF_i|ΔF_j`>, `β_i` = <`ΔF_i|r`>.
    /// This is `O(n_history²)` work on a tiny matrix — CPU is appropriate.
    fn compute_broyden_gammas(&self, residual: &[f64]) -> Result<Vec<f64>> {
        let m = self.df_history.len();
        if m == 0 {
            return Ok(Vec::new());
        }

        // Build overlap matrix A_ij = <ΔF_i|ΔF_j> and RHS β_i = <ΔF_i|r>
        let mut a = vec![0.0f64; m * m];
        let mut beta = vec![0.0f64; m];

        for i in 0..m {
            beta[i] = dot(&self.df_history[i], residual);
            for j in 0..m {
                a[i * m + j] = dot(&self.df_history[i], &self.df_history[j]);
            }
        }

        // Solve A·γ = β via Cholesky (A is symmetric positive semi-definite).
        // Add Tikhonov regularization for stability: A → A + εI
        let eps = 1e-12;
        for i in 0..m {
            a[i * m + i] += eps;
        }

        solve_symmetric_positive(m, &mut a, &mut beta);
        Ok(beta)
    }

    /// Apply Broyden update on GPU.
    /// `x_new` = x + α·r - `Σ_k` `γ_k` · (`Δx_k` + `α·ΔF_k`)
    /// The linear-mixing part (x + α·r) goes through the GPU mixer.
    /// The Broyden correction is accumulated on CPU (`O(n_history` × dim)) then
    /// added to the GPU result. For large dim this could be a second GPU kernel,
    /// but `n_history` is typically 5–10 so the CPU path is <1ms.
    async fn broyden_update_gpu(
        &self,
        x: &[f64],
        residual: &[f64],
        gammas: &[f64],
    ) -> Result<Vec<f64>> {
        let x_computed: Vec<f64> = x.iter().zip(residual).map(|(a, b)| a + b).collect();
        let mut result = self.linear_mixer.mix(x, &x_computed).await?;

        let alpha = self.params.alpha;
        for (k, gamma_k) in gammas.iter().enumerate() {
            if gamma_k.abs() < 1e-30 {
                continue;
            }
            let dx_k = &self.dx_history[k];
            let df_k = &self.df_history[k];
            for ((r, &dx), &df) in result.iter_mut().zip(dx_k.iter()).zip(df_k.iter()) {
                *r -= gamma_k * alpha.mul_add(df, dx);
            }
        }

        Ok(result)
    }

    /// Update history vectors
    fn update_history(&mut self, x_old: &[f64], x_new: &[f64], residual: &[f64]) {
        if self.dx_history.len() >= self.max_history {
            // Remove oldest history
            self.dx_history.remove(0);
            self.df_history.remove(0);
        }

        // Store Δx = x_new - x_old
        let dx: Vec<f64> = x_new.iter().zip(x_old).map(|(a, b)| a - b).collect();
        self.dx_history.push(dx);

        // Store ΔF (would need previous residual, simplified here)
        self.df_history.push(residual.to_vec());
    }
}

/// Inner product of two equal-length slices.
fn dot(a: &[f64], b: &[f64]) -> f64 {
    a.iter().zip(b).map(|(x, y)| x * y).sum()
}

/// In-place Cholesky solve for a small symmetric positive-definite system.
///
/// On entry, `a` is the column-major m×m matrix, `b` is the RHS.
/// On exit, `b` contains the solution.
fn solve_symmetric_positive(m: usize, a: &mut [f64], b: &mut [f64]) {
    // Cholesky decomposition: A = L·Lᵀ (in-place, lower triangle in `a`)
    for j in 0..m {
        let mut s = a[j * m + j];
        for k in 0..j {
            s = a[j * m + k].mul_add(-a[j * m + k], s);
        }
        if s <= 0.0 {
            // Not positive definite — zero out remaining gammas
            b[j..m].fill(0.0);
            return;
        }
        let ljj = s.sqrt();
        a[j * m + j] = ljj;

        for i in (j + 1)..m {
            let mut s = a[i * m + j];
            for k in 0..j {
                s = a[i * m + k].mul_add(-a[j * m + k], s);
            }
            a[i * m + j] = s / ljj;
        }
    }

    // Forward substitution: L·y = b
    for i in 0..m {
        let mut s = b[i];
        for k in 0..i {
            s = a[i * m + k].mul_add(-b[k], s);
        }
        b[i] = s / a[i * m + i];
    }

    // Back substitution: Lᵀ·x = y
    for i in (0..m).rev() {
        let mut s = b[i];
        for k in (i + 1)..m {
            s = a[k * m + i].mul_add(-b[k], s);
        }
        b[i] = s / a[i * m + i];
    }
}

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

    #[tokio::test]
    async fn test_linear_mixer() {
        let Some(device) = get_test_device_if_f64_gpu_available().await else {
            return; // Skip if no f64-capable GPU
        };

        let params = MixingParams {
            alpha: 0.5,
            ..Default::default()
        };
        let mixer = LinearMixer::new(device, 1024, params).unwrap();

        let x_old = vec![1.0; 1024];
        let x_computed = vec![2.0; 1024];

        let result = mixer.mix(&x_old, &x_computed).await.unwrap();

        // Expected: 0.5 * 1.0 + 0.5 * 2.0 = 1.5
        for val in &result {
            assert!((val - 1.5).abs() < 1e-10, "Expected 1.5, got {val}");
        }
    }

    #[test]
    fn test_cholesky_solve() {
        // 2×2 system: [4 2; 2 3] · x = [6; 5] → x = [1; 1]
        let mut a = vec![4.0, 2.0, 2.0, 3.0];
        let mut b = vec![6.0, 5.0];
        solve_symmetric_positive(2, &mut a, &mut b);
        assert!((b[0] - 1.0).abs() < 1e-10, "x[0]={}", b[0]);
        assert!((b[1] - 1.0).abs() < 1e-10, "x[1]={}", b[1]);
    }

    #[test]
    fn test_dot_product() {
        let a = vec![1.0, 2.0, 3.0];
        let b = vec![4.0, 5.0, 6.0];
        assert!((dot(&a, &b) - 32.0).abs() < 1e-10);
    }
}