Skip to content

Commit 31586a0

Browse files
committed
Simplify pondering time management
stopOnPonderhit is used to stop search quickly on a ponderhit. It is set by mainThread as part of its time management. However, master employs it as a signal between mainThread and the UCI thread. This is not necessary, it is sufficient for the UCI thread to signal that pondering finished, and mainThread should do its usual time-keeping job, and in this case stop immediately. This patch implements this, removing stopOnPonderHit as an atomic variable from the ThreadPool, and moving it as a normal variable to mainThread, reducing its scope. In MainThread::check_time() the search is stopped immediately if ponder switches to false, and the variable stopOnPonderHit is set. Furthermore, ponder has been moved to mainThread, as the variable is only used to exchange signals between the UCI thread and mainThread. The version has been tested locally (as fishtest doesn't support ponder): Score of ponderSimp vs master: 2616 - 2528 - 8630 [0.503] 13774 Elo difference: 2.22 +/- 3.54 which indicates no regression. No functional change.
1 parent 59b2486 commit 31586a0

File tree

4 files changed

+19
-20
lines changed

4 files changed

+19
-20
lines changed

src/search.cpp

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -227,10 +227,9 @@ void MainThread::search() {
227227
// Threads.stop. However, if we are pondering or in an infinite search,
228228
// the UCI protocol states that we shouldn't print the best move before the
229229
// GUI sends a "stop" or "ponderhit" command. We therefore simply wait here
230-
// until the GUI sends one of those commands (which also raises Threads.stop).
231-
Threads.stopOnPonderhit = true;
230+
// until the GUI sends one of those commands.
232231

233-
while (!Threads.stop && (Threads.ponder || Limits.infinite))
232+
while (!Threads.stop && (ponder || Limits.infinite))
234233
{} // Busy wait for a stop or a ponder reset
235234

236235
// Stop the threads if not already stopped (also raise the stop if
@@ -448,7 +447,7 @@ void Thread::search() {
448447
{
449448
failedHighCnt = 0;
450449
failedLow = true;
451-
Threads.stopOnPonderhit = false;
450+
mainThread->stopOnPonderhit = false;
452451
}
453452
}
454453
else if (bestValue >= beta)
@@ -497,7 +496,7 @@ void Thread::search() {
497496
// Do we have time for the next iteration? Can we stop searching now?
498497
if ( Limits.use_time_management()
499498
&& !Threads.stop
500-
&& !Threads.stopOnPonderhit)
499+
&& !mainThread->stopOnPonderhit)
501500
{
502501
double fallingEval = (306 + 119 * failedLow + 6 * (mainThread->previousScore - bestValue)) / 581.0;
503502
fallingEval = std::max(0.5, std::min(1.5, fallingEval));
@@ -515,8 +514,8 @@ void Thread::search() {
515514
{
516515
// If we are allowed to ponder do not stop the search now but
517516
// keep pondering until the GUI sends "ponderhit" or "stop".
518-
if (Threads.ponder)
519-
Threads.stopOnPonderhit = true;
517+
if (mainThread->ponder)
518+
mainThread->stopOnPonderhit = true;
520519
else
521520
Threads.stop = true;
522521
}
@@ -1595,10 +1594,10 @@ void MainThread::check_time() {
15951594
}
15961595

15971596
// We should not stop pondering until told so by the GUI
1598-
if (Threads.ponder)
1597+
if (ponder)
15991598
return;
16001599

1601-
if ( (Limits.use_time_management() && elapsed > Time.maximum() - 10)
1600+
if ( (Limits.use_time_management() && (elapsed > Time.maximum() - 10 || stopOnPonderhit))
16021601
|| (Limits.movetime && elapsed >= Limits.movetime)
16031602
|| (Limits.nodes && Threads.nodes_searched() >= (uint64_t)Limits.nodes))
16041603
Threads.stop = true;

src/thread.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -162,8 +162,8 @@ void ThreadPool::start_thinking(Position& pos, StateListPtr& states,
162162

163163
main()->wait_for_search_finished();
164164

165-
stopOnPonderhit = stop = false;
166-
ponder = ponderMode;
165+
main()->stopOnPonderhit = stop = false;
166+
main()->ponder = ponderMode;
167167
Search::Limits = limits;
168168
Search::RootMoves rootMoves;
169169

src/thread.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,8 @@ struct MainThread : public Thread {
8888
double bestMoveChanges, previousTimeReduction;
8989
Value previousScore;
9090
int callsCnt;
91+
bool stopOnPonderhit;
92+
std::atomic_bool ponder;
9193
};
9294

9395

@@ -105,7 +107,7 @@ struct ThreadPool : public std::vector<Thread*> {
105107
uint64_t nodes_searched() const { return accumulate(&Thread::nodes); }
106108
uint64_t tb_hits() const { return accumulate(&Thread::tbHits); }
107109

108-
std::atomic_bool stop, ponder, stopOnPonderhit;
110+
std::atomic_bool stop;
109111

110112
private:
111113
StateListPtr setupStates;

src/uci.cpp

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -207,18 +207,16 @@ void UCI::loop(int argc, char* argv[]) {
207207
token.clear(); // Avoid a stale if getline() returns empty or blank line
208208
is >> skipws >> token;
209209

210-
// The GUI sends 'ponderhit' to tell us the user has played the expected move.
211-
// So 'ponderhit' will be sent if we were told to ponder on the same move the
212-
// user has played. We should continue searching but switch from pondering to
213-
// normal search. In case Threads.stopOnPonderhit is set we are waiting for
214-
// 'ponderhit' to stop the search, for instance if max search depth is reached.
215210
if ( token == "quit"
216-
|| token == "stop"
217-
|| (token == "ponderhit" && Threads.stopOnPonderhit))
211+
|| token == "stop")
218212
Threads.stop = true;
219213

214+
// The GUI sends 'ponderhit' to tell us the user has played the expected move.
215+
// So 'ponderhit' will be sent if we were told to ponder on the same move the
216+
// user has played. We should continue searching but switch from pondering to
217+
// normal search.
220218
else if (token == "ponderhit")
221-
Threads.ponder = false; // Switch to normal search
219+
Threads.main()->ponder = false; // Switch to normal search
222220

223221
else if (token == "uci")
224222
sync_cout << "id name " << engine_info(true)

0 commit comments

Comments
 (0)