trueno/backends/gpu/device/reductions/
mod.rs1mod reduce_1d;
11mod tiled_2d;
12
13#[cfg(any(feature = "gpu", feature = "gpu-wasm"))]
14use super::super::runtime;
15use super::super::shaders;
16use super::GpuDevice;
17
18impl GpuDevice {
19 #[cfg(all(feature = "gpu", not(target_arch = "wasm32")))]
34 pub fn tiled_sum_2d(&self, data: &[f32], width: usize, height: usize) -> Result<f32, String> {
35 runtime::block_on(self.tiled_sum_2d_async(data, width, height))
36 }
37
38 pub async fn tiled_sum_2d_async(
40 &self,
41 data: &[f32],
42 width: usize,
43 height: usize,
44 ) -> Result<f32, String> {
45 self.tiled_reduce_2d_async(
46 data,
47 width,
48 height,
49 shaders::TILED_SUM_REDUCTION_SHADER,
50 "TiledSum",
51 0.0, |partials| partials.iter().sum(),
53 )
54 .await
55 }
56
57 #[cfg(all(feature = "gpu", not(target_arch = "wasm32")))]
62 pub fn tiled_max_2d(&self, data: &[f32], width: usize, height: usize) -> Result<f32, String> {
63 runtime::block_on(self.tiled_max_2d_async(data, width, height))
64 }
65
66 pub async fn tiled_max_2d_async(
68 &self,
69 data: &[f32],
70 width: usize,
71 height: usize,
72 ) -> Result<f32, String> {
73 self.tiled_reduce_2d_async(
74 data,
75 width,
76 height,
77 shaders::TILED_MAX_REDUCTION_SHADER,
78 "TiledMax",
79 f32::NEG_INFINITY, |partials| partials.iter().copied().fold(f32::NEG_INFINITY, f32::max),
81 )
82 .await
83 }
84
85 #[cfg(all(feature = "gpu", not(target_arch = "wasm32")))]
90 pub fn tiled_min_2d(&self, data: &[f32], width: usize, height: usize) -> Result<f32, String> {
91 runtime::block_on(self.tiled_min_2d_async(data, width, height))
92 }
93
94 pub async fn tiled_min_2d_async(
96 &self,
97 data: &[f32],
98 width: usize,
99 height: usize,
100 ) -> Result<f32, String> {
101 self.tiled_reduce_2d_async(
102 data,
103 width,
104 height,
105 shaders::TILED_MIN_REDUCTION_SHADER,
106 "TiledMin",
107 f32::INFINITY, |partials| partials.iter().copied().fold(f32::INFINITY, f32::min),
109 )
110 .await
111 }
112}