Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
official-stockfish
GitHub Repository: official-stockfish/stockfish
Path: blob/master/src/engine.cpp
503 views
1
/*
2
Stockfish, a UCI chess playing engine derived from Glaurung 2.1
3
Copyright (C) 2004-2026 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 "engine.h"
20
21
#include <algorithm>
22
#include <cassert>
23
#include <deque>
24
#include <iosfwd>
25
#include <memory>
26
#include <ostream>
27
#include <sstream>
28
#include <string_view>
29
#include <utility>
30
#include <vector>
31
32
#include "evaluate.h"
33
#include "misc.h"
34
#include "nnue/network.h"
35
#include "nnue/nnue_common.h"
36
#include "nnue/nnue_misc.h"
37
#include "numa.h"
38
#include "perft.h"
39
#include "position.h"
40
#include "search.h"
41
#include "shm.h"
42
#include "syzygy/tbprobe.h"
43
#include "types.h"
44
#include "uci.h"
45
#include "ucioption.h"
46
47
namespace Stockfish {
48
49
namespace NN = Eval::NNUE;
50
51
constexpr auto StartFEN = "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1";
52
constexpr int MaxHashMB = Is64Bit ? 33554432 : 2048;
53
int MaxThreads = std::max(1024, 4 * int(get_hardware_concurrency()));
54
55
// The default configuration will attempt to group L3 domains up to 32 threads.
56
// This size was found to be a good balance between the Elo gain of increased
57
// history sharing and the speed loss from more cross-cache accesses (see
58
// PR#6526). The user can always explicitly override this behavior.
59
constexpr NumaAutoPolicy DefaultNumaPolicy = BundledL3Policy{32};
60
61
Engine::Engine(std::optional<std::string> path) :
62
binaryDirectory(path ? CommandLine::get_binary_directory(*path) : ""),
63
numaContext(NumaConfig::from_system(DefaultNumaPolicy)),
64
states(new std::deque<StateInfo>(1)),
65
threads(),
66
networks(numaContext,
67
// Heap-allocate because sizeof(NN::Networks) is large
68
std::make_unique<NN::Networks>(NN::EvalFile{EvalFileDefaultNameBig, "None", ""},
69
NN::EvalFile{EvalFileDefaultNameSmall, "None", ""})) {
70
71
pos.set(StartFEN, false, &states->back());
72
73
options.add( //
74
"Debug Log File", Option("", [](const Option& o) {
75
start_logger(o);
76
return std::nullopt;
77
}));
78
79
options.add( //
80
"NumaPolicy", Option("auto", [this](const Option& o) {
81
set_numa_config_from_option(o);
82
return numa_config_information_as_string() + "\n"
83
+ thread_allocation_information_as_string();
84
}));
85
86
options.add( //
87
"Threads", Option(1, 1, MaxThreads, [this](const Option&) {
88
resize_threads();
89
return thread_allocation_information_as_string();
90
}));
91
92
options.add( //
93
"Hash", Option(16, 1, MaxHashMB, [this](const Option& o) {
94
set_tt_size(o);
95
return std::nullopt;
96
}));
97
98
options.add( //
99
"Clear Hash", Option([this](const Option&) {
100
search_clear();
101
return std::nullopt;
102
}));
103
104
options.add( //
105
"Ponder", Option(false));
106
107
options.add( //
108
"MultiPV", Option(1, 1, MAX_MOVES));
109
110
options.add("Skill Level", Option(20, 0, 20));
111
112
options.add("Move Overhead", Option(10, 0, 5000));
113
114
options.add("nodestime", Option(0, 0, 10000));
115
116
options.add("UCI_Chess960", Option(false));
117
118
options.add("UCI_LimitStrength", Option(false));
119
120
options.add("UCI_Elo",
121
Option(Stockfish::Search::Skill::LowestElo, Stockfish::Search::Skill::LowestElo,
122
Stockfish::Search::Skill::HighestElo));
123
124
options.add("UCI_ShowWDL", Option(false));
125
126
options.add( //
127
"SyzygyPath", Option("", [](const Option& o) {
128
Tablebases::init(o);
129
return std::nullopt;
130
}));
131
132
options.add("SyzygyProbeDepth", Option(1, 1, 100));
133
134
options.add("Syzygy50MoveRule", Option(true));
135
136
options.add("SyzygyProbeLimit", Option(7, 0, 7));
137
138
options.add( //
139
"EvalFile", Option(EvalFileDefaultNameBig, [this](const Option& o) {
140
load_big_network(o);
141
return std::nullopt;
142
}));
143
144
options.add( //
145
"EvalFileSmall", Option(EvalFileDefaultNameSmall, [this](const Option& o) {
146
load_small_network(o);
147
return std::nullopt;
148
}));
149
150
load_networks();
151
resize_threads();
152
}
153
154
std::uint64_t Engine::perft(const std::string& fen, Depth depth, bool isChess960) {
155
verify_networks();
156
157
return Benchmark::perft(fen, depth, isChess960);
158
}
159
160
void Engine::go(Search::LimitsType& limits) {
161
assert(limits.perft == 0);
162
verify_networks();
163
164
threads.start_thinking(options, pos, states, limits);
165
}
166
void Engine::stop() { threads.stop = true; }
167
168
void Engine::search_clear() {
169
wait_for_search_finished();
170
171
tt.clear(threads);
172
threads.clear();
173
174
// @TODO wont work with multiple instances
175
Tablebases::init(options["SyzygyPath"]); // Free mapped files
176
}
177
178
void Engine::set_on_update_no_moves(std::function<void(const Engine::InfoShort&)>&& f) {
179
updateContext.onUpdateNoMoves = std::move(f);
180
}
181
182
void Engine::set_on_update_full(std::function<void(const Engine::InfoFull&)>&& f) {
183
updateContext.onUpdateFull = std::move(f);
184
}
185
186
void Engine::set_on_iter(std::function<void(const Engine::InfoIter&)>&& f) {
187
updateContext.onIter = std::move(f);
188
}
189
190
void Engine::set_on_bestmove(std::function<void(std::string_view, std::string_view)>&& f) {
191
updateContext.onBestmove = std::move(f);
192
}
193
194
void Engine::set_on_verify_networks(std::function<void(std::string_view)>&& f) {
195
onVerifyNetworks = std::move(f);
196
}
197
198
void Engine::wait_for_search_finished() { threads.main_thread()->wait_for_search_finished(); }
199
200
void Engine::set_position(const std::string& fen, const std::vector<std::string>& moves) {
201
// Drop the old state and create a new one
202
states = StateListPtr(new std::deque<StateInfo>(1));
203
pos.set(fen, options["UCI_Chess960"], &states->back());
204
205
for (const auto& move : moves)
206
{
207
auto m = UCIEngine::to_move(pos, move);
208
209
if (m == Move::none())
210
break;
211
212
states->emplace_back();
213
pos.do_move(m, states->back());
214
}
215
}
216
217
// modifiers
218
219
void Engine::set_numa_config_from_option(const std::string& o) {
220
if (o == "auto" || o == "system")
221
{
222
numaContext.set_numa_config(NumaConfig::from_system(DefaultNumaPolicy));
223
}
224
else if (o == "hardware")
225
{
226
// Don't respect affinity set in the system.
227
numaContext.set_numa_config(NumaConfig::from_system(DefaultNumaPolicy, false));
228
}
229
else if (o == "none")
230
{
231
numaContext.set_numa_config(NumaConfig{});
232
}
233
else
234
{
235
numaContext.set_numa_config(NumaConfig::from_string(o));
236
}
237
238
// Force reallocation of threads in case affinities need to change.
239
resize_threads();
240
threads.ensure_network_replicated();
241
}
242
243
void Engine::resize_threads() {
244
threads.wait_for_search_finished();
245
threads.set(numaContext.get_numa_config(), {options, threads, tt, sharedHists, networks},
246
updateContext);
247
248
// Reallocate the hash with the new threadpool size
249
set_tt_size(options["Hash"]);
250
threads.ensure_network_replicated();
251
}
252
253
void Engine::set_tt_size(size_t mb) {
254
wait_for_search_finished();
255
tt.resize(mb, threads);
256
}
257
258
void Engine::set_ponderhit(bool b) { threads.main_manager()->ponder = b; }
259
260
// network related
261
262
void Engine::verify_networks() const {
263
networks->big.verify(options["EvalFile"], onVerifyNetworks);
264
networks->small.verify(options["EvalFileSmall"], onVerifyNetworks);
265
266
auto statuses = networks.get_status_and_errors();
267
for (size_t i = 0; i < statuses.size(); ++i)
268
{
269
const auto [status, error] = statuses[i];
270
std::string message = "Network replica " + std::to_string(i + 1) + ": ";
271
if (status == SystemWideSharedConstantAllocationStatus::NoAllocation)
272
{
273
message += "No allocation.";
274
}
275
else if (status == SystemWideSharedConstantAllocationStatus::LocalMemory)
276
{
277
message += "Local memory.";
278
}
279
else if (status == SystemWideSharedConstantAllocationStatus::SharedMemory)
280
{
281
message += "Shared memory.";
282
}
283
else
284
{
285
message += "Unknown status.";
286
}
287
288
if (error.has_value())
289
{
290
message += " " + *error;
291
}
292
293
onVerifyNetworks(message);
294
}
295
}
296
297
void Engine::load_networks() {
298
networks.modify_and_replicate([this](NN::Networks& networks_) {
299
networks_.big.load(binaryDirectory, options["EvalFile"]);
300
networks_.small.load(binaryDirectory, options["EvalFileSmall"]);
301
});
302
threads.clear();
303
threads.ensure_network_replicated();
304
}
305
306
void Engine::load_big_network(const std::string& file) {
307
networks.modify_and_replicate(
308
[this, &file](NN::Networks& networks_) { networks_.big.load(binaryDirectory, file); });
309
threads.clear();
310
threads.ensure_network_replicated();
311
}
312
313
void Engine::load_small_network(const std::string& file) {
314
networks.modify_and_replicate(
315
[this, &file](NN::Networks& networks_) { networks_.small.load(binaryDirectory, file); });
316
threads.clear();
317
threads.ensure_network_replicated();
318
}
319
320
void Engine::save_network(const std::pair<std::optional<std::string>, std::string> files[2]) {
321
networks.modify_and_replicate([&files](NN::Networks& networks_) {
322
networks_.big.save(files[0].first);
323
networks_.small.save(files[1].first);
324
});
325
}
326
327
// utility functions
328
329
void Engine::trace_eval() const {
330
StateListPtr trace_states(new std::deque<StateInfo>(1));
331
Position p;
332
p.set(pos.fen(), options["UCI_Chess960"], &trace_states->back());
333
334
verify_networks();
335
336
sync_cout << "\n" << Eval::trace(p, *networks) << sync_endl;
337
}
338
339
const OptionsMap& Engine::get_options() const { return options; }
340
OptionsMap& Engine::get_options() { return options; }
341
342
std::string Engine::fen() const { return pos.fen(); }
343
344
void Engine::flip() { pos.flip(); }
345
346
std::string Engine::visualize() const {
347
std::stringstream ss;
348
ss << pos;
349
return ss.str();
350
}
351
352
int Engine::get_hashfull(int maxAge) const { return tt.hashfull(maxAge); }
353
354
std::vector<std::pair<size_t, size_t>> Engine::get_bound_thread_count_by_numa_node() const {
355
auto counts = threads.get_bound_thread_count_by_numa_node();
356
const NumaConfig& cfg = numaContext.get_numa_config();
357
std::vector<std::pair<size_t, size_t>> ratios;
358
NumaIndex n = 0;
359
for (; n < counts.size(); ++n)
360
ratios.emplace_back(counts[n], cfg.num_cpus_in_numa_node(n));
361
if (!counts.empty())
362
for (; n < cfg.num_numa_nodes(); ++n)
363
ratios.emplace_back(0, cfg.num_cpus_in_numa_node(n));
364
return ratios;
365
}
366
367
std::string Engine::get_numa_config_as_string() const {
368
return numaContext.get_numa_config().to_string();
369
}
370
371
std::string Engine::numa_config_information_as_string() const {
372
auto cfgStr = get_numa_config_as_string();
373
return "Available processors: " + cfgStr;
374
}
375
376
std::string Engine::thread_binding_information_as_string() const {
377
auto boundThreadsByNode = get_bound_thread_count_by_numa_node();
378
std::stringstream ss;
379
if (boundThreadsByNode.empty())
380
return ss.str();
381
382
bool isFirst = true;
383
384
for (auto&& [current, total] : boundThreadsByNode)
385
{
386
if (!isFirst)
387
ss << ":";
388
ss << current << "/" << total;
389
isFirst = false;
390
}
391
392
return ss.str();
393
}
394
395
std::string Engine::thread_allocation_information_as_string() const {
396
std::stringstream ss;
397
398
size_t threadsSize = threads.size();
399
ss << "Using " << threadsSize << (threadsSize > 1 ? " threads" : " thread");
400
401
auto boundThreadsByNodeStr = thread_binding_information_as_string();
402
if (boundThreadsByNodeStr.empty())
403
return ss.str();
404
405
ss << " with NUMA node thread binding: ";
406
ss << boundThreadsByNodeStr;
407
408
return ss.str();
409
}
410
}
411
412