]> git.sesse.net Git - stockfish/blob - src/thread.h
fae866c4f30d21657f1ae396f5277a0b83cf50ee
[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 "material.h"
29 #include "movepick.h"
30 #include "pawns.h"
31 #include "position.h"
32 #include "search.h"
33 #include "thread_win32_osx.h"
34
35 namespace Stockfish {
36
37 /// Thread class keeps together all the thread-related stuff. We use
38 /// per-thread pawn and material hash tables so that once we get a
39 /// pointer to an entry its life time is unlimited and we don't have
40 /// to care about someone changing the entry under our feet.
41
42 class Thread {
43
44   std::mutex mutex;
45   std::condition_variable cv;
46   size_t idx;
47   bool exit = false, searching = true; // Set before starting std::thread
48   NativeThread stdThread;
49
50 public:
51   explicit Thread(size_t);
52   virtual ~Thread();
53   virtual void search();
54   void clear();
55   void idle_loop();
56   void start_searching();
57   void wait_for_search_finished();
58   size_t id() const { return idx; }
59
60   Pawns::Table pawnsTable;
61   Material::Table materialTable;
62   size_t pvIdx, pvLast;
63   RunningAverage doubleExtensionAverage[COLOR_NB];
64   uint64_t nodesLastExplosive;
65   uint64_t nodesLastNormal;
66   std::atomic<uint64_t> nodes, tbHits, bestMoveChanges;
67   int selDepth, nmpMinPly;
68   Color nmpColor;
69   ExplosionState state;
70
71   Position rootPos;
72   StateInfo rootState;
73   Search::RootMoves rootMoves;
74   Depth rootDepth, completedDepth;
75   Value rootDelta;
76   CounterMoveHistory counterMoves;
77   ButterflyHistory mainHistory;
78   LowPlyHistory lowPlyHistory;
79   CapturePieceToHistory captureHistory;
80   ContinuationHistory continuationHistory[2][2];
81   Score trend;
82 };
83
84
85 /// MainThread is a derived class specific for main thread
86
87 struct MainThread : public Thread {
88
89   using Thread::Thread;
90
91   void search() override;
92   void check_time();
93
94   double previousTimeReduction;
95   Value bestPreviousScore;
96   Value iterValue[4];
97   int callsCnt;
98   bool stopOnPonderhit;
99   std::atomic_bool ponder;
100 };
101
102
103 /// ThreadPool struct handles all the threads-related stuff like init, starting,
104 /// parking and, most importantly, launching a thread. All the access to threads
105 /// is done through this class.
106
107 struct ThreadPool : public std::vector<Thread*> {
108
109   void start_thinking(Position&, StateListPtr&, const Search::LimitsType&, bool = false);
110   void clear();
111   void set(size_t);
112
113   MainThread* main()        const { return static_cast<MainThread*>(front()); }
114   uint64_t nodes_searched() const { return accumulate(&Thread::nodes); }
115   uint64_t tb_hits()        const { return accumulate(&Thread::tbHits); }
116   Thread* get_best_thread() const;
117   void start_searching();
118   void wait_for_search_finished() const;
119
120   std::atomic_bool stop, increaseDepth;
121
122 private:
123   StateListPtr setupStates;
124
125   uint64_t accumulate(std::atomic<uint64_t> Thread::* member) const {
126
127     uint64_t sum = 0;
128     for (Thread* th : *this)
129         sum += (th->*member).load(std::memory_order_relaxed);
130     return sum;
131   }
132 };
133
134 extern ThreadPool Threads;
135
136 } // namespace Stockfish
137
138 #endif // #ifndef THREAD_H_INCLUDED