Merge pull request #195 from rektdie/thread-voting
thread voting | bench 1632393
This commit was merged in pull request #195.
This commit is contained in:
3 files changed
+179
-6
No files matched your search
@@ -13,6 +13,74 @@
|
|||||||
|
|
||||||
namespace SEARCH {
|
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<int>(ctx->voteTable->size()))
|
||||||
|
return;
|
||||||
|
std::lock_guard<std::mutex> 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<RootVote>& votes) {
|
||||||
|
struct Tally {
|
||||||
|
Move move;
|
||||||
|
int count = 0;
|
||||||
|
int score = 0;
|
||||||
|
int depth = 0;
|
||||||
|
};
|
||||||
|
std::vector<Tally> 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) {
|
static WDLTriplet GetRoundedWDL(int score, Board& board) {
|
||||||
WDLTriplet wdl = getWDL(score, board);
|
WDLTriplet wdl = getWDL(score, board);
|
||||||
|
|
||||||
@@ -932,6 +1000,8 @@ static SearchResults ID(Board &board, SearchParams params, SearchContext* ctx) {
|
|||||||
safeResults = currentResults;
|
safeResults = currentResults;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
PublishRootVote(ctx, safeResults, depth);
|
||||||
|
|
||||||
if constexpr (mode == normal || mode == nodesMode) {
|
if constexpr (mode == normal || mode == nodesMode) {
|
||||||
if (ctx->doPrint)
|
if (ctx->doPrint)
|
||||||
PrintSearchInfo(board, ctx, safeResults, depth, elapsed);
|
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 constexpr (mode != normal && mode != nodesMode) return results;
|
||||||
|
|
||||||
|
if (ctx->voteTable != nullptr) {
|
||||||
|
return results;
|
||||||
|
}
|
||||||
|
|
||||||
if (ctx->doPrint) {
|
if (ctx->doPrint) {
|
||||||
if (UCIEnabled) {
|
if (UCIEnabled) {
|
||||||
std::cout << "bestmove ";
|
std::cout << "bestmove ";
|
||||||
|
|||||||
@@ -2,6 +2,7 @@
|
|||||||
#include "board.h"
|
#include "board.h"
|
||||||
#include "uci.h"
|
#include "uci.h"
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
#include <mutex>
|
||||||
#include "move.h"
|
#include "move.h"
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
#include "stopwatch.h"
|
#include "stopwatch.h"
|
||||||
@@ -55,6 +56,14 @@ void RefreshTunableCaches();
|
|||||||
|
|
||||||
class SearchContext;
|
class SearchContext;
|
||||||
|
|
||||||
|
struct RootVote {
|
||||||
|
Move bestMove;
|
||||||
|
int score = 0;
|
||||||
|
int depth = 0;
|
||||||
|
};
|
||||||
|
|
||||||
|
SearchResults VoteRootMoves(const std::vector<RootVote>& votes);
|
||||||
|
|
||||||
class CaptHistory {
|
class CaptHistory {
|
||||||
private:
|
private:
|
||||||
MultiArray<int, 2, 6, 6, 64> historyMoves;
|
MultiArray<int, 2, 6, 6, 64> historyMoves;
|
||||||
@@ -216,6 +225,10 @@ class SearchContext {
|
|||||||
public:
|
public:
|
||||||
bool doPrint = false;
|
bool doPrint = false;
|
||||||
|
|
||||||
|
int threadId = 0;
|
||||||
|
std::vector<RootVote>* voteTable = nullptr;
|
||||||
|
std::mutex* voteMutex = nullptr;
|
||||||
|
|
||||||
U64 nodes = 0;
|
U64 nodes = 0;
|
||||||
U64 nodesToGo = 0;
|
U64 nodesToGo = 0;
|
||||||
int timeToSearch = 0;
|
int timeToSearch = 0;
|
||||||
|
|||||||
+92
-6
@@ -13,6 +13,7 @@
|
|||||||
#include "utils.h"
|
#include "utils.h"
|
||||||
#include "datagen.h"
|
#include "datagen.h"
|
||||||
#include "tunables.h"
|
#include "tunables.h"
|
||||||
|
#include "termcolor.hpp"
|
||||||
|
|
||||||
// OS-dependent threading includes
|
// OS-dependent threading includes
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
@@ -20,6 +21,8 @@
|
|||||||
#else
|
#else
|
||||||
#include <pthread.h>
|
#include <pthread.h>
|
||||||
#endif
|
#endif
|
||||||
|
#include <mutex>
|
||||||
|
#include <atomic>
|
||||||
|
|
||||||
namespace {
|
namespace {
|
||||||
|
|
||||||
@@ -43,6 +46,47 @@ static std::vector<std::thread> searchThreads;
|
|||||||
static std::vector<pthread_t> searchThreads;
|
static std::vector<pthread_t> searchThreads;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
static std::vector<SEARCH::RootVote> rootVotes;
|
||||||
|
static std::mutex voteMutex;
|
||||||
|
static std::atomic<bool> 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<std::mutex> 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() {
|
static void JoinSearchThreads() {
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
for (auto& thread : searchThreads) {
|
for (auto& thread : searchThreads) {
|
||||||
@@ -64,6 +108,16 @@ static void StopSearchThreads() {
|
|||||||
JoinSearchThreads();
|
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
|
} // namespace
|
||||||
|
|
||||||
static void ParsePosition(Board &board, std::string_view command, SEARCH::SearchContext* ctx) {
|
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
|
// Windows implementation using std::thread
|
||||||
template <SEARCH::searchMode mode>
|
template <SEARCH::searchMode mode>
|
||||||
static void ThreadFunc(Board board, SearchParams params, SEARCH::SearchContext* ctx) {
|
static void ThreadFunc(Board board, SearchParams params, SEARCH::SearchContext* ctx) {
|
||||||
SEARCH::SearchPosition<mode>(board, params, ctx);
|
SearchResults results = SEARCH::SearchPosition<mode>(board, params, ctx);
|
||||||
|
int id = ctx->threadId;
|
||||||
delete ctx;
|
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) {
|
static void StartSearchThread(Board& board, SearchParams params, SEARCH::SearchContext* ctx, int id) {
|
||||||
SEARCH::SearchContext* ctxCopy = new SEARCH::SearchContext(*ctx);
|
SEARCH::SearchContext* ctxCopy = new SEARCH::SearchContext(*ctx);
|
||||||
|
ctxCopy->threadId = id;
|
||||||
|
ctxCopy->voteTable = &rootVotes;
|
||||||
|
ctxCopy->voteMutex = &voteMutex;
|
||||||
if (id == 0)
|
if (id == 0)
|
||||||
ctxCopy->doPrint = true;
|
ctxCopy->doPrint = true;
|
||||||
|
|
||||||
@@ -162,9 +224,14 @@ 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);
|
||||||
SEARCH::SearchContext* ctx = std::get<2>(*tup);
|
SEARCH::SearchContext* ctx = std::get<2>(*tup);
|
||||||
SEARCH::SearchPosition<mode>(std::get<0>(*tup), std::get<1>(*tup), ctx);
|
SearchResults results = SEARCH::SearchPosition<mode>(std::get<0>(*tup), std::get<1>(*tup), ctx);
|
||||||
|
int id = ctx->threadId;
|
||||||
delete ctx;
|
delete ctx;
|
||||||
delete tup;
|
delete tup;
|
||||||
|
if (id == 0) {
|
||||||
|
searchStopped.store(true, std::memory_order_relaxed);
|
||||||
|
TryPrintVotedBestmove(results);
|
||||||
|
}
|
||||||
return nullptr;
|
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
|
pthread_attr_setstacksize(&attr, 8 * 1024 * 1024); // 8 MB stack
|
||||||
|
|
||||||
SEARCH::SearchContext* ctxCopy = new SEARCH::SearchContext(*ctx);
|
SEARCH::SearchContext* ctxCopy = new SEARCH::SearchContext(*ctx);
|
||||||
|
ctxCopy->threadId = id;
|
||||||
|
ctxCopy->voteTable = &rootVotes;
|
||||||
|
ctxCopy->voteMutex = &voteMutex;
|
||||||
if (id == 0)
|
if (id == 0)
|
||||||
ctxCopy->doPrint = true;
|
ctxCopy->doPrint = true;
|
||||||
|
|
||||||
@@ -265,8 +335,20 @@ static void ParseGo(Board &board, std::string &command, SEARCH::SearchContext* c
|
|||||||
|
|
||||||
ctx->searchMoves = params.searchMoves;
|
ctx->searchMoves = params.searchMoves;
|
||||||
|
|
||||||
searchThreads.reserve(threads);
|
int numThreads = threads;
|
||||||
for (int i = 0; i < threads; i++) {
|
if (numThreads < 1)
|
||||||
|
numThreads = 1;
|
||||||
|
if (numThreads > 512)
|
||||||
|
numThreads = 512;
|
||||||
|
|
||||||
|
{
|
||||||
|
std::lock_guard<std::mutex> 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);
|
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) {
|
if (command.find("Threads") != std::string::npos) {
|
||||||
threads = ReadParam("value", command);
|
threads = static_cast<int>(ReadParam("value", command));
|
||||||
|
if (threads < 1)
|
||||||
|
threads = 1;
|
||||||
|
if (threads > 512)
|
||||||
|
threads = 512;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -404,7 +490,7 @@ void UCILoop(Board &board) {
|
|||||||
|
|
||||||
// parse UCI "stop" command
|
// parse UCI "stop" command
|
||||||
if (input.find("stop") != std::string::npos) {
|
if (input.find("stop") != std::string::npos) {
|
||||||
StopSearchThreads();
|
StopSearchThreadsAndVote();
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user