Skip to content

Commit

Permalink
Multithreaded movelist_buf.
Browse files Browse the repository at this point in the history
  • Loading branch information
dpldgr committed Nov 25, 2024
1 parent 533f965 commit 8513896
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 38 deletions.
22 changes: 10 additions & 12 deletions src/movegen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,31 +23,29 @@

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)};
//movelist_buf* mlb = ::operator new(sizeof(movelist_buf)*MAX_THREADS);
movelist_buf mlb[MAX_THREADS];

/*
void movelist_buf::Init()
int movelist_buf::thread_count = 1;
int movelist_buf::max_moves = MAX_MOVES;

void movelist_buf::mlb_init( const size_t thread_cnt )
{
int thread_count = Options["Threads"];
int max_moves = Options["MaxMoves"];
assert( thread_cnt<= MAX_THREADS );
thread_count = thread_cnt;

for ( int i = 0 ; i < thread_count ; i++ )
{
new (&mlb[i]) movelist_buf(max_moves,64);
mlb[i].init();
}
}

void movelist_buf::Shutdown()
void movelist_buf::mlb_shutdown()
{
int thread_count = Options["Threads"];
for ( int i = 0 ; i < thread_count ; i++ )
{
delete mlb[i];
mlb[i].shutdown();
}
}
//*/

template<GenType T>
MoveList<T>::MoveList(const Position& pos)
Expand Down
42 changes: 26 additions & 16 deletions src/movegen.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,39 +59,47 @@ ExtMove* generate(const Position& pos, ExtMove* moveList);
class movelist_buf
{
public:
movelist_buf(int move_count_, int list_count_)
{
this->move_count = move_count_;
this->list_count = list_count_;
movelist_buf() : movelist_buf(64) {}
movelist_buf(const int list_cnt) : list_count(list_cnt) {}

static void mlb_init( const size_t thread_cnt );
static void mlb_shutdown();

void init()
{
constexpr std::size_t move_size = sizeof(ExtMove);
const std::size_t list_size = move_size * move_count;
const std::size_t list_size = move_size * movelist_buf::max_moves;
const std::size_t malloc_size = list_size * list_count;

ptr_stack = (ExtMove**)malloc(sizeof(ExtMove*) * list_count );
ptr_stack = (ExtMove**)malloc(sizeof(ExtMove*) * list_count);
data = (ExtMove*)malloc(malloc_size);

for (int i = 0; i < (list_count - 1); i++)
{
ptr_stack[i] = (ExtMove*)(data + i * move_count);
ptr_stack[i] = (ExtMove*)(data + i * movelist_buf::max_moves);
}

ptr_stack[list_count - 1] = nullptr; // The last element is used as a guard value.
}

void shutdown()
{
if (ptr_stack) { free(ptr_stack); ptr_stack = nullptr; }
if (data) { free(data); data = nullptr; }
}

~movelist_buf()
{
if (ptr_stack) free(ptr_stack);
if (data) free(data);
}

void resize(const int move_count_, int list_count_)
void resize(const int list_cnt)
{
this->move_count = move_count_;
this->list_count = list_count_;
this->list_count = list_cnt;

constexpr std::size_t move_size = sizeof(ExtMove);
const std::size_t list_size = move_size * move_count;
const std::size_t list_size = move_size * movelist_buf::max_moves;
const std::size_t malloc_size = list_size * list_count;

if (ptr_stack) free(ptr_stack);
Expand All @@ -102,7 +110,7 @@ ExtMove* generate(const Position& pos, ExtMove* moveList);

for (int i = 0; i < (list_count - 1); i++)
{
ptr_stack[i] = (ExtMove*)(data + i * move_count);
ptr_stack[i] = (ExtMove*)(data + i * movelist_buf::max_moves);
}

ptr_stack[list_count - 1] = nullptr; // The last element is used as a guard value.
Expand All @@ -118,15 +126,17 @@ ExtMove* generate(const Position& pos, ExtMove* moveList);
ptr_stack[--top] = ptr;
}

static int thread_count;
static int max_moves;

ExtMove** ptr_stack = nullptr;
ExtMove* data = nullptr;
int top = 0;
int list_count = 0;
int move_count = 0;
int move_count;
int list_count;
};

extern movelist_buf mlb[8];
//extern movelist_buf* mlb;
extern movelist_buf mlb[MAX_THREADS];

/// The MoveList struct is a simple wrapper around generate(). It sometimes comes
/// in handy to use this class instead of the low level generate() function.
Expand Down
4 changes: 4 additions & 0 deletions src/thread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,8 @@ void ThreadPool::set(size_t requested) {

while (size() > 0)
delete back(), pop_back();

movelist_buf::mlb_shutdown();
}

if (requested > 0) // create new thread(s)
Expand All @@ -160,6 +162,8 @@ void ThreadPool::set(size_t requested) {

// Init thread number dependent search params.
Search::init();

movelist_buf::mlb_init(requested);
}
}

Expand Down
20 changes: 10 additions & 10 deletions src/ucioption.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,19 +65,19 @@ 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 (o == "256") movelist_buf::max_moves = 256;
else if (o == "512") movelist_buf::max_moves = 512;
else if (o == "1024") movelist_buf::max_moves = 1024;
else if (o == "2048") movelist_buf::max_moves = 2048;
else if (o == "4096") movelist_buf::max_moves = 4096;
else if (o == "8192") movelist_buf::max_moves = 8192;
else if (o == "16384") movelist_buf::max_moves = 16384;
else movelist_buf::max_moves = 1024;
Threads.set(size_t(Options["Threads"]));
}

#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; }
void on_stack_size(const Option& o) { TH_STACK_SIZE=size_t(o)*1024*1024; Threads.set(size_t(Options["Threads"])); }
#endif

void on_use_NNUE(const Option& ) { Eval::NNUE::init(); }
Expand Down

0 comments on commit 8513896

Please sign in to comment.