X-Git-Url: https://git.sesse.net/?p=stockfish;a=blobdiff_plain;f=src%2Fsearch.cpp;h=ac870f5bf617aa8d75fbdede2e5e04048eaf970c;hp=c5c8b839db343d9ec4396e2f74d759965465aa4c;hb=4ad03c9bad914b337fb6431c4bcbc1d909a1a841;hpb=fb50e16cdda68e05d08ed3992a04e5a396a461e3 diff --git a/src/search.cpp b/src/search.cpp index c5c8b839..ac870f5b 100644 --- a/src/search.cpp +++ b/src/search.cpp @@ -59,6 +59,10 @@ namespace { // Used for debugging SMP code. const bool FakeSplit = false; + // Fast lookup table of sliding pieces indexed by Piece + const bool Slidings[18] = { 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 1, 1, 1 }; + inline bool piece_is_slider(Piece p) { return Slidings[p]; } + // ThreadsManager class is used to handle all the threads related stuff in search, // init, starting, parking and, the most important, launching a slave thread at a // split point are what this class does. All the access to shared thread data is @@ -90,8 +94,8 @@ namespace { int ActiveThreads; volatile bool AllThreadsShouldExit; Thread threads[MAX_THREADS]; - Lock MPLock; - WaitCondition WaitCond[MAX_THREADS]; + Lock MPLock, SleepLock[MAX_THREADS]; + WaitCondition SleepCond[MAX_THREADS]; }; @@ -228,7 +232,10 @@ namespace { const Value EasyMoveMargin = Value(0x200); - /// Global variables + /// Namespace variables + + // Book object + Book OpeningBook; // Iteration counter int Iteration; @@ -256,6 +263,7 @@ namespace { // Multi-threads related variables Depth MinimumSplitDepth; int MaxThreadsPerSplitPoint; + bool UseSleepingThreads; ThreadsManager ThreadsMgr; // Node counters, used only by thread[0] but try to keep in different cache @@ -340,7 +348,7 @@ void init_search() { // Init reductions array for (hd = 1; hd < 64; hd++) for (mc = 1; mc < 64; mc++) { - double pvRed = 0.33 + log(double(hd)) * log(double(mc)) / 4.5; + double pvRed = log(double(hd)) * log(double(mc)) / 3.0; double nonPVRed = 0.33 + log(double(hd)) * log(double(mc)) / 2.25; ReductionMatrix[PV][hd][mc] = (int8_t) ( pvRed >= 1.0 ? floor( pvRed * int(ONE_PLY)) : 0); ReductionMatrix[NonPV][hd][mc] = (int8_t) (nonPVRed >= 1.0 ? floor(nonPVRed * int(ONE_PLY)) : 0); @@ -407,12 +415,12 @@ bool think(Position& pos, bool infinite, bool ponder, int time[], int increment[ UseTimeManagement = !ExactMaxTime && !MaxDepth && !MaxNodes && !InfiniteSearch; // Look for a book move, only during games, not tests - if (UseTimeManagement && get_option_value_bool("OwnBook")) + if (UseTimeManagement && Options["OwnBook"].value()) { - if (get_option_value_string("Book File") != OpeningBook.file_name()) - OpeningBook.open(get_option_value_string("Book File")); + if (Options["Book File"].value() != OpeningBook.file_name()) + OpeningBook.open(Options["Book File"].value()); - Move bookMove = OpeningBook.get_move(pos, get_option_value_bool("Best Book Move")); + Move bookMove = OpeningBook.get_move(pos, Options["Best Book Move"].value()); if (bookMove != MOVE_NONE) { if (PonderSearch) @@ -424,44 +432,49 @@ bool think(Position& pos, bool infinite, bool ponder, int time[], int increment[ } // Read UCI option values - TT.set_size(get_option_value_int("Hash")); - if (get_option_value_bool("Clear Hash")) + TT.set_size(Options["Hash"].value()); + if (Options["Clear Hash"].value()) { - set_option_value("Clear Hash", "false"); + Options["Clear Hash"].set_value("false"); TT.clear(); } - CheckExtension[1] = Depth(get_option_value_int("Check Extension (PV nodes)")); - CheckExtension[0] = Depth(get_option_value_int("Check Extension (non-PV nodes)")); - SingleEvasionExtension[1] = Depth(get_option_value_int("Single Evasion Extension (PV nodes)")); - SingleEvasionExtension[0] = Depth(get_option_value_int("Single Evasion Extension (non-PV nodes)")); - PawnPushTo7thExtension[1] = Depth(get_option_value_int("Pawn Push to 7th Extension (PV nodes)")); - PawnPushTo7thExtension[0] = Depth(get_option_value_int("Pawn Push to 7th Extension (non-PV nodes)")); - PassedPawnExtension[1] = Depth(get_option_value_int("Passed Pawn Extension (PV nodes)")); - PassedPawnExtension[0] = Depth(get_option_value_int("Passed Pawn Extension (non-PV nodes)")); - PawnEndgameExtension[1] = Depth(get_option_value_int("Pawn Endgame Extension (PV nodes)")); - PawnEndgameExtension[0] = Depth(get_option_value_int("Pawn Endgame Extension (non-PV nodes)")); - MateThreatExtension[1] = Depth(get_option_value_int("Mate Threat Extension (PV nodes)")); - MateThreatExtension[0] = Depth(get_option_value_int("Mate Threat Extension (non-PV nodes)")); - - MinimumSplitDepth = get_option_value_int("Minimum Split Depth") * ONE_PLY; - MaxThreadsPerSplitPoint = get_option_value_int("Maximum Number of Threads per Split Point"); - MultiPV = get_option_value_int("MultiPV"); - UseLogFile = get_option_value_bool("Use Search Log"); + CheckExtension[1] = Options["Check Extension (PV nodes)"].value(); + CheckExtension[0] = Options["Check Extension (non-PV nodes)"].value(); + SingleEvasionExtension[1] = Options["Single Evasion Extension (PV nodes)"].value(); + SingleEvasionExtension[0] = Options["Single Evasion Extension (non-PV nodes)"].value(); + PawnPushTo7thExtension[1] = Options["Pawn Push to 7th Extension (PV nodes)"].value(); + PawnPushTo7thExtension[0] = Options["Pawn Push to 7th Extension (non-PV nodes)"].value(); + PassedPawnExtension[1] = Options["Passed Pawn Extension (PV nodes)"].value(); + PassedPawnExtension[0] = Options["Passed Pawn Extension (non-PV nodes)"].value(); + PawnEndgameExtension[1] = Options["Pawn Endgame Extension (PV nodes)"].value(); + PawnEndgameExtension[0] = Options["Pawn Endgame Extension (non-PV nodes)"].value(); + MateThreatExtension[1] = Options["Mate Threat Extension (PV nodes)"].value(); + MateThreatExtension[0] = Options["Mate Threat Extension (non-PV nodes)"].value(); + + MinimumSplitDepth = Options["Minimum Split Depth"].value() * ONE_PLY; + MaxThreadsPerSplitPoint = Options["Maximum Number of Threads per Split Point"].value(); + MultiPV = Options["MultiPV"].value(); + UseLogFile = Options["Use Search Log"].value(); + UseSleepingThreads = Options["Use Sleeping Threads"].value(); if (UseLogFile) - LogFile.open(get_option_value_string("Search Log Filename").c_str(), std::ios::out | std::ios::app); + LogFile.open(Options["Search Log Filename"].value().c_str(), std::ios::out | std::ios::app); read_weights(pos.side_to_move()); // Set the number of active threads - int newActiveThreads = get_option_value_int("Threads"); + int newActiveThreads = Options["Threads"].value(); if (newActiveThreads != ThreadsMgr.active_threads()) { ThreadsMgr.set_active_threads(newActiveThreads); - init_eval(ThreadsMgr.active_threads()); + init_eval(newActiveThreads); } + // 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()]; @@ -494,6 +507,9 @@ bool think(Position& pos, bool infinite, bool ponder, int time[], int increment[ if (UseLogFile) LogFile.close(); + // This makes all the threads to go to sleep + ThreadsMgr.set_active_threads(1); + return !Quit; } @@ -647,7 +663,7 @@ namespace { << " time " << current_search_time() << endl; // Print the best move and the ponder move to the standard output - if (pv[0] == MOVE_NONE) + if (pv[0] == MOVE_NONE || MultiPV > 1) { pv[0] = rml.move(0); pv[1] = MOVE_NONE; @@ -1056,7 +1072,6 @@ namespace { && !isCheck && refinedValue < beta - razor_margin(depth) && ttMove == MOVE_NONE - && (ss-1)->currentMove != MOVE_NULL && !value_is_mate(beta) && !pos.has_pawn_on_7th(pos.side_to_move())) { @@ -1273,6 +1288,17 @@ split_point_start: // At split points actual search starts from here continue; } + + // Prune neg. see moves at low depths + if ( predictedDepth < 2 * ONE_PLY + && bestValue > value_mated_in(PLY_MAX) + && pos.see_sign(move) < 0) + { + if (SpNode) + lock_grab(&(sp->lock)); + + continue; + } } // Step 13. Make the move @@ -1927,7 +1953,7 @@ split_point_start: // At split points actual search starts from here int t = current_search_time(); // Poll for input - if (Bioskey()) + if (data_available()) { // We are line oriented, don't read single chars std::string command; @@ -2175,6 +2201,9 @@ split_point_start: // At split points actual search starts from here assert(threadID >= 0 && threadID < MAX_THREADS); + int i; + bool allFinished = false; + while (true) { // Slave threads can exit as soon as AllThreadsShouldExit raises, @@ -2188,31 +2217,33 @@ 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 - || (!sp && threads[threadID].state == THREAD_AVAILABLE)) + while ( threadID >= ActiveThreads || threads[threadID].state == THREAD_INITIALIZING + || (UseSleepingThreads && threads[threadID].state == THREAD_AVAILABLE)) { - assert(!sp); - assert(threadID != 0); + assert(!sp || UseSleepingThreads); + assert(threadID != 0 || UseSleepingThreads); - if (AllThreadsShouldExit) - break; + if (threads[threadID].state == THREAD_INITIALIZING) + threads[threadID].state = THREAD_AVAILABLE; - lock_grab(&MPLock); + // Grab the lock to avoid races with wake_sleeping_thread() + lock_grab(&SleepLock[threadID]); - // Retest condition under lock protection - if (!( threadID >= ActiveThreads - || threads[threadID].state == THREAD_INITIALIZING - || (!sp && threads[threadID].state == THREAD_AVAILABLE))) + // If we are master and all slaves have finished do not go to sleep + for (i = 0; sp && i < ActiveThreads && !sp->slaves[i]; i++) {} + allFinished = (i == ActiveThreads); + + if (allFinished || AllThreadsShouldExit) { - lock_release(&MPLock); - continue; + lock_release(&SleepLock[threadID]); + break; } - // Put thread to sleep - threads[threadID].state = THREAD_AVAILABLE; - cond_wait(&WaitCond[threadID], &MPLock); - lock_release(&MPLock); + // Do sleep here after retesting sleep conditions + if (threadID >= ActiveThreads || threads[threadID].state == THREAD_AVAILABLE) + cond_wait(&SleepCond[threadID], &SleepLock[threadID]); + + lock_release(&SleepLock[threadID]); } // If this thread has been assigned work, launch a search @@ -2230,20 +2261,25 @@ 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; + + // 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 (UseSleepingThreads && threadID != tsp->master && threads[tsp->master].state == THREAD_AVAILABLE) + wake_sleeping_thread(tsp->master); } // 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. - int i = 0; - for ( ; sp && i < ActiveThreads && !sp->slaves[i]; i++) {} + for (i = 0; sp && i < ActiveThreads && !sp->slaves[i]; i++) {} + allFinished = (i == ActiveThreads); - if (i == ActiveThreads) + if (allFinished) { // Because sp->slaves[] is reset under lock protection, // be sure sp->lock has been released before to return. @@ -2274,7 +2310,10 @@ split_point_start: // At split points actual search starts from here lock_init(&MPLock); for (i = 0; i < MAX_THREADS; i++) - cond_init(&WaitCond[i]); + { + lock_init(&SleepLock[i]); + cond_init(&SleepCond[i]); + } // Initialize splitPoints[] locks for (i = 0; i < MAX_THREADS; i++) @@ -2307,7 +2346,7 @@ split_point_start: // At split points actual search starts from here if (!ok) { cout << "Failed to create thread number " << i << endl; - Application::exit_with_failure(); + exit(EXIT_FAILURE); } // Wait until the thread has finished launching and is gone to sleep @@ -2339,7 +2378,10 @@ split_point_start: // At split points actual search starts from here // Now we can safely destroy the wait conditions for (int i = 0; i < MAX_THREADS; i++) - cond_destroy(&WaitCond[i]); + { + lock_destroy(&SleepLock[i]); + cond_destroy(&SleepCond[i]); + } } @@ -2451,6 +2493,7 @@ split_point_start: // At split points actual search starts from here // Initialize the split point object splitPoint.parent = masterThread.splitPoint; + splitPoint.master = master; splitPoint.stopRequest = false; splitPoint.ply = ply; splitPoint.depth = depth; @@ -2500,7 +2543,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) + + if (UseSleepingThreads && i != master) wake_sleeping_thread(i); } @@ -2525,14 +2569,14 @@ split_point_start: // At split points actual search starts from here } - // wake_sleeping_thread() wakes up all sleeping threads when it is time - // to start a new search from the root. + // wake_sleeping_thread() wakes up the thread with the given threadID + // when it is time to start a new search. void ThreadsManager::wake_sleeping_thread(int threadID) { - lock_grab(&MPLock); - cond_signal(&WaitCond[threadID]); - lock_release(&MPLock); + lock_grab(&SleepLock[threadID]); + cond_signal(&SleepCond[threadID]); + lock_release(&SleepLock[threadID]); }