X-Git-Url: https://git.sesse.net/?p=stockfish;a=blobdiff_plain;f=src%2Fsearch.cpp;h=60d1a86866d74b4cd3753cdc3df80ff78c525fd5;hp=cd815c2af266e679294ff81ba0f5806f7b548d4a;hb=f6e98a924af233a5e69f3494168cf5d80168c705;hpb=5413fda7397f8ffe32e41b9c7f13297c39929f5c diff --git a/src/search.cpp b/src/search.cpp index cd815c2a..60d1a868 100644 --- a/src/search.cpp +++ b/src/search.cpp @@ -57,7 +57,7 @@ namespace { 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); } @@ -85,7 +85,7 @@ namespace { GainsStats Gains; MovesStats Countermoves, Followupmoves; - template + template Value search(Position& pos, Stack* ss, Value alpha, Value beta, Depth depth, bool cutNode); template @@ -337,7 +337,7 @@ namespace { // high/low anymore. while (true) { - bestValue = search(pos, ss, alpha, beta, depth * ONE_PLY, false); + bestValue = search(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 @@ -443,12 +443,11 @@ namespace { // 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 + template 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)); @@ -621,7 +620,7 @@ namespace { pos.do_null_move(st); (ss+1)->skipNullMove = true; nullValue = depth-R < ONE_PLY ? -qsearch(pos, ss+1, -beta, -beta+1, DEPTH_ZERO) - : - search(pos, ss+1, -beta, -beta+1, depth-R, !cutNode); + : - search(pos, ss+1, -beta, -beta+1, depth-R, !cutNode); (ss+1)->skipNullMove = false; pos.undo_null_move(); @@ -637,7 +636,7 @@ namespace { // Do verification search at high depths ss->skipNullMove = true; Value v = depth-R < ONE_PLY ? qsearch(pos, ss, beta-1, beta, DEPTH_ZERO) - : search(pos, ss, beta-1, beta, depth-R, false); + : search(pos, ss, beta-1, beta, depth-R, false); ss->skipNullMove = false; if (v >= beta) @@ -669,7 +668,7 @@ namespace { { ss->currentMove = move; pos.do_move(move, st, ci, pos.gives_check(move, ci)); - value = -search(pos, ss+1, -rbeta, -rbeta+1, rdepth, !cutNode); + value = -search(pos, ss+1, -rbeta, -rbeta+1, rdepth, !cutNode); pos.undo_move(move); if (value >= rbeta) return value; @@ -684,7 +683,7 @@ namespace { Depth d = depth - 2 * ONE_PLY - (PvNode ? DEPTH_ZERO : depth / 4); ss->skipNullMove = true; - search(pos, ss, alpha, beta, d, true); + search(pos, ss, alpha, beta, d, true); ss->skipNullMove = false; tte = TT.probe(posKey); @@ -784,7 +783,7 @@ moves_loop: // When in check and at SpNode search starts from here Value rBeta = ttValue - int(depth); ss->excludedMove = move; ss->skipNullMove = true; - value = search(pos, ss, rBeta - 1, rBeta, depth / 2, cutNode); + value = search(pos, ss, rBeta - 1, rBeta, depth / 2, cutNode); ss->skipNullMove = false; ss->excludedMove = MOVE_NONE; @@ -884,13 +883,13 @@ moves_loop: // When in check and at SpNode search starts from here if (SpNode) alpha = splitPoint->alpha; - value = -search(pos, ss+1, -(alpha+1), -alpha, d, true); + value = -search(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(pos, ss+1, -(alpha+1), -alpha, d2, true); + value = -search(pos, ss+1, -(alpha+1), -alpha, d2, true); } doFullDepthSearch = (value > alpha && ss->reduction != DEPTH_ZERO); @@ -908,7 +907,7 @@ moves_loop: // When in check and at SpNode search starts from here value = newDepth < ONE_PLY ? givesCheck ? -qsearch(pos, ss+1, -(alpha+1), -alpha, DEPTH_ZERO) : -qsearch(pos, ss+1, -(alpha+1), -alpha, DEPTH_ZERO) - : - search(pos, ss+1, -(alpha+1), -alpha, newDepth, !cutNode); + : - search(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 @@ -918,7 +917,7 @@ moves_loop: // When in check and at SpNode search starts from here value = newDepth < ONE_PLY ? givesCheck ? -qsearch(pos, ss+1, -beta, -alpha, DEPTH_ZERO) : -qsearch(pos, ss+1, -beta, -alpha, DEPTH_ZERO) - : - search(pos, ss+1, -beta, -alpha, newDepth, false); + : - search(pos, ss+1, -beta, -alpha, newDepth, false); // Step 17. Undo move pos.undo_move(move); @@ -985,8 +984,10 @@ moves_loop: // When in check and at SpNode search starts from here // 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 > -VALUE_INFINITE && bestValue < beta); @@ -1043,7 +1044,7 @@ moves_loop: // When in check and at SpNode search starts from here template 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()); @@ -1514,25 +1515,23 @@ void Thread::idle_loop() { activePosition = &pos; - switch (sp->nodeType) { - case Root: - search(pos, ss, sp->alpha, sp->beta, sp->depth, sp->cutNode); - break; - case PV: - search(pos, ss, sp->alpha, sp->beta, sp->depth, sp->cutNode); - break; - case NonPV: - search(pos, ss, sp->alpha, sp->beta, sp->depth, sp->cutNode); - break; - default: + if (sp->nodeType == NonPV) + search(pos, ss, sp->alpha, sp->beta, sp->depth, sp->cutNode); + + else if (sp->nodeType == PV) + search(pos, ss, sp->alpha, sp->beta, sp->depth, sp->cutNode); + + else if (sp->nodeType == Root) + search(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 @@ -1550,6 +1549,10 @@ void Thread::idle_loop() { // 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 @@ -1565,6 +1568,44 @@ void Thread::idle_loop() { } } +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