Tt test #87

Merged
rektdie merged 5 commits from tt-test into main 2025-08-06 16:53:28 +02:00
4 changed files with 84 additions and 54 deletions

No files matched your search

+33 -13
View File
@@ -2,6 +2,8 @@
#include <fstream>
#include <iomanip>
#include <thread>
#include <pthread.h>
#include <tuple>
#include <atomic>
#include <mutex>
#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<int> moveDist(0, 1);
bool plusOne = moveDist(rng);
for (int count = 0; count < RAND_MOVES + plusOne; count++) {
MOVEGEN::GenerateMoves<All>(board);
@@ -141,7 +143,7 @@ static void PlayGames(int id, std::atomic<int>& positions, std::atomic<bool>& st
while (!stopFlag) {
Board board;
auto ctx = std::make_unique<SEARCH::SearchContext>();
PlayRandMoves(board, ctx.get());
if (IsGameOver(board, ctx.get())) continue;
@@ -210,11 +212,31 @@ void Run(int targetPositions, int threads) {
std::atomic<int> positions = 0;
std::atomic<bool> stopFlag = false;
std::vector<std::thread> workerThreads;
std::vector<pthread_t> 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, 8 * 1024 * 1024); // 2 MB stack
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)
{
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;
+18 -10
View File
@@ -359,16 +359,23 @@ SearchResults PVS(Board& board, int depth, int alpha, int beta, int ply, SearchC
if (ply && (IsDraw(board, ctx))) return 0;
TTEntry entry = TTEntry(invalidEntry);
TTEntry* entry = nullptr;
if (!ctx->excluded)
entry = ctx->TT.ReadEntry(board.hashKey, depth, alpha, beta);
entry = ctx->TT.GetRawEntry(board.hashKey);
const bool ttHit = entry.score != invalidEntry;
const bool ttHit = entry != nullptr
&& entry->hashKey == board.hashKey;
// if NOT PV node then we try to hit the TTable
if constexpr (!isPV) {
if (ttHit)
return SearchResults(entry.score, entry.bestMove);
if (ttHit) {
if (entry->depth >= depth &&
((entry->nodeType == PV) ||
(entry->nodeType == AllNode && entry->score <= alpha) ||
(entry->nodeType == CutNode && entry->score >= beta))) {
return SearchResults(entry->score, entry->bestMove);
}
}
}
if (depth <= 0) return Quiescence<mode>(board, alpha, beta, ply, ctx);
@@ -480,12 +487,13 @@ SearchResults PVS(Board& board, int depth, int alpha, int beta, int ply, SearchC
if (ply
&& depth >= 8
&& currMove == entry.bestMove
&& ttHit
&& currMove == entry->bestMove
&& ctx->excluded == 0
&& entry.depth >= depth - 3
&& entry.nodeType != AllNode)
&& entry->depth >= depth - 3
&& entry->nodeType != AllNode)
{
const int sBeta = std::max(-inf + 1, entry.score - depth * 2);
const int sBeta = std::max(-inf + 1, entry->score - depth * 2);
const int sDepth = (depth - 1) / 2;
ctx->excluded = currMove;
-19
View File
@@ -1,24 +1,5 @@
#include "tt.h"
TTEntry TTable::ReadEntry(U64 &hashKey, int depth, int alpha, int beta) {
TTEntry *current = &table[hashKey % table.size()];
if (current->hashKey == hashKey) {
if (current->depth >= depth) {
if (current->nodeType == PV){
return *current;
} else if (current->nodeType == AllNode && current->score <= alpha) {
return *current;
} else if (current->nodeType == CutNode && current->score >= beta) {
return *current;
}
}
}
// returning invalid value
return invalidEntry;
}
void TTable::WriteEntry(U64 &hashKey, int depth, int score, int nodeType, Move bestMove) {
TTEntry *current = &table[hashKey % table.size()];
+33 -12
View File
@@ -1,9 +1,9 @@
#include "uci.h"
#include <iostream>
#include <algorithm>
#include <cstring>
#include <string>
#include <thread>
#include <pthread.h>
#include <tuple>
#include "types.h"
#include "movegen.h"
#include "search.h"
@@ -69,6 +69,33 @@ static int ReadParam(std::string param, std::string &command) {
return 0;
}
template <SEARCH::searchMode mode>
static void* ThreadFunc(void* arg) {
auto* tup = static_cast<std::tuple<Board*, SearchParams, SEARCH::SearchContext*>*>(arg);
SEARCH::SearchPosition<mode>(*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, 8 * 1024 * 1024); // 2 MB stack
pthread_t thread;
if (params.nodes) {
auto* arg = new std::tuple<Board*, SearchParams, SEARCH::SearchContext*>(&board, params, ctx);
pthread_create(&thread, &attr, ThreadFunc<SEARCH::nodesMode>, arg);
} else {
auto* arg = new std::tuple<Board*, SearchParams, SEARCH::SearchContext*>(&board, params, ctx);
pthread_create(&thread, &attr, ThreadFunc<SEARCH::normal>, arg);
}
pthread_attr_destroy(&attr);
pthread_detach(thread);
}
static void ParseGo(Board &board, std::string &command, SEARCH::SearchContext* ctx) {
SearchParams params;
@@ -87,13 +114,7 @@ static void ParseGo(Board &board, std::string &command, SEARCH::SearchContext* c
params.btime = 99999999;
}
if (params.nodes) {
auto worker = std::thread(SEARCH::SearchPosition<SEARCH::nodesMode>, std::ref(board), params, ctx);
worker.detach();
} else {
auto worker = std::thread(SEARCH::SearchPosition<SEARCH::normal>, std::ref(board), params, ctx);
worker.detach();
}
StartSearchThread(board, params, ctx);
}
static void SetOption(std::string &command, SEARCH::SearchContext* ctx) {
@@ -136,10 +157,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 +172,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;
}