From 926f215061311392bc26c7bc4bde5b719dbab4e5 Mon Sep 17 00:00:00 2001 From: Marco Costalba Date: Sun, 22 Mar 2015 21:15:44 +0100 Subject: [PATCH] Add support for playing in 'nodes as time' mode When running more games in parallel, or simply when running a game with a background process, due to how OS scheduling works, there is no guarantee that the CPU resources allocated evenly between the two players. This introduces noise in the result that leads to unreliable result and in the worst cases can even invalidate the result. For instance in SF test framework we avoid running from clouds virtual machines because are a known source of very unstable CPU speed. To overcome this issue, without requiring changes to the GUI, the idea is to use searched nodes instead of time, and to convert time to available nodes upfront, at the beginning of the game. When nodestime UCI option is set at a given nodes per milliseconds (npmsec), at the beginning of the game (and only once), the engine reads the available time to think, sent by the GUI with 'go wtime x' UCI command. Then it translates time in available nodes (nodes = npmsec * x), then feeds available nodes instead of time to the time management logic and starts the search. During the search the engine checks the searched nodes against the available ones in such a way that all the time management logic still fully applies, and the game mimics a real one played on real time. When the search finishes, before returning best move, the total available nodes are updated, subtracting the real searched nodes. After the first move, the time information sent by the GUI is ignored, and the engine fully relies on the updated total available nodes to feed time management. To avoid time losses, the speed of the engine (npms) must be set to a value lower than real speed so that if the real TC is for instance 30 secs, and npms is half of the real speed, the game will last on average 15 secs, so much less than the TC limit, providing for a safety 'time buffer'. There are 2 main limitations with this mode. 1. Engine speed should be the same for both players, and this limits the approach to mainly parameter tuning patches. 2. Because npms is fixed while, in real engines, the speed increases toward endgame, this introduces an artifact that is equivalent to an altered time management. Namely it is like the time management gives less available time than what should be in standard case. May be the second limitation could be mitigated in a future with a smarter 'dynamic npms' approach. Tests shows that the standard deviation of the results with 'nodestime' is lower than in standard TC, as is expected because now all the introduced noise due the random speed variability of the engines during the game is fully removed. Original NIT idea by Michael Hoffman that shows how to play in NIT mode without requiring changes to the GUI. This implementation goes a bit further, the key difference is that we read TC from GUI only once upfront instead of re-reading after every move as in Michael's implementation. No functional change. --- src/search.cpp | 13 +++++++++---- src/search.h | 4 ++-- src/timeman.cpp | 20 +++++++++++++++++++- src/timeman.h | 6 +++++- src/uci.cpp | 7 ++++++- src/ucioption.cpp | 1 + 6 files changed, 42 insertions(+), 9 deletions(-) diff --git a/src/search.cpp b/src/search.cpp index 4590bd4a..f0c1bdd9 100644 --- a/src/search.cpp +++ b/src/search.cpp @@ -129,7 +129,6 @@ namespace { }; size_t PVIdx; - TimeManagement Time; EasyMoveManager EasyMove; double BestMoveChanges; Value DrawValue[COLOR_NB]; @@ -218,11 +217,12 @@ template uint64_t Search::perft(Position& pos, Depth depth); void Search::think() { - Time.init(Limits, RootPos.side_to_move(), RootPos.game_ply(), now()); + Color us = RootPos.side_to_move(); + Time.init(Limits, us, RootPos.game_ply(), now()); int contempt = Options["Contempt"] * PawnValueEg / 100; // From centipawns - DrawValue[ RootPos.side_to_move()] = VALUE_DRAW - Value(contempt); - DrawValue[~RootPos.side_to_move()] = VALUE_DRAW + Value(contempt); + DrawValue[ us] = VALUE_DRAW - Value(contempt); + DrawValue[~us] = VALUE_DRAW + Value(contempt); TB::Hits = 0; TB::RootInTB = false; @@ -291,6 +291,11 @@ void Search::think() { Threads.timer->run = false; } + // When playing in 'nodes as time' mode, subtract the searched nodes from + // the available ones before to exit. + if (Limits.npmsec) + Time.availableNodes += Limits.inc[us] - RootPos.nodes_searched(); + // When we reach the maximum depth, we can arrive here without a raise of // Signals.stop. However, if we are pondering or in an infinite search, // the UCI protocol states that we shouldn't print the best move before the diff --git a/src/search.h b/src/search.h index ed2c1e18..5e17b829 100644 --- a/src/search.h +++ b/src/search.h @@ -76,7 +76,7 @@ typedef std::vector RootMoveVector; struct LimitsType { LimitsType() { // Init explicitly due to broken value-initialization of non POD in MSVC - nodes = time[WHITE] = time[BLACK] = inc[WHITE] = inc[BLACK] = movestogo = + nodes = time[WHITE] = time[BLACK] = inc[WHITE] = inc[BLACK] = npmsec = movestogo = depth = movetime = mate = infinite = ponder = 0; } @@ -85,7 +85,7 @@ struct LimitsType { } std::vector searchmoves; - int time[COLOR_NB], inc[COLOR_NB], movestogo, depth, movetime, mate, infinite, ponder; + int time[COLOR_NB], inc[COLOR_NB], npmsec, movestogo, depth, movetime, mate, infinite, ponder; int64_t nodes; }; diff --git a/src/timeman.cpp b/src/timeman.cpp index 4763671a..7a5db255 100644 --- a/src/timeman.cpp +++ b/src/timeman.cpp @@ -25,6 +25,8 @@ #include "timeman.h" #include "uci.h" +TimeManagement Time; // Our global time management object + namespace { enum TimeType { OptimumTime, MaxTime }; @@ -78,11 +80,27 @@ 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(const Search::LimitsType& limits, Color us, int ply, TimePoint now) +void TimeManagement::init(Search::LimitsType& limits, Color us, int ply, TimePoint now) { int minThinkingTime = Options["Minimum Thinking Time"]; int moveOverhead = Options["Move Overhead"]; int slowMover = Options["Slow Mover"]; + int npmsec = Options["nodestime"]; + + // If we have to play in 'nodes as time' mode, then convert from time + // to nodes, and use resulting values in time management formulas. + // WARNING: Given npms (nodes per millisecond) must be much lower then + // real engine speed to avoid time losses. + if (npmsec) + { + if (!availableNodes) // Only once at game start + availableNodes = npmsec * limits.time[us]; // Time is in msec + + // Convert from millisecs to nodes + limits.time[us] = (int)availableNodes; + limits.inc[us] *= npmsec; + limits.npmsec = npmsec; + } start = now; unstablePvFactor = 1; diff --git a/src/timeman.h b/src/timeman.h index 3cb59bba..f2c3663b 100644 --- a/src/timeman.h +++ b/src/timeman.h @@ -27,12 +27,14 @@ class TimeManagement { public: - void init(const Search::LimitsType& limits, Color us, int ply, TimePoint now); + void init(Search::LimitsType& limits, Color us, int ply, TimePoint now); 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 now() - start; } + int64_t availableNodes; // When in 'nodes as time' mode + private: TimePoint start; int optimumTime; @@ -40,4 +42,6 @@ private: double unstablePvFactor; }; +extern TimeManagement Time; + #endif // #ifndef TIMEMAN_H_INCLUDED diff --git a/src/uci.cpp b/src/uci.cpp index 2c07807c..a9fe4f20 100644 --- a/src/uci.cpp +++ b/src/uci.cpp @@ -26,6 +26,7 @@ #include "position.h" #include "search.h" #include "thread.h" +#include "timeman.h" #include "tt.h" #include "uci.h" @@ -178,8 +179,12 @@ void UCI::loop(int argc, char* argv[]) { << "\n" << Options << "\nuciok" << sync_endl; + else if (token == "ucinewgame") + { + TT.clear(); + Time.availableNodes = 0; + } else if (token == "isready") sync_cout << "readyok" << sync_endl; - else if (token == "ucinewgame") TT.clear(); else if (token == "go") go(pos, is); else if (token == "position") position(pos, is); else if (token == "setoption") setoption(is); diff --git a/src/ucioption.cpp b/src/ucioption.cpp index 06641cf5..35c907ae 100644 --- a/src/ucioption.cpp +++ b/src/ucioption.cpp @@ -67,6 +67,7 @@ void init(OptionsMap& o) { o["Move Overhead"] << Option(30, 0, 5000); o["Minimum Thinking Time"] << Option(20, 0, 5000); o["Slow Mover"] << Option(80, 10, 1000); + o["nodestime"] << Option(0, 0, 10000); o["UCI_Chess960"] << Option(false); o["SyzygyPath"] << Option("", on_tb_path); o["SyzygyProbeDepth"] << Option(1, 1, 100); -- 2.39.2