forked from Maxluli/RustyPropagation
working version
remove const no need for RwLock as we don't use mut in thread \o/
This commit is contained in:
+11
-1
@@ -12,8 +12,18 @@ mod prelude {
|
||||
}
|
||||
|
||||
use prelude::*;
|
||||
use clap::Parser;
|
||||
|
||||
#[derive(Parser, Debug)]
|
||||
struct Args {
|
||||
/// Number of threads
|
||||
#[clap(short, long, default_value_t = 1)]
|
||||
threads: usize,
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let args = Args::parse();
|
||||
|
||||
let term = Term::stdout();
|
||||
term.write_line("********** Rusty Propagation (Console) 2022 **********")
|
||||
.expect("Oops Looks like we have a problem here...");
|
||||
@@ -29,7 +39,7 @@ fn main() {
|
||||
let mut counter: u32 = 0;
|
||||
loop {
|
||||
counter += 1;
|
||||
stats = population.propagate_new();
|
||||
stats = population.propagate_new(Some(args.threads));
|
||||
//population.display();
|
||||
println!(
|
||||
"Normal: {} Infecteds: {} Immunes: {} Deads: {}",
|
||||
|
||||
+141
-68
@@ -1,10 +1,13 @@
|
||||
|
||||
use std::sync::Arc;
|
||||
use std::thread;
|
||||
use crate::prelude::*;
|
||||
|
||||
pub struct Population {
|
||||
pub start_infected_ratio: i32,
|
||||
pub start_immune_ratio: i32,
|
||||
pub start_dead_ratio: i32,
|
||||
pub humans: Vec<Human>,
|
||||
pub humans: Arc<Vec<Human>>,
|
||||
pub width: i32,
|
||||
pub height: i32,
|
||||
pub age: i32,
|
||||
@@ -65,42 +68,91 @@ impl Population {
|
||||
height: height,
|
||||
plague: plague,
|
||||
age: 0,
|
||||
humans: the_humans,
|
||||
humans: Arc::new(the_humans),
|
||||
size: size,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn propagate_new(&mut self) -> [i32; 4] {
|
||||
let mut stats: [i32; 4] = [0, 0, 0, 0];
|
||||
let mut humans_n_plus_1: Vec<Human> = Vec::with_capacity(self.humans.len());
|
||||
pub fn propagate_new(&mut self, num_threads: Option<usize>) -> [i32; 4] {
|
||||
let humans = Arc::clone(&self.humans);
|
||||
let len = humans.len();
|
||||
|
||||
for human in self.humans.iter() {
|
||||
let mut neighbors: Vec<&Human> = Vec::with_capacity(8);
|
||||
if human.present_state == State::Normal {
|
||||
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, self.width, self.height);
|
||||
match neigh_idx {
|
||||
Some(x) => neighbors.push(&self.humans[x]),
|
||||
None => {},
|
||||
}
|
||||
}
|
||||
}
|
||||
let new_human = evolve(human, neighbors, self.plague.infection_rate, self.plague.curing_rate, self.plague.death_rate);
|
||||
match human.present_state {
|
||||
State::Normal => { stats[0] += 1; }
|
||||
State::Infected => { stats[1] += 1; }
|
||||
State::Immune => { stats[2] += 1; }
|
||||
State::Dead => { stats[3] += 1; }
|
||||
}
|
||||
humans_n_plus_1.push(new_human);
|
||||
let mut humans_n_plus_1: Vec<Human> = Vec::with_capacity(len);
|
||||
let mut stats: [i32; 4] = [0, 0, 0, 0];
|
||||
|
||||
let num_threads = num_threads.unwrap_or(1);
|
||||
if num_threads > 8 {
|
||||
panic!("too many threads")
|
||||
}
|
||||
|
||||
self.humans = humans_n_plus_1;
|
||||
// number of items per batch
|
||||
let mut num_items = (len / num_threads) as usize;
|
||||
if len % num_threads != 0 {
|
||||
num_items += 1;
|
||||
}
|
||||
|
||||
let mut threads = vec![];
|
||||
|
||||
for x in 0..num_threads {
|
||||
|
||||
// find items to process
|
||||
let lower_bound = x * num_items;
|
||||
let mut upper_bound = (x + 1) * num_items;
|
||||
if upper_bound > len {
|
||||
upper_bound = len;
|
||||
};
|
||||
|
||||
// borrow copies of data for thread
|
||||
let humans = Arc::clone(&self.humans);
|
||||
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);
|
||||
|
||||
threads.push(thread::spawn(move || {
|
||||
|
||||
// no write on data
|
||||
// let humans = humans;
|
||||
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 {
|
||||
let human = &humans[idx];
|
||||
let mut neighbors: Vec<&Human> = Vec::with_capacity(8);
|
||||
if human.present_state == State::Normal {
|
||||
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 {
|
||||
State::Normal => { stats[0] += 1; }
|
||||
State::Infected => { stats[1] += 1; }
|
||||
State::Immune => { stats[2] += 1; }
|
||||
State::Dead => { stats[3] += 1; }
|
||||
}
|
||||
humans_n_plus_1.push(new_human);
|
||||
}
|
||||
(humans_n_plus_1, stats)
|
||||
}));
|
||||
}
|
||||
|
||||
for t in threads {
|
||||
let mut res = t.join().unwrap();
|
||||
humans_n_plus_1.append(&mut res.0);
|
||||
for x in 0..4 {
|
||||
stats[x] += res.1[x];
|
||||
}
|
||||
}
|
||||
|
||||
self.humans = Arc::new(humans_n_plus_1);
|
||||
stats
|
||||
}
|
||||
}
|
||||
@@ -163,6 +215,8 @@ mod tests {
|
||||
use super::*;
|
||||
use parameterized::parameterized;
|
||||
|
||||
const THREADS: Option<usize> = Some(4);
|
||||
|
||||
#[derive(Debug)]
|
||||
struct Stats {
|
||||
normal: i32,
|
||||
@@ -251,14 +305,14 @@ mod tests {
|
||||
fn population_new() {
|
||||
let disease = Disease::new(20, 10, 5, String::from("Covid 44"));
|
||||
let (width, height) = (5, 7);
|
||||
let population = Population::new(20, 10, 5, 5, 7, disease);
|
||||
assert_eq!(population.humans.len(), 5 * 7);
|
||||
for h in population.humans.iter() {
|
||||
let population = Population::new(20, 10, 5, width, height, disease);
|
||||
let humans = Arc::clone(&population.humans);
|
||||
assert_eq!(humans.len(), 5 * 7);
|
||||
for h in humans.iter() {
|
||||
let idx = human_idx(h.x, h.y, width);
|
||||
assert_eq!(population.humans[idx].x, h.x, "coordinates should match");
|
||||
assert_eq!(population.humans[idx].y, h.y, "coordinates should match");
|
||||
assert_eq!(humans[idx].x, h.x, "coordinates should match");
|
||||
assert_eq!(humans[idx].y, h.y, "coordinates should match");
|
||||
}
|
||||
assert_eq!(population.humans.len(), (width * height) as usize);
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -267,7 +321,8 @@ mod tests {
|
||||
let (width, height) = (5, 7);
|
||||
let population = Population::new(20, 10, 5, 5, 7, disease);
|
||||
|
||||
let stats: Stats = humans_stats(&population.humans);
|
||||
let humans = Arc::clone(&population.humans);
|
||||
let stats: Stats = humans_stats(&humans);
|
||||
println!("Stats: {:?}", stats);
|
||||
|
||||
assert_eq!(
|
||||
@@ -285,25 +340,29 @@ mod tests {
|
||||
|
||||
disease = Disease::new(0, 0, 0, String::from("Test"));
|
||||
population = Population::new(0, 0, 0, width, height, disease);
|
||||
stats = humans_stats(&population.humans);
|
||||
let humans = Arc::clone(&population.humans);
|
||||
stats = humans_stats(&humans);
|
||||
println!("should be normal: {:?}", stats);
|
||||
assert_eq!(stats.normal, width * height);
|
||||
|
||||
disease = Disease::new(0, 0, 0, String::from("Test"));
|
||||
population = Population::new(100, 0, 0, width, height, disease);
|
||||
stats = humans_stats(&population.humans);
|
||||
let humans = Arc::clone(&population.humans);
|
||||
stats = humans_stats(&humans);
|
||||
println!("should be infected: {:?}", stats);
|
||||
assert_eq!(stats.infected, width * height);
|
||||
|
||||
disease = Disease::new(0, 0, 0, String::from("Test"));
|
||||
population = Population::new(0, 100, 0, width, height, disease);
|
||||
stats = humans_stats(&population.humans);
|
||||
let humans = Arc::clone(&population.humans);
|
||||
stats = humans_stats(&humans);
|
||||
println!("should be immune: {:?}", stats);
|
||||
assert_eq!(stats.immune, width * height);
|
||||
|
||||
disease = Disease::new(0, 0, 0, String::from("Test"));
|
||||
population = Population::new(0, 0, 100, width, height, disease);
|
||||
stats = humans_stats(&population.humans);
|
||||
let humans = Arc::clone(&population.humans);
|
||||
stats = humans_stats(&humans);
|
||||
println!("should be dead: {:?}", stats);
|
||||
assert_eq!(stats.dead, width * height);
|
||||
}
|
||||
@@ -329,13 +388,15 @@ mod tests {
|
||||
let mut propagate_stats: [i32; 4];
|
||||
|
||||
// infect every one
|
||||
stats = humans_stats(&population.humans);
|
||||
let humans = Arc::clone(&population.humans);
|
||||
stats = humans_stats(&humans);
|
||||
println!("stats after init: {:?}", stats);
|
||||
assert_eq!(stats.infected, 100, "everybody should be infected");
|
||||
|
||||
// kill every one
|
||||
propagate_stats = population.propagate_new();
|
||||
stats = humans_stats(&population.humans);
|
||||
propagate_stats = population.propagate_new(THREADS);
|
||||
let humans = Arc::clone(&population.humans);
|
||||
stats = humans_stats(&humans);
|
||||
println!("propate_stats: {:?}", propagate_stats);
|
||||
assert_eq!(propagate_stats, [0, 100, 0, 0]);
|
||||
assert_eq!(stats.normal, 0);
|
||||
@@ -344,8 +405,9 @@ mod tests {
|
||||
assert_eq!(stats.dead, 100);
|
||||
|
||||
for _x in 0..100 {
|
||||
propagate_stats = population.propagate_new();
|
||||
stats = humans_stats(&population.humans);
|
||||
propagate_stats = population.propagate_new(THREADS);
|
||||
let humans = Arc::clone(&population.humans);
|
||||
stats = humans_stats(&humans);
|
||||
println!("propate_stats: {:?}", propagate_stats);
|
||||
assert_eq!(propagate_stats, [0, 0, 0, 100]);
|
||||
assert_eq!(stats.normal, 0);
|
||||
@@ -363,7 +425,7 @@ mod tests {
|
||||
let mut propagate_stats: [i32; 4];
|
||||
|
||||
// start with normal population
|
||||
population.humans = vec![
|
||||
population.humans = Arc::new(vec![
|
||||
Human {
|
||||
present_state: State::Normal,
|
||||
x: 0,
|
||||
@@ -409,14 +471,16 @@ mod tests {
|
||||
x: 2,
|
||||
y: 2,
|
||||
},
|
||||
];
|
||||
stats = humans_stats(&population.humans);
|
||||
]);
|
||||
let humans = Arc::clone(&population.humans);
|
||||
stats = humans_stats(&humans);
|
||||
println!("stats after init: {:?}", stats);
|
||||
assert_eq!(stats.normal, 8);
|
||||
|
||||
// kill every one
|
||||
propagate_stats = population.propagate_new();
|
||||
stats = humans_stats(&population.humans);
|
||||
propagate_stats = population.propagate_new(THREADS);
|
||||
let humans = Arc::clone(&population.humans);
|
||||
stats = humans_stats(&humans);
|
||||
println!("propate_stats: {:?}", propagate_stats);
|
||||
assert_eq!(propagate_stats, [8, 1, 0, 0]);
|
||||
assert_eq!(stats.normal, 0);
|
||||
@@ -425,8 +489,9 @@ mod tests {
|
||||
assert_eq!(stats.dead, 0);
|
||||
|
||||
for _x in 0..100 {
|
||||
propagate_stats = population.propagate_new();
|
||||
stats = humans_stats(&population.humans);
|
||||
propagate_stats = population.propagate_new(THREADS);
|
||||
let humans = Arc::clone(&population.humans);
|
||||
stats = humans_stats(&humans);
|
||||
println!("propate_stats: {:?}", propagate_stats);
|
||||
assert_eq!(propagate_stats, [0, 9, 0, 0]);
|
||||
assert_eq!(stats.normal, 0);
|
||||
@@ -444,7 +509,7 @@ mod tests {
|
||||
let mut propagate_stats: [i32; 4];
|
||||
|
||||
// start with normal population
|
||||
population.humans = vec![
|
||||
population.humans = Arc::new(vec![
|
||||
Human {
|
||||
present_state: State::Normal,
|
||||
x: 0,
|
||||
@@ -490,14 +555,16 @@ mod tests {
|
||||
x: 2,
|
||||
y: 2,
|
||||
},
|
||||
];
|
||||
stats = humans_stats(&population.humans);
|
||||
]);
|
||||
let humans = Arc::clone(&population.humans);
|
||||
stats = humans_stats(&humans);
|
||||
println!("stats after init: {:?}", stats);
|
||||
assert_eq!(stats.normal, 8);
|
||||
|
||||
// infect every one
|
||||
propagate_stats = population.propagate_new();
|
||||
stats = humans_stats(&population.humans);
|
||||
propagate_stats = population.propagate_new(THREADS);
|
||||
let humans = Arc::clone(&population.humans);
|
||||
stats = humans_stats(&humans);
|
||||
println!("propate_stats: {:?}", propagate_stats);
|
||||
println!("population: {:?}", stats);
|
||||
assert_eq!(propagate_stats, [8, 1, 0, 0]);
|
||||
@@ -507,8 +574,9 @@ mod tests {
|
||||
assert_eq!(stats.dead, 0);
|
||||
|
||||
// cure every one
|
||||
propagate_stats = population.propagate_new();
|
||||
stats = humans_stats(&population.humans);
|
||||
propagate_stats = population.propagate_new(THREADS);
|
||||
let humans = Arc::clone(&population.humans);
|
||||
stats = humans_stats(&humans);
|
||||
println!("propate_stats: {:?}", propagate_stats);
|
||||
println!("population: {:?}", stats);
|
||||
assert_eq!(propagate_stats, [0, 8, 1, 0]);
|
||||
@@ -519,8 +587,9 @@ mod tests {
|
||||
|
||||
// then
|
||||
for _x in 0..100 {
|
||||
propagate_stats = population.propagate_new();
|
||||
stats = humans_stats(&population.humans);
|
||||
propagate_stats = population.propagate_new(THREADS);
|
||||
let humans = Arc::clone(&population.humans);
|
||||
stats = humans_stats(&humans);
|
||||
println!("propate_stats: {:?}", propagate_stats);
|
||||
println!("population: {:?}", stats);
|
||||
assert_eq!(propagate_stats, [0, 0, 9, 0]);
|
||||
@@ -539,16 +608,18 @@ mod tests {
|
||||
let stats_before: Stats;
|
||||
let stats_after: Stats;
|
||||
|
||||
stats_before = humans_stats(&population.humans);
|
||||
let humans = Arc::clone(&population.humans);
|
||||
stats_before = humans_stats(&humans);
|
||||
let should_be_infected = population.size as i32 * infection_start / 100;
|
||||
let tolerance = (should_be_infected as f32 * 0.20) as i32;
|
||||
println!("{:?}", stats_before);
|
||||
assert!(stats_before.infected <= should_be_infected + tolerance, "{} infected, should be less than {}", stats_before.infected, should_be_infected + tolerance);
|
||||
assert!(stats_before.infected >= should_be_infected - tolerance, "{} infected, should be more than {}", stats_before.infected, should_be_infected - tolerance);
|
||||
|
||||
population.propagate_new();
|
||||
population.propagate_new(THREADS);
|
||||
|
||||
stats_after = humans_stats(&population.humans);
|
||||
let humans = Arc::clone(&population.humans);
|
||||
stats_after = humans_stats(&humans);
|
||||
assert_eq!(stats_before.infected, stats_after.infected, "no one should have been infected");
|
||||
}
|
||||
|
||||
@@ -568,7 +639,8 @@ mod tests {
|
||||
disease = Disease::new(infection_rate, 0, death_rate, String::from("Test"));
|
||||
population = Population::new(start_infected, 0, 0, width, height, disease);
|
||||
|
||||
stats = humans_stats(&population.humans);
|
||||
let humans = Arc::clone(&population.humans);
|
||||
stats = humans_stats(&humans);
|
||||
println!("Population after init: {:?}", stats);
|
||||
|
||||
// total * proba - 20% < infected < total * proba + 20%
|
||||
@@ -581,7 +653,7 @@ mod tests {
|
||||
let infected_at_start = stats.infected;
|
||||
let dead_at_start = stats.dead;
|
||||
|
||||
let propa_stats: [i32; 4] = population.propagate_new();
|
||||
let propa_stats: [i32; 4] = population.propagate_new(THREADS);
|
||||
|
||||
assert!(propa_stats[3] >= dead_at_start);
|
||||
|
||||
@@ -589,7 +661,8 @@ mod tests {
|
||||
assert_eq!(propa_stats[3], 0, "no human should have died");
|
||||
}
|
||||
|
||||
stats = humans_stats(&population.humans);
|
||||
let humans = Arc::clone(&population.humans);
|
||||
stats = humans_stats(&humans);
|
||||
println!("Population after propagate: {:?}", stats);
|
||||
|
||||
assert!(stats.infected <= infected_at_start + width * height * infected_expected);
|
||||
|
||||
Reference in New Issue
Block a user