X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;ds=sidebyside;f=src%2Fsearch.cpp;h=f3b092d2dc1910365d70245409dbaa2392d6f792;hb=2991ff0dc2cbf762cb3e48027fa3baed5f1aebc3;hp=b3d54fb0c71328b8ca337c485589088106358683;hpb=472971f8516eb068a3e93473b4d9f5c95e6874d4;p=stockfish diff --git a/src/search.cpp b/src/search.cpp index b3d54fb0..f3b092d2 100644 --- a/src/search.cpp +++ b/src/search.cpp @@ -95,7 +95,7 @@ namespace { int ActiveThreads; volatile bool AllThreadsShouldExit; Thread threads[MAX_THREADS]; - Lock MPLock, WaitLock; + Lock MPLock; WaitCondition WaitCond[MAX_THREADS]; }; @@ -280,12 +280,14 @@ namespace { Value search(Position& pos, SearchStack* ss, Value alpha, Value beta, Depth depth, int ply); template - inline Value search(Position& pos, SearchStack* ss, Value alpha, Value beta, Depth depth, int ply) { - return search(pos, ss, alpha, beta, depth, ply); - } + Value qsearch(Position& pos, SearchStack* ss, Value alpha, Value beta, Depth depth, int ply); template - Value qsearch(Position& pos, SearchStack* ss, Value alpha, Value beta, Depth depth, int ply); + inline Value search(Position& pos, SearchStack* ss, Value alpha, Value beta, Depth depth, int ply) { + + return depth < ONE_PLY ? qsearch(pos, ss, alpha, beta, DEPTH_ZERO, ply) + : search(pos, ss, alpha, beta, depth, ply); + } template Depth extension(const Position& pos, Move m, bool captureOrPromotion, bool moveIsCheck, bool singleEvasion, bool mateThreat, bool* dangerous); @@ -464,10 +466,6 @@ bool think(const Position& pos, bool infinite, bool ponder, int time[], int incr init_eval(ThreadsMgr.active_threads()); } - // Wake up needed threads - for (int i = 1; i < newActiveThreads; i++) - ThreadsMgr.wake_sleeping_thread(i); - // Set thinking time int myTime = time[pos.side_to_move()]; int myIncrement = increment[pos.side_to_move()]; @@ -500,9 +498,6 @@ bool think(const Position& pos, bool infinite, bool ponder, int time[], int incr if (UseLogFile) LogFile.close(); - // This makes all the threads to go to sleep - ThreadsMgr.set_active_threads(1); - return !Quit; } @@ -1006,10 +1001,8 @@ namespace { } // Step 2. Check for aborted search and immediate draw - if (AbortSearch || ThreadsMgr.thread_should_stop(threadID)) - return VALUE_DRAW; - - if (pos.is_draw() || ply >= PLY_MAX - 1) + if ( AbortSearch || ThreadsMgr.thread_should_stop(threadID) + || pos.is_draw() || ply >= PLY_MAX - 1) return VALUE_DRAW; // Step 3. Mate distance pruning @@ -1026,7 +1019,7 @@ namespace { posKey = excludedMove ? pos.get_exclusion_key() : pos.get_key(); tte = TT.retrieve(posKey); - ttMove = (tte ? tte->move() : MOVE_NONE); + ttMove = tte ? tte->move() : MOVE_NONE; // At PV nodes, we don't use the TT for pruning, but only for move ordering. // This is to avoid problems in the following areas: @@ -1035,12 +1028,9 @@ namespace { // * Fifty move rule detection // * Searching for a mate // * Printing of full PV line - if (!PvNode && tte && ok_to_use_TT(tte, depth, beta, ply)) { - // Refresh tte entry to avoid aging - TT.store(posKey, tte->value(), tte->type(), tte->depth(), ttMove, tte->static_value(), tte->static_value_margin()); - + TT.refresh(tte); ss->bestMove = ttMove; // Can be MOVE_NONE return value_from_tt(tte->value(), ply); } @@ -1116,9 +1106,7 @@ namespace { pos.do_null_move(st); (ss+1)->skipNullMove = true; - - nullValue = depth-R*ONE_PLY < ONE_PLY ? -qsearch(pos, ss+1, -beta, -alpha, DEPTH_ZERO, ply+1) - : - search(pos, ss+1, -beta, -alpha, depth-R*ONE_PLY, ply+1); + nullValue = -search(pos, ss+1, -beta, -alpha, depth-R*ONE_PLY, ply+1); (ss+1)->skipNullMove = false; pos.undo_null_move(); @@ -1181,7 +1169,7 @@ split_point_start: // At split points actual search starts from here // Initialize a MovePicker object for the current position // FIXME currently MovePicker() c'tor is needless called also in SplitPoint - MovePicker mpBase = MovePicker(pos, ttMove, depth, H, ss, (PvNode ? -VALUE_INFINITE : beta)); + MovePicker mpBase(pos, ttMove, depth, H, ss, (PvNode ? -VALUE_INFINITE : beta)); MovePicker& mp = SpNode ? *sp->mp : mpBase; CheckInfo ci(pos); ss->bestMove = MOVE_NONE; @@ -1206,16 +1194,17 @@ split_point_start: // At split points actual search starts from here && (move = mp.get_next_move()) != MOVE_NONE && !ThreadsMgr.thread_should_stop(threadID)) { + assert(move_is_ok(move)); + if (SpNode) { moveCount = ++sp->moveCount; lock_release(&(sp->lock)); } - - assert(move_is_ok(move)); - - if (move == excludedMove) + else if (move == excludedMove) continue; + else + movesSearched[moveCount++] = move; moveIsCheck = pos.move_is_check(move, ci); captureOrPromotion = pos.move_is_capture_or_promotion(move); @@ -1247,10 +1236,9 @@ split_point_start: // At split points actual search starts from here } } - newDepth = depth - ONE_PLY + ext; - // Update current move (this must be done after singular extension search) - movesSearched[moveCount++] = ss->currentMove = move; + ss->currentMove = move; + newDepth = depth - ONE_PLY + ext; // Step 12. Futility pruning (is omitted in PV nodes) if ( !PvNode @@ -1299,8 +1287,7 @@ split_point_start: // At split points actual search starts from here // Step extra. pv search (only in PV nodes) // The first move in list is the expected PV if (!SpNode && PvNode && moveCount == 1) - value = newDepth < ONE_PLY ? -qsearch(pos, ss+1, -beta, -alpha, DEPTH_ZERO, ply+1) - : - search(pos, ss+1, -beta, -alpha, newDepth, ply+1); + value = -search(pos, ss+1, -beta, -alpha, newDepth, ply+1); else { // Step 14. Reduced depth search @@ -1318,8 +1305,7 @@ split_point_start: // At split points actual search starts from here { alpha = SpNode ? sp->alpha : alpha; Depth d = newDepth - ss->reduction; - value = d < ONE_PLY ? -qsearch(pos, ss+1, -(alpha+1), -alpha, DEPTH_ZERO, ply+1) - : - search(pos, ss+1, -(alpha+1), -alpha, d, ply+1); + value = -search(pos, ss+1, -(alpha+1), -alpha, d, ply+1); doFullDepthSearch = (value > alpha); } @@ -1343,15 +1329,13 @@ split_point_start: // At split points actual search starts from here if (doFullDepthSearch) { alpha = SpNode ? sp->alpha : alpha; - value = newDepth < ONE_PLY ? -qsearch(pos, ss+1, -(alpha+1), -alpha, DEPTH_ZERO, ply+1) - : - search(pos, ss+1, -(alpha+1), -alpha, newDepth, ply+1); + value = -search(pos, ss+1, -(alpha+1), -alpha, newDepth, ply+1); // Step extra. pv search (only in PV nodes) // Search only for possible new PV nodes, if instead value >= beta then // parent node fails low with value <= alpha and tries another move. if (PvNode && value > alpha && value < beta) - value = newDepth < ONE_PLY ? -qsearch(pos, ss+1, -beta, -alpha, DEPTH_ZERO, ply+1) - : - search(pos, ss+1, -beta, -alpha, newDepth, ply+1); + value = -search(pos, ss+1, -beta, -alpha, newDepth, ply+1); } } @@ -1371,24 +1355,29 @@ split_point_start: // At split points actual search starts from here if (value > bestValue && !(SpNode && ThreadsMgr.thread_should_stop(threadID))) { bestValue = value; + + if (SpNode) + sp->bestValue = value; + if (value > alpha) { if (SpNode && (!PvNode || value >= beta)) sp->stopRequest = true; if (PvNode && value < beta) // We want always alpha < beta + { alpha = value; + if (SpNode) + sp->alpha = value; + } if (value == value_mate_in(ply + 1)) ss->mateKiller = move; ss->bestMove = move; - } - if (SpNode) - { - sp->bestValue = bestValue; - sp->alpha = alpha; - sp->parentSstack->bestMove = ss->bestMove; + + if (SpNode) + sp->parentSstack->bestMove = move; } } @@ -1407,7 +1396,7 @@ split_point_start: // At split points actual search starts from here if (SpNode) { - /* Here we have the lock still grabbed */ + // Here we have the lock still grabbed sp->slaves[threadID] = 0; lock_release(&(sp->lock)); return bestValue; @@ -2225,7 +2214,8 @@ split_point_start: // At split points actual search starts from here // If we are not thinking, wait for a condition to be signaled // instead of wasting CPU time polling for work. while ( threadID >= ActiveThreads - || threads[threadID].state == THREAD_INITIALIZING) + || threads[threadID].state == THREAD_INITIALIZING + || (!sp && threads[threadID].state == THREAD_AVAILABLE)) { assert(!sp); assert(threadID != 0); @@ -2233,16 +2223,21 @@ split_point_start: // At split points actual search starts from here if (AllThreadsShouldExit) break; - threads[threadID].state = THREAD_AVAILABLE; + lock_grab(&MPLock); -#if !defined(_MSC_VER) - lock_grab(&WaitLock); - if (threadID >= ActiveThreads) - pthread_cond_wait(&WaitCond[threadID], &WaitLock); - lock_release(&WaitLock); -#else - WaitForSingleObject(WaitCond[threadID], INFINITE); -#endif + // Retest condition under lock protection + if (!( threadID >= ActiveThreads + || threads[threadID].state == THREAD_INITIALIZING + || (!sp && threads[threadID].state == THREAD_AVAILABLE))) + { + lock_release(&MPLock); + continue; + } + + // Put thread to sleep + threads[threadID].state = THREAD_AVAILABLE; + cond_wait(&WaitCond[threadID], &MPLock); + lock_release(&MPLock); } // If this thread has been assigned work, launch a search @@ -2260,9 +2255,9 @@ split_point_start: // At split points actual search starts from here if (tsp->pvNode) search(pos, ss, tsp->alpha, tsp->beta, tsp->depth, tsp->ply); - else + else { search(pos, ss, tsp->alpha, tsp->beta, tsp->depth, tsp->ply); - + } assert(threads[threadID].state == THREAD_SEARCHING); threads[threadID].state = THREAD_AVAILABLE; @@ -2302,14 +2297,9 @@ split_point_start: // At split points actual search starts from here // Initialize global locks lock_init(&MPLock); - lock_init(&WaitLock); for (i = 0; i < MAX_THREADS; i++) -#if !defined(_MSC_VER) - pthread_cond_init(&WaitCond[i], NULL); -#else - WaitCond[i] = CreateEvent(0, FALSE, FALSE, 0); -#endif + cond_init(&WaitCond[i]); // Initialize splitPoints[] locks for (i = 0; i < MAX_THREADS; i++) @@ -2334,6 +2324,7 @@ split_point_start: // At split points actual search starts from here #if !defined(_MSC_VER) pthread_t pthread[1]; ok = (pthread_create(pthread, NULL, init_thread, (void*)(&i)) == 0); + pthread_detach(pthread[0]); #else ok = (CreateThread(NULL, 0, init_thread, (LPVOID)(&i), 0, NULL) != NULL); #endif @@ -2369,7 +2360,6 @@ split_point_start: // At split points actual search starts from here for (int j = 0; j < MAX_ACTIVE_SPLIT_POINTS; j++) lock_destroy(&(threads[i].splitPoints[j].lock)); - lock_destroy(&WaitLock); lock_destroy(&MPLock); // Now we can safely destroy the wait conditions @@ -2450,9 +2440,8 @@ split_point_start: // At split points actual search starts from here // split point objects), the function immediately returns. If splitting is // possible, a SplitPoint object is initialized with all the data that must be // copied to the helper threads and we tell our helper threads that they have - // been assigned work. This will cause them to instantly leave their idle loops - // and call sp_search(). When all threads have returned from sp_search() then - // split() returns. + // been assigned work. This will cause them to instantly leave their idle loops and + // call search().When all threads have returned from search() then split() returns. template void ThreadsManager::split(const Position& p, SearchStack* ss, int ply, Value* alpha, @@ -2535,6 +2524,8 @@ split_point_start: // At split points actual search starts from here assert(i == master || threads[i].state == THREAD_BOOKED); threads[i].state = THREAD_WORKISWAITING; // This makes the slave to exit from idle_loop() + if (i != master) + wake_sleeping_thread(i); } // Everything is set up. The master thread enters the idle loop, from @@ -2562,9 +2553,9 @@ split_point_start: // At split points actual search starts from here void ThreadsManager::wake_sleeping_thread(int threadID) { - lock_grab(&WaitLock); + lock_grab(&MPLock); cond_signal(&WaitCond[threadID]); - lock_release(&WaitLock); + lock_release(&MPLock); }