From d5f883ab29d43b35746ff605cf13c3722df56041 Mon Sep 17 00:00:00 2001 From: Joost VandeVondele Date: Fri, 18 Aug 2017 19:38:18 +0200 Subject: [PATCH] Improve multi-threaded mate finding If any thread found a 'mate in x' stop the search. Previously only mainThread would do so. Requires the bestThread selection to be adjusted to always prefer mate scores, even if the search depth is less. I've tried to collect some data for this patch. On 30 cores, mate finding seems 5-30% faster on average. It is not so easy to get numbers for this, as the time to find a mate fluctuates significantly with multi-threaded runs, so it is an average over 100 searches for the same position. Furthermore, hash size and position make a difference as well. Bench: 5965302 --- src/search.cpp | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/src/search.cpp b/src/search.cpp index 09e12ed1..06262131 100644 --- a/src/search.cpp +++ b/src/search.cpp @@ -308,7 +308,9 @@ void MainThread::search() { Depth depthDiff = th->completedDepth - bestThread->completedDepth; Value scoreDiff = th->rootMoves[0].score - bestThread->rootMoves[0].score; - if (scoreDiff > 0 && depthDiff >= 0) + // Select the thread with the best score, always if it is a mate + if ( scoreDiff > 0 + && (depthDiff >= 0 || th->rootMoves[0].score >= VALUE_MATE_IN_MAX_PLY)) bestThread = th; } } @@ -455,16 +457,20 @@ void Thread::search() { // Sort the PV lines searched so far and update the GUI std::stable_sort(rootMoves.begin(), rootMoves.begin() + PVIdx + 1); - if (!mainThread) - continue; - - if (Threads.stop || PVIdx + 1 == multiPV || Time.elapsed() > 3000) + if ( mainThread + && (Threads.stop || PVIdx + 1 == multiPV || Time.elapsed() > 3000)) sync_cout << UCI::pv(rootPos, rootDepth, alpha, beta) << sync_endl; } if (!Threads.stop) completedDepth = rootDepth; + // Have we found a "mate in x"? + if ( Limits.mate + && bestValue >= VALUE_MATE_IN_MAX_PLY + && VALUE_MATE - bestValue <= 2 * Limits.mate) + Threads.stop = true; + if (!mainThread) continue; @@ -472,12 +478,6 @@ void Thread::search() { if (skill.enabled() && skill.time_to_pick(rootDepth)) skill.pick_best(multiPV); - // Have we found a "mate in x"? - if ( Limits.mate - && bestValue >= VALUE_MATE_IN_MAX_PLY - && VALUE_MATE - bestValue <= 2 * Limits.mate) - Threads.stop = true; - // Do we have time for the next iteration? Can we stop searching now? if (Limits.use_time_management()) { -- 2.39.2