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
// SPDX-License-Identifier: AGPL-3.0-or-later
//! Batch pairwise dN/dS (Nei-Gojobori 1986) on GPU.
//!
//! One thread per coding sequence pair. Classifies synonymous/nonsynonymous
//! sites and differences, then applies Jukes-Cantor correction.
//! Polyfill required for Ada Lovelace (uses f64 log in Jukes-Cantor).
//!
//! ## Absorbed from
//!
//! wetSpring handoff v6, `dnds_batch_f64.wgsl` — 9/9 GPU checks PASS.
use crate::device::WgpuDevice;
use crate::device::capabilities::WORKGROUP_SIZE_COMPACT;
use crate::error::Result;
use bytemuck::{Pod, Zeroable};
use std::sync::Arc;
const SHADER: &str = include_str!("../../shaders/bio/dnds_batch_f64.wgsl");
#[repr(C)]
#[derive(Copy, Clone, Pod, Zeroable)]
struct DnDsParams {
n_pairs: u32,
n_codons: u32,
}
/// Batch dN/dS computation on GPU.
pub struct DnDsBatchF64 {
device: Arc<WgpuDevice>,
pipeline: wgpu::ComputePipeline,
bgl: wgpu::BindGroupLayout,
}
impl DnDsBatchF64 {
/// Create dN/dS batch calculator.
///
/// # Errors
///
/// Returns [`Err`] if buffer allocation, GPU dispatch, or buffer
/// readback fails (e.g. device lost or out of memory).
pub fn new(device: Arc<WgpuDevice>) -> Result<Self> {
let module = device.compile_shader_f64(SHADER, Some("dnds_batch_f64"));
let bgl = super::snp::make_bgl(&device, &[true, true, true, false, false, false]);
let layout = super::snp::make_layout(&device, &bgl, "DnDsBatch");
let pipeline = super::snp::make_pipeline(&device, &layout, &module, "main", "DnDsBatch");
Ok(Self {
device,
pipeline,
bgl,
})
}
/// Dispatch dN/dS computation for codon sequence pairs.
///
/// # Errors
///
/// Returns [`Err`] if buffer allocation, GPU dispatch, or buffer
/// readback fails (e.g. device lost or out of memory).
pub fn dispatch(
&self,
n_pairs: u32,
n_codons: u32,
seq_a: &wgpu::Buffer,
seq_b: &wgpu::Buffer,
genetic_code: &wgpu::Buffer,
dn_out: &wgpu::Buffer,
ds_out: &wgpu::Buffer,
omega_out: &wgpu::Buffer,
) -> Result<()> {
let params = DnDsParams { n_pairs, n_codons };
let pbuf = super::snp::upload_uniform(&self.device, ¶ms);
let bg = self
.device
.device
.create_bind_group(&wgpu::BindGroupDescriptor {
label: None,
layout: &self.bgl,
entries: &[
super::snp::bg_entry(0, &pbuf),
super::snp::bg_entry(1, seq_a),
super::snp::bg_entry(2, seq_b),
super::snp::bg_entry(3, genetic_code),
super::snp::bg_entry(4, dn_out),
super::snp::bg_entry(5, ds_out),
super::snp::bg_entry(6, omega_out),
],
});
super::snp::submit(
&self.device,
&self.pipeline,
&bg,
n_pairs.div_ceil(WORKGROUP_SIZE_COMPACT),
);
Ok(())
}
}