parallel grid deposit
This commit is contained in:
parent
b5bc14faa3
commit
99960c9523
@ -88,6 +88,8 @@ pub struct Grid {
|
||||
// Scratch space for the blur operation.
|
||||
buf: Vec<f32>,
|
||||
blur: Blur,
|
||||
|
||||
pub i: Vec<usize>,
|
||||
}
|
||||
|
||||
impl Clone for Grid {
|
||||
@ -99,13 +101,14 @@ impl Clone for Grid {
|
||||
data: self.data.clone(),
|
||||
buf: self.buf.clone(),
|
||||
blur: self.blur.clone(),
|
||||
i: self.i.clone(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Grid {
|
||||
// Create a new grid filled with random floats in the [0.0..1.0) range.
|
||||
pub fn new<R: Rng + ?Sized>(width: usize, height: usize, rng: &mut R) -> Self {
|
||||
pub fn new<R: Rng + ?Sized>(width: usize, height: usize, rng: &mut R, i: Vec<usize>) -> Self {
|
||||
if !width.is_power_of_two() || !height.is_power_of_two() {
|
||||
panic!("Grid dimensions must be a power of two.");
|
||||
}
|
||||
@ -119,6 +122,7 @@ impl Grid {
|
||||
config: PopulationConfig::new(rng),
|
||||
buf: vec![0.0; width * height],
|
||||
blur: Blur::new(width),
|
||||
i,
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -2,20 +2,22 @@ use physarum::model;
|
||||
|
||||
fn main() {
|
||||
// # of iterations to go through
|
||||
let n_iterations = 2048;
|
||||
let n_iterations = 256;
|
||||
|
||||
// Size of grid and pictures
|
||||
let (width, height) = (256, 256);
|
||||
// let (width, height) = (1024, 1024);
|
||||
|
||||
// # of agents
|
||||
let n_particles = 1 << 20;
|
||||
// let n_particles = 1 << 20;
|
||||
let n_particles = 1 << 16;
|
||||
println!("n_particles: {}", n_particles);
|
||||
|
||||
let diffusivity = 1;
|
||||
|
||||
// `n_populations` is the # of types of agents
|
||||
let n_populations = 1;
|
||||
// let n_populations = 1;
|
||||
let n_populations = 3;
|
||||
// let n_populations = 1 + rng.gen_range(1..4); // make # of populations between 2 and 5
|
||||
|
||||
let mut model = model::Model::new(width, height, n_particles, n_populations, diffusivity); // Create the model
|
||||
|
||||
38
src/model.rs
38
src/model.rs
@ -126,11 +126,11 @@ impl Model {
|
||||
}
|
||||
|
||||
Model {
|
||||
agents: (0..n_particles)
|
||||
agents: (0..(n_particles-1))
|
||||
.map(|i| Agent::new(width, height, i / particles_per_grid, &mut rng, i))
|
||||
.collect(),
|
||||
grids: (0..n_populations)
|
||||
.map(|_| Grid::new(width, height, &mut rng))
|
||||
.map(|_| Grid::new(width, height, &mut rng, vec![]))
|
||||
.collect(),
|
||||
attraction_table,
|
||||
diffusivity,
|
||||
@ -156,6 +156,24 @@ impl Model {
|
||||
|
||||
let mut time_per_agent_list: Vec<f64> = Vec::new();
|
||||
let mut time_per_step_list: Vec<f64> = Vec::new();
|
||||
|
||||
|
||||
println!("Doing some population stuff...");
|
||||
let time_1 = Instant::now();
|
||||
let mut i_pop: Vec<Vec<usize>> = Vec::new();
|
||||
for i in (0..(self.grids.len())) {
|
||||
i_pop.push(vec![]);
|
||||
}
|
||||
for agent in self.agents.iter() {
|
||||
i_pop[agent.population_id].push(agent.i);
|
||||
}
|
||||
|
||||
for i in (0..(self.grids.len())) {
|
||||
self.grids[i].i = i_pop[i].clone();
|
||||
}
|
||||
println!("Took {}ms", time_1.elapsed().as_millis());
|
||||
|
||||
let use_exp_deposit: bool = true;
|
||||
for i in 0..steps {
|
||||
if debug {
|
||||
println!("Starting tick for all agents...")
|
||||
@ -222,9 +240,19 @@ impl Model {
|
||||
);
|
||||
});
|
||||
|
||||
// Deposit // TODO - Make this parallel
|
||||
for agent in self.agents.iter() {
|
||||
self.grids[agent.population_id].deposit(agent.x, agent.y);
|
||||
// Deposit
|
||||
if use_exp_deposit {
|
||||
let agent_list = self.agents.clone();
|
||||
self.grids.par_iter_mut().for_each(|grid|{
|
||||
for i in (0..grid.i.len()) {
|
||||
let agent = &agent_list[i];
|
||||
grid.deposit(agent.x, agent.y);
|
||||
}
|
||||
});
|
||||
} else {
|
||||
for agent in self.agents.iter() {
|
||||
self.grids[agent.population_id].deposit(agent.x, agent.y);
|
||||
}
|
||||
}
|
||||
|
||||
// Diffuse + Decay
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user