This commit is contained in:
Simon Gardling
2021-03-27 02:43:27 +00:00
parent a17b81c354
commit 06b9c0aef6
2 changed files with 127 additions and 106 deletions

View File

@@ -5,7 +5,7 @@ use rand::Rng;
use arrayfire as af; use arrayfire as af;
fn main() { fn main() {
let gpu_compute: bool = false; let gpu_compute: bool = true;
if gpu_compute { if gpu_compute {
backend_man(); backend_man();
// af::set_backend(af::Backend::CPU); // af::set_backend(af::Backend::CPU);
@@ -14,7 +14,7 @@ fn main() {
} }
// let n_iterations = 16384; // let n_iterations = 16384;
let n_iterations = 1000; let n_iterations = 2048;
// let n_iterations = 10; // let n_iterations = 10;
// let (width, height) = (512, 512); // let (width, height) = (512, 512);
@@ -23,12 +23,23 @@ fn main() {
// let n_particles = 1 << 22; // let n_particles = 1 << 22;
let n_particles = 1 << 24; let n_particles = 1 << 24;
// let n_particles = 1 << 10;
// let n_particles = 100; // let n_particles = 100;
println!("n_particles: {}", n_particles); println!("n_particles: {}", n_particles);
let diffusivity = 1; let diffusivity = 1;
let mut rng = rand::thread_rng(); let mut rng = rand::thread_rng();
let pb = ProgressBar::new(n_iterations);
// let n_populations = 1 + rng.gen_range(1..4);
let n_populations = 1;
let mut model = model::Model::new(width, height, n_particles, n_populations, diffusivity);
model.print_configurations();
if gpu_compute {
model.step_cl(n_iterations);
} else {
let pb = ProgressBar::new(n_iterations as u64);
pb.set_style( pb.set_style(
ProgressStyle::default_bar() ProgressStyle::default_bar()
.template( .template(
@@ -37,24 +48,13 @@ fn main() {
.progress_chars("#>-"), .progress_chars("#>-"),
); );
// let n_populations = 1 + rng.gen_range(1..4);
let n_populations = 10;
let mut model = model::Model::new(width, height, n_particles, n_populations, diffusivity);
model.print_configurations();
if gpu_compute {
let dims = af::Dim4::new(&[n_particles as u64, 1, 1, 1]);
for i in 0..n_iterations {
model.step_cl(dims);
pb.set_position(i);
}
} else {
for i in 0..n_iterations { for i in 0..n_iterations {
model.step(); model.step();
pb.set_position(i); pb.set_position(i as u64);
}
} }
pb.finish(); pb.finish();
}
println!("Rendering all saved image data...."); println!("Rendering all saved image data....");
model.render_all_imgdata(); model.render_all_imgdata();

View File

@@ -231,30 +231,48 @@ impl Model {
self.iteration += 1; self.iteration += 1;
} }
pub fn step_cl(&mut self, dims: af::Dim4) { pub fn step_cl(&mut self, steps: usize) {
// Combine grids let pb = ProgressBar::new(steps as u64);
let grids = &mut self.grids; pb.set_style(
combine(grids, &self.attraction_table); ProgressStyle::default_bar()
.template(
"{spinner:.green} [{elapsed_precise}] [{bar:40.cyan/blue}] {pos}/{len} ({eta} {percent}%, {per_sec})",
)
.progress_chars("#>-"),
);
println!("Starting tick for all agents...");
let agents_tick_time = Instant::now();
let agents_list = &*self.agents.clone(); let agents_list = &*self.agents.clone();
let agent_num: usize = self.agents.len() as usize; let agent_num: usize = self.agents.len() as usize;
// let dims = af::Dim4::new(&[self.agents.len() as u64, 1, 1, 1]); let dims = af::Dim4::new(&[self.agents.len() as u64, 1, 1, 1]);
let agent_angles_list: Vec<f32> = agents_list.iter().map(|agent| agent.angle).collect();
let agent_x_list: Vec<f32> = agents_list.iter().map(|agent| agent.x).collect();
let agent_y_list: Vec<f32> = agents_list.iter().map(|agent| agent.y).collect();
let mut sensor_distance_list: Vec<f32> = Vec::new(); let mut sensor_distance_list: Vec<f32> = Vec::new();
let mut sensor_angle_list: Vec<f32> = Vec::new(); let mut sensor_angle_list: Vec<f32> = Vec::new();
let mut rotation_angle_list: Vec<f32> = Vec::new(); let mut rotation_angle_list: Vec<f32> = Vec::new();
let mut step_distance_list: Vec<f32> = Vec::new(); let mut step_distance_list: Vec<f32> = Vec::new();
let mut agent_angles_list: Vec<f32> = Vec::new();
let mut agent_x_list: Vec<f32> = Vec::new();
let mut agent_y_list: Vec<f32> = Vec::new();
for i in 0..steps {
println!("Starting tick for all agents...");
let agents_tick_time = Instant::now();
// Combine grids
let grids = &mut self.grids;
combine(grids, &self.attraction_table);
agent_angles_list = agents_list.iter().map(|agent| agent.angle).collect();
agent_x_list = agents_list.iter().map(|agent| agent.x).collect();
agent_y_list = agents_list.iter().map(|agent| agent.y).collect();
for agent in &*self.agents.clone() { for agent in &*self.agents.clone() {
let grid = &grids[agent.population_id]; let grid = &grids.clone()[agent.population_id];
let PopulationConfig { let PopulationConfig {
sensor_distance, sensor_distance,
sensor_angle, sensor_angle,
@@ -341,6 +359,9 @@ impl Model {
self.save_image_data(); self.save_image_data();
self.iteration += 1; self.iteration += 1;
pb.set_position(i as u64);
}
pb.finish();
} }
fn to_vec<T:af::HasAfEnum+Default+Clone>(array: &af::Array<T>) -> Vec<T> { fn to_vec<T:af::HasAfEnum+Default+Clone>(array: &af::Array<T>) -> Vec<T> {