From f2eb1f57acf8b7024f388e4bdad8ee84704ff29e Mon Sep 17 00:00:00 2001 From: "Christoph J. Scherr" Date: Mon, 22 Jul 2024 12:17:11 +0200 Subject: [PATCH] fix: start to fix this mess (at least it compiles now) --- Cargo.toml | 2 +- src/bench/builtin.rs | 13 +++++-------- src/bench/mod.rs | 12 ++++++++---- src/bin/bench/cli.rs | 6 +++--- src/bin/game/cli.rs | 4 ++-- src/bin/solve/simple.rs | 4 ++-- src/solve/mod.rs | 17 +++++++++++++++++ 7 files changed, 38 insertions(+), 20 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 4e6d0de..68d345b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -27,7 +27,7 @@ anyhow = "1.0.81" chrono = { version = "0.4.37" } clap = { version = "4.5.3", features = ["derive"], optional = true } colored = { version = "2.1.0", optional = false } -libpt = "0.4.2" +libpt = "0.6.0" num_cpus = "1.16.0" rand = "0.8.5" rayon = "1.10.0" diff --git a/src/bench/builtin.rs b/src/bench/builtin.rs index 184d41b..c570fc8 100644 --- a/src/bench/builtin.rs +++ b/src/bench/builtin.rs @@ -17,7 +17,7 @@ pub struct BuiltinBenchmark<'wl, WL: WordList, SL: Solver<'wl, WL>> { builder: GameBuilder<'wl, WL>, report: Arc>, finished: AtomicBool, - bench_th: Arc>>>> // HACK: this is unholy + bench_th: Arc>>>>, // HACK: this is unholy } impl<'wl, WL, SL> Benchmark<'wl, WL, SL> for BuiltinBenchmark<'wl, WL, SL> @@ -40,7 +40,7 @@ where report: Arc::new(RwLock::new(Report::new(builder.build()?))), builder, finished: AtomicBool::new(false), - bench_th: Arc::new(Mutex::new(None)) + bench_th: Arc::new(Mutex::new(None)), }) } #[inline] @@ -72,14 +72,11 @@ where fn is_finished(&self) -> bool { self.finished.load(std::sync::atomic::Ordering::Relaxed) } - fn start(&'wl self, n: usize) -> WResult<()> { + fn start(&'wl self, n: usize, builder: &'wl GameBuilder<'wl, WL>) -> WResult<()> { let report = self.report_shared(); let solver = self.solver(); - let builder = self.builder(); - let th = std::thread::spawn(move||{ - Self::bench(n, report, solver, builder) - }); - *self.bench_th.lock().expect("lock is poisoned") = Some(th); + // TODO: make this run in another thread somehow + Self::bench(n, report, solver, &builder); Ok(()) } } diff --git a/src/bench/mod.rs b/src/bench/mod.rs index f60a430..4a3a8b5 100644 --- a/src/bench/mod.rs +++ b/src/bench/mod.rs @@ -44,14 +44,18 @@ where // 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 - fn bench(n: usize, report: Arc>, solver: SL, builder: GameBuilder<'wl, WL>) -> WResult { - + fn bench( + n: usize, + report: Arc>, + solver: SL, + builder: &'wl GameBuilder<'wl, WL>, + ) -> WResult { (0..n) .into_par_iter() .for_each_with(report.clone(), |outside_data, _i| { let report = outside_data; let r = solver - .play(&mut builder.build().expect("could not create game")) + .play_owned(builder.build().expect("could not create game")) .expect("error playing the game during benchmark"); report.write().expect("lock is poisoned").add(r); }); @@ -63,6 +67,6 @@ where // PERF: Somehow returning &Report would be better as we don't need to clone then fn report(&'wl self) -> Report; fn report_shared(&'wl self) -> Arc>; - fn start(&'wl self, n: usize) -> WResult<()>; + fn start(&'wl self, n: usize, builder: &'wl GameBuilder<'wl, WL>) -> WResult<()>; fn is_finished(&self) -> bool; } diff --git a/src/bin/bench/cli.rs b/src/bin/bench/cli.rs index 9dd234a..9b88ac0 100644 --- a/src/bin/bench/cli.rs +++ b/src/bin/bench/cli.rs @@ -48,9 +48,9 @@ struct Cli { fn main() -> anyhow::Result<()> { let cli = Cli::parse(); if cli.verbose { - Logger::build_mini(Some(Level::DEBUG))?; + Logger::builder().set_level(Level::DEBUG).build().unwrap(); } else { - Logger::build_mini(Some(Level::INFO))?; + Logger::builder().set_level(Level::INFO).build().unwrap(); } trace!("dumping CLI: {:#?}", cli); @@ -63,7 +63,7 @@ fn main() -> anyhow::Result<()> { let bench = BuiltinBenchmark::build(&wl, solver, builder, cli.threads)?; trace!("{bench:#?}"); - bench.start()?; + bench.start(50, &bench.builder())?; loop { sleep_ms(1000); diff --git a/src/bin/game/cli.rs b/src/bin/game/cli.rs index 3c74d21..1001e44 100644 --- a/src/bin/game/cli.rs +++ b/src/bin/game/cli.rs @@ -32,9 +32,9 @@ struct Cli { fn main() -> anyhow::Result<()> { let cli = Cli::parse(); if cli.verbose { - Logger::build_mini(Some(Level::TRACE))?; + Logger::builder().set_level(Level::DEBUG).build().unwrap(); } else { - Logger::build_mini(Some(Level::INFO))?; + Logger::builder().set_level(Level::INFO).build().unwrap(); } debug!("dumping CLI: {:#?}", cli); diff --git a/src/bin/solve/simple.rs b/src/bin/solve/simple.rs index bec7407..e235562 100644 --- a/src/bin/solve/simple.rs +++ b/src/bin/solve/simple.rs @@ -35,9 +35,9 @@ struct Cli { fn main() -> anyhow::Result<()> { let cli = Cli::parse(); if cli.verbose { - Logger::build_mini(Some(Level::TRACE))?; + Logger::builder().set_level(Level::DEBUG).build().unwrap(); } else { - Logger::build_mini(Some(Level::INFO))?; + Logger::builder().set_level(Level::INFO).build().unwrap(); } debug!("dumping CLI: {:#?}", cli); diff --git a/src/solve/mod.rs b/src/solve/mod.rs index 800f8a4..c7e08fb 100644 --- a/src/solve/mod.rs +++ b/src/solve/mod.rs @@ -68,6 +68,23 @@ pub trait Solver<'wl, WL: WordList>: Clone + std::fmt::Debug + Sized + Sync { } Ok(resp) } + /// Play a [Game] and return the last [GuessResponse]. + /// + /// Like [play](Solver::play) but takes an owned game instead of a mutable reference. + /// + /// # Errors + /// + /// This function will return an error if [make_a_move](Solver::make_a_move) fails. + fn play_owned(&self, mut game: Game<'wl, WL>) -> WResult { + let mut resp: GuessResponse; + loop { + resp = self.make_a_move(&mut game)?; + if game.finished() { + break; + } + } + Ok(resp) + } /// Play a [Game] and return the solution if one was found /// /// If no solution was found, this function will return [None].