From 5b8ca1eee77b736c35b418b1cb11da9fc66f83e0 Mon Sep 17 00:00:00 2001 From: Marco Costalba Date: Sun, 4 Dec 2011 11:46:31 +0100 Subject: [PATCH] Move SearchStack under Search namespace No functional change. Signed-off-by: Marco Costalba --- src/movepick.cpp | 2 +- src/movepick.h | 5 ++--- src/search.cpp | 31 +++++++++++++++---------------- src/search.h | 20 +++++++++++--------- src/thread.cpp | 25 ++++++++++++++----------- src/thread.h | 4 ++-- 6 files changed, 45 insertions(+), 42 deletions(-) diff --git a/src/movepick.cpp b/src/movepick.cpp index 2b20f25f..0b3b6fd6 100644 --- a/src/movepick.cpp +++ b/src/movepick.cpp @@ -72,7 +72,7 @@ namespace { /// move ordering is at the current node. MovePicker::MovePicker(const Position& p, Move ttm, Depth d, const History& h, - SearchStack* ss, Value beta) : pos(p), H(h), depth(d) { + Search::Stack* ss, Value beta) : pos(p), H(h), depth(d) { captureThreshold = 0; badCaptures = moves + MAX_MOVES; diff --git a/src/movepick.h b/src/movepick.h index 34329582..b2a8dc98 100644 --- a/src/movepick.h +++ b/src/movepick.h @@ -22,10 +22,9 @@ #include "history.h" #include "position.h" +#include "search.h" #include "types.h" -struct SearchStack; - /// MovePicker is a class which is used to pick one pseudo legal move at a time /// from the current position. It is initialized with a Position object and a few /// moves we have reason to believe are good. The most important method is @@ -39,7 +38,7 @@ class MovePicker { MovePicker& operator=(const MovePicker&); // Silence a warning under MSVC public: - MovePicker(const Position&, Move, Depth, const History&, SearchStack*, Value); + MovePicker(const Position&, Move, Depth, const History&, Search::Stack*, Value); MovePicker(const Position&, Move, Depth, const History&, Square recaptureSq); MovePicker(const Position&, Move, const History&, PieceType parentCapture); Move get_next_move(); diff --git a/src/search.cpp b/src/search.cpp index e625405d..77acd89e 100644 --- a/src/search.cpp +++ b/src/search.cpp @@ -38,12 +38,6 @@ #include "tt.h" #include "ucioption.h" -using std::cout; -using std::endl; -using std::string; -using Search::Signals; -using Search::Limits; - namespace Search { volatile SignalsType Signals; @@ -52,6 +46,11 @@ namespace Search { Position RootPosition; } +using std::cout; +using std::endl; +using std::string; +using namespace Search; + namespace { // Set to true to force running with one thread. Used for debugging @@ -186,10 +185,10 @@ namespace { Move id_loop(Position& pos, Move rootMoves[], Move* ponderMove); template - Value search(Position& pos, SearchStack* ss, Value alpha, Value beta, Depth depth); + Value search(Position& pos, Stack* ss, Value alpha, Value beta, Depth depth); template - Value qsearch(Position& pos, SearchStack* ss, Value alpha, Value beta, Depth depth); + Value qsearch(Position& pos, Stack* ss, Value alpha, Value beta, Depth depth); bool check_is_dangerous(Position &pos, Move move, Value futilityBase, Value beta, Value *bValue); bool connected_moves(const Position& pos, Move m1, Move m2); @@ -213,14 +212,14 @@ namespace { // we simply create and use a standard MovePicker object. template struct MovePickerExt : public MovePicker { - MovePickerExt(const Position& p, Move ttm, Depth d, const History& h, SearchStack* ss, Value b) + MovePickerExt(const Position& p, Move ttm, Depth d, const History& h, Stack* ss, Value b) : MovePicker(p, ttm, d, h, ss, b) {} }; // In case of a SpNode we use split point's shared MovePicker object as moves source template<> struct MovePickerExt : public MovePicker { - MovePickerExt(const Position& p, Move ttm, Depth d, const History& h, SearchStack* ss, Value b) + MovePickerExt(const Position& p, Move ttm, Depth d, const History& h, Stack* ss, Value b) : MovePicker(p, ttm, d, h, ss, b), mp(ss->sp->mp) {} Move get_next_move() { return mp->get_next_move(); } @@ -487,7 +486,7 @@ namespace { Move id_loop(Position& pos, Move rootMoves[], Move* ponderMove) { - SearchStack ss[PLY_MAX_PLUS_2]; + Stack ss[PLY_MAX_PLUS_2]; Value bestValues[PLY_MAX_PLUS_2]; int bestMoveChanges[PLY_MAX_PLUS_2]; int depth, aspirationDelta; @@ -496,7 +495,7 @@ namespace { bool bestMoveNeverChanged = true; // Initialize stuff before a new search - memset(ss, 0, 4 * sizeof(SearchStack)); + memset(ss, 0, 4 * sizeof(Stack)); TT.new_search(); H.clear(); *ponderMove = bestMove = skillBest = skillPonder = MOVE_NONE; @@ -708,7 +707,7 @@ namespace { // here: This is taken care of after we return from the split point. template - Value search(Position& pos, SearchStack* ss, Value alpha, Value beta, Depth depth) { + Value search(Position& pos, Stack* ss, Value alpha, Value beta, Depth depth) { const bool PvNode = (NT == PV || NT == Root || NT == SplitPointPV || NT == SplitPointRoot); const bool SpNode = (NT == SplitPointPV || NT == SplitPointNonPV || NT == SplitPointRoot); @@ -1297,7 +1296,7 @@ split_point_start: // At split points actual search starts from here // less than ONE_PLY). template - Value qsearch(Position& pos, SearchStack* ss, Value alpha, Value beta, Depth depth) { + Value qsearch(Position& pos, Stack* ss, Value alpha, Value beta, Depth depth) { const bool PvNode = (NT == PV); @@ -2101,11 +2100,11 @@ void Thread::idle_loop(SplitPoint* sp) { assert(!do_terminate); // Copy split point position and search stack and call search() - SearchStack ss[PLY_MAX_PLUS_2]; + Stack ss[PLY_MAX_PLUS_2]; SplitPoint* tsp = splitPoint; Position pos(*tsp->pos, threadID); - memcpy(ss, tsp->ss - 1, 4 * sizeof(SearchStack)); + memcpy(ss, tsp->ss - 1, 4 * sizeof(Stack)); (ss+1)->sp = tsp; if (tsp->nodeType == Root) diff --git a/src/search.h b/src/search.h index 53e7444c..02aaa652 100644 --- a/src/search.h +++ b/src/search.h @@ -28,12 +28,13 @@ class Position; struct SplitPoint; -/// The SearchStack struct keeps track of the information we need to remember -/// from nodes shallower and deeper in the tree during the search. Each -/// search thread has its own array of SearchStack objects, indexed by the -/// current ply. +namespace Search { + +/// The Stack struct keeps track of the information we need to remember from +/// nodes shallower and deeper in the tree during the search. Each search thread +/// has its own array of Stack objects, indexed by the current ply. -struct SearchStack { +struct Stack { SplitPoint* sp; int ply; Move currentMove; @@ -46,9 +47,8 @@ struct SearchStack { int skipNullMove; }; -namespace Search { -/// The SearchLimits struct stores information sent by GUI about available time +/// The LimitsType struct stores information sent by GUI about available time /// to search the current move, maximum depth/time, if we are in analysis mode /// or if we have to ponder while is our opponent's side to move. @@ -60,6 +60,10 @@ struct LimitsType { int time, increment, movesToGo, maxTime, maxDepth, maxNodes, infinite, ponder; }; + +/// The SignalsType struct stores volatile flags updated during the search +/// typically in an async fashion, for instance to stop the search by the GUI. + struct SignalsType { bool stopOnPonderhit, firstRootMove, stop, failedLowAtRoot; }; @@ -75,6 +79,4 @@ extern void think(); } // namespace -extern void do_timer_event(); - #endif // !defined(SEARCH_H_INCLUDED) diff --git a/src/thread.cpp b/src/thread.cpp index 8af9dc41..cf35c288 100644 --- a/src/thread.cpp +++ b/src/thread.cpp @@ -23,6 +23,8 @@ #include "thread.h" #include "ucioption.h" +using namespace Search; + ThreadsManager Threads; // Global object namespace { extern "C" { @@ -257,7 +259,7 @@ bool ThreadsManager::split_point_finished(SplitPoint* sp) const { // search(). When all threads have returned from search() then split() returns. template -Value ThreadsManager::split(Position& pos, SearchStack* ss, Value alpha, Value beta, +Value ThreadsManager::split(Position& pos, Stack* ss, Value alpha, Value beta, Value bestValue, Depth depth, Move threatMove, int moveCount, MovePicker* mp, int nodeType) { assert(pos.pos_is_ok()); @@ -359,12 +361,13 @@ Value ThreadsManager::split(Position& pos, SearchStack* ss, Value alpha, Value b } // Explicit template instantiations -template Value ThreadsManager::split(Position&, SearchStack*, Value, Value, Value, Depth, Move, int, MovePicker*, int); -template Value ThreadsManager::split(Position&, SearchStack*, Value, Value, Value, Depth, Move, int, MovePicker*, int); +template Value ThreadsManager::split(Position&, Stack*, Value, Value, Value, Depth, Move, int, MovePicker*, int); +template Value ThreadsManager::split(Position&, Stack*, Value, Value, Value, Depth, Move, int, MovePicker*, int); // Thread::timer_loop() is where the timer thread waits maxPly milliseconds and // then calls do_timer_event(). If maxPly is 0 thread sleeps until is woken up. +extern void do_timer_event(); void Thread::timer_loop() { @@ -417,7 +420,7 @@ void Thread::main_loop() { if (do_terminate) return; - Search::think(); // This is the search entry point + think(); // This is the search entry point } } @@ -427,7 +430,7 @@ void Thread::main_loop() { // then function returns immediately, otherwise caller is blocked waiting for // the search to finish. -void ThreadsManager::start_thinking(const Position& pos, const Search::LimitsType& limits, +void ThreadsManager::start_thinking(const Position& pos, const LimitsType& limits, const std::vector& searchMoves, bool asyncMode) { Thread& main = threads[0]; @@ -438,12 +441,12 @@ void ThreadsManager::start_thinking(const Position& pos, const Search::LimitsTyp cond_wait(&sleepCond, &main.sleepLock); // Copy input arguments to initialize the search - Search::RootPosition.copy(pos, 0); - Search::Limits = limits; - Search::RootMoves = searchMoves; + RootPosition.copy(pos, 0); + Limits = limits; + RootMoves = searchMoves; // Reset signals before to start the new search - memset((void*)&Search::Signals, 0, sizeof(Search::Signals)); + memset((void*)&Signals, 0, sizeof(Signals)); main.do_sleep = false; cond_signal(&main.sleepCond); // Wake up main thread and start searching @@ -464,13 +467,13 @@ void ThreadsManager::start_thinking(const Position& pos, const Search::LimitsTyp void ThreadsManager::wait_for_stop_or_ponderhit() { - Search::Signals.stopOnPonderhit = true; + Signals.stopOnPonderhit = true; Thread& main = threads[0]; lock_grab(&main.sleepLock); - while (!Search::Signals.stop) + while (!Signals.stop) cond_wait(&main.sleepCond, &main.sleepLock); lock_release(&main.sleepLock); diff --git a/src/thread.h b/src/thread.h index 13615c2c..dcaa5ca3 100644 --- a/src/thread.h +++ b/src/thread.h @@ -46,7 +46,7 @@ struct SplitPoint { // Const pointers to shared data MovePicker* mp; - SearchStack* ss; + Search::Stack* ss; // Shared data Lock lock; @@ -122,7 +122,7 @@ public: const std::vector& searchMoves, bool asyncMode); template - Value split(Position& pos, SearchStack* ss, Value alpha, Value beta, Value bestValue, + Value split(Position& pos, Search::Stack* ss, Value alpha, Value beta, Value bestValue, Depth depth, Move threatMove, int moveCount, MovePicker* mp, int nodeType); private: friend struct Thread; -- 2.39.2