fix: benchmark now works and exits correctly
cargo devel CI / cargo CI (push) Successful in 1m40s Details

This commit is contained in:
Christoph J. Scherr 2024-07-22 12:32:01 +02:00
parent 86b75e5160
commit 3b0eec9032
2 changed files with 14 additions and 4 deletions

View File

@ -2,7 +2,7 @@ use std::sync::atomic::AtomicBool;
use std::sync::{Arc, Mutex, RwLock}; use std::sync::{Arc, Mutex, RwLock};
use std::thread::JoinHandle; use std::thread::JoinHandle;
use libpt::log::info; use libpt::log::{debug, info};
use crate::error::WResult; use crate::error::WResult;
use crate::game::{self, GameBuilder}; use crate::game::{self, GameBuilder};
@ -76,7 +76,13 @@ where
let report = self.report_shared(); let report = self.report_shared();
let solver = self.solver(); let solver = self.solver();
// TODO: make this run in another thread somehow // TODO: make this run in another thread somehow
Self::bench(n, report, solver, &builder)?; self.bench(n, report, solver, builder)?;
debug!("finisihed the benchmark");
Ok(())
}
fn set_finished(&self, value: bool) -> WResult<()> {
self.finished
.store(value, std::sync::atomic::Ordering::Relaxed);
Ok(()) Ok(())
} }
} }

View File

@ -1,6 +1,7 @@
use std::fmt::Debug; use std::fmt::Debug;
use std::sync::{Arc, RwLock}; use std::sync::{Arc, RwLock};
use libpt::log::debug;
use rayon::prelude::*; use rayon::prelude::*;
use crate::error::WResult; use crate::error::WResult;
@ -46,6 +47,7 @@ where
// NOTE: This is blocking, use start to let it run in another thread // NOTE: This is blocking, use start to let it run in another thread
// FIXME: this never stops? Reports just keep getting printed // FIXME: this never stops? Reports just keep getting printed
fn bench( fn bench(
&self,
n: usize, n: usize,
report: Arc<RwLock<Report>>, report: Arc<RwLock<Report>>,
solver: SL, solver: SL,
@ -60,9 +62,10 @@ where
.expect("error playing the game during benchmark"); .expect("error playing the game during benchmark");
report.write().expect("lock is poisoned").add(r); report.write().expect("lock is poisoned").add(r);
}); });
libpt::log::info!("finished playing games, finalizing report");
report.write().expect("lock is poisoned").finalize(); report.write().expect("lock is poisoned").finalize();
debug!("finalized the report");
self.set_finished(true)?;
Ok(report.read().expect("lock is poisoned").clone()) Ok(report.read().expect("lock is poisoned").clone())
} }
// PERF: Somehow returning &Report would be better as we don't need to clone then // PERF: Somehow returning &Report would be better as we don't need to clone then
@ -70,4 +73,5 @@ where
fn report_shared(&'wl self) -> Arc<RwLock<Report>>; fn report_shared(&'wl self) -> Arc<RwLock<Report>>;
fn start(&'wl self, n: usize, builder: &'wl GameBuilder<'wl, WL>) -> WResult<()>; fn start(&'wl self, n: usize, builder: &'wl GameBuilder<'wl, WL>) -> WResult<()>;
fn is_finished(&self) -> bool; fn is_finished(&self) -> bool;
fn set_finished(&self, value: bool) -> WResult<()>;
} }