Subversion Repositories Games.Chess Giants

Rev

Rev 154 | Go to most recent revision | 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
169 pmbaty 5
  Copyright (C) 2015-2018 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"
96 pmbaty 29
 
30
ThreadPool Threads; // Global object
31
 
32
 
169 pmbaty 33
/// Thread constructor launches the thread and waits until it goes to sleep
34
/// in idle_loop(). Note that 'searching' and 'exit' should be alredy set.
96 pmbaty 35
 
169 pmbaty 36
Thread::Thread(size_t n) : idx(n), stdThread(&Thread::idle_loop, this) {
96 pmbaty 37
 
169 pmbaty 38
  wait_for_search_finished();
96 pmbaty 39
}
40
 
41
 
169 pmbaty 42
/// Thread destructor wakes up the thread in idle_loop() and waits
43
/// for its termination. Thread should be already waiting.
96 pmbaty 44
 
45
Thread::~Thread() {
46
 
169 pmbaty 47
  assert(!searching);
48
 
96 pmbaty 49
  exit = true;
169 pmbaty 50
  start_searching();
51
  stdThread.join();
96 pmbaty 52
}
53
 
54
 
169 pmbaty 55
/// Thread::clear() reset histories, usually before a new game
96 pmbaty 56
 
169 pmbaty 57
void Thread::clear() {
96 pmbaty 58
 
169 pmbaty 59
  counterMoves.fill(MOVE_NONE);
60
  mainHistory.fill(0);
61
  captureHistory.fill(0);
62
 
63
  for (auto& to : contHistory)
64
      for (auto& h : to)
65
          h.fill(0);
66
 
67
  contHistory[NO_PIECE][0].fill(Search::CounterMovePruneThreshold - 1);
96 pmbaty 68
}
69
 
169 pmbaty 70
/// Thread::start_searching() wakes up the thread that will start the search
96 pmbaty 71
 
169 pmbaty 72
void Thread::start_searching() {
96 pmbaty 73
 
169 pmbaty 74
  std::lock_guard<Mutex> lk(mutex);
75
  searching = true;
76
  cv.notify_one(); // Wake up the thread in idle_loop()
96 pmbaty 77
}
78
 
79
 
169 pmbaty 80
/// Thread::wait_for_search_finished() blocks on the condition variable
81
/// until the thread has finished searching.
96 pmbaty 82
 
169 pmbaty 83
void Thread::wait_for_search_finished() {
96 pmbaty 84
 
85
  std::unique_lock<Mutex> lk(mutex);
169 pmbaty 86
  cv.wait(lk, [&]{ return !searching; });
96 pmbaty 87
}
88
 
89
 
169 pmbaty 90
/// Thread::idle_loop() is where the thread is parked, blocked on the
91
/// condition variable, when it has no work to do.
96 pmbaty 92
 
93
void Thread::idle_loop() {
94
 
169 pmbaty 95
  // If OS already scheduled us on a different group than 0 then don't overwrite
96
  // the choice, eventually we are one of many one-threaded processes running on
97
  // some Windows NUMA hardware, for instance in fishtest. To make it simple,
98
  // just check if running threads are below a threshold, in this case all this
99
  // NUMA machinery is not needed.
100
  if (Options["Threads"] >= 8)
101
      WinProcGroup::bindThisThread(idx);
102
 
103
  while (true)
96 pmbaty 104
  {
105
      std::unique_lock<Mutex> lk(mutex);
106
      searching = false;
169 pmbaty 107
      cv.notify_one(); // Wake up anyone waiting for search finished
108
      cv.wait(lk, [&]{ return searching; });
96 pmbaty 109
 
169 pmbaty 110
      if (exit)
111
          return;
96 pmbaty 112
 
113
      lk.unlock();
114
 
169 pmbaty 115
      search();
96 pmbaty 116
  }
117
}
118
 
