Color RootColor;
Time::point SearchTime;
StateStackPtr SetupStates;
- Value Contempt[2]; // [bestValue > VALUE_DRAW]
}
using std::string;
const bool FakeSplit = false;
// Different node types, used as template parameter
- enum NodeType { Root, PV, NonPV, SplitPointRoot, SplitPointPV, SplitPointNonPV };
+ enum NodeType { Root, PV, NonPV };
// Dynamic razoring margin based on depth
inline Value razor_margin(Depth d) { return Value(512 + 16 * d); }
GainsStats Gains;
MovesStats Countermoves, Followupmoves;
- template <NodeType NT>
+ template <NodeType NT, bool SpNode>
Value search(Position& pos, Stack* ss, Value alpha, Value beta, Depth depth, bool cutNode);
template <NodeType NT, bool InCheck>
{
double pvRed = 0.00 + log(double(hd)) * log(double(mc)) / 3.00;
double nonPVRed = 0.33 + log(double(hd)) * log(double(mc)) / 2.25;
- Reductions[1][1][hd][mc] = (int8_t) ( pvRed >= 1.0 ? floor( pvRed * int(ONE_PLY)) : 0);
- Reductions[0][1][hd][mc] = (int8_t) (nonPVRed >= 1.0 ? floor(nonPVRed * int(ONE_PLY)) : 0);
+ Reductions[1][1][hd][mc] = int8_t( pvRed >= 1.0 ? pvRed * int(ONE_PLY) : 0);
+ Reductions[0][1][hd][mc] = int8_t(nonPVRed >= 1.0 ? nonPVRed * int(ONE_PLY) : 0);
Reductions[1][0][hd][mc] = Reductions[1][1][hd][mc];
Reductions[0][0][hd][mc] = Reductions[0][1][hd][mc];
RootColor = RootPos.side_to_move();
TimeMgr.init(Limits, RootPos.game_ply(), RootColor);
- DrawValue[0] = DrawValue[1] = VALUE_DRAW;
- Contempt[0] = Options["Contempt Factor"] * PawnValueEg / 100; // From centipawns
- Contempt[1] = (Options["Contempt Factor"] + 12) * PawnValueEg / 100;
+ int cf = Options["Contempt Factor"] * PawnValueEg / 100; // From centipawns
+ DrawValue[ RootColor] = VALUE_DRAW - Value(cf);
+ DrawValue[~RootColor] = VALUE_DRAW + Value(cf);
if (RootMoves.empty())
{
// high/low anymore.
while (true)
{
- bestValue = search<Root>(pos, ss, alpha, beta, depth * ONE_PLY, false);
-
- DrawValue[ RootColor] = VALUE_DRAW - Contempt[bestValue > VALUE_DRAW];
- DrawValue[~RootColor] = VALUE_DRAW + Contempt[bestValue > VALUE_DRAW];
+ bestValue = search<Root, false>(pos, ss, alpha, beta, depth * ONE_PLY, false);
// Bring the best move to the front. It is critical that sorting
// is done with a stable algorithm because all the values but the
// repeat all this work again. We also don't need to store anything to the hash
// table here: This is taken care of after we return from the split point.
- template <NodeType NT>
+ template <NodeType NT, bool SpNode>
Value search(Position& pos, Stack* ss, Value alpha, Value beta, Depth depth, bool cutNode) {
- 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);
+ const bool RootNode = NT == Root;
+ const bool PvNode = NT == PV || NT == Root;
assert(-VALUE_INFINITE <= alpha && alpha < beta && beta <= VALUE_INFINITE);
assert(PvNode || (alpha == beta - 1));
pos.do_null_move(st);
(ss+1)->skipNullMove = true;
nullValue = depth-R < ONE_PLY ? -qsearch<NonPV, false>(pos, ss+1, -beta, -beta+1, DEPTH_ZERO)
- : - search<NonPV>(pos, ss+1, -beta, -beta+1, depth-R, !cutNode);
+ : - search<NonPV, false>(pos, ss+1, -beta, -beta+1, depth-R, !cutNode);
(ss+1)->skipNullMove = false;
pos.undo_null_move();
// Do verification search at high depths
ss->skipNullMove = true;
Value v = depth-R < ONE_PLY ? qsearch<NonPV, false>(pos, ss, beta-1, beta, DEPTH_ZERO)
- : search<NonPV>(pos, ss, beta-1, beta, depth-R, false);
+ : search<NonPV, false>(pos, ss, beta-1, beta, depth-R, false);
ss->skipNullMove = false;
if (v >= beta)
{
ss->currentMove = move;
pos.do_move(move, st, ci, pos.gives_check(move, ci));
- value = -search<NonPV>(pos, ss+1, -rbeta, -rbeta+1, rdepth, !cutNode);
+ value = -search<NonPV, false>(pos, ss+1, -rbeta, -rbeta+1, rdepth, !cutNode);
pos.undo_move(move);
if (value >= rbeta)
return value;
Depth d = depth - 2 * ONE_PLY - (PvNode ? DEPTH_ZERO : depth / 4);
ss->skipNullMove = true;
- search<PvNode ? PV : NonPV>(pos, ss, alpha, beta, d, true);
+ search<PvNode ? PV : NonPV, false>(pos, ss, alpha, beta, d, true);
ss->skipNullMove = false;
tte = TT.probe(posKey);
Value rBeta = ttValue - int(depth);
ss->excludedMove = move;
ss->skipNullMove = true;
- value = search<NonPV>(pos, ss, rBeta - 1, rBeta, depth / 2, cutNode);
+ value = search<NonPV, false>(pos, ss, rBeta - 1, rBeta, depth / 2, cutNode);
ss->skipNullMove = false;
ss->excludedMove = MOVE_NONE;
if (SpNode)
alpha = splitPoint->alpha;
- value = -search<NonPV>(pos, ss+1, -(alpha+1), -alpha, d, true);
+ value = -search<NonPV, false>(pos, ss+1, -(alpha+1), -alpha, d, true);
// Research at intermediate depth if reduction is very high
if (value > alpha && ss->reduction >= 4 * ONE_PLY)
{
Depth d2 = std::max(newDepth - 2 * ONE_PLY, ONE_PLY);
- value = -search<NonPV>(pos, ss+1, -(alpha+1), -alpha, d2, true);
+ value = -search<NonPV, false>(pos, ss+1, -(alpha+1), -alpha, d2, true);
}
doFullDepthSearch = (value > alpha && ss->reduction != DEPTH_ZERO);
value = newDepth < ONE_PLY ?
givesCheck ? -qsearch<NonPV, true>(pos, ss+1, -(alpha+1), -alpha, DEPTH_ZERO)
: -qsearch<NonPV, false>(pos, ss+1, -(alpha+1), -alpha, DEPTH_ZERO)
- : - search<NonPV>(pos, ss+1, -(alpha+1), -alpha, newDepth, !cutNode);
+ : - search<NonPV, false>(pos, ss+1, -(alpha+1), -alpha, newDepth, !cutNode);
}
// For PV nodes only, do a full PV search on the first move or after a fail
value = newDepth < ONE_PLY ?
givesCheck ? -qsearch<PV, true>(pos, ss+1, -beta, -alpha, DEPTH_ZERO)
: -qsearch<PV, false>(pos, ss+1, -beta, -alpha, DEPTH_ZERO)
- : - search<PV>(pos, ss+1, -beta, -alpha, newDepth, false);
+ : - search<PV, false>(pos, ss+1, -beta, -alpha, newDepth, false);
// Step 17. Undo move
pos.undo_move(move);
alpha = splitPoint->alpha;
}
- // Finished searching the move. If Signals.stop 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.
+ // Finished searching the move. If a stop or a cutoff occurred, the return
+ // value of the search cannot be trusted, and we return immediately without
+ // updating best move, PV and TT.
if (Signals.stop || thisThread->cutoff_occurred())
- return value; // To avoid returning VALUE_INFINITE
+ return VALUE_ZERO;
if (RootNode)
{
// Step 19. Check for splitting the search
if ( !SpNode
+ && Threads.size() >= 2
&& depth >= Threads.minimumSplitDepth
- && Threads.available_slave(thisThread)
+ && ( !thisThread->activeSplitPoint
+ || !thisThread->activeSplitPoint->allowLatejoin)
&& thisThread->splitPointsSize < MAX_SPLITPOINTS_PER_THREAD)
{
- assert(bestValue < beta);
+ assert(bestValue > -VALUE_INFINITE && bestValue < beta);
thisThread->split<FakeSplit>(pos, ss, alpha, beta, &bestValue, &bestMove,
depth, moveCount, &mp, NT, cutNode);
+
+ if (Signals.stop || thisThread->cutoff_occurred())
+ return VALUE_ZERO;
+
if (bestValue >= beta)
break;
}
if (SpNode)
return bestValue;
+ // Following condition would detect a stop or a cutoff set only after move
+ // loop has been completed. But in this case bestValue is valid because we
+ // have fully searched our subtree, and we can anyhow save the result in TT.
+ /*
+ if (Signals.stop || thisThread->cutoff_occurred())
+ return VALUE_DRAW;
+ */
+
// Step 20. Check for mate and stalemate
// All legal moves have been searched and if there are no legal moves, it
- // must be mate or stalemate. Note that we can have a false positive in
- // case of Signals.stop or thread.cutoff_occurred() are set, but this is
- // harmless because return value is discarded anyhow in the parent nodes.
- // If we are in a singular extension search then return a fail low score.
- // A split node has at least one move - the one tried before to be split.
+ // must be mate or stalemate. If we are in a singular extension search then
+ // return a fail low score.
if (!moveCount)
return excludedMove ? alpha
: inCheck ? mated_in(ss->ply) : DrawValue[pos.side_to_move()];
- // If we have pruned all the moves without searching return a fail-low score
- if (bestValue == -VALUE_INFINITE)
- bestValue = alpha;
-
TT.store(posKey, value_to_tt(bestValue, ss->ply),
bestValue >= beta ? BOUND_LOWER :
PvNode && bestMove ? BOUND_EXACT : BOUND_UPPER,
template <NodeType NT, bool InCheck>
Value qsearch(Position& pos, Stack* ss, Value alpha, Value beta, Depth depth) {
- const bool PvNode = (NT == PV);
+ const bool PvNode = NT == PV;
assert(NT == PV || NT == NonPV);
assert(InCheck == !!pos.checkers());
activePosition = &pos;
- switch (sp->nodeType) {
- case Root:
- search<SplitPointRoot>(pos, ss, sp->alpha, sp->beta, sp->depth, sp->cutNode);
- break;
- case PV:
- search<SplitPointPV>(pos, ss, sp->alpha, sp->beta, sp->depth, sp->cutNode);
- break;
- case NonPV:
- search<SplitPointNonPV>(pos, ss, sp->alpha, sp->beta, sp->depth, sp->cutNode);
- break;
- default:
+ if (sp->nodeType == NonPV)
+ search<NonPV, true>(pos, ss, sp->alpha, sp->beta, sp->depth, sp->cutNode);
+
+ else if (sp->nodeType == PV)
+ search<PV, true>(pos, ss, sp->alpha, sp->beta, sp->depth, sp->cutNode);
+
+ else if (sp->nodeType == Root)
+ search<Root, true>(pos, ss, sp->alpha, sp->beta, sp->depth, sp->cutNode);
+
+ else
assert(false);
- }
assert(searching);
- searching = false;
activePosition = NULL;
sp->slavesMask.reset(idx);
+ sp->allowLatejoin = false;
sp->nodes += pos.nodes_searched();
// Wake up the master thread so to allow it to return from the idle
// the sp master. Also accessing other Thread objects is unsafe because
// if we are exiting there is a chance that they are already freed.
sp->mutex.unlock();
+
+ // Try to late join to another splitpoint
+ if (Threads.size() <= 2 || !attempt_to_latejoin()) // FIXME: attempt_to_latejoin() is theoretically unsafe when were are exiting the program...
+ searching = false;
}
// If this thread is the master of a split point and all slaves have finished
}
}
+bool Thread::attempt_to_latejoin()
+{
+ SplitPoint *sp;
+ size_t i;
+ bool success = false;
+
+ for (i = 0; i < Threads.size(); ++i)
+ {
+ int size = Threads[i]->splitPointsSize; // Make a local copy to prevent size from changing under our feet.
+
+ sp = size ? &Threads[i]->splitPoints[size - 1] : NULL;
+
+ if ( sp
+ && sp->allowLatejoin
+ && available_to(Threads[i], true))
+ break;
+ }
+
+ if (i == Threads.size())
+ return false; // No suitable splitpoint found!
+
+ // Recheck conditions under lock protection
+ Threads.mutex.lock();
+ sp->mutex.lock();
+
+ if ( sp->allowLatejoin
+ && available_to(Threads[i], true))
+ {
+ activeSplitPoint = sp;
+ sp->slavesMask.set(this->idx);
+ success = true;
+ }
+
+ sp->mutex.unlock();
+ Threads.mutex.unlock();
+
+ return success;
+}
/// check_time() is called by the timer thread when the timer triggers. It is
/// used to print debug info and, more importantly, to detect when we are out of