Skip to content
This repository has been archived by the owner on Apr 25, 2024. It is now read-only.

Commit

Permalink
Refactor elapsed time checks in search
Browse files Browse the repository at this point in the history
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 official-stockfish#5163

No functional change
  • Loading branch information
Disservin authored and vondele committed Apr 21, 2024
1 parent 432995a commit d3fc1d8
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 18 deletions.
28 changes: 15 additions & 13 deletions src/search.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,7 @@ void Search::Worker::iterative_deepening() {
// When failing high/low give some update (without cluttering
// the UI) before a re-search.
if (mainThread && multiPV == 1 && (bestValue <= alpha || bestValue >= beta)
&& mainThread->tm.elapsed(threads.nodes_searched()) > 3000)
&& elapsed() > 3000)
main_manager()->pv(*this, threads, tt, rootDepth);

// In case of failing low/high increase aspiration window and
Expand Down Expand Up @@ -371,8 +371,7 @@ void Search::Worker::iterative_deepening() {
std::stable_sort(rootMoves.begin() + pvFirst, rootMoves.begin() + pvIdx + 1);

if (mainThread
&& (threads.stop || pvIdx + 1 == multiPV
|| mainThread->tm.elapsed(threads.nodes_searched()) > 3000)
&& (threads.stop || pvIdx + 1 == multiPV || elapsed() > 3000)
// 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
// had time to fully search other root-moves. Thus we suppress this output and
Expand Down Expand Up @@ -448,13 +447,14 @@ void Search::Worker::iterative_deepening() {
if (rootMoves.size() == 1)
totalTime = std::min(500.0, totalTime);

if (completedDepth >= 10 && nodesEffort >= 97
&& mainThread->tm.elapsed(threads.nodes_searched()) > totalTime * 0.739
auto elapsedTime = elapsed();

if (completedDepth >= 10 && nodesEffort >= 97 && elapsedTime > totalTime * 0.739
&& !mainThread->ponder)
threads.stop = true;

// 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
// keep pondering until the GUI sends "ponderhit" or "stop".
Expand All @@ -464,9 +464,7 @@ void Search::Worker::iterative_deepening() {
threads.stop = true;
}
else
threads.increaseDepth =
mainThread->ponder
|| mainThread->tm.elapsed(threads.nodes_searched()) <= totalTime * 0.506;
threads.increaseDepth = mainThread->ponder || elapsedTime <= totalTime * 0.506;
}

mainThread->iterValue[iterIdx] = bestValue;
Expand Down Expand Up @@ -928,8 +926,7 @@ Value Search::Worker::search(

ss->moveCount = ++moveCount;

if (rootNode && is_mainthread()
&& main_manager()->tm.elapsed(threads.nodes_searched()) > 3000)
if (rootNode && is_mainthread() && elapsed() > 3000)
{
main_manager()->updates.onIter(
{depth, UCIEngine::move(move, pos.is_chess960()), moveCount + thisThread->pvIdx});
Expand Down Expand Up @@ -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);
}

TimePoint Search::Worker::elapsed() const {
return main_manager()->tm.elapsed([this]() { return threads.nodes_searched(); });
}


namespace {
// 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.
Expand Down Expand Up @@ -1845,7 +1847,7 @@ void SearchManager::check_time(Search::Worker& worker) {

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;

if (tick - lastInfoTime >= 1000)
Expand Down Expand Up @@ -1877,7 +1879,7 @@ void SearchManager::pv(const Search::Worker& worker,
const auto& rootMoves = worker.rootMoves;
const auto& pos = worker.rootPos;
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());
uint64_t tbHits = threads.tb_hits() + (worker.tbConfig.rootInTB ? rootMoves.size() : 0);

Expand Down
2 changes: 2 additions & 0 deletions src/search.h
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,8 @@ class Worker {
return static_cast<SearchManager*>(manager.get());
}

TimePoint elapsed() const;

LimitsType limits;

size_t pvIdx, pvLast;
Expand Down
3 changes: 0 additions & 3 deletions src/timeman.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,6 @@ namespace Stockfish {

TimePoint TimeManagement::optimum() const { return optimumTime; }
TimePoint TimeManagement::maximum() const { return maximumTime; }
TimePoint TimeManagement::elapsed(size_t nodes) const {
return useNodesTime ? TimePoint(nodes) : now() - startTime;
}

void TimeManagement::clear() {
availableNodes = 0; // When in 'nodes as time' mode
Expand Down
6 changes: 4 additions & 2 deletions src/timeman.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
#ifndef TIMEMAN_H_INCLUDED
#define TIMEMAN_H_INCLUDED

#include <cstddef>
#include <cstdint>

#include "misc.h"
Expand All @@ -41,7 +40,10 @@ class TimeManagement {

TimePoint optimum() 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 advance_nodes_time(std::int64_t nodes);
Expand Down

0 comments on commit d3fc1d8

Please sign in to comment.