Rev 154 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
| Rev 154 | Rev 169 | ||
|---|---|---|---|
| Line 4... | Line 4... | ||
| 4 | Copyright (C) 2008-2015 Marco Costalba, Joona Kiiski, Tord Romstad |
4 | Copyright (C) 2008-2015 Marco Costalba, Joona Kiiski, Tord Romstad |
| 5 | Copyright (C) 2015- |
5 | Copyright (C) 2015-2018 Marco Costalba, Joona Kiiski, Gary Linscott, Tord Romstad |
| 6 | 6 | ||
| 7 | Stockfish is free software: you can redistribute it and/or modify |
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 |
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 |
9 | the Free Software Foundation, either version 3 of the License, or |
| 10 | (at your option) any later version. |
10 | (at your option) any later version. |
| Line 20... | Line 20... | ||
| 20 | 20 | ||
| 21 | #ifndef THREAD_H_INCLUDED |
21 | #ifndef THREAD_H_INCLUDED |
| 22 | #define THREAD_H_INCLUDED |
22 | #define THREAD_H_INCLUDED |
| 23 | 23 | ||
| 24 | #include <atomic> |
24 | #include <atomic> |
| 25 | #include <bitset> |
- | |
| 26 | #include <condition_variable> |
25 | #include <condition_variable> |
| 27 | #include <mutex> |
26 | #include <mutex> |
| 28 | #include <thread> |
27 | #include <thread> |
| 29 | #include <vector> |
28 | #include <vector> |
| 30 | 29 | ||
| Line 34... | Line 33... | ||
| 34 | #include "position.h" |
33 | #include "position.h" |
| 35 | #include "search.h" |
34 | #include "search.h" |
| 36 | #include "thread_win32.h" |
35 | #include "thread_win32.h" |
| 37 | 36 | ||
| 38 | 37 | ||
| 39 | /// Thread |
38 | /// 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 |
39 | /// per-thread pawn and material hash tables so that once we get a |
| 41 | /// |
40 | /// pointer to an entry its life time is unlimited and we don't have |
| 42 | /// changing the entry under our feet. |
41 | /// to care about someone changing the entry under our feet. |
| 43 | 42 | ||
| 44 | class Thread { |
43 | class Thread { |
| 45 | 44 | ||
| 46 | std::thread nativeThread; |
- | |
| 47 | Mutex mutex; |
45 | Mutex mutex; |
| 48 | ConditionVariable |
46 | ConditionVariable cv; |
| - | 47 | size_t idx; |
|
| - | 48 | bool exit = false, searching = true; // Set before starting std::thread |
|
| 49 |
|
49 | std::thread stdThread; |
| 50 | 50 | ||
| 51 | public: |
51 | public: |
| 52 | Thread(); |
52 | explicit Thread(size_t); |
| 53 | virtual ~Thread(); |
53 | virtual ~Thread(); |
| 54 | virtual void search(); |
54 | virtual void search(); |
| - | 55 | void clear(); |
|
| 55 | void idle_loop(); |
56 | void idle_loop(); |
| 56 | void start_searching( |
57 | void start_searching(); |
| 57 | void wait_for_search_finished(); |
58 | void wait_for_search_finished(); |
| 58 | void wait(std::atomic_bool& b); |
- | |
| 59 | 59 | ||
| 60 | Pawns::Table pawnsTable; |
60 | Pawns::Table pawnsTable; |
| 61 | Material::Table materialTable; |
61 | Material::Table materialTable; |
| 62 | Endgames endgames; |
62 | Endgames endgames; |
| 63 | size_t |
63 | size_t PVIdx; |
| 64 | int |
64 | int selDepth, nmp_ply, nmp_odd; |
| 65 | uint64_t tbHits; |
65 | std::atomic<uint64_t> nodes, tbHits; |
| 66 | 66 | ||
| 67 | Position rootPos; |
67 | Position rootPos; |
| 68 | Search::RootMoves rootMoves; |
68 | Search::RootMoves rootMoves; |
| 69 | Depth rootDepth; |
- | |
| 70 | Depth completedDepth; |
69 | Depth rootDepth, completedDepth; |
| 71 |
|
70 | CounterMoveHistory counterMoves; |
| 72 |
|
71 | ButterflyHistory mainHistory; |
| 73 |
|
72 | CapturePieceToHistory captureHistory; |
| 74 | FromToStats fromTo; |
- | |
| 75 |
|
73 | ContinuationHistory contHistory; |
| 76 | }; |
74 | }; |
| 77 | 75 | ||
| 78 | 76 | ||
| 79 | /// MainThread is a derived class |
77 | /// MainThread is a derived class specific for main thread |
| 80 | 78 | ||
| 81 | struct MainThread : public Thread { |
79 | struct MainThread : public Thread { |
| 82 | virtual void search(); |
- | |
| 83 | 80 | ||
| - | 81 | using Thread::Thread; |
|
| - | 82 | ||
| - | 83 | void search() override; |
|
| - | 84 | void check_time(); |
|
| - | 85 | ||
| 84 | bool |
86 | bool failedLow; |
| 85 | double |
87 | double bestMoveChanges, previousTimeReduction; |
| 86 | Value previousScore; |
88 | Value previousScore; |
| - | 89 | int callsCnt; |
|
| 87 | }; |
90 | }; |
| 88 | 91 | ||
| 89 | 92 | ||
| 90 | /// ThreadPool struct handles all the threads-related stuff like init, starting, |
93 | /// ThreadPool struct handles all the threads-related stuff like init, starting, |
| 91 | /// parking and, most importantly, launching a thread. All the access to threads |
94 | /// parking and, most importantly, launching a thread. All the access to threads |
| 92 | /// |
95 | /// is done through this class. |
| 93 | 96 | ||
| 94 | struct ThreadPool : public std::vector<Thread*> { |
97 | struct ThreadPool : public std::vector<Thread*> { |
| 95 | 98 | ||
| 96 | void |
99 | void start_thinking(Position&, StateListPtr&, const Search::LimitsType&, bool = false); |
| - | 100 | void clear(); |
|
| 97 | void |
101 | void set(size_t); |
| 98 | 102 | ||
| 99 | MainThread* main() { return static_cast<MainThread*>( |
103 | MainThread* main() const { return static_cast<MainThread*>(front()); } |
| 100 |
|
104 | uint64_t nodes_searched() const { return accumulate(&Thread::nodes); } |
| 101 | void read_uci_options(); |
- | |
| 102 | uint64_t |
105 | uint64_t tb_hits() const { return accumulate(&Thread::tbHits); } |
| - | 106 | ||
| 103 |
|
107 | std::atomic_bool stop, ponder, stopOnPonderhit; |
| 104 | 108 | ||
| 105 | private: |
109 | private: |
| 106 | StateListPtr setupStates; |
110 | StateListPtr setupStates; |
| - | 111 | ||
| - | 112 | uint64_t accumulate(std::atomic<uint64_t> Thread::* member) const { |
|
| - | 113 | ||
| - | 114 | uint64_t sum = 0; |
|
| - | 115 | for (Thread* th : *this) |
|
| - | 116 | sum += (th->*member).load(std::memory_order_relaxed); |
|
| - | 117 | return sum; |
|
| - | 118 | } |
|
| 107 | }; |
119 | }; |
| 108 | 120 | ||
| 109 | extern ThreadPool Threads; |
121 | extern ThreadPool Threads; |
| 110 | 122 | ||
| 111 | #endif // #ifndef THREAD_H_INCLUDED |
123 | #endif // #ifndef THREAD_H_INCLUDED |