]> git.sesse.net Git - stockfish/blob - src/thread.cpp
Decrease reduction for exact PV nodes
[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-2017 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 "syzygy/tbprobe.h"
28
29 ThreadPool Threads; // Global object
30
31
32 /// Thread constructor launches the thread and waits until it goes to sleep
33 /// in idle_loop(). Note that 'searching' and 'exit' should be alredy set.
34
35 Thread::Thread(size_t n) : idx(n), stdThread(&Thread::idle_loop, this) {
36
37   wait_for_search_finished();
38   clear(); // Zero-init histories (based on std::array)
39 }
40
41
42 /// Thread destructor wakes up the thread in idle_loop() and waits
43 /// for its termination. Thread should be already waiting.
44
45 Thread::~Thread() {
46
47   assert(!searching);
48
49   exit = true;
50   start_searching();
51   stdThread.join();
52 }
53
54
55 /// Thread::clear() reset histories, usually before a new game
56
57 void Thread::clear() {
58
59   counterMoves.fill(MOVE_NONE);
60   mainHistory.fill(0);
61
62   for (auto& to : contHistory)
63       for (auto& h : to)
64           h.fill(0);
65
66   contHistory[NO_PIECE][0].fill(Search::CounterMovePruneThreshold - 1);
67 }
68
69 /// Thread::start_searching() wakes up the thread that will start the search
70
71 void Thread::start_searching() {
72
73   std::lock_guard<Mutex> lk(mutex);
74   searching = true;
75   cv.notify_one(); // Wake up the thread in idle_loop()
76 }
77
78
79 /// Thread::wait_for_search_finished() blocks on the condition variable
80 /// until the thread has finished searching.
81
82 void Thread::wait_for_search_finished() {
83
84   std::unique_lock<Mutex> lk(mutex);
85   cv.wait(lk, [&]{ return !searching; });
86 }
87
88
89 /// Thread::idle_loop() is where the thread is parked, blocked on the
90 /// condition variable, when it has no work to do.
91
92 void Thread::idle_loop() {
93
94   WinProcGroup::bindThisThread(idx);
95
96   while (true)
97   {
98       std::unique_lock<Mutex> lk(mutex);
99       searching = false;
100       cv.notify_one(); // Wake up anyone waiting for search finished
101       cv.wait(lk, [&]{ return searching; });
102
103       if (exit)
104           return;
105
106       lk.unlock();
107
108       search();
109   }
110 }
111
112
113 /// ThreadPool::init() creates and launches the threads that will go
114 /// immediately to sleep in idle_loop. We cannot use the constructor because
115 /// Threads is a static object and we need a fully initialized engine at
116 /// this point due to allocation of Endgames in the Thread constructor.
117
118 void ThreadPool::init(size_t requested) {
119
120   push_back(new MainThread(0));
121   set(requested);
122 }
123
124
125 /// ThreadPool::exit() terminates threads before the program exits. Cannot be
126 /// done in the destructor because threads must be terminated before deleting
127 /// any static object, so before main() returns.
128
129 void ThreadPool::exit() {
130
131   main()->wait_for_search_finished();
132   set(0);
133 }
134
135
136 /// ThreadPool::set() creates/destroys threads to match the requested number
137
138 void ThreadPool::set(size_t requested) {
139
140   while (size() < requested)
141       push_back(new Thread(size()));
142
143   while (size() > requested)
144       delete back(), pop_back();
145 }
146
147
148 /// ThreadPool::start_thinking() wakes up main thread waiting in idle_loop() and
149 /// returns immediately. Main thread will wake up other threads and start the search.
150
151 void ThreadPool::start_thinking(Position& pos, StateListPtr& states,
152                                 const Search::LimitsType& limits, bool ponderMode) {
153
154   main()->wait_for_search_finished();
155
156   stopOnPonderhit = stop = false;
157   ponder = ponderMode;
158   Search::Limits = limits;
159   Search::RootMoves rootMoves;
160
161   for (const auto& m : MoveList<LEGAL>(pos))
162       if (   limits.searchmoves.empty()
163           || std::count(limits.searchmoves.begin(), limits.searchmoves.end(), m))
164           rootMoves.emplace_back(m);
165
166   if (!rootMoves.empty())
167       Tablebases::filter_root_moves(pos, rootMoves);
168
169   // After ownership transfer 'states' becomes empty, so if we stop the search
170   // and call 'go' again without setting a new position states.get() == NULL.
171   assert(states.get() || setupStates.get());
172
173   if (states.get())
174       setupStates = std::move(states); // Ownership transfer, states is now empty
175
176   // We use Position::set() to set root position across threads. But there are
177   // some StateInfo fields (previous, pliesFromNull, capturedPiece) that cannot
178   // be deduced from a fen string, so set() clears them and to not lose the info
179   // we need to backup and later restore setupStates->back(). Note that setupStates
180   // is shared by threads but is accessed in read-only mode.
181   StateInfo tmp = setupStates->back();
182
183   for (Thread* th : Threads)
184   {
185       th->nodes = th->tbHits = 0;
186       th->rootDepth = th->completedDepth = DEPTH_ZERO;
187       th->rootMoves = rootMoves;
188       th->rootPos.set(pos.fen(), pos.is_chess960(), &setupStates->back(), th);
189   }
190
191   setupStates->back() = tmp;
192
193   main()->start_searching();
194 }