π End-to-end encrypted cloud for everything.
Dart 2faandroidauthye2eeencryptionend-to-end-encryptionfluttergolanggoogle-photosgoogle-photos-alternativeiosopensourcephotosprivacyprivacy-toolstypescriptzero-knowledge
[rust] Use one threshold for elementwise parallelism
laurenspriemed6ff8a8ce46
diff --git a/rust/crates/ml/src/cv/bilateral.rs b/rust/crates/ml/src/cv/bilateral.rsindex 68855c5c62..64dead5a9b 100644--- a/rust/crates/ml/src/cv/bilateral.rs+++ b/rust/crates/ml/src/cv/bilateral.rs@@ -1,8 +1,8 @@use rayon::prelude::*;use super::reflect101;-use crate::cv::OpResult;use crate::cv::image::ImageU8;+use crate::cv::{OpResult, PARALLEL_MIN_ELEMS};pub(crate) fn bilateral_filter_u8(src: &ImageU8,@@ -59,23 +59,30 @@ pub(crate) fn bilateral_filter_u8(}let mut out = vec![0u8; w * h];- out.par_chunks_exact_mut(w)- .enumerate()- .for_each(|(y, dst)| {- let win = &temp[y * stride..];- let centre = radius * stride + radius;- for (x, d) in dst.iter_mut().enumerate() {- let mid = win[centre + x] as i32;- let mut sum = 0.0f32;- let mut wsum = 0.0f32;- for &(off, sw) in &taps {- let val = win[off + x];- let weight = sw * color_weight[(val as i32 - mid).unsigned_abs() as usize];- wsum += weight;- sum += val as f32 * weight;- }- *d = (sum / wsum).round_ties_even() as u8;+ let row = |y: usize, dst: &mut [u8]| {+ let win = &temp[y * stride..];+ let centre = radius * stride + radius;+ for (x, d) in dst.iter_mut().enumerate() {+ let mid = win[centre + x] as i32;+ let mut sum = 0.0f32;+ let mut wsum = 0.0f32;+ for &(off, sw) in &taps {+ let val = win[off + x];+ let weight = sw * color_weight[(val as i32 - mid).unsigned_abs() as usize];+ wsum += weight;+ sum += val as f32 * weight;}- });+ *d = (sum / wsum).round_ties_even() as u8;+ }+ };+ if w * h >= PARALLEL_MIN_ELEMS {+ out.par_chunks_exact_mut(w)+ .enumerate()+ .for_each(|(y, dst)| row(y, dst));+ } else {+ for (y, dst) in out.chunks_exact_mut(w).enumerate() {+ row(y, dst);+ }+ }ImageU8::new(src.width, src.height, 1, out)}diff --git a/rust/crates/ml/src/cv/image.rs b/rust/crates/ml/src/cv/image.rsindex 08ab5db7f6..9d8797222e 100644--- a/rust/crates/ml/src/cv/image.rs+++ b/rust/crates/ml/src/cv/image.rs@@ -1,9 +1,6 @@use rayon::prelude::*;-use super::{OpResult, saturate_u8_f32};--/// Elementwise work below this many samples is not worth splitting across threads.-const PARALLEL_MIN_ELEMS: usize = 200_000;+use super::{OpResult, PARALLEL_MIN_ELEMS, saturate_u8_f32};#[derive(Clone, Debug, PartialEq, Eq)]pub(crate) struct ImageU8 {diff --git a/rust/crates/ml/src/cv/mod.rs b/rust/crates/ml/src/cv/mod.rsindex f77c38cc37..ca329a6d3b 100644--- a/rust/crates/ml/src/cv/mod.rs+++ b/rust/crates/ml/src/cv/mod.rs@@ -37,7 +37,9 @@ pub(crate) use structuring::ellipse_kernel;pub(crate) use transform::rotate_u8;pub(crate) use warp::warp_perspective;-const PARALLEL_MIN_PIXELS: usize = 65_536;+/// One answer for every elementwise op: below this many samples, threading+/// costs more than it saves.+pub(crate) const PARALLEL_MIN_ELEMS: usize = 200_000;const PIXELS_PER_CHUNK: usize = 65_536;pub(crate) fn pointwise<T: Sync, U: Send>(@@ -47,7 +49,7 @@ pub(crate) fn pointwise<T: Sync, U: Send>(src_stride: usize,f: impl Fn(&mut [U], &[T]) + Send + Sync,) {- if out.len() / out_stride.max(1) < PARALLEL_MIN_PIXELS {+ if out.len() < PARALLEL_MIN_ELEMS {f(out, src);return;}@@ -65,7 +67,7 @@ pub(crate) fn pointwise3<T: Sync, V: Sync, U: Send>(b_stride: usize,f: impl Fn(&mut [U], &[T], &[V]) + Send + Sync,) {- if out.len() / out_stride.max(1) < PARALLEL_MIN_PIXELS {+ if out.len() < PARALLEL_MIN_ELEMS {f(out, a, b);return;}diff --git a/rust/crates/ml/src/cv/morph.rs b/rust/crates/ml/src/cv/morph.rsindex 0560dac93a..c4809d34d9 100644--- a/rust/crates/ml/src/cv/morph.rs+++ b/rust/crates/ml/src/cv/morph.rs@@ -1,9 +1,7 @@use rayon::prelude::*;-use crate::cv::OpResult;use crate::cv::image::ImageU8;--const PARALLEL_MIN_PIXELS: usize = 200_000;+use crate::cv::{OpResult, PARALLEL_MIN_ELEMS};fn morph(src: &ImageU8, kernel: &ImageU8, erode: bool) -> OpResult<ImageU8> {if src.channels != 1 || kernel.channels != 1 {@@ -48,7 +46,7 @@ fn morph(src: &ImageU8, kernel: &ImageU8, erode: bool) -> OpResult<ImageU8> {}}};- if w * h >= PARALLEL_MIN_PIXELS {+ if w * h >= PARALLEL_MIN_ELEMS {out.par_chunks_mut(w).enumerate().for_each(|(y, dst)| row(y, dst));