some inconsequential refactors to tidepool::main
This commit is contained in:
parent
cf9fd27ca0
commit
e673198704
|
@ -61,9 +61,11 @@ fn single(args: cli::SingleRun) -> Result<()> {
|
||||||
let (tx, rx) = create_mailbox();
|
let (tx, rx) = create_mailbox();
|
||||||
let exit_early = Arc::new(atomic::AtomicBool::new(false));
|
let exit_early = Arc::new(atomic::AtomicBool::new(false));
|
||||||
|
|
||||||
let exit_early2 = exit_early.clone();
|
std::thread::spawn({
|
||||||
let config2 = config.clone();
|
let config = config.clone();
|
||||||
std::thread::spawn(move || run_simulation(&args.data, config2, args.seed, 0, tx, exit_early2));
|
let exit_early = exit_early.clone();
|
||||||
|
move || run_simulation(&args.data, config, args.seed, 0, &tx, &exit_early)
|
||||||
|
});
|
||||||
|
|
||||||
while let Ok(msg) = rx.recv() {
|
while let Ok(msg) = rx.recv() {
|
||||||
tracing::trace!(msg = debug(&msg));
|
tracing::trace!(msg = debug(&msg));
|
||||||
|
@ -116,8 +118,8 @@ fn multi(args: cli::MultiRun) -> Result<()> {
|
||||||
let config = parse_config_file(&args.config_file)?;
|
let config = parse_config_file(&args.config_file)?;
|
||||||
|
|
||||||
let (tx, rx) = create_mailbox();
|
let (tx, rx) = create_mailbox();
|
||||||
let tasks = args.count.unwrap_or(u32::MAX);
|
let tasks = args.count.map_or(i64::MAX, |n| n as i64);
|
||||||
let tasks = Arc::new(atomic::AtomicI64::new(tasks as i64));
|
let tasks = Arc::new(atomic::AtomicI64::new(tasks));
|
||||||
let exit_early = Arc::new(atomic::AtomicBool::new(false));
|
let exit_early = Arc::new(atomic::AtomicBool::new(false));
|
||||||
|
|
||||||
let n_jobs = args.jobs.unwrap_or_else(num_cpus::get);
|
let n_jobs = args.jobs.unwrap_or_else(num_cpus::get);
|
||||||
|
@ -127,13 +129,13 @@ fn multi(args: cli::MultiRun) -> Result<()> {
|
||||||
tracing::debug!("spawning {n_jobs} jobs");
|
tracing::debug!("spawning {n_jobs} jobs");
|
||||||
for id in 0..n_jobs {
|
for id in 0..n_jobs {
|
||||||
jobs.insert(id, None);
|
jobs.insert(id, None);
|
||||||
let tx2 = tx.clone();
|
std::thread::spawn({
|
||||||
let tasks2 = tasks.clone();
|
let data_args = args.data.clone();
|
||||||
let exit_early2 = exit_early.clone();
|
let config = config.clone();
|
||||||
let config2 = config.clone();
|
let tx = tx.clone();
|
||||||
let data_args = args.data.clone();
|
let tasks = tasks.clone();
|
||||||
std::thread::spawn(move || {
|
let exit_early = exit_early.clone();
|
||||||
run_simulations(&data_args, config2, id, tx2, tasks2, exit_early2);
|
move || run_simulations(&data_args, config, id, &tx, &tasks, &exit_early)
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -335,8 +337,8 @@ fn run_simulation(
|
||||||
config: config::Config,
|
config: config::Config,
|
||||||
seed: Option<u64>,
|
seed: Option<u64>,
|
||||||
id: usize,
|
id: usize,
|
||||||
tx: mpsc::SyncSender<Msg>,
|
tx: &mpsc::SyncSender<Msg>,
|
||||||
exit_early: Arc<atomic::AtomicBool>,
|
exit_early: &atomic::AtomicBool,
|
||||||
) {
|
) {
|
||||||
let mut moves = data_args.list_moves.then(Vec::new);
|
let mut moves = data_args.list_moves.then(Vec::new);
|
||||||
let seed = seed.unwrap_or_else(|| rand::thread_rng().next_u64());
|
let seed = seed.unwrap_or_else(|| rand::thread_rng().next_u64());
|
||||||
|
@ -415,20 +417,16 @@ fn run_simulations(
|
||||||
data_args: &cli::OutputDataArgs,
|
data_args: &cli::OutputDataArgs,
|
||||||
config: config::Config,
|
config: config::Config,
|
||||||
id: usize,
|
id: usize,
|
||||||
tx: mpsc::SyncSender<Msg>,
|
tx: &mpsc::SyncSender<Msg>,
|
||||||
tasks: Arc<atomic::AtomicI64>,
|
tasks: &atomic::AtomicI64,
|
||||||
exit_early: Arc<atomic::AtomicBool>,
|
exit_early: &atomic::AtomicBool,
|
||||||
) {
|
) {
|
||||||
while tasks.fetch_sub(1, atomic::Ordering::Relaxed) > 0 {
|
while tasks.fetch_sub(1, atomic::Ordering::Relaxed) > 0 {
|
||||||
if exit_early.load(atomic::Ordering::Relaxed) {
|
if exit_early.load(atomic::Ordering::Relaxed) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
let seed = None;
|
run_simulation(data_args, config.clone(), None, id, tx, exit_early);
|
||||||
let config2 = config.clone();
|
|
||||||
let tx2 = tx.clone();
|
|
||||||
let exit_early2 = exit_early.clone();
|
|
||||||
run_simulation(&data_args, config2, seed, id, tx2, exit_early2);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if tx.send(Msg::Shutdown(id)).is_err() {
|
if tx.send(Msg::Shutdown(id)).is_err() {
|
||||||
|
|
Loading…
Reference in New Issue