-/// ThreadsManager class handles all the threads related stuff like init, starting,
-/// parking and, the most important, launching a slave thread at a split point.
-/// All the access to shared thread data is done through this class.
-
-class ThreadsManager {
- /* As long as the single ThreadsManager object is defined as a global we don't
- need to explicitly initialize to zero its data members because variables with
- static storage duration are automatically set to zero before enter main()
- */
-public:
- void init(); // No c'tor becuase Threads is static and we need engine initialized
- ~ThreadsManager();
-
- Thread& operator[](int id) { return *threads[id]; }
- bool use_sleeping_threads() const { return useSleepingThreads; }
- int min_split_depth() const { return minimumSplitDepth; }
- int size() const { return (int)threads.size(); }
- Thread* main_thread() { return threads[0]; }
-
- void wake_up() const;
- void sleep() const;
- void read_uci_options();
- bool available_slave_exists(Thread* master) const;
- void set_timer(int msec);
- void wait_for_search_finished();
- void start_searching(const Position& pos, const Search::LimitsType& limits,
- const std::vector<Move>& searchMoves);
-
- template <bool Fake>
- Value split(Position& pos, Search::Stack* ss, Value alpha, Value beta, Value bestValue, Move* bestMove,
- Depth depth, Move threatMove, int moveCount, MovePicker* mp, int nodeType);
-private:
- friend class Thread;
-
- std::vector<Thread*> threads;
- Thread* timer;
- Lock splitLock;
- WaitCondition sleepCond;
- Depth minimumSplitDepth;
- int maxThreadsPerSplitPoint;
- bool useSleepingThreads;
+// ThreadPool struct handles all the threads-related stuff like init, starting,
+// parking and, most importantly, launching a thread. All the access to threads
+// is done through this class.
+class ThreadPool {
+ public:
+ ThreadPool() {}
+
+ ~ThreadPool() {
+ // destroy any existing thread(s)
+ if (threads.size() > 0)
+ {
+ main_thread()->wait_for_search_finished();
+
+ threads.clear();
+ }
+ }
+
+ ThreadPool(const ThreadPool&) = delete;
+ ThreadPool(ThreadPool&&) = delete;
+
+ ThreadPool& operator=(const ThreadPool&) = delete;
+ ThreadPool& operator=(ThreadPool&&) = delete;
+
+ void start_thinking(const OptionsMap&, Position&, StateListPtr&, Search::LimitsType);
+ void run_on_thread(size_t threadId, std::function<void()> f);
+ void wait_on_thread(size_t threadId);
+ size_t num_threads() const;
+ void clear();
+ void set(const NumaConfig& numaConfig,
+ Search::SharedState,
+ const Search::SearchManager::UpdateContext&);
+
+ Search::SearchManager* main_manager();
+ Thread* main_thread() const { return threads.front().get(); }
+ uint64_t nodes_searched() const;
+ uint64_t tb_hits() const;
+ Thread* get_best_thread() const;
+ void start_searching();
+ void wait_for_search_finished() const;
+
+ std::vector<size_t> get_bound_thread_count_by_numa_node() const;
+
+ void ensure_network_replicated();
+
+ std::atomic_bool stop, abortedSearch, increaseDepth;
+
+ auto cbegin() const noexcept { return threads.cbegin(); }
+ auto begin() noexcept { return threads.begin(); }
+ auto end() noexcept { return threads.end(); }
+ auto cend() const noexcept { return threads.cend(); }
+ auto size() const noexcept { return threads.size(); }
+ auto empty() const noexcept { return threads.empty(); }
+
+ private:
+ StateListPtr setupStates;
+ std::vector<std::unique_ptr<Thread>> threads;
+ std::vector<NumaIndex> boundThreadToNumaNode;
+
+ uint64_t accumulate(std::atomic<uint64_t> Search::Worker::*member) const {
+
+ uint64_t sum = 0;
+ for (auto&& th : threads)
+ sum += (th->worker.get()->*member).load(std::memory_order_relaxed);
+ return sum;
+ }