From 41641e3b1eea0038ab6984766d2b3bca869be7fa Mon Sep 17 00:00:00 2001 From: Marco Costalba Date: Sun, 9 Feb 2014 17:31:45 +0100 Subject: [PATCH] Assorted tweaks from DON Mainly renames and some little code style improvment, inspired by looking at DON sources: https://github.com/erashid/DON No functional change. --- src/benchmark.cpp | 6 ++-- src/bitboard.cpp | 4 +-- src/main.cpp | 2 +- src/misc.cpp | 4 +-- src/misc.h | 6 ++-- src/position.h | 10 +++--- src/rkiss.h | 8 ++--- src/search.cpp | 6 ++-- src/search.h | 5 +-- src/thread.cpp | 84 +++++++++++++++++++++++------------------------ src/thread.h | 5 ++- src/tt.cpp | 4 +-- src/tt.h | 2 +- src/uci.cpp | 5 ++- src/ucioption.cpp | 12 +++---- src/ucioption.h | 12 +++---- 16 files changed, 87 insertions(+), 88 deletions(-) diff --git a/src/benchmark.cpp b/src/benchmark.cpp index 7ca4b59e..63a9d84c 100644 --- a/src/benchmark.cpp +++ b/src/benchmark.cpp @@ -126,7 +126,7 @@ void benchmark(const Position& current, istream& is) { file.close(); } - int64_t nodes = 0; + uint64_t nodes = 0; Search::StateStackPtr st; Time::point elapsed = Time::now(); @@ -138,13 +138,13 @@ void benchmark(const Position& current, istream& is) { if (limitType == "perft") { - size_t cnt = Search::perft(pos, limits.depth * ONE_PLY); + uint64_t cnt = Search::perft(pos, limits.depth * ONE_PLY); cerr << "\nPerft " << limits.depth << " leaf nodes: " << cnt << endl; nodes += cnt; } else { - Threads.start_thinking(pos, limits, vector(), st); + Threads.start_thinking(pos, limits, st); Threads.wait_for_think_finished(); nodes += Search::RootPos.nodes_searched(); } diff --git a/src/bitboard.cpp b/src/bitboard.cpp index 9adf1407..6784effd 100644 --- a/src/bitboard.cpp +++ b/src/bitboard.cpp @@ -325,11 +325,11 @@ namespace { if (attack && attack != reference[i]) break; - assert(reference[i] != 0); + assert(reference[i]); attack = reference[i]; } - } while (i != size); + } while (i < size); } } } diff --git a/src/main.cpp b/src/main.cpp index 98f6f387..dd0b7ff8 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -40,7 +40,7 @@ int main(int argc, char* argv[]) { Pawns::init(); Eval::init(); Threads.init(); - TT.set_size(Options["Hash"]); + TT.resize(Options["Hash"]); std::string args; diff --git a/src/misc.cpp b/src/misc.cpp index b650b960..99c00ebe 100644 --- a/src/misc.cpp +++ b/src/misc.cpp @@ -144,10 +144,10 @@ std::ostream& operator<<(std::ostream& os, SyncCout sc) { static Mutex m; - if (sc == io_lock) + if (sc == IO_LOCK) m.lock(); - if (sc == io_unlock) + if (sc == IO_UNLOCK) m.unlock(); return os; diff --git a/src/misc.h b/src/misc.h index d7addd84..c4232a48 100644 --- a/src/misc.h +++ b/src/misc.h @@ -59,10 +59,10 @@ private: }; -enum SyncCout { io_lock, io_unlock }; +enum SyncCout { IO_LOCK, IO_UNLOCK }; std::ostream& operator<<(std::ostream&, SyncCout); -#define sync_cout std::cout << io_lock -#define sync_endl std::endl << io_unlock +#define sync_cout std::cout << IO_LOCK +#define sync_endl std::endl << IO_UNLOCK #endif // #ifndef MISC_H_INCLUDED diff --git a/src/position.h b/src/position.h index 8fc62c98..5d3048ae 100644 --- a/src/position.h +++ b/src/position.h @@ -159,8 +159,8 @@ public: int game_ply() const; bool is_chess960() const; Thread* this_thread() const; - int64_t nodes_searched() const; - void set_nodes_searched(int64_t n); + uint64_t nodes_searched() const; + void set_nodes_searched(uint64_t n); bool is_draw() const; // Position consistency check, for debugging @@ -201,7 +201,7 @@ private: Square castlingRookSquare[COLOR_NB][CASTLING_SIDE_NB]; Bitboard castlingPath[COLOR_NB][CASTLING_SIDE_NB]; StateInfo startState; - int64_t nodes; + uint64_t nodes; int gamePly; Color sideToMove; Thread* thisThread; @@ -209,11 +209,11 @@ private: int chess960; }; -inline int64_t Position::nodes_searched() const { +inline uint64_t Position::nodes_searched() const { return nodes; } -inline void Position::set_nodes_searched(int64_t n) { +inline void Position::set_nodes_searched(uint64_t n) { nodes = n; } diff --git a/src/rkiss.h b/src/rkiss.h index 23bd614e..b9b82cef 100644 --- a/src/rkiss.h +++ b/src/rkiss.h @@ -45,15 +45,15 @@ class RKISS { uint64_t a, b, c, d; - uint64_t rotate(uint64_t x, uint64_t k) const { + uint64_t rotate_L(uint64_t x, unsigned k) const { return (x << k) | (x >> (64 - k)); } uint64_t rand64() { - const uint64_t e = a - rotate(b, 7); - a = b ^ rotate(c, 13); - b = c + rotate(d, 37); + const uint64_t e = a - rotate_L(b, 7); + a = b ^ rotate_L(c, 13); + b = c + rotate_L(d, 37); c = d + e; return d = e + a; } diff --git a/src/search.cpp b/src/search.cpp index 0b57c1fe..097faf90 100644 --- a/src/search.cpp +++ b/src/search.cpp @@ -154,10 +154,10 @@ void Search::init() { /// Search::perft() is our utility to verify move generation. All the leaf nodes /// up to the given depth are generated and counted and the sum returned. -static size_t perft(Position& pos, Depth depth) { +static uint64_t perft(Position& pos, Depth depth) { StateInfo st; - size_t cnt = 0; + uint64_t cnt = 0; CheckInfo ci(pos); const bool leaf = depth == 2 * ONE_PLY; @@ -170,7 +170,7 @@ static size_t perft(Position& pos, Depth depth) { return cnt; } -size_t Search::perft(Position& pos, Depth depth) { +uint64_t Search::perft(Position& pos, Depth depth) { return depth > ONE_PLY ? ::perft(pos, depth) : MoveList(pos).size(); } diff --git a/src/search.h b/src/search.h index 25e71a84..2fd8d47d 100644 --- a/src/search.h +++ b/src/search.h @@ -81,6 +81,7 @@ struct LimitsType { LimitsType() { std::memset(this, 0, sizeof(LimitsType)); } bool use_time_management() const { return !(mate | movetime | depth | nodes | infinite); } + std::vector searchmoves; int time[COLOR_NB], inc[COLOR_NB], movestogo, depth, nodes, movetime, mate, infinite, ponder; }; @@ -89,7 +90,7 @@ struct LimitsType { /// typically in an async fashion e.g. to stop the search by the GUI. struct SignalsType { - bool stopOnPonderhit, firstRootMove, stop, failedLowAtRoot; + bool stop, stopOnPonderhit, firstRootMove, failedLowAtRoot; }; typedef std::auto_ptr > StateStackPtr; @@ -103,7 +104,7 @@ extern Time::point SearchTime, IterationTime; extern StateStackPtr SetupStates; extern void init(); -extern size_t perft(Position& pos, Depth depth); +extern uint64_t perft(Position& pos, Depth depth); extern void think(); } // namespace Search diff --git a/src/thread.cpp b/src/thread.cpp index fdb99e62..e5bcc64d 100644 --- a/src/thread.cpp +++ b/src/thread.cpp @@ -29,6 +29,8 @@ using namespace Search; ThreadPool Threads; // Global object +extern void check_time(); + namespace { // start_routine() is the C function which is called when a new thread @@ -90,9 +92,43 @@ Thread::Thread() /* : splitPoints() */ { // Value-initialization bug in MSVC } +// Thread::cutoff_occurred() checks whether a beta cutoff has occurred in the +// current active split point, or in some ancestor of the split point. + +bool Thread::cutoff_occurred() const { + + for (SplitPoint* sp = activeSplitPoint; sp; sp = sp->parentSplitPoint) + if (sp->cutoff) + return true; + + return false; +} + + +// Thread::available_to() checks whether the thread is available to help the +// thread 'master' at a split point. An obvious requirement is that thread must +// be idle. With more than two threads, this is not sufficient: If the thread is +// the master of some split point, it is only available as a slave to the slaves +// which are busy searching the split point at the top of slave's split point +// stack (the "helpful master concept" in YBWC terminology). + +bool Thread::available_to(const Thread* master) const { + + if (searching) + return false; + + // Make a local copy to be sure it doesn't become zero under our feet while + // testing next condition and so leading to an out of bounds access. + int size = splitPointsSize; + + // No split points means that the thread is available as a slave for any + // other thread otherwise apply the "helpful master" concept if possible. + return !size || (splitPoints[size - 1].slavesMask & (1ULL << master->idx)); +} + + // TimerThread::idle_loop() is where the timer thread waits msec milliseconds // and then calls check_time(). If msec is 0 thread sleeps until it's woken up. -extern void check_time(); void TimerThread::idle_loop() { @@ -144,41 +180,6 @@ void MainThread::idle_loop() { } -// Thread::cutoff_occurred() checks whether a beta cutoff has occurred in the -// current active split point, or in some ancestor of the split point. - -bool Thread::cutoff_occurred() const { - - for (SplitPoint* sp = activeSplitPoint; sp; sp = sp->parentSplitPoint) - if (sp->cutoff) - return true; - - return false; -} - - -// Thread::available_to() checks whether the thread is available to help the -// thread 'master' at a split point. An obvious requirement is that thread must -// be idle. With more than two threads, this is not sufficient: If the thread is -// the master of some split point, it is only available as a slave to the slaves -// which are busy searching the split point at the top of slave's split point -// stack (the "helpful master concept" in YBWC terminology). - -bool Thread::available_to(const Thread* master) const { - - if (searching) - return false; - - // Make a local copy to be sure it doesn't become zero under our feet while - // testing next condition and so leading to an out of bounds access. - int size = splitPointsSize; - - // No split points means that the thread is available as a slave for any - // other thread otherwise apply the "helpful master" concept if possible. - return !size || (splitPoints[size - 1].slavesMask & (1ULL << master->idx)); -} - - // init() is called at startup to create and launch requested threads, that will // go immediately to sleep due to 'sleepWhileIdle' set to true. We cannot use // a c'tor because Threads is a static object and we need a fully initialized @@ -264,8 +265,7 @@ void Thread::split(Position& pos, const Stack* ss, Value alpha, Value beta, Valu MovePicker* movePicker, int nodeType, bool cutNode) { assert(pos.pos_is_ok()); - assert(*bestValue <= alpha && alpha < beta && beta <= VALUE_INFINITE); - assert(*bestValue > -VALUE_INFINITE); + assert(-VALUE_INFINITE < *bestValue && *bestValue <= alpha && alpha < beta && beta <= VALUE_INFINITE); assert(depth >= Threads.minimumSplitDepth); assert(searching); assert(splitPointsSize < MAX_SPLITPOINTS_PER_THREAD); @@ -367,8 +367,8 @@ void ThreadPool::wait_for_think_finished() { // start_thinking() wakes up the main thread sleeping in MainThread::idle_loop() // so to start a new search, then returns immediately. -void ThreadPool::start_thinking(const Position& pos, const LimitsType& limits, - const std::vector& searchMoves, StateStackPtr& states) { +void ThreadPool::start_thinking(const Position& pos, const LimitsType& limits, StateStackPtr& states) { + wait_for_think_finished(); SearchTime = Time::now(); // As early as possible @@ -386,8 +386,8 @@ void ThreadPool::start_thinking(const Position& pos, const LimitsType& limits, } for (MoveList it(pos); *it; ++it) - if ( searchMoves.empty() - || std::count(searchMoves.begin(), searchMoves.end(), *it)) + if ( limits.searchmoves.empty() + || std::count(limits.searchmoves.begin(), limits.searchmoves.end(), *it)) RootMoves.push_back(RootMove(*it)); main()->thinking = true; diff --git a/src/thread.h b/src/thread.h index c5690a02..c9152b22 100644 --- a/src/thread.h +++ b/src/thread.h @@ -76,7 +76,7 @@ struct SplitPoint { // Shared data Mutex mutex; volatile uint64_t slavesMask; - volatile int64_t nodes; + volatile uint64_t nodes; volatile Value alpha; volatile Value bestValue; volatile Move bestMove; @@ -162,8 +162,7 @@ struct ThreadPool : public std::vector { void read_uci_options(); Thread* available_slave(const Thread* master) const; void wait_for_think_finished(); - void start_thinking(const Position&, const Search::LimitsType&, - const std::vector&, Search::StateStackPtr&); + void start_thinking(const Position&, const Search::LimitsType&, Search::StateStackPtr&); bool sleepWhileIdle; Depth minimumSplitDepth; diff --git a/src/tt.cpp b/src/tt.cpp index 24e94c80..04673901 100644 --- a/src/tt.cpp +++ b/src/tt.cpp @@ -26,11 +26,11 @@ TranspositionTable TT; // Our global transposition table -/// TranspositionTable::set_size() sets the size of the transposition table, +/// TranspositionTable::resize() sets the size of the transposition table, /// measured in megabytes. Transposition table consists of a power of 2 number /// of clusters and each cluster consists of ClusterSize number of TTEntry. -void TranspositionTable::set_size(uint64_t mbSize) { +void TranspositionTable::resize(uint64_t mbSize) { assert(msb((mbSize << 20) / sizeof(TTEntry)) < 32); diff --git a/src/tt.h b/src/tt.h index a926d4ad..9e8c0481 100644 --- a/src/tt.h +++ b/src/tt.h @@ -81,7 +81,7 @@ public: const TTEntry* probe(const Key key) const; TTEntry* first_entry(const Key key) const; void refresh(const TTEntry* tte) const; - void set_size(uint64_t mbSize); + void resize(uint64_t mbSize); void clear(); void store(const Key key, Value v, Bound type, Depth d, Move m, Value statV); diff --git a/src/uci.cpp b/src/uci.cpp index 9f9d84cc..9b7ab033 100644 --- a/src/uci.cpp +++ b/src/uci.cpp @@ -194,14 +194,13 @@ namespace { void go(const Position& pos, istringstream& is) { Search::LimitsType limits; - vector searchMoves; string token; while (is >> token) { if (token == "searchmoves") while (is >> token) - searchMoves.push_back(move_from_uci(pos, token)); + limits.searchmoves.push_back(move_from_uci(pos, token)); else if (token == "wtime") is >> limits.time[WHITE]; else if (token == "btime") is >> limits.time[BLACK]; @@ -216,6 +215,6 @@ namespace { else if (token == "ponder") limits.ponder = true; } - Threads.start_thinking(pos, limits, searchMoves, SetupStates); + Threads.start_thinking(pos, limits, SetupStates); } } diff --git a/src/ucioption.cpp b/src/ucioption.cpp index 417765a5..22a335f5 100644 --- a/src/ucioption.cpp +++ b/src/ucioption.cpp @@ -38,7 +38,7 @@ namespace UCI { void on_logger(const Option& o) { start_logger(o); } void on_eval(const Option&) { Eval::init(); } void on_threads(const Option&) { Threads.read_uci_options(); } -void on_hash_size(const Option& o) { TT.set_size(o); } +void on_hash_size(const Option& o) { TT.resize(o); } void on_clear_hash(const Option&) { TT.clear(); } @@ -115,16 +115,16 @@ std::ostream& operator<<(std::ostream& os, const OptionsMap& om) { /// Option class constructors and conversion operators -Option::Option(const char* v, Fn* f) : type("string"), min(0), max(0), idx(Options.size()), on_change(f) +Option::Option(const char* v, OnChange f) : type("string"), min(0), max(0), idx(Options.size()), on_change(f) { defaultValue = currentValue = v; } -Option::Option(bool v, Fn* f) : type("check"), min(0), max(0), idx(Options.size()), on_change(f) +Option::Option(bool v, OnChange f) : type("check"), min(0), max(0), idx(Options.size()), on_change(f) { defaultValue = currentValue = (v ? "true" : "false"); } -Option::Option(Fn* f) : type("button"), min(0), max(0), idx(Options.size()), on_change(f) +Option::Option(OnChange f) : type("button"), min(0), max(0), idx(Options.size()), on_change(f) {} -Option::Option(int v, int minv, int maxv, Fn* f) : type("spin"), min(minv), max(maxv), idx(Options.size()), on_change(f) +Option::Option(int v, int minv, int maxv, OnChange f) : type("spin"), min(minv), max(maxv), idx(Options.size()), on_change(f) { std::ostringstream ss; ss << v; defaultValue = currentValue = ss.str(); } @@ -156,7 +156,7 @@ Option& Option::operator=(const string& v) { currentValue = v; if (on_change) - (*on_change)(*this); + on_change(*this); return *this; } diff --git a/src/ucioption.h b/src/ucioption.h index 5f04184d..2eb937a8 100644 --- a/src/ucioption.h +++ b/src/ucioption.h @@ -38,13 +38,13 @@ typedef std::map OptionsMap; /// Option class implements an option as defined by UCI protocol class Option { - typedef void (Fn)(const Option&); + typedef void (*OnChange)(const Option&); public: - Option(Fn* = NULL); - Option(bool v, Fn* = NULL); - Option(const char* v, Fn* = NULL); - Option(int v, int min, int max, Fn* = NULL); + Option(OnChange = NULL); + Option(bool v, OnChange = NULL); + Option(const char* v, OnChange = NULL); + Option(int v, int min, int max, OnChange = NULL); Option& operator=(const std::string& v); operator int() const; @@ -56,7 +56,7 @@ private: std::string defaultValue, currentValue, type; int min, max; size_t idx; - Fn* on_change; + OnChange on_change; }; void init(OptionsMap&); -- 2.39.2