From 47b524293087dac5516b30bc63781bb97f8dedf4 Mon Sep 17 00:00:00 2001 From: Marco Costalba Date: Sat, 10 Oct 2015 08:34:29 +0200 Subject: [PATCH] Set the search start time much earlier The start time is the time point we start our clock and in an ideal case it should be set to the same clock running in the tournament manager when it sends to the engine the 'go' UCI command. Currently it is set at the beginning of MainThread::think() but this means we have not accounted for the time to wake up the thread and the time it takes start_thinking to join() the main thread, possibly waiting for the slave threads to finish. In standard conditions we are talking of very few msecs, but with high number of cores and high time pressure, it is difficult to get a reliable estimate. So move it much earlier, just before processing the 'go' command. bench: 8397672 --- src/benchmark.cpp | 1 + src/search.cpp | 2 +- src/search.h | 1 + src/timeman.cpp | 4 ++-- src/timeman.h | 6 +++--- src/uci.cpp | 2 ++ 6 files changed, 10 insertions(+), 6 deletions(-) diff --git a/src/benchmark.cpp b/src/benchmark.cpp index c683a381..8f3e6ae1 100644 --- a/src/benchmark.cpp +++ b/src/benchmark.cpp @@ -156,6 +156,7 @@ void benchmark(const Position& current, istream& is) { else { Search::StateStackPtr st; + limits.startTime = now(); Threads.start_thinking(pos, limits, st); Threads.main()->join(); nodes += Threads.nodes_searched(); diff --git a/src/search.cpp b/src/search.cpp index 344a0093..207616ef 100644 --- a/src/search.cpp +++ b/src/search.cpp @@ -226,7 +226,7 @@ template uint64_t Search::perft(Position& pos, Depth depth); void MainThread::think() { Color us = pos.side_to_move(); - Time.init(Limits, us, pos.game_ply(), now()); + Time.init(Limits, us, pos.game_ply()); int contempt = Options["Contempt"] * PawnValueEg / 100; // From centipawns DrawValue[ us] = VALUE_DRAW - Value(contempt); diff --git a/src/search.h b/src/search.h index 81f7179c..c7abb9dc 100644 --- a/src/search.h +++ b/src/search.h @@ -88,6 +88,7 @@ struct LimitsType { std::vector searchmoves; int time[COLOR_NB], inc[COLOR_NB], npmsec, movestogo, depth, movetime, mate, infinite, ponder; int64_t nodes; + TimePoint startTime; }; /// The SignalsType struct stores volatile flags updated during the search diff --git a/src/timeman.cpp b/src/timeman.cpp index 7a5db255..3a4e157f 100644 --- a/src/timeman.cpp +++ b/src/timeman.cpp @@ -80,7 +80,7 @@ namespace { /// inc > 0 && movestogo == 0 means: x basetime + z increment /// inc > 0 && movestogo != 0 means: x moves in y minutes + z increment -void TimeManagement::init(Search::LimitsType& limits, Color us, int ply, TimePoint now) +void TimeManagement::init(Search::LimitsType& limits, Color us, int ply) { int minThinkingTime = Options["Minimum Thinking Time"]; int moveOverhead = Options["Move Overhead"]; @@ -102,7 +102,7 @@ void TimeManagement::init(Search::LimitsType& limits, Color us, int ply, TimePoi limits.npmsec = npmsec; } - start = now; + startTime = limits.startTime; unstablePvFactor = 1; optimumTime = maximumTime = std::max(limits.time[us], minThinkingTime); diff --git a/src/timeman.h b/src/timeman.h index 24632d79..b6eb3485 100644 --- a/src/timeman.h +++ b/src/timeman.h @@ -29,16 +29,16 @@ class TimeManagement { public: - void init(Search::LimitsType& limits, Color us, int ply, TimePoint now); + void init(Search::LimitsType& limits, Color us, int ply); void pv_instability(double bestMoveChanges) { unstablePvFactor = 1 + bestMoveChanges; } int available() const { return int(optimumTime * unstablePvFactor * 0.76); } int maximum() const { return maximumTime; } - int elapsed() const { return int(Search::Limits.npmsec ? Threads.nodes_searched() : now() - start); } + int elapsed() const { return int(Search::Limits.npmsec ? Threads.nodes_searched() : now() - startTime); } int64_t availableNodes; // When in 'nodes as time' mode private: - TimePoint start; + TimePoint startTime; int optimumTime; int maximumTime; double unstablePvFactor; diff --git a/src/uci.cpp b/src/uci.cpp index 4e56542a..c5dbafae 100644 --- a/src/uci.cpp +++ b/src/uci.cpp @@ -112,6 +112,8 @@ namespace { Search::LimitsType limits; string token; + limits.startTime = now(); // As early as possible! + while (is >> token) if (token == "searchmoves") while (is >> token)