2011-04-24 02:20:03 -06:00
|
|
|
/*
|
|
|
|
Stockfish, a UCI chess playing engine derived from Glaurung 2.1
|
|
|
|
Copyright (C) 2004-2008 Tord Romstad (Glaurung author)
|
2013-02-18 23:54:14 -07:00
|
|
|
Copyright (C) 2008-2013 Marco Costalba, Joona Kiiski, Tord Romstad
|
2011-04-24 02:20:03 -06: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-02-02 08:04:41 -07:00
|
|
|
#include <algorithm> // For std::count
|
2012-01-16 13:20:13 -07:00
|
|
|
#include <cassert>
|
2011-04-24 02:20:03 -06:00
|
|
|
#include <iostream>
|
|
|
|
|
2012-01-14 05:49:25 -07:00
|
|
|
#include "movegen.h"
|
2011-11-05 04:19:21 -06:00
|
|
|
#include "search.h"
|
2011-04-24 02:20:03 -06:00
|
|
|
#include "thread.h"
|
|
|
|
#include "ucioption.h"
|
|
|
|
|
2011-12-04 03:46:31 -07:00
|
|
|
using namespace Search;
|
|
|
|
|
2012-06-24 02:30:40 -06:00
|
|
|
ThreadPool Threads; // Global object
|
2011-04-24 02:20:03 -06:00
|
|
|
|
2011-04-29 02:07:23 -06:00
|
|
|
namespace { extern "C" {
|
2011-04-24 02:20:03 -06:00
|
|
|
|
2011-04-29 02:07:23 -06:00
|
|
|
// start_routine() is the C function which is called when a new thread
|
2013-01-13 16:32:30 -07:00
|
|
|
// is launched. It is a wrapper to the virtual function idle_loop().
|
2011-04-24 02:20:03 -06:00
|
|
|
|
2013-01-13 16:32:30 -07:00
|
|
|
long start_routine(Thread* th) { th->idle_loop(); return 0; }
|
2011-04-24 02:20:03 -06:00
|
|
|
|
2012-04-06 04:39:07 -06:00
|
|
|
} }
|
2011-04-24 02:20:03 -06:00
|
|
|
|
2012-04-06 11:36:46 -06:00
|
|
|
|
2012-03-25 05:01:56 -06:00
|
|
|
// Thread c'tor starts a newly-created thread of execution that will call
|
2013-01-13 16:32:30 -07:00
|
|
|
// the the virtual function idle_loop(), going immediately to sleep.
|
2012-03-24 14:36:33 -06:00
|
|
|
|
2013-02-08 00:49:36 -07:00
|
|
|
Thread::Thread() /* : splitPoints() */ { // Value-initialization bug in MSVC
|
2012-03-24 13:10:13 -06:00
|
|
|
|
2013-01-16 01:28:41 -07:00
|
|
|
searching = exit = false;
|
2013-01-20 03:54:30 -07:00
|
|
|
maxPly = splitPointsSize = 0;
|
|
|
|
activeSplitPoint = NULL;
|
2013-02-08 03:04:07 -07:00
|
|
|
activePosition = NULL;
|
2012-04-04 00:54:02 -06:00
|
|
|
idx = Threads.size();
|
2013-01-13 15:51:15 -07:00
|
|
|
|
2012-03-24 13:10:13 -06:00
|
|
|
if (!thread_create(handle, start_routine, this))
|
|
|
|
{
|
2012-04-04 00:54:02 -06:00
|
|
|
std::cerr << "Failed to create thread number " << idx << std::endl;
|
2012-03-24 13:10:13 -06:00
|
|
|
::exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-01-13 16:32:30 -07:00
|
|
|
// Thread d'tor waits for thread termination before to return
|
2012-03-24 14:36:33 -06:00
|
|
|
|
2012-03-24 13:10:13 -06:00
|
|
|
Thread::~Thread() {
|
|
|
|
|
2013-01-16 01:28:41 -07:00
|
|
|
exit = true; // Search must be already finished
|
2013-01-13 07:35:38 -07:00
|
|
|
notify_one();
|
2012-03-24 13:10:13 -06:00
|
|
|
thread_join(handle); // Wait for thread termination
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-01-13 16:32:30 -07:00
|
|
|
// TimerThread::idle_loop() is where the timer thread waits msec milliseconds
|
|
|
|
// and then calls check_time(). If msec is 0 thread sleeps until is woken up.
|
2012-02-03 08:07:13 -07:00
|
|
|
extern void check_time();
|
|
|
|
|
2013-01-13 16:32:30 -07:00
|
|
|
void TimerThread::idle_loop() {
|
2012-02-03 08:07:13 -07:00
|
|
|
|
2013-01-16 01:28:41 -07:00
|
|
|
while (!exit)
|
2012-02-03 08:07:13 -07:00
|
|
|
{
|
2012-08-24 03:59:41 -06:00
|
|
|
mutex.lock();
|
2013-01-16 01:26:10 -07:00
|
|
|
|
2013-01-16 01:28:41 -07:00
|
|
|
if (!exit)
|
2013-01-16 01:26:10 -07:00
|
|
|
sleepCondition.wait_for(mutex, msec ? msec : INT_MAX);
|
|
|
|
|
2012-08-24 03:59:41 -06:00
|
|
|
mutex.unlock();
|
2013-01-14 11:32:30 -07:00
|
|
|
|
2013-01-16 01:26:10 -07:00
|
|
|
if (msec)
|
|
|
|
check_time();
|
2012-02-03 08:07:13 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-01-13 16:32:30 -07:00
|
|
|
// MainThread::idle_loop() is where the main thread is parked waiting to be started
|
2012-02-03 08:07:13 -07:00
|
|
|
// when there is a new search. Main thread will launch all the slave threads.
|
|
|
|
|
2013-01-13 16:32:30 -07:00
|
|
|
void MainThread::idle_loop() {
|
2012-02-03 08:07:13 -07:00
|
|
|
|
|
|
|
while (true)
|
|
|
|
{
|
2012-08-24 03:59:41 -06:00
|
|
|
mutex.lock();
|
2012-02-03 08:07:13 -07:00
|
|
|
|
2013-01-16 01:28:41 -07:00
|
|
|
thinking = false;
|
2012-02-03 08:07:13 -07:00
|
|
|
|
2013-01-16 01:28:41 -07:00
|
|
|
while (!thinking && !exit)
|
2012-02-03 08:07:13 -07:00
|
|
|
{
|
2012-08-24 03:59:41 -06:00
|
|
|
Threads.sleepCondition.notify_one(); // Wake up UI thread if needed
|
|
|
|
sleepCondition.wait(mutex);
|
2012-02-03 08:07:13 -07:00
|
|
|
}
|
|
|
|
|
2012-08-24 03:59:41 -06:00
|
|
|
mutex.unlock();
|
2012-02-03 08:07:13 -07:00
|
|
|
|
2013-01-16 01:28:41 -07:00
|
|
|
if (exit)
|
2012-02-03 08:07:13 -07:00
|
|
|
return;
|
|
|
|
|
2013-01-16 01:28:41 -07:00
|
|
|
searching = true;
|
2012-02-03 08:07:13 -07:00
|
|
|
|
|
|
|
Search::think();
|
2012-08-24 05:40:58 -06:00
|
|
|
|
2013-01-16 01:28:41 -07:00
|
|
|
assert(searching);
|
|
|
|
|
|
|
|
searching = false;
|
2012-02-03 08:07:13 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-01-16 01:28:41 -07:00
|
|
|
// Thread::notify_one() wakes up the thread when there is some search to do
|
2011-04-24 17:22:48 -06:00
|
|
|
|
2013-01-13 07:35:38 -07:00
|
|
|
void Thread::notify_one() {
|
2011-04-24 17:22:48 -06:00
|
|
|
|
2012-08-24 03:59:41 -06:00
|
|
|
mutex.lock();
|
|
|
|
sleepCondition.notify_one();
|
|
|
|
mutex.unlock();
|
2011-04-24 17:22:48 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-01-13 07:50:57 -07:00
|
|
|
// Thread::wait_for() set the thread to sleep until condition 'b' turns true
|
2012-02-03 08:07:13 -07:00
|
|
|
|
2013-01-13 07:50:57 -07:00
|
|
|
void Thread::wait_for(volatile const bool& b) {
|
2012-02-03 08:07:13 -07:00
|
|
|
|
2012-08-24 03:59:41 -06:00
|
|
|
mutex.lock();
|
2013-01-13 07:50:57 -07:00
|
|
|
while (!b) sleepCondition.wait(mutex);
|
2012-08-24 03:59:41 -06:00
|
|
|
mutex.unlock();
|
2012-02-03 08:07:13 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-03-26 00:58:10 -06:00
|
|
|
// Thread::cutoff_occurred() checks whether a beta cutoff has occurred in the
|
|
|
|
// current active split point, or in some ancestor of the split point.
|
2011-04-24 17:22:48 -06:00
|
|
|
|
|
|
|
bool Thread::cutoff_occurred() const {
|
|
|
|
|
2013-02-04 11:48:56 -07:00
|
|
|
for (SplitPoint* sp = activeSplitPoint; sp; sp = sp->parentSplitPoint)
|
2012-02-03 08:07:13 -07:00
|
|
|
if (sp->cutoff)
|
2011-04-24 17:22:48 -06:00
|
|
|
return true;
|
2011-11-28 07:54:40 -07:00
|
|
|
|
2011-04-24 17:22:48 -06:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-03-26 00:58:10 -06:00
|
|
|
// Thread::is_available_to() checks whether the thread is available to help the
|
2012-04-04 00:54:02 -06:00
|
|
|
// thread 'master' at a split point. An obvious requirement is that thread must
|
|
|
|
// be idle. With more than two threads, this is not sufficient: If the thread is
|
2013-01-20 03:54:30 -07:00
|
|
|
// the master of some split point, it is only available as a slave to the slaves
|
|
|
|
// which are busy searching the split point at the top of slaves split point
|
|
|
|
// stack (the "helpful master concept" in YBWC terminology).
|
2011-04-24 17:22:48 -06:00
|
|
|
|
2012-04-06 06:46:53 -06:00
|
|
|
bool Thread::is_available_to(Thread* master) const {
|
2011-04-24 17:22:48 -06:00
|
|
|
|
2013-01-16 01:28:41 -07:00
|
|
|
if (searching)
|
2011-04-24 17:22:48 -06:00
|
|
|
return false;
|
|
|
|
|
|
|
|
// Make a local copy to be sure doesn't become zero under our feet while
|
|
|
|
// testing next condition and so leading to an out of bound access.
|
2013-01-20 03:54:30 -07:00
|
|
|
int size = splitPointsSize;
|
2011-04-24 17:22:48 -06:00
|
|
|
|
2013-01-20 03:54:30 -07:00
|
|
|
// No split points means that the thread is available as a slave for any
|
2011-04-24 17:22:48 -06:00
|
|
|
// other thread otherwise apply the "helpful master" concept if possible.
|
2013-01-20 03:54:30 -07:00
|
|
|
return !size || (splitPoints[size - 1].slavesMask & (1ULL << master->idx));
|
2011-04-24 17:22:48 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-01-21 09:22:31 -07:00
|
|
|
// init() is called at startup to create and launch requested threads, that will
|
|
|
|
// go immediately to sleep due to 'sleepWhileIdle' set to true. We cannot use
|
2012-03-26 00:58:10 -06:00
|
|
|
// a c'tor becuase Threads is a static object and we need a fully initialized
|
2013-01-21 09:22:31 -07:00
|
|
|
// engine at this point due to allocation of Endgames in Thread c'tor.
|
2012-03-26 00:58:10 -06:00
|
|
|
|
2012-06-24 02:30:40 -06:00
|
|
|
void ThreadPool::init() {
|
2012-03-26 00:58:10 -06:00
|
|
|
|
2013-01-13 10:38:44 -07:00
|
|
|
sleepWhileIdle = true;
|
2013-01-13 16:32:30 -07:00
|
|
|
timer = new TimerThread();
|
2013-02-04 14:38:42 -07:00
|
|
|
push_back(new MainThread());
|
2012-03-26 00:58:10 -06:00
|
|
|
read_uci_options();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-01-21 09:22:31 -07:00
|
|
|
// exit() cleanly terminates the threads before the program exits
|
2012-03-26 00:58:10 -06:00
|
|
|
|
2012-08-29 08:43:01 -06:00
|
|
|
void ThreadPool::exit() {
|
2012-03-26 00:58:10 -06:00
|
|
|
|
2013-01-21 09:22:31 -07:00
|
|
|
delete timer; // As first because check_time() accesses threads data
|
2012-09-29 22:49:56 -06:00
|
|
|
|
2013-02-04 14:38:42 -07:00
|
|
|
for (iterator it = begin(); it != end(); ++it)
|
|
|
|
delete *it;
|
2012-03-26 00:58:10 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-02-12 06:07:21 -07:00
|
|
|
// read_uci_options() updates internal threads parameters from the corresponding
|
2012-03-24 14:36:33 -06:00
|
|
|
// UCI options and creates/destroys threads to match the requested number. Thread
|
|
|
|
// objects are dynamically allocated to avoid creating in advance all possible
|
|
|
|
// threads, with included pawns and material tables, if only few are used.
|
2011-04-24 02:20:03 -06:00
|
|
|
|
2012-06-24 02:30:40 -06:00
|
|
|
void ThreadPool::read_uci_options() {
|
2011-04-24 02:20:03 -06:00
|
|
|
|
2011-12-28 10:27:18 -07:00
|
|
|
maxThreadsPerSplitPoint = Options["Max Threads per Split Point"];
|
|
|
|
minimumSplitDepth = Options["Min Split Depth"] * ONE_PLY;
|
2012-08-19 03:20:15 -06:00
|
|
|
size_t requested = Options["Threads"];
|
2012-03-24 14:36:33 -06:00
|
|
|
|
2012-03-25 05:01:56 -06:00
|
|
|
assert(requested > 0);
|
|
|
|
|
2013-02-04 14:38:42 -07:00
|
|
|
while (size() < requested)
|
|
|
|
push_back(new Thread());
|
2012-03-24 14:36:33 -06:00
|
|
|
|
2013-02-04 14:38:42 -07:00
|
|
|
while (size() > requested)
|
2012-03-24 14:36:33 -06:00
|
|
|
{
|
2013-02-04 14:38:42 -07:00
|
|
|
delete back();
|
|
|
|
pop_back();
|
2012-03-24 14:36:33 -06:00
|
|
|
}
|
2012-03-24 12:29:12 -06:00
|
|
|
}
|
2011-08-08 16:07:09 -06:00
|
|
|
|
2012-02-12 06:07:21 -07:00
|
|
|
|
2013-01-20 03:54:30 -07:00
|
|
|
// slave_available() tries to find an idle thread which is available as a slave
|
|
|
|
// for the thread 'master'.
|
2011-04-24 02:20:03 -06:00
|
|
|
|
2013-02-06 01:36:13 -07:00
|
|
|
Thread* ThreadPool::available_slave(Thread* master) const {
|
2011-04-24 02:20:03 -06:00
|
|
|
|
2013-02-04 14:38:42 -07:00
|
|
|
for (const_iterator it = begin(); it != end(); ++it)
|
|
|
|
if ((*it)->is_available_to(master))
|
2013-02-06 01:36:13 -07:00
|
|
|
return *it;
|
2011-04-24 02:20:03 -06:00
|
|
|
|
2013-02-06 01:36:13 -07:00
|
|
|
return NULL;
|
2011-04-24 02:20:03 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// split() does the actual work of distributing the work at a node between
|
2011-11-28 07:54:40 -07:00
|
|
|
// several available threads. If it does not succeed in splitting the node
|
2013-01-21 09:22:31 -07:00
|
|
|
// (because no idle threads are available), the function immediately returns.
|
|
|
|
// If splitting is possible, a SplitPoint object is initialized with all the
|
|
|
|
// data that must be copied to the helper threads and then helper threads are
|
|
|
|
// told that they have been assigned work. This will cause them to instantly
|
|
|
|
// leave their idle loops and call search(). When all threads have returned from
|
|
|
|
// search() then split() returns.
|
2011-04-24 02:20:03 -06:00
|
|
|
|
|
|
|
template <bool Fake>
|
2013-02-04 22:30:05 -07:00
|
|
|
void Thread::split(Position& pos, Stack* ss, Value alpha, Value beta, Value* bestValue,
|
|
|
|
Move* bestMove, Depth depth, Move threatMove, int moveCount,
|
2013-06-09 01:43:04 -06:00
|
|
|
MovePicker* movePicker, int nodeType, bool cutNode) {
|
2012-06-24 02:30:40 -06:00
|
|
|
|
2011-10-16 16:56:25 -06:00
|
|
|
assert(pos.pos_is_ok());
|
2013-02-04 22:30:05 -07:00
|
|
|
assert(*bestValue <= alpha && alpha < beta && beta <= VALUE_INFINITE);
|
|
|
|
assert(*bestValue > -VALUE_INFINITE);
|
2013-01-21 09:22:31 -07:00
|
|
|
assert(depth >= Threads.minimumSplitDepth);
|
2013-02-04 14:09:52 -07:00
|
|
|
assert(searching);
|
|
|
|
assert(splitPointsSize < MAX_SPLITPOINTS_PER_THREAD);
|
2011-04-24 02:20:03 -06:00
|
|
|
|
2011-11-28 07:54:40 -07:00
|
|
|
// Pick the next available split point from the split point stack
|
2013-02-04 14:09:52 -07:00
|
|
|
SplitPoint& sp = splitPoints[splitPointsSize];
|
2012-08-20 00:54:38 -06:00
|
|
|
|
2013-02-04 14:09:52 -07:00
|
|
|
sp.masterThread = this;
|
|
|
|
sp.parentSplitPoint = activeSplitPoint;
|
|
|
|
sp.slavesMask = 1ULL << idx;
|
2012-08-20 00:54:38 -06:00
|
|
|
sp.depth = depth;
|
2013-02-04 22:30:05 -07:00
|
|
|
sp.bestValue = *bestValue;
|
2012-08-20 00:54:38 -06:00
|
|
|
sp.bestMove = *bestMove;
|
|
|
|
sp.threatMove = threatMove;
|
|
|
|
sp.alpha = alpha;
|
|
|
|
sp.beta = beta;
|
|
|
|
sp.nodeType = nodeType;
|
2013-06-09 01:43:04 -06:00
|
|
|
sp.cutNode = cutNode;
|
2013-02-04 22:30:05 -07:00
|
|
|
sp.movePicker = movePicker;
|
2012-08-20 00:54:38 -06:00
|
|
|
sp.moveCount = moveCount;
|
|
|
|
sp.pos = &pos;
|
|
|
|
sp.nodes = 0;
|
2013-01-20 03:54:30 -07:00
|
|
|
sp.cutoff = false;
|
2012-08-20 00:54:38 -06:00
|
|
|
sp.ss = ss;
|
2011-11-28 07:54:40 -07:00
|
|
|
|
2011-08-07 12:56:53 -06:00
|
|
|
// Try to allocate available threads and ask them to start searching setting
|
2013-01-20 03:54:30 -07:00
|
|
|
// 'searching' flag. This must be done under lock protection to avoid concurrent
|
2011-11-28 07:54:40 -07:00
|
|
|
// allocation of the same slave by another master.
|
2013-02-04 14:09:52 -07:00
|
|
|
Threads.mutex.lock();
|
2012-09-29 22:49:56 -06:00
|
|
|
sp.mutex.lock();
|
2011-04-24 02:20:03 -06:00
|
|
|
|
2013-02-04 14:09:52 -07:00
|
|
|
splitPointsSize++;
|
|
|
|
activeSplitPoint = &sp;
|
2013-02-08 03:04:07 -07:00
|
|
|
activePosition = NULL;
|
2013-01-21 09:22:31 -07:00
|
|
|
|
2013-02-04 14:38:42 -07:00
|
|
|
size_t slavesCnt = 1; // This thread is always included
|
2013-02-06 01:36:13 -07:00
|
|
|
Thread* slave;
|
2013-01-21 09:22:31 -07:00
|
|
|
|
2013-02-06 01:36:13 -07:00
|
|
|
while ( (slave = Threads.available_slave(this)) != NULL
|
|
|
|
&& ++slavesCnt <= Threads.maxThreadsPerSplitPoint && !Fake)
|
2013-02-04 14:38:42 -07:00
|
|
|
{
|
2013-02-06 01:36:13 -07:00
|
|
|
sp.slavesMask |= 1ULL << slave->idx;
|
|
|
|
slave->activeSplitPoint = &sp;
|
|
|
|
slave->searching = true; // Slave leaves idle_loop()
|
|
|
|
slave->notify_one(); // Could be sleeping
|
2013-02-04 14:38:42 -07:00
|
|
|
}
|
2011-04-24 02:20:03 -06:00
|
|
|
|
2011-08-10 05:12:10 -06:00
|
|
|
// Everything is set up. The master thread enters the idle loop, from which
|
2013-01-20 03:54:30 -07:00
|
|
|
// it will instantly launch a search, because its 'searching' flag is set.
|
2012-08-19 04:20:33 -06:00
|
|
|
// The thread will return from the idle loop when all slaves have finished
|
2011-08-10 05:12:10 -06:00
|
|
|
// their work at this split point.
|
2013-01-21 09:22:31 -07:00
|
|
|
if (slavesCnt > 1 || Fake)
|
2012-02-19 02:32:06 -07:00
|
|
|
{
|
2013-02-27 17:55:56 -07:00
|
|
|
sp.mutex.unlock();
|
|
|
|
Threads.mutex.unlock();
|
|
|
|
|
2013-02-04 14:09:52 -07:00
|
|
|
Thread::idle_loop(); // Force a call to base class idle_loop()
|
2011-04-24 02:20:03 -06:00
|
|
|
|
2012-02-19 02:32:06 -07:00
|
|
|
// In helpful master concept a master can help only a sub-tree of its split
|
|
|
|
// point, and because here is all finished is not possible master is booked.
|
2013-02-04 14:09:52 -07:00
|
|
|
assert(!searching);
|
2013-02-08 03:04:07 -07:00
|
|
|
assert(!activePosition);
|
2012-02-19 02:32:06 -07:00
|
|
|
|
2013-02-27 17:55:56 -07:00
|
|
|
// We have returned from the idle loop, which means that all threads are
|
|
|
|
// finished. Note that setting 'searching' and decreasing splitPointsSize is
|
|
|
|
// done under lock protection to avoid a race with Thread::is_available_to().
|
|
|
|
Threads.mutex.lock();
|
|
|
|
sp.mutex.lock();
|
|
|
|
}
|
2012-01-26 15:18:43 -07:00
|
|
|
|
2013-02-04 14:09:52 -07:00
|
|
|
searching = true;
|
|
|
|
splitPointsSize--;
|
|
|
|
activeSplitPoint = sp.parentSplitPoint;
|
2013-02-08 03:04:07 -07:00
|
|
|
activePosition = &pos;
|
2012-08-20 00:54:38 -06:00
|
|
|
pos.set_nodes_searched(pos.nodes_searched() + sp.nodes);
|
|
|
|
*bestMove = sp.bestMove;
|
2013-02-04 22:30:05 -07:00
|
|
|
*bestValue = sp.bestValue;
|
2011-08-08 03:53:52 -06:00
|
|
|
|
2012-08-24 03:59:41 -06:00
|
|
|
sp.mutex.unlock();
|
2013-02-04 14:09:52 -07:00
|
|
|
Threads.mutex.unlock();
|
2011-04-24 02:20:03 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// Explicit template instantiations
|
2013-06-09 01:43:04 -06:00
|
|
|
template void Thread::split<false>(Position&, Stack*, Value, Value, Value*, Move*, Depth, Move, int, MovePicker*, int, bool);
|
|
|
|
template void Thread::split< true>(Position&, Stack*, Value, Value, Value*, Move*, Depth, Move, int, MovePicker*, int, bool);
|
2011-11-05 00:53:19 -06:00
|
|
|
|
|
|
|
|
2013-01-16 01:28:41 -07:00
|
|
|
// wait_for_think_finished() waits for main thread to go to sleep then returns
|
2012-03-26 09:19:37 -06:00
|
|
|
|
2013-01-16 01:28:41 -07:00
|
|
|
void ThreadPool::wait_for_think_finished() {
|
2012-03-26 09:19:37 -06:00
|
|
|
|
2013-01-13 16:32:30 -07:00
|
|
|
MainThread* t = main_thread();
|
2012-08-24 03:59:41 -06:00
|
|
|
t->mutex.lock();
|
2013-01-16 01:28:41 -07:00
|
|
|
while (t->thinking) sleepCondition.wait(t->mutex);
|
2012-08-24 03:59:41 -06:00
|
|
|
t->mutex.unlock();
|
2012-03-26 09:19:37 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-01-26 06:23:37 -07:00
|
|
|
// start_thinking() wakes up the main thread sleeping in MainThread::idle_loop()
|
|
|
|
// so to start a new search, then returns immediately.
|
2011-11-05 00:53:19 -06:00
|
|
|
|
2013-03-04 01:28:21 -07:00
|
|
|
void ThreadPool::start_thinking(const Position& pos, const LimitsType& limits,
|
|
|
|
const std::vector<Move>& searchMoves, StateStackPtr& states) {
|
2013-01-16 01:28:41 -07:00
|
|
|
wait_for_think_finished();
|
2012-03-26 00:58:10 -06:00
|
|
|
|
2012-09-02 08:39:01 -06:00
|
|
|
SearchTime = Time::now(); // As early as possible
|
2012-03-31 03:01:31 -06:00
|
|
|
|
2012-03-26 00:58:10 -06:00
|
|
|
Signals.stopOnPonderhit = Signals.firstRootMove = false;
|
|
|
|
Signals.stop = Signals.failedLowAtRoot = false;
|
2011-11-27 04:16:23 -07:00
|
|
|
|
2013-06-01 07:45:46 -06:00
|
|
|
RootMoves.clear();
|
2012-10-24 06:37:52 -06:00
|
|
|
RootPos = pos;
|
2011-12-04 03:46:31 -07:00
|
|
|
Limits = limits;
|
2013-06-01 07:45:46 -06:00
|
|
|
if (states.get()) // If we don't set a new position, preserve current state
|
|
|
|
{
|
|
|
|
SetupStates = states; // Ownership transfer here
|
|
|
|
assert(!states.get());
|
|
|
|
}
|
2012-01-14 05:49:25 -07:00
|
|
|
|
2013-05-19 14:00:49 -06:00
|
|
|
for (MoveList<LEGAL> it(pos); *it; ++it)
|
2013-02-02 09:13:33 -07:00
|
|
|
if ( searchMoves.empty()
|
2013-05-19 05:28:25 -06:00
|
|
|
|| std::count(searchMoves.begin(), searchMoves.end(), *it))
|
|
|
|
RootMoves.push_back(RootMove(*it));
|
2011-11-05 00:53:19 -06:00
|
|
|
|
2013-01-16 01:28:41 -07:00
|
|
|
main_thread()->thinking = true;
|
2013-01-13 10:38:44 -07:00
|
|
|
main_thread()->notify_one(); // Starts main thread
|
2011-12-29 01:55:09 -07:00
|
|
|
}
|