BenBot 1.7.5
A chess engine
Loading...
Searching...
No Matches
Context.hpp
Go to the documentation of this file.
1/*
2 * ======================================================================================
3 *
4 * ░▒▓███████▓▒░░▒▓████████▓▒░▒▓███████▓▒░ ░▒▓███████▓▒░ ░▒▓██████▓▒░▒▓████████▓▒░
5 * ░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░ ░▒▓█▓▒░░▒▓█▓▒░ ░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░░▒▓█▓▒░ ░▒▓█▓▒░
6 * ░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░ ░▒▓█▓▒░░▒▓█▓▒░ ░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░░▒▓█▓▒░ ░▒▓█▓▒░
7 * ░▒▓███████▓▒░░▒▓██████▓▒░ ░▒▓█▓▒░░▒▓█▓▒░ ░▒▓███████▓▒░░▒▓█▓▒░░▒▓█▓▒░ ░▒▓█▓▒░
8 * ░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░ ░▒▓█▓▒░░▒▓█▓▒░ ░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░░▒▓█▓▒░ ░▒▓█▓▒░
9 * ░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░ ░▒▓█▓▒░░▒▓█▓▒░ ░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░░▒▓█▓▒░ ░▒▓█▓▒░
10 * ░▒▓███████▓▒░░▒▓████████▓▒░▒▓█▓▒░░▒▓█▓▒░ ░▒▓███████▓▒░ ░▒▓██████▓▒░ ░▒▓█▓▒░
11 *
12 * ======================================================================================
13 */
14
19
20#pragma once
21
22#include <atomic>
31#include <libutil/Memory.hpp>
32#include <memory>
33#include <optional>
34#include <string_view>
35#include <utility>
36
37namespace ben_bot::search {
38
39using chess::game::Position;
40
47struct Context final {
49 Context() = default;
50
52 explicit Context(Callbacks&& callbacksToUse)
53 : callbacks { std::move(callbacksToUse) }
54 {
55 }
56
57 Context(const Context&) = delete;
58 Context& operator=(const Context&) = delete;
59
60 Context(Context&&) = delete;
62
63 ~Context() = default;
64
73 void search();
74
81 void abort() noexcept { exitFlag.store(true, std::memory_order::release); }
82
88 void reset_abort() noexcept { exitFlag.store(false, std::memory_order::release); }
89
94 {
95 abort();
96 wait();
97 transTable.clear();
98 pawnTable.clear();
99 }
100
108 {
109 abort();
110 wait();
111 killerMoves.clear();
112 history->clear();
113 continuationHistory->clear();
114 }
115
117 [[nodiscard]] auto get_history_table() const noexcept -> const HistoryTable& { return *history; }
118
120 [[nodiscard]] auto get_continuation_history() const noexcept -> const ContinuationHistory& { return *continuationHistory; }
121
125 void resize_transposition_table(const size_t sizeMB)
126 {
127 abort();
128 wait();
129 transTable.resize(sizeMB);
130 }
131
133 [[nodiscard]] auto probe_transposition_table(const Position& pos) const
134 -> std::optional<TTData>
135 {
136 return transTable.find(pos);
137 }
138
140 [[nodiscard]] auto in_progress() const noexcept -> bool { return activeFlag.load(std::memory_order::acquire); }
141
145 void wait() const;
146
151 void set_pondering(const bool isPonderMode) noexcept { pondering.store(isPonderMode, std::memory_order::release); }
152
156 void ponder_hit() noexcept { pondering.store(false, std::memory_order::release); }
157
161 void set_position(const Position& pos)
162 {
163 abort();
164 wait();
165 position = pos;
166
167 // clear this so that all legal moves will be searched by default
168 options.movesToSearch.clear();
169 }
170
174 void set_options(const Options& opts)
175 {
176 abort();
177 wait();
178 options = opts;
179 }
180
185 {
186 abort();
187 wait();
188 options = Options::from_libchess(opts, position.is_white_to_move());
189 }
190
194 void set_callbacks(Callbacks&& callbacksToUse)
195 {
196 abort();
197 wait();
198 callbacks = std::move(callbacksToUse);
199 }
200
202 [[nodiscard]] auto get_position() const noexcept -> const Position& { return position; }
203
205 [[nodiscard]] auto get_options() const noexcept -> const Options& { return options; }
206
207private:
208 Position position;
209
210 Options options;
211
212 Callbacks callbacks;
213
214 TranspositionTable transTable;
215
216 PawnTable pawnTable;
217
218 KillerMoves killerMoves;
219
220 // on the heap, so that this context doesn't take up too much
221 // space on the stack of whichever thread creates it
222 std::unique_ptr<HistoryTable> history { std::make_unique<HistoryTable>() };
223
224 // about 1.1 MiB, so this one must never be on the stack
225 std::unique_ptr<ContinuationHistory> continuationHistory { std::make_unique<ContinuationHistory>() };
226
227 // these flags are all on their own cache lines to prevent
228 // false sharing between the searcher & main (input) threads
229 alignas(util::memory::CacheLineSize) std::atomic_bool exitFlag { false };
230 alignas(util::memory::CacheLineSize) std::atomic_bool activeFlag { false };
231 alignas(util::memory::CacheLineSize) std::atomic_bool pondering { false };
232};
233
234} // namespace ben_bot::search
constexpr auto CacheLineSize
Definition Memory.hpp:73
Context(Callbacks &&callbacksToUse)
Definition Context.hpp:52
void ponder_hit() noexcept
Definition Context.hpp:156
void set_options(const chess::uci::GoCommandOptions &opts)
Definition Context.hpp:184
Context(const Context &)=delete
auto probe_transposition_table(const Position &pos) const -> std::optional< TTData >
Definition Context.hpp:133
void set_callbacks(Callbacks &&callbacksToUse)
Definition Context.hpp:194
Context & operator=(Context &&)=delete
auto in_progress() const noexcept -> bool
Definition Context.hpp:140
Context(Context &&)=delete
void reset_abort() noexcept
Definition Context.hpp:88
auto get_position() const noexcept -> const Position &
Definition Context.hpp:202
void abort() noexcept
Definition Context.hpp:81
void resize_transposition_table(const size_t sizeMB)
Definition Context.hpp:125
auto get_options() const noexcept -> const Options &
Definition Context.hpp:205
auto get_continuation_history() const noexcept -> const ContinuationHistory &
Definition Context.hpp:120
void set_position(const Position &pos)
Definition Context.hpp:161
void set_pondering(const bool isPonderMode) noexcept
Definition Context.hpp:151
auto get_history_table() const noexcept -> const HistoryTable &
Definition Context.hpp:117
Context & operator=(const Context &)=delete
void set_options(const Options &opts)
Definition Context.hpp:174
static auto from_libchess(const chess::uci::GoCommandOptions &goOptions, bool isWhiteToMove, std::chrono::milliseconds overhead=std::chrono::milliseconds { 0 }) -> Options