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