From 64b2e3d9b143e901386231e48a0ec4eed92479ec Mon Sep 17 00:00:00 2001 From: rektdie Date: Tue, 24 Feb 2026 23:34:26 +0100 Subject: [PATCH] maybe fixed crashes | bench 1801941 --- source/search.cpp | 24 ++++++++++++------------ source/tt.cpp | 4 ++-- source/tt.h | 11 ++++++----- source/uci.cpp | 46 ++++++++++++++++++++++++++++++++++++++-------- source/uci.h | 3 ++- 5 files changed, 60 insertions(+), 28 deletions(-) diff --git a/source/search.cpp b/source/search.cpp index a6c5602..824c031 100644 --- a/source/search.cpp +++ b/source/search.cpp @@ -207,18 +207,18 @@ static bool ShouldStop(SearchContext* ctx) { if constexpr (mode == normal) { if (ctx->nodes % 1024 == 0) { if (ctx->sw.GetElapsedMS() >= ctx->timeToSearch) { - searchStopped = true; + searchStopped.store(true, std::memory_order_relaxed); return true; } } } else if constexpr (mode == nodesMode) { if (ctx->nodes > ctx->nodesToGo) { - searchStopped = true; + searchStopped.store(true, std::memory_order_relaxed); return true; } } else if constexpr (mode == datagen) { if (ctx->nodes > DATAGEN::HARD_NODES) { - searchStopped = true; + searchStopped.store(true, std::memory_order_relaxed); return true; } } @@ -314,7 +314,7 @@ static SearchResults Quiescence(Board& board, int alpha, int beta, int ply, Sear } results.score = bestScore; - if (searchStopped) return 0; + if (searchStopped.load(std::memory_order_relaxed)) return 0; if (!ctx->excluded) { ctx->TT->WriteEntry(board.hashKey, 0, results.score, nodeType, results.bestMove, ttpv); } @@ -410,7 +410,7 @@ SearchResults PVS(Board& board, int depth, int alpha, int beta, int ply, SearchC int score = -PVS(copy, depth - reduction, -beta, -beta + 1, ply + 1, ctx, !cutnode).score; - if (searchStopped) return 0; + if (searchStopped.load(std::memory_order_relaxed)) return 0; if (score >= beta) { if (depth <= 14 || ctx->minNmpPly > 0) { return score > MATE_SCORE - MAX_DEPTH ? beta : score; @@ -469,7 +469,7 @@ SearchResults PVS(Board& board, int depth, int alpha, int beta, int ply, SearchC ply + 1, ctx, !cutnode).score; } - if (searchStopped) return 0; + if (searchStopped.load(std::memory_order_relaxed)) return 0; if (score >= probcutBeta) { ctx->TT->WriteEntry(board.hashKey, probcutDepth, score, CutNode, currMove, ttpv); @@ -640,7 +640,7 @@ SearchResults PVS(Board& board, int depth, int alpha, int beta, int ply, SearchC ctx->nodesTable[currMove % 4096] += ctx->nodes - nodesBeforeSearch; } - if (searchStopped) return 0; + if (searchStopped.load(std::memory_order_relaxed)) return 0; if (currMove != 0 && currMove.IsQuiet()) { seenQuiets[seenQuietsCount] = currMove; @@ -754,7 +754,7 @@ SearchResults PVS(Board& board, int depth, int alpha, int beta, int ply, SearchC } } - if (searchStopped) return 0; + if (searchStopped.load(std::memory_order_relaxed)) return 0; if (!ctx->excluded) { if (!board.InCheck() && ((results.bestMove.IsQuiet() || !results.bestMove)) @@ -864,7 +864,7 @@ static SearchResults ID(Board &board, SearchParams params, SearchContext* ctx) { aw.Set(currentResults.score); - if (searchStopped) { + if (searchStopped.load(std::memory_order_relaxed)) { break; } else { if (currentResults.bestMove) { @@ -877,13 +877,13 @@ static SearchResults ID(Board &board, SearchParams params, SearchContext* ctx) { if constexpr (mode == normal) { if (ctx->sw.GetElapsedMS() >= softTime) { - searchStopped = true; + searchStopped.store(true, std::memory_order_relaxed); break; } } } else if constexpr (mode == datagen) { if (ctx->nodes >= DATAGEN::SOFT_NODES) { - searchStopped = true; + searchStopped.store(true, std::memory_order_relaxed); break; } } @@ -895,7 +895,7 @@ static SearchResults ID(Board &board, SearchParams params, SearchContext* ctx) { template SearchResults SearchPosition(Board &board, SearchParams params, SearchContext* ctx) { - searchStopped = false; + searchStopped.store(false, std::memory_order_relaxed); ctx->seldepth = 0; ctx->nodesTable = {}; if constexpr (mode != bench) { diff --git a/source/tt.cpp b/source/tt.cpp index 2eaa1a2..8ed48ff 100644 --- a/source/tt.cpp +++ b/source/tt.cpp @@ -2,7 +2,7 @@ TTable SharedTT; -void TTable::WriteEntry(U64 &hashKey, int depth, int score, int nodeType, Move bestMove, bool ttpv) { +void TTable::WriteEntry(const U64 &hashKey, int depth, int score, int nodeType, Move bestMove, bool ttpv) { TTBucket *bucket = &table[hashKey % table.size()]; TTEntry *current = nullptr; @@ -24,4 +24,4 @@ void TTable::WriteEntry(U64 &hashKey, int depth, int score, int nodeType, Move b current->depth = depth; current->bestMove = bestMove; current->ttpv = ttpv; -} \ No newline at end of file +} diff --git a/source/tt.h b/source/tt.h index 48861ab..6df5c4c 100644 --- a/source/tt.h +++ b/source/tt.h @@ -68,11 +68,11 @@ public: table.resize(size); } - void PrefetchEntry(U64 &hashKey) { + void PrefetchEntry(const U64 &hashKey) { __builtin_prefetch(&table[hashKey % table.size()]); } - TTEntry GetEntry(U64 &hashKey) { + TTEntry GetEntry(const U64 &hashKey) { TTBucket *current = &table[hashKey % table.size()]; if (current->depthPreferred.hashKey == hashKey) { return current->depthPreferred; @@ -86,15 +86,16 @@ public: // returns used space per mill int GetUsedPercentage() { int count = 0; + const U64 sampleSize = std::min(1000, table.size()); - for (int i = 0; i < 1000; i++) { + for (U64 i = 0; i < sampleSize; i++) { if (table[i].depthPreferred || table[i].alwaysReplace) count++; } return count; } - void WriteEntry(U64 &hashKey, int depth, int score, int nodeType, Move bestMove, bool ttpv); + void WriteEntry(const U64 &hashKey, int depth, int score, int nodeType, Move bestMove, bool ttpv); }; -extern TTable SharedTT; \ No newline at end of file +extern TTable SharedTT; diff --git a/source/uci.cpp b/source/uci.cpp index 5b222b1..0e57fb6 100644 --- a/source/uci.cpp +++ b/source/uci.cpp @@ -4,6 +4,7 @@ #include #include #include +#include #include "types.h" #include "movegen.h" #include "search.h" @@ -88,27 +89,38 @@ static double ReadParam(const std::string& param, const std::string &command) { #ifdef _WIN32 // Windows implementation using std::thread +static std::vector activeThreads; + template -static void ThreadFunc(Board* board, SearchParams params, SEARCH::SearchContext* ctx) { - SEARCH::SearchPosition(*board, params, ctx); +static void ThreadFunc(Board board, SearchParams params, SEARCH::SearchContext* ctx) { + SEARCH::SearchPosition(board, params, ctx); delete ctx; } static void StartSearchThread(Board& board, SearchParams params, SEARCH::SearchContext* ctx, int id) { - std::thread searchThread; SEARCH::SearchContext* ctxCopy = new SEARCH::SearchContext(*ctx); if (id == 0) ctxCopy->doPrint = true; if (params.nodes) { - searchThread = std::thread(ThreadFunc, &board, params, ctxCopy); + activeThreads.emplace_back(ThreadFunc, board, params, ctxCopy); } else { - searchThread = std::thread(ThreadFunc, &board, params, ctxCopy); + activeThreads.emplace_back(ThreadFunc, board, params, ctxCopy); } - searchThread.detach(); +} + +static void WaitForSearchThreads() { + for (auto &thread : activeThreads) { + if (thread.joinable()) { + thread.join(); + } + } + activeThreads.clear(); } #else // Unix/Linux implementation using pthread +static std::vector activeThreads; + template static void* ThreadFunc(void* arg) { auto* tup = static_cast*>(arg); @@ -138,10 +150,22 @@ static void StartSearchThread(Board& board, SearchParams params, SEARCH::SearchC } pthread_attr_destroy(&attr); - pthread_detach(thread); + activeThreads.push_back(thread); +} + +static void WaitForSearchThreads() { + for (pthread_t thread : activeThreads) { + pthread_join(thread, nullptr); + } + activeThreads.clear(); } #endif +static void StopAndWaitForSearchThreads() { + searchStopped.store(true, std::memory_order_relaxed); + WaitForSearchThreads(); +} + static void ParseGo(Board &board, std::string &command, SEARCH::SearchContext* ctx) { SearchParams params; @@ -160,6 +184,8 @@ static void ParseGo(Board &board, std::string &command, SEARCH::SearchContext* c params.btime = 99999999; } + StopAndWaitForSearchThreads(); + for (int i = 0; i < threads; i++) { StartSearchThread(board, params, ctx, i); } @@ -167,12 +193,14 @@ static void ParseGo(Board &board, std::string &command, SEARCH::SearchContext* c static void SetOption(std::string& command, SEARCH::SearchContext* ctx) { if (command.find("Hash") != std::string::npos) { + StopAndWaitForSearchThreads(); U64 hashSize = (ReadParam("value", command) * 1000000ULL) / sizeof(TTEntry); ctx->TT->Resize(hashSize); return; } if (command.find("Threads") != std::string::npos) { + StopAndWaitForSearchThreads(); threads = ReadParam("value", command); return; } @@ -256,6 +284,7 @@ void UCILoop(Board &board) { // parse UCI "ucinewgame" command if (input.find("ucinewgame") != std::string::npos) { + StopAndWaitForSearchThreads(); board.SetByFen(StartingFen); // Clearing @@ -279,6 +308,7 @@ void UCILoop(Board &board) { // parse UCI "quit" command if (input.find("quit") != std::string::npos) { + StopAndWaitForSearchThreads(); // stop the loop break; } @@ -286,7 +316,7 @@ void UCILoop(Board &board) { // parse UCI "stop" command if (input.find("stop") != std::string::npos) { // stop the loop - searchStopped = true; + StopAndWaitForSearchThreads(); continue; } diff --git a/source/uci.h b/source/uci.h index 21fb131..2e6ca21 100644 --- a/source/uci.h +++ b/source/uci.h @@ -1,10 +1,11 @@ #pragma once #include "move.h" #include "board.h" +#include inline bool UCIEnabled = false; inline int threads = 1; -inline bool searchStopped = false; +inline std::atomic_bool searchStopped = false; class SearchParams { public: