use crate::device::WgpuDevice;
use crate::device::capabilities::WORKGROUP_SIZE_1D;
use crate::error::Result;
use bytemuck::{Pod, Zeroable};
use std::sync::Arc;
const PRNG: &str = include_str!("../../shaders/health/prng_wang_f64.wgsl");
const SHADER_BODY: &str = include_str!("../../shaders/health/michaelis_menten_batch_f64.wgsl");
#[repr(C)]
#[derive(Copy, Clone, Pod, Zeroable)]
struct MmParams {
vmax: f64,
km: f64,
vd: f64,
dt: f64,
n_steps: u32,
n_patients: u32,
base_seed: u32,
_pad: u32,
}
#[derive(Debug, Clone, Copy)]
pub struct MmBatchConfig {
pub vmax: f64,
pub km: f64,
pub vd: f64,
pub dt: f64,
pub n_steps: u32,
pub n_patients: u32,
pub seed: u32,
}
pub struct MichaelisMentenBatchGpu {
device: Arc<WgpuDevice>,
}
impl MichaelisMentenBatchGpu {
#[must_use]
pub fn new(device: Arc<WgpuDevice>) -> Self {
Self { device }
}
pub fn simulate(&self, config: &MmBatchConfig) -> Result<Vec<f64>> {
let params = MmParams {
vmax: config.vmax,
km: config.km,
vd: config.vd,
dt: config.dt,
n_steps: config.n_steps,
n_patients: config.n_patients,
base_seed: config.seed,
_pad: 0,
};
let n = config.n_patients as usize;
let out_buf = self.device.create_buffer_f64(n)?;
let params_buf = self
.device
.create_uniform_buffer("mm_batch:params", ¶ms);
let wg_count = config.n_patients.div_ceil(WORKGROUP_SIZE_1D);
let shader = format!("{PRNG}\n{SHADER_BODY}");
crate::device::compute_pipeline::ComputeDispatch::new(&self.device, "mm_batch")
.shader(&shader, "main")
.f64()
.storage_rw(0, &out_buf)
.uniform(1, ¶ms_buf)
.dispatch(wg_count, 1, 1)
.submit()?;
self.device.read_f64_buffer(&out_buf, n)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn params_layout() {
assert_eq!(std::mem::size_of::<MmParams>(), 48);
}
#[test]
fn shader_source_valid() {
let combined = format!("{PRNG}\n{SHADER_BODY}");
assert!(combined.contains("michaelis"));
assert!(combined.contains("wang_hash"));
assert!(combined.contains("u32_to_uniform_f64"));
assert!(combined.contains("Params"));
}
}