some cleanups
This commit is contained in:
parent
d3342faed2
commit
041d22418e
@ -1,7 +1,7 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "physarum"
|
name = "physarum"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
authors = ["mindv0rtex <mindv0rtex@users.noreply.github.com>"]
|
authors = ["mindv0rtex <mindv0rtex@users.noreply.github.com>", "Simon Gardling <titaniumtown@gmail.com>"]
|
||||||
edition = "2018"
|
edition = "2018"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
@ -24,10 +24,12 @@ harness = false
|
|||||||
codegen-units = 1
|
codegen-units = 1
|
||||||
opt-level = 3
|
opt-level = 3
|
||||||
target-cpu = "native"
|
target-cpu = "native"
|
||||||
lto = "thin"
|
lto = "fat"
|
||||||
|
debug = true
|
||||||
|
|
||||||
[profile.release]
|
[profile.release]
|
||||||
codegen-units = 1
|
codegen-units = 1
|
||||||
opt-level = 3
|
opt-level = 3
|
||||||
target-cpu = "native"
|
target-cpu = "native"
|
||||||
lto = "fat"
|
lto = "fat"
|
||||||
|
debug = false
|
||||||
15
src/blur.rs
15
src/blur.rs
@ -38,8 +38,7 @@ impl Blur {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Approximate 1D Gaussian filter of standard deviation sigma with N box filter passes. Each
|
/// 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
|
/// element in the output array contains the radius of the box filter for the corresponding pass.
|
||||||
/// pass.
|
|
||||||
fn boxes_for_gaussian<const N: usize>(sigma: f32) -> ([usize; N]) {
|
fn boxes_for_gaussian<const N: usize>(sigma: f32) -> ([usize; N]) {
|
||||||
let w_ideal = (12.0 * sigma * sigma / N as f32 + 1.0).sqrt();
|
let w_ideal = (12.0 * sigma * sigma / N as f32 + 1.0).sqrt();
|
||||||
let mut w = w_ideal as usize;
|
let mut w = w_ideal as usize;
|
||||||
@ -79,13 +78,14 @@ impl Blur {
|
|||||||
.for_each(|(src_row, dst_row)| {
|
.for_each(|(src_row, dst_row)| {
|
||||||
// First we build a value for the beginning of each row. We assume periodic boundary
|
// 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.
|
// 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];
|
let mut value = src_row[width - radius - 1];
|
||||||
for j in 0..radius {
|
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() {
|
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);
|
let right = (i + radius) & (width - 1);
|
||||||
value += src_row[right] - src_row[left];
|
value += src_row[right] - src_row[left];
|
||||||
*dst_elem = value * weight;
|
*dst_elem = value * weight;
|
||||||
@ -109,12 +109,13 @@ impl Blur {
|
|||||||
// We don't replicate the horizontal filter logic because of the cache-unfriendly memory
|
// 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
|
// access patterns of sequential iteration over individual columns. Instead, we iterate over
|
||||||
// rows via loop interchange.
|
// 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
|
self.row_buffer
|
||||||
.copy_from_slice(&src[offset..offset + width]);
|
.copy_from_slice(&src[offset..offset + width]);
|
||||||
|
|
||||||
for j in 0..radius {
|
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 bottom_row = &src[bottom_off..bottom_off + width];
|
||||||
let top_off = j * width;
|
let top_off = j * width;
|
||||||
let top_row = &src[top_off..top_off + 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.
|
// 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() {
|
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 bottom_row = &src[bottom_off..bottom_off + width];
|
||||||
let top_off = ((i + radius) & (height - 1)) * width;
|
let top_off = ((i + radius) & (height - 1)) * width;
|
||||||
let top_row = &src[top_off..top_off + width];
|
let top_row = &src[top_off..top_off + width];
|
||||||
|
|||||||
@ -5,7 +5,7 @@ use rand::Rng;
|
|||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
// let n_iterations = 16384;
|
// let n_iterations = 16384;
|
||||||
let n_iterations = 1024;
|
let n_iterations = 10;
|
||||||
// let n_iterations = 100;
|
// let n_iterations = 100;
|
||||||
// let n_iterations = 10;
|
// let n_iterations = 10;
|
||||||
|
|
||||||
@ -22,10 +22,8 @@ fn main() {
|
|||||||
let diffusivity = 1;
|
let diffusivity = 1;
|
||||||
let mut rng = rand::thread_rng();
|
let mut rng = rand::thread_rng();
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// let n_populations = 1 + rng.gen_range(1..4);
|
// 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);
|
let mut model = model::Model::new(width, height, n_particles, n_populations, diffusivity);
|
||||||
model.print_configurations();
|
model.print_configurations();
|
||||||
|
|
||||||
|
|||||||
35
src/model.rs
35
src/model.rs
@ -170,7 +170,7 @@ impl Model {
|
|||||||
|
|
||||||
/// Simulates `steps` # of steps
|
/// Simulates `steps` # of steps
|
||||||
pub fn run(&mut self, steps: usize) {
|
pub fn run(&mut self, steps: usize) {
|
||||||
let debug: bool = true;
|
let debug: bool = false;
|
||||||
|
|
||||||
let pb = ProgressBar::new(steps as u64);
|
let pb = ProgressBar::new(steps as u64);
|
||||||
pb.set_style(
|
pb.set_style(
|
||||||
@ -227,16 +227,6 @@ impl Model {
|
|||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
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
|
// Deposit
|
||||||
for agent in self.agents.iter() {
|
for agent in self.agents.iter() {
|
||||||
self.grids[agent.population_id].deposit(agent.x, agent.y);
|
self.grids[agent.population_id].deposit(agent.x, agent.y);
|
||||||
@ -249,6 +239,16 @@ impl Model {
|
|||||||
});
|
});
|
||||||
|
|
||||||
self.save_image_data();
|
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;
|
self.iteration += 1;
|
||||||
pb.set_position(i as u64);
|
pb.set_position(i as u64);
|
||||||
}
|
}
|
||||||
@ -256,12 +256,21 @@ impl Model {
|
|||||||
|
|
||||||
let avg_per_step: f64 = time_per_step_list.iter().sum::<f64>() as f64 / time_per_step_list.len() as f64;
|
let avg_per_step: f64 = time_per_step_list.iter().sum::<f64>() as f64 / time_per_step_list.len() as f64;
|
||||||
let avg_per_agent: f64 = time_per_agent_list.iter().sum::<f64>() as f64 / time_per_agent_list.len() as f64;
|
let avg_per_agent: f64 = time_per_agent_list.iter().sum::<f64>() 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) {
|
fn save_image_data(&mut self) {
|
||||||
let grids = self.grids.clone();
|
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) {
|
pub fn flush_image_data(&mut self) {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user