From ab9574bf1e1f8295a1bae0f9f520d897c65524ab Mon Sep 17 00:00:00 2001 From: Russel Arbore <russel.jma@gmail.com> Date: Wed, 5 Mar 2025 09:59:03 -0600 Subject: [PATCH 1/2] edge opt --- .../benches/edge_detection_bench.rs | 2 - juno_samples/edge_detection/src/cpu.sch | 4 +- .../edge_detection/src/edge_detection.jn | 40 +++++++++---------- juno_samples/edge_detection/src/lib.rs | 2 - 4 files changed, 23 insertions(+), 25 deletions(-) diff --git a/juno_samples/edge_detection/benches/edge_detection_bench.rs b/juno_samples/edge_detection/benches/edge_detection_bench.rs index 76035275..49e67a29 100644 --- a/juno_samples/edge_detection/benches/edge_detection_bench.rs +++ b/juno_samples/edge_detection/benches/edge_detection_bench.rs @@ -88,8 +88,6 @@ fn edge_detection_bench(c: &mut Criterion) { height as u64, width as u64, gs as u64, - sz as u64, - sb as u64, input_h.to(), gaussian_filter_h, structure_h, diff --git a/juno_samples/edge_detection/src/cpu.sch b/juno_samples/edge_detection/src/cpu.sch index 64fee6b6..34431711 100644 --- a/juno_samples/edge_detection/src/cpu.sch +++ b/juno_samples/edge_detection/src/cpu.sch @@ -134,7 +134,9 @@ if !feature("seq") { reject_zero_crossings = reject_zero_crossings_body; } -async-call(edge_detection@le, edge_detection@zc); +if !feature("seq") { + async-call(edge_detection@le, edge_detection@zc); +} fork-split(gaussian_smoothing, laplacian_estimate, zero_crossings, gradient, reject_zero_crossings); unforkify(gaussian_smoothing, laplacian_estimate, zero_crossings, gradient, reject_zero_crossings); diff --git a/juno_samples/edge_detection/src/edge_detection.jn b/juno_samples/edge_detection/src/edge_detection.jn index 3e49cb36..da1977b6 100644 --- a/juno_samples/edge_detection/src/edge_detection.jn +++ b/juno_samples/edge_detection/src/edge_detection.jn @@ -13,12 +13,9 @@ fn gaussian_smoothing<n, m, gs : usize>( @filter_loop for i = 0 to gs { for j = 0 to gs { - let val = input[if row + i < gr then 0 - else if row + i - gr > n - 1 then n - 1 - else row + i - gr, - if col + j < gr then 0 - else if col + j - gr > m - 1 then m - 1 - else col + j - gr]; + let br = min!(max!(row + i, gr) - gr, n - 1); + let bc = min!(max!(col + j, gr) - gr, m - 1); + let val = input[br, bc]; smoothed += val * filter[i, j]; } } @@ -33,10 +30,11 @@ fn gaussian_smoothing<n, m, gs : usize>( const MIN_BR : f32 = 0; const MAX_BR : f32 = 1; -fn laplacian_estimate<n, m, sz: usize>( +fn laplacian_estimate<n, m : usize>( input: f32[n, m], - structure: f32[sz, sz], + structure: f32[3, 3], ) -> f32[n, m] { + const sz = 3; const r = sz / 2; @res let result : f32[n, m]; @@ -77,10 +75,11 @@ fn laplacian_estimate<n, m, sz: usize>( return result; } -fn zero_crossings<n, m, sz: usize>( +fn zero_crossings<n, m : usize>( input: f32[n, m], - structure: f32[sz, sz], + structure: f32[3, 3], ) -> f32[n, m] { + const sz = 3; const r = sz / 2; @res let result : f32[n, m]; @@ -123,11 +122,12 @@ fn zero_crossings<n, m, sz: usize>( return result; } -fn gradient<n, m, sb: usize>( +fn gradient<n, m : usize>( input: f32[n, m], - sx: f32[sb, sb], - sy: f32[sb, sb], + sx: f32[3, 3], + sy: f32[3, 3], ) -> f32[n, m] { + const sb = 3; const sbr = sb / 2; @res let result : f32[n, m]; @@ -191,18 +191,18 @@ fn reject_zero_crossings<n, m: usize>( } #[entry] -fn edge_detection<n, m, gs, sz, sb: usize>( +fn edge_detection<n, m, gs: usize>( input: f32[n, m], gaussian_filter: f32[gs, gs], - structure: f32[sz, sz], - sx: f32[sb, sb], - sy: f32[sb, sb], + structure: f32[3, 3], + sx: f32[3, 3], + sy: f32[3, 3], theta: f32, ) -> f32[n, m] { let smoothed = gaussian_smoothing::<n, m, gs>(input, gaussian_filter); - @le let laplacian = laplacian_estimate::<n, m, sz>(smoothed, structure); - @zc let zcs = zero_crossings::<n, m, sz>(laplacian, structure); - let gradient = gradient::<n, m, sb>(smoothed, sx, sy); + @le let laplacian = laplacian_estimate::<n, m>(smoothed, structure); + @zc let zcs = zero_crossings::<n, m>(laplacian, structure); + let gradient = gradient::<n, m>(smoothed, sx, sy); let maxgrad = max_gradient::<n, m>(gradient); return reject_zero_crossings::<n, m>(zcs, gradient, maxgrad, theta); } diff --git a/juno_samples/edge_detection/src/lib.rs b/juno_samples/edge_detection/src/lib.rs index aa44e2e7..3eaf38dc 100644 --- a/juno_samples/edge_detection/src/lib.rs +++ b/juno_samples/edge_detection/src/lib.rs @@ -195,8 +195,6 @@ pub fn edge_detection_harness(args: EdgeDetectionInputs) { height as u64, width as u64, gs as u64, - sz as u64, - sb as u64, input_h.to(), gaussian_filter_h.to(), structure_h.to(), -- GitLab From ba7cfb6a8872ca7975f36cf2f4aa3be317cc5cfa Mon Sep 17 00:00:00 2001 From: Russel Arbore <russel.jma@gmail.com> Date: Wed, 5 Mar 2025 10:01:44 -0600 Subject: [PATCH 2/2] Inline gaussian size --- .../benches/edge_detection_bench.rs | 1 - juno_samples/edge_detection/src/edge_detection.jn | 15 ++++++++------- juno_samples/edge_detection/src/lib.rs | 1 - 3 files changed, 8 insertions(+), 9 deletions(-) diff --git a/juno_samples/edge_detection/benches/edge_detection_bench.rs b/juno_samples/edge_detection/benches/edge_detection_bench.rs index 49e67a29..01ae418a 100644 --- a/juno_samples/edge_detection/benches/edge_detection_bench.rs +++ b/juno_samples/edge_detection/benches/edge_detection_bench.rs @@ -87,7 +87,6 @@ fn edge_detection_bench(c: &mut Criterion) { r.run( height as u64, width as u64, - gs as u64, input_h.to(), gaussian_filter_h, structure_h, diff --git a/juno_samples/edge_detection/src/edge_detection.jn b/juno_samples/edge_detection/src/edge_detection.jn index da1977b6..be987812 100644 --- a/juno_samples/edge_detection/src/edge_detection.jn +++ b/juno_samples/edge_detection/src/edge_detection.jn @@ -1,6 +1,10 @@ -fn gaussian_smoothing<n, m, gs : usize>( +const gs : usize = 7; +const sz : usize = 3; +const sb : usize = 3; + +fn gaussian_smoothing<n, m : usize>( input: f32[n, m], - filter: f32[gs, gs], + filter: f32[7, 7], ) -> f32[n, m] { @res let result : f32[n, m]; @@ -34,7 +38,6 @@ fn laplacian_estimate<n, m : usize>( input: f32[n, m], structure: f32[3, 3], ) -> f32[n, m] { - const sz = 3; const r = sz / 2; @res let result : f32[n, m]; @@ -79,7 +82,6 @@ fn zero_crossings<n, m : usize>( input: f32[n, m], structure: f32[3, 3], ) -> f32[n, m] { - const sz = 3; const r = sz / 2; @res let result : f32[n, m]; @@ -127,7 +129,6 @@ fn gradient<n, m : usize>( sx: f32[3, 3], sy: f32[3, 3], ) -> f32[n, m] { - const sb = 3; const sbr = sb / 2; @res let result : f32[n, m]; @@ -191,7 +192,7 @@ fn reject_zero_crossings<n, m: usize>( } #[entry] -fn edge_detection<n, m, gs: usize>( +fn edge_detection<n, m : usize>( input: f32[n, m], gaussian_filter: f32[gs, gs], structure: f32[3, 3], @@ -199,7 +200,7 @@ fn edge_detection<n, m, gs: usize>( sy: f32[3, 3], theta: f32, ) -> f32[n, m] { - let smoothed = gaussian_smoothing::<n, m, gs>(input, gaussian_filter); + let smoothed = gaussian_smoothing::<n, m>(input, gaussian_filter); @le let laplacian = laplacian_estimate::<n, m>(smoothed, structure); @zc let zcs = zero_crossings::<n, m>(laplacian, structure); let gradient = gradient::<n, m>(smoothed, sx, sy); diff --git a/juno_samples/edge_detection/src/lib.rs b/juno_samples/edge_detection/src/lib.rs index 3eaf38dc..c1d04b0f 100644 --- a/juno_samples/edge_detection/src/lib.rs +++ b/juno_samples/edge_detection/src/lib.rs @@ -194,7 +194,6 @@ pub fn edge_detection_harness(args: EdgeDetectionInputs) { r.run( height as u64, width as u64, - gs as u64, input_h.to(), gaussian_filter_h.to(), structure_h.to(), -- GitLab