use crate::device::WgpuDevice;
use crate::device::capabilities::WORKGROUP_SIZE_1D;
use crate::error::{BarracudaError, Result};
use crate::utils::chunk_to_array;
use bytemuck;
use std::sync::Arc;
const SHADER_DF64: &str = include_str!("../shaders/reduce/sum_reduce_df64.wgsl");
const DF64_CORE: &str = include_str!("../shaders/math/df64_core.wgsl");
const SHADER_SCALAR_F64: &str = include_str!("../shaders/reduce/sum_reduce_scalar_f64.wgsl");
const SHADER_SUBGROUP_F64: &str = include_str!("../shaders/reduce/sum_reduce_subgroup_f64.wgsl");
fn shader_for_device(device: &WgpuDevice) -> &'static str {
let has_subgroups = device.has_subgroups();
let f64_builtins = crate::device::probe::cached_f64_builtins(device);
if has_subgroups && f64_builtins.is_some() {
tracing::info!(
"Subgroup f64 reduction on {} (max_subgroup_size={})",
device.adapter_info().name,
device.adapter_info().subgroup_max_size,
);
return SHADER_SUBGROUP_F64;
}
if let Some(caps) = f64_builtins
&& !caps.df64_workgroup_reduce
{
tracing::info!(
"DF64 workgroup reduce not verified on {} — using scalar f64 storage reduction",
device.adapter_info().name
);
return SHADER_SCALAR_F64;
}
static DF64_COMBINED: std::sync::LazyLock<String> =
std::sync::LazyLock::new(|| format!("{DF64_CORE}\n{SHADER_DF64}"));
&DF64_COMBINED
}
pub struct ReduceScalarPipeline {
device: Arc<WgpuDevice>,
n: u32,
partial_buffer: wgpu::Buffer, scalar_staging: wgpu::Buffer, sum_pipeline: wgpu::ComputePipeline,
_sum_bg_pass1: wgpu::BindGroup, sum_bg_pass2: wgpu::BindGroup,
scalar_output: wgpu::Buffer, params_buf: wgpu::Buffer,
_partial_params: wgpu::Buffer, }
impl ReduceScalarPipeline {
pub fn new(device: Arc<WgpuDevice>, n: usize) -> Result<Self> {
let n_u32 = n as u32;
let n_partial = n_u32.div_ceil(WORKGROUP_SIZE_1D) as usize;
let module = device.compile_shader_f64(shader_for_device(&device), Some("sum_reduce_f64"));
let bgl = device
.device
.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
label: Some("ReduceScalar:BGL"),
entries: &[
bgl_entry(0, wgpu::BufferBindingType::Storage { read_only: true }),
bgl_entry(1, wgpu::BufferBindingType::Storage { read_only: false }),
bgl_entry(2, wgpu::BufferBindingType::Uniform),
],
});
let pipeline_layout =
device
.device
.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
label: Some("ReduceScalar:PL"),
bind_group_layouts: &[&bgl],
immediate_size: 0,
});
let sum_pipeline =
device
.device
.create_compute_pipeline(&wgpu::ComputePipelineDescriptor {
label: Some("sum_reduce_f64"),
layout: Some(&pipeline_layout),
module: &module,
entry_point: Some("sum_reduce_f64"),
compilation_options: Default::default(),
cache: None,
});
let partial_buffer = device.device.create_buffer(&wgpu::BufferDescriptor {
label: Some("ReduceScalar:partial"),
size: (n_partial * 8) as u64,
usage: wgpu::BufferUsages::STORAGE
| wgpu::BufferUsages::COPY_SRC
| wgpu::BufferUsages::COPY_DST,
mapped_at_creation: false,
});
let scalar_output = device.device.create_buffer(&wgpu::BufferDescriptor {
label: Some("ReduceScalar:scalar"),
size: 8,
usage: wgpu::BufferUsages::STORAGE | wgpu::BufferUsages::COPY_SRC,
mapped_at_creation: false,
});
let scalar_staging = device.device.create_buffer(&wgpu::BufferDescriptor {
label: Some("ReduceScalar:staging"),
size: 8,
usage: wgpu::BufferUsages::MAP_READ | wgpu::BufferUsages::COPY_DST,
mapped_at_creation: false,
});
let params_data: [u32; 4] = [n_u32, 0, 0, 0];
let params_bytes: &[u8] = bytemuck::cast_slice(¶ms_data);
let params_buf = device.device.create_buffer(&wgpu::BufferDescriptor {
label: Some("ReduceScalar:params"),
size: 16,
usage: wgpu::BufferUsages::UNIFORM | wgpu::BufferUsages::COPY_DST,
mapped_at_creation: false,
});
device.queue.write_buffer(¶ms_buf, 0, params_bytes);
let partial_size = n_partial as u32;
let partial_params_data: [u32; 4] = [partial_size, 0, 0, 0];
let partial_params_bytes: &[u8] = bytemuck::cast_slice(&partial_params_data);
let partial_params = device.device.create_buffer(&wgpu::BufferDescriptor {
label: Some("ReduceScalar:partial_params"),
size: 16,
usage: wgpu::BufferUsages::UNIFORM | wgpu::BufferUsages::COPY_DST,
mapped_at_creation: false,
});
device
.queue
.write_buffer(&partial_params, 0, partial_params_bytes);
let sum_bg_pass2 = device.device.create_bind_group(&wgpu::BindGroupDescriptor {
label: Some("ReduceScalar:BG:pass2"),
layout: &bgl,
entries: &[
wgpu::BindGroupEntry {
binding: 0,
resource: partial_buffer.as_entire_binding(),
},
wgpu::BindGroupEntry {
binding: 1,
resource: scalar_output.as_entire_binding(),
},
wgpu::BindGroupEntry {
binding: 2,
resource: partial_params.as_entire_binding(),
},
],
});
let sum_bg_pass1 = device.device.create_bind_group(&wgpu::BindGroupDescriptor {
label: Some("ReduceScalar:BG:pass1:init"),
layout: &bgl,
entries: &[
wgpu::BindGroupEntry {
binding: 0,
resource: partial_buffer.as_entire_binding(),
},
wgpu::BindGroupEntry {
binding: 1,
resource: partial_buffer.as_entire_binding(),
},
wgpu::BindGroupEntry {
binding: 2,
resource: params_buf.as_entire_binding(),
},
],
});
Ok(Self {
device,
n: n_u32,
partial_buffer,
scalar_staging,
sum_pipeline,
_sum_bg_pass1: sum_bg_pass1,
sum_bg_pass2,
scalar_output,
params_buf,
_partial_params: partial_params,
})
}
pub fn sum_f64(&self, input: &wgpu::Buffer) -> Result<f64> {
self.reduce(input, "sum_reduce_f64")
}
pub fn max_f64(&self, input: &wgpu::Buffer) -> Result<f64> {
self.reduce(input, "max_reduce_f64")
}
pub fn min_f64(&self, input: &wgpu::Buffer) -> Result<f64> {
self.reduce(input, "min_reduce_f64")
}
#[must_use]
pub fn scalar_buffer(&self) -> &wgpu::Buffer {
&self.scalar_output
}
pub fn encode_reduce_to_buffer(
&self,
encoder: &mut wgpu::CommandEncoder,
input: &wgpu::Buffer,
) {
let bgl = self.sum_pipeline.get_bind_group_layout(0);
let bg_pass1 = self
.device
.device
.create_bind_group(&wgpu::BindGroupDescriptor {
label: Some("ReduceScalar:BG:pass1:encode"),
layout: &bgl,
entries: &[
wgpu::BindGroupEntry {
binding: 0,
resource: input.as_entire_binding(),
},
wgpu::BindGroupEntry {
binding: 1,
resource: self.partial_buffer.as_entire_binding(),
},
wgpu::BindGroupEntry {
binding: 2,
resource: self.params_buf.as_entire_binding(),
},
],
});
let n_partial = self.n.div_ceil(WORKGROUP_SIZE_1D);
{
let mut pass = encoder.begin_compute_pass(&wgpu::ComputePassDescriptor {
label: Some("reduce:encode:pass1"),
timestamp_writes: None,
});
pass.set_pipeline(&self.sum_pipeline);
pass.set_bind_group(0, Some(&bg_pass1), &[]);
pass.dispatch_workgroups(n_partial, 1, 1);
}
{
let mut pass = encoder.begin_compute_pass(&wgpu::ComputePassDescriptor {
label: Some("reduce:encode:pass2"),
timestamp_writes: None,
});
pass.set_pipeline(&self.sum_pipeline);
pass.set_bind_group(0, Some(&self.sum_bg_pass2), &[]);
pass.dispatch_workgroups(1, 1, 1);
}
}
pub fn readback_scalar(&self) -> Result<f64> {
let mut enc = self
.device
.create_encoder_guarded(&wgpu::CommandEncoderDescriptor {
label: Some("ReduceScalar:readback"),
});
enc.copy_buffer_to_buffer(&self.scalar_output, 0, &self.scalar_staging, 0, 8);
self.device.submit_commands(Some(enc.finish()));
let slice = self.scalar_staging.slice(..);
let (tx, rx) = std::sync::mpsc::channel();
slice.map_async(wgpu::MapMode::Read, move |r| {
let _ = tx.send(r);
});
self.device.poll_safe()?;
rx.recv()
.map_err(|_| {
BarracudaError::execution_failed("ReduceScalarPipeline: readback channel closed")
})?
.map_err(|e| BarracudaError::execution_failed(e.to_string()))?;
let data = slice.get_mapped_range();
let v = f64::from_le_bytes(chunk_to_array::<8>(&data[..8])?);
drop(data);
self.scalar_staging.unmap();
Ok(v)
}
fn reduce(&self, input: &wgpu::Buffer, entry: &str) -> Result<f64> {
let bgl = self.sum_pipeline.get_bind_group_layout(0);
let bg_pass1 = self
.device
.device
.create_bind_group(&wgpu::BindGroupDescriptor {
label: Some("ReduceScalar:BG:pass1"),
layout: &bgl,
entries: &[
wgpu::BindGroupEntry {
binding: 0,
resource: input.as_entire_binding(),
},
wgpu::BindGroupEntry {
binding: 1,
resource: self.partial_buffer.as_entire_binding(),
},
wgpu::BindGroupEntry {
binding: 2,
resource: self.params_buf.as_entire_binding(),
},
],
});
let pass2_pipeline = if entry == "sum_reduce_f64" {
None } else {
let module = self
.device
.compile_shader_f64(shader_for_device(&self.device), Some(entry));
let layout =
self.device
.device
.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
label: Some("ReduceScalar:PL:alt"),
bind_group_layouts: &[&bgl],
immediate_size: 0,
});
Some(
self.device
.device
.create_compute_pipeline(&wgpu::ComputePipelineDescriptor {
label: Some(entry),
layout: Some(&layout),
module: &module,
entry_point: Some(entry),
compilation_options: Default::default(),
cache: None,
}),
)
};
let n_partial = self.n.div_ceil(WORKGROUP_SIZE_1D);
let mut enc = self
.device
.create_encoder_guarded(&wgpu::CommandEncoderDescriptor {
label: Some("ReduceScalar"),
});
{
let mut pass = enc.begin_compute_pass(&wgpu::ComputePassDescriptor {
label: Some("reduce:pass1"),
timestamp_writes: None,
});
let pl = pass2_pipeline.as_ref().unwrap_or(&self.sum_pipeline);
pass.set_pipeline(pl);
pass.set_bind_group(0, Some(&bg_pass1), &[]);
pass.dispatch_workgroups(n_partial, 1, 1);
}
{
let mut pass = enc.begin_compute_pass(&wgpu::ComputePassDescriptor {
label: Some("reduce:pass2"),
timestamp_writes: None,
});
let pl = pass2_pipeline.as_ref().unwrap_or(&self.sum_pipeline);
pass.set_pipeline(pl);
pass.set_bind_group(0, Some(&self.sum_bg_pass2), &[]);
pass.dispatch_workgroups(1, 1, 1);
}
enc.copy_buffer_to_buffer(&self.scalar_output, 0, &self.scalar_staging, 0, 8);
self.device.submit_commands(Some(enc.finish()));
let slice = self.scalar_staging.slice(..);
let (tx, rx) = std::sync::mpsc::channel();
slice.map_async(wgpu::MapMode::Read, move |r| {
let _ = tx.send(r);
});
self.device.poll_safe()?;
rx.recv()
.map_err(|_| BarracudaError::execution_failed("ReduceScalarPipeline: channel closed"))?
.map_err(|e| BarracudaError::execution_failed(e.to_string()))?;
let data = slice.get_mapped_range();
let v = f64::from_le_bytes(chunk_to_array::<8>(&data[..8])?);
drop(data);
self.scalar_staging.unmap();
Ok(v)
}
}
fn bgl_entry(binding: u32, ty: wgpu::BufferBindingType) -> wgpu::BindGroupLayoutEntry {
wgpu::BindGroupLayoutEntry {
binding,
visibility: wgpu::ShaderStages::COMPUTE,
ty: wgpu::BindingType::Buffer {
ty,
has_dynamic_offset: false,
min_binding_size: None,
},
count: None,
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_bgl_entry_storage() {
let e = bgl_entry(0, wgpu::BufferBindingType::Storage { read_only: true });
assert_eq!(e.binding, 0);
assert!(e.visibility.contains(wgpu::ShaderStages::COMPUTE));
}
#[test]
fn test_bgl_entry_uniform() {
let e = bgl_entry(2, wgpu::BufferBindingType::Uniform);
assert_eq!(e.binding, 2);
}
#[test]
fn test_workgroup_size_constant() {
assert_eq!(WORKGROUP_SIZE_1D, 256);
}
#[test]
fn test_n_partial_ceiling() {
assert_eq!(1u32.div_ceil(256), 1);
assert_eq!(256u32.div_ceil(256), 1);
assert_eq!(257u32.div_ceil(256), 2);
assert_eq!(10_000u32.div_ceil(256), 40);
}
}