fixed pthread bug on windows | bench 10419455

This commit is contained in:
rektdie committed 2025-08-23 23:57:36 +02:00
1 parent c07858dab4
commit 3135f430ad
3 files changed
+82 -27

No files matched your search

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