- // 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:
- void init_threads();
- void exit_threads();
-
- int min_split_depth() const { return minimumSplitDepth; }
- int active_threads() const { return activeThreads; }
- void set_active_threads(int cnt) { activeThreads = cnt; }
-
- void read_uci_options();
- bool available_thread_exists(int master) const;
- bool thread_is_available(int slave, int master) const;
- bool cutoff_at_splitpoint(int threadID) const;
- void wake_sleeping_thread(int threadID);
- void idle_loop(int threadID, SplitPoint* sp);
-
- template <bool Fake>
- void split(Position& pos, SearchStack* ss, Value* alpha, const Value beta, Value* bestValue,
- Depth depth, Move threatMove, int moveCount, MovePicker* mp, bool pvNode);
-
- private:
- Depth minimumSplitDepth;
- int maxThreadsPerSplitPoint;
- bool useSleepingThreads;
- int activeThreads;
- volatile bool allThreadsShouldExit;
- Thread threads[MAX_THREADS];
- Lock mpLock, sleepLock[MAX_THREADS];
- WaitCondition sleepCond[MAX_THREADS];
- };
-
-
- // RootMove struct is used for moves at the root of the tree. For each root
- // move, we store two scores, a node count, and a PV (really a refutation
- // in the case of moves which fail low). Value pv_score is normally set at
- // -VALUE_INFINITE for all non-pv moves, while non_pv_score is computed
- // according to the order in which moves are returned by MovePicker.
-
- struct RootMove {
-
- RootMove();
- RootMove(const RootMove& rm) { *this = rm; }
- RootMove& operator=(const RootMove& rm);
-
- // RootMove::operator<() is the comparison function used when
- // sorting the moves. A move m1 is considered to be better
- // than a move m2 if it has an higher pv_score, or if it has
- // equal pv_score but m1 has the higher non_pv_score. In this way
- // we are guaranteed that PV moves are always sorted as first.
- bool operator<(const RootMove& m) const {
- return pv_score != m.pv_score ? pv_score < m.pv_score
- : non_pv_score < m.non_pv_score;
- }
-
- void extract_pv_from_tt(Position& pos);
- void insert_pv_in_tt(Position& pos);
- std::string pv_info_to_uci(Position& pos, int depth, Value alpha, Value beta, int pvIdx);
-
- int64_t nodes;
- Value pv_score;
- Value non_pv_score;
- Move pv[PLY_MAX_PLUS_2];
- };
-
-
- // RootMoveList struct is just a std::vector<> of RootMove objects,
- // with an handful of methods above the standard ones.
-
- struct RootMoveList : public std::vector<RootMove> {
-
- typedef std::vector<RootMove> Base;
-
- void init(Position& pos, Move searchMoves[]);
- void sort() { insertion_sort<RootMove, Base::iterator>(begin(), end()); }
- void sort_multipv(int n) { insertion_sort<RootMove, Base::iterator>(begin(), begin() + n); }
-
- int bestMoveChanges;
- };
-
-
- // Overload operator<<() to make it easier to print moves in a coordinate
- // notation compatible with UCI protocol.
- std::ostream& operator<<(std::ostream& os, Move m) {
-
- bool chess960 = (os.iword(0) != 0); // See set960()
- return os << move_to_uci(m, chess960);
- }
-
-
- // When formatting a move for std::cout we must know if we are in Chess960
- // or not. To keep using the handy operator<<() on the move the trick is to
- // embed this flag in the stream itself. Function-like named enum set960 is
- // used as a custom manipulator and the stream internal general-purpose array,
- // accessed through ios_base::iword(), is used to pass the flag to the move's
- // operator<<() that will read it to properly format castling moves.
- enum set960 {};
-
- std::ostream& operator<< (std::ostream& os, const set960& f) {
-
- os.iword(0) = int(f);
- return os;
- }
-
-
- /// Adjustments
-
- // Step 6. Razoring
-