]> git.sesse.net Git - stockfish/blob - src/thread.cpp
LMR Simplification
[stockfish] / src / thread.cpp
1 /*
2   Stockfish, a UCI chess playing engine derived from Glaurung 2.1
3   Copyright (C) 2004-2008 Tord Romstad (Glaurung author)
4   Copyright (C) 2008-2015 Marco Costalba, Joona Kiiski, Tord Romstad
5   Copyright (C) 2015-2016 Marco Costalba, Joona Kiiski, Gary Linscott, Tord Romstad
6
7   Stockfish is free software: you can redistribute it and/or modify
8   it under the terms of the GNU General Public License as published by
9   the Free Software Foundation, either version 3 of the License, or
10   (at your option) any later version.
11
12   Stockfish is distributed in the hope that it will be useful,
13   but WITHOUT ANY WARRANTY; without even the implied warranty of
14   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15   GNU General Public License for more details.
16
17   You should have received a copy of the GNU General Public License
18   along with this program.  If not, see <http://www.gnu.org/licenses/>.
19 */
20
21 #include <algorithm> // For std::count
22 #include <cassert>
23
24 #include "movegen.h"
25 #include "search.h"
26 #include "thread.h"
27 #include "uci.h"
28
29 ThreadPool Threads; // Global object
30
31 /// Thread constructor launches the thread and then waits until it goes to sleep
32 /// in idle_loop().
33
34 Thread::Thread() {
35
36   resetCalls = exit = false;
37   maxPly = callsCnt = 0;
38   history.clear();
39   counterMoves.clear();
40   idx = Threads.size(); // Start from 0
41
42   std::unique_lock<Mutex> lk(mutex);
43   searching = true;
44   nativeThread = std::thread(&Thread::idle_loop, this);
45   sleepCondition.wait(lk, [&]{ return !searching; });
46 }
47
48
49 /// Thread destructor waits for thread termination before returning
50
51 Thread::~Thread() {
52
53   mutex.lock();
54   exit = true;
55   sleepCondition.notify_one();
56   mutex.unlock();
57   nativeThread.join();
58 }
59
60
61 /// Thread::wait_for_search_finished() waits on sleep condition
62 /// until not searching
63
64 void Thread::wait_for_search_finished() {
65
66   std::unique_lock<Mutex> lk(mutex);
67   sleepCondition.wait(lk, [&]{ return !searching; });
68 }
69
70
71 /// Thread::wait() waits on sleep condition until condition is true
72
73 void Thread::wait(std::atomic_bool& condition) {
74
75   std::unique_lock<Mutex> lk(mutex);
76   sleepCondition.wait(lk, [&]{ return bool(condition); });
77 }
78
79
80 /// Thread::start_searching() wakes up the thread that will start the search
81
82 void Thread::start_searching(bool resume) {
83
84   std::unique_lock<Mutex> lk(mutex);
85
86   if (!resume)
87       searching = true;
88
89   sleepCondition.notify_one();
90 }
91
92
93 /// Thread::idle_loop() is where the thread is parked when it has no work to do
94
95 void Thread::idle_loop() {
96
97   while (!exit)
98   {
99       std::unique_lock<Mutex> lk(mutex);
100
101       searching = false;
102
103       while (!searching && !exit)
104       {
105           sleepCondition.notify_one(); // Wake up any waiting thread
106           sleepCondition.wait(lk);
107       }
108
109       lk.unlock();
110
111       if (!exit)
112           search();
113   }
114 }
115
116
117 /// ThreadPool::init() creates and launches requested threads that will go
118 /// immediately to sleep. We cannot use a constructor because Threads is a
119 /// static object and we need a fully initialized engine at this point due to
120 /// allocation of Endgames in the Thread constructor.
121
122 void ThreadPool::init() {
123
124   push_back(new MainThread);
125   read_uci_options();
126 }
127
128
129 /// ThreadPool::exit() terminates threads before the program exits. Cannot be
130 /// done in destructor because threads must be terminated before deleting any
131 /// static objects while still in main().
132
133 void ThreadPool::exit() {
134
135   while (size())
136       delete back(), pop_back();
137 }
138
139
140 /// ThreadPool::read_uci_options() updates internal threads parameters from the
141 /// corresponding UCI options and creates/destroys threads to match requested
142 /// number. Thread objects are dynamically allocated.
143
144 void ThreadPool::read_uci_options() {
145
146   size_t requested = Options["Threads"];
147
148   assert(requested > 0);
149
150   while (size() < requested)
151       push_back(new Thread);
152
153   while (size() > requested)
154       delete back(), pop_back();
155 }
156
157
158 /// ThreadPool::nodes_searched() returns the number of nodes searched
159
160 int64_t ThreadPool::nodes_searched() {
161
162   int64_t nodes = 0;
163   for (Thread* th : *this)
164       nodes += th->rootPos.nodes_searched();
165   return nodes;
166 }
167
168
169 /// ThreadPool::start_thinking() wakes up the main thread sleeping in idle_loop()
170 /// and starts a new search, then returns immediately.
171
172 void ThreadPool::start_thinking(const Position& pos, StateListPtr& states,
173                                 const Search::LimitsType& limits) {
174
175   main()->wait_for_search_finished();
176
177   Search::Signals.stopOnPonderhit = Search::Signals.stop = false;
178   Search::Limits = limits;
179   Search::RootMoves rootMoves;
180
181   for (const auto& m : MoveList<LEGAL>(pos))
182       if (   limits.searchmoves.empty()
183           || std::count(limits.searchmoves.begin(), limits.searchmoves.end(), m))
184           rootMoves.push_back(Search::RootMove(m));
185
186   // After ownership transfer 'states' becomes empty, so if we stop the search
187   // and call 'go' again without setting a new position states.get() == NULL.
188   assert(states.get() || setupStates.get());
189
190   if (states.get())
191       setupStates = std::move(states); // Ownership transfer, states is now empty
192
193   StateInfo tmp = setupStates->back();
194
195   for (Thread* th : Threads)
196   {
197       th->maxPly = 0;
198       th->rootDepth = DEPTH_ZERO;
199       th->rootMoves = rootMoves;
200       th->rootPos.set(pos.fen(), pos.is_chess960(), &setupStates->back(), th);
201   }
202
203   setupStates->back() = tmp; // Restore st->previous, cleared by Position::set()
204
205   main()->start_searching();
206 }