diff --git a/Makefile b/Makefile index 38d8bf7..d64c121 100644 --- a/Makefile +++ b/Makefile @@ -7,16 +7,16 @@ CXX := clang++ EXE ?= Eleanor EVALFILE := ./nnue.bin -UNAME_S := $(shell uname -s) -ARCH := $(shell uname -m) - ifeq ($(OS),Windows_NT) + UNAME_S := Windows EXE_EXT := .exe MKDIR := mkdir RM := rmdir /s /q DEL := del /q PLATFORM := windows else + UNAME_S := $(shell uname -s) + ARCH := $(shell uname -m) EXE_EXT := MKDIR := mkdir -p RM := rm -rf diff --git a/source/datagen.cpp b/source/datagen.cpp index c56b893..f3e0f16 100644 --- a/source/datagen.cpp +++ b/source/datagen.cpp @@ -2,7 +2,6 @@ #include #include #include -#include #include #include #include @@ -12,8 +11,11 @@ #include "movegen.h" #include "utils.h" +// OS-dependent threading includes #ifdef _WIN32 -#include + #include +#else + #include #endif #ifdef _WIN32 @@ -32,15 +34,15 @@ static void ShowCursor() { cursorInfo.bVisible = TRUE; SetConsoleCursorInfo(hConsole, &cursorInfo); } -#endif - -static void HideCursorLinux() { +#else +static void HideCursor() { std::cout << "\033[?25l"; } -static void ShowCursorLinux() { +static void ShowCursor() { std::cout << "\033[?25h"; } +#endif namespace DATAGEN { @@ -200,18 +202,42 @@ static void PlayGames(int id, std::atomic& positions, std::atomic& st } } +#ifdef _WIN32 +// Windows implementation using std::thread +static void CreateWorkerThread(int id, std::atomic& positions, std::atomic& stopFlag, std::vector& threads) { + threads.emplace_back(PlayGames, id, std::ref(positions), std::ref(stopFlag)); +} +#else +// Unix/Linux implementation using pthread +static void* ThreadFunc(void* arg) { + auto* tup = static_cast*, std::atomic*>*>(arg); + PlayGames(std::get<0>(*tup), *std::get<1>(*tup), *std::get<2>(*tup)); + delete tup; + return nullptr; +} +#endif + void Run(int targetPositions, int threads) { #ifdef _WIN32 system("cls"); HideCursor(); #else std::cout << "\033[2J\033[H"; - HideCursorLinux(); + HideCursor(); #endif std::atomic positions = 0; std::atomic stopFlag = false; +#ifdef _WIN32 + // Windows implementation using std::thread + std::vector workerThreads; + + for (int i = 0; i < threads - 1; ++i) { + CreateWorkerThread(i, positions, stopFlag, workerThreads); + } +#else + // Unix/Linux implementation using pthread std::vector workerThreads; for (int i = 0; i < threads - 1; ++i) { @@ -222,14 +248,7 @@ void Run(int targetPositions, int threads) { auto* args = new std::tuple*, std::atomic*>(i, &positions, &stopFlag); pthread_t thread; - if (pthread_create(&thread, &attr, - [](void* a) -> void* { - auto* tup = static_cast*, std::atomic*>*>(a); - PlayGames(std::get<0>(*tup), *std::get<1>(*tup), *std::get<2>(*tup)); - delete tup; - return nullptr; - }, args) == 0) - { + if (pthread_create(&thread, &attr, ThreadFunc, args) == 0) { workerThreads.push_back(thread); } else { std::cerr << "Failed to create thread " << i << "\n"; @@ -238,6 +257,7 @@ void Run(int targetPositions, int threads) { pthread_attr_destroy(&attr); } +#endif Stopwatch sw; while (positions < targetPositions) { @@ -245,20 +265,28 @@ void Run(int targetPositions, int threads) { std::this_thread::sleep_for(std::chrono::seconds(1)); } - PrintProgress(positions, targetPositions, sw, threads); stopFlag = true; +#ifdef _WIN32 + // Join Windows threads + for (std::thread& thread : workerThreads) { + if (thread.joinable()) { + thread.join(); + } + } +#else + // Join pthread threads for (pthread_t& thread : workerThreads) { pthread_join(thread, nullptr); } - +#endif #ifdef _WIN32 ShowCursor(); #else - ShowCursorLinux(); + ShowCursor(); #endif } @@ -328,7 +356,7 @@ void PrintProgress(int positions, int targetPositions, Stopwatch &stopwatch, int remainingTime = (targetPositions - positions) / positionsPerSec; } - remainingTime = std::max(0.0, remainingTime); + remainingTime = (std::max)(0.0, remainingTime); int remainingHours = static_cast(remainingTime) / 3600; int remainingMinutes = (static_cast(remainingTime) % 3600) / 60; @@ -371,5 +399,4 @@ void PrintProgress(int positions, int targetPositions, Stopwatch &stopwatch, int std::cout << std::endl; } - -} +} \ No newline at end of file diff --git a/source/uci.cpp b/source/uci.cpp index 5781f46..de40107 100644 --- a/source/uci.cpp +++ b/source/uci.cpp @@ -2,7 +2,6 @@ #include #include #include -#include #include #include #include "types.h" @@ -13,6 +12,13 @@ #include "utils.h" #include "datagen.h" +// OS-dependent threading includes +#ifdef _WIN32 + #include +#else + #include +#endif + static void ParsePosition(Board &board, std::string_view command, SEARCH::SearchContext* ctx) { if (command.find("startpos") != std::string::npos) { board.SetByFen(StartingFen); @@ -70,6 +76,27 @@ static int ReadParam(std::string param, std::string &command) { return 0; } +#ifdef _WIN32 +// Windows implementation using std::thread +template +static void ThreadFunc(Board* board, SearchParams params, SEARCH::SearchContext* ctx) { + SEARCH::SearchPosition(*board, params, ctx); +} + +static void StartSearchThread(Board& board, SearchParams params, SEARCH::SearchContext* ctx) { + std::thread searchThread; + + if (params.nodes) { + searchThread = std::thread(ThreadFunc, &board, params, ctx); + } else { + searchThread = std::thread(ThreadFunc, &board, params, ctx); + } + + searchThread.detach(); +} + +#else +// Unix/Linux implementation using pthread template static void* ThreadFunc(void* arg) { auto* tup = static_cast*>(arg); @@ -81,7 +108,7 @@ static void* ThreadFunc(void* arg) { static void StartSearchThread(Board& board, SearchParams params, SEARCH::SearchContext* ctx) { pthread_attr_t attr; pthread_attr_init(&attr); - pthread_attr_setstacksize(&attr, 8 * 1024 * 1024); // 2 MB stack + pthread_attr_setstacksize(&attr, 8 * 1024 * 1024); // 8 MB stack pthread_t thread; @@ -96,6 +123,7 @@ static void StartSearchThread(Board& board, SearchParams params, SEARCH::SearchC pthread_attr_destroy(&attr); pthread_detach(thread); } +#endif static void ParseGo(Board &board, std::string &command, SEARCH::SearchContext* ctx) { SearchParams params; @@ -233,4 +261,4 @@ void UCILoop(Board &board) { continue; } } -} +} \ No newline at end of file