From 041d22418e0c0c8be25edbc2ce5799c274f09768 Mon Sep 17 00:00:00 2001 From: Simon Gardling Date: Mon, 29 Mar 2021 13:35:37 +0000 Subject: [PATCH] some cleanups --- Cargo.toml | 8 +++++--- src/blur.rs | 15 ++++++++------- src/main.rs | 6 ++---- src/model.rs | 35 ++++++++++++++++++++++------------- 4 files changed, 37 insertions(+), 27 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 6f02c8c..f5341fd 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "physarum" version = "0.1.0" -authors = ["mindv0rtex "] +authors = ["mindv0rtex ", "Simon Gardling "] edition = "2018" [dependencies] @@ -24,10 +24,12 @@ harness = false codegen-units = 1 opt-level = 3 target-cpu = "native" -lto = "thin" +lto = "fat" +debug = true [profile.release] codegen-units = 1 opt-level = 3 target-cpu = "native" -lto = "fat" \ No newline at end of file +lto = "fat" +debug = false \ No newline at end of file diff --git a/src/blur.rs b/src/blur.rs index b3bed17..288d00d 100644 --- a/src/blur.rs +++ b/src/blur.rs @@ -38,8 +38,7 @@ impl Blur { } /// Approximate 1D Gaussian filter of standard deviation sigma with N box filter passes. Each - /// element in the output array contains the radius of the box filter for the corresponding - /// pass. + /// element in the output array contains the radius of the box filter for the corresponding pass. fn boxes_for_gaussian(sigma: f32) -> ([usize; N]) { let w_ideal = (12.0 * sigma * sigma / N as f32 + 1.0).sqrt(); let mut w = w_ideal as usize; @@ -79,13 +78,14 @@ impl Blur { .for_each(|(src_row, dst_row)| { // First we build a value for the beginning of each row. We assume periodic boundary // conditions, so we need to push the left index to the opposite side of the row. + let width_sub_radius = width - radius; let mut value = src_row[width - radius - 1]; for j in 0..radius { - value += src_row[width - radius + j] + src_row[j]; + value += src_row[width_sub_radius + j] + src_row[j]; } for (i, dst_elem) in dst_row.iter_mut().enumerate() { - let left = (i + width - radius - 1) & (width - 1); + let left = (i + width_sub_radius - 1) & (width - 1); let right = (i + radius) & (width - 1); value += src_row[right] - src_row[left]; *dst_elem = value * weight; @@ -109,12 +109,13 @@ impl Blur { // We don't replicate the horizontal filter logic because of the cache-unfriendly memory // access patterns of sequential iteration over individual columns. Instead, we iterate over // rows via loop interchange. - let offset = (height - radius - 1) * width; + let height_sub_radius = height - radius; + let offset = (height_sub_radius - 1) * width; self.row_buffer .copy_from_slice(&src[offset..offset + width]); for j in 0..radius { - let bottom_off = (height - radius + j) * width; + let bottom_off = (height_sub_radius + j) * width; let bottom_row = &src[bottom_off..bottom_off + width]; let top_off = j * width; let top_row = &src[top_off..top_off + width]; @@ -126,7 +127,7 @@ impl Blur { // The outer loop cannot be parallelized because we need to use the buffer sequentially. for (i, dst_row) in dst.chunks_exact_mut(width).enumerate() { - let bottom_off = ((i + height - radius - 1) & (height - 1)) * width; + let bottom_off = ((i + height_sub_radius - 1) & (height - 1)) * width; let bottom_row = &src[bottom_off..bottom_off + width]; let top_off = ((i + radius) & (height - 1)) * width; let top_row = &src[top_off..top_off + width]; diff --git a/src/main.rs b/src/main.rs index 2e5bc30..e47cb2f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -5,7 +5,7 @@ use rand::Rng; fn main() { // let n_iterations = 16384; - let n_iterations = 1024; + let n_iterations = 10; // let n_iterations = 100; // let n_iterations = 10; @@ -22,10 +22,8 @@ fn main() { let diffusivity = 1; let mut rng = rand::thread_rng(); - - // let n_populations = 1 + rng.gen_range(1..4); - let n_populations = 1; + let n_populations = 2; let mut model = model::Model::new(width, height, n_particles, n_populations, diffusivity); model.print_configurations(); diff --git a/src/model.rs b/src/model.rs index a1c4b11..fdb8585 100644 --- a/src/model.rs +++ b/src/model.rs @@ -170,7 +170,7 @@ impl Model { /// Simulates `steps` # of steps pub fn run(&mut self, steps: usize) { - let debug: bool = true; + let debug: bool = false; let pb = ProgressBar::new(steps as u64); pb.set_style( @@ -226,16 +226,6 @@ impl Model { agent.rotate_and_move(direction, rotation_angle, step_distance, width, height); }); - - let agents_tick_elapsed: f64 = agents_tick_time.elapsed().as_millis() as f64; - let ms_per_agent: f64 = (agents_tick_elapsed as f64) / (self.agents.len() as f64); - time_per_agent_list.push(ms_per_agent); - time_per_step_list.push(agents_tick_elapsed); - - if debug { - println!("Finished tick for all agents. took {}ms\nTime per agent: {}ms\n", agents_tick_elapsed, ms_per_agent); - } - // Deposit for agent in self.agents.iter() { @@ -249,6 +239,16 @@ impl Model { }); self.save_image_data(); + + let agents_tick_elapsed: f64 = agents_tick_time.elapsed().as_millis() as f64; + let ms_per_agent: f64 = (agents_tick_elapsed as f64) / (self.agents.len() as f64); + time_per_agent_list.push(ms_per_agent); + time_per_step_list.push(agents_tick_elapsed); + + if debug { + println!("Finished tick for all agents. took {}ms\nTime per agent: {}ms\n", agents_tick_elapsed, ms_per_agent); + } + self.iteration += 1; pb.set_position(i as u64); } @@ -256,12 +256,21 @@ impl Model { let avg_per_step: f64 = time_per_step_list.iter().sum::() as f64 / time_per_step_list.len() as f64; let avg_per_agent: f64 = time_per_agent_list.iter().sum::() as f64 / time_per_agent_list.len() as f64; - println!("Average time per step: {}\nAverage time per agent: {}", avg_per_step, avg_per_agent); + println!("Average time per step: {}ms\nAverage time per agent: {}ms", avg_per_step, avg_per_agent); } fn save_image_data(&mut self) { let grids = self.grids.clone(); - self.img_data_vec.push(ImgData::new(grids, self.palette, self.iteration)); + let img_data = ImgData::new(grids, self.palette, self.iteration); + self.img_data_vec.push(img_data); + if self.grids[0].width > 1024 && self.grids[0].height > 1024 { + if self.img_data_vec.len() > 100 { + self.render_all_imgdata(); + self.flush_image_data(); + return; + } + } + } pub fn flush_image_data(&mut self) {