Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use node counting to early stop search #5053

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions src/search.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include <atomic>
#include <cassert>
#include <cmath>
#include <cstdint>
#include <cstdlib>
#include <initializer_list>
#include <iostream>
Expand Down Expand Up @@ -418,6 +419,10 @@ void Search::Worker::iterative_deepening() {
// Do we have time for the next iteration? Can we stop searching now?
if (limits.use_time_management() && !threads.stop && !mainThread->stopOnPonderhit)
{
auto bestmove = rootMoves[0].pv[0];
int nodesEffort = effort[bestmove.from_sq()][bestmove.to_sq()] * 100
/ std::max(size_t(1), size_t(nodes));

double fallingEval = (66 + 14 * (mainThread->bestPreviousAverageScore - bestValue)
+ 6 * (mainThread->iterValue[iterIdx] - bestValue))
/ 616.6;
Expand All @@ -435,6 +440,13 @@ void Search::Worker::iterative_deepening() {
if (rootMoves.size() == 1)
totalTime = std::min(500.0, totalTime);

if (completedDepth >= 10 && nodesEffort >= 95
&& mainThread->tm.elapsed(threads.nodes_searched()) > totalTime * 3 / 4
&& !mainThread->ponder)
{
threads.stop = true;
}

// Stop the search if we have exceeded the totalTime
if (mainThread->tm.elapsed(threads.nodes_searched()) > totalTime)
{
Expand Down Expand Up @@ -1087,6 +1099,8 @@ Value Search::Worker::search(
ss->continuationHistory =
&thisThread->continuationHistory[ss->inCheck][capture][movedPiece][move.to_sq()];

uint64_t nodeCount = rootNode ? uint64_t(nodes) : 0;

// Step 16. Make the move
thisThread->nodes.fetch_add(1, std::memory_order_relaxed);
pos.do_move(move, st, givesCheck);
Expand Down Expand Up @@ -1186,6 +1200,9 @@ Value Search::Worker::search(
// Step 19. Undo move
pos.undo_move(move);

if (rootNode)
effort[move.from_sq()][move.to_sq()] += nodes - nodeCount;

assert(value > -VALUE_INFINITE && value < VALUE_INFINITE);

// Step 20. Check for a new best move
Expand Down
2 changes: 2 additions & 0 deletions src/search.h
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,8 @@ class Worker {
return static_cast<SearchManager*>(manager.get());
}

std::array<std::array<uint64_t, SQUARE_NB>, SQUARE_NB> effort;

LimitsType limits;

size_t pvIdx, pvLast;
Expand Down
2 changes: 2 additions & 0 deletions src/thread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include <memory>
#include <unordered_map>
#include <utility>
#include <array>

#include "misc.h"
#include "movegen.h"
Expand Down Expand Up @@ -203,6 +204,7 @@ void ThreadPool::start_thinking(const OptionsMap& options,
th->worker->rootPos.set(pos.fen(), pos.is_chess960(), &th->worker->rootState);
th->worker->rootState = setupStates->back();
th->worker->tbConfig = tbConfig;
th->worker->effort = {};
}

main_thread()->start_searching();
Expand Down
Loading