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