]> git.sesse.net Git - stockfish/commitdiff
Save threadID info in Position
authorMarco Costalba <mcostalba@gmail.com>
Wed, 2 Jun 2010 19:13:51 +0000 (20:13 +0100)
committerMarco Costalba <mcostalba@gmail.com>
Wed, 2 Jun 2010 19:19:43 +0000 (20:19 +0100)
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 <mcostalba@gmail.com>
src/benchmark.cpp
src/material.cpp
src/position.cpp
src/position.h
src/san.cpp
src/search.cpp
src/tt.cpp
src/uci.cpp

index 1d40b8fdb29e64c83b8c58ccfd11416197c4102b..ee262ba5bc1c2407b14f95c8f6036dfd6945eef4 100644 (file)
@@ -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")
       {
index 0fce07b1d03ea28313253c8866a5d120ecdf46d9..a5f560baee15f29d1c250daaf18687a9ed173912 100644 (file)
@@ -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) {
index a472c84594a219dd22ecf6d09a91bb04c820b74a..c5c63c2b3862701ffdf65ba258d0a407b82c2a5c 100644 (file)
@@ -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++)
index cc816910a3d0320f8e866f12b041dca0d5549ce4..676122c23d400efc87db144749d840323f7beb22 100644 (file)
@@ -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;
 }
index 62383535bf0fa1d9959cd90920a6ef0fd1ab1276..f113e87028c39f4b5c1358073186e8d4f86225f7 100644 (file)
@@ -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++)
   {
index 7ab74998295aa864750a2643a9bd56a5cb6e3bd5..f7595eeb2ced963358a6d73ba1815707e2b52283 100644 (file)
@@ -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;
index cded402bdf7ce9782d18552ce10856f772c3d916..16331b8762ae04c60689a341f46af7c3ad49e9c9 100644 (file)
@@ -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
index 77c963435f3faed7945c62c6851d141a134b3a14..88c1229e320ea804c4ef277f29c7c0d4da95013c 100644 (file)
@@ -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;