-/// ThreadsManager class is used to handle 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:
- Thread& operator[](int threadID) { return threads[threadID]; }
- void init();
- void exit();
- void init_hash_tables();
-
- bool use_sleeping_threads() const { return useSleepingThreads; }
- int min_split_depth() const { return minimumSplitDepth; }
- int size() const { return activeThreads; }
-
- void set_size(int cnt);
- void read_uci_options();
- bool available_slave_exists(int master) const;
-
- template <bool Fake>
- Value split(Position& pos, SearchStack* ss, Value alpha, Value beta, Value bestValue,
- Depth depth, Move threatMove, int moveCount, MovePicker* mp, int nodeType);
-private:
- Thread threads[MAX_THREADS];
- Lock threadsLock;
- Depth minimumSplitDepth;
- int maxThreadsPerSplitPoint;
- int activeThreads;
- 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;
+ }