at least it compiles
cargo devel CI / cargo CI (push) Successful in 1m36s Details

This commit is contained in:
Christoph J. Scherr 2024-04-17 13:32:04 +02:00
parent d161b27e42
commit 37ce70d23b
3 changed files with 21 additions and 35 deletions

View File

@ -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<Mutex<Report>>,
report: Arc<RwLock<Report>>,
benchth: Option<JoinHandle<()>>,
}
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<Mutex<Report>>, GameBuilder<'wl, WL>, SL), _index: usize) {
todo!()
}
fn run_bench(n: usize, report: Arc<Mutex<Report>>, 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<Mutex<Report>> {
fn report_shared(&'wl self) -> Arc<RwLock<Report>> {
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(())
}

View File

@ -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<GuessResponse> {
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<bool>;
// 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<Report> {
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<Mutex<Report>>;
fn report_shared(&'wl self) -> Arc<RwLock<Report>>;
}

View File

@ -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());
}