From 2f6927ac08887ff3b709cfe9228b27a85bdd1d88 Mon Sep 17 00:00:00 2001 From: Marco Costalba Date: Wed, 2 Jun 2010 20:13:51 +0100 Subject: [PATCH] Save threadID info in Position This is the best place because when we split we do a copy of the position and also threadID, once set in a given position, never changes anymore. Forbid use of Position's default and copy c'tor to avoid nasty bugs in case a position is created without explicitly setting the threadID. No functional change. Signed-off-by: Marco Costalba --- src/benchmark.cpp | 2 +- src/material.cpp | 2 +- src/position.cpp | 19 ++++++++++--------- src/position.h | 15 ++++++++++++--- src/san.cpp | 2 +- src/search.cpp | 4 ++-- src/tt.cpp | 4 ++-- src/uci.cpp | 6 +++--- 8 files changed, 32 insertions(+), 22 deletions(-) diff --git a/src/benchmark.cpp b/src/benchmark.cpp index 1d40b8fd..ee262ba5 100644 --- a/src/benchmark.cpp +++ b/src/benchmark.cpp @@ -151,7 +151,7 @@ void benchmark(const string& commandLine) { { Move moves[1] = {MOVE_NONE}; int dummy[2] = {0, 0}; - Position pos(*it); + Position pos(*it, 0); cerr << "\nBench position: " << cnt << '/' << positions.size() << endl << endl; if (limitType == "perft") { diff --git a/src/material.cpp b/src/material.cpp index 0fce07b1..a5f560ba 100644 --- a/src/material.cpp +++ b/src/material.cpp @@ -403,7 +403,7 @@ Key EndgameFunctions::buildKey(const string& keyCode) { s << char(upcase? toupper(keyCode[i]) : tolower(keyCode[i])); } s << 8 - keyCode.length() << "/8/8/8/8/8/8/8 w -"; - return Position(s.str()).get_material_key(); + return Position(s.str(), 0).get_material_key(); } const string EndgameFunctions::swapColors(const string& keyCode) { diff --git a/src/position.cpp b/src/position.cpp index a472c845..c5c63c2b 100644 --- a/src/position.cpp +++ b/src/position.cpp @@ -74,23 +74,23 @@ CheckInfo::CheckInfo(const Position& pos) { } -/// Position c'tors. Here we always create a slower but safer copy of -/// the original position or the FEN string, we want the new born Position -/// object do not depend on any external data. Instead if we know what we -/// are doing and we need speed we can create a position with default -/// c'tor Position() and then use just fast_copy(). +/// Position c'tors. Here we always create a copy of the original position +/// or the FEN string, we want the new born Position object do not depend +/// on any external data so we detach state pointer from the source one. -Position::Position() {} +Position::Position(int th) : threadID(th) {} -Position::Position(const Position& pos) { +Position::Position(const Position& pos, int th) { memcpy(this, &pos, sizeof(Position)); detach(); // Always detach() in copy c'tor to avoid surprises + threadID = th; } -Position::Position(const string& fen) { +Position::Position(const string& fen, int th) { from_fen(fen); + threadID = th; } @@ -340,7 +340,7 @@ void Position::print(Move m) const { std::cout << std::endl; if (m != MOVE_NONE) { - Position p(*this); + Position p(*this, thread()); string col = (color_of_piece_on(move_from(m)) == BLACK ? ".." : ""); std::cout << "Move is: " << col << move_to_san(p, m) << std::endl; } @@ -1785,6 +1785,7 @@ void Position::flipped_copy(const Position& pos) { assert(pos.is_ok()); clear(); + threadID = pos.thread(); // Board for (Square s = SQ_A1; s <= SQ_H8; s++) diff --git a/src/position.h b/src/position.h index cc816910..676122c2 100644 --- a/src/position.h +++ b/src/position.h @@ -139,6 +139,9 @@ class Position { friend class MaterialInfo; friend class EndgameFunctions; + Position(); // No default or copy c'tor allowed + Position(const Position& pos); + public: enum GamePhase { MidGame, @@ -146,9 +149,9 @@ public: }; // Constructors - Position(); - explicit Position(const Position& pos); - explicit Position(const std::string& fen); + explicit Position(int threadID); + Position(const Position& pos, int threadID); + Position(const std::string& fen, int threadID); // Text input/output void from_fen(const std::string& fen); @@ -272,6 +275,7 @@ public: bool has_pawn_on_7th(Color c) const; // Game ply information + int thread() const; int ply() const; void reset_ply(); @@ -328,6 +332,7 @@ private: int castleRightsMask[64]; StateInfo startState; File initialKFile, initialKRFile, initialQRFile; + int threadID; StateInfo* st; // Static variables @@ -557,6 +562,10 @@ inline PieceType Position::captured_piece() const { return st->capture; } +inline int Position::thread() const { + return threadID; +} + inline int Position::ply() const { return st->ply; } diff --git a/src/san.cpp b/src/san.cpp index 62383535..f113e870 100644 --- a/src/san.cpp +++ b/src/san.cpp @@ -299,7 +299,7 @@ const string line_to_san(const Position& pos, Move line[], int startColumn, bool string moveStr; size_t length = 0; size_t maxLength = 80 - startColumn; - Position p(pos); + Position p(pos, pos.thread()); for (int i = 0; line[i] != MOVE_NONE; i++) { diff --git a/src/search.cpp b/src/search.cpp index 7ab74998..f7595eeb 100644 --- a/src/search.cpp +++ b/src/search.cpp @@ -603,7 +603,7 @@ namespace { Value id_loop(const Position& pos, Move searchMoves[]) { - Position p(pos); + Position p(pos, pos.thread()); SearchStack ss[PLY_MAX_PLUS_2]; Move EasyMove = MOVE_NONE; Value value, alpha = -VALUE_INFINITE, beta = VALUE_INFINITE; @@ -1662,7 +1662,7 @@ namespace { int moveCount; value = -VALUE_INFINITE; - Position pos(*sp->pos); + Position pos(*sp->pos, threadID); CheckInfo ci(pos); int ply = pos.ply(); SearchStack* ss = sp->sstack[threadID] + 1; diff --git a/src/tt.cpp b/src/tt.cpp index cded402b..16331b87 100644 --- a/src/tt.cpp +++ b/src/tt.cpp @@ -205,7 +205,7 @@ void TranspositionTable::new_search() { void TranspositionTable::insert_pv(const Position& pos, Move pv[]) { StateInfo st; - Position p(pos); + Position p(pos, pos.thread()); for (int i = 0; pv[i] != MOVE_NONE; i++) { @@ -227,7 +227,7 @@ void TranspositionTable::extract_pv(const Position& pos, Move pv[], const int PL const TTEntry* tte; StateInfo st; - Position p(pos); + Position p(pos, pos.thread()); int ply = 0; // Update position to the end of current PV diff --git a/src/uci.cpp b/src/uci.cpp index 77c96343..88c1229e 100644 --- a/src/uci.cpp +++ b/src/uci.cpp @@ -54,7 +54,7 @@ namespace { // The root position. This is set up when the user (or in practice, the GUI) // sends the "position" UCI command. The root position is sent to the think() // function when the program receives the "go" command. - Position RootPosition; + Position RootPosition(0); // Local functions bool handle_command(const string& command); @@ -143,7 +143,7 @@ namespace { RootPosition.print(); else if (token == "flip") { - Position p(RootPosition); + Position p(RootPosition, RootPosition.thread()); RootPosition.flipped_copy(p); } else if (token == "eval") @@ -308,7 +308,7 @@ namespace { string token; int depth, tm, n; - Position pos(RootPosition); + Position pos(RootPosition, RootPosition.thread()); if (!(uip >> depth)) return; -- 2.39.2