169 pmbaty 119
/// ThreadPool::set() creates/destroys threads to match the requested number.
120
/// Created and launced threads wil go immediately to sleep in idle_loop.
121
/// Upon resizing, threads are recreated to allow for binding if necessary.
96 pmbaty 122
 
169 pmbaty 123
void ThreadPool::set(size_t requested) {
96 pmbaty 124
 
169 pmbaty 125
  if (size() > 0) { // destroy any existing thread(s)
126
      main()->wait_for_search_finished();
96 pmbaty 127
 
169 pmbaty 128
      while (size() > 0)
129
          delete back(), pop_back();
130
  }
96 pmbaty 131
 
169 pmbaty 132
  if (requested > 0) { // create new thread(s)
133
      push_back(new MainThread(0));
96 pmbaty 134
 
169 pmbaty 135
      while (size() < requested)
136
          push_back(new Thread(size()));
137
      clear();
138
  }
96 pmbaty 139
}
140
 
169 pmbaty 141
/// ThreadPool::clear() sets threadPool data to initial values.
96 pmbaty 142
 
169 pmbaty 143
void ThreadPool::clear() {
96 pmbaty 144
 
145
  for (Thread* th : *this)
169 pmbaty 146
      th->clear();
96 pmbaty 147
 
169 pmbaty 148
  main()->callsCnt = 0;
149
  main()->previousScore = VALUE_INFINITE;
150
  main()->previousTimeReduction = 1;
154 pmbaty 151
}
152
 
169 pmbaty 153
/// ThreadPool::start_thinking() wakes up main thread waiting in idle_loop() and
154
/// returns immediately. Main thread will wake up other threads and start the search.
154 pmbaty 155
 
156
void ThreadPool::start_thinking(Position& pos, StateListPtr& states,
169 pmbaty 157
                                const Search::LimitsType& limits, bool ponderMode) {
96 pmbaty 158
 
159
  main()->wait_for_search_finished();
160
 
169 pmbaty 161
  stopOnPonderhit = stop = false;
162
  ponder = ponderMode;
154 pmbaty 163
  Search::Limits = limits;
164
  Search::RootMoves rootMoves;
96 pmbaty 165
 
166
  for (const auto& m : MoveList<LEGAL>(pos))
167
      if (   limits.searchmoves.empty()
168
          || std::count(limits.searchmoves.begin(), limits.searchmoves.end(), m))
169 pmbaty 169
          rootMoves.emplace_back(m);
96 pmbaty 170
 
154 pmbaty 171
  if (!rootMoves.empty())
172
      Tablebases::filter_root_moves(pos, rootMoves);
173
 
174
  // After ownership transfer 'states' becomes empty, so if we stop the search
175
  // and call 'go' again without setting a new position states.get() == NULL.
176
  assert(states.get() || setupStates.get());
177
 
178
  if (states.get())
179
      setupStates = std::move(states); // Ownership transfer, states is now empty
180
 
169 pmbaty 181
  // We use Position::set() to set root position across threads. But there are
182
  // some StateInfo fields (previous, pliesFromNull, capturedPiece) that cannot
183
  // be deduced from a fen string, so set() clears them and to not lose the info
184
  // we need to backup and later restore setupStates->back(). Note that setupStates
185
  // is shared by threads but is accessed in read-only mode.
154 pmbaty 186
  StateInfo tmp = setupStates->back();
187
 
169 pmbaty 188
  for (Thread* th : *this)
154 pmbaty 189
  {
169 pmbaty 190
      th->nodes = th->tbHits = th->nmp_ply = th->nmp_odd = 0;
191
      th->rootDepth = th->completedDepth = DEPTH_ZERO;
154 pmbaty 192
      th->rootMoves = rootMoves;
193
      th->rootPos.set(pos.fen(), pos.is_chess960(), &setupStates->back(), th);
194
  }
195
 
169 pmbaty 196
  setupStates->back() = tmp;
154 pmbaty 197
 
96 pmbaty 198
  main()->start_searching();
199
}