not working multi threaded solution

This commit is contained in:
Fabian Schmidt 2024-04-09 15:07:55 +02:00
parent 9606b06313
commit 14e55e8c5c

View File

@ -2,55 +2,72 @@ use std::{
collections::HashMap, collections::HashMap,
fs::File, fs::File,
io::{BufRead, BufReader}, io::{BufRead, BufReader},
sync::{Arc, Mutex, MutexGuard},
thread,
}; };
struct StationMeasurements { struct StationMeasurements {
min: f64, min: f64,
max: f64, max: f64,
temps: Vec<f64>, count: usize,
sum: f64,
} }
fn main() { fn main() {
let mut stations: HashMap<String, StationMeasurements> = HashMap::new(); let stations: Arc<Mutex<HashMap<String, StationMeasurements>>> =
Arc::new(Mutex::new(HashMap::new()));
let cores: usize = std::thread::available_parallelism().unwrap().into();
let file = File::open("../measurements.txt").expect("File measurements.txt not found"); let file = File::open("../measurements.txt").expect("File measurements.txt not found");
let reader = BufReader::new(file); let reader = BufReader::new(file);
let mut line_num = 0; let lines = Arc::new(Mutex::new(reader.lines()));
for line_result in reader.lines() { let chunk_length = 1_000_000_000 / cores;
line_num += 1; let mut handles = vec![];
if line_num % 50000 == 0 { for _i in 0..cores {
println!("Calculated {line_num} stations"); let stations_clone = stations.clone();
} let lines_clone = lines.clone();
let line = line_result.expect("could not read line"); let handle = thread::spawn(move || {
let (station, temp) = line.split_once(';').unwrap(); let lines_guard = lines_clone.lock().unwrap();
let temp = temp.parse().unwrap(); let chunk = lines_guard.take(chunk_length);
let measurements_option = stations.get_mut(station); lines_guard.skip(chunk_length);
if let Some(measurements) = measurements_option { for line_result in chunk {
if temp < measurements.min { let line = line_result.expect("could not read line");
measurements.min = temp; let (station, temp) = line.split_once(';').unwrap();
} else if temp > measurements.max { let temp = temp.parse().unwrap();
measurements.max = temp; let mut stations_guard = stations_clone.lock().unwrap();
let measurements_option = stations_guard.get_mut(station);
if let Some(measurements) = measurements_option {
if temp < measurements.min {
measurements.min = temp;
} else if temp > measurements.max {
measurements.max = temp;
}
measurements.count += 1;
measurements.sum += temp;
} else {
let measurements = StationMeasurements {
min: temp,
max: temp,
count: 1,
sum: temp,
};
stations_clone
.lock()
.unwrap()
.insert(station.to_owned(), measurements);
}
} }
measurements.temps.push(temp); });
} else { handles.push(handle);
let measurements = StationMeasurements {
min: temp,
max: temp,
temps: vec![temp],
};
stations.insert(station.to_owned(), measurements);
}
} }
for (station, measurments) in stations { for handle in handles {
handle.join().unwrap();
}
for (station, measurments) in stations.lock().unwrap().iter() {
let min = measurments.min; let min = measurments.min;
let max = measurments.max; let max = measurments.max;
let avg = avg(measurments.temps); let avg = measurments.sum / measurments.count as f64;
println!("{station}={min}/{max}/{avg}"); println!("{station}={min}/{max}/{avg:.2}");
} }
} }
fn avg(temps: Vec<f64>) -> f64 {
let num_temps = temps.len() as f64;
let sum_temps: f64 = temps.iter().sum();
sum_temps / num_temps
}