]> git.sesse.net Git - stockfish/blob - src/thread.h
Introduce pawn structure based history
[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 <cstddef>
25 #include <cstdint>
26 #include <mutex>
27 #include <vector>
28
29 #include "movepick.h"
30 #include "position.h"
31 #include "search.h"
32 #include "thread_win32_osx.h"
33 #include "types.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 lifetime is unlimited and we don't have
40 // to care about someone changing the entry under our feet.
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     size_t       id() const { return idx; }
58
59     size_t                pvIdx, pvLast;
60     std::atomic<uint64_t> nodes, tbHits, bestMoveChanges;
61     int                   selDepth, nmpMinPly;
62     Value                 bestValue, optimism[COLOR_NB];
63
64     Position              rootPos;
65     StateInfo             rootState;
66     Search::RootMoves     rootMoves;
67     Depth                 rootDepth, completedDepth;
68     Value                 rootDelta;
69     Value                 rootSimpleEval;
70     CounterMoveHistory    counterMoves;
71     ButterflyHistory      mainHistory;
72     CapturePieceToHistory captureHistory;
73     ContinuationHistory   continuationHistory[2][2];
74     PawnHistory           pawnHistory;
75 };
76
77
78 // MainThread is a derived class specific for main thread
79 struct MainThread: public Thread {
80
81     using Thread::Thread;
82
83     void search() override;
84     void check_time();
85
86     double           previousTimeReduction;
87     Value            bestPreviousScore;
88     Value            bestPreviousAverageScore;
89     Value            iterValue[4];
90     int              callsCnt;
91     bool             stopOnPonderhit;
92     std::atomic_bool ponder;
93 };
94
95
96 // ThreadPool struct handles all the threads-related stuff like init, starting,
97 // parking and, most importantly, launching a thread. All the access to threads
98 // is done through this class.
99 struct ThreadPool {
100
101     void start_thinking(Position&, StateListPtr&, const Search::LimitsType&, bool = false);
102     void clear();
103     void set(size_t);
104
105     MainThread* main() const { return static_cast<MainThread*>(threads.front()); }
106     uint64_t    nodes_searched() const { return accumulate(&Thread::nodes); }
107     uint64_t    tb_hits() const { return accumulate(&Thread::tbHits); }
108     Thread*     get_best_thread() const;
109     void        start_searching();
110     void        wait_for_search_finished() const;
111
112     std::atomic_bool stop, increaseDepth;
113
114     auto cbegin() const noexcept { return threads.cbegin(); }
115     auto begin() noexcept { return threads.begin(); }
116     auto end() noexcept { return threads.end(); }
117     auto cend() const noexcept { return threads.cend(); }
118     auto size() const noexcept { return threads.size(); }
119     auto empty() const noexcept { return threads.empty(); }
120
121    private:
122     StateListPtr         setupStates;
123     std::vector<Thread*> threads;
124
125     uint64_t accumulate(std::atomic<uint64_t> Thread::*member) const {
126
127         uint64_t sum = 0;
128         for (Thread* th : threads)
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