mirror of
https://github.com/sockspls/badfish
synced 2025-05-02 01:29:36 +00:00
Further reformat lazy smp
No functional change.
This commit is contained in:
parent
9c587288da
commit
309cc4fcd8
4 changed files with 39 additions and 43 deletions
|
@ -40,7 +40,6 @@ namespace Search {
|
||||||
volatile SignalsType Signals;
|
volatile SignalsType Signals;
|
||||||
LimitsType Limits;
|
LimitsType Limits;
|
||||||
StateStackPtr SetupStates;
|
StateStackPtr SetupStates;
|
||||||
CounterMovesHistoryStats CounterMovesHistory;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
namespace Tablebases {
|
namespace Tablebases {
|
||||||
|
@ -130,6 +129,7 @@ namespace {
|
||||||
EasyMoveManager EasyMove;
|
EasyMoveManager EasyMove;
|
||||||
double BestMoveChanges;
|
double BestMoveChanges;
|
||||||
Value DrawValue[COLOR_NB];
|
Value DrawValue[COLOR_NB];
|
||||||
|
CounterMovesHistoryStats CounterMovesHistory;
|
||||||
|
|
||||||
template <NodeType NT>
|
template <NodeType NT>
|
||||||
Value search(Position& pos, Stack* ss, Value alpha, Value beta, Depth depth, bool cutNode);
|
Value search(Position& pos, Stack* ss, Value alpha, Value beta, Depth depth, bool cutNode);
|
||||||
|
@ -219,16 +219,14 @@ uint64_t Search::perft(Position& pos, Depth depth) {
|
||||||
template uint64_t Search::perft<true>(Position& pos, Depth depth);
|
template uint64_t Search::perft<true>(Position& pos, Depth depth);
|
||||||
|
|
||||||
|
|
||||||
/// Search::think() is the external interface to Stockfish's search, and is
|
/// MainThread::think() is called by the main thread when the program receives
|
||||||
/// called by the main thread when the program receives the UCI 'go' command. It
|
/// the UCI 'go' command. It searches from root position and at the end prints
|
||||||
/// searches from RootPos and at the end prints the "bestmove" to output.
|
/// the "bestmove" to output.
|
||||||
|
|
||||||
void Search::think() {
|
void MainThread::think() {
|
||||||
|
|
||||||
Position& rootPos = Threads.main()->pos;
|
Color us = pos.side_to_move();
|
||||||
Search::RootMoveVector& rootMoves = Threads.main()->rootMoves;
|
Time.init(Limits, us, pos.game_ply(), now());
|
||||||
Color us = rootPos.side_to_move();
|
|
||||||
Time.init(Limits, us, rootPos.game_ply(), now());
|
|
||||||
|
|
||||||
int contempt = Options["Contempt"] * PawnValueEg / 100; // From centipawns
|
int contempt = Options["Contempt"] * PawnValueEg / 100; // From centipawns
|
||||||
DrawValue[ us] = VALUE_DRAW - Value(contempt);
|
DrawValue[ us] = VALUE_DRAW - Value(contempt);
|
||||||
|
@ -251,17 +249,17 @@ void Search::think() {
|
||||||
{
|
{
|
||||||
rootMoves.push_back(RootMove(MOVE_NONE));
|
rootMoves.push_back(RootMove(MOVE_NONE));
|
||||||
sync_cout << "info depth 0 score "
|
sync_cout << "info depth 0 score "
|
||||||
<< UCI::value(rootPos.checkers() ? -VALUE_MATE : VALUE_DRAW)
|
<< UCI::value(pos.checkers() ? -VALUE_MATE : VALUE_DRAW)
|
||||||
<< sync_endl;
|
<< sync_endl;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
if (TB::Cardinality >= rootPos.count<ALL_PIECES>(WHITE)
|
if (TB::Cardinality >= pos.count<ALL_PIECES>(WHITE)
|
||||||
+ rootPos.count<ALL_PIECES>(BLACK))
|
+ pos.count<ALL_PIECES>(BLACK))
|
||||||
{
|
{
|
||||||
// If the current root position is in the tablebases then RootMoves
|
// If the current root position is in the tablebases then RootMoves
|
||||||
// contains only moves that preserve the draw or win.
|
// contains only moves that preserve the draw or win.
|
||||||
TB::RootInTB = Tablebases::root_probe(rootPos, rootMoves, TB::Score);
|
TB::RootInTB = Tablebases::root_probe(pos, rootMoves, TB::Score);
|
||||||
|
|
||||||
if (TB::RootInTB)
|
if (TB::RootInTB)
|
||||||
TB::Cardinality = 0; // Do not probe tablebases during the search
|
TB::Cardinality = 0; // Do not probe tablebases during the search
|
||||||
|
@ -269,7 +267,7 @@ void Search::think() {
|
||||||
else // If DTZ tables are missing, use WDL tables as a fallback
|
else // If DTZ tables are missing, use WDL tables as a fallback
|
||||||
{
|
{
|
||||||
// Filter out moves that do not preserve a draw or win
|
// Filter out moves that do not preserve a draw or win
|
||||||
TB::RootInTB = Tablebases::root_probe_wdl(rootPos, rootMoves, TB::Score);
|
TB::RootInTB = Tablebases::root_probe_wdl(pos, rootMoves, TB::Score);
|
||||||
|
|
||||||
// Only probe during search if winning
|
// Only probe during search if winning
|
||||||
if (TB::Score <= VALUE_DRAW)
|
if (TB::Score <= VALUE_DRAW)
|
||||||
|
@ -292,9 +290,9 @@ void Search::think() {
|
||||||
th->maxPly = 0;
|
th->maxPly = 0;
|
||||||
th->depth = DEPTH_ZERO;
|
th->depth = DEPTH_ZERO;
|
||||||
th->searching = true;
|
th->searching = true;
|
||||||
if (th != Threads.main())
|
if (th != this)
|
||||||
{
|
{
|
||||||
th->pos = Position(rootPos, th);
|
th->pos = Position(pos, th);
|
||||||
th->rootMoves = rootMoves;
|
th->rootMoves = rootMoves;
|
||||||
th->notify_one(); // Wake up the thread and start searching
|
th->notify_one(); // Wake up the thread and start searching
|
||||||
}
|
}
|
||||||
|
@ -303,19 +301,15 @@ void Search::think() {
|
||||||
Threads.timer->run = true;
|
Threads.timer->run = true;
|
||||||
Threads.timer->notify_one(); // Start the recurring timer
|
Threads.timer->notify_one(); // Start the recurring timer
|
||||||
|
|
||||||
BestMoveChanges = 0;
|
search(true); // Here we go!
|
||||||
|
|
||||||
TT.new_search();
|
// Stop the threads and the timer
|
||||||
|
|
||||||
Threads.main()->search();
|
|
||||||
|
|
||||||
// Stop the threads and timer
|
|
||||||
Signals.stop = true;
|
Signals.stop = true;
|
||||||
Threads.timer->run = false;
|
Threads.timer->run = false;
|
||||||
|
|
||||||
// Wait until all threads have finished
|
// Wait until all threads have finished
|
||||||
for (Thread* th : Threads)
|
for (Thread* th : Threads)
|
||||||
if (th != Threads.main())
|
if (th != this)
|
||||||
th->wait_while(th->searching);
|
th->wait_while(th->searching);
|
||||||
|
|
||||||
// Clear any candidate easy move that wasn't stable for the last search
|
// Clear any candidate easy move that wasn't stable for the last search
|
||||||
|
@ -328,8 +322,8 @@ void Search::think() {
|
||||||
|
|
||||||
// If skill level is enabled, swap best PV line with the sub-optimal one
|
// If skill level is enabled, swap best PV line with the sub-optimal one
|
||||||
if (skill.enabled())
|
if (skill.enabled())
|
||||||
std::swap(Threads.main()->rootMoves[0], *std::find(Threads.main()->rootMoves.begin(),
|
std::swap(rootMoves[0], *std::find(rootMoves.begin(),
|
||||||
Threads.main()->rootMoves.end(), skill.best_move(multiPV)));
|
rootMoves.end(), skill.best_move(multiPV)));
|
||||||
}
|
}
|
||||||
|
|
||||||
// When playing in 'nodes as time' mode, subtract the searched nodes from
|
// When playing in 'nodes as time' mode, subtract the searched nodes from
|
||||||
|
@ -345,13 +339,13 @@ void Search::think() {
|
||||||
if (!Signals.stop && (Limits.ponder || Limits.infinite))
|
if (!Signals.stop && (Limits.ponder || Limits.infinite))
|
||||||
{
|
{
|
||||||
Signals.stopOnPonderhit = true;
|
Signals.stopOnPonderhit = true;
|
||||||
Threads.main()->wait_for(Signals.stop);
|
wait(Signals.stop);
|
||||||
}
|
}
|
||||||
|
|
||||||
sync_cout << "bestmove " << UCI::move(rootMoves[0].pv[0], rootPos.is_chess960());
|
sync_cout << "bestmove " << UCI::move(rootMoves[0].pv[0], pos.is_chess960());
|
||||||
|
|
||||||
if (rootMoves[0].pv.size() > 1 || rootMoves[0].extract_ponder_from_tt(rootPos))
|
if (rootMoves[0].pv.size() > 1 || rootMoves[0].extract_ponder_from_tt(pos))
|
||||||
std::cout << " ponder " << UCI::move(rootMoves[0].pv[1], rootPos.is_chess960());
|
std::cout << " ponder " << UCI::move(rootMoves[0].pv[1], pos.is_chess960());
|
||||||
|
|
||||||
std::cout << sync_endl;
|
std::cout << sync_endl;
|
||||||
}
|
}
|
||||||
|
@ -361,15 +355,17 @@ void Search::think() {
|
||||||
// repeatedly with increasing depth until the allocated thinking time has been
|
// repeatedly with increasing depth until the allocated thinking time has been
|
||||||
// consumed, user stops the search, or the maximum search depth is reached.
|
// consumed, user stops the search, or the maximum search depth is reached.
|
||||||
|
|
||||||
void Thread::search() {
|
void Thread::search(bool isMainThread) {
|
||||||
|
|
||||||
Value bestValue, alpha, beta, delta;
|
Value bestValue, alpha, beta, delta;
|
||||||
|
|
||||||
Move easyMove = MOVE_NONE;
|
Move easyMove = MOVE_NONE;
|
||||||
if (this == Threads.main())
|
if (isMainThread)
|
||||||
{
|
{
|
||||||
easyMove = EasyMove.get(pos.key());
|
easyMove = EasyMove.get(pos.key());
|
||||||
EasyMove.clear();
|
EasyMove.clear();
|
||||||
|
BestMoveChanges = 0;
|
||||||
|
TT.new_search();
|
||||||
}
|
}
|
||||||
|
|
||||||
Stack* ss = stack+2; // To allow referencing (ss-2) and (ss+2)
|
Stack* ss = stack+2; // To allow referencing (ss-2) and (ss+2)
|
||||||
|
@ -396,7 +392,7 @@ void Thread::search() {
|
||||||
// The main thread modifies other threads rootDepth, if it is <= main
|
// The main thread modifies other threads rootDepth, if it is <= main
|
||||||
// thread depth. The new depth will take effect after the other thread
|
// thread depth. The new depth will take effect after the other thread
|
||||||
// returns to id_loop().
|
// returns to id_loop().
|
||||||
if (this == Threads.main())
|
if (isMainThread)
|
||||||
{
|
{
|
||||||
++depth;
|
++depth;
|
||||||
for (Thread* th : Threads)
|
for (Thread* th : Threads)
|
||||||
|
@ -411,7 +407,7 @@ void Thread::search() {
|
||||||
break;
|
break;
|
||||||
|
|
||||||
// Age out PV variability metric
|
// Age out PV variability metric
|
||||||
if (this == Threads.main())
|
if (isMainThread)
|
||||||
BestMoveChanges *= 0.5;
|
BestMoveChanges *= 0.5;
|
||||||
|
|
||||||
// Save the last iteration's scores before first PV line is searched and
|
// Save the last iteration's scores before first PV line is searched and
|
||||||
|
@ -457,7 +453,7 @@ void Thread::search() {
|
||||||
if (Signals.stop)
|
if (Signals.stop)
|
||||||
break;
|
break;
|
||||||
|
|
||||||
if (this == Threads.main())
|
if (isMainThread)
|
||||||
{
|
{
|
||||||
// When failing high/low give some update (without cluttering
|
// When failing high/low give some update (without cluttering
|
||||||
// the UI) before a re-search.
|
// the UI) before a re-search.
|
||||||
|
@ -474,7 +470,7 @@ void Thread::search() {
|
||||||
beta = (alpha + beta) / 2;
|
beta = (alpha + beta) / 2;
|
||||||
alpha = std::max(bestValue - delta, -VALUE_INFINITE);
|
alpha = std::max(bestValue - delta, -VALUE_INFINITE);
|
||||||
|
|
||||||
if (this == Threads.main())
|
if (isMainThread)
|
||||||
{
|
{
|
||||||
Signals.failedLowAtRoot = true;
|
Signals.failedLowAtRoot = true;
|
||||||
Signals.stopOnPonderhit = false;
|
Signals.stopOnPonderhit = false;
|
||||||
|
@ -507,7 +503,7 @@ void Thread::search() {
|
||||||
sync_cout << UCI::pv(pos, depth, alpha, beta) << sync_endl;
|
sync_cout << UCI::pv(pos, depth, alpha, beta) << sync_endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (this != Threads.main())
|
if (!isMainThread)
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
// If skill level is enabled and time is up, pick a sub-optimal best move
|
// If skill level is enabled and time is up, pick a sub-optimal best move
|
||||||
|
|
|
@ -104,7 +104,6 @@ extern LimitsType Limits;
|
||||||
extern StateStackPtr SetupStates;
|
extern StateStackPtr SetupStates;
|
||||||
|
|
||||||
void init();
|
void init();
|
||||||
void think();
|
|
||||||
void reset();
|
void reset();
|
||||||
template<bool Root> uint64_t perft(Position& pos, Depth depth);
|
template<bool Root> uint64_t perft(Position& pos, Depth depth);
|
||||||
|
|
||||||
|
|
|
@ -66,9 +66,9 @@ void ThreadBase::notify_one() {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// ThreadBase::wait_for() set the thread to sleep until 'condition' turns true
|
// ThreadBase::wait() set the thread to sleep until 'condition' turns true
|
||||||
|
|
||||||
void ThreadBase::wait_for(volatile const bool& condition) {
|
void ThreadBase::wait(volatile const bool& condition) {
|
||||||
|
|
||||||
std::unique_lock<Mutex> lk(mutex);
|
std::unique_lock<Mutex> lk(mutex);
|
||||||
sleepCondition.wait(lk, [&]{ return condition; });
|
sleepCondition.wait(lk, [&]{ return condition; });
|
||||||
|
@ -130,7 +130,7 @@ void Thread::idle_loop() {
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!exit && searching)
|
if (!exit && searching)
|
||||||
this->search();
|
search();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -155,7 +155,7 @@ void MainThread::idle_loop() {
|
||||||
lk.unlock();
|
lk.unlock();
|
||||||
|
|
||||||
if (!exit)
|
if (!exit)
|
||||||
Search::think();
|
think();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -47,7 +47,7 @@ struct ThreadBase : public std::thread {
|
||||||
virtual ~ThreadBase() = default;
|
virtual ~ThreadBase() = default;
|
||||||
virtual void idle_loop() = 0;
|
virtual void idle_loop() = 0;
|
||||||
void notify_one();
|
void notify_one();
|
||||||
void wait_for(volatile const bool& b);
|
void wait(volatile const bool& b);
|
||||||
void wait_while(volatile const bool& b);
|
void wait_while(volatile const bool& b);
|
||||||
|
|
||||||
Mutex mutex;
|
Mutex mutex;
|
||||||
|
@ -65,7 +65,7 @@ struct Thread : public ThreadBase {
|
||||||
|
|
||||||
Thread();
|
Thread();
|
||||||
virtual void idle_loop();
|
virtual void idle_loop();
|
||||||
void search();
|
void search(bool isMainThread = false);
|
||||||
|
|
||||||
Pawns::Table pawnsTable;
|
Pawns::Table pawnsTable;
|
||||||
Material::Table materialTable;
|
Material::Table materialTable;
|
||||||
|
@ -89,6 +89,7 @@ struct Thread : public ThreadBase {
|
||||||
struct MainThread : public Thread {
|
struct MainThread : public Thread {
|
||||||
virtual void idle_loop();
|
virtual void idle_loop();
|
||||||
void join();
|
void join();
|
||||||
|
void think();
|
||||||
volatile bool thinking = true; // Avoid a race with start_thinking()
|
volatile bool thinking = true; // Avoid a race with start_thinking()
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue