Skip to content

Commit d5f883a

Browse files
vondelemcostalba
authored andcommitted
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
1 parent 92c3952 commit d5f883a

File tree

1 file changed

+11
-11
lines changed

1 file changed

+11
-11
lines changed

src/search.cpp

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,9 @@ void MainThread::search() {
308308
Depth depthDiff = th->completedDepth - bestThread->completedDepth;
309309
Value scoreDiff = th->rootMoves[0].score - bestThread->rootMoves[0].score;
310310

311-
if (scoreDiff > 0 && depthDiff >= 0)
311+
// Select the thread with the best score, always if it is a mate
312+
if ( scoreDiff > 0
313+
&& (depthDiff >= 0 || th->rootMoves[0].score >= VALUE_MATE_IN_MAX_PLY))
312314
bestThread = th;
313315
}
314316
}
@@ -455,29 +457,27 @@ void Thread::search() {
455457
// Sort the PV lines searched so far and update the GUI
456458
std::stable_sort(rootMoves.begin(), rootMoves.begin() + PVIdx + 1);
457459

458-
if (!mainThread)
459-
continue;
460-
461-
if (Threads.stop || PVIdx + 1 == multiPV || Time.elapsed() > 3000)
460+
if ( mainThread
461+
&& (Threads.stop || PVIdx + 1 == multiPV || Time.elapsed() > 3000))
462462
sync_cout << UCI::pv(rootPos, rootDepth, alpha, beta) << sync_endl;
463463
}
464464

465465
if (!Threads.stop)
466466
completedDepth = rootDepth;
467467

468+
// Have we found a "mate in x"?
469+
if ( Limits.mate
470+
&& bestValue >= VALUE_MATE_IN_MAX_PLY
471+
&& VALUE_MATE - bestValue <= 2 * Limits.mate)
472+
Threads.stop = true;
473+
468474
if (!mainThread)
469475
continue;
470476

471477
// If skill level is enabled and time is up, pick a sub-optimal best move
472478
if (skill.enabled() && skill.time_to_pick(rootDepth))
473479
skill.pick_best(multiPV);
474480

475-
// Have we found a "mate in x"?
476-
if ( Limits.mate
477-
&& bestValue >= VALUE_MATE_IN_MAX_PLY
478-
&& VALUE_MATE - bestValue <= 2 * Limits.mate)
479-
Threads.stop = true;
480-
481481
// Do we have time for the next iteration? Can we stop searching now?
482482
if (Limits.use_time_management())
483483
{

0 commit comments

Comments
 (0)