Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
official-stockfish
GitHub Repository: official-stockfish/Stockfish
Path: blob/master/src/thread.cpp
376 views
1
/*
2
Stockfish, a UCI chess playing engine derived from Glaurung 2.1
3
Copyright (C) 2004-2025 The Stockfish developers (see AUTHORS file)
4
5
Stockfish is free software: you can redistribute it and/or modify
6
it under the terms of the GNU General Public License as published by
7
the Free Software Foundation, either version 3 of the License, or
8
(at your option) any later version.
9
10
Stockfish is distributed in the hope that it will be useful,
11
but WITHOUT ANY WARRANTY; without even the implied warranty of
12
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
GNU General Public License for more details.
14
15
You should have received a copy of the GNU General Public License
16
along with this program. If not, see <http://www.gnu.org/licenses/>.
17
*/
18
19
#include "thread.h"
20
21
#include <algorithm>
22
#include <cassert>
23
#include <deque>
24
#include <memory>
25
#include <string>
26
#include <unordered_map>
27
#include <utility>
28
29
#include "movegen.h"
30
#include "search.h"
31
#include "syzygy/tbprobe.h"
32
#include "timeman.h"
33
#include "types.h"
34
#include "uci.h"
35
#include "ucioption.h"
36
37
namespace Stockfish {
38
39
// Constructor launches the thread and waits until it goes to sleep
40
// in idle_loop(). Note that 'searching' and 'exit' should be already set.
41
Thread::Thread(Search::SharedState& sharedState,
42
std::unique_ptr<Search::ISearchManager> sm,
43
size_t n,
44
OptionalThreadToNumaNodeBinder binder) :
45
idx(n),
46
nthreads(sharedState.options["Threads"]),
47
stdThread(&Thread::idle_loop, this) {
48
49
wait_for_search_finished();
50
51
run_custom_job([this, &binder, &sharedState, &sm, n]() {
52
// Use the binder to [maybe] bind the threads to a NUMA node before doing
53
// the Worker allocation. Ideally we would also allocate the SearchManager
54
// here, but that's minor.
55
this->numaAccessToken = binder();
56
this->worker =
57
std::make_unique<Search::Worker>(sharedState, std::move(sm), n, this->numaAccessToken);
58
});
59
60
wait_for_search_finished();
61
}
62
63
64
// Destructor wakes up the thread in idle_loop() and waits
65
// for its termination. Thread should be already waiting.
66
Thread::~Thread() {
67
68
assert(!searching);
69
70
exit = true;
71
start_searching();
72
stdThread.join();
73
}
74
75
// Wakes up the thread that will start the search
76
void Thread::start_searching() {
77
assert(worker != nullptr);
78
run_custom_job([this]() { worker->start_searching(); });
79
}
80
81
// Clears the histories for the thread worker (usually before a new game)
82
void Thread::clear_worker() {
83
assert(worker != nullptr);
84
run_custom_job([this]() { worker->clear(); });
85
}
86
87
// Blocks on the condition variable until the thread has finished searching
88
void Thread::wait_for_search_finished() {
89
90
std::unique_lock<std::mutex> lk(mutex);
91
cv.wait(lk, [&] { return !searching; });
92
}
93
94
// Launching a function in the thread
95
void Thread::run_custom_job(std::function<void()> f) {
96
{
97
std::unique_lock<std::mutex> lk(mutex);
98
cv.wait(lk, [&] { return !searching; });
99
jobFunc = std::move(f);
100
searching = true;
101
}
102
cv.notify_one();
103
}
104
105
void Thread::ensure_network_replicated() { worker->ensure_network_replicated(); }
106
107
// Thread gets parked here, blocked on the condition variable
108
// when the thread has no work to do.
109
110
void Thread::idle_loop() {
111
while (true)
112
{
113
std::unique_lock<std::mutex> lk(mutex);
114
searching = false;
115
cv.notify_one(); // Wake up anyone waiting for search finished
116
cv.wait(lk, [&] { return searching; });
117
118
if (exit)
119
return;
120
121
std::function<void()> job = std::move(jobFunc);
122
jobFunc = nullptr;
123
124
lk.unlock();
125
126
if (job)
127
job();
128
}
129
}
130
131
Search::SearchManager* ThreadPool::main_manager() { return main_thread()->worker->main_manager(); }
132
133
uint64_t ThreadPool::nodes_searched() const { return accumulate(&Search::Worker::nodes); }
134
uint64_t ThreadPool::tb_hits() const { return accumulate(&Search::Worker::tbHits); }
135
136
// Creates/destroys threads to match the requested number.
137
// Created and launched threads will immediately go to sleep in idle_loop.
138
// Upon resizing, threads are recreated to allow for binding if necessary.
139
void ThreadPool::set(const NumaConfig& numaConfig,
140
Search::SharedState sharedState,
141
const Search::SearchManager::UpdateContext& updateContext) {
142
143
if (threads.size() > 0) // destroy any existing thread(s)
144
{
145
main_thread()->wait_for_search_finished();
146
147
threads.clear();
148
149
boundThreadToNumaNode.clear();
150
}
151
152
const size_t requested = sharedState.options["Threads"];
153
154
if (requested > 0) // create new thread(s)
155
{
156
// Binding threads may be problematic when there's multiple NUMA nodes and
157
// multiple Stockfish instances running. In particular, if each instance
158
// runs a single thread then they would all be mapped to the first NUMA node.
159
// This is undesirable, and so the default behaviour (i.e. when the user does not
160
// change the NumaConfig UCI setting) is to not bind the threads to processors
161
// unless we know for sure that we span NUMA nodes and replication is required.
162
const std::string numaPolicy(sharedState.options["NumaPolicy"]);
163
const bool doBindThreads = [&]() {
164
if (numaPolicy == "none")
165
return false;
166
167
if (numaPolicy == "auto")
168
return numaConfig.suggests_binding_threads(requested);
169
170
// numaPolicy == "system", or explicitly set by the user
171
return true;
172
}();
173
174
boundThreadToNumaNode = doBindThreads
175
? numaConfig.distribute_threads_among_numa_nodes(requested)
176
: std::vector<NumaIndex>{};
177
178
while (threads.size() < requested)
179
{
180
const size_t threadId = threads.size();
181
const NumaIndex numaId = doBindThreads ? boundThreadToNumaNode[threadId] : 0;
182
auto manager = threadId == 0 ? std::unique_ptr<Search::ISearchManager>(
183
std::make_unique<Search::SearchManager>(updateContext))
184
: std::make_unique<Search::NullSearchManager>();
185
186
// When not binding threads we want to force all access to happen
187
// from the same NUMA node, because in case of NUMA replicated memory
188
// accesses we don't want to trash cache in case the threads get scheduled
189
// on the same NUMA node.
190
auto binder = doBindThreads ? OptionalThreadToNumaNodeBinder(numaConfig, numaId)
191
: OptionalThreadToNumaNodeBinder(numaId);
192
193
threads.emplace_back(
194
std::make_unique<Thread>(sharedState, std::move(manager), threadId, binder));
195
}
196
197
clear();
198
199
main_thread()->wait_for_search_finished();
200
}
201
}
202
203
204
// Sets threadPool data to initial values
205
void ThreadPool::clear() {
206
if (threads.size() == 0)
207
return;
208
209
for (auto&& th : threads)
210
th->clear_worker();
211
212
for (auto&& th : threads)
213
th->wait_for_search_finished();
214
215
// These two affect the time taken on the first move of a game:
216
main_manager()->bestPreviousAverageScore = VALUE_INFINITE;
217
main_manager()->previousTimeReduction = 0.85;
218
219
main_manager()->callsCnt = 0;
220
main_manager()->bestPreviousScore = VALUE_INFINITE;
221
main_manager()->originalTimeAdjust = -1;
222
main_manager()->tm.clear();
223
}
224
225
void ThreadPool::run_on_thread(size_t threadId, std::function<void()> f) {
226
assert(threads.size() > threadId);
227
threads[threadId]->run_custom_job(std::move(f));
228
}
229
230
void ThreadPool::wait_on_thread(size_t threadId) {
231
assert(threads.size() > threadId);
232
threads[threadId]->wait_for_search_finished();
233
}
234
235
size_t ThreadPool::num_threads() const { return threads.size(); }
236
237
238
// Wakes up main thread waiting in idle_loop() and returns immediately.
239
// Main thread will wake up other threads and start the search.
240
void ThreadPool::start_thinking(const OptionsMap& options,
241
Position& pos,
242
StateListPtr& states,
243
Search::LimitsType limits) {
244
245
main_thread()->wait_for_search_finished();
246
247
main_manager()->stopOnPonderhit = stop = abortedSearch = false;
248
main_manager()->ponder = limits.ponderMode;
249
250
increaseDepth = true;
251
252
Search::RootMoves rootMoves;
253
const auto legalmoves = MoveList<LEGAL>(pos);
254
255
for (const auto& uciMove : limits.searchmoves)
256
{
257
auto move = UCIEngine::to_move(pos, uciMove);
258
259
if (std::find(legalmoves.begin(), legalmoves.end(), move) != legalmoves.end())
260
rootMoves.emplace_back(move);
261
}
262
263
if (rootMoves.empty())
264
for (const auto& m : legalmoves)
265
rootMoves.emplace_back(m);
266
267
Tablebases::Config tbConfig = Tablebases::rank_root_moves(options, pos, rootMoves);
268
269
// After ownership transfer 'states' becomes empty, so if we stop the search
270
// and call 'go' again without setting a new position states.get() == nullptr.
271
assert(states.get() || setupStates.get());
272
273
if (states.get())
274
setupStates = std::move(states); // Ownership transfer, states is now empty
275
276
// We use Position::set() to set root position across threads. But there are
277
// some StateInfo fields (previous, pliesFromNull, capturedPiece) that cannot
278
// be deduced from a fen string, so set() clears them and they are set from
279
// setupStates->back() later. The rootState is per thread, earlier states are
280
// shared since they are read-only.
281
for (auto&& th : threads)
282
{
283
th->run_custom_job([&]() {
284
th->worker->limits = limits;
285
th->worker->nodes = th->worker->tbHits = th->worker->nmpMinPly =
286
th->worker->bestMoveChanges = 0;
287
th->worker->rootDepth = th->worker->completedDepth = 0;
288
th->worker->rootMoves = rootMoves;
289
th->worker->rootPos.set(pos.fen(), pos.is_chess960(), &th->worker->rootState);
290
th->worker->rootState = setupStates->back();
291
th->worker->tbConfig = tbConfig;
292
});
293
}
294
295
for (auto&& th : threads)
296
th->wait_for_search_finished();
297
298
main_thread()->start_searching();
299
}
300
301
Thread* ThreadPool::get_best_thread() const {
302
303
Thread* bestThread = threads.front().get();
304
Value minScore = VALUE_NONE;
305
306
std::unordered_map<Move, int64_t, Move::MoveHash> votes(
307
2 * std::min(size(), bestThread->worker->rootMoves.size()));
308
309
// Find the minimum score of all threads
310
for (auto&& th : threads)
311
minScore = std::min(minScore, th->worker->rootMoves[0].score);
312
313
// Vote according to score and depth, and select the best thread
314
auto thread_voting_value = [minScore](Thread* th) {
315
return (th->worker->rootMoves[0].score - minScore + 14) * int(th->worker->completedDepth);
316
};
317
318
for (auto&& th : threads)
319
votes[th->worker->rootMoves[0].pv[0]] += thread_voting_value(th.get());
320
321
for (auto&& th : threads)
322
{
323
const auto bestThreadScore = bestThread->worker->rootMoves[0].score;
324
const auto newThreadScore = th->worker->rootMoves[0].score;
325
326
const auto& bestThreadPV = bestThread->worker->rootMoves[0].pv;
327
const auto& newThreadPV = th->worker->rootMoves[0].pv;
328
329
const auto bestThreadMoveVote = votes[bestThreadPV[0]];
330
const auto newThreadMoveVote = votes[newThreadPV[0]];
331
332
const bool bestThreadInProvenWin = is_win(bestThreadScore);
333
const bool newThreadInProvenWin = is_win(newThreadScore);
334
335
const bool bestThreadInProvenLoss =
336
bestThreadScore != -VALUE_INFINITE && is_loss(bestThreadScore);
337
const bool newThreadInProvenLoss =
338
newThreadScore != -VALUE_INFINITE && is_loss(newThreadScore);
339
340
// We make sure not to pick a thread with truncated principal variation
341
const bool betterVotingValue =
342
thread_voting_value(th.get()) * int(newThreadPV.size() > 2)
343
> thread_voting_value(bestThread) * int(bestThreadPV.size() > 2);
344
345
if (bestThreadInProvenWin)
346
{
347
// Make sure we pick the shortest mate / TB conversion
348
if (newThreadScore > bestThreadScore)
349
bestThread = th.get();
350
}
351
else if (bestThreadInProvenLoss)
352
{
353
// Make sure we pick the shortest mated / TB conversion
354
if (newThreadInProvenLoss && newThreadScore < bestThreadScore)
355
bestThread = th.get();
356
}
357
else if (newThreadInProvenWin || newThreadInProvenLoss
358
|| (!is_loss(newThreadScore)
359
&& (newThreadMoveVote > bestThreadMoveVote
360
|| (newThreadMoveVote == bestThreadMoveVote && betterVotingValue))))
361
bestThread = th.get();
362
}
363
364
return bestThread;
365
}
366
367
368
// Start non-main threads.
369
// Will be invoked by main thread after it has started searching.
370
void ThreadPool::start_searching() {
371
372
for (auto&& th : threads)
373
if (th != threads.front())
374
th->start_searching();
375
}
376
377
378
// Wait for non-main threads
379
void ThreadPool::wait_for_search_finished() const {
380
381
for (auto&& th : threads)
382
if (th != threads.front())
383
th->wait_for_search_finished();
384
}
385
386
std::vector<size_t> ThreadPool::get_bound_thread_count_by_numa_node() const {
387
std::vector<size_t> counts;
388
389
if (!boundThreadToNumaNode.empty())
390
{
391
NumaIndex highestNumaNode = 0;
392
for (NumaIndex n : boundThreadToNumaNode)
393
if (n > highestNumaNode)
394
highestNumaNode = n;
395
396
counts.resize(highestNumaNode + 1, 0);
397
398
for (NumaIndex n : boundThreadToNumaNode)
399
counts[n] += 1;
400
}
401
402
return counts;
403
}
404
405
void ThreadPool::ensure_network_replicated() {
406
for (auto&& th : threads)
407
th->ensure_network_replicated();
408
}
409
410
} // namespace Stockfish
411
412