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