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