]> git.sesse.net Git - stockfish/blob - src/thread.h
Merge branch 'master' into clusterMergeMaster13
[stockfish] / src / thread.h
1 /*
2   Stockfish, a UCI chess playing engine derived from Glaurung 2.1
3   Copyright (C) 2004-2021 The Stockfish developers (see AUTHORS file)
4
5   Stockfish is free software: you can redistribute it and/or modify
6   it under the terms of the GNU General Public License as published by
7   the Free Software Foundation, either version 3 of the License, or
8   (at your option) any later version.
9
10   Stockfish is distributed in the hope that it will be useful,
11   but WITHOUT ANY WARRANTY; without even the implied warranty of
12   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   GNU General Public License for more details.
14
15   You should have received a copy of the GNU General Public License
16   along with this program.  If not, see <http://www.gnu.org/licenses/>.
17 */
18
19 #ifndef THREAD_H_INCLUDED
20 #define THREAD_H_INCLUDED
21
22 #include <atomic>
23 #include <condition_variable>
24 #include <mutex>
25 #include <thread>
26 #include <vector>
27
28 #include "cluster.h"
29 #include "material.h"
30 #include "movepick.h"
31 #include "pawns.h"
32 #include "position.h"
33 #include "search.h"
34 #include "thread_win32_osx.h"
35
36 namespace Stockfish {
37
38 /// Thread class keeps together all the thread-related stuff. We use
39 /// per-thread pawn and material hash tables so that once we get a
40 /// pointer to an entry its life time is unlimited and we don't have
41 /// to care about someone changing the entry under our feet.
42
43 class Thread {
44
45   std::mutex mutex;
46   std::condition_variable cv;
47   size_t idx;
48   bool exit = false, searching = true; // Set before starting std::thread
49   NativeThread stdThread;
50
51 public:
52   explicit Thread(size_t);
53   virtual ~Thread();
54   virtual void search();
55   void clear();
56   void idle_loop();
57   void start_searching();
58   void wait_for_search_finished();
59   size_t id() const { return idx; }
60
61   Pawns::Table pawnsTable;
62   Material::Table materialTable;
63   size_t pvIdx, pvLast;
64   RunningAverage doubleExtensionAverage[COLOR_NB];
65   uint64_t nodesLastExplosive;
66   uint64_t nodesLastNormal;
67   std::atomic<uint64_t> nodes, tbHits, TTsaves, bestMoveChanges;
68   int selDepth, nmpMinPly;
69   Color nmpColor;
70   ExplosionState state;
71
72   Position rootPos;
73   StateInfo rootState;
74   Search::RootMoves rootMoves;
75   Depth rootDepth, completedDepth;
76   CounterMoveHistory counterMoves;
77   ButterflyHistory mainHistory;
78   LowPlyHistory lowPlyHistory;
79   CapturePieceToHistory captureHistory;
80   ContinuationHistory continuationHistory[2][2];
81   Score trend;
82 #ifdef USE_MPI
83   struct {
84       std::mutex mutex;
85       Cluster::TTCache<Cluster::TTCacheSize> buffer = {};
86   } ttCache;
87 #endif
88 };
89
90
91 /// MainThread is a derived class specific for main thread
92
93 struct MainThread : public Thread {
94
95   using Thread::Thread;
96
97   void search() override;
98   void check_time();
99
100   double previousTimeReduction;
101   Value bestPreviousScore;
102   Value iterValue[4];
103   int callsCnt;
104   bool stopOnPonderhit;
105   std::atomic_bool ponder;
106 };
107
108
109 /// ThreadPool struct handles all the threads-related stuff like init, starting,
110 /// parking and, most importantly, launching a thread. All the access to threads
111 /// is done through this class.
112
113 struct ThreadPool : public std::vector<Thread*> {
114
115   void start_thinking(Position&, StateListPtr&, const Search::LimitsType&, bool = false);
116   void clear();
117   void set(size_t);
118
119   MainThread* main()        const { return static_cast<MainThread*>(front()); }
120   uint64_t nodes_searched() const { return accumulate(&Thread::nodes); }
121   uint64_t tb_hits()        const { return accumulate(&Thread::tbHits); }
122   uint64_t TT_saves()       const { return accumulate(&Thread::TTsaves); }
123   Thread* get_best_thread() const;
124   void start_searching();
125   void wait_for_search_finished() const;
126
127   std::atomic_bool stop, increaseDepth;
128
129 private:
130   StateListPtr setupStates;
131
132   uint64_t accumulate(std::atomic<uint64_t> Thread::* member) const {
133
134     uint64_t sum = 0;
135     for (Thread* th : *this)
136         sum += (th->*member).load(std::memory_order_relaxed);
137     return sum;
138   }
139 };
140
141 extern ThreadPool Threads;
142
143 } // namespace Stockfish
144
145 #endif // #ifndef THREAD_H_INCLUDED