fixed pthread bug on windows | bench 10419455
This commit is contained in:
1 parent
c07858dab4
commit
3135f430ad
3 files changed
+79
-24
No files matched your search
@@ -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
|
||||
|
||||
+46
-19
@@ -2,7 +2,6 @@
|
||||
#include <fstream>
|
||||
#include <iomanip>
|
||||
#include <thread>
|
||||
#include <pthread.h>
|
||||
#include <tuple>
|
||||
#include <atomic>
|
||||
#include <mutex>
|
||||
@@ -12,8 +11,11 @@
|
||||
#include "movegen.h"
|
||||
#include "utils.h"
|
||||
|
||||
// OS-dependent threading includes
|
||||
#ifdef _WIN32
|
||||
#include <windows.h>
|
||||
#else
|
||||
#include <pthread.h>
|
||||
#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<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) {
|
||||
#ifdef _WIN32
|
||||
system("cls");
|
||||
HideCursor();
|
||||
#else
|
||||
std::cout << "\033[2J\033[H";
|
||||
HideCursorLinux();
|
||||
HideCursor();
|
||||
#endif
|
||||
|
||||
std::atomic<int> positions = 0;
|
||||
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;
|
||||
|
||||
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);
|
||||
|
||||
pthread_t thread;
|
||||
if (pthread_create(&thread, &attr,
|
||||
[](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)
|
||||
{
|
||||
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<int>(remainingTime) / 3600;
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
+30
-2
@@ -2,7 +2,6 @@
|
||||
#include <iostream>
|
||||
#include <cstring>
|
||||
#include <string>
|
||||
#include <pthread.h>
|
||||
#include <tuple>
|
||||
#include <memory>
|
||||
#include "types.h"
|
||||
@@ -13,6 +12,13 @@
|
||||
#include "utils.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) {
|
||||
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 <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>
|
||||
static void* ThreadFunc(void* 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) {
|
||||
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;
|
||||
|
||||
Reference in new issue
Block a user