1
0
Fork 0
mirror of https://github.com/sockspls/badfish synced 2025-04-30 00:33:09 +00:00

Use a global instead of a variable in pos

This commit is contained in:
Joost VandeVondele 2020-07-28 15:41:35 +02:00
parent e42258db5a
commit 8e28c99f79
7 changed files with 26 additions and 26 deletions

View file

@ -30,6 +30,10 @@
#include "pawns.h" #include "pawns.h"
#include "thread.h" #include "thread.h"
namespace Eval {
bool useNNUE;
}
namespace Trace { namespace Trace {
enum Tracing { NO_TRACE, TRACE }; enum Tracing { NO_TRACE, TRACE };
@ -900,7 +904,7 @@ make_v:
/// evaluation of the position from the point of view of the side to move. /// evaluation of the position from the point of view of the side to move.
Value Eval::evaluate(const Position& pos) { Value Eval::evaluate(const Position& pos) {
if (pos.use_nnue()) if (Eval::useNNUE)
return NNUE::evaluate(pos); return NNUE::evaluate(pos);
else else
return Evaluation<NO_TRACE>(pos).value(); return Evaluation<NO_TRACE>(pos).value();
@ -921,7 +925,7 @@ std::string Eval::trace(const Position& pos) {
Value v; Value v;
if (pos.use_nnue()) if (Eval::useNNUE)
{ {
v = NNUE::evaluate(pos); v = NNUE::evaluate(pos);
} }

View file

@ -31,6 +31,7 @@ namespace Eval {
std::string trace(const Position& pos); std::string trace(const Position& pos);
Value evaluate(const Position& pos); Value evaluate(const Position& pos);
extern bool useNNUE;
namespace NNUE { namespace NNUE {

View file

@ -80,7 +80,7 @@ std::ostream& operator<<(std::ostream& os, const Position& pos) {
{ {
StateInfo st; StateInfo st;
Position p; Position p;
p.set(pos.fen(), pos.is_chess960(), pos.use_nnue(), &st, pos.this_thread()); p.set(pos.fen(), pos.is_chess960(), &st, pos.this_thread());
Tablebases::ProbeState s1, s2; Tablebases::ProbeState s1, s2;
Tablebases::WDLScore wdl = Tablebases::probe_wdl(p, &s1); Tablebases::WDLScore wdl = Tablebases::probe_wdl(p, &s1);
int dtz = Tablebases::probe_dtz(p, &s2); int dtz = Tablebases::probe_dtz(p, &s2);
@ -154,7 +154,7 @@ void Position::init() {
/// This function is not very robust - make sure that input FENs are correct, /// This function is not very robust - make sure that input FENs are correct,
/// this is assumed to be the responsibility of the GUI. /// this is assumed to be the responsibility of the GUI.
Position& Position::set(const string& fenStr, bool isChess960, bool useNnue, StateInfo* si, Thread* th) { Position& Position::set(const string& fenStr, bool isChess960, StateInfo* si, Thread* th) {
/* /*
A FEN string defines a particular position using only the ASCII character set. A FEN string defines a particular position using only the ASCII character set.
@ -219,7 +219,7 @@ Position& Position::set(const string& fenStr, bool isChess960, bool useNnue, Sta
auto pc = Piece(idx); auto pc = Piece(idx);
put_piece(pc, sq); put_piece(pc, sq);
if (useNnue) if (Eval::useNNUE)
{ {
// Kings get a fixed ID, other pieces get ID in order of placement // Kings get a fixed ID, other pieces get ID in order of placement
piece_id = piece_id =
@ -295,7 +295,6 @@ Position& Position::set(const string& fenStr, bool isChess960, bool useNnue, Sta
gamePly = std::max(2 * (gamePly - 1), 0) + (sideToMove == BLACK); gamePly = std::max(2 * (gamePly - 1), 0) + (sideToMove == BLACK);
chess960 = isChess960; chess960 = isChess960;
nnue = useNnue;
thisThread = th; thisThread = th;
set_state(st); set_state(st);
@ -404,7 +403,7 @@ Position& Position::set(const string& code, Color c, StateInfo* si) {
string fenStr = "8/" + sides[0] + char(8 - sides[0].length() + '0') + "/8/8/8/8/" string fenStr = "8/" + sides[0] + char(8 - sides[0].length() + '0') + "/8/8/8/8/"
+ sides[1] + char(8 - sides[1].length() + '0') + "/8 w - - 0 10"; + sides[1] + char(8 - sides[1].length() + '0') + "/8 w - - 0 10";
return set(fenStr, false, false, si, nullptr); return set(fenStr, false, si, nullptr);
} }
@ -776,7 +775,7 @@ void Position::do_move(Move m, StateInfo& newSt, bool givesCheck) {
else else
st->nonPawnMaterial[them] -= PieceValue[MG][captured]; st->nonPawnMaterial[them] -= PieceValue[MG][captured];
if (use_nnue()) if (Eval::useNNUE)
{ {
dp.dirty_num = 2; // 2 pieces moved dp.dirty_num = 2; // 2 pieces moved
dp1 = piece_id_on(capsq); dp1 = piece_id_on(capsq);
@ -823,7 +822,7 @@ void Position::do_move(Move m, StateInfo& newSt, bool givesCheck) {
// Move the piece. The tricky Chess960 castling is handled earlier // Move the piece. The tricky Chess960 castling is handled earlier
if (type_of(m) != CASTLING) { if (type_of(m) != CASTLING) {
if (use_nnue()) if (Eval::useNNUE)
{ {
dp0 = piece_id_on(from); dp0 = piece_id_on(from);
dp.pieceId[0] = dp0; dp.pieceId[0] = dp0;
@ -855,7 +854,7 @@ void Position::do_move(Move m, StateInfo& newSt, bool givesCheck) {
remove_piece(to); remove_piece(to);
put_piece(promotion, to); put_piece(promotion, to);
if (use_nnue()) if (Eval::useNNUE)
{ {
dp0 = piece_id_on(to); dp0 = piece_id_on(to);
evalList.put_piece(dp0, to, promotion); evalList.put_piece(dp0, to, promotion);
@ -951,10 +950,9 @@ void Position::undo_move(Move m) {
} }
else else
{ {
move_piece(to, from); // Put the piece back at the source square move_piece(to, from); // Put the piece back at the source square
if (use_nnue()) if (Eval::useNNUE)
{ {
PieceId dp0 = st->dirtyPiece.pieceId[0]; PieceId dp0 = st->dirtyPiece.pieceId[0];
evalList.put_piece(dp0, from, pc); evalList.put_piece(dp0, from, pc);
@ -977,7 +975,7 @@ void Position::undo_move(Move m) {
put_piece(st->capturedPiece, capsq); // Restore the captured piece put_piece(st->capturedPiece, capsq); // Restore the captured piece
if (use_nnue()) if (Eval::useNNUE)
{ {
PieceId dp1 = st->dirtyPiece.pieceId[1]; PieceId dp1 = st->dirtyPiece.pieceId[1];
assert(evalList.piece_with_id(dp1).from[WHITE] == PS_NONE); assert(evalList.piece_with_id(dp1).from[WHITE] == PS_NONE);
@ -1005,7 +1003,7 @@ void Position::do_castling(Color us, Square from, Square& to, Square& rfrom, Squ
rto = relative_square(us, kingSide ? SQ_F1 : SQ_D1); rto = relative_square(us, kingSide ? SQ_F1 : SQ_D1);
to = relative_square(us, kingSide ? SQ_G1 : SQ_C1); to = relative_square(us, kingSide ? SQ_G1 : SQ_C1);
if (use_nnue()) if (Eval::useNNUE)
{ {
PieceId dp0, dp1; PieceId dp0, dp1;
auto& dp = st->dirtyPiece; auto& dp = st->dirtyPiece;
@ -1048,7 +1046,8 @@ void Position::do_null_move(StateInfo& newSt) {
assert(!checkers()); assert(!checkers());
assert(&newSt != st); assert(&newSt != st);
if (use_nnue()) { if (Eval::useNNUE)
{
std::memcpy(&newSt, st, sizeof(StateInfo)); std::memcpy(&newSt, st, sizeof(StateInfo));
st->accumulator.computed_score = false; st->accumulator.computed_score = false;
} }
@ -1321,7 +1320,7 @@ void Position::flip() {
std::getline(ss, token); // Half and full moves std::getline(ss, token); // Half and full moves
f += token; f += token;
set(f, is_chess960(), use_nnue(), st, this_thread()); set(f, is_chess960(), st, this_thread());
assert(pos_is_ok()); assert(pos_is_ok());
} }

View file

@ -86,7 +86,7 @@ public:
Position& operator=(const Position&) = delete; Position& operator=(const Position&) = delete;
// FEN string input/output // FEN string input/output
Position& set(const std::string& fenStr, bool isChess960, bool useNnue, StateInfo* si, Thread* th); Position& set(const std::string& fenStr, bool isChess960, StateInfo* si, Thread* th);
Position& set(const std::string& code, Color c, StateInfo* si); Position& set(const std::string& code, Color c, StateInfo* si);
const std::string fen() const; const std::string fen() const;
@ -157,7 +157,6 @@ public:
Color side_to_move() const; Color side_to_move() const;
int game_ply() const; int game_ply() const;
bool is_chess960() const; bool is_chess960() const;
bool use_nnue() const;
Thread* this_thread() const; Thread* this_thread() const;
bool is_draw(int ply) const; bool is_draw(int ply) const;
bool has_game_cycle(int ply) const; bool has_game_cycle(int ply) const;
@ -376,10 +375,6 @@ inline bool Position::is_chess960() const {
return chess960; return chess960;
} }
inline bool Position::use_nnue() const {
return nnue;
}
inline bool Position::capture_or_promotion(Move m) const { inline bool Position::capture_or_promotion(Move m) const {
assert(is_ok(m)); assert(is_ok(m));
return type_of(m) != NORMAL ? type_of(m) != CASTLING : !empty(to_sq(m)); return type_of(m) != NORMAL ? type_of(m) != CASTLING : !empty(to_sq(m));

View file

@ -216,7 +216,7 @@ void ThreadPool::start_thinking(Position& pos, StateListPtr& states,
th->nodes = th->tbHits = th->nmpMinPly = th->bestMoveChanges = 0; th->nodes = th->tbHits = th->nmpMinPly = th->bestMoveChanges = 0;
th->rootDepth = th->completedDepth = 0; th->rootDepth = th->completedDepth = 0;
th->rootMoves = rootMoves; th->rootMoves = rootMoves;
th->rootPos.set(pos.fen(), pos.is_chess960(), pos.use_nnue(), &setupStates->back(), th); th->rootPos.set(pos.fen(), pos.is_chess960(), &setupStates->back(), th);
} }
setupStates->back() = tmp; setupStates->back() = tmp;

View file

@ -68,7 +68,7 @@ namespace {
return; return;
states = StateListPtr(new std::deque<StateInfo>(1)); // Drop old and create a new one states = StateListPtr(new std::deque<StateInfo>(1)); // Drop old and create a new one
pos.set(fen, Options["UCI_Chess960"], Options["Use NNUE"], &states->back(), Threads.main()); pos.set(fen, Options["UCI_Chess960"], &states->back(), Threads.main());
// Parse move list (if any) // Parse move list (if any)
while (is >> token && (m = UCI::to_move(pos, token)) != MOVE_NONE) while (is >> token && (m = UCI::to_move(pos, token)) != MOVE_NONE)
@ -236,7 +236,7 @@ void UCI::loop(int argc, char* argv[]) {
string token, cmd; string token, cmd;
StateListPtr states(new std::deque<StateInfo>(1)); StateListPtr states(new std::deque<StateInfo>(1));
pos.set(StartFEN, false, Options["Use NNUE"], &states->back(), Threads.main()); pos.set(StartFEN, false, &states->back(), Threads.main());
if (argc > 1) if (argc > 1)
init_nnue(Options["EvalFile"]); init_nnue(Options["EvalFile"]);

View file

@ -48,8 +48,9 @@ void on_use_nnue(const Option& o) {
if (o) if (o)
std::cout << "info string NNUE eval used" << std::endl; std::cout << "info string NNUE eval used" << std::endl;
else else
std::cout << "info string Standard eval used" << std::endl; std::cout << "info string classic eval used" << std::endl;
Eval::useNNUE = o;
init_nnue(Options["EvalFile"]); init_nnue(Options["EvalFile"]);
} }