/*
Stockfish, a UCI chess playing engine derived from Glaurung 2.1
Copyright (C) 2004-2008 Tord Romstad (Glaurung author)
/*
Stockfish, a UCI chess playing engine derived from Glaurung 2.1
Copyright (C) 2004-2008 Tord Romstad (Glaurung author)
Stockfish is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
Stockfish is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
template <NodeType NT>
Value search(Position& pos, Stack* ss, Value alpha, Value beta, Depth depth);
template <NodeType NT>
Value search(Position& pos, Stack* ss, Value alpha, Value beta, Depth depth);
void id_loop(Position& pos);
Value value_to_tt(Value v, int ply);
Value value_from_tt(Value v, int ply);
void id_loop(Position& pos);
Value value_to_tt(Value v, int ply);
Value value_from_tt(Value v, int ply);
- bool check_is_dangerous(Position& pos, Move move, Value futilityBase, Value beta);
- bool allows_move(const Position& pos, Move first, Move second);
- bool prevents_move(const Position& pos, Move first, Move second);
+ bool check_is_dangerous(const Position& pos, Move move, Value futilityBase, Value beta);
+ bool allows(const Position& pos, Move first, Move second);
+ bool refutes(const Position& pos, Move first, Move second);
string uci_pv(const Position& pos, int depth, Value alpha, Value beta);
struct Skill {
string uci_pv(const Position& pos, int depth, Value alpha, Value beta);
struct Skill {
static PolyglotBook book; // Defined static to initialize the PRNG only once
RootColor = RootPos.side_to_move();
static PolyglotBook book; // Defined static to initialize the PRNG only once
RootColor = RootPos.side_to_move();
+ // Reset the threads, still sleeping: will be wake up at split time
+ for (size_t i = 0; i < Threads.size(); i++)
+ Threads[i]->maxPly = 0;
+
+ Threads.sleepWhileIdle = Options["Use Sleeping Threads"];
// Set best timer interval to avoid lagging under time pressure. Timer is
// used to check for remaining available thinking time.
// Set best timer interval to avoid lagging under time pressure. Timer is
// used to check for remaining available thinking time.
- if (Limits.use_time_management())
- Threads.set_timer(std::min(100, std::max(TimeMgr.available_time() / 16,
- TimerResolution)));
- else if (Limits.nodes)
- Threads.set_timer(2 * TimerResolution);
- else
- Threads.set_timer(100);
+ Threads.timer->msec =
+ Limits.use_time_management() ? std::min(100, std::max(TimeMgr.available_time() / 16, TimerResolution)) :
+ Limits.nodes ? 2 * TimerResolution
+ : 100;
+
+ Threads.timer->notify_one(); // Wake up the recurring timer
- // but if we are pondering or in infinite search, we shouldn't print the best
- // move before we are told to do so.
+ // but if we are pondering or in infinite search, according to UCI protocol,
+ // we shouldn't print the best move before the GUI sends a "stop" or "ponderhit"
+ // command. We simply wait here until GUI sends one of those commands (that
+ // raise Signals.stop).
// we want to keep the same order for all the moves but the new
// PV that goes to the front. Note that in case of MultiPV search
// the already searched PV lines are preserved.
// we want to keep the same order for all the moves but the new
// PV that goes to the front. Note that in case of MultiPV search
// the already searched PV lines are preserved.
// Write PV back to transposition table in case the relevant
// entries have been overwritten during the search.
// Write PV back to transposition table in case the relevant
// entries have been overwritten during the search.
if (PVIdx + 1 == PVSize || Time::now() - SearchTime > 3000)
sync_cout << uci_pv(pos, depth, alpha, beta) << sync_endl;
}
if (PVIdx + 1 == PVSize || Time::now() - SearchTime > 3000)
sync_cout << uci_pv(pos, depth, alpha, beta) << sync_endl;
}
Value bestValue, value, ttValue;
Value eval, nullValue, futilityValue;
bool inCheck, givesCheck, pvMove, singularExtensionNode;
Value bestValue, value, ttValue;
Value eval, nullValue, futilityValue;
bool inCheck, givesCheck, pvMove, singularExtensionNode;
- bool captureOrPromotion, dangerous, doFullDepthSearch, threatExtension;
+ bool captureOrPromotion, dangerous, doFullDepthSearch;
int moveCount, playedMoveCount;
// Step 1. Initialize node
Thread* thisThread = pos.this_thread();
moveCount = playedMoveCount = 0;
int moveCount, playedMoveCount;
// Step 1. Initialize node
Thread* thisThread = pos.this_thread();
moveCount = playedMoveCount = 0;
return DrawValue[pos.side_to_move()];
// Step 3. Mate distance pruning. Even if we mate at the next move our score
return DrawValue[pos.side_to_move()];
// Step 3. Mate distance pruning. Even if we mate at the next move our score
- if ( (ss->staticEval = eval = tte->static_value()) == VALUE_NONE
- ||(ss->evalMargin = tte->static_value_margin()) == VALUE_NONE)
+ if ( (ss->staticEval = eval = tte->eval_value()) == VALUE_NONE
+ ||(ss->evalMargin = tte->eval_margin()) == VALUE_NONE)
eval = ss->staticEval = evaluate(pos, ss->evalMargin);
// Can ttValue be used as a better position evaluation?
eval = ss->staticEval = evaluate(pos, ss->evalMargin);
// Can ttValue be used as a better position evaluation?
(ss+1)->skipNullMove = true;
nullValue = depth-R < ONE_PLY ? -qsearch<NonPV, false>(pos, ss+1, -beta, -alpha, DEPTH_ZERO)
: - search<NonPV>(pos, ss+1, -beta, -alpha, depth-R);
(ss+1)->skipNullMove = false;
(ss+1)->skipNullMove = true;
nullValue = depth-R < ONE_PLY ? -qsearch<NonPV, false>(pos, ss+1, -beta, -alpha, DEPTH_ZERO)
: - search<NonPV>(pos, ss+1, -beta, -alpha, depth-R);
(ss+1)->skipNullMove = false;
// The null move failed low, which means that we may be faced with
// some kind of threat. If the previous move was reduced, check if
// the move that refuted the null move was somehow connected to the
// The null move failed low, which means that we may be faced with
// some kind of threat. If the previous move was reduced, check if
// the move that refuted the null move was somehow connected to the
- // move which was reduced. If a connection is found extend moves that
- // defend against threat.
+ // move which was reduced. If a connection is found, return a fail
+ // low score (which will cause the reduced move to fail high in the
+ // parent node, which will trigger a re-search with full depth).
- && allows_move(pos, (ss-1)->currentMove, threatMove))
- threatExtension = true;
+ && allows(pos, (ss-1)->currentMove, threatMove))
+ return beta - 1;
- MovePicker mp(pos, ttMove, H, pos.captured_piece_type());
+ MovePicker mp(pos, ttMove, Hist, pos.captured_piece_type());
- MovePicker mp(pos, ttMove, depth, H, ss, PvNode ? -VALUE_INFINITE : beta);
+ MovePicker mp(pos, ttMove, depth, Hist, ss, PvNode ? -VALUE_INFINITE : beta);
CheckInfo ci(pos);
value = bestValue; // Workaround a bogus 'uninitialized' warning under gcc
singularExtensionNode = !RootNode
CheckInfo ci(pos);
value = bestValue; // Workaround a bogus 'uninitialized' warning under gcc
singularExtensionNode = !RootNode
// Move count based pruning
if ( depth < 16 * ONE_PLY
&& moveCount >= FutilityMoveCounts[depth]
// Move count based pruning
if ( depth < 16 * ONE_PLY
&& moveCount >= FutilityMoveCounts[depth]
// but fixing this made program slightly weaker.
Depth predictedDepth = newDepth - reduction<PvNode>(depth, moveCount);
futilityValue = ss->staticEval + ss->evalMargin + futility_margin(predictedDepth, moveCount)
// but fixing this made program slightly weaker.
Depth predictedDepth = newDepth - reduction<PvNode>(depth, moveCount);
futilityValue = ss->staticEval + ss->evalMargin + futility_margin(predictedDepth, moveCount)
{
ss->reduction = reduction<PvNode>(depth, moveCount);
Depth d = std::max(newDepth - ss->reduction, ONE_PLY);
{
ss->reduction = reduction<PvNode>(depth, moveCount);
Depth d = std::max(newDepth - ss->reduction, ONE_PLY);
- && depth >= Threads.min_split_depth()
- && Threads.available_slave_exists(thisThread))
+ && depth >= Threads.minimumSplitDepth
+ && Threads.available_slave(thisThread)
+ && thisThread->splitPointsSize < MAX_SPLITPOINTS_PER_THREAD)
- bestValue = Threads.split<FakeSplit>(pos, ss, alpha, beta, bestValue, &bestMove,
- depth, threatMove, moveCount, mp, NT);
+ thisThread->split<FakeSplit>(pos, ss, alpha, beta, &bestValue, &bestMove,
+ depth, threatMove, moveCount, &mp, NT);
// Decrease history of all the other played non-capture moves
for (int i = 0; i < playedMoveCount - 1; i++)
{
Move m = movesSearched[i];
// Decrease history of all the other played non-capture moves
for (int i = 0; i < playedMoveCount - 1; i++)
{
Move m = movesSearched[i];
return DrawValue[pos.side_to_move()];
// Transposition table lookup. At PV nodes, we don't use the TT for
return DrawValue[pos.side_to_move()];
// Transposition table lookup. At PV nodes, we don't use the TT for
- if ( (ss->staticEval = bestValue = tte->static_value()) == VALUE_NONE
- ||(ss->evalMargin = tte->static_value_margin()) == VALUE_NONE)
+ if ( (ss->staticEval = bestValue = tte->eval_value()) == VALUE_NONE
+ ||(ss->evalMargin = tte->eval_margin()) == VALUE_NONE)
// to search the moves. Because the depth is <= 0 here, only captures,
// queen promotions and checks (only if depth >= DEPTH_QS_CHECKS) will
// be generated.
// to search the moves. Because the depth is <= 0 here, only captures,
// queen promotions and checks (only if depth >= DEPTH_QS_CHECKS) will
// be generated.
- MovePicker mp(pos, ttMove, depth, H, to_sq((ss-1)->currentMove));
+ MovePicker mp(pos, ttMove, depth, Hist, to_sq((ss-1)->currentMove));
- bool check_is_dangerous(Position& pos, Move move, Value futilityBase, Value beta)
+ bool check_is_dangerous(const Position& pos, Move move, Value futilityBase, Value beta)
- // allows_move() tests whether the move at previous ply (first) somehow makes a
- // second move possible, for instance if the moving piece is the same in both
- // moves. Normally the second move is the threat move (the best move returned
+ // allows() tests whether the 'first' move at previous ply somehow makes the
+ // 'second' move possible, for instance if the moving piece is the same in
+ // both moves. Normally the second move is the threat (the best move returned
- // prevents_move() tests whether a move (first) is able to defend against an
- // opponent's move (second). In this case will not be pruned. Normally the
- // second move is the threat move (the best move returned from a null search
- // that fails low).
+ // refutes() tests whether a 'first' move is able to defend against a 'second'
+ // opponent's move. In this case will not be pruned. Normally the second move
+ // is the threat (the best move returned from a null search that fails low).
&& pos.is_pseudo_legal(m = tte->move()) // Local copy, TT could change
&& pos.pl_move_is_legal(m, pos.pinned_pieces())
&& ply < MAX_PLY
&& pos.is_pseudo_legal(m = tte->move()) // Local copy, TT could change
&& pos.pl_move_is_legal(m, pos.pinned_pieces())
&& ply < MAX_PLY
- // Pointer 'sp_master', if non-NULL, points to the active SplitPoint
- // object for which the thread is the master.
- const SplitPoint* sp_master = splitPointsCnt ? curSplitPoint : NULL;
+ // Pointer 'this_sp' is not null only if we are called from split(), and not
+ // at the thread creation. So it means we are the split point's master.
+ const SplitPoint* this_sp = splitPointsSize ? activeSplitPoint : NULL;
- // If this thread is the master of a split point and all slaves have
- // finished their work at this split point, return from the idle loop.
- while (!sp_master || sp_master->slavesMask)
+ // If this thread is the master of a split point and all slaves have finished
+ // their work at this split point, return from the idle loop.
+ while (!this_sp || this_sp->slavesMask)
- // If we are not searching, wait for a condition to be signaled
- // instead of wasting CPU time polling for work.
- while ( do_sleep
- || do_exit
- || (!is_searching && Threads.use_sleeping_threads()))
+ // If we are not searching, wait for a condition to be signaled instead of
+ // wasting CPU time polling for work.
+ while ((!searching && Threads.sleepWhileIdle) || exit)
- // If we are master and all slaves have finished don't go to sleep
- if (sp_master && !sp_master->slavesMask)
+ // If we are master and all slaves have finished then exit idle_loop
+ if (this_sp && !this_sp->slavesMask)
// Do sleep after retesting sleep conditions under lock protection, in
// particular we need to avoid a deadlock in case a master thread has,
// Do sleep after retesting sleep conditions under lock protection, in
// particular we need to avoid a deadlock in case a master thread has,
- // in the meanwhile, allocated us and sent the wake_up() call before we
- // had the chance to grab the lock.
- if (do_sleep || !is_searching)
+ // in the meanwhile, allocated us and sent the notify_one() call before
+ // we had the chance to grab the lock.
+ if (!searching && !exit)
sleepCondition.wait(mutex);
mutex.unlock();
}
// If this thread has been assigned work, launch a search
sleepCondition.wait(mutex);
mutex.unlock();
}
// If this thread has been assigned work, launch a search
search<SplitPointRoot>(pos, ss+1, sp->alpha, sp->beta, sp->depth);
search<SplitPointRoot>(pos, ss+1, sp->alpha, sp->beta, sp->depth);
search<SplitPointPV>(pos, ss+1, sp->alpha, sp->beta, sp->depth);
search<SplitPointPV>(pos, ss+1, sp->alpha, sp->beta, sp->depth);
search<SplitPointNonPV>(pos, ss+1, sp->alpha, sp->beta, sp->depth);
search<SplitPointNonPV>(pos, ss+1, sp->alpha, sp->beta, sp->depth);
- // Wake up master thread so to allow it to return from the idle loop in
- // case we are the last slave of the split point.
- if ( Threads.use_sleeping_threads()
- && this != sp->master
+ // Wake up master thread so to allow it to return from the idle loop
+ // in case we are the last slave of the split point.
+ if ( Threads.sleepWhileIdle
+ && this != sp->masterThread
nodes = RootPos.nodes_searched();
// Loop across all split points and sum accumulated SplitPoint nodes plus
nodes = RootPos.nodes_searched();
// Loop across all split points and sum accumulated SplitPoint nodes plus