diff --git a/src/movegen.cpp b/src/movegen.cpp index c6ba995ed..a95fb03f2 100644 --- a/src/movegen.cpp +++ b/src/movegen.cpp @@ -25,6 +25,20 @@ namespace Stockfish { movelist_buf mlb[8] {movelist_buf(MAX_MOVES,64),movelist_buf(MAX_MOVES,64),movelist_buf(MAX_MOVES,64),movelist_buf(MAX_MOVES,64),movelist_buf(MAX_MOVES,64),movelist_buf(MAX_MOVES,64),movelist_buf(MAX_MOVES,64),movelist_buf(MAX_MOVES,64)}; +template +MoveList::MoveList(const Position& pos) +{ + this->mlb = get_thread_mlb(pos); + this->moveList = this->mlb->acquire(); + this->last = generate(pos, this->moveList); +} + +template +MoveList::~MoveList() +{ + this->mlb->release(this->moveList); +} + namespace { template @@ -504,7 +518,12 @@ template ExtMove* generate(const Position&, ExtMove*); template ExtMove* generate(const Position&, ExtMove*); template ExtMove* generate(const Position&, ExtMove*); template ExtMove* generate(const Position&, ExtMove*); - +template class MoveList; +template class MoveList; +template class MoveList; +template class MoveList; +template class MoveList; +template class MoveList; /// generate generates all the legal moves in the given position diff --git a/src/movegen.h b/src/movegen.h index c9b49740d..37d781632 100644 --- a/src/movegen.h +++ b/src/movegen.h @@ -20,14 +20,13 @@ #define MOVEGEN_H_INCLUDED #include -#include #include "types.h" namespace Stockfish { class Position; -size_t get_thread_id( const Position& pos ); +class movelist_buf; enum GenType { CAPTURES, @@ -133,18 +132,8 @@ ExtMove* generate(const Position& pos, ExtMove* moveList); template struct MoveList { - explicit MoveList(const Position& pos) - { - this->thread_id = get_thread_id(pos); - this->moveList = mlb[thread_id].acquire(); - this->last = generate(pos, this->moveList); - } - - ~MoveList() - { - mlb[thread_id].release(this->moveList); - } - + explicit MoveList(const Position& pos); + ~MoveList(); const ExtMove* begin() const { return moveList; } const ExtMove* end() const { return last; } size_t size() const { return last - moveList; } @@ -153,11 +142,13 @@ struct MoveList { } private: - size_t thread_id; + movelist_buf* mlb; ExtMove* last; ExtMove* moveList = nullptr; }; +movelist_buf* get_thread_mlb( const Position& pos ); } // namespace Stockfish + #endif // #ifndef MOVEGEN_H_INCLUDED diff --git a/src/movepick.cpp b/src/movepick.cpp index e0146e644..5371671a8 100644 --- a/src/movepick.cpp +++ b/src/movepick.cpp @@ -69,8 +69,8 @@ MovePicker::MovePicker(const Position& p, Move ttm, Depth d, const ButterflyHist ttMove(ttm), refutations{{killers[0], 0}, {killers[1], 0}, {cm, 0}}, depth(d), ply(pl) { assert(d > 0); - this->thread_id = get_thread_id(pos); - this->moves = mlb[thread_id].acquire(); + this->mlb = get_thread_mlb(pos); + this->moves = this->mlb->acquire(); stage = (pos.checkers() ? EVASION_TT : MAIN_TT) + !(ttm && pos.pseudo_legal(ttm)); @@ -82,8 +82,8 @@ MovePicker::MovePicker(const Position& p, Move ttm, Depth d, const ButterflyHist : pos(p), mainHistory(mh), gateHistory(dh), captureHistory(cph), continuationHistory(ch), ttMove(ttm), recaptureSquare(rs), depth(d) { assert(d <= 0); - this->thread_id = get_thread_id(pos); - this->moves = mlb[thread_id].acquire(); + this->mlb = get_thread_mlb(pos); + this->moves = this->mlb->acquire(); stage = (pos.checkers() ? EVASION_TT : QSEARCH_TT) + !( ttm @@ -97,8 +97,8 @@ MovePicker::MovePicker(const Position& p, Move ttm, Value th, const GateHistory* : pos(p), gateHistory(dh), captureHistory(cph), ttMove(ttm), threshold(th) { assert(!pos.checkers()); - this->thread_id = get_thread_id(pos); - this->moves = mlb[thread_id].acquire(); + this->mlb = get_thread_mlb(pos); + this->moves = this->mlb->acquire(); stage = PROBCUT_TT + !(ttm && pos.capture(ttm) && pos.pseudo_legal(ttm) @@ -107,7 +107,7 @@ MovePicker::MovePicker(const Position& p, Move ttm, Value th, const GateHistory* MovePicker::~MovePicker() { - mlb[thread_id].release(this->moves); + this->mlb->release(this->moves); } diff --git a/src/movepick.h b/src/movepick.h index e51ca3236..2569b02a6 100644 --- a/src/movepick.h +++ b/src/movepick.h @@ -162,7 +162,7 @@ class MovePicker { Value threshold; Depth depth; int ply; - size_t thread_id; + movelist_buf* mlb; ExtMove* moves; }; diff --git a/src/position.cpp b/src/position.cpp index ec0764bbc..fab21d9af 100644 --- a/src/position.cpp +++ b/src/position.cpp @@ -148,7 +148,7 @@ Move cuckooMove[8192]; size_t get_thread_id( const Position& pos ) { - return pos.this_thread()->id(); + return pos.thread_id; } /// Position::init() initializes at startup the various arrays used to compute hash keys @@ -533,6 +533,16 @@ Position& Position::set(const Variant* v, const string& fenStr, bool isChess960, chess960 = isChess960 || v->chess960; tsumeMode = Options["TsumeMode"]; thisThread = th; + if ( th ) + { + this->thread_id = th->id(); + this->mlb = &mlb[this->thread_id]; + } + else + { + this->thread_id = 0; + this->mlb = nullptr; + } set_state(st); assert(pos_is_ok()); diff --git a/src/position.h b/src/position.h index d0d20509a..e8e4977c8 100644 --- a/src/position.h +++ b/src/position.h @@ -36,6 +36,11 @@ namespace Stockfish { +class movelist_buf; + +size_t get_thread_id( const Position& pos ); +movelist_buf* get_thread_mlb( const Position& pos ); + /// StateInfo struct stores information needed to restore a Position object to /// its previous state when we retract a move. Whenever a move is made on the /// board (by calling Position::do_move), a StateInfo object must be passed. @@ -339,6 +344,11 @@ class Position { void put_piece(Piece pc, Square s, bool isPromoted = false, Piece unpromotedPc = NO_PIECE); void remove_piece(Square s); + size_t thread_id; + movelist_buf* mlb; + + movelist_buf* get_mlb() const; + private: // Initialization helpers (used while setting up a position) void set_castling_right(Color c, Square rfrom); @@ -1416,8 +1426,6 @@ inline const std::string Position::piece_to_partner() const { return std::string(1, piece_to_char()[piece]); } -size_t get_thread_id( const Position& pos ); - inline Thread* Position::this_thread() const { return thisThread; } @@ -1569,6 +1577,21 @@ inline bool Position::can_drop(Color c, PieceType pt) const { return variant()->freeDrops || count_in_hand(c, pt) > 0; } +inline void set_thread_mlb(Position& pos) +{ + pos.mlb = &mlb[pos.thread_id]; +} + +inline movelist_buf* get_thread_mlb(const Position& pos) +{ + return &mlb[pos.thread_id]; +} + +inline movelist_buf* Position::get_mlb() const +{ + return &mlb[this->thread_id]; +} + } // namespace Stockfish #endif // #ifndef POSITION_H_INCLUDED diff --git a/src/thread.cpp b/src/thread.cpp index 32aaf58ea..8543b8592 100644 --- a/src/thread.cpp +++ b/src/thread.cpp @@ -30,6 +30,8 @@ namespace Stockfish { +size_t TH_STACK_SIZE = 8 * 1024 * 1024; // Default size. + ThreadPool Threads; // Global object diff --git a/src/thread.h b/src/thread.h index 464be79ed..9fd4f5b2c 100644 --- a/src/thread.h +++ b/src/thread.h @@ -34,6 +34,8 @@ namespace Stockfish { +extern size_t TH_STACK_SIZE; + /// Thread class keeps together all the thread-related stuff. We use /// per-thread pawn and material hash tables so that once we get a /// pointer to an entry its life time is unlimited and we don't have diff --git a/src/thread_win32_osx.h b/src/thread_win32_osx.h index e2adc76d5..fe44ab2c5 100644 --- a/src/thread_win32_osx.h +++ b/src/thread_win32_osx.h @@ -33,7 +33,7 @@ namespace Stockfish { -static const size_t TH_STACK_SIZE = 8 * 1024 * 1024; +extern size_t TH_STACK_SIZE; template > void* start_routine(void* ptr) diff --git a/src/ucioption.cpp b/src/ucioption.cpp index 0326e047f..8d8e9f30a 100644 --- a/src/ucioption.cpp +++ b/src/ucioption.cpp @@ -63,6 +63,22 @@ void on_hash_size(const Option& o) { TT.resize(size_t(o)); } void on_logger(const Option& o) { start_logger(o); } void on_threads(const Option& o) { Threads.set(size_t(o)); } void on_tb_path(const Option& o) { Tablebases::init(o); } +void on_max_moves(const Option& o) +{ + /* + if (o == "256") max_moves_index = 0; + else if (o == "512") max_moves_index = 1; + else if (o == "1024") max_moves_index = 2; + else if (o == "2048") max_moves_index = 3; + else if (o == "4096") max_moves_index = 4; + else if (o == "8192") max_moves_index = 5; + else max_moves_index = 3; + //*/ +} + +#if defined(__APPLE__) || defined(__MINGW32__) || defined(__MINGW64__) || defined(USE_PTHREADS) +void on_stack_size(const Option& o) { TH_STACK_SIZE=size_t(o)*1024*1024; } +#endif void on_use_NNUE(const Option& ) { Eval::NNUE::init(); } void on_eval_file(const Option& ) { Eval::NNUE::init(); } @@ -209,6 +225,10 @@ void init(OptionsMap& o) { o["TsumeMode"] << Option(false); o["VariantPath"] << Option("", on_variant_path); o["usemillisec"] << Option(true); // time unit for UCCI + o["MaxMoves"] << Option("1024", {"256", "512", "1024", "2048", "4096", "8192", "16384"}, on_max_moves); +#if defined(__APPLE__) || defined(__MINGW32__) || defined(__MINGW64__) || defined(USE_PTHREADS) + o["StackSize"] << Option(8, 1, 64, on_stack_size); +#endif }