Rev 169 | Details | Compare with Previous | Last modification | View Log | RSS feed
| Rev | Author | Line No. | Line | 
|---|---|---|---|
| 96 | pmbaty | 1 | /* | 
| 2 |   Stockfish, a UCI chess playing engine derived from Glaurung 2.1 | ||
| 3 |   Copyright (C) 2004-2008 Tord Romstad (Glaurung author) | ||
| 4 |   Copyright (C) 2008-2015 Marco Costalba, Joona Kiiski, Tord Romstad | ||
| 185 | pmbaty | 5 |   Copyright (C) 2015-2019 Marco Costalba, Joona Kiiski, Gary Linscott, Tord Romstad | 
| 96 | pmbaty | 6 | |
| 7 |   Stockfish is free software: you can redistribute it and/or modify | ||
| 8 |   it under the terms of the GNU General Public License as published by | ||
| 9 |   the Free Software Foundation, either version 3 of the License, or | ||
| 10 |   (at your option) any later version. | ||
| 11 | |||
| 12 |   Stockfish is distributed in the hope that it will be useful, | ||
| 13 |   but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 14 |   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | ||
| 15 |   GNU General Public License for more details. | ||
| 16 | |||
| 17 |   You should have received a copy of the GNU General Public License | ||
| 18 |   along with this program.  If not, see <http://www.gnu.org/licenses/>. | ||
| 19 | */ | ||
| 20 | |||
| 21 | #include <algorithm> // For std::count | ||
| 22 | #include <cassert> | ||
| 23 | |||
| 24 | #include "movegen.h" | ||
| 25 | #include "search.h" | ||
| 26 | #include "thread.h" | ||
| 27 | #include "uci.h" | ||
| 154 | pmbaty | 28 | #include "syzygy/tbprobe.h" | 
| 185 | pmbaty | 29 | #include "tt.h" | 
| 96 | pmbaty | 30 | |
| 31 | ThreadPool Threads; // Global object | ||
| 32 | |||
| 33 | |||
| 169 | pmbaty | 34 | /// Thread constructor launches the thread and waits until it goes to sleep | 
| 35 | /// in idle_loop(). Note that 'searching' and 'exit' should be alredy set. | ||
| 96 | pmbaty | 36 | |
| 169 | pmbaty | 37 | Thread::Thread(size_t n) : idx(n), stdThread(&Thread::idle_loop, this) { | 
| 96 | pmbaty | 38 | |
| 169 | pmbaty | 39 | wait_for_search_finished(); | 
| 96 | pmbaty | 40 | } | 
| 41 | |||
| 42 | |||
| 169 | pmbaty | 43 | /// Thread destructor wakes up the thread in idle_loop() and waits | 
| 44 | /// for its termination. Thread should be already waiting. | ||
| 96 | pmbaty | 45 | |
| 46 | Thread::~Thread() { | ||
| 47 | |||
| 169 | pmbaty | 48 | assert(!searching); | 
| 49 | |||
| 96 | pmbaty | 50 | exit = true; | 
| 169 | pmbaty | 51 | start_searching(); | 
| 52 | stdThread.join(); | ||
| 96 | pmbaty | 53 | } | 
| 54 | |||
| 55 | |||
| 169 | pmbaty | 56 | /// Thread::clear() reset histories, usually before a new game | 
| 96 | pmbaty | 57 | |
| 169 | pmbaty | 58 | void Thread::clear() { | 
| 96 | pmbaty | 59 | |
| 169 | pmbaty | 60 | counterMoves.fill(MOVE_NONE); | 
| 61 | mainHistory.fill(0); | ||
| 62 | captureHistory.fill(0); | ||
| 63 | |||
| 185 | pmbaty | 64 | for (auto& to : continuationHistory) | 
| 169 | pmbaty | 65 | for (auto& h : to) | 
| 185 | pmbaty | 66 | h->fill(0); | 
| 169 | pmbaty | 67 | |
| 185 | pmbaty | 68 | continuationHistory[NO_PIECE][0]->fill(Search::CounterMovePruneThreshold - 1); | 
| 96 | pmbaty | 69 | } | 
| 70 | |||
| 169 | pmbaty | 71 | /// Thread::start_searching() wakes up the thread that will start the search | 
| 96 | pmbaty | 72 | |
| 169 | pmbaty | 73 | void Thread::start_searching() { | 
| 96 | pmbaty | 74 | |
| 169 | pmbaty | 75 | std::lock_guard<Mutex> lk(mutex); | 
| 76 | searching = true; | ||
| 77 | cv.notify_one(); // Wake up the thread in idle_loop() | ||
| 96 | pmbaty | 78 | } | 
| 79 | |||
| 80 | |||
| 169 | pmbaty | 81 | /// Thread::wait_for_search_finished() blocks on the condition variable | 
| 82 | /// until the thread has finished searching. | ||
| 96 | pmbaty | 83 | |
| 169 | pmbaty | 84 | void Thread::wait_for_search_finished() { | 
| 96 | pmbaty | 85 | |
| 86 | std::unique_lock<Mutex> lk(mutex); | ||
| 169 | pmbaty | 87 | cv.wait(lk, [&]{ return !searching; }); | 
| 96 | pmbaty | 88 | } | 
| 89 | |||
| 90 | |||
| 169 | pmbaty | 91 | /// Thread::idle_loop() is where the thread is parked, blocked on the | 
| 92 | /// condition variable, when it has no work to do. | ||
| 96 | pmbaty | 93 | |
| 94 | void Thread::idle_loop() { | ||
| 95 | |||
| 169 | pmbaty | 96 |   // If OS already scheduled us on a different group than 0 then don't overwrite | 
| 97 |   // the choice, eventually we are one of many one-threaded processes running on | ||
| 98 |   // some Windows NUMA hardware, for instance in fishtest. To make it simple, | ||
| 99 |   // just check if running threads are below a threshold, in this case all this | ||
| 100 |   // NUMA machinery is not needed. | ||
| 185 | pmbaty | 101 | if (Options["Threads"] > 8) | 
| 169 | pmbaty | 102 | WinProcGroup::bindThisThread(idx); | 
| 103 | |||
| 104 | while (true) | ||
| 96 | pmbaty | 105 |   { | 
| 106 | std::unique_lock<Mutex> lk(mutex); | ||
| 107 | searching = false; | ||
| 169 | pmbaty | 108 | cv.notify_one(); // Wake up anyone waiting for search finished | 
| 109 | cv.wait(lk, [&]{ return searching; }); | ||
| 96 | pmbaty | 110 | |
| 169 | pmbaty | 111 | if (exit) | 
| 112 | return; | ||
| 96 | pmbaty | 113 | |
| 114 | lk.unlock(); | ||
| 115 | |||
| 169 | pmbaty | 116 | search(); | 
| 96 | pmbaty | 117 |   } | 
| 118 | } | ||
| 119 | |||
| 169 | pmbaty | 120 | /// ThreadPool::set() creates/destroys threads to match the requested number. | 
| 185 | pmbaty | 121 | /// Created and launched threads will go immediately to sleep in idle_loop. | 
| 169 | pmbaty | 122 | /// Upon resizing, threads are recreated to allow for binding if necessary. | 
| 96 | pmbaty | 123 | |
| 169 | pmbaty | 124 | void ThreadPool::set(size_t requested) { | 
| 96 | pmbaty | 125 | |
| 169 | pmbaty | 126 | if (size() > 0) { // destroy any existing thread(s) | 
| 127 | main()->wait_for_search_finished(); | ||
| 96 | pmbaty | 128 | |
| 169 | pmbaty | 129 | while (size() > 0) | 
| 130 | delete back(), pop_back(); | ||
| 131 |   } | ||
| 96 | pmbaty | 132 | |
| 169 | pmbaty | 133 | if (requested > 0) { // create new thread(s) | 
| 134 | push_back(new MainThread(0)); | ||
| 96 | pmbaty | 135 | |
| 169 | pmbaty | 136 | while (size() < requested) | 
| 137 | push_back(new Thread(size())); | ||
| 138 | clear(); | ||
| 139 |   } | ||
| 185 | pmbaty | 140 | |
| 141 |   // Reallocate the hash with the new threadpool size | ||
| 142 | TT.resize(Options["Hash"]); | ||
| 96 | pmbaty | 143 | } | 
| 144 | |||
| 169 | pmbaty | 145 | /// ThreadPool::clear() sets threadPool data to initial values. | 
| 96 | pmbaty | 146 | |
| 169 | pmbaty | 147 | void ThreadPool::clear() { | 
| 96 | pmbaty | 148 | |
| 149 | for (Thread* th : *this) | ||
| 169 | pmbaty | 150 | th->clear(); | 
| 96 | pmbaty | 151 | |
| 169 | pmbaty | 152 | main()->callsCnt = 0; | 
| 153 | main()->previousScore = VALUE_INFINITE; | ||
| 185 | pmbaty | 154 | main()->previousTimeReduction = 1.0; | 
| 154 | pmbaty | 155 | } | 
| 156 | |||
| 169 | pmbaty | 157 | /// ThreadPool::start_thinking() wakes up main thread waiting in idle_loop() and | 
| 158 | /// returns immediately. Main thread will wake up other threads and start the search. | ||
| 154 | pmbaty | 159 | |
| 160 | void ThreadPool::start_thinking(Position& pos, StateListPtr& states, | ||
| 169 | pmbaty | 161 | const Search::LimitsType& limits, bool ponderMode) { | 
| 96 | pmbaty | 162 | |
| 163 | main()->wait_for_search_finished(); | ||
| 164 | |||
| 169 | pmbaty | 165 | stopOnPonderhit = stop = false; | 
| 166 | ponder = ponderMode; | ||
| 154 | pmbaty | 167 | Search::Limits = limits; | 
| 168 | Search::RootMoves rootMoves; | ||
| 96 | pmbaty | 169 | |
| 170 | for (const auto& m : MoveList<LEGAL>(pos)) | ||
| 171 | if ( limits.searchmoves.empty() | ||
| 172 | || std::count(limits.searchmoves.begin(), limits.searchmoves.end(), m)) | ||
| 169 | pmbaty | 173 | rootMoves.emplace_back(m); | 
| 96 | pmbaty | 174 | |
| 154 | pmbaty | 175 | if (!rootMoves.empty()) | 
| 185 | pmbaty | 176 | Tablebases::rank_root_moves(pos, rootMoves); | 
| 154 | pmbaty | 177 | |
| 178 |   // After ownership transfer 'states' becomes empty, so if we stop the search | ||
| 179 |   // and call 'go' again without setting a new position states.get() == NULL. | ||
| 180 | assert(states.get() || setupStates.get()); | ||
| 181 | |||
| 182 | if (states.get()) | ||
| 183 | setupStates = std::move(states); // Ownership transfer, states is now empty | ||
| 184 | |||
| 169 | pmbaty | 185 |   // We use Position::set() to set root position across threads. But there are | 
| 186 |   // some StateInfo fields (previous, pliesFromNull, capturedPiece) that cannot | ||
| 187 |   // be deduced from a fen string, so set() clears them and to not lose the info | ||
| 188 |   // we need to backup and later restore setupStates->back(). Note that setupStates | ||
| 189 |   // is shared by threads but is accessed in read-only mode. | ||
| 154 | pmbaty | 190 | StateInfo tmp = setupStates->back(); | 
| 191 | |||
| 169 | pmbaty | 192 | for (Thread* th : *this) | 
| 154 | pmbaty | 193 |   { | 
| 185 | pmbaty | 194 | th->nodes = th->tbHits = th->nmpMinPly = 0; | 
| 169 | pmbaty | 195 | th->rootDepth = th->completedDepth = DEPTH_ZERO; | 
| 154 | pmbaty | 196 | th->rootMoves = rootMoves; | 
| 197 | th->rootPos.set(pos.fen(), pos.is_chess960(), &setupStates->back(), th); | ||
| 198 |   } | ||
| 199 | |||
| 169 | pmbaty | 200 | setupStates->back() = tmp; | 
| 154 | pmbaty | 201 | |
| 96 | pmbaty | 202 | main()->start_searching(); | 
| 203 | } |