]> git.sesse.net Git - stockfish/blob - src/main.cpp
d20d8df4b3dc8af0eb5fe90d7f2c63b900b9dac2
[stockfish] / src / main.cpp
1 /*
2   Stockfish, a UCI chess playing engine derived from Glaurung 2.1
3   Copyright (C) 2004-2008 Tord Romstad (Glaurung author)
4   Copyright (C) 2008-2015 Marco Costalba, Joona Kiiski, Tord Romstad
5   Copyright (C) 2015-2019 Marco Costalba, Joona Kiiski, Gary Linscott, Tord Romstad
6
7   Stockfish is free software: you can redistribute it and/or modify
8   it under the terms of the GNU General Public License as published by
9   the Free Software Foundation, either version 3 of the License, or
10   (at your option) any later version.
11
12   Stockfish is distributed in the hope that it will be useful,
13   but WITHOUT ANY WARRANTY; without even the implied warranty of
14   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15   GNU General Public License for more details.
16
17   You should have received a copy of the GNU General Public License
18   along with this program.  If not, see <http://www.gnu.org/licenses/>.
19 */
20
21 #include <iostream>
22 #include <thread>
23
24 #include "bitboard.h"
25 #include "position.h"
26 #include "search.h"
27 #include "thread.h"
28 #include "tt.h"
29 #include "uci.h"
30 #include "syzygy/tbprobe.h"
31
32 #include <grpc/grpc.h>
33 #include <grpc++/server.h>
34 #include <grpc++/server_builder.h>
35 #include "hashprobe.grpc.pb.h"
36
37 using grpc::Server;
38 using grpc::ServerBuilder;
39 using grpc::ServerContext;
40 using grpc::Status;
41 using grpc::StatusCode;
42 using namespace hashprobe;
43
44 class HashProbeImpl final : public HashProbe::Service {
45 public:
46         Status Probe(ServerContext* context,
47                      const HashProbeRequest* request,
48                      HashProbeResponse *response) {
49                 Position pos(request->fen(), /*isChess960=*/false, Threads.main());
50                 if (!pos.pos_is_ok()) {
51                         return Status(StatusCode::INVALID_ARGUMENT, "Invalid FEN");
52                 }
53
54                 bool invert = (pos.side_to_move() == BLACK);
55                 Search::StateStackPtr setup_states = Search::StateStackPtr(new std::stack<StateInfo>);
56
57                 ProbeMove(&pos, setup_states.get(), invert, response->mutable_root());
58
59                 MoveList<LEGAL> moves(pos);
60                 for (const ExtMove* em = moves.begin(); em != moves.end(); ++em) {
61                         HashProbeLine *line = response->add_line();
62                         FillMove(em->move, line->mutable_move());
63                         setup_states->push(StateInfo());
64                         pos.do_move(em->move, setup_states->top(), pos.gives_check(em->move, CheckInfo(pos)));
65                         ProbeMove(&pos, setup_states.get(), !invert, line);
66                         pos.undo_move(em->move);
67                 }
68
69                 return Status::OK;
70         }
71
72         void FillMove(Move move, HashProbeMove* decoded) {
73                 if (!is_ok(move)) return;
74
75                 Square from = from_sq(move);
76                 Square to = to_sq(move);
77
78                 if (type_of(move) == CASTLING) {
79                         to = make_square(to > from ? FILE_G : FILE_C, rank_of(from));
80                 }
81                         
82                 decoded->set_from_sq(UCI::square(from));
83                 decoded->set_to_sq(UCI::square(to));
84
85                 if (type_of(move) == PROMOTION) {
86                         decoded->set_promotion(std::string() + " PNBRQK"[promotion_type(move)]);
87                 }
88         }
89
90         void ProbeMove(Position* pos, std::stack<StateInfo>* setup_states, bool invert, HashProbeLine* response) {
91                 bool found;
92                 TTEntry *entry = TT.probe(pos->key(), found);
93                 response->set_found(found);
94                 if (found) {
95                         Value value = entry->value();
96                         Value eval = entry->eval();
97                         Bound bound = entry->bound();
98
99                         if (invert) {
100                                 value = -value;
101                                 eval = -eval;
102                                 if (bound == BOUND_UPPER) {
103                                         bound = BOUND_LOWER;
104                                 } else if (bound == BOUND_LOWER) {
105                                         bound = BOUND_UPPER;
106                                 }
107                         }
108
109                         response->set_depth(entry->depth());
110                         FillValue(eval, response->mutable_eval());
111                         if (entry->depth() > DEPTH_NONE) {
112                                 FillValue(value, response->mutable_value());
113                         }
114                         response->set_bound(HashProbeLine::ValueBound(bound));
115
116                         // Follow the PV until we hit an illegal move.
117                         std::stack<Move> pv;
118                         std::set<Key> seen;
119                         while (found && is_ok(entry->move())) {
120                                 FillMove(entry->move(), response->add_pv());
121                                 if (seen.count(pos->key())) break;
122                                 pv.push(entry->move());
123                                 seen.insert(pos->key());
124                                 setup_states->push(StateInfo());
125                                 pos->do_move(entry->move(), setup_states->top(), pos->gives_check(entry->move(), CheckInfo(*pos)));
126                                 entry = TT.probe(pos->key(), found);
127                         }
128
129                         // Unroll the PV back again, so the Position object remains unchanged.
130                         while (!pv.empty()) {
131                                 pos->undo_move(pv.top());
132                                 pv.pop();
133                         }
134                 }
135         }
136
137         void FillValue(Value value, HashProbeScore* score) {
138                 if (abs(value) < VALUE_MATE - MAX_PLY) {
139                         score->set_score_type(HashProbeScore::SCORE_CP);
140                         score->set_score_cp(value * 100 / PawnValueEg);
141                 } else {
142                         score->set_score_type(HashProbeScore::SCORE_MATE);
143                         score->set_score_mate((value > 0 ? VALUE_MATE - value + 1 : -VALUE_MATE - value) / 2);
144                 }
145         }
146 };
147
148 class HashProbeThread {
149 public:
150         HashProbeThread(const std::string &server_address) {
151                 builder.AddListeningPort(server_address, grpc::InsecureServerCredentials());
152                 builder.RegisterService(&service);
153                 server = std::move(builder.BuildAndStart());
154                 std::cout << "Server listening on " << server_address << std::endl;
155                 std::thread([this]{ server->Wait(); }).detach();
156         }
157
158         void Shutdown() {
159                 server->Shutdown();
160         }
161
162 private:
163         HashProbeImpl service;
164         ServerBuilder builder;
165         std::unique_ptr<Server> server;
166 };
167
168 namespace PSQT {
169   void init();
170 }
171
172 int main(int argc, char* argv[]) {
173
174   std::cout << engine_info() << std::endl;
175
176   UCI::init(Options);
177   PSQT::init();
178   Bitboards::init();
179   Position::init();
180   Bitbases::init();
181   Search::init();
182   Pawns::init();
183   Threads.set(Options["Threads"]);
184   Search::clear(); // After threads are up
185
186   HashProbeThread thr("0.0.0.0:50051");
187
188   UCI::loop(argc, argv);
189
190   Threads.set(0);
191   return 0;
192 }