-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsearch_handler.cpp
More file actions
170 lines (161 loc) · 8.91 KB
/
search_handler.cpp
File metadata and controls
170 lines (161 loc) · 8.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#include "search.hpp"
#include <iostream>
#include <ostream>
#include <fmt/format.h>
#include "common.hpp"
#include "move_generator.hpp"
void SearchHandler::search_thread_function() {
int this_search_id;
while (true) {
barrier.wait();
this_search_id = current_search_id;
if (this->shutting_down) {
return;
}
search_cancelled = false;
in_search = true;
{
std::lock_guard<std::mutex> lock(search_mutex);
if (should_perft) {
Perft::run_perft(board_hist[board_hist.len() - 1], perft_depth, true);
should_perft = false;
} else {
auto move = run_iterative_deepening_search();
if (move.is_null_move()) {
// Unlikely, but possible!
move = Search::select_random_move(board_hist[board_hist.len() - 1]);
// Just choose a random move
}
if (this_search_id == current_search_id && print_info) {
fmt::println("bestmove {}", move);
std::fflush(stdout);
}
// We've had issues with stdout not being flushed in the past
}
}
in_search = false;
supervisor_cv.notify_all();
}
}
void SearchHandler::shutdown() {
this->shutting_down = true;
this->search_cancelled = true;
// If we're in a search, quit searching ASAP
barrier.wait();
this->search_thread.join();
}
SearchHandler::SearchHandler() {
this->search_thread = std::thread(&SearchHandler::search_thread_function, this);
recompute_table();
}
void SearchHandler::search(const TimeControlInfo& tc) {
current_search_id += 1;
search_cancelled = true;
// cancel a search if performing one
in_search = true;
this->tc = tc;
barrier.wait();
// We then wake up the search thread
int id_to_cancel = current_search_id;
const auto tc_search_time = TimeManagement::get_search_time(tc);
const auto tc_end_time = std::chrono::steady_clock::now() + std::chrono::milliseconds(tc_search_time);
if (TimeManagement::is_time_based_tc(tc)) {
cancelFuture = std::async(std::launch::async, [this, id_to_cancel, tc_end_time]() {
auto lk = std::unique_lock(supervisor_mutex);
supervisor_cv.wait_until(lk, tc_end_time, [&]() {
return !in_search || current_search_id != id_to_cancel || shutting_down;
});
if (this->get_current_search_id() == id_to_cancel) {
// only cancel the current search
this->EndSearch();
}
});
}
}
void SearchHandler::run_perft(uint16_t depth) {
search_cancelled = true;
// cancel any existing search
perft_depth = depth;
should_perft = true;
in_search = true;
barrier.wait();
}
void SearchHandler::reset() {
this->EndSearch();
board_hist = BoardHistory();
tt.clear();
history_table.clear();
}
void SearchHandler::run_bench(uint16_t depth) {
static constexpr std::array fens = {// taken from alexandria, originally from bitgenie
"r3k2r/2pb1ppp/2pp1q2/p7/1nP1B3/1P2P3/P2N1PPP/R2QK2R w KQkq a6 0 14",
"4rrk1/2p1b1p1/p1p3q1/4p3/2P2n1p/1P1NR2P/PB3PP1/3R1QK1 b - - 2 24",
"r3qbrk/6p1/2b2pPp/p3pP1Q/PpPpP2P/3P1B2/2PB3K/R5R1 w - - 16 42",
"6k1/1R3p2/6p1/2Bp3p/3P2q1/P7/1P2rQ1K/5R2 b - - 4 44",
"8/8/1p2k1p1/3p3p/1p1P1P1P/1P2PK2/8/8 w - - 3 54",
"7r/2p3k1/1p1p1qp1/1P1Bp3/p1P2r1P/P7/4R3/Q4RK1 w - - 0 36",
"r1bq1rk1/pp2b1pp/n1pp1n2/3P1p2/2P1p3/2N1P2N/PP2BPPP/R1BQ1RK1 b - - 2 10",
"3r3k/2r4p/1p1b3q/p4P2/P2Pp3/1B2P3/3BQ1RP/6K1 w - - 3 87",
"2r4r/1p4k1/1Pnp4/3Qb1pq/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1/5k2/p1b1r2p/1pB1p1p1/1Pp3PP/2P1R1K1/2P2P2/3R4 w - - 1 36",
"rnbqkb1r/pppppppp/5n2/8/2PP4/8/PP2PPPP/RNBQKBNR b KQkq c3 0 2",
"2rr2k1/1p4bp/p1q1p1p1/4Pp1n/2PB4/1PN3P1/P3Q2P/2RR2K1 w - f6 0 20",
"3br1k1/p1pn3p/1p3n2/5pNq/2P1p3/1PN3PP/P2Q1PB1/4R1K1 w - - 0 23",
"2r2b2/5p2/5k2/p1r1pP2/P2pB3/1P3P2/K1P3R1/7R w - - 23 93",
"8/P6p/2K1q1pk/2Q5/4p3/8/7P/8 w - - 4 44",
"7k/8/7P/5B2/5K2/8/8/8 b - - 0 175"};
print_info = false;
uint64_t total_nodes = 0;
const auto start = std::chrono::steady_clock::now();
for (const auto& fen : fens) {
this->reset();
Position pos;
pos.set_from_fen(fen);
this->set_pos(pos);
this->tc = DepthTC{depth};
search_cancelled = false;
run_iterative_deepening_search();
// loop until search completes
total_nodes += node_count;
fmt::println("{} {}", fen, node_count);
}
const auto duration =
std::max(std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::steady_clock::now() - start).count(), (int64_t) 1);
fmt::println("{} nodes {} nps", total_nodes, (total_nodes / duration) * 1000);
}