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
// SPDX-License-Identifier: AGPL-3.0-or-later
//! Standard Jacobi sweep — multi-dispatch variant (all matrix sizes)
//!
//! Iterates over (p,q) pairs, dispatching compute angles → rotate A → update blocks → rotate V
//! for each pair. Used by `execute_f64` and `execute_f64_buffers`.
use super::BatchedEighGpu;
use super::params::{BatchedEighParams, ParallelSweepParams};
use super::pipelines::create_eigh_pipelines;
use super::sweep::run_sweep_pass;
use crate::device::WgpuDevice;
use crate::device::capabilities::{WORKGROUP_SIZE_1D, WORKGROUP_SIZE_COMPACT};
use crate::error::{BarracudaError, Result};
use std::sync::Arc;
impl BatchedEighGpu {
/// Execute batched eigenvalue decomposition on GPU with full f64 precision
/// This processes multiple symmetric matrices in parallel, ideal for HFB
/// Hamiltonian diagonalization where 52 nuclei need simultaneous eigensolves.
/// # Arguments
/// * `device` - `WgpuDevice` to execute on
/// * `data` - Packed matrices [`batch_size` × n × n] in row-major order (f64)
/// * `n` - Matrix dimension (same for all matrices)
/// * `batch_size` - Number of matrices
/// * `max_sweeps` - Maximum Jacobi sweeps (default: 30)
/// # Returns
/// Tuple (eigenvalues, eigenvectors) where:
/// - eigenvalues: [`batch_size` × n] f64
/// - eigenvectors: [`batch_size` × n × n] f64
/// # Errors
/// Returns [`Err`] if `data.len() != batch_size * n * n`, buffer allocation fails, pipeline execution fails, or the device is lost.
pub fn execute_f64(
device: Arc<WgpuDevice>,
data: &[f64],
n: usize,
batch_size: usize,
max_sweeps: u32,
) -> Result<(Vec<f64>, Vec<f64>)> {
let expected_len = batch_size * n * n;
if data.len() != expected_len {
return Err(BarracudaError::InvalidInput {
message: format!(
"Expected {} elements for {} matrices of {}x{}, got {}",
expected_len,
batch_size,
n,
n,
data.len()
),
});
}
let nu = n as u32;
let batch_u = batch_size as u32;
// Create A buffer (input matrices, will be modified in-place)
let a_buffer = {
let bytes: &[u8] = bytemuck::cast_slice(data);
device
.device
.create_buffer_init(&wgpu::util::BufferInitDescriptor {
label: Some("Batched A f64"),
contents: bytes,
usage: wgpu::BufferUsages::STORAGE | wgpu::BufferUsages::COPY_SRC,
})
};
// Create V buffer (eigenvectors output)
let v_size = (batch_size * n * n * 8) as u64;
let v_buffer = device.device.create_buffer(&wgpu::BufferDescriptor {
label: Some("Batched V f64"),
size: v_size,
usage: wgpu::BufferUsages::STORAGE | wgpu::BufferUsages::COPY_SRC,
mapped_at_creation: false,
});
// Create eigenvalues buffer
let eig_size = (batch_size * n * 8) as u64;
let eig_buffer = device.device.create_buffer(&wgpu::BufferDescriptor {
label: Some("Batched eigenvalues f64"),
size: eig_size,
usage: wgpu::BufferUsages::STORAGE | wgpu::BufferUsages::COPY_SRC,
mapped_at_creation: false,
});
// Create cos/sin buffer for rotation angles [batch_size × 2]
let cs_size = (batch_size * 2 * 8) as u64;
let cs_buffer = device.device.create_buffer(&wgpu::BufferDescriptor {
label: Some("Batched cs f64"),
size: cs_size,
usage: wgpu::BufferUsages::STORAGE | wgpu::BufferUsages::COPY_SRC,
mapped_at_creation: false,
});
let shader = device.compile_shader_f64(Self::wgsl_shader(), Some("Batched Eigh f64"));
let pipelines = create_eigh_pipelines(&device, &shader);
let params = BatchedEighParams {
n: nu,
batch_size: batch_u,
max_sweeps,
_pad: 0,
};
let params_buffer = device.create_uniform_buffer("Batched Eigh Params", ¶ms);
let init_bg = device.device.create_bind_group(&wgpu::BindGroupDescriptor {
label: Some("Batched Init BG"),
layout: &pipelines.init_bgl,
entries: &[
wgpu::BindGroupEntry {
binding: 0,
resource: params_buffer.as_entire_binding(),
},
wgpu::BindGroupEntry {
binding: 1,
resource: a_buffer.as_entire_binding(),
},
wgpu::BindGroupEntry {
binding: 2,
resource: v_buffer.as_entire_binding(),
},
wgpu::BindGroupEntry {
binding: 3,
resource: eig_buffer.as_entire_binding(),
},
],
});
// Step 1: Initialize V = Identity for all matrices
{
let mut encoder = device.create_encoder_guarded(&wgpu::CommandEncoderDescriptor {
label: Some("Init V Encoder"),
});
{
let mut pass = encoder.begin_compute_pass(&wgpu::ComputePassDescriptor {
label: Some("Init V Pass"),
timestamp_writes: None,
});
pass.set_pipeline(&pipelines.init_v_pipeline);
pass.set_bind_group(0, Some(&init_bg), &[]);
let wg_xy = nu.div_ceil(16);
pass.dispatch_workgroups(wg_xy, wg_xy, batch_u);
}
device.submit_commands(Some(encoder.finish()));
}
// Step 2: Jacobi sweeps
for _sweep in 0..max_sweeps {
for p in 0..(n - 1) {
for q in (p + 1)..n {
let sweep_params = ParallelSweepParams {
n: nu,
batch_size: batch_u,
current_p: p as u32,
current_q: q as u32,
};
let sweep_params_buffer =
device.create_uniform_buffer("Sweep Params", &sweep_params);
let sweep_bg = device.device.create_bind_group(&wgpu::BindGroupDescriptor {
label: Some("Sweep BG"),
layout: &pipelines.sweep_bgl,
entries: &[
wgpu::BindGroupEntry {
binding: 0,
resource: sweep_params_buffer.as_entire_binding(),
},
wgpu::BindGroupEntry {
binding: 1,
resource: a_buffer.as_entire_binding(),
},
wgpu::BindGroupEntry {
binding: 2,
resource: v_buffer.as_entire_binding(),
},
wgpu::BindGroupEntry {
binding: 3,
resource: cs_buffer.as_entire_binding(),
},
],
});
run_sweep_pass(
&device,
&sweep_bg,
&pipelines.compute_angles_pipeline,
(batch_u.div_ceil(WORKGROUP_SIZE_COMPACT), 1, 1),
);
run_sweep_pass(
&device,
&sweep_bg,
&pipelines.rotate_a_pipeline,
(nu.div_ceil(WORKGROUP_SIZE_COMPACT), batch_u, 1),
);
run_sweep_pass(
&device,
&sweep_bg,
&pipelines.update_blocks_pipeline,
(batch_u.div_ceil(WORKGROUP_SIZE_COMPACT), 1, 1),
);
run_sweep_pass(
&device,
&sweep_bg,
&pipelines.rotate_v_pipeline,
(nu.div_ceil(WORKGROUP_SIZE_COMPACT), batch_u, 1),
);
}
}
}
// Step 3: Extract eigenvalues from diagonal of A
{
let mut encoder = device.create_encoder_guarded(&wgpu::CommandEncoderDescriptor {
label: Some("Extract Eigenvalues"),
});
{
let mut pass = encoder.begin_compute_pass(&wgpu::ComputePassDescriptor {
label: Some("Extract Eigenvalues Pass"),
timestamp_writes: None,
});
pass.set_pipeline(&pipelines.extract_pipeline);
pass.set_bind_group(0, Some(&init_bg), &[]);
pass.dispatch_workgroups(nu.div_ceil(WORKGROUP_SIZE_1D), batch_u, 1);
}
device.submit_commands(Some(encoder.finish()));
}
let eigenvalues = device.read_f64_buffer(&eig_buffer, batch_size * n)?;
let eigenvectors = device.read_f64_buffer(&v_buffer, batch_size * n * n)?;
Ok((eigenvalues, eigenvectors))
}
/// Execute batched eigenvalue decomposition on GPU **without CPU readback**
/// This is the GPU-resident variant for use in pipelines where data should
/// stay on GPU between operations (e.g., SCF iteration loops).
/// # Errors
/// Returns [`Err`] if buffer sizes are invalid, shader compilation fails, pipeline execution fails, or the device is lost.
pub fn execute_f64_buffers(
device: &Arc<WgpuDevice>,
matrices_buffer: &wgpu::Buffer,
eigenvalues_buffer: &wgpu::Buffer,
eigenvectors_buffer: &wgpu::Buffer,
n: usize,
batch_size: usize,
max_sweeps: u32,
) -> Result<()> {
let nu = n as u32;
let batch_u = batch_size as u32;
let cs_size = (batch_size * 2 * 8) as u64;
let cs_buffer = device.device.create_buffer(&wgpu::BufferDescriptor {
label: Some("BatchedEigh cs f64"),
size: cs_size,
usage: wgpu::BufferUsages::STORAGE,
mapped_at_creation: false,
});
let shader =
device.compile_shader_f64(Self::wgsl_shader(), Some("Batched Eigh f64 (buffers)"));
let pipelines = create_eigh_pipelines(device, &shader);
let params = BatchedEighParams {
n: nu,
batch_size: batch_u,
max_sweeps,
_pad: 0,
};
let params_buffer = device.create_uniform_buffer("Batched Eigh Params (buffers)", ¶ms);
let init_bg = device.device.create_bind_group(&wgpu::BindGroupDescriptor {
label: Some("Batched Init BG (buffers)"),
layout: &pipelines.init_bgl,
entries: &[
wgpu::BindGroupEntry {
binding: 0,
resource: params_buffer.as_entire_binding(),
},
wgpu::BindGroupEntry {
binding: 1,
resource: matrices_buffer.as_entire_binding(),
},
wgpu::BindGroupEntry {
binding: 2,
resource: eigenvectors_buffer.as_entire_binding(),
},
wgpu::BindGroupEntry {
binding: 3,
resource: eigenvalues_buffer.as_entire_binding(),
},
],
});
{
let mut encoder = device.create_encoder_guarded(&wgpu::CommandEncoderDescriptor {
label: Some("Init V Encoder (buffers)"),
});
{
let mut pass = encoder.begin_compute_pass(&wgpu::ComputePassDescriptor {
label: Some("Init V Pass (buffers)"),
timestamp_writes: None,
});
pass.set_pipeline(&pipelines.init_v_pipeline);
pass.set_bind_group(0, Some(&init_bg), &[]);
let wg_xy = nu.div_ceil(16);
pass.dispatch_workgroups(wg_xy, wg_xy, batch_u);
}
device.submit_commands(Some(encoder.finish()));
}
for _sweep in 0..max_sweeps {
for p in 0..(n - 1) {
for q in (p + 1)..n {
let sweep_params = ParallelSweepParams {
n: nu,
batch_size: batch_u,
current_p: p as u32,
current_q: q as u32,
};
let sweep_params_buffer =
device.create_uniform_buffer("Sweep Params (buffers)", &sweep_params);
let sweep_bg = device.device.create_bind_group(&wgpu::BindGroupDescriptor {
label: Some("Sweep BG (buffers)"),
layout: &pipelines.sweep_bgl,
entries: &[
wgpu::BindGroupEntry {
binding: 0,
resource: sweep_params_buffer.as_entire_binding(),
},
wgpu::BindGroupEntry {
binding: 1,
resource: matrices_buffer.as_entire_binding(),
},
wgpu::BindGroupEntry {
binding: 2,
resource: eigenvectors_buffer.as_entire_binding(),
},
wgpu::BindGroupEntry {
binding: 3,
resource: cs_buffer.as_entire_binding(),
},
],
});
run_sweep_pass(
device,
&sweep_bg,
&pipelines.compute_angles_pipeline,
(batch_u.div_ceil(WORKGROUP_SIZE_COMPACT), 1, 1),
);
run_sweep_pass(
device,
&sweep_bg,
&pipelines.rotate_a_pipeline,
(nu.div_ceil(WORKGROUP_SIZE_COMPACT), batch_u, 1),
);
run_sweep_pass(
device,
&sweep_bg,
&pipelines.update_blocks_pipeline,
(batch_u.div_ceil(WORKGROUP_SIZE_COMPACT), 1, 1),
);
run_sweep_pass(
device,
&sweep_bg,
&pipelines.rotate_v_pipeline,
(nu.div_ceil(WORKGROUP_SIZE_COMPACT), batch_u, 1),
);
}
}
}
{
let mut encoder = device.create_encoder_guarded(&wgpu::CommandEncoderDescriptor {
label: Some("Extract Eigenvalues (buffers)"),
});
{
let mut pass = encoder.begin_compute_pass(&wgpu::ComputePassDescriptor {
label: Some("Extract Eigenvalues Pass (buffers)"),
timestamp_writes: None,
});
pass.set_pipeline(&pipelines.extract_pipeline);
pass.set_bind_group(0, Some(&init_bg), &[]);
pass.dispatch_workgroups(nu.div_ceil(WORKGROUP_SIZE_1D), batch_u, 1);
}
device.submit_commands(Some(encoder.finish()));
}
Ok(())
}
}