]> git.sesse.net Git - stockfish/blobdiff - src/search.cpp
Avoid a needless locking in sp_search()
[stockfish] / src / search.cpp
index 6dd854c68689c6a5c16b767eeaa9686e89e3cf8b..ec4272a9e0f9bcb7ebf601515621e34be4e632ba 100644 (file)
@@ -308,7 +308,9 @@ namespace {
   bool thread_is_available(int slave, int master);
   bool idle_thread_exists(int master);
   bool split(const Position& pos, SearchStack* ss, int ply,
-             Value *alpha, Value *beta, Value *bestValue, Depth depth, int *moves,
+             Value *alpha, Value *beta, Value *bestValue,
+             const Value futilityValue, const Value approximateValue,
+             Depth depth, int *moves,
              MovePicker *mp, Bitboard dcCandidates, int master, bool pvNode);
   void wake_sleeping_threads();
 
@@ -471,11 +473,9 @@ bool think(const Position& pos, bool infinite, bool ponder, int side_to_move,
       NodesBetweenPolls = Min(MaxNodes, 30000);
       InfiniteSearch = true; // HACK
   }
-  else if (InfiniteSearch)
-      NodesBetweenPolls = 30000;
-  else if (myTime < 1000)
+  else if (myTime && myTime < 1000)
       NodesBetweenPolls = 1000;
-  else if (myTime < 5000)
+  else if (myTime && myTime < 5000)
       NodesBetweenPolls = 5000;
   else
       NodesBetweenPolls = 30000;
@@ -1050,7 +1050,7 @@ namespace {
     EvalInfo ei;
 
     if (ply >= PLY_MAX - 1)
-        return evaluate(pos, ei, threadID);
+        return pos.is_check() ? quick_evaluate(pos) : evaluate(pos, ei, threadID);
 
     // Mate distance pruning
     Value oldAlpha = alpha;
@@ -1178,7 +1178,7 @@ namespace {
           && idle_thread_exists(threadID)
           && !AbortSearch
           && !thread_should_stop(threadID)
-          && split(pos, ss, ply, &alpha, &beta, &bestValue, depth,
+          && split(pos, ss, ply, &alpha, &beta, &bestValue, VALUE_NONE, VALUE_NONE, depth,
                    &moveCount, &mp, dcCandidates, threadID, true))
           break;
     }
@@ -1240,7 +1240,7 @@ namespace {
     EvalInfo ei;
 
     if (ply >= PLY_MAX - 1)
-        return evaluate(pos, ei, threadID);
+        return pos.is_check() ? quick_evaluate(pos) : evaluate(pos, ei, threadID);
 
     // Mate distance pruning
     if (value_mated_in(ply) >= beta)
@@ -1371,7 +1371,8 @@ namespace {
       {
           // History pruning. See ok_to_prune() definition
           if (   moveCount >= 2 + int(depth)
-              && ok_to_prune(pos, move, ss[ply].threatMove, depth))
+              && ok_to_prune(pos, move, ss[ply].threatMove, depth)
+              && bestValue > value_mated_in(PLY_MAX))
               continue;
 
           // Value based pruning
@@ -1438,7 +1439,7 @@ namespace {
           && idle_thread_exists(threadID)
           && !AbortSearch
           && !thread_should_stop(threadID)
-          && split(pos, ss, ply, &beta, &beta, &bestValue, depth, &moveCount,
+          && split(pos, ss, ply, &beta, &beta, &bestValue, futilityValue, approximateEval, depth, &moveCount,
                    &mp, dcCandidates, threadID, false))
         break;
     }
@@ -1531,8 +1532,8 @@ namespace {
     else
         staticValue = evaluate(pos, ei, threadID);
 
-    if (ply == PLY_MAX - 1)
-        return evaluate(pos, ei, threadID);
+    if (ply >= PLY_MAX - 1)
+        return pos.is_check() ? quick_evaluate(pos) : evaluate(pos, ei, threadID);
 
     // Initialize "stand pat score", and return it immediately if it is
     // at least beta.
@@ -1694,10 +1695,37 @@ namespace {
       if (    useFutilityPruning
           && !dangerous
           && !moveIsCapture
-          && !move_is_promotion(move)
-          &&  moveCount >= 2 + int(sp->depth)
-          &&  ok_to_prune(pos, move, ss[sp->ply].threatMove, sp->depth))
-        continue;
+          && !move_is_promotion(move))
+      {
+          // History pruning. See ok_to_prune() definition
+          if (   moveCount >= 2 + int(sp->depth)
+              && ok_to_prune(pos, move, ss[sp->ply].threatMove, sp->depth)
+              && sp->bestValue > value_mated_in(PLY_MAX))
+              continue;
+
+          // Value based pruning
+          if (sp->approximateEval < sp->beta)
+          {
+              if (sp->futilityValue == VALUE_NONE)
+              {
+                  EvalInfo ei;
+                  sp->futilityValue =  evaluate(pos, ei, threadID)
+                                    + FutilityMargins[int(sp->depth) - 2];
+              }
+
+              if (sp->futilityValue < sp->beta)
+              {
+                  if (sp->futilityValue > sp->bestValue) // Less then 1% of cases
+                  {
+                      lock_grab(&(sp->lock));
+                      if (sp->futilityValue > sp->bestValue)
+                          sp->bestValue = sp->futilityValue;
+                      lock_release(&(sp->lock));
+                  }
+                  continue;
+              }
+          }
+      }
 
       // Make and search the move.
       StateInfo st;
@@ -1731,21 +1759,24 @@ namespace {
           break;
 
       // New best move?
-      lock_grab(&(sp->lock));
-      if (value > sp->bestValue && !thread_should_stop(threadID))
+      if (value > sp->bestValue) // Less then 2% of cases
       {
-          sp->bestValue = value;
-          if (sp->bestValue >= sp->beta)
+          lock_grab(&(sp->lock));
+          if (value > sp->bestValue && !thread_should_stop(threadID))
           {
-              sp_update_pv(sp->parentSstack, ss, sp->ply);
-              for (int i = 0; i < ActiveThreads; i++)
-                  if (i != threadID && (i == sp->master || sp->slaves[i]))
-                      Threads[i].stop = true;
+              sp->bestValue = value;
+              if (sp->bestValue >= sp->beta)
+              {
+                  sp_update_pv(sp->parentSstack, ss, sp->ply);
+                  for (int i = 0; i < ActiveThreads; i++)
+                      if (i != threadID && (i == sp->master || sp->slaves[i]))
+                          Threads[i].stop = true;
 
-              sp->finished = true;
-        }
+                  sp->finished = true;
+              }
+          }
+          lock_release(&(sp->lock));
       }
-      lock_release(&(sp->lock));
     }
 
     lock_grab(&(sp->lock));
@@ -2757,7 +2788,8 @@ namespace {
   // splitPoint->cpus becomes 0), split() returns true.
 
   bool split(const Position& p, SearchStack* sstck, int ply,
-             Value* alpha, Value* beta, Value* bestValue, Depth depth, int* moves,
+             Value* alpha, Value* beta, Value* bestValue, const Value futilityValue,
+             const Value approximateEval, Depth depth, int* moves,
              MovePicker* mp, Bitboard dcCandidates, int master, bool pvNode) {
 
     assert(p.is_ok());
@@ -2797,6 +2829,8 @@ namespace {
     splitPoint->pvNode = pvNode;
     splitPoint->dcCandidates = dcCandidates;
     splitPoint->bestValue = *bestValue;
+    splitPoint->futilityValue = futilityValue;
+    splitPoint->approximateEval = approximateEval;
     splitPoint->master = master;
     splitPoint->mp = mp;
     splitPoint->moves = *moves;