use wgpu::{Buffer, Queue};
use super::super::linear_grid::dispatch_linear;
use super::super::pipeline::{LayoutKey, PipelineCache};
use crate::dtype::DType;
use crate::error::{Error, Result};
const BINCOUNT_UNWEIGHTED_SHADER: &str = include_str!("../bincount_i32.wgsl");
const BINCOUNT_WEIGHTED_SHADER_F32: &str = include_str!("../bincount_weighted_f32.wgsl");
pub fn launch_bincount(
cache: &PipelineCache,
queue: &Queue,
input: &Buffer,
weights: Option<&Buffer>,
output: &Buffer,
params_buffer: &Buffer,
n: usize,
weights_dtype: Option<DType>,
) -> Result<()> {
let (name, shader) = if let Some(dtype) = weights_dtype {
if dtype != DType::F32 {
return Err(Error::UnsupportedDType {
dtype,
op: "bincount_weighted",
});
}
("bincount_weighted_f32", BINCOUNT_WEIGHTED_SHADER_F32)
} else {
("bincount_i32", BINCOUNT_UNWEIGHTED_SHADER)
};
let module = cache.get_or_create_module(name, shader);
let (layout, bind_group) = if let Some(weights_buf) = weights {
let layout = cache.get_or_create_layout(LayoutKey {
num_storage_buffers: 3,
num_uniform_buffers: 1,
num_readonly_storage: 2, });
let bind_group =
cache.create_bind_group(&layout, &[input, weights_buf, output, params_buffer])?;
(layout, bind_group)
} else {
let layout = cache.get_or_create_layout(LayoutKey {
num_storage_buffers: 2,
num_uniform_buffers: 1,
num_readonly_storage: 1, });
let bind_group = cache.create_bind_group(&layout, &[input, output, params_buffer])?;
(layout, bind_group)
};
let pipeline = cache.get_or_create_pipeline(name, name, &module, &layout);
let mut encoder = cache
.device()
.create_command_encoder(&wgpu::CommandEncoderDescriptor {
label: Some("bincount"),
});
{
let mut pass = encoder.begin_compute_pass(&wgpu::ComputePassDescriptor {
label: Some("bincount"),
timestamp_writes: None,
});
pass.set_pipeline(&pipeline);
pass.set_bind_group(0, Some(&bind_group), &[]);
dispatch_linear(&mut pass, n);
}
queue.submit(std::iter::once(encoder.finish()));
Ok(())
}