diff --git a/fish/benches/think.rs b/fish/benches/think.rs index fd67e58..87af3ac 100644 --- a/fish/benches/think.rs +++ b/fish/benches/think.rs @@ -59,7 +59,7 @@ const ITERS: u32 = 50_000; fn think(iters: u32, weights: &Weights, matrix: &Mat, queue: Queue<'_>) -> Option { let mut bot = fish::bot::Bot::new(weights, matrix, queue); - bot.think_for(iters); + bot.think(iters); bot.suggest() } diff --git a/fish/src/bot.rs b/fish/src/bot.rs index 784bb64..d8d2eb9 100644 --- a/fish/src/bot.rs +++ b/fish/src/bot.rs @@ -59,20 +59,12 @@ impl Bot { } } - /// Runs the bot for up to `gas` more iterations. An "iteration" is a unit of work - /// that is intentionally kept vague, but should be proportional to the amount CPU - /// time. Iterations are deterministic, so similar versions of the engine will produce - /// the same suggestions if run the for the same number of iterations. - pub fn think_for(&mut self, gas: u32) { - // NOTICE: The actual number of iterations may slightly exceed the provided gas due to - // how the bot is currently structured. This shouldn't have a substantial impact over - // the long run since the overshoot will be very small in terms of CPU - // time. - // - // Runs will be deterministic as long as two runs end on the same *target* - // iterations on the last call to `think_for`, e.g. "bot.think_for(5000)" is the - // same as "bot.think_for(2500); bot.think_for(5000 - bot.iterations());" - let max_iters = self.iters + gas; + /// Runs the bot, stopping it when it has done `max_iters` iterations since it was + /// first initialized. An "iteration" is an abstract unit of work that but should be + /// roughly proportional to the amount CPU time. Iterations are deterministic, so the + /// same version of the engine will produce the same suggestions if run the for the + /// same number of iterations. + pub fn think(&mut self, max_iters: u32) { while self.iters < max_iters { let did_update = self.algorithm.step( &self.arena, @@ -98,7 +90,7 @@ impl Bot { } /// Returns the number of iterations done so far. - pub fn iterations(&self) -> u32 { + pub fn iters(&self) -> u32 { self.iters } diff --git a/tidepool/src/main.rs b/tidepool/src/main.rs index bb836d2..6f6c683 100644 --- a/tidepool/src/main.rs +++ b/tidepool/src/main.rs @@ -388,12 +388,10 @@ fn run_simulation( bot.start_instrumenting(); } - // run the bot for a while; this could have been just `bot.think_for(iters)` but - // we limit the gas argument to prevent the thread from hanging too long while - // thinking. - while bot.iterations() < config.bot.iters { - let gas = std::cmp::min(50_000, config.bot.iters - bot.iterations()); - bot.think_for(gas); + while bot.iters() < config.bot.iters { + // limit iterations to ~25k to allow us to periodically check exit_early. + let max_iters = config.bot.iters.min(bot.iters() + 25_000); + bot.think(max_iters); // send a heartbeat back to the main thread to test if its still alive if tx.send(Msg::Heartbeat(id)).is_err() {