use std::sync::Arc;
use wgpu::util::DeviceExt;
use crate::device::WgpuDevice;
use crate::device::capabilities::WORKGROUP_SIZE_1D;
pub const WGSL_WRIGHT_FISHER_F64: &str =
include_str!("../../shaders/bio/wright_fisher_step_f64.wgsl");
#[repr(C)]
#[derive(Copy, Clone, bytemuck::Pod, bytemuck::Zeroable)]
struct WfParams {
n_pops: u32,
n_loci: u32,
two_n: u32,
_pad: u32,
}
pub struct WrightFisherGpu {
pipeline: wgpu::ComputePipeline,
bgl: wgpu::BindGroupLayout,
device: Arc<WgpuDevice>,
}
impl WrightFisherGpu {
#[must_use]
pub fn new(device: Arc<WgpuDevice>) -> Self {
let d = device.device();
let bgl = d.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
label: Some("WrightFisher BGL"),
entries: &[
storage_entry(0, true), storage_entry(1, true), storage_entry(2, false), storage_entry(3, false), uniform_entry(4), ],
});
let layout = d.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
label: Some("WrightFisher Layout"),
bind_group_layouts: &[&bgl],
immediate_size: 0,
});
let module = device.compile_shader_f64(WGSL_WRIGHT_FISHER_F64, Some("WrightFisher f64"));
let pipeline = d.create_compute_pipeline(&wgpu::ComputePipelineDescriptor {
label: Some("WrightFisher Pipeline"),
layout: Some(&layout),
module: &module,
entry_point: Some("wright_fisher"),
compilation_options: Default::default(),
cache: None,
});
Self {
pipeline,
bgl,
device,
}
}
pub fn dispatch(
&self,
freq_in_buf: &wgpu::Buffer,
selection_buf: &wgpu::Buffer,
freq_out_buf: &wgpu::Buffer,
prng_state_buf: &wgpu::Buffer,
n_pops: u32,
n_loci: u32,
two_n: u32,
) {
let d = self.device.device();
let q = self.device.queue();
let params = WfParams {
n_pops,
n_loci,
two_n,
_pad: 0,
};
let params_buf = d.create_buffer_init(&wgpu::util::BufferInitDescriptor {
label: Some("WrightFisher Params"),
contents: bytemuck::bytes_of(¶ms),
usage: wgpu::BufferUsages::UNIFORM,
});
let total = n_pops * n_loci;
let bg = d.create_bind_group(&wgpu::BindGroupDescriptor {
label: Some("WrightFisher BG"),
layout: &self.bgl,
entries: &[
wgpu::BindGroupEntry {
binding: 0,
resource: freq_in_buf.as_entire_binding(),
},
wgpu::BindGroupEntry {
binding: 1,
resource: selection_buf.as_entire_binding(),
},
wgpu::BindGroupEntry {
binding: 2,
resource: freq_out_buf.as_entire_binding(),
},
wgpu::BindGroupEntry {
binding: 3,
resource: prng_state_buf.as_entire_binding(),
},
wgpu::BindGroupEntry {
binding: 4,
resource: params_buf.as_entire_binding(),
},
],
});
let mut encoder = self
.device
.create_encoder_guarded(&wgpu::CommandEncoderDescriptor {
label: Some("WrightFisher"),
});
{
let mut pass = encoder.begin_compute_pass(&wgpu::ComputePassDescriptor {
label: Some("WrightFisher Pass"),
timestamp_writes: None,
});
pass.set_pipeline(&self.pipeline);
pass.set_bind_group(0, Some(&bg), &[]);
pass.dispatch_workgroups(total.div_ceil(WORKGROUP_SIZE_1D), 1, 1);
}
q.submit(std::iter::once(encoder.finish()));
}
}
fn storage_entry(binding: u32, read_only: bool) -> wgpu::BindGroupLayoutEntry {
wgpu::BindGroupLayoutEntry {
binding,
visibility: wgpu::ShaderStages::COMPUTE,
ty: wgpu::BindingType::Buffer {
ty: wgpu::BufferBindingType::Storage { read_only },
has_dynamic_offset: false,
min_binding_size: None,
},
count: None,
}
}
fn uniform_entry(binding: u32) -> wgpu::BindGroupLayoutEntry {
wgpu::BindGroupLayoutEntry {
binding,
visibility: wgpu::ShaderStages::COMPUTE,
ty: wgpu::BindingType::Buffer {
ty: wgpu::BufferBindingType::Uniform,
has_dynamic_offset: false,
min_binding_size: None,
},
count: None,
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn f64_shader_contains_wright_fisher() {
assert!(WGSL_WRIGHT_FISHER_F64.contains("fn wright_fisher"));
assert!(WGSL_WRIGHT_FISHER_F64.contains("f64"));
}
#[test]
fn f64_shader_compiles_via_naga() {
let Some(device) = crate::device::test_pool::get_test_device_if_f64_gpu_available_sync()
else {
return;
};
let _ = device.compile_shader_f64(WGSL_WRIGHT_FISHER_F64, Some("wright_fisher_f64"));
}
}