]> git.sesse.net Git - stockfish/blobdiff - src/search.h
Simplify time management and fix 'ponder on' bug
[stockfish] / src / search.h
index e233c1c8577059dd4e416a46d829eb4621793592..2a9a37370af7b713bc730ece27ed47ae1de066c7 100644 (file)
@@ -20,7 +20,8 @@
 #ifndef SEARCH_H_INCLUDED
 #define SEARCH_H_INCLUDED
 
-#include <memory>  // For std::auto_ptr
+#include <atomic>
+#include <memory>  // For std::unique_ptr
 #include <stack>
 #include <vector>
 
@@ -28,8 +29,6 @@
 #include "position.h"
 #include "types.h"
 
-struct SplitPoint;
-
 namespace Search {
 
 /// Stack struct keeps track of the information we need to remember from nodes
@@ -37,7 +36,6 @@ namespace Search {
 /// its own array of Stack objects, indexed by the current ply.
 
 struct Stack {
-  SplitPoint* splitPoint;
   Move* pv;
   int ply;
   Move currentMove;
@@ -47,6 +45,7 @@ struct Stack {
   Depth reduction;
   Value staticEval;
   bool skipEarlyPruning;
+  int moveCount;
 };
 
 /// RootMove struct is used for moves at the root of the tree. For each root move
@@ -57,7 +56,7 @@ struct RootMove {
 
   explicit RootMove(Move m) : pv(1, m) {}
 
-  bool operator<(const RootMove& m) const { return score > m.score; } // Ascending sort
+  bool operator<(const RootMove& m) const { return m.score < score; } // Descending sort
   bool operator==(const Move& m) const { return pv[0] == m; }
   void insert_pv_in_tt(Position& pos);
   bool extract_ponder_from_tt(Position& pos);
@@ -76,7 +75,7 @@ typedef std::vector<RootMove> RootMoveVector;
 struct LimitsType {
 
   LimitsType() { // Init explicitly due to broken value-initialization of non POD in MSVC
-    nodes = time[WHITE] = time[BLACK] = inc[WHITE] = inc[BLACK] = movestogo =
+    nodes = time[WHITE] = time[BLACK] = inc[WHITE] = inc[BLACK] = npmsec = movestogo =
     depth = movetime = mate = infinite = ponder = 0;
   }
 
@@ -85,29 +84,27 @@ struct LimitsType {
   }
 
   std::vector<Move> searchmoves;
-  int time[COLOR_NB], inc[COLOR_NB], movestogo, depth, movetime, mate, infinite, ponder;
+  int time[COLOR_NB], inc[COLOR_NB], npmsec, movestogo, depth, movetime, mate, infinite, ponder;
   int64_t nodes;
+  TimePoint startTime;
 };
 
-/// The SignalsType struct stores volatile flags updated during the search
+/// The SignalsType struct stores atomic flags updated during the search
 /// typically in an async fashion e.g. to stop the search by the GUI.
 
 struct SignalsType {
-  bool stop, stopOnPonderhit, firstRootMove, failedLowAtRoot;
+  std::atomic_bool stop, stopOnPonderhit;
 };
 
 typedef std::unique_ptr<std::stack<StateInfo>> StateStackPtr;
 
-extern volatile SignalsType Signals;
+extern SignalsType Signals;
 extern LimitsType Limits;
-extern RootMoveVector RootMoves;
-extern Position RootPos;
-extern TimePoint SearchTime;
 extern StateStackPtr SetupStates;
 
 void init();
-void think();
-template<bool Root> uint64_t perft(Position& pos, Depth depth);
+void clear();
+template<bool Root = true> uint64_t perft(Position& pos, Depth depth);
 
 } // namespace Search