Skip to content

Commit

Permalink
Use average bestMoveChanges across all threads #2072
Browse files Browse the repository at this point in the history
The current update only by main thread depends on the luck of
whether main thread sees any/many changes to the best move or not.
It then makes large, lumpy changes to the time to be
used (1x, 2x, 3x, etc) depending on that sample of 1.
Use the average across all threads to get a more reliable
number with a smoother distribution.

STC @ 5+0.05 th 4 :
LLR: 2.95 (-2.94,2.94) [0.50,4.50]
Total: 51899 W: 11446 L: 11029 D: 29424
http://tests.stockfishchess.org/tests/view/5ca32ff20ebc5925cf0016fb

STC @ 5+0.05 th 8 :
LLR: 2.96 (-2.94,2.94) [0.50,4.50]
Total: 13851 W: 2843 L: 2620 D: 8388
http://tests.stockfishchess.org/tests/view/5ca35ae00ebc5925cf001adb

LTC @ 20+0.2 th 8 :
LLR: 2.95 (-2.94,2.94) [0.00,3.50]
Total: 48527 W: 7941 L: 7635 D: 32951
http://tests.stockfishchess.org/tests/view/5ca37cb70ebc5925cf001cec

Further work:
Similar changes might be possible for the fallingEval and timeReduction calculations (and elsewhere?), using either the min, average or max values across all threads.

Bench 3506898
  • Loading branch information
xoto10 authored and mcostalba committed Apr 5, 2019
1 parent 0f63b35 commit 1982fe2
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 11 deletions.
21 changes: 12 additions & 9 deletions src/search.cpp
Expand Up @@ -191,8 +191,11 @@ void MainThread::search() {
else
{
for (Thread* th : Threads)
{
th->bestMoveChanges = 0;
if (th != this)
th->start_searching();
}

Thread::search(); // Let's start searching!
}
Expand Down Expand Up @@ -283,7 +286,7 @@ void Thread::search() {
Move lastBestMove = MOVE_NONE;
Depth lastBestMoveDepth = DEPTH_ZERO;
MainThread* mainThread = (this == Threads.main() ? Threads.main() : nullptr);
double timeReduction = 1.0;
double timeReduction = 1, totBestMoveChanges = 0;
Color us = rootPos.side_to_move();

std::memset(ss-7, 0, 10 * sizeof(Stack));
Expand All @@ -294,9 +297,6 @@ void Thread::search() {
bestValue = delta = alpha = -VALUE_INFINITE;
beta = VALUE_INFINITE;

if (mainThread)
mainThread->bestMoveChanges = 0;

size_t multiPV = Options["MultiPV"];
Skill skill(Options["Skill Level"]);

Expand Down Expand Up @@ -328,7 +328,7 @@ void Thread::search() {
{
// Age out PV variability metric
if (mainThread)
mainThread->bestMoveChanges *= 0.517;
totBestMoveChanges /= 2;

// Save the last iteration's scores before first PV line is searched and
// all the move scores except the (new) PV are set to -VALUE_INFINITE.
Expand Down Expand Up @@ -459,15 +459,18 @@ void Thread::search() {
&& !Threads.stop
&& !mainThread->stopOnPonderhit)
{
double fallingEval = (306 + 9 * (mainThread->previousScore - bestValue)) / 581.0;
double fallingEval = (314 + 9 * (mainThread->previousScore - bestValue)) / 581.0;
fallingEval = clamp(fallingEval, 0.5, 1.5);

// If the bestMove is stable over several iterations, reduce time accordingly
timeReduction = lastBestMoveDepth + 10 * ONE_PLY < completedDepth ? 1.95 : 1.0;
double reduction = std::pow(mainThread->previousTimeReduction, 0.528) / timeReduction;

// Use part of the gained time from a previous stable move for the current move
double bestMoveInstability = 1.0 + mainThread->bestMoveChanges;
for (Thread* th : Threads)
totBestMoveChanges += th->bestMoveChanges;

double bestMoveInstability = 1 + totBestMoveChanges / Threads.size();

// Stop the search if we have only one legal move, or if available time elapsed
if ( rootMoves.size() == 1
Expand Down Expand Up @@ -1101,8 +1104,8 @@ namespace {
// We record how often the best move has been changed in each
// iteration. This information is used for time management: When
// the best move changes frequently, we allocate some more time.
if (moveCount > 1 && thisThread == Threads.main())
++static_cast<MainThread*>(thisThread)->bestMoveChanges;
if (moveCount > 1)
++thisThread->bestMoveChanges;
}
else
// All other moves but the PV are set to the lowest value: this
Expand Down
4 changes: 2 additions & 2 deletions src/thread.h
Expand Up @@ -63,7 +63,7 @@ class Thread {
size_t pvIdx, pvLast;
int selDepth, nmpMinPly;
Color nmpColor;
std::atomic<uint64_t> nodes, tbHits;
std::atomic<uint64_t> nodes, tbHits, bestMoveChanges;

Position rootPos;
Search::RootMoves rootMoves;
Expand All @@ -85,7 +85,7 @@ struct MainThread : public Thread {
void search() override;
void check_time();

double bestMoveChanges, previousTimeReduction;
double previousTimeReduction;
Value previousScore;
int callsCnt;
bool stopOnPonderhit;
Expand Down

0 comments on commit 1982fe2

Please sign in to comment.