mirror of
https://github.com/sockspls/badfish
synced 2025-05-02 01:29:36 +00:00
Move wait_for_stop_or_ponderhit() under Thread
This method belongs to Thread, not to ThreadsManager. Reshuffle stuff in thread.cpp while there. No functional change. Signed-off-by: Marco Costalba <mcostalba@gmail.com>
This commit is contained in:
parent
c94cfebb7e
commit
b1cf1acb93
3 changed files with 78 additions and 78 deletions
|
@ -348,7 +348,7 @@ finalize:
|
||||||
// but if we are pondering or in infinite search, we shouldn't print the best
|
// but if we are pondering or in infinite search, we shouldn't print the best
|
||||||
// move before we are told to do so.
|
// move before we are told to do so.
|
||||||
if (!Signals.stop && (Limits.ponder || Limits.infinite))
|
if (!Signals.stop && (Limits.ponder || Limits.infinite))
|
||||||
Threads.wait_for_stop_or_ponderhit();
|
Threads[pos.thread()].wait_for_stop_or_ponderhit();
|
||||||
|
|
||||||
// Best move could be MOVE_NONE when searching on a stalemate position
|
// Best move could be MOVE_NONE when searching on a stalemate position
|
||||||
cout << "bestmove " << move_to_uci(RootMoves[0].pv[0], Chess960)
|
cout << "bestmove " << move_to_uci(RootMoves[0].pv[0], Chess960)
|
||||||
|
@ -1061,7 +1061,9 @@ split_point_start: // At split points actual search starts from here
|
||||||
sp->bestValue = value;
|
sp->bestValue = value;
|
||||||
sp->ss->bestMove = move;
|
sp->ss->bestMove = move;
|
||||||
sp->alpha = alpha;
|
sp->alpha = alpha;
|
||||||
sp->is_betaCutoff = (value >= beta);
|
|
||||||
|
if (value >= beta)
|
||||||
|
sp->cutoff = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
146
src/thread.cpp
146
src/thread.cpp
|
@ -59,8 +59,54 @@ namespace { extern "C" {
|
||||||
} }
|
} }
|
||||||
|
|
||||||
|
|
||||||
// wake_up() wakes up the thread, normally at the beginning of the search or,
|
// Thread::timer_loop() is where the timer thread waits maxPly milliseconds and
|
||||||
// if "sleeping threads" is used, when there is some work to do.
|
// then calls do_timer_event(). If maxPly is 0 thread sleeps until is woken up.
|
||||||
|
extern void check_time();
|
||||||
|
|
||||||
|
void Thread::timer_loop() {
|
||||||
|
|
||||||
|
while (!do_exit)
|
||||||
|
{
|
||||||
|
lock_grab(sleepLock);
|
||||||
|
timed_wait(sleepCond, sleepLock, maxPly ? maxPly : INT_MAX);
|
||||||
|
lock_release(sleepLock);
|
||||||
|
check_time();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// Thread::main_loop() is where the main thread is parked waiting to be started
|
||||||
|
// when there is a new search. Main thread will launch all the slave threads.
|
||||||
|
|
||||||
|
void Thread::main_loop() {
|
||||||
|
|
||||||
|
while (true)
|
||||||
|
{
|
||||||
|
lock_grab(sleepLock);
|
||||||
|
|
||||||
|
do_sleep = true; // Always return to sleep after a search
|
||||||
|
is_searching = false;
|
||||||
|
|
||||||
|
while (do_sleep && !do_exit)
|
||||||
|
{
|
||||||
|
cond_signal(Threads.sleepCond); // Wake up UI thread if needed
|
||||||
|
cond_wait(sleepCond, sleepLock);
|
||||||
|
}
|
||||||
|
|
||||||
|
lock_release(sleepLock);
|
||||||
|
|
||||||
|
if (do_exit)
|
||||||
|
return;
|
||||||
|
|
||||||
|
is_searching = true;
|
||||||
|
|
||||||
|
Search::think();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// Thread::wake_up() wakes up the thread, normally at the beginning of the search
|
||||||
|
// or, if "sleeping threads" is used, when there is some work to do.
|
||||||
|
|
||||||
void Thread::wake_up() {
|
void Thread::wake_up() {
|
||||||
|
|
||||||
|
@ -70,13 +116,33 @@ void Thread::wake_up() {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// Thread::wait_for_stop_or_ponderhit() is called when the maximum depth is
|
||||||
|
// reached while the program is pondering. The point is to work around a wrinkle
|
||||||
|
// in the UCI protocol: When pondering, the engine is not allowed to give a
|
||||||
|
// "bestmove" before the GUI sends it a "stop" or "ponderhit" command. We simply
|
||||||
|
// wait here until one of these commands (that raise StopRequest) is sent and
|
||||||
|
// then return, after which the bestmove and pondermove will be printed.
|
||||||
|
|
||||||
|
void Thread::wait_for_stop_or_ponderhit() {
|
||||||
|
|
||||||
|
Signals.stopOnPonderhit = true;
|
||||||
|
|
||||||
|
lock_grab(sleepLock);
|
||||||
|
|
||||||
|
while (!Signals.stop)
|
||||||
|
cond_wait(sleepCond, sleepLock);
|
||||||
|
|
||||||
|
lock_release(sleepLock);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
// cutoff_occurred() checks whether a beta cutoff has occurred in the current
|
// cutoff_occurred() checks whether a beta cutoff has occurred in the current
|
||||||
// active split point, or in some ancestor of the split point.
|
// active split point, or in some ancestor of the split point.
|
||||||
|
|
||||||
bool Thread::cutoff_occurred() const {
|
bool Thread::cutoff_occurred() const {
|
||||||
|
|
||||||
for (SplitPoint* sp = splitPoint; sp; sp = sp->parent)
|
for (SplitPoint* sp = splitPoint; sp; sp = sp->parent)
|
||||||
if (sp->is_betaCutoff)
|
if (sp->cutoff)
|
||||||
return true;
|
return true;
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
|
@ -184,10 +250,10 @@ void ThreadsManager::init() {
|
||||||
|
|
||||||
void ThreadsManager::exit() {
|
void ThreadsManager::exit() {
|
||||||
|
|
||||||
assert(threads[0].is_searching == false);
|
|
||||||
|
|
||||||
for (int i = 0; i <= MAX_THREADS; i++)
|
for (int i = 0; i <= MAX_THREADS; i++)
|
||||||
{
|
{
|
||||||
|
assert(threads[i].do_sleep);
|
||||||
|
|
||||||
threads[i].do_exit = true; // Search must be already finished
|
threads[i].do_exit = true; // Search must be already finished
|
||||||
threads[i].wake_up();
|
threads[i].wake_up();
|
||||||
|
|
||||||
|
@ -253,7 +319,7 @@ Value ThreadsManager::split(Position& pos, Stack* ss, Value alpha, Value beta,
|
||||||
|
|
||||||
sp->parent = masterThread.splitPoint;
|
sp->parent = masterThread.splitPoint;
|
||||||
sp->master = master;
|
sp->master = master;
|
||||||
sp->is_betaCutoff = false;
|
sp->cutoff = false;
|
||||||
sp->slavesMask = 1ULL << master;
|
sp->slavesMask = 1ULL << master;
|
||||||
sp->depth = depth;
|
sp->depth = depth;
|
||||||
sp->threatMove = threatMove;
|
sp->threatMove = threatMove;
|
||||||
|
@ -327,22 +393,6 @@ template Value ThreadsManager::split<false>(Position&, Stack*, Value, Value, Val
|
||||||
template Value ThreadsManager::split<true>(Position&, Stack*, Value, Value, Value, Depth, Move, int, MovePicker*, int);
|
template Value ThreadsManager::split<true>(Position&, Stack*, Value, Value, Value, Depth, Move, int, MovePicker*, int);
|
||||||
|
|
||||||
|
|
||||||
// Thread::timer_loop() is where the timer thread waits maxPly milliseconds and
|
|
||||||
// then calls do_timer_event(). If maxPly is 0 thread sleeps until is woken up.
|
|
||||||
extern void check_time();
|
|
||||||
|
|
||||||
void Thread::timer_loop() {
|
|
||||||
|
|
||||||
while (!do_exit)
|
|
||||||
{
|
|
||||||
lock_grab(sleepLock);
|
|
||||||
timed_wait(sleepCond, sleepLock, maxPly ? maxPly : INT_MAX);
|
|
||||||
lock_release(sleepLock);
|
|
||||||
check_time();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
// ThreadsManager::set_timer() is used to set the timer to trigger after msec
|
// ThreadsManager::set_timer() is used to set the timer to trigger after msec
|
||||||
// milliseconds. If msec is 0 then timer is stopped.
|
// milliseconds. If msec is 0 then timer is stopped.
|
||||||
|
|
||||||
|
@ -357,36 +407,6 @@ void ThreadsManager::set_timer(int msec) {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// Thread::main_loop() is where the main thread is parked waiting to be started
|
|
||||||
// when there is a new search. Main thread will launch all the slave threads.
|
|
||||||
|
|
||||||
void Thread::main_loop() {
|
|
||||||
|
|
||||||
while (true)
|
|
||||||
{
|
|
||||||
lock_grab(sleepLock);
|
|
||||||
|
|
||||||
do_sleep = true; // Always return to sleep after a search
|
|
||||||
is_searching = false;
|
|
||||||
|
|
||||||
while (do_sleep && !do_exit)
|
|
||||||
{
|
|
||||||
cond_signal(Threads.sleepCond); // Wake up UI thread if needed
|
|
||||||
cond_wait(sleepCond, sleepLock);
|
|
||||||
}
|
|
||||||
|
|
||||||
is_searching = true;
|
|
||||||
|
|
||||||
lock_release(sleepLock);
|
|
||||||
|
|
||||||
if (do_exit)
|
|
||||||
return;
|
|
||||||
|
|
||||||
Search::think();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
// ThreadsManager::start_thinking() is used by UI thread to wake up the main
|
// ThreadsManager::start_thinking() is used by UI thread to wake up the main
|
||||||
// thread parked in main_loop() and starting a new search. If asyncMode is true
|
// thread parked in main_loop() and starting a new search. If asyncMode is true
|
||||||
// then function returns immediately, otherwise caller is blocked waiting for
|
// then function returns immediately, otherwise caller is blocked waiting for
|
||||||
|
@ -447,25 +467,3 @@ void ThreadsManager::stop_thinking() {
|
||||||
|
|
||||||
lock_release(main.sleepLock);
|
lock_release(main.sleepLock);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// ThreadsManager::wait_for_stop_or_ponderhit() is called when the maximum depth
|
|
||||||
// is reached while the program is pondering. The point is to work around a wrinkle
|
|
||||||
// in the UCI protocol: When pondering, the engine is not allowed to give a
|
|
||||||
// "bestmove" before the GUI sends it a "stop" or "ponderhit" command. We simply
|
|
||||||
// wait here until one of these commands (that raise StopRequest) is sent and
|
|
||||||
// then return, after which the bestmove and pondermove will be printed.
|
|
||||||
|
|
||||||
void ThreadsManager::wait_for_stop_or_ponderhit() {
|
|
||||||
|
|
||||||
Signals.stopOnPonderhit = true;
|
|
||||||
|
|
||||||
Thread& main = threads[0];
|
|
||||||
|
|
||||||
lock_grab(main.sleepLock);
|
|
||||||
|
|
||||||
while (!Signals.stop)
|
|
||||||
cond_wait(main.sleepCond, main.sleepLock);
|
|
||||||
|
|
||||||
lock_release(main.sleepLock);
|
|
||||||
}
|
|
||||||
|
|
|
@ -54,7 +54,7 @@ struct SplitPoint {
|
||||||
volatile Value alpha;
|
volatile Value alpha;
|
||||||
volatile Value bestValue;
|
volatile Value bestValue;
|
||||||
volatile int moveCount;
|
volatile int moveCount;
|
||||||
volatile bool is_betaCutoff;
|
volatile bool cutoff;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
@ -71,6 +71,7 @@ struct Thread {
|
||||||
void idle_loop(SplitPoint* sp_master);
|
void idle_loop(SplitPoint* sp_master);
|
||||||
void main_loop();
|
void main_loop();
|
||||||
void timer_loop();
|
void timer_loop();
|
||||||
|
void wait_for_stop_or_ponderhit();
|
||||||
|
|
||||||
SplitPoint splitPoints[MAX_ACTIVE_SPLIT_POINTS];
|
SplitPoint splitPoints[MAX_ACTIVE_SPLIT_POINTS];
|
||||||
MaterialInfoTable materialTable;
|
MaterialInfoTable materialTable;
|
||||||
|
@ -110,7 +111,6 @@ public:
|
||||||
void read_uci_options();
|
void read_uci_options();
|
||||||
bool available_slave_exists(int master) const;
|
bool available_slave_exists(int master) const;
|
||||||
void set_timer(int msec);
|
void set_timer(int msec);
|
||||||
void wait_for_stop_or_ponderhit();
|
|
||||||
void stop_thinking();
|
void stop_thinking();
|
||||||
void start_thinking(const Position& pos, const Search::LimitsType& limits,
|
void start_thinking(const Position& pos, const Search::LimitsType& limits,
|
||||||
const std::set<Move>& = std::set<Move>(), bool async = false);
|
const std::set<Move>& = std::set<Move>(), bool async = false);
|
||||||
|
|
Loading…
Add table
Reference in a new issue