diff --git a/source/search.cpp b/source/search.cpp index 03b627e..59b4173 100644 --- a/source/search.cpp +++ b/source/search.cpp @@ -13,6 +13,74 @@ namespace SEARCH { +static void PublishRootVote(SearchContext* ctx, const SearchResults& results, int depth) { + if (!ctx->voteTable || !ctx->voteMutex) + return; + Move m = results.bestMove; + if (uint16_t(m) == 0) + return; + if (ctx->threadId < 0 || ctx->threadId >= static_cast(ctx->voteTable->size())) + return; + std::lock_guard lock(*ctx->voteMutex); + RootVote& slot = (*ctx->voteTable)[ctx->threadId]; + slot.bestMove = results.bestMove; + slot.score = results.score; + slot.depth = depth; +} + +SearchResults VoteRootMoves(const std::vector& votes) { + struct Tally { + Move move; + int count = 0; + int score = 0; + int depth = 0; + }; + std::vector tallies; + tallies.reserve(votes.size()); + + for (const RootVote& v : votes) { + Move mv = v.bestMove; + if (uint16_t(mv) == 0) + continue; + const uint16_t key = mv; + bool found = false; + for (Tally& t : tallies) { + if (uint16_t(t.move) == key) { + t.count++; + if (v.depth > t.depth || (v.depth == t.depth && v.score > t.score)) { + t.score = v.score; + t.depth = v.depth; + t.move = v.bestMove; + } + found = true; + break; + } + } + if (!found) { + Tally t; + t.move = v.bestMove; + t.count = 1; + t.score = v.score; + t.depth = v.depth; + tallies.push_back(t); + } + } + + if (tallies.empty()) + return SearchResults(0, Move()); + + Tally* best = &tallies[0]; + for (size_t i = 1; i < tallies.size(); i++) { + if (tallies[i].count > best->count + || (tallies[i].count == best->count && tallies[i].depth > best->depth) + || (tallies[i].count == best->count && tallies[i].depth == best->depth && tallies[i].score > best->score)) { + best = &tallies[i]; + } + } + + return SearchResults(best->score, best->move); +} + static WDLTriplet GetRoundedWDL(int score, Board& board) { WDLTriplet wdl = getWDL(score, board); @@ -932,6 +1000,8 @@ static SearchResults ID(Board &board, SearchParams params, SearchContext* ctx) { safeResults = currentResults; } + PublishRootVote(ctx, safeResults, depth); + if constexpr (mode == normal || mode == nodesMode) { if (ctx->doPrint) PrintSearchInfo(board, ctx, safeResults, depth, elapsed); @@ -985,6 +1055,10 @@ SearchResults SearchPosition(Board &board, SearchParams params, SearchContext* c if constexpr (mode != normal && mode != nodesMode) return results; + if (ctx->voteTable != nullptr) { + return results; + } + if (ctx->doPrint) { if (UCIEnabled) { std::cout << "bestmove "; diff --git a/source/search.h b/source/search.h index e9f6e40..1639cb2 100644 --- a/source/search.h +++ b/source/search.h @@ -2,6 +2,7 @@ #include "board.h" #include "uci.h" #include +#include #include "move.h" #include #include "stopwatch.h" @@ -55,6 +56,14 @@ void RefreshTunableCaches(); class SearchContext; +struct RootVote { + Move bestMove; + int score = 0; + int depth = 0; +}; + +SearchResults VoteRootMoves(const std::vector& votes); + class CaptHistory { private: MultiArray historyMoves; @@ -216,6 +225,10 @@ class SearchContext { public: bool doPrint = false; + int threadId = 0; + std::vector* voteTable = nullptr; + std::mutex* voteMutex = nullptr; + U64 nodes = 0; U64 nodesToGo = 0; int timeToSearch = 0; diff --git a/source/uci.cpp b/source/uci.cpp index 0082599..9bbff0f 100644 --- a/source/uci.cpp +++ b/source/uci.cpp @@ -13,6 +13,7 @@ #include "utils.h" #include "datagen.h" #include "tunables.h" +#include "termcolor.hpp" // OS-dependent threading includes #ifdef _WIN32 @@ -20,6 +21,8 @@ #else #include #endif +#include +#include namespace { @@ -43,6 +46,47 @@ static std::vector searchThreads; static std::vector searchThreads; #endif +static std::vector rootVotes; +static std::mutex voteMutex; +static std::atomic votePrinted{false}; + +static void PrintVotedBestmove(Move move) { + if (UCIEnabled) { + std::cout << "bestmove "; + if (!move) + std::cout << "0000"; + else + move.PrintMove(); + std::cout << std::endl; + } else { + std::cout << termcolor::color<244> << "bestmove " << termcolor::reset; + if (!move) { + std::cout << "0000" << std::endl; + } else { + std::cout << termcolor::bold << termcolor::bright_green; + move.PrintMove(); + std::cout << termcolor::reset << std::endl; + } + } +} + +static SearchResults GetVotedResult(const SearchResults& fallback) { + std::lock_guard lock(voteMutex); + SearchResults voted = SEARCH::VoteRootMoves(rootVotes); + Move m = voted.bestMove; + if (uint16_t(m) == 0) + return fallback; + return voted; +} + +static bool TryPrintVotedBestmove(const SearchResults& fallback) { + if (votePrinted.exchange(true, std::memory_order_acq_rel)) + return false; + SearchResults voted = GetVotedResult(fallback); + PrintVotedBestmove(voted.bestMove); + return true; +} + static void JoinSearchThreads() { #ifdef _WIN32 for (auto& thread : searchThreads) { @@ -64,6 +108,16 @@ static void StopSearchThreads() { JoinSearchThreads(); } +static void StopSearchThreadsAndVote() { + if (searchThreads.empty()) { + searchStopped.store(true, std::memory_order_relaxed); + return; + } + searchStopped.store(true, std::memory_order_relaxed); + JoinSearchThreads(); + TryPrintVotedBestmove(SearchResults(0, Move())); +} + } // namespace static void ParsePosition(Board &board, std::string_view command, SEARCH::SearchContext* ctx) { @@ -140,12 +194,20 @@ static double ReadParam(const std::string& param, const std::string &command) { // Windows implementation using std::thread template static void ThreadFunc(Board board, SearchParams params, SEARCH::SearchContext* ctx) { - SEARCH::SearchPosition(board, params, ctx); + SearchResults results = SEARCH::SearchPosition(board, params, ctx); + int id = ctx->threadId; delete ctx; + if (id == 0) { + searchStopped.store(true, std::memory_order_relaxed); + TryPrintVotedBestmove(results); + } } static void StartSearchThread(Board& board, SearchParams params, SEARCH::SearchContext* ctx, int id) { SEARCH::SearchContext* ctxCopy = new SEARCH::SearchContext(*ctx); + ctxCopy->threadId = id; + ctxCopy->voteTable = &rootVotes; + ctxCopy->voteMutex = &voteMutex; if (id == 0) ctxCopy->doPrint = true; @@ -162,9 +224,14 @@ template static void* ThreadFunc(void* arg) { auto* tup = static_cast*>(arg); SEARCH::SearchContext* ctx = std::get<2>(*tup); - SEARCH::SearchPosition(std::get<0>(*tup), std::get<1>(*tup), ctx); + SearchResults results = SEARCH::SearchPosition(std::get<0>(*tup), std::get<1>(*tup), ctx); + int id = ctx->threadId; delete ctx; delete tup; + if (id == 0) { + searchStopped.store(true, std::memory_order_relaxed); + TryPrintVotedBestmove(results); + } return nullptr; } @@ -174,6 +241,9 @@ static void StartSearchThread(Board& board, SearchParams params, SEARCH::SearchC pthread_attr_setstacksize(&attr, 8 * 1024 * 1024); // 8 MB stack SEARCH::SearchContext* ctxCopy = new SEARCH::SearchContext(*ctx); + ctxCopy->threadId = id; + ctxCopy->voteTable = &rootVotes; + ctxCopy->voteMutex = &voteMutex; if (id == 0) ctxCopy->doPrint = true; @@ -265,8 +335,20 @@ static void ParseGo(Board &board, std::string &command, SEARCH::SearchContext* c ctx->searchMoves = params.searchMoves; - searchThreads.reserve(threads); - for (int i = 0; i < threads; i++) { + int numThreads = threads; + if (numThreads < 1) + numThreads = 1; + if (numThreads > 512) + numThreads = 512; + + { + std::lock_guard lock(voteMutex); + rootVotes.assign(numThreads, SEARCH::RootVote()); + } + votePrinted.store(false, std::memory_order_relaxed); + + searchThreads.reserve(numThreads); + for (int i = 0; i < numThreads; i++) { StartSearchThread(board, params, ctx, i); } } @@ -281,7 +363,11 @@ static void SetOption(std::string& command, SEARCH::SearchContext* ctx) { } if (command.find("Threads") != std::string::npos) { - threads = ReadParam("value", command); + threads = static_cast(ReadParam("value", command)); + if (threads < 1) + threads = 1; + if (threads > 512) + threads = 512; return; } @@ -404,7 +490,7 @@ void UCILoop(Board &board) { // parse UCI "stop" command if (input.find("stop") != std::string::npos) { - StopSearchThreads(); + StopSearchThreadsAndVote(); continue; }