use crate::device::WgpuDevice;
use crate::device::capabilities::WORKGROUP_SIZE_1D;
use crate::error::Result;
use bytemuck::{Pod, Zeroable};
use std::sync::Arc;
const SHADER: &str = include_str!("../../shaders/health/scfa_batch_f64.wgsl");
#[repr(C)]
#[derive(Copy, Clone, Pod, Zeroable)]
struct ScfaGpuParams {
vmax_acetate: f64,
km_acetate: f64,
vmax_propionate: f64,
km_propionate: f64,
vmax_butyrate: f64,
km_butyrate: f64,
n_elements: u32,
_pad: u32,
}
pub struct ScfaBatchGpu {
device: Arc<WgpuDevice>,
}
impl ScfaBatchGpu {
#[must_use]
pub fn new(device: Arc<WgpuDevice>) -> Self {
Self { device }
}
pub fn compute(
&self,
fiber_values: &[f64],
scfa_params: &crate::health::microbiome::ScfaParams,
) -> Result<Vec<f64>> {
let n = fiber_values.len();
let params = ScfaGpuParams {
vmax_acetate: scfa_params.vmax_acetate,
km_acetate: scfa_params.km_acetate,
vmax_propionate: scfa_params.vmax_propionate,
km_propionate: scfa_params.km_propionate,
vmax_butyrate: scfa_params.vmax_butyrate,
km_butyrate: scfa_params.km_butyrate,
n_elements: n as u32,
_pad: 0,
};
let in_buf = self
.device
.create_buffer_f64_init("scfa:input", fiber_values);
let out_buf = self.device.create_buffer_f64(n * 3)?;
let params_buf = self.device.create_uniform_buffer("scfa:params", ¶ms);
let wg_count = (n as u32).div_ceil(WORKGROUP_SIZE_1D);
crate::device::compute_pipeline::ComputeDispatch::new(&self.device, "scfa_batch")
.shader(SHADER, "main")
.f64()
.storage_read(0, &in_buf)
.storage_rw(1, &out_buf)
.uniform(2, ¶ms_buf)
.dispatch(wg_count, 1, 1)
.submit()?;
self.device.read_f64_buffer(&out_buf, n * 3)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn params_layout() {
assert_eq!(std::mem::size_of::<ScfaGpuParams>(), 56);
}
#[test]
fn shader_source_valid() {
assert!(SHADER.contains("michaelis_menten"));
assert!(SHADER.contains("acetate"));
assert!(SHADER.contains("Params"));
}
}