Rev 96 | Rev 169 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 96 | Rev 154 | ||
---|---|---|---|
Line 23... | Line 23... | ||
23 | 23 | ||
24 | #include "movegen.h" |
24 | #include "movegen.h" |
25 | #include "search.h" |
25 | #include "search.h" |
26 | #include "thread.h" |
26 | #include "thread.h" |
27 | #include "uci.h" |
27 | #include "uci.h" |
28 | - | ||
29 |
|
28 | #include "syzygy/tbprobe.h" |
30 | 29 | ||
31 | ThreadPool Threads; // Global object |
30 | ThreadPool Threads; // Global object |
32 | 31 | ||
33 | /// Thread constructor launches the thread and then waits until it goes to sleep |
32 | /// Thread constructor launches the thread and then waits until it goes to sleep |
34 | /// in idle_loop(). |
33 | /// in idle_loop(). |
35 | 34 | ||
36 | Thread::Thread() { |
35 | Thread::Thread() { |
37 | 36 | ||
38 | resetCalls = exit = false; |
37 | resetCalls = exit = false; |
39 | maxPly = callsCnt = 0; |
38 | maxPly = callsCnt = 0; |
- | 39 | tbHits = 0; |
|
40 | history.clear(); |
40 | history.clear(); |
41 | counterMoves.clear(); |
41 | counterMoves.clear(); |
42 | idx = Threads.size(); // Start from 0 |
42 | idx = Threads.size(); // Start from 0 |
43 | 43 | ||
44 | std::unique_lock<Mutex> lk(mutex); |
44 | std::unique_lock<Mutex> lk(mutex); |
Line 157... | Line 157... | ||
157 | } |
157 | } |
158 | 158 | ||
159 | 159 | ||
160 | /// ThreadPool::nodes_searched() returns the number of nodes searched |
160 | /// ThreadPool::nodes_searched() returns the number of nodes searched |
161 | 161 | ||
162 |
|
162 | uint64_t ThreadPool::nodes_searched() const { |
163 | 163 | ||
164 |
|
164 | uint64_t nodes = 0; |
165 | for (Thread* th : *this) |
165 | for (Thread* th : *this) |
166 | nodes += th->rootPos.nodes_searched(); |
166 | nodes += th->rootPos.nodes_searched(); |
167 | return nodes; |
167 | return nodes; |
- | 168 | } |
|
- | 169 | ||
- | 170 | ||
- | 171 | /// ThreadPool::tb_hits() returns the number of TB hits |
|
- | 172 | ||
- | 173 | uint64_t ThreadPool::tb_hits() const { |
|
- | 174 | ||
- | 175 | uint64_t hits = 0; |
|
- | 176 | for (Thread* th : *this) |
|
- | 177 | hits += th->tbHits; |
|
- | 178 | return hits; |
|
168 | } |
179 | } |
169 | 180 | ||
170 | 181 | ||
171 | /// ThreadPool::start_thinking() wakes up the main thread sleeping in idle_loop() |
182 | /// ThreadPool::start_thinking() wakes up the main thread sleeping in idle_loop() |
172 | /// and starts a new search, then returns immediately. |
183 | /// and starts a new search, then returns immediately. |
173 | 184 | ||
174 | void ThreadPool::start_thinking( |
185 | void ThreadPool::start_thinking(Position& pos, StateListPtr& states, |
175 |
|
186 | const Search::LimitsType& limits) { |
176 | 187 | ||
177 | main()->wait_for_search_finished(); |
188 | main()->wait_for_search_finished(); |
178 | 189 | ||
179 | Signals.stopOnPonderhit = Signals.stop = false; |
190 | Search::Signals.stopOnPonderhit = Search::Signals.stop = false; |
180 | - | ||
181 | main()->rootMoves.clear(); |
- | |
182 | main()->rootPos = pos; |
- | |
183 | Limits = limits; |
191 | Search::Limits = limits; |
184 | if (states.get()) // If we don't set a new position, preserve current state |
- | |
185 | { |
- | |
186 | SetupStates = std::move(states); // Ownership transfer here |
- | |
187 |
|
192 | Search::RootMoves rootMoves; |
188 | } |
- | |
189 | 193 | ||
190 | for (const auto& m : MoveList<LEGAL>(pos)) |
194 | for (const auto& m : MoveList<LEGAL>(pos)) |
191 | if ( limits.searchmoves.empty() |
195 | if ( limits.searchmoves.empty() |
192 | || std::count(limits.searchmoves.begin(), limits.searchmoves.end(), m)) |
196 | || std::count(limits.searchmoves.begin(), limits.searchmoves.end(), m)) |
193 |
|
197 | rootMoves.push_back(Search::RootMove(m)); |
- | 198 | ||
- | 199 | if (!rootMoves.empty()) |
|
- | 200 | Tablebases::filter_root_moves(pos, rootMoves); |
|
- | 201 | ||
- | 202 | // After ownership transfer 'states' becomes empty, so if we stop the search |
|
- | 203 | // and call 'go' again without setting a new position states.get() == NULL. |
|
- | 204 | assert(states.get() || setupStates.get()); |
|
- | 205 | ||
- | 206 | if (states.get()) |
|
- | 207 | setupStates = std::move(states); // Ownership transfer, states is now empty |
|
- | 208 | ||
- | 209 | StateInfo tmp = setupStates->back(); |
|
- | 210 | ||
- | 211 | for (Thread* th : Threads) |
|
- | 212 | { |
|
- | 213 | th->maxPly = 0; |
|
- | 214 | th->tbHits = 0; |
|
- | 215 | th->rootDepth = DEPTH_ZERO; |
|
- | 216 | th->rootMoves = rootMoves; |
|
- | 217 | th->rootPos.set(pos.fen(), pos.is_chess960(), &setupStates->back(), th); |
|
- | 218 | } |
|
- | 219 | ||
- | 220 | setupStates->back() = tmp; // Restore st->previous, cleared by Position::set() |
|
194 | 221 | ||
195 | main()->start_searching(); |
222 | main()->start_searching(); |
196 | } |
223 | } |