From 37ce70d23b65e3dd95337fc0091c72d2e39343d9 Mon Sep 17 00:00:00 2001 From: PlexSheep Date: Wed, 17 Apr 2024 13:32:04 +0200 Subject: [PATCH] at least it compiles --- src/bench/builtin.rs | 42 ++++++++++++++---------------------------- src/bench/mod.rs | 12 ++++++------ src/bin/bench/cli.rs | 2 +- 3 files changed, 21 insertions(+), 35 deletions(-) diff --git a/src/bench/builtin.rs b/src/bench/builtin.rs index ed0d30c..adf49d5 100644 --- a/src/bench/builtin.rs +++ b/src/bench/builtin.rs @@ -1,4 +1,4 @@ -use std::sync::{Arc, Mutex}; +use std::sync::{Arc, Mutex, RwLock}; use std::thread::JoinHandle; use libpt::log::info; @@ -16,28 +16,9 @@ use rayon::prelude::*; pub struct BuiltinBenchmark<'wl, WL: WordList, SL: Solver<'wl, WL>> { solver: SL, builder: GameBuilder<'wl, WL>, - report: Arc>, + report: Arc>, benchth: Option>, } -impl<'wl, WL, SL> BuiltinBenchmark<'wl, WL, SL> -where - WL: WordList, - WL: 'wl, - SL: Solver<'wl, WL>, - SL: 'wl, - SL: Send, -{ - #[inline] - fn inner_bench(outside_data: (Arc>, GameBuilder<'wl, WL>, SL), _index: usize) { - todo!() - } - fn run_bench(n: usize, report: Arc>, builder: GameBuilder<'wl, WL>, solver: SL) { - (0..n) - .into_par_iter() - .map(|idx| Self::inner_bench((report.clone(), builder.clone(), solver.clone()), idx)); - report.lock().expect("lock is poisoned").finalize(); - } -} impl<'wl, WL, SL> Benchmark<'wl, WL, SL> for BuiltinBenchmark<'wl, WL, SL> where @@ -60,7 +41,7 @@ where .unwrap(); Ok(Self { solver, - report: Arc::new(Mutex::new(Report::new(builder.build()?))), + report: Arc::new(RwLock::new(Report::new(builder.build()?))), builder, benchth: None, }) @@ -78,21 +59,26 @@ where &self.builder } - fn report_mutex(&'wl self) -> Arc> { + fn report_shared(&'wl self) -> Arc> { self.report.clone() } fn report(&'wl self) -> super::Report { - self.report.lock().expect("lock is poisoned").clone() + self.report.read().expect("lock is poisoned").clone() } - fn start(&'wl mut self, n: usize) -> WResult<()> { - let report = self.report_mutex(); // FIXME: needs to keep self borrowed for some reason? + fn start(&'wl self, n: usize) -> WResult<()> { + let report = self.report_shared(); // FIXME: needs to keep self borrowed for some reason? let solver = self.solver(); let builder = self.builder(); - let benchth = std::thread::spawn(move || Self::run_bench(n, report, builder, solver)); + let benchth = std::thread::spawn({ + move || { + // TODO: do the stuff + report.write().expect("lock is poisoned").finalize(); + } + }); - self.benchth = Some(benchth); + // self.benchth = Some(benchth); Ok(()) } diff --git a/src/bench/mod.rs b/src/bench/mod.rs index 658aa1b..b674731 100644 --- a/src/bench/mod.rs +++ b/src/bench/mod.rs @@ -1,5 +1,5 @@ use std::fmt::Debug; -use std::sync::{Arc, Mutex}; +use std::sync::{Arc, Mutex, RwLock}; use libpt::log::debug; use rayon::prelude::*; @@ -42,14 +42,14 @@ where fn play(&'wl self) -> WResult { self.solver_ref().play(&mut self.make_game()?) } - fn start(&'wl mut self, n: usize) -> WResult<()>; + fn start(&'wl self, n: usize) -> WResult<()>; fn is_finished(&self) -> Option; // TODO: add some interface to get reports while the benchmark runs // TODO: make the benchmark optionally multithreaded // NOTE: This is blocking, use start to let it run in another thread #[deprecated] fn bench(&'wl self, n: usize) -> WResult { - let report = self.report_mutex(); + let report = self.report_shared(); let this = std::sync::Arc::new(self); (0..n) @@ -59,15 +59,15 @@ where let r = this .play() .expect("error playing the game during benchmark"); - report.lock().expect("lock is poisoned").add(r); + report.write().expect("lock is poisoned").add(r); }); - report.lock().expect("lock is poisoned").finalize(); + report.write().expect("lock is poisoned").finalize(); drop(report); Ok(self.report()) } // PERF: Somehow returning &Report would be better as we don't need to clone then fn report(&'wl self) -> Report; - fn report_mutex(&'wl self) -> Arc>; + fn report_shared(&'wl self) -> Arc>; } diff --git a/src/bin/bench/cli.rs b/src/bin/bench/cli.rs index 0480bd7..bb05494 100644 --- a/src/bin/bench/cli.rs +++ b/src/bin/bench/cli.rs @@ -65,7 +65,7 @@ fn main() -> anyhow::Result<()> { let n = cli.n; bench.start(n)?; - while !bench.is_finished()? { + while bench.is_finished().is_some_and(|b| b) { println!("{}", bench.report()); }