]> git.sesse.net Git - stockfish/blobdiff - src/search.cpp
Retire broken SendSearchedNodes
[stockfish] / src / search.cpp
index 0fa5290616861f0f32cf8dea39d9f79e6ea9296b..4864b1cb151f70e2e1fc1849e198c9abc24802a6 100644 (file)
@@ -49,7 +49,7 @@ namespace {
   const bool FakeSplit = false;
 
   // Different node types, used as template parameter
-  enum NodeType { Root, PV, NonPV, SplitPointPV, SplitPointNonPV };
+  enum NodeType { Root, PV, NonPV, SplitPointRoot, SplitPointPV, SplitPointNonPV };
 
   // RootMove struct is used for moves at the root of the tree. For each root
   // move, we store a score, a node count, and a PV (really a refutation
@@ -175,7 +175,6 @@ namespace {
 
   // Node counters, used only by thread[0] but try to keep in different cache
   // lines (64 bytes each) from the heavy multi-thread read accessed variables.
-  bool SendSearchedNodes;
   int NodesSincePoll;
   int NodesBetweenPolls = 30000;
 
@@ -197,7 +196,7 @@ namespace {
   bool connected_moves(const Position& pos, Move m1, Move m2);
   Value value_to_tt(Value v, int ply);
   Value value_from_tt(Value v, int ply);
-  bool ok_to_use_TT(const TTEntry* tte, Depth depth, Value beta, int ply);
+  bool can_return_tt(const TTEntry* tte, Depth depth, Value beta, int ply);
   bool connected_threat(const Position& pos, Move m, Move threat);
   Value refine_eval(const TTEntry* tte, Value defaultEval, int ply);
   void update_history(const Position& pos, Move move, Depth depth, Move movesSearched[], int moveCount);
@@ -216,14 +215,14 @@ namespace {
   // MovePickerExt template class extends MovePicker and allows to choose at compile
   // time the proper moves source according to the type of node. In the default case
   // we simply create and use a standard MovePicker object.
-  template<NodeType> struct MovePickerExt : public MovePicker {
+  template<bool SpNode> struct MovePickerExt : public MovePicker {
 
     MovePickerExt(const Position& p, Move ttm, Depth d, const History& h, SearchStack* ss, Value b)
                   : MovePicker(p, ttm, d, h, ss, b) {}
   };
 
   // In case of a SpNode we use split point's shared MovePicker object as moves source
-  template<> struct MovePickerExt<SplitPointNonPV> : public MovePicker {
+  template<> struct MovePickerExt<true> : public MovePicker {
 
     MovePickerExt(const Position& p, Move ttm, Depth d, const History& h, SearchStack* ss, Value b)
                   : MovePicker(p, ttm, d, h, ss, b), mp(ss->sp->mp) {}
@@ -232,12 +231,6 @@ namespace {
     MovePicker* mp;
   };
 
-  template<> struct MovePickerExt<SplitPointPV> : public MovePickerExt<SplitPointNonPV> {
-
-    MovePickerExt(const Position& p, Move ttm, Depth d, const History& h, SearchStack* ss, Value b)
-                  : MovePickerExt<SplitPointNonPV>(p, ttm, d, h, ss, b) {}
-  };
-
   // Overload operator<<() to make it easier to print moves in a coordinate
   // notation compatible with UCI protocol.
   std::ostream& operator<<(std::ostream& os, Move m) {
@@ -373,7 +366,7 @@ bool think(Position& pos, const SearchLimits& limits, Move searchMoves[]) {
   static Book book;
 
   // Initialize global search-related variables
-  StopOnPonderhit = StopRequest = QuitRequest = AspirationFailLow = SendSearchedNodes = false;
+  StopOnPonderhit = StopRequest = QuitRequest = AspirationFailLow = false;
   NodesSincePoll = 0;
   current_search_time(get_system_time());
   Limits = limits;
@@ -566,7 +559,8 @@ namespace {
             // Start with a small aspiration window and, in case of fail high/low,
             // research with bigger window until not failing high/low anymore.
             do {
-                // Search starting from ss+1 to allow calling update_gains()
+                // Search starting from ss+1 to allow referencing (ss-1). This is
+                // needed by update_gains() and ss copy when splitting at Root.
                 value = search<Root>(pos, ss+1, alpha, beta, depth * ONE_PLY);
 
                 // It is critical that sorting is done with a stable algorithm
@@ -593,8 +587,7 @@ namespace {
                 // Send full PV info to GUI if we are going to leave the loop or
                 // if we have a fail high/low and we are deep in the search.
                 if ((value > alpha && value < beta) || current_search_time() > 2000)
-                    for (int i = 0; i < Min(UCIMultiPV, MultiPVIteration); i++)
-                    {
+                    for (int i = 0; i < Min(UCIMultiPV, MultiPVIteration + 1); i++)
                         cout << "info"
                              << depth_to_uci(depth * ONE_PLY)
                              << (i == MultiPVIteration ? score_to_uci(Rml[i].score, alpha, beta) :
@@ -602,7 +595,6 @@ namespace {
                              << speed_to_uci(pos.nodes_searched())
                              << pv_to_uci(&Rml[i].pv[0], i + 1, pos.is_chess960())
                              << endl;
-                    }
 
                 // In case of failing high/low increase aspiration window and research,
                 // otherwise exit the fail high/low loop.
@@ -701,9 +693,9 @@ namespace {
   template <NodeType NT>
   Value search(Position& pos, SearchStack* ss, Value alpha, Value beta, Depth depth) {
 
-    const bool PvNode   = (NT == PV || NT == Root || NT == SplitPointPV);
-    const bool SpNode   = (NT == SplitPointPV || NT == SplitPointNonPV);
-    const bool RootNode = (NT == Root);
+    const bool PvNode   = (NT == PV || NT == Root || NT == SplitPointPV || NT == SplitPointRoot);
+    const bool SpNode   = (NT == SplitPointPV || NT == SplitPointNonPV || NT == SplitPointRoot);
+    const bool RootNode = (NT == Root || NT == SplitPointRoot);
 
     assert(alpha >= -VALUE_INFINITE && alpha <= VALUE_INFINITE);
     assert(beta > alpha && beta <= VALUE_INFINITE);
@@ -719,7 +711,7 @@ namespace {
     Depth ext, newDepth;
     ValueType vt;
     Value bestValue, value, oldAlpha;
-    Value refinedValue, nullValue, futilityBase, futilityValueScaled; // Non-PV specific
+    Value refinedValue, nullValue, futilityBase, futilityValue;
     bool isPvMove, inCheck, singularExtensionNode, givesCheck, captureOrPromotion, dangerous;
     int moveCount = 0, playedMoveCount = 0;
     Thread& thread = Threads[pos.thread()];
@@ -777,14 +769,14 @@ namespace {
     excludedMove = ss->excludedMove;
     posKey = excludedMove ? pos.get_exclusion_key() : pos.get_key();
     tte = TT.probe(posKey);
-    ttMove = tte ? tte->move() : MOVE_NONE;
+    ttMove = RootNode ? Rml[MultiPVIteration].pv[0] : tte ? tte->move() : MOVE_NONE;
 
     // At PV nodes we check for exact scores, while at non-PV nodes we check for
     // a fail high/low. Biggest advantage at probing at PV nodes is to have a
     // smooth experience in analysis mode. We don't probe at Root nodes otherwise
     // we should also update RootMoveList to avoid bogus output.
     if (!RootNode && tte && (PvNode ? tte->depth() >= depth && tte->type() == VALUE_TYPE_EXACT
-                                    : ok_to_use_TT(tte, depth, beta, ss->ply)))
+                                    : can_return_tt(tte, depth, beta, ss->ply)))
     {
         TT.refresh(tte);
         ss->bestMove = ttMove; // Can be MOVE_NONE
@@ -948,7 +940,7 @@ namespace {
 split_point_start: // At split points actual search starts from here
 
     // Initialize a MovePicker object for the current position
-    MovePickerExt<NT> mp(pos, RootNode ? Rml[MultiPVIteration].pv[0] : ttMove, depth, H, ss, PvNode ? -VALUE_INFINITE : beta);
+    MovePickerExt<SpNode> mp(pos, ttMove, depth, H, ss, PvNode ? -VALUE_INFINITE : beta);
     CheckInfo ci(pos);
     ss->bestMove = MOVE_NONE;
     futilityBase = ss->eval + ss->evalMargin;
@@ -978,7 +970,7 @@ split_point_start: // At split points actual search starts from here
 
       // At root obey the "searchmoves" option and skip moves not listed in Root Move List.
       // Also in MultiPV mode we skip moves which already have got an exact score
-      // in previous MultiPV Iteration.
+      // in previous MultiPV Iteration. Finally any illegal move is skipped here.
       if (RootNode && !Rml.find(move, MultiPVIteration))
           continue;
 
@@ -1002,22 +994,15 @@ split_point_start: // At split points actual search starts from here
           // Save the current node count before the move is searched
           nodes = pos.nodes_searched();
 
-          // If it's time to send nodes info, do it here where we have the
-          // correct accumulated node counts searched by each thread.
-          if (SendSearchedNodes)
-          {
-              SendSearchedNodes = false;
-              cout << "info" << speed_to_uci(pos.nodes_searched()) << endl;
-          }
-
           // For long searches send current move info to GUI
-          if (current_search_time() > 2000)
+          if (pos.thread() == 0 && current_search_time() > 2000)
               cout << "info" << depth_to_uci(depth)
-                   << " currmove " << move << " currmovenumber " << moveCount + MultiPVIteration << endl;
+                   << " currmove " << move
+                   << " currmovenumber " << moveCount + MultiPVIteration << endl;
       }
 
       // At Root and at first iteration do a PV search on all the moves to score root moves
-      isPvMove = (PvNode && moveCount <= ((RootNode && depth <= ONE_PLY) ? MAX_MOVES : 1));
+      isPvMove = (PvNode && moveCount <= (RootNode && depth <= ONE_PLY ? MAX_MOVES : 1));
       givesCheck = pos.move_gives_check(move, ci);
       captureOrPromotion = pos.move_is_capture_or_promotion(move);
 
@@ -1076,19 +1061,19 @@ split_point_start: // At split points actual search starts from here
           // We illogically ignore reduction condition depth >= 3*ONE_PLY for predicted depth,
           // but fixing this made program slightly weaker.
           Depth predictedDepth = newDepth - reduction<PvNode>(depth, moveCount);
-          futilityValueScaled =  futilityBase + futility_margin(predictedDepth, moveCount)
-                               + H.gain(pos.piece_on(move_from(move)), move_to(move));
+          futilityValue =  futilityBase + futility_margin(predictedDepth, moveCount)
+                         + H.gain(pos.piece_on(move_from(move)), move_to(move));
 
-          if (futilityValueScaled < beta)
+          if (futilityValue < beta)
           {
               if (SpNode)
               {
                   lock_grab(&(sp->lock));
-                  if (futilityValueScaled > sp->bestValue)
-                      sp->bestValue = bestValue = futilityValueScaled;
+                  if (futilityValue > sp->bestValue)
+                      sp->bestValue = bestValue = futilityValue;
               }
-              else if (futilityValueScaled > bestValue)
-                  bestValue = futilityValueScaled;
+              else if (futilityValue > bestValue)
+                  bestValue = futilityValue;
 
               continue;
           }
@@ -1177,36 +1162,12 @@ split_point_start: // At split points actual search starts from here
           alpha = sp->alpha;
       }
 
-      if (value > bestValue)
-      {
-          bestValue = value;
-          ss->bestMove = move;
-
-          if (  !RootNode
-              && PvNode
-              && value > alpha
-              && value < beta) // We want always alpha < beta
-              alpha = value;
-
-          if (SpNode && !thread.cutoff_occurred())
-          {
-              sp->bestValue = value;
-              sp->ss->bestMove = move;
-              sp->alpha = alpha;
-              sp->is_betaCutoff = (value >= beta);
-          }
-      }
-
-      if (RootNode)
+      // Finished searching the move. If StopRequest is true, the search
+      // was aborted because the user interrupted the search or because we
+      // ran out of time. In this case, the return value of the search cannot
+      // be trusted, and we don't update the best move and/or PV.
+      if (RootNode && !StopRequest)
       {
-          // Finished searching the move. If StopRequest is true, the search
-          // was aborted because the user interrupted the search or because we
-          // ran out of time. In this case, the return value of the search cannot
-          // be trusted, and we break out of the loop without updating the best
-          // move and/or PV.
-          if (StopRequest)
-              break;
-
           // Remember searched nodes counts for this move
           RootMove* rm = Rml.find(move);
           rm->nodes += pos.nodes_searched() - nodes;
@@ -1223,10 +1184,6 @@ split_point_start: // At split points actual search starts from here
               // the best move changes frequently, we allocate some more time.
               if (!isPvMove && MultiPV == 1)
                   Rml.bestMoveChanges++;
-
-              // Update alpha.
-              if (value > alpha)
-                  alpha = value;
           }
           else
               // All other moves but the PV are set to the lowest value, this
@@ -1236,16 +1193,34 @@ split_point_start: // At split points actual search starts from here
 
       } // RootNode
 
+      if (value > bestValue)
+      {
+          bestValue = value;
+          ss->bestMove = move;
+
+          if (   PvNode
+              && value > alpha
+              && value < beta) // We want always alpha < beta
+              alpha = value;
+
+          if (SpNode && !thread.cutoff_occurred())
+          {
+              sp->bestValue = value;
+              sp->ss->bestMove = move;
+              sp->alpha = alpha;
+              sp->is_betaCutoff = (value >= beta);
+          }
+      }
+
       // Step 19. Check for split
-      if (   !RootNode
-          && !SpNode
+      if (   !SpNode
           && depth >= Threads.min_split_depth()
           && bestValue < beta
           && Threads.available_slave_exists(pos.thread())
           && !StopRequest
           && !thread.cutoff_occurred())
-          Threads.split<FakeSplit>(pos, ss, &alpha, beta, &bestValue, depth,
-                                   threatMove, moveCount, &mp, PvNode);
+          bestValue = Threads.split<FakeSplit>(pos, ss, alpha, beta, bestValue, depth,
+                                               threatMove, moveCount, &mp, NT);
     }
 
     // Step 20. Check for mate and stalemate
@@ -1314,6 +1289,7 @@ split_point_start: // At split points actual search starts from here
     bool inCheck, enoughMaterial, givesCheck, evasionPrunable;
     const TTEntry* tte;
     Depth ttDepth;
+    ValueType vt;
     Value oldAlpha = alpha;
 
     ss->bestMove = ss->currentMove = MOVE_NONE;
@@ -1334,7 +1310,7 @@ split_point_start: // At split points actual search starts from here
     tte = TT.probe(pos.get_key());
     ttMove = (tte ? tte->move() : MOVE_NONE);
 
-    if (!PvNode && tte && ok_to_use_TT(tte, ttDepth, beta, ss->ply))
+    if (!PvNode && tte && can_return_tt(tte, ttDepth, beta, ss->ply))
     {
         ss->bestMove = ttMove; // Can be MOVE_NONE
         return value_from_tt(tte->value(), ss->ply);
@@ -1384,7 +1360,7 @@ split_point_start: // At split points actual search starts from here
     CheckInfo ci(pos);
 
     // Loop through the moves until no moves remain or a beta cutoff occurs
-    while (   alpha < beta
+    while (   bestValue < beta
            && (move = mp.get_next_move()) != MOVE_NONE)
     {
       assert(move_is_ok(move));
@@ -1404,10 +1380,11 @@ split_point_start: // At split points actual search starts from here
                          + piece_value_endgame(pos.piece_on(move_to(move)))
                          + (move_is_ep(move) ? PawnValueEndgame : VALUE_ZERO);
 
-          if (futilityValue < alpha)
+          if (futilityValue < beta)
           {
               if (futilityValue > bestValue)
                   bestValue = futilityValue;
+
               continue;
           }
 
@@ -1466,11 +1443,12 @@ split_point_start: // At split points actual search starts from here
       if (value > bestValue)
       {
           bestValue = value;
-          if (value > alpha)
-          {
+          ss->bestMove = move;
+
+          if (   PvNode
+              && value > alpha
+              && value < beta) // We want always alpha < beta
               alpha = value;
-              ss->bestMove = move;
-          }
        }
     }
 
@@ -1480,8 +1458,11 @@ split_point_start: // At split points actual search starts from here
         return value_mated_in(ss->ply);
 
     // Update transposition table
-    ValueType vt = (bestValue <= oldAlpha ? VALUE_TYPE_UPPER : bestValue >= beta ? VALUE_TYPE_LOWER : VALUE_TYPE_EXACT);
-    TT.store(pos.get_key(), value_to_tt(bestValue, ss->ply), vt, ttDepth, ss->bestMove, ss->eval, evalMargin);
+    move = bestValue <= oldAlpha ? MOVE_NONE : ss->bestMove;
+    vt   = bestValue <= oldAlpha ? VALUE_TYPE_UPPER
+         : bestValue >= beta ? VALUE_TYPE_LOWER : VALUE_TYPE_EXACT;
+
+    TT.store(pos.get_key(), value_to_tt(bestValue, ss->ply), vt, ttDepth, move, ss->eval, evalMargin);
 
     assert(bestValue > -VALUE_INFINITE && bestValue < VALUE_INFINITE);
 
@@ -1669,10 +1650,10 @@ split_point_start: // At split points actual search starts from here
   }
 
 
-  // ok_to_use_TT() returns true if a transposition table score
-  // can be used at a given point in search.
+  // can_return_tt() returns true if a transposition table score
+  // can be used to cut-off at a given point in search.
 
-  bool ok_to_use_TT(const TTEntry* tte, Depth depth, Value beta, int ply) {
+  bool can_return_tt(const TTEntry* tte, Depth depth, Value beta, int ply) {
 
     Value v = value_from_tt(tte->value(), ply);
 
@@ -1961,9 +1942,6 @@ split_point_start: // At split points actual search starts from here
 
         dbg_print_mean();
         dbg_print_hit_rate();
-
-        // Send info on searched nodes as soon as we return to root
-        SendSearchedNodes = true;
     }
 
     // Should we stop the search?
@@ -2221,10 +2199,14 @@ void ThreadsManager::idle_loop(int threadID, SplitPoint* sp) {
           memcpy(ss, tsp->ss - 1, 4 * sizeof(SearchStack));
           (ss+1)->sp = tsp;
 
-          if (tsp->pvNode)
+          if (tsp->nodeType == Root)
+              search<SplitPointRoot>(pos, ss+1, tsp->alpha, tsp->beta, tsp->depth);
+          else if (tsp->nodeType == PV)
               search<SplitPointPV>(pos, ss+1, tsp->alpha, tsp->beta, tsp->depth);
-          else
+          else if (tsp->nodeType == NonPV)
               search<SplitPointNonPV>(pos, ss+1, tsp->alpha, tsp->beta, tsp->depth);
+          else
+              assert(false);
 
           assert(threads[threadID].state == Thread::SEARCHING);
 
@@ -2253,8 +2235,6 @@ void ThreadsManager::idle_loop(int threadID, SplitPoint* sp) {
           // In helpful master concept a master can help only a sub-tree, and
           // because here is all finished is not possible master is booked.
           assert(threads[threadID].state == Thread::AVAILABLE);
-
-          threads[threadID].state = Thread::SEARCHING;
           return;
       }
   }