]> git.sesse.net Git - stockfish/commitdiff
Merge commit '299707d2c2cbf1694bb21ed4a375b54ef35d719e^'
authorSteinar H. Gunderson <sgunderson@bigfoot.com>
Fri, 13 Sep 2024 13:12:36 +0000 (15:12 +0200)
committerSteinar H. Gunderson <sgunderson@bigfoot.com>
Fri, 13 Sep 2024 13:17:49 +0000 (15:17 +0200)
1  2 
src/hash_probe_impl.cpp
src/hashprobe.h
src/uci.cpp

index 90510f97f696c8947e8a89a4babea7a98574c323,0000000000000000000000000000000000000000..e7e7684a655c98d48527eb37ae16512cce9b3b19
mode 100644,000000..100644
--- /dev/null
@@@ -1,214 -1,0 +1,214 @@@
-               FillValue(eval, response->mutable_eval());
 +#include <deque>
 +#include <cstddef>
 +#include <iostream>
 +#include <stack>
 +#include <thread>
 +
 +#include "bitboard.h"
 +#include "evaluate.h"
 +#include "misc.h"
 +#include "position.h"
 +#include "search.h"
 +#include "thread.h"
 +#include "tune.h"
 +#include "types.h"
 +#include "uci.h"
 +
 +#include <grpc/grpc.h>
 +#include <grpc++/server.h>
 +#include <grpc++/server_builder.h>
 +#include "hashprobe.h"
 +#include "hashprobe.grpc.pb.h"
 +#include "tt.h"
 +
 +using grpc::Server;
 +using grpc::ServerBuilder;
 +using grpc::ServerContext;
 +using grpc::Status;
 +using grpc::StatusCode;
 +using namespace hashprobe;
 +using namespace Stockfish;
 +
 +Status HashProbeImpl::Probe(ServerContext* context,
 +                            const HashProbeRequest* request,
 +                          HashProbeResponse *response) {
 +      Position pos;
 +      StateInfo st;
 +      pos.set(request->fen(), /*isChess960=*/false, &st);
 +      if (!pos.pos_is_ok()) {
 +              return Status(StatusCode::INVALID_ARGUMENT, "Invalid FEN");
 +      }
 +
 +      bool invert = (pos.side_to_move() == BLACK);
 +      StateListPtr setup_states = StateListPtr(new std::deque<StateInfo>(1));
 +
 +      ProbeMove(&pos, setup_states.get(), invert, response->mutable_root());
 +
 +      MoveList<LEGAL> moves(pos);
 +      for (const ExtMove* em = moves.begin(); em != moves.end(); ++em) {
 +              HashProbeLine *line = response->add_line();
 +              FillMove(&pos, *em, line->mutable_move());
 +              setup_states->push_back(StateInfo());
 +              pos.do_move(*em, setup_states->back());
 +              ProbeMove(&pos, setup_states.get(), !invert, line);
 +              pos.undo_move(*em);
 +      }
 +
 +      return Status::OK;
 +}
 +
 +void HashProbeImpl::FillMove(Position *pos, Move move, HashProbeMove* decoded) {
 +      if (!move.is_ok()) return;
 +
 +      Square from = move.from_sq();
 +      Square to = move.to_sq();
 +
 +      if (move.type_of() == CASTLING) {
 +              to = make_square(to > from ? FILE_G : FILE_C, rank_of(from));
 +      }
 +
 +      Piece moved_piece = pos->moved_piece(move);
 +      std::string pretty;
 +      if (move.type_of() == CASTLING) {
 +              if (to > from) {
 +                      pretty = "O-O";
 +              } else {
 +                      pretty = "O-O-O";
 +              }
 +      } else if (type_of(moved_piece) == PAWN) {
 +              if (move.type_of() == EN_PASSANT || pos->piece_on(to) != NO_PIECE) {
 +                      // Capture.
 +                      pretty = char('a' + file_of(from));
 +                      pretty += "x";
 +              }
 +              pretty += UCI::square(to);
 +              if (move.type_of() == PROMOTION) {
 +                      pretty += "=";
 +                      pretty += " PNBRQK"[move.promotion_type()];
 +              }
 +      } else {
 +              pretty = " PNBRQK"[type_of(moved_piece)];
 +              Bitboard attackers = pos->attackers_to(to) & pos->pieces(color_of(moved_piece), type_of(moved_piece));
 +              if (more_than_one(attackers)) {
 +                      // Remove all illegal moves to disambiguate.
 +                      Bitboard att_copy = attackers;
 +                      while (att_copy) {
 +                              Square s = pop_lsb(att_copy);
 +                              Move m(s, to);
 +                              if (!pos->pseudo_legal(m) || !pos->legal(m)) {
 +                                      attackers &= ~square_bb(s);
 +                              }
 +                      }
 +              }
 +              if (more_than_one(attackers)) {
 +                      // Disambiguate by file if possible.
 +                      Bitboard attackers_this_file = attackers & file_bb(file_of(from));
 +                      if (attackers != attackers_this_file) {
 +                              pretty += char('a' + file_of(from));
 +                              attackers = attackers_this_file;
 +                      }
 +                      if (more_than_one(attackers)) {
 +                              // Still ambiguous, so need to disambiguate by rank.
 +                              pretty += char('1' + rank_of(from));
 +                      }
 +              }
 +
 +              if (move.type_of() == EN_PASSANT || pos->piece_on(to) != NO_PIECE) {
 +                      pretty += "x";
 +              }
 +
 +              pretty += UCI::square(to);
 +      }
 +
 +      if (pos->gives_check(move)) {
 +              // Check if mate.
 +              StateInfo si;
 +              pos->do_move(move, si, true);
 +              if (MoveList<LEGAL>(*pos).size() > 0) {
 +                      pretty += "+";
 +              } else {
 +                      pretty += "#";
 +              }
 +              pos->undo_move(move);
 +      }
 +
 +      decoded->set_pretty(pretty);
 +}
 +
 +void HashProbeImpl::ProbeMove(Position* pos, std::deque<StateInfo>* setup_states, bool invert, HashProbeLine* response) {
 +      bool found;
 +      TTEntry *entry = tt.probe(pos->key(), found);
 +      response->set_found(found);
 +      if (found) {
 +              TTEntry entry_copy = *entry;
 +              Value value = entry_copy.value();
 +              Value eval = entry_copy.eval();
 +              Bound bound = entry_copy.bound();
 +
 +              if (invert) {
 +                      value = -value;
 +                      eval = -eval;
 +                      if (bound == BOUND_UPPER) {
 +                              bound = BOUND_LOWER;
 +                      } else if (bound == BOUND_LOWER) {
 +                              bound = BOUND_UPPER;
 +                      }
 +              }
 +
 +              response->set_depth(entry_copy.depth());
-                       FillValue(value, response->mutable_value());
++              FillValue(eval, response->mutable_eval(), *pos);
 +              if (entry_copy.depth() > DEPTH_NONE) {
- void HashProbeImpl::FillValue(Value value, HashProbeScore* score) {
++                      FillValue(value, response->mutable_value(), *pos);
 +              }
 +              response->set_bound(HashProbeLine::ValueBound(bound));
 +
 +              // Follow the PV until we hit an illegal move.
 +              std::stack<Move> pv;
 +              std::set<Key> seen;
 +              while (entry_copy.move().is_ok() &&
 +                     pos->pseudo_legal(entry_copy.move()) &&
 +                     pos->legal(entry_copy.move())) {
 +                      FillMove(pos, entry_copy.move(), response->add_pv());
 +                      if (seen.count(pos->key())) break;
 +                      pv.push(entry_copy.move());
 +                      seen.insert(pos->key());
 +                      setup_states->push_back(StateInfo());
 +                      pos->do_move(entry_copy.move(), setup_states->back());
 +                      entry = tt.probe(pos->key(), found);
 +                      if (!found) {
 +                              break;
 +                      }
 +                      entry_copy = *entry;
 +              }
 +
 +              // Unroll the PV back again, so the Position object remains unchanged.
 +              while (!pv.empty()) {
 +                      pos->undo_move(pv.top());
 +                      pv.pop();
 +              }
 +      }
 +}
 +
-               score->set_score_cp(UCI::to_cp(value));
++void HashProbeImpl::FillValue(Value value, HashProbeScore* score, Position& pos) {
 +      if (abs(value) < VALUE_MATE - MAX_PLY) {
 +              score->set_score_type(HashProbeScore::SCORE_CP);
++              score->set_score_cp(UCI::to_cp(value, pos));
 +      } else {
 +              score->set_score_type(HashProbeScore::SCORE_MATE);
 +              score->set_score_mate((value > 0 ? VALUE_MATE - value + 1 : -VALUE_MATE - value) / 2);
 +      }
 +}
 +
 +HashProbeThread::HashProbeThread(const std::string &server_address, TranspositionTable &tt)
 +      : service(tt) {
 +      builder.AddListeningPort(server_address, grpc::InsecureServerCredentials());
 +      builder.RegisterService(&service);
 +      server = std::move(builder.BuildAndStart());
 +      std::cout << "Server listening on " << server_address << std::endl;
 +      std::thread([this]{ server->Wait(); }).detach();
 +}
 +
 +void HashProbeThread::Shutdown() {
 +      server->Shutdown();
 +}
 +
diff --cc src/hashprobe.h
index 4e9438de098c184a2a3c0308021064e61f82bc7e,0000000000000000000000000000000000000000..a227c8919eb96c16ccc12e67c0d703b9a80d22b7
mode 100644,000000..100644
--- /dev/null
@@@ -1,43 -1,0 +1,43 @@@
-       void FillValue(Stockfish::Value value, hashprobe::HashProbeScore* score);
 +#ifndef HASHPROBE_H_INCLUDED
 +#define HASHPROBE_H_INCLUDED
 +
 +#include "position.h"
 +#include "types.h"
 +#include "tt.h"
 +
 +#include <deque>
 +#include <string>
 +
 +#include <grpc/grpc.h>
 +#include <grpc++/server.h>
 +#include <grpc++/server_builder.h>
 +#include "hashprobe.grpc.pb.h"
 +
 +class HashProbeImpl final : public hashprobe::HashProbe::Service {
 +public:
 +      HashProbeImpl(Stockfish::TranspositionTable& tt) : tt(tt) {}
 +
 +      grpc::Status Probe(grpc::ServerContext* context,
 +                         const hashprobe::HashProbeRequest* request,
 +                         hashprobe::HashProbeResponse *response);
 +
 +private:
 +      void FillMove(Stockfish::Position* pos, Stockfish::Move move, hashprobe::HashProbeMove* decoded);
 +      void ProbeMove(Stockfish::Position* pos, std::deque<Stockfish::StateInfo>* setup_states, bool invert, hashprobe::HashProbeLine* response);
++      void FillValue(Stockfish::Value value, hashprobe::HashProbeScore* score, Stockfish::Position& pos);
 +
 +      Stockfish::TranspositionTable& tt;
 +};
 +
 +class HashProbeThread {
 +public:
 +      HashProbeThread(const std::string &server_address, Stockfish::TranspositionTable& tt);
 +      void Shutdown();
 +
 +private:
 +      HashProbeImpl service;
 +      grpc::ServerBuilder builder;
 +      std::unique_ptr<grpc::Server> server;
 +};
 +
 +#endif
diff --cc src/uci.cpp
index a767a6ff2bb79d0b1bab4b3733eb5a025d1b3215,ee95d5be5e6a3d49f0fc6cb7a4a594e1dfd1b57d..361cf2cc4beac7d673f2aaab30ae87649156173e
  
  namespace Stockfish {
  
- constexpr auto StartFEN             = "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1";
- constexpr int  NormalizeToPawnValue = 356;
- constexpr int  MaxHashMB            = Is64Bit ? 33554432 : 2048;
+ constexpr auto StartFEN  = "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1";
+ constexpr int  MaxHashMB = Is64Bit ? 33554432 : 2048;
  
 -
  namespace NN = Eval::NNUE;
  
 +std::unique_ptr<HashProbeThread> hash_probe_thread;
  
  UCI::UCI(int argc, char** argv) :
      networks(NN::Networks(