From a56705383fa8687f672a051063fe920f77974a21 Mon Sep 17 00:00:00 2001 From: randogoth Date: Sun, 7 Apr 2024 16:25:48 +0300 Subject: [PATCH] optional parallel processing --- readme.md | 4 +++- src/lib.rs | 33 +++++++++++++++++++++------------ 2 files changed, 24 insertions(+), 13 deletions(-) diff --git a/readme.md b/readme.md index dae142b..2261de9 100644 --- a/readme.md +++ b/readme.md @@ -44,7 +44,9 @@ fn main() { println!("Generated Delaunay triangulation"); // Pre-process triangles - xeno.preprocess(0); + // 1 - attractors, 2 - voids, 0 - both + // true/false - parallel processing + xeno.preprocess(0, false); // Execute delfin function with the generated GeometryData let min_area: f32 = 1000.0; // threshold for voidness diff --git a/src/lib.rs b/src/lib.rs index d134e5f..89854b0 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -258,18 +258,27 @@ impl Xenobalanus { self.triangulation = result.triangles } - pub fn preprocess(&mut self, types: usize) { - let geometry_data = Arc::new(Mutex::new(GeometryData::new())); - - self.triangulation.par_chunks(3).enumerate().for_each(|(index, tri_idx)| { - let gd = geometry_data.clone(); // Clone Arc for use in each thread, not the data itself - - // Perform locked update - let mut gd_lock = gd.lock().unwrap(); - gd_lock.add_triangle(index, &self.points, tri_idx, types); - }); - - self.geometry_data = Arc::try_unwrap(geometry_data).unwrap().into_inner().unwrap(); + pub fn preprocess(&mut self, types: usize, parallel: bool) { + if parallel { + + let geometry_data = Arc::new(Mutex::new(GeometryData::new())); + + self.triangulation.par_chunks(3).enumerate().for_each(|(index, tri_idx)| { + let gd = geometry_data.clone(); // Clone Arc for use in each thread, not the data itself + + // Perform locked update + let mut gd_lock = gd.lock().unwrap(); + gd_lock.add_triangle(index, &self.points, tri_idx, types); + }); + + self.geometry_data = Arc::try_unwrap(geometry_data).unwrap().into_inner().unwrap(); + } else { + + self.triangulation.chunks(3).enumerate().for_each(|(index, tri_idx)| { + self.geometry_data.add_triangle(index, &self.points, tri_idx, types); + }); + + } } pub fn delfin(