From 462cb5b8efc8c279462ed9791923e46570ad1a98 Mon Sep 17 00:00:00 2001 From: rektdie Date: Wed, 6 Aug 2025 14:17:52 +0200 Subject: [PATCH] changed std::thread to pthread | bench 10099292 --- Makefile | 4 ++-- source/benchmark.h | 2 +- source/datagen.cpp | 46 +++++++++++++++++++++++++++++++++------------- source/uci.cpp | 43 +++++++++++++++++++++++++++++++++---------- 4 files changed, 69 insertions(+), 26 deletions(-) diff --git a/Makefile b/Makefile index df05320..38d8bf7 100644 --- a/Makefile +++ b/Makefile @@ -28,8 +28,8 @@ else endif endif -CXXFLAGS := -std=c++20 -O0 -flto -Wall -g -LDFLAGS := -O0 -flto +CXXFLAGS := -std=c++20 -O3 -flto -Wall +LDFLAGS := -O3 -flto ifeq ($(PLATFORM),mac) ifeq ($(ARCH),arm64) diff --git a/source/benchmark.h b/source/benchmark.h index 5feb20b..139a7a3 100644 --- a/source/benchmark.h +++ b/source/benchmark.h @@ -1,6 +1,6 @@ #pragma once #include -constexpr int BENCH_DEPTH = 5; +constexpr int BENCH_DEPTH = 13; void RunBenchmark(); diff --git a/source/datagen.cpp b/source/datagen.cpp index ab8f7cb..9e1a2ac 100644 --- a/source/datagen.cpp +++ b/source/datagen.cpp @@ -2,6 +2,8 @@ #include #include #include +#include +#include #include #include #include "datagen.h" @@ -54,7 +56,7 @@ static bool IsGameOver(Board &board, SEARCH::SearchContext* ctx) { bool isLegal = copy.MakeMove(board.moveList[i]); if (!isLegal) continue; - moveSeen++; + moveSeen++; } return moveSeen == 0; @@ -66,7 +68,7 @@ static void PlayRandMoves(Board &board, SEARCH::SearchContext* ctx) { std::uniform_int_distribution moveDist(0, 1); bool plusOne = moveDist(rng); - + for (int count = 0; count < RAND_MOVES + plusOne; count++) { MOVEGEN::GenerateMoves(board); @@ -141,7 +143,7 @@ static void PlayGames(int id, std::atomic& positions, std::atomic& st while (!stopFlag) { Board board; auto ctx = std::make_unique(); - + PlayRandMoves(board, ctx.get()); if (IsGameOver(board, ctx.get())) continue; @@ -210,11 +212,31 @@ void Run(int targetPositions, int threads) { std::atomic positions = 0; std::atomic stopFlag = false; - std::vector workerThreads; + std::vector workerThreads; - // Start N-1 threads - for (int i = 0; i < threads-1; i++) { - workerThreads.emplace_back(std::thread(PlayGames, i, std::ref(positions), std::ref(stopFlag))); + for (int i = 0; i < threads - 1; ++i) { + pthread_attr_t attr; + pthread_attr_init(&attr); + pthread_attr_setstacksize(&attr, 2 * 1024 * 1024); // 2 MB stack + + 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) + { + workerThreads.push_back(thread); + } else { + std::cerr << "Failed to create thread " << i << "\n"; + delete args; + } + + pthread_attr_destroy(&attr); } Stopwatch sw; @@ -228,14 +250,12 @@ void Run(int targetPositions, int threads) { stopFlag = true; - for (auto& thread : workerThreads) { - if (thread.joinable()) { - thread.join(); - } + for (pthread_t& thread : workerThreads) { + pthread_join(thread, nullptr); } - #ifdef _WIN32 + #ifdef _WIN32 ShowCursor(); #else ShowCursorLinux(); @@ -278,7 +298,7 @@ void PrintProgress(int positions, int targetPositions, Stopwatch &stopwatch, int std::cout << std::fixed << std::setprecision(0); std::ostringstream datagenTextStream; - datagenTextStream << std::fixed << std::setprecision(2) + datagenTextStream << std::fixed << std::setprecision(2) << "Positions processed: " << positions / 1000.0 << "K | Target: " << targetPositions / 1000.0 << "K"; std::string datagenText = datagenTextStream.str(); int datagenPadding = (width - datagenText.length()) / 2; diff --git a/source/uci.cpp b/source/uci.cpp index 2c74f17..47a0742 100644 --- a/source/uci.cpp +++ b/source/uci.cpp @@ -4,6 +4,8 @@ #include #include #include +#include +#include #include "types.h" #include "movegen.h" #include "search.h" @@ -69,6 +71,33 @@ static int ReadParam(std::string param, std::string &command) { return 0; } +template +static void* ThreadFunc(void* arg) { + auto* tup = static_cast*>(arg); + SEARCH::SearchPosition(*std::get<0>(*tup), std::get<1>(*tup), std::get<2>(*tup)); + delete tup; + return nullptr; +} + +static void StartSearchThread(Board& board, SearchParams params, SEARCH::SearchContext* ctx) { + pthread_attr_t attr; + pthread_attr_init(&attr); + pthread_attr_setstacksize(&attr, 2 * 1024 * 1024); // 2 MB stack + + pthread_t thread; + + if (params.nodes) { + auto* arg = new std::tuple(&board, params, ctx); + pthread_create(&thread, &attr, ThreadFunc, arg); + } else { + auto* arg = new std::tuple(&board, params, ctx); + pthread_create(&thread, &attr, ThreadFunc, arg); + } + + pthread_attr_destroy(&attr); + pthread_detach(thread); +} + static void ParseGo(Board &board, std::string &command, SEARCH::SearchContext* ctx) { SearchParams params; @@ -87,13 +116,7 @@ static void ParseGo(Board &board, std::string &command, SEARCH::SearchContext* c params.btime = 99999999; } - if (params.nodes) { - auto worker = std::thread(SEARCH::SearchPosition, std::ref(board), params, ctx); - worker.detach(); - } else { - auto worker = std::thread(SEARCH::SearchPosition, std::ref(board), params, ctx); - worker.detach(); - } + StartSearchThread(board, params, ctx); } static void SetOption(std::string &command, SEARCH::SearchContext* ctx) { @@ -136,10 +159,10 @@ void UCILoop(Board &board) { // parse UCI "ucinewgame" command if (input.find("ucinewgame") != std::string::npos) { board.SetByFen(StartingFen); - + // Clearing ctx->TT.Clear(); - + ctx->killerMoves = {}; ctx->history.Clear(); ctx->conthist.Clear(); @@ -151,7 +174,7 @@ void UCILoop(Board &board) { // parse UCI "go" command if (input.find("go") != std::string::npos) { - ParseGo(board, input, ctx.get()); + ParseGo(board, input, ctx.get()); continue; }