Improve multi-threaded go-mate searches

Ever since #5094 master only lets the main thread terminate `go mate`
searches, reverting the earlier improvement #1215.

This PR restores the old logic. So any thread that found a "mate in x"
can now stop the search.

To make this work robustly, we need to guard against inexact mate scores
in the best thread selection. In addition, in contrast to time limits,
the main thread may now not complete a d1 search for a mated-in
position.
In master an aborted d1 search may have a PV beginning with
`Move::none()`, in which case no thread selection is performed. See
#623. We fix this bug here by checking if `lastBestPV` is empty or not.
For interrupted d1 searches we now label mated-in scores as inexact.

While at it, we also simplify the logic for detecting if we can
terminate a go mate x search, using the fact that threads.stop can only
be false if we have a completed iteration with a valid score.

The PR has no effect on game play, but should slightly improve general
mate finding and speed up multi-threaded `go mate` searches.

We also add a corresponding matecheck run to the CI. This only involves
61 mates up to mate-in-2. Test runs with the first 50 or 100 mates from
`mates2000.epd` did at times not finish within 30 minutes on my fork or
in local tests, possibly due to search explosions for some mate-in-3
positions.

closes https://github.com/official-stockfish/Stockfish/pull/6668

No functional change
This commit is contained in:
Robert Nurnberg @ elitebook
2026-03-18 20:49:06 +01:00
committed by Disservin
parent e093339c2f
commit 67a2c247d4
3 changed files with 60 additions and 27 deletions
+11 -5
View File
@@ -364,6 +364,10 @@ Thread* ThreadPool::get_best_thread() const {
for (auto&& th : threads)
votes[th->worker->rootMoves[0].pv[0]] += thread_voting_value(th.get());
auto has_bound = [](const Thread* th) {
return th->worker->rootMoves[0].scoreLowerbound || th->worker->rootMoves[0].scoreUpperbound;
};
for (auto&& th : threads)
{
const auto bestThreadScore = bestThread->worker->rootMoves[0].score;
@@ -375,13 +379,15 @@ Thread* ThreadPool::get_best_thread() const {
const auto bestThreadMoveVote = votes[bestThreadPV[0]];
const auto newThreadMoveVote = votes[newThreadPV[0]];
const bool bestThreadInProvenWin = is_win(bestThreadScore);
const bool newThreadInProvenWin = is_win(newThreadScore);
// Aborted searches may lead to inexact win scores.
const bool bestThreadInProvenWin = is_win(bestThreadScore) && !has_bound(bestThread);
const bool newThreadInProvenWin = is_win(newThreadScore) && !has_bound(th.get());
// Loss scores may be inexact only for aborted d1 searches.
const bool bestThreadInProvenLoss =
bestThreadScore != -VALUE_INFINITE && is_loss(bestThreadScore);
bestThreadScore != -VALUE_INFINITE && is_loss(bestThreadScore) && !has_bound(bestThread);
const bool newThreadInProvenLoss =
newThreadScore != -VALUE_INFINITE && is_loss(newThreadScore);
newThreadScore != -VALUE_INFINITE && is_loss(newThreadScore) && !has_bound(th.get());
// We make sure not to pick a thread with truncated principal variation
const bool betterVotingValue =
@@ -391,7 +397,7 @@ Thread* ThreadPool::get_best_thread() const {
if (bestThreadInProvenWin)
{
// Make sure we pick the shortest mate / TB conversion
if (newThreadScore > bestThreadScore)
if (newThreadInProvenWin && newThreadScore > bestThreadScore)
bestThread = th.get();
}
else if (bestThreadInProvenLoss)