Skip to content

Commit

Permalink
Added uci opts MaxMoves & StackSize
Browse files Browse the repository at this point in the history
  • Loading branch information
dpldgr committed Nov 25, 2024
1 parent f030d0a commit c497469
Show file tree
Hide file tree
Showing 10 changed files with 95 additions and 28 deletions.
21 changes: 20 additions & 1 deletion src/movegen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<GenType T>
MoveList<T>::MoveList(const Position& pos)
{
this->mlb = get_thread_mlb(pos);
this->moveList = this->mlb->acquire();
this->last = generate<T>(pos, this->moveList);
}

template<GenType T>
MoveList<T>::~MoveList()
{
this->mlb->release(this->moveList);
}

namespace {

template<MoveType T>
Expand Down Expand Up @@ -504,7 +518,12 @@ template ExtMove* generate<QUIETS>(const Position&, ExtMove*);
template ExtMove* generate<EVASIONS>(const Position&, ExtMove*);
template ExtMove* generate<QUIET_CHECKS>(const Position&, ExtMove*);
template ExtMove* generate<NON_EVASIONS>(const Position&, ExtMove*);

template class MoveList<CAPTURES>;
template class MoveList<QUIETS>;
template class MoveList<EVASIONS>;
template class MoveList<QUIET_CHECKS>;
template class MoveList<NON_EVASIONS>;
template class MoveList<LEGAL>;

/// generate<LEGAL> generates all the legal moves in the given position

Expand Down
21 changes: 6 additions & 15 deletions src/movegen.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,13 @@
#define MOVEGEN_H_INCLUDED

#include <algorithm>
#include <iostream>

#include "types.h"

namespace Stockfish {

class Position;
size_t get_thread_id( const Position& pos );
class movelist_buf;

enum GenType {
CAPTURES,
Expand Down Expand Up @@ -133,18 +132,8 @@ ExtMove* generate(const Position& pos, ExtMove* moveList);
template<GenType T>
struct MoveList {

explicit MoveList(const Position& pos)
{
this->thread_id = get_thread_id(pos);
this->moveList = mlb[thread_id].acquire();
this->last = generate<T>(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; }
Expand All @@ -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
14 changes: 7 additions & 7 deletions src/movepick.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand All @@ -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
Expand All @@ -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)
Expand All @@ -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);
}


Expand Down
2 changes: 1 addition & 1 deletion src/movepick.h
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ class MovePicker {
Value threshold;
Depth depth;
int ply;
size_t thread_id;
movelist_buf* mlb;
ExtMove* moves;
};

Expand Down
12 changes: 11 additions & 1 deletion src/position.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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());
Expand Down
27 changes: 25 additions & 2 deletions src/position.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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
2 changes: 2 additions & 0 deletions src/thread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@

namespace Stockfish {

size_t TH_STACK_SIZE = 8 * 1024 * 1024; // Default size.

ThreadPool Threads; // Global object


Expand Down
2 changes: 2 additions & 0 deletions src/thread.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/thread_win32_osx.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@

namespace Stockfish {

static const size_t TH_STACK_SIZE = 8 * 1024 * 1024;
extern size_t TH_STACK_SIZE;

template <class T, class P = std::pair<T*, void(T::*)()>>
void* start_routine(void* ptr)
Expand Down
20 changes: 20 additions & 0 deletions src/ucioption.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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(); }
Expand Down Expand Up @@ -209,6 +225,10 @@ void init(OptionsMap& o) {
o["TsumeMode"] << Option(false);
o["VariantPath"] << Option("<empty>", 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
}


Expand Down

0 comments on commit c497469

Please sign in to comment.