1
0
Fork 0
mirror of https://github.com/sockspls/badfish synced 2025-04-29 16:23:09 +00:00

Refactor elapsed time checks in search

Small improvement of the elapsed time usage in search, makes the code easier to read overall.
Also Search::Worker::iterative_deepening() now only checks the elapsed time once, instead of 3 times in a row.

Non Regression STC:
https://tests.stockfishchess.org/tests/view/6617005d5a4693796d965c3c
LLR: 2.97 (-2.94,2.94) <-1.75,0.25>
Total: 61024 W: 16002 L: 15806 D: 29216
Ptnml(0-2): 243, 6874, 16102, 7030, 263

closes https://github.com/official-stockfish/Stockfish/pull/5163

No functional change
This commit is contained in:
Disservin 2024-04-10 23:10:07 +02:00 committed by Joost VandeVondele
parent 432995ad82
commit d3fc1d835e
4 changed files with 21 additions and 18 deletions

View file

@ -340,7 +340,7 @@ void Search::Worker::iterative_deepening() {
// When failing high/low give some update (without cluttering // When failing high/low give some update (without cluttering
// the UI) before a re-search. // the UI) before a re-search.
if (mainThread && multiPV == 1 && (bestValue <= alpha || bestValue >= beta) if (mainThread && multiPV == 1 && (bestValue <= alpha || bestValue >= beta)
&& mainThread->tm.elapsed(threads.nodes_searched()) > 3000) && elapsed() > 3000)
main_manager()->pv(*this, threads, tt, rootDepth); main_manager()->pv(*this, threads, tt, rootDepth);
// In case of failing low/high increase aspiration window and // In case of failing low/high increase aspiration window and
@ -371,8 +371,7 @@ void Search::Worker::iterative_deepening() {
std::stable_sort(rootMoves.begin() + pvFirst, rootMoves.begin() + pvIdx + 1); std::stable_sort(rootMoves.begin() + pvFirst, rootMoves.begin() + pvIdx + 1);
if (mainThread if (mainThread
&& (threads.stop || pvIdx + 1 == multiPV && (threads.stop || pvIdx + 1 == multiPV || elapsed() > 3000)
|| mainThread->tm.elapsed(threads.nodes_searched()) > 3000)
// A thread that aborted search can have mated-in/TB-loss PV and score // A thread that aborted search can have mated-in/TB-loss PV and score
// that cannot be trusted, i.e. it can be delayed or refuted if we would have // that cannot be trusted, i.e. it can be delayed or refuted if we would have
// had time to fully search other root-moves. Thus we suppress this output and // had time to fully search other root-moves. Thus we suppress this output and
@ -448,13 +447,14 @@ void Search::Worker::iterative_deepening() {
if (rootMoves.size() == 1) if (rootMoves.size() == 1)
totalTime = std::min(500.0, totalTime); totalTime = std::min(500.0, totalTime);
if (completedDepth >= 10 && nodesEffort >= 97 auto elapsedTime = elapsed();
&& mainThread->tm.elapsed(threads.nodes_searched()) > totalTime * 0.739
if (completedDepth >= 10 && nodesEffort >= 97 && elapsedTime > totalTime * 0.739
&& !mainThread->ponder) && !mainThread->ponder)
threads.stop = true; threads.stop = true;
// Stop the search if we have exceeded the totalTime // Stop the search if we have exceeded the totalTime
if (mainThread->tm.elapsed(threads.nodes_searched()) > totalTime) if (elapsedTime > totalTime)
{ {
// If we are allowed to ponder do not stop the search now but // If we are allowed to ponder do not stop the search now but
// keep pondering until the GUI sends "ponderhit" or "stop". // keep pondering until the GUI sends "ponderhit" or "stop".
@ -464,9 +464,7 @@ void Search::Worker::iterative_deepening() {
threads.stop = true; threads.stop = true;
} }
else else
threads.increaseDepth = threads.increaseDepth = mainThread->ponder || elapsedTime <= totalTime * 0.506;
mainThread->ponder
|| mainThread->tm.elapsed(threads.nodes_searched()) <= totalTime * 0.506;
} }
mainThread->iterValue[iterIdx] = bestValue; mainThread->iterValue[iterIdx] = bestValue;
@ -928,8 +926,7 @@ moves_loop: // When in check, search starts here
ss->moveCount = ++moveCount; ss->moveCount = ++moveCount;
if (rootNode && is_mainthread() if (rootNode && is_mainthread() && elapsed() > 3000)
&& main_manager()->tm.elapsed(threads.nodes_searched()) > 3000)
{ {
main_manager()->updates.onIter( main_manager()->updates.onIter(
{depth, UCIEngine::move(move, pos.is_chess960()), moveCount + thisThread->pvIdx}); {depth, UCIEngine::move(move, pos.is_chess960()), moveCount + thisThread->pvIdx});
@ -1631,6 +1628,11 @@ Depth Search::Worker::reduction(bool i, Depth d, int mn, int delta) {
return (reductionScale + 1123 - delta * 832 / rootDelta) / 1024 + (!i && reductionScale > 1025); return (reductionScale + 1123 - delta * 832 / rootDelta) / 1024 + (!i && reductionScale > 1025);
} }
TimePoint Search::Worker::elapsed() const {
return main_manager()->tm.elapsed([this]() { return threads.nodes_searched(); });
}
namespace { namespace {
// Adjusts a mate or TB score from "plies to mate from the root" // Adjusts a mate or TB score from "plies to mate from the root"
// to "plies to mate from the current position". Standard scores are unchanged. // to "plies to mate from the current position". Standard scores are unchanged.
@ -1845,7 +1847,7 @@ void SearchManager::check_time(Search::Worker& worker) {
static TimePoint lastInfoTime = now(); static TimePoint lastInfoTime = now();
TimePoint elapsed = tm.elapsed(worker.threads.nodes_searched()); TimePoint elapsed = tm.elapsed([&worker]() { return worker.threads.nodes_searched(); });
TimePoint tick = worker.limits.startTime + elapsed; TimePoint tick = worker.limits.startTime + elapsed;
if (tick - lastInfoTime >= 1000) if (tick - lastInfoTime >= 1000)
@ -1877,7 +1879,7 @@ void SearchManager::pv(const Search::Worker& worker,
const auto& rootMoves = worker.rootMoves; const auto& rootMoves = worker.rootMoves;
const auto& pos = worker.rootPos; const auto& pos = worker.rootPos;
size_t pvIdx = worker.pvIdx; size_t pvIdx = worker.pvIdx;
TimePoint time = tm.elapsed(nodes) + 1; TimePoint time = tm.elapsed([nodes]() { return nodes; }) + 1;
size_t multiPV = std::min(size_t(worker.options["MultiPV"]), rootMoves.size()); size_t multiPV = std::min(size_t(worker.options["MultiPV"]), rootMoves.size());
uint64_t tbHits = threads.tb_hits() + (worker.tbConfig.rootInTB ? rootMoves.size() : 0); uint64_t tbHits = threads.tb_hits() + (worker.tbConfig.rootInTB ? rootMoves.size() : 0);

View file

@ -275,6 +275,8 @@ class Worker {
return static_cast<SearchManager*>(manager.get()); return static_cast<SearchManager*>(manager.get());
} }
TimePoint elapsed() const;
LimitsType limits; LimitsType limits;
size_t pvIdx, pvLast; size_t pvIdx, pvLast;

View file

@ -30,9 +30,6 @@ namespace Stockfish {
TimePoint TimeManagement::optimum() const { return optimumTime; } TimePoint TimeManagement::optimum() const { return optimumTime; }
TimePoint TimeManagement::maximum() const { return maximumTime; } TimePoint TimeManagement::maximum() const { return maximumTime; }
TimePoint TimeManagement::elapsed(size_t nodes) const {
return useNodesTime ? TimePoint(nodes) : now() - startTime;
}
void TimeManagement::clear() { void TimeManagement::clear() {
availableNodes = 0; // When in 'nodes as time' mode availableNodes = 0; // When in 'nodes as time' mode

View file

@ -19,7 +19,6 @@
#ifndef TIMEMAN_H_INCLUDED #ifndef TIMEMAN_H_INCLUDED
#define TIMEMAN_H_INCLUDED #define TIMEMAN_H_INCLUDED
#include <cstddef>
#include <cstdint> #include <cstdint>
#include "misc.h" #include "misc.h"
@ -41,7 +40,10 @@ class TimeManagement {
TimePoint optimum() const; TimePoint optimum() const;
TimePoint maximum() const; TimePoint maximum() const;
TimePoint elapsed(std::size_t nodes) const; template<typename FUNC>
TimePoint elapsed(FUNC nodes) const {
return useNodesTime ? TimePoint(nodes()) : now() - startTime;
}
void clear(); void clear();
void advance_nodes_time(std::int64_t nodes); void advance_nodes_time(std::int64_t nodes);