diff --git a/src/misc.h b/src/misc.h index 994f551d..f73e7889 100644 --- a/src/misc.h +++ b/src/misc.h @@ -19,12 +19,14 @@ #ifndef MISC_H_INCLUDED #define MISC_H_INCLUDED +#include #include #include #include #include #include #include +#include #define stringify2(x) #x #define stringify(x) stringify2(x) @@ -188,6 +190,19 @@ struct CommandLine { std::string workingDirectory; // path of the working directory }; +namespace Utility { + +template +void move_to_front(std::vector& vec, Predicate pred) { + auto it = std::find_if(vec.begin(), vec.end(), pred); + + if (it != vec.end()) + { + std::rotate(vec.begin(), it, it + 1); + } +} +} + } // namespace Stockfish #endif // #ifndef MISC_H_INCLUDED diff --git a/src/search.cpp b/src/search.cpp index 8901dfd1..8363f221 100644 --- a/src/search.cpp +++ b/src/search.cpp @@ -248,15 +248,16 @@ void Search::Worker::iterative_deepening() { // Allocate stack with extra size to allow access from (ss - 7) to (ss + 2): // (ss - 7) is needed for update_continuation_histories(ss - 1) which accesses (ss - 6), // (ss + 2) is needed for initialization of cutOffCnt and killers. - Stack stack[MAX_PLY + 10], *ss = stack + 7; - Move pv[MAX_PLY + 1]; - Value alpha, beta; - Move lastBestMove = Move::none(); - Depth lastBestMoveDepth = 0; - SearchManager* mainThread = (thread_idx == 0 ? main_manager() : nullptr); - double timeReduction = 1, totBestMoveChanges = 0; - Color us = rootPos.side_to_move(); - int delta, iterIdx = 0; + Stack stack[MAX_PLY + 10], *ss = stack + 7; + Move pv[MAX_PLY + 1]; + Value alpha, beta; + Value lastBestScore = -VALUE_INFINITE; + std::vector lastBestPV = {Move::none()}; + Depth lastBestMoveDepth = 0; + SearchManager* mainThread = (thread_idx == 0 ? main_manager() : nullptr); + double timeReduction = 1, totBestMoveChanges = 0; + Color us = rootPos.side_to_move(); + int delta, iterIdx = 0; std::memset(ss - 7, 0, 10 * sizeof(Stack)); for (int i = 7; i > 0; --i) @@ -402,7 +403,12 @@ void Search::Worker::iterative_deepening() { if (mainThread && (threads.stop || pvIdx + 1 == multiPV - || mainThread->tm.elapsed(threads.nodes_searched()) > 3000)) + || mainThread->tm.elapsed(threads.nodes_searched()) > 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 + // below pick a proven score/PV for this thread (from the previous iteration). + && !(threads.abortedSearch && rootMoves[0].uciScore <= VALUE_TB_LOSS_IN_MAX_PLY)) sync_cout << UCI::pv(*this, mainThread->tm.elapsed(threads.nodes_searched()), threads.nodes_searched(), threads.tb_hits(), tt.hashfull(), tbConfig.rootInTB) @@ -412,9 +418,21 @@ void Search::Worker::iterative_deepening() { if (!threads.stop) completedDepth = rootDepth; - if (rootMoves[0].pv[0] != lastBestMove) + // We make sure not to pick an unproven mated-in score, + // in case this thread prematurely stopped search (aborted-search). + if (threads.abortedSearch && rootMoves[0].score != -VALUE_INFINITE + && rootMoves[0].score <= VALUE_TB_LOSS_IN_MAX_PLY) { - lastBestMove = rootMoves[0].pv[0]; + // Bring the last best move to the front for best thread selection. + Utility::move_to_front(rootMoves, [&lastBestPV = std::as_const(lastBestPV)]( + const auto& rm) { return rm == lastBestPV[0]; }); + rootMoves[0].pv = lastBestPV; + rootMoves[0].score = rootMoves[0].uciScore = lastBestScore; + } + else if (rootMoves[0].pv[0] != lastBestPV[0]) + { + lastBestPV = rootMoves[0].pv; + lastBestScore = rootMoves[0].score; lastBestMoveDepth = rootDepth; } @@ -1916,11 +1934,14 @@ void SearchManager::check_time(Search::Worker& worker) { if (ponder) return; - if ((worker.limits.use_time_management() && (elapsed > tm.maximum() || stopOnPonderhit)) - || (worker.limits.movetime && elapsed >= worker.limits.movetime) - || (worker.limits.nodes - && worker.threads.nodes_searched() >= uint64_t(worker.limits.nodes))) - worker.threads.stop = true; + if ( + // Later we rely on the fact that we can at least use the mainthread previous + // root-search score and PV in a multithreaded environment to prove mated-in scores. + worker.completedDepth >= 1 + && ((worker.limits.use_time_management() && (elapsed > tm.maximum() || stopOnPonderhit)) + || (worker.limits.movetime && elapsed >= worker.limits.movetime) + || (worker.limits.nodes && worker.threads.nodes_searched() >= worker.limits.nodes))) + worker.threads.stop = worker.threads.abortedSearch = true; } // Called in case we have no ponder move before exiting the search, diff --git a/src/search.h b/src/search.h index daf0ff85..4bd013ad 100644 --- a/src/search.h +++ b/src/search.h @@ -115,7 +115,7 @@ struct LimitsType { std::vector searchmoves; TimePoint time[COLOR_NB], inc[COLOR_NB], npmsec, movetime, startTime; int movestogo, depth, mate, perft, infinite; - int64_t nodes; + uint64_t nodes; }; diff --git a/src/thread.cpp b/src/thread.cpp index a4bc3d67..4c1d01f4 100644 --- a/src/thread.cpp +++ b/src/thread.cpp @@ -20,8 +20,6 @@ #include #include -#include -#include #include #include #include @@ -169,8 +167,8 @@ void ThreadPool::start_thinking(const OptionsMap& options, main_thread()->wait_for_search_finished(); - main_manager()->stopOnPonderhit = stop = false; - main_manager()->ponder = ponderMode; + main_manager()->stopOnPonderhit = stop = abortedSearch = false; + main_manager()->ponder = ponderMode; increaseDepth = true; @@ -229,13 +227,23 @@ Thread* ThreadPool::get_best_thread() const { votes[th->worker->rootMoves[0].pv[0]] += thread_value(th); for (Thread* th : threads) - if (std::abs(bestThread->worker->rootMoves[0].score) >= VALUE_TB_WIN_IN_MAX_PLY) + if (bestThread->worker->rootMoves[0].score >= VALUE_TB_WIN_IN_MAX_PLY) { - // Make sure we pick the shortest mate / TB conversion or stave off mate the longest + // Make sure we pick the shortest mate / TB conversion if (th->worker->rootMoves[0].score > bestThread->worker->rootMoves[0].score) bestThread = th; } + else if (bestThread->worker->rootMoves[0].score != -VALUE_INFINITE + && bestThread->worker->rootMoves[0].score <= VALUE_TB_LOSS_IN_MAX_PLY) + { + // Make sure we pick the shortest mated / TB conversion + if (th->worker->rootMoves[0].score != -VALUE_INFINITE + && th->worker->rootMoves[0].score < bestThread->worker->rootMoves[0].score) + bestThread = th; + } else if (th->worker->rootMoves[0].score >= VALUE_TB_WIN_IN_MAX_PLY + || (th->worker->rootMoves[0].score != -VALUE_INFINITE + && th->worker->rootMoves[0].score <= VALUE_TB_LOSS_IN_MAX_PLY) || (th->worker->rootMoves[0].score > VALUE_TB_LOSS_IN_MAX_PLY && (votes[th->worker->rootMoves[0].pv[0]] > votes[bestThread->worker->rootMoves[0].pv[0]] diff --git a/src/thread.h b/src/thread.h index 6575b14e..a2a1d18c 100644 --- a/src/thread.h +++ b/src/thread.h @@ -94,7 +94,7 @@ class ThreadPool { void start_searching(); void wait_for_search_finished() const; - std::atomic_bool stop, increaseDepth; + std::atomic_bool stop, abortedSearch, increaseDepth; auto cbegin() const noexcept { return threads.cbegin(); } auto begin() noexcept { return threads.begin(); }