3 Commits

Author SHA1 Message Date
herel f0583c35cf use mutex for stats instead of yield at the end 2022-05-05 21:57:02 +02:00
herel fbf3b376c6 unfold neighbor functions to gain some speed 2022-05-05 21:20:08 +02:00
herel 13fc7e9bd5 configurable width & height 2022-05-05 20:56:32 +02:00
2 changed files with 62 additions and 58 deletions
+7 -1
View File
@@ -23,6 +23,12 @@ struct Args {
/// Display stats after each propagation /// Display stats after each propagation
#[clap(short, long)] #[clap(short, long)]
display: bool, display: bool,
/// Width of humans grid in population
#[clap(short, long, default_value_t = 1000)]
width: i32,
/// Height of humans grid in population
#[clap(short, long, default_value_t = 1000)]
height: i32,
} }
fn main() { fn main() {
@@ -33,7 +39,7 @@ fn main() {
.expect("Oops Looks like we have a problem here..."); .expect("Oops Looks like we have a problem here...");
let disease = Disease::new(20, 10, 5, String::from("Covid 44")); let disease = Disease::new(20, 10, 5, String::from("Covid 44"));
let mut population = Population::new(20, 10, 5, 1000, 1000, disease); let mut population = Population::new(20,10,5,args.width,args.height,disease);
//population.change_disease(disease); //population.change_disease(disease);
println!("After Filling"); println!("After Filling");
//population.display(); //population.display();
+55 -57
View File
@@ -1,5 +1,6 @@
use std::sync::Arc; use std::sync::Arc;
use std::sync::Mutex;
use std::thread; use std::thread;
use crate::prelude::*; use crate::prelude::*;
@@ -78,7 +79,7 @@ impl Population {
let len = humans.len(); let len = humans.len();
let mut humans_n_plus_1: Vec<Human> = Vec::with_capacity(len); let mut humans_n_plus_1: Vec<Human> = Vec::with_capacity(len);
let mut stats: [i32; 4] = [0, 0, 0, 0]; let stats: [i32; 4] = [0, 0, 0, 0];
let num_threads = num_threads.unwrap_or(1); let num_threads = num_threads.unwrap_or(1);
if num_threads > 48 { if num_threads > 48 {
@@ -93,6 +94,8 @@ impl Population {
let mut threads = vec![]; let mut threads = vec![];
let stats_arc = Arc::new(Mutex::new(stats));
for x in 0..num_threads { for x in 0..num_threads {
// find items to process // find items to process
@@ -104,6 +107,7 @@ impl Population {
// borrow copies of data for thread // borrow copies of data for thread
let humans = Arc::clone(&self.humans); let humans = Arc::clone(&self.humans);
let stats = Arc::clone(&stats_arc);
let (width, height, infection_rate, curing_rate, death_rate) = let (width, height, infection_rate, curing_rate, death_rate) =
(self.width, self.height, self.plague.infection_rate, self.plague.curing_rate, self.plague.death_rate); (self.width, self.height, self.plague.infection_rate, self.plague.curing_rate, self.plague.death_rate);
@@ -112,84 +116,78 @@ impl Population {
// no write on data // no write on data
// let humans = humans; // let humans = humans;
let mut humans_n_plus_1: Vec<Human> = Vec::with_capacity(num_items); let mut humans_n_plus_1: Vec<Human> = Vec::with_capacity(num_items);
let mut stats: [i32; 4] = [0, 0, 0, 0];
for idx in lower_bound..upper_bound { for idx in lower_bound..upper_bound {
let human = &humans[idx]; let human = &humans[idx];
let mut neighbors: Vec<&Human> = Vec::with_capacity(8); // let mut neighbors: Vec<&Human> = Vec::with_capacity(8);
if human.present_state == State::Normal { let mut new_human = human.clone();
let possible = [
(human.x - 1, human.y - 1), (human.x, human.y - 1), (human.x + 1, human.y - 1),
(human.x - 1, human.y) , (human.x + 1, human.y),
(human.x - 1, human.y + 1), (human.x, human.y + 1), (human.x + 1, human.y + 1),
];
for neigh_coords in possible.iter() {
let neigh_idx = point_to_index(neigh_coords.0, neigh_coords.1, width, height);
match neigh_idx {
Some(x) => neighbors.push(&humans[x]),
None => {},
}
}
}
let new_human = evolve(human, neighbors, infection_rate, curing_rate, death_rate);
match human.present_state { match human.present_state {
State::Normal => { stats[0] += 1; } State::Normal => {
State::Infected => { stats[1] += 1; } let possible = [
State::Immune => { stats[2] += 1; } (human.x - 1, human.y - 1), (human.x, human.y - 1), (human.x + 1, human.y - 1),
State::Dead => { stats[3] += 1; } (human.x - 1, human.y) , (human.x + 1, human.y),
(human.x - 1, human.y + 1), (human.x, human.y + 1), (human.x + 1, human.y + 1),
];
for neigh_coords in possible.iter() {
let neigh_idx = point_to_index(neigh_coords.0, neigh_coords.1, width, height);
match neigh_idx {
Some(x) => if humans[x].present_state == State::Infected {
if roll(infection_rate) {
new_human.present_state = State::Infected;
break;
}
}
None => {}
}
}
let mut stats = stats.lock().unwrap();
stats[0] += 1;
}
State::Infected => {
match die_or_cure(curing_rate, death_rate) {
Some(x) => new_human.present_state = x,
None => {}
}
let mut stats = stats.lock().unwrap();
stats[1] += 1;
}
State::Immune => {
let mut stats = stats.lock().unwrap();
stats[2] += 1;
}
State::Dead => {
let mut stats = stats.lock().unwrap();
stats[3] += 1;
}
} }
humans_n_plus_1.push(new_human); humans_n_plus_1.push(new_human);
} }
(humans_n_plus_1, stats) (humans_n_plus_1,)
})); }));
} }
for t in threads { for t in threads {
let mut res = t.join().unwrap(); let mut res = t.join().unwrap();
humans_n_plus_1.append(&mut res.0); humans_n_plus_1.append(&mut res.0);
for x in 0..4 { // for x in 0..4 {
stats[x] += res.1[x]; // stats[x] += res.1[x];
} // }
} }
self.humans = Arc::new(humans_n_plus_1); self.humans = Arc::new(humans_n_plus_1);
let stats = Arc::clone(&stats_arc);
let stats = *stats.lock().unwrap();
stats stats
} }
} }
fn evolve(human: &Human, neighbors: Vec<&Human>, infection_rate: i32, curing_rate: i32, death_rate: i32) -> Human { fn die_or_cure(curing_rate: i32, death_rate: i32) -> Option<State> {
let mut new_human = human.clone();
match human.present_state {
State::Normal => {
new_human.present_state = infect_by_neighbors(neighbors, infection_rate);
}
State::Infected => {
new_human.present_state = die_or_cure(curing_rate, death_rate);
}
State::Immune => {}
State::Dead => {}
}
new_human
}
fn infect_by_neighbors(neighbors: Vec<&Human>, infection_rate: i32) -> State {
for neighbor in neighbors.iter() {
if neighbor.present_state == State::Infected {
if roll(infection_rate) {
return State::Infected;
}
}
}
State::Normal
}
fn die_or_cure(curing_rate: i32, death_rate: i32) -> State {
if roll(curing_rate) { if roll(curing_rate) {
State::Immune Some(State::Immune)
} else if roll(death_rate) { } else if roll(death_rate) {
State::Dead Some(State::Dead)
} else { } else {
State::Infected None
} }
} }
@@ -201,7 +199,7 @@ fn point_to_index(x: i32, y: i32, width: i32, height: i32) -> Option<usize> {
} }
} }
pub fn roll(probability: i32) -> bool { fn roll(probability: i32) -> bool {
if probability > 0 { if probability > 0 {
let mut rng = rand::thread_rng(); let mut rng = rand::thread_rng();
rng.gen_range(0..CORRECTED_PERCENTAGE) <= probability rng.gen_range(0..CORRECTED_PERCENTAGE) <= probability