Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
official-stockfish
GitHub Repository: official-stockfish/Stockfish
Path: blob/master/src/engine.h
630 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
#ifndef ENGINE_H_INCLUDED
20
#define ENGINE_H_INCLUDED
21
22
#include <cstddef>
23
#include <cstdint>
24
#include <functional>
25
#include <map>
26
#include <optional>
27
#include <string>
28
#include <string_view>
29
#include <utility>
30
#include <vector>
31
32
#include "history.h"
33
#include "nnue/network.h"
34
#include "numa.h"
35
#include "position.h"
36
#include "search.h"
37
#include "syzygy/tbprobe.h" // for Stockfish::Depth
38
#include "thread.h"
39
#include "tt.h"
40
#include "ucioption.h"
41
42
namespace Stockfish {
43
44
class Engine {
45
public:
46
using InfoShort = Search::InfoShort;
47
using InfoFull = Search::InfoFull;
48
using InfoIter = Search::InfoIteration;
49
50
Engine(std::optional<std::string> path = std::nullopt);
51
52
// Cannot be movable due to components holding backreferences to fields
53
Engine(const Engine&) = delete;
54
Engine(Engine&&) = delete;
55
Engine& operator=(const Engine&) = delete;
56
Engine& operator=(Engine&&) = delete;
57
58
~Engine() { wait_for_search_finished(); }
59
60
std::uint64_t perft(const std::string& fen, Depth depth, bool isChess960);
61
62
// non blocking call to start searching
63
void go(Search::LimitsType&);
64
// non blocking call to stop searching
65
void stop();
66
67
// blocking call to wait for search to finish
68
void wait_for_search_finished();
69
// set a new position, moves are in UCI format
70
void set_position(const std::string& fen, const std::vector<std::string>& moves);
71
72
// modifiers
73
74
void set_numa_config_from_option(const std::string& o);
75
void resize_threads();
76
void set_tt_size(size_t mb);
77
void set_ponderhit(bool);
78
void search_clear();
79
80
void set_on_update_no_moves(std::function<void(const InfoShort&)>&&);
81
void set_on_update_full(std::function<void(const InfoFull&)>&&);
82
void set_on_iter(std::function<void(const InfoIter&)>&&);
83
void set_on_bestmove(std::function<void(std::string_view, std::string_view)>&&);
84
void set_on_verify_networks(std::function<void(std::string_view)>&&);
85
86
// network related
87
88
void verify_networks() const;
89
void load_networks();
90
void load_big_network(const std::string& file);
91
void load_small_network(const std::string& file);
92
void save_network(const std::pair<std::optional<std::string>, std::string> files[2]);
93
94
// utility functions
95
96
void trace_eval() const;
97
98
const OptionsMap& get_options() const;
99
OptionsMap& get_options();
100
101
int get_hashfull(int maxAge = 0) const;
102
103
std::string fen() const;
104
void flip();
105
std::string visualize() const;
106
std::vector<std::pair<size_t, size_t>> get_bound_thread_count_by_numa_node() const;
107
std::string get_numa_config_as_string() const;
108
std::string numa_config_information_as_string() const;
109
std::string thread_allocation_information_as_string() const;
110
std::string thread_binding_information_as_string() const;
111
112
private:
113
const std::string binaryDirectory;
114
115
NumaReplicationContext numaContext;
116
117
Position pos;
118
StateListPtr states;
119
120
OptionsMap options;
121
ThreadPool threads;
122
TranspositionTable tt;
123
LazyNumaReplicatedSystemWide<Eval::NNUE::Networks> networks;
124
125
Search::SearchManager::UpdateContext updateContext;
126
std::function<void(std::string_view)> onVerifyNetworks;
127
std::map<NumaIndex, SharedHistories> sharedHists;
128
};
129
130
} // namespace Stockfish
131
132
133
#endif // #ifndef ENGINE_H_INCLUDED
134
135