1
0
Fork 0
mirror of https://github.com/sockspls/badfish synced 2025-04-30 08:43:09 +00:00

Unify sp_search() and search() step 2

Modify search() to be able to handle split points

No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
This commit is contained in:
Marco Costalba 2010-10-16 10:01:45 +01:00
parent 37055ad002
commit f7722d4de7

View file

@ -284,9 +284,14 @@ namespace {
Value id_loop(const Position& pos, Move searchMoves[]); Value id_loop(const Position& pos, Move searchMoves[]);
Value root_search(Position& pos, SearchStack* ss, Move* pv, RootMoveList& rml, Value* alphaPtr, Value* betaPtr); Value root_search(Position& pos, SearchStack* ss, Move* pv, RootMoveList& rml, Value* alphaPtr, Value* betaPtr);
template <NodeType PvNode> template <NodeType PvNode, bool SplitPoint>
Value search(Position& pos, SearchStack* ss, Value alpha, Value beta, Depth depth, int ply); Value search(Position& pos, SearchStack* ss, Value alpha, Value beta, Depth depth, int ply);
template <NodeType PvNode>
inline Value search(Position& pos, SearchStack* ss, Value alpha, Value beta, Depth depth, int ply) {
return search<PvNode, false>(pos, ss, alpha, beta, depth, ply);
}
template <NodeType PvNode> template <NodeType PvNode>
void sp_search(Position& pos, SearchStack* ss, Value dumy, Value beta, Depth depth, int ply); void sp_search(Position& pos, SearchStack* ss, Value dumy, Value beta, Depth depth, int ply);
@ -960,7 +965,7 @@ namespace {
// search<>() is the main search function for both PV and non-PV nodes // search<>() is the main search function for both PV and non-PV nodes
template <NodeType PvNode> template <NodeType PvNode, bool SplitPoint>
Value search(Position& pos, SearchStack* ss, Value alpha, Value beta, Depth depth, int ply) { Value search(Position& pos, SearchStack* ss, Value alpha, Value beta, Depth depth, int ply) {
assert(alpha >= -VALUE_INFINITE && alpha <= VALUE_INFINITE); assert(alpha >= -VALUE_INFINITE && alpha <= VALUE_INFINITE);
@ -983,6 +988,16 @@ namespace {
int threadID = pos.thread(); int threadID = pos.thread();
refinedValue = bestValue = value = -VALUE_INFINITE; refinedValue = bestValue = value = -VALUE_INFINITE;
oldAlpha = alpha; oldAlpha = alpha;
isCheck = pos.is_check();
if (SplitPoint)
{
tte = NULL;
ttMove = excludedMove = MOVE_NONE;
threatMove = ss->sp->threatMove;
mateThreat = ss->sp->mateThreat;
goto split_start;
}
// Step 1. Initialize node and poll. Polling can abort search // Step 1. Initialize node and poll. Polling can abort search
ThreadsMgr.incrementNodeCounter(threadID); ThreadsMgr.incrementNodeCounter(threadID);
@ -1037,7 +1052,6 @@ namespace {
// Step 5. Evaluate the position statically and // Step 5. Evaluate the position statically and
// update gain statistics of parent move. // update gain statistics of parent move.
isCheck = pos.is_check();
if (isCheck) if (isCheck)
ss->eval = evalMargin = VALUE_NONE; ss->eval = evalMargin = VALUE_NONE;
else if (tte) else if (tte)
@ -1168,13 +1182,17 @@ namespace {
if (PvNode) if (PvNode)
mateThreat = pos.has_mate_threat(); mateThreat = pos.has_mate_threat();
split_start:
// Initialize a MovePicker object for the current position // Initialize a MovePicker object for the current position
MovePicker mp = MovePicker(pos, ttMove, depth, H, ss, (PvNode ? -VALUE_INFINITE : beta)); MovePicker mpBase = MovePicker(pos, ttMove, depth, H, ss, (PvNode ? -VALUE_INFINITE : beta));
MovePicker& mp = SplitPoint ? *ss->sp->mp : mpBase;
CheckInfo ci(pos); CheckInfo ci(pos);
ss->bestMove = MOVE_NONE; ss->bestMove = MOVE_NONE;
singleEvasion = isCheck && mp.number_of_evasions() == 1; singleEvasion = SplitPoint ? false : isCheck && mp.number_of_evasions() == 1;
futilityBase = ss->eval + evalMargin; futilityBase = SplitPoint ? ss->eval : ss->eval + evalMargin;
singularExtensionNode = depth >= SingularExtensionDepth[PvNode] singularExtensionNode = !SplitPoint
&& depth >= SingularExtensionDepth[PvNode]
&& tte && tte
&& tte->move() && tte->move()
&& !excludedMove // Do not allow recursive singular extension search && !excludedMove // Do not allow recursive singular extension search
@ -1183,10 +1201,22 @@ namespace {
// Step 10. Loop through moves // Step 10. Loop through moves
// Loop through all legal moves until no moves remain or a beta cutoff occurs // Loop through all legal moves until no moves remain or a beta cutoff occurs
if (SplitPoint)
{
lock_grab(&(ss->sp->lock));
bestValue = ss->sp->bestValue;
}
while ( bestValue < beta while ( bestValue < beta
&& (move = mp.get_next_move()) != MOVE_NONE && (move = mp.get_next_move()) != MOVE_NONE
&& !ThreadsMgr.thread_should_stop(threadID)) && !ThreadsMgr.thread_should_stop(threadID))
{ {
if (SplitPoint)
{
moveCount = ++ss->sp->moveCount;
lock_release(&(ss->sp->lock));
}
assert(move_is_ok(move)); assert(move_is_ok(move));
if (move == excludedMove) if (move == excludedMove)
@ -1238,8 +1268,12 @@ namespace {
// Move count based pruning // Move count based pruning
if ( moveCount >= futility_move_count(depth) if ( moveCount >= futility_move_count(depth)
&& !(threatMove && connected_threat(pos, move, threatMove)) && !(threatMove && connected_threat(pos, move, threatMove))
&& bestValue > value_mated_in(PLY_MAX)) && bestValue > value_mated_in(PLY_MAX)) // FIXME bestValue is racy
{
if (SplitPoint)
lock_grab(&(ss->sp->lock));
continue; continue;
}
// Value based pruning // Value based pruning
// We illogically ignore reduction condition depth >= 3*ONE_PLY for predicted depth, // We illogically ignore reduction condition depth >= 3*ONE_PLY for predicted depth,
@ -1250,7 +1284,13 @@ namespace {
if (futilityValueScaled < beta) if (futilityValueScaled < beta)
{ {
if (futilityValueScaled > bestValue) if (SplitPoint)
{
lock_grab(&(ss->sp->lock));
if (futilityValueScaled > ss->sp->bestValue)
ss->sp->bestValue = futilityValueScaled;
}
else if (futilityValueScaled > bestValue)
bestValue = futilityValueScaled; bestValue = futilityValueScaled;
continue; continue;
} }
@ -1261,7 +1301,7 @@ namespace {
// Step extra. pv search (only in PV nodes) // Step extra. pv search (only in PV nodes)
// The first move in list is the expected PV // The first move in list is the expected PV
if (PvNode && moveCount == 1) if (!SplitPoint && PvNode && moveCount == 1)
value = newDepth < ONE_PLY ? -qsearch<PV>(pos, ss+1, -beta, -alpha, DEPTH_ZERO, ply+1) value = newDepth < ONE_PLY ? -qsearch<PV>(pos, ss+1, -beta, -alpha, DEPTH_ZERO, ply+1)
: - search<PV>(pos, ss+1, -beta, -alpha, newDepth, ply+1); : - search<PV>(pos, ss+1, -beta, -alpha, newDepth, ply+1);
else else
@ -1279,6 +1319,7 @@ namespace {
ss->reduction = reduction<PvNode>(depth, moveCount); ss->reduction = reduction<PvNode>(depth, moveCount);
if (ss->reduction) if (ss->reduction)
{ {
alpha = SplitPoint ? ss->sp->alpha : alpha;
Depth d = newDepth - ss->reduction; Depth d = newDepth - ss->reduction;
value = d < ONE_PLY ? -qsearch<NonPV>(pos, ss+1, -(alpha+1), -alpha, DEPTH_ZERO, ply+1) value = d < ONE_PLY ? -qsearch<NonPV>(pos, ss+1, -(alpha+1), -alpha, DEPTH_ZERO, ply+1)
: - search<NonPV>(pos, ss+1, -(alpha+1), -alpha, d, ply+1); : - search<NonPV>(pos, ss+1, -(alpha+1), -alpha, d, ply+1);
@ -1294,6 +1335,7 @@ namespace {
assert(newDepth - ONE_PLY >= ONE_PLY); assert(newDepth - ONE_PLY >= ONE_PLY);
ss->reduction = ONE_PLY; ss->reduction = ONE_PLY;
alpha = SplitPoint ? ss->sp->alpha : alpha;
value = -search<NonPV>(pos, ss+1, -(alpha+1), -alpha, newDepth-ss->reduction, ply+1); value = -search<NonPV>(pos, ss+1, -(alpha+1), -alpha, newDepth-ss->reduction, ply+1);
doFullDepthSearch = (value > alpha); doFullDepthSearch = (value > alpha);
} }
@ -1303,6 +1345,7 @@ namespace {
// Step 15. Full depth search // Step 15. Full depth search
if (doFullDepthSearch) if (doFullDepthSearch)
{ {
alpha = SplitPoint ? ss->sp->alpha : alpha;
value = newDepth < ONE_PLY ? -qsearch<NonPV>(pos, ss+1, -(alpha+1), -alpha, DEPTH_ZERO, ply+1) value = newDepth < ONE_PLY ? -qsearch<NonPV>(pos, ss+1, -(alpha+1), -alpha, DEPTH_ZERO, ply+1)
: - search<NonPV>(pos, ss+1, -(alpha+1), -alpha, newDepth, ply+1); : - search<NonPV>(pos, ss+1, -(alpha+1), -alpha, newDepth, ply+1);
@ -1321,11 +1364,21 @@ namespace {
assert(value > -VALUE_INFINITE && value < VALUE_INFINITE); assert(value > -VALUE_INFINITE && value < VALUE_INFINITE);
// Step 17. Check for new best move // Step 17. Check for new best move
if (value > bestValue) if (SplitPoint)
{
lock_grab(&(ss->sp->lock));
bestValue = ss->sp->bestValue;
alpha = ss->sp->alpha;
}
if (value > bestValue && !(SplitPoint && ThreadsMgr.thread_should_stop(threadID)))
{ {
bestValue = value; bestValue = value;
if (value > alpha) if (value > alpha)
{ {
if (SplitPoint && (!PvNode || value >= beta))
ss->sp->stopRequest = true;
if (PvNode && value < beta) // We want always alpha < beta if (PvNode && value < beta) // We want always alpha < beta
alpha = value; alpha = value;
@ -1334,10 +1387,17 @@ namespace {
ss->bestMove = move; ss->bestMove = move;
} }
if (SplitPoint)
{
ss->sp->bestValue = bestValue;
ss->sp->alpha = alpha;
ss->sp->parentSstack->bestMove = ss->bestMove;
}
} }
// Step 18. Check for split // Step 18. Check for split
if ( depth >= MinimumSplitDepth if ( !SplitPoint
&& depth >= MinimumSplitDepth
&& ThreadsMgr.active_threads() > 1 && ThreadsMgr.active_threads() > 1
&& bestValue < beta && bestValue < beta
&& ThreadsMgr.available_thread_exists(threadID) && ThreadsMgr.available_thread_exists(threadID)
@ -1348,6 +1408,14 @@ namespace {
threatMove, mateThreat, moveCount, &mp, PvNode); threatMove, mateThreat, moveCount, &mp, PvNode);
} }
if (SplitPoint)
{
/* Here we have the lock still grabbed */
ss->sp->slaves[threadID] = 0;
lock_release(&(ss->sp->lock));
return bestValue;
}
// Step 19. Check for mate and stalemate // Step 19. Check for mate and stalemate
// All legal moves have been searched and if there are // All legal moves have been searched and if there are
// no legal moves, it must be mate or stalemate. // no legal moves, it must be mate or stalemate.
@ -1567,7 +1635,7 @@ namespace {
SearchStack* ss = sp->sstack[threadID] + 1; SearchStack* ss = sp->sstack[threadID] + 1;
ss->sp = sp; ss->sp = sp;
sp_search<PvNode>(pos, ss, Value(threadID), sp->beta, sp->depth, sp->ply); search<PvNode, true>(pos, ss, sp->alpha, sp->beta, sp->depth, sp->ply);
} }
template <NodeType PvNode> template <NodeType PvNode>