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