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