2011-11-12 20:44:06 +01:00
|
|
|
/*
|
|
|
|
Stockfish, a UCI chess playing engine derived from Glaurung 2.1
|
|
|
|
Copyright (C) 2004-2008 Tord Romstad (Glaurung author)
|
2015-02-01 01:46:09 +01:00
|
|
|
Copyright (C) 2008-2015 Marco Costalba, Joona Kiiski, Tord Romstad
|
2011-11-12 20:44:06 +01:00
|
|
|
|
|
|
|
Stockfish is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
Stockfish is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2013-05-03 19:03:42 +02:00
|
|
|
#include <algorithm> // For std::count
|
2012-06-04 18:25:51 +02:00
|
|
|
#include <cassert>
|
2011-11-12 20:44:06 +01:00
|
|
|
|
2012-01-15 02:13:33 +01:00
|
|
|
#include "movegen.h"
|
2012-01-01 01:52:19 +01:00
|
|
|
#include "search.h"
|
2011-11-12 20:44:06 +01:00
|
|
|
#include "thread.h"
|
2015-02-01 01:46:09 +01:00
|
|
|
#include "uci.h"
|
2011-11-12 20:44:06 +01:00
|
|
|
|
2012-01-01 01:52:19 +01:00
|
|
|
using namespace Search;
|
|
|
|
|
2012-09-16 17:16:15 +02:00
|
|
|
ThreadPool Threads; // Global object
|
2011-11-12 20:44:06 +01:00
|
|
|
|
2014-05-31 14:23:03 +02:00
|
|
|
extern void check_time();
|
|
|
|
|
2013-08-20 20:02:33 +02:00
|
|
|
namespace {
|
2011-11-12 20:44:06 +01:00
|
|
|
|
2013-08-20 20:02:33 +02:00
|
|
|
// Helpers to launch a thread after creation and joining before delete. Must be
|
2015-02-01 01:46:09 +01:00
|
|
|
// outside Thread c'tor and d'tor because the object must be fully initialized
|
2013-08-20 20:02:33 +02:00
|
|
|
// when start_routine (and hence virtual idle_loop) is called and when joining.
|
2012-06-04 18:25:51 +02:00
|
|
|
|
2013-08-20 20:02:33 +02:00
|
|
|
template<typename T> T* new_thread() {
|
2015-10-23 22:58:14 +02:00
|
|
|
std::thread* th = new T;
|
|
|
|
*th = std::thread(&T::idle_loop, (T*)th); // Will go to sleep
|
|
|
|
return (T*)th;
|
2013-08-20 20:02:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void delete_thread(ThreadBase* th) {
|
2015-02-01 01:46:09 +01:00
|
|
|
|
|
|
|
th->mutex.lock();
|
2013-08-20 20:02:33 +02:00
|
|
|
th->exit = true; // Search must be already finished
|
2015-02-01 01:46:09 +01:00
|
|
|
th->mutex.unlock();
|
|
|
|
|
2013-08-20 20:02:33 +02:00
|
|
|
th->notify_one();
|
2015-10-23 22:58:14 +02:00
|
|
|
th->join(); // Wait for thread termination
|
2013-08-20 20:02:33 +02:00
|
|
|
delete th;
|
|
|
|
}
|
2011-11-12 20:44:06 +01:00
|
|
|
|
2013-07-31 19:49:22 +02:00
|
|
|
}
|
2011-11-12 20:44:06 +01:00
|
|
|
|
2013-08-20 20:02:33 +02:00
|
|
|
|
2015-02-01 01:46:09 +01:00
|
|
|
// ThreadBase::notify_one() wakes up the thread when there is some work to do
|
2013-08-20 20:02:33 +02:00
|
|
|
|
|
|
|
void ThreadBase::notify_one() {
|
|
|
|
|
2015-10-23 22:58:14 +02:00
|
|
|
std::unique_lock<Mutex> lk(mutex);
|
2013-08-20 20:02:33 +02:00
|
|
|
sleepCondition.notify_one();
|
2012-06-04 18:25:51 +02:00
|
|
|
}
|
2011-11-12 20:44:06 +01:00
|
|
|
|
|
|
|
|
2015-10-23 22:58:14 +02:00
|
|
|
// ThreadBase::wait() set the thread to sleep until 'condition' turns true
|
2013-08-20 20:02:33 +02:00
|
|
|
|
2015-10-23 22:58:14 +02:00
|
|
|
void ThreadBase::wait(volatile const bool& condition) {
|
2013-08-20 20:02:33 +02:00
|
|
|
|
2015-10-23 22:58:14 +02:00
|
|
|
std::unique_lock<Mutex> lk(mutex);
|
|
|
|
sleepCondition.wait(lk, [&]{ return condition; });
|
2013-07-31 19:49:22 +02:00
|
|
|
}
|
2012-06-04 18:25:51 +02:00
|
|
|
|
2013-08-20 20:02:33 +02:00
|
|
|
|
2015-10-23 22:58:14 +02:00
|
|
|
// ThreadBase::wait_while() set the thread to sleep until 'condition' turns false
|
2013-08-20 20:02:33 +02:00
|
|
|
|
2015-10-23 22:58:14 +02:00
|
|
|
void ThreadBase::wait_while(volatile const bool& condition) {
|
2013-08-20 20:02:33 +02:00
|
|
|
|
2015-10-23 22:58:14 +02:00
|
|
|
std::unique_lock<Mutex> lk(mutex);
|
|
|
|
sleepCondition.wait(lk, [&]{ return !condition; });
|
2014-05-31 14:23:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-10-23 22:58:14 +02:00
|
|
|
// Thread c'tor makes some init but does not launch any execution thread that
|
|
|
|
// will be started only when c'tor returns.
|
2014-05-31 14:23:03 +02:00
|
|
|
|
2015-10-23 22:58:14 +02:00
|
|
|
Thread::Thread() /* : splitPoints() */ { // Initialization of non POD broken in MSVC
|
2014-05-31 14:23:03 +02:00
|
|
|
|
2015-10-23 22:58:14 +02:00
|
|
|
searching = false;
|
|
|
|
maxPly = 0;
|
|
|
|
idx = Threads.size(); // Starts from 0
|
2014-05-31 14:23:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-10-23 22:58:14 +02:00
|
|
|
// TimerThread::idle_loop() is where the timer thread waits Resolution milliseconds
|
|
|
|
// and then calls check_time(). When not searching, thread sleeps until it's woken up.
|
2014-05-31 14:23:03 +02:00
|
|
|
|
2015-10-23 22:58:14 +02:00
|
|
|
void TimerThread::idle_loop() {
|
2014-05-31 14:23:03 +02:00
|
|
|
|
2015-10-23 22:58:14 +02:00
|
|
|
while (!exit)
|
|
|
|
{
|
|
|
|
std::unique_lock<Mutex> lk(mutex);
|
2014-05-31 14:23:03 +02:00
|
|
|
|
2015-10-23 22:58:14 +02:00
|
|
|
if (!exit)
|
|
|
|
sleepCondition.wait_for(lk, std::chrono::milliseconds(run ? Resolution : INT_MAX));
|
2012-06-04 18:25:51 +02:00
|
|
|
|
2015-10-23 22:58:14 +02:00
|
|
|
lk.unlock();
|
2012-06-04 18:25:51 +02:00
|
|
|
|
2015-10-23 22:58:14 +02:00
|
|
|
if (!exit && run)
|
|
|
|
check_time();
|
2015-02-01 01:46:09 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-10-23 22:58:14 +02:00
|
|
|
// Thread::idle_loop() is where the thread is parked when it has no work to do
|
2012-06-04 18:25:51 +02:00
|
|
|
|
2015-10-23 22:58:14 +02:00
|
|
|
void Thread::idle_loop() {
|
2012-06-04 18:25:51 +02:00
|
|
|
|
2013-05-03 19:03:42 +02:00
|
|
|
while (!exit)
|
2012-06-04 18:25:51 +02:00
|
|
|
{
|
2015-10-23 22:58:14 +02:00
|
|
|
std::unique_lock<Mutex> lk(mutex);
|
2013-05-03 19:03:42 +02:00
|
|
|
|
2015-10-23 22:58:14 +02:00
|
|
|
while (!searching && !exit)
|
|
|
|
sleepCondition.wait(lk);
|
2013-05-03 19:03:42 +02:00
|
|
|
|
2015-10-23 22:58:14 +02:00
|
|
|
lk.unlock();
|
2013-05-03 19:03:42 +02:00
|
|
|
|
2015-10-23 22:58:14 +02:00
|
|
|
if (!exit && searching)
|
|
|
|
search();
|
2012-06-04 18:25:51 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-05-03 19:03:42 +02:00
|
|
|
// MainThread::idle_loop() is where the main thread is parked waiting to be started
|
2014-05-31 14:23:03 +02:00
|
|
|
// when there is a new search. The main thread will launch all the slave threads.
|
2012-06-04 18:25:51 +02:00
|
|
|
|
2013-05-03 19:03:42 +02:00
|
|
|
void MainThread::idle_loop() {
|
2012-06-04 18:25:51 +02:00
|
|
|
|
2015-02-01 01:46:09 +01:00
|
|
|
while (!exit)
|
2012-06-04 18:25:51 +02:00
|
|
|
{
|
2015-10-23 22:58:14 +02:00
|
|
|
std::unique_lock<Mutex> lk(mutex);
|
2012-06-04 18:25:51 +02:00
|
|
|
|
2013-05-03 19:03:42 +02:00
|
|
|
thinking = false;
|
2012-06-04 18:25:51 +02:00
|
|
|
|
2013-05-03 19:03:42 +02:00
|
|
|
while (!thinking && !exit)
|
2012-06-04 18:25:51 +02:00
|
|
|
{
|
2015-10-23 22:58:14 +02:00
|
|
|
sleepCondition.notify_one(); // Wake up the UI thread if needed
|
|
|
|
sleepCondition.wait(lk);
|
2012-06-04 18:25:51 +02:00
|
|
|
}
|
|
|
|
|
2015-10-23 22:58:14 +02:00
|
|
|
lk.unlock();
|
2011-11-12 20:44:06 +01:00
|
|
|
|
2015-02-01 01:46:09 +01:00
|
|
|
if (!exit)
|
2015-10-23 22:58:14 +02:00
|
|
|
think();
|
|
|
|
}
|
|
|
|
}
|
2012-06-04 18:25:51 +02:00
|
|
|
|
2012-09-16 17:16:15 +02:00
|
|
|
|
2015-10-23 22:58:14 +02:00
|
|
|
// MainThread::join() waits for main thread to finish thinking
|
2013-05-03 19:03:42 +02:00
|
|
|
|
2015-10-23 22:58:14 +02:00
|
|
|
void MainThread::join() {
|
|
|
|
|
|
|
|
std::unique_lock<Mutex> lk(mutex);
|
|
|
|
sleepCondition.wait(lk, [&]{ return !thinking; });
|
2012-06-04 18:25:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-02-01 01:46:09 +01:00
|
|
|
// ThreadPool::init() is called at startup to create and launch requested threads,
|
|
|
|
// that will go immediately to sleep. We cannot use a c'tor because Threads is a
|
|
|
|
// static object and we need a fully initialized engine at this point due to
|
|
|
|
// allocation of Endgames in Thread c'tor.
|
2011-11-12 20:44:06 +01:00
|
|
|
|
2012-09-16 17:16:15 +02:00
|
|
|
void ThreadPool::init() {
|
2012-01-01 01:52:19 +01:00
|
|
|
|
2013-08-20 20:02:33 +02:00
|
|
|
timer = new_thread<TimerThread>();
|
|
|
|
push_back(new_thread<MainThread>());
|
2012-06-04 18:25:51 +02:00
|
|
|
read_uci_options();
|
2011-11-12 20:44:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-02-01 01:46:09 +01:00
|
|
|
// ThreadPool::exit() terminates the threads before the program exits. Cannot be
|
|
|
|
// done in d'tor because threads must be terminated before freeing us.
|
2011-11-12 20:44:06 +01:00
|
|
|
|
2012-09-16 17:16:15 +02:00
|
|
|
void ThreadPool::exit() {
|
2011-11-12 20:44:06 +01:00
|
|
|
|
2013-08-20 20:02:33 +02:00
|
|
|
delete_thread(timer); // As first because check_time() accesses threads data
|
2015-10-23 22:58:14 +02:00
|
|
|
timer = nullptr;
|
|
|
|
|
|
|
|
for (Thread* th : *this)
|
|
|
|
delete_thread(th);
|
2011-11-12 20:44:06 +01:00
|
|
|
|
2015-10-23 22:58:14 +02:00
|
|
|
clear(); // Get rid of stale pointers
|
2012-01-01 01:52:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-02-01 01:46:09 +01:00
|
|
|
// ThreadPool::read_uci_options() updates internal threads parameters from the
|
|
|
|
// corresponding UCI options and creates/destroys threads to match the requested
|
|
|
|
// number. Thread objects are dynamically allocated to avoid creating all possible
|
|
|
|
// threads in advance (which include pawns and material tables), even if only a
|
|
|
|
// few are to be used.
|
2012-01-01 01:52:19 +01:00
|
|
|
|
2012-09-16 17:16:15 +02:00
|
|
|
void ThreadPool::read_uci_options() {
|
2011-11-12 20:44:06 +01:00
|
|
|
|
2014-05-31 14:23:03 +02:00
|
|
|
size_t requested = Options["Threads"];
|
2011-11-12 20:44:06 +01:00
|
|
|
|
2012-06-04 18:25:51 +02:00
|
|
|
assert(requested > 0);
|
2011-11-12 20:44:06 +01:00
|
|
|
|
2013-05-03 19:03:42 +02:00
|
|
|
while (size() < requested)
|
2013-08-20 20:02:33 +02:00
|
|
|
push_back(new_thread<Thread>());
|
2011-11-12 20:44:06 +01:00
|
|
|
|
2013-05-03 19:03:42 +02:00
|
|
|
while (size() > requested)
|
2011-11-12 20:44:06 +01:00
|
|
|
{
|
2013-08-20 20:02:33 +02:00
|
|
|
delete_thread(back());
|
2013-05-03 19:03:42 +02:00
|
|
|
pop_back();
|
2012-06-04 18:25:51 +02:00
|
|
|
}
|
2011-11-12 20:44:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-10-23 22:58:14 +02:00
|
|
|
// ThreadPool::nodes_searched() returns the number of nodes searched
|
2012-01-01 01:52:19 +01:00
|
|
|
|
2015-10-23 22:58:14 +02:00
|
|
|
int64_t ThreadPool::nodes_searched() {
|
2012-01-01 01:52:19 +01:00
|
|
|
|
2015-10-23 22:58:14 +02:00
|
|
|
int64_t nodes = 0;
|
|
|
|
for (Thread *th : *this)
|
|
|
|
nodes += th->rootPos.nodes_searched();
|
|
|
|
return nodes;
|
2012-01-01 01:52:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-02-01 01:46:09 +01:00
|
|
|
// ThreadPool::start_thinking() wakes up the main thread sleeping in
|
|
|
|
// MainThread::idle_loop() and starts a new search, then returns immediately.
|
2014-05-31 14:23:03 +02:00
|
|
|
|
2015-02-01 01:46:09 +01:00
|
|
|
void ThreadPool::start_thinking(const Position& pos, const LimitsType& limits,
|
|
|
|
StateStackPtr& states) {
|
2015-10-23 22:58:14 +02:00
|
|
|
main()->join();
|
2012-01-01 01:52:19 +01:00
|
|
|
|
2012-06-04 18:25:51 +02:00
|
|
|
Signals.stopOnPonderhit = Signals.firstRootMove = false;
|
|
|
|
Signals.stop = Signals.failedLowAtRoot = false;
|
2012-01-01 01:52:19 +01:00
|
|
|
|
2015-10-23 22:58:14 +02:00
|
|
|
main()->rootMoves.clear();
|
|
|
|
main()->rootPos = pos;
|
2012-01-01 01:52:19 +01:00
|
|
|
Limits = limits;
|
2013-08-20 20:02:33 +02:00
|
|
|
if (states.get()) // If we don't set a new position, preserve current state
|
|
|
|
{
|
2015-10-23 22:58:14 +02:00
|
|
|
SetupStates = std::move(states); // Ownership transfer here
|
2013-08-20 20:02:33 +02:00
|
|
|
assert(!states.get());
|
|
|
|
}
|
2012-01-15 02:13:33 +01:00
|
|
|
|
2015-10-23 22:58:14 +02:00
|
|
|
for (const auto& m : MoveList<LEGAL>(pos))
|
2014-05-31 14:23:03 +02:00
|
|
|
if ( limits.searchmoves.empty()
|
2015-10-23 22:58:14 +02:00
|
|
|
|| std::count(limits.searchmoves.begin(), limits.searchmoves.end(), m))
|
|
|
|
main()->rootMoves.push_back(RootMove(m));
|
2012-01-01 01:52:19 +01:00
|
|
|
|
2013-08-20 20:02:33 +02:00
|
|
|
main()->thinking = true;
|
2015-10-23 22:58:14 +02:00
|
|
|
main()->notify_one(); // Wake up main thread: 'thinking' must be already set
|
2012-01-01 01:52:19 +01:00
|
|
|
}
|