Skip to content

Commit

Permalink
Fix go mate x in multithreading
Browse files Browse the repository at this point in the history
Fixes two issues with master for go mate x:

- when running go mate x in losing positions, master always goes to the
  maximal depth, arguably against what the UCI protocol demands

- when running go mate x in winning positions with multiple
  threads, master may return non-mate scores from the search (this issue
  is present in stockfish since at least sf16) The issues are fixed by
  (a) also checking if score is mate -x and by (b) only letting
  mainthread stop the search for go mate x commands, and by not looking
  for a best thread but using mainthread as per the default. Related:
    niklasf/python-chess#1070

More diagnostics can be found here peregrineshahin#6 (comment)

closes #5094

No functional change

Co-Authored-By: Robert Nürnberg <[email protected]>
  • Loading branch information
2 people authored and Disservin committed Mar 7, 2024
1 parent 6136d09 commit 748791f
Showing 1 changed file with 10 additions and 6 deletions.
16 changes: 10 additions & 6 deletions src/search.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ void Search::Worker::start_searching() {
Skill skill =
Skill(options["Skill Level"], options["UCI_LimitStrength"] ? int(options["UCI_Elo"]) : 0);

if (int(options["MultiPV"]) == 1 && !limits.depth && !skill.enabled()
if (int(options["MultiPV"]) == 1 && !limits.depth && !limits.mate && !skill.enabled()
&& rootMoves[0].pv[0] != Move::none())
bestThread = threads.get_best_thread()->worker.get();

Expand Down Expand Up @@ -399,14 +399,18 @@ void Search::Worker::iterative_deepening() {
lastBestMoveDepth = 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;

// Have we found a "mate in x"?
if (limits.mate && rootMoves[0].score == rootMoves[0].uciScore
&& ((rootMoves[0].score >= VALUE_MATE_IN_MAX_PLY
&& VALUE_MATE - rootMoves[0].score <= 2 * limits.mate)
|| (rootMoves[0].score != -VALUE_INFINITE
&& rootMoves[0].score <= VALUE_TB_LOSS_IN_MAX_PLY
&& VALUE_MATE + rootMoves[0].score <= 2 * limits.mate)))
threads.stop = true;

// If the skill level is enabled and time is up, pick a sub-optimal best move
if (skill.enabled() && skill.time_to_pick(rootDepth))
skill.pick_best(rootMoves, multiPV);
Expand Down

0 comments on commit 748791f

Please sign in to comment.