Datagen rewrite #55

Merged
rektdie merged 3 commits from datagen-rewrite into main 2025-04-15 18:56:07 +02:00
Showing only changes of commit 55b73dc85d - Show all commits
+13 -7
View File
@@ -4,7 +4,6 @@
#include <iomanip> #include <iomanip>
#include <thread> #include <thread>
#include <atomic> #include <atomic>
#include <mutex>
#include "datagen.h" #include "datagen.h"
#include "movegen.h" #include "movegen.h"
#include "evaluate.h" #include "evaluate.h"
@@ -53,6 +52,7 @@ static void PlayRandMoves(Board &board, SEARCH::SearchContext& ctx) {
bool isLegal = board.MakeMove(board.moveList[dist(rng)]); bool isLegal = board.MakeMove(board.moveList[dist(rng)]);
if (!isLegal) count--; if (!isLegal) count--;
ctx.positionHistory[board.positionIndex] = board.hashKey; ctx.positionHistory[board.positionIndex] = board.hashKey;
} }
@@ -88,12 +88,15 @@ static void WriteToFile(std::vector<Game> &gamesBuffer, std::ofstream &file) {
static void PlayGames(int id, std::atomic<int>& positions, std::atomic<bool>& stopFlag) { static void PlayGames(int id, std::atomic<int>& positions, std::atomic<bool>& stopFlag) {
std::vector<Game> gamesBuffer; std::vector<Game> gamesBuffer;
gamesBuffer.reserve(GAME_BUFFER);
std::string fileName = "thread" + std::to_string(id) + ".binpack"; std::string fileName = "thread" + std::to_string(id) + ".binpack";
std::ofstream file(fileName, std::ios::app | std::ios::binary); std::ofstream file(fileName, std::ios::app | std::ios::binary);
if (!file.is_open()) { if (!file.is_open()) {
std::cerr << "Failed to open file: " << fileName << std::endl; std::cerr << "Failed to open file: " << fileName << std::endl;
exit(-1);
} }
while (!stopFlag) { while (!stopFlag) {
@@ -115,7 +118,7 @@ static void PlayGames(int id, std::atomic<int>& positions, std::atomic<bool>& st
board.MakeMove(results.bestMove); board.MakeMove(results.bestMove);
ctx.positionHistory[board.positionIndex] = board.hashKey; ctx.positionHistory[board.positionIndex] = board.hashKey;
game.moves.push_back(ScoredMove(results.bestMove.ConvertToViriMoveFormat(), game.moves.emplace_back(ScoredMove(results.bestMove.ConvertToViriMoveFormat(),
UTILS::ConvertToWhiteRelative(board, results.score))); UTILS::ConvertToWhiteRelative(board, results.score)));
positions++; positions++;
@@ -128,7 +131,7 @@ static void PlayGames(int id, std::atomic<int>& positions, std::atomic<bool>& st
} }
game.format.packFrom(board, staticEval, wdl); game.format.packFrom(board, staticEval, wdl);
gamesBuffer.push_back(game); gamesBuffer.emplace_back(game);
if (gamesBuffer.size() >= GAME_BUFFER) { if (gamesBuffer.size() >= GAME_BUFFER) {
WriteToFile(gamesBuffer, file); WriteToFile(gamesBuffer, file);
@@ -157,8 +160,9 @@ void Run(int targetPositions, int threads) {
std::vector<std::thread> workerThreads; std::vector<std::thread> workerThreads;
for (int i = 0; i < threads; i++) { // Start N-1 threads
workerThreads.push_back(std::thread(PlayGames, i, std::ref(positions), std::ref(stopFlag))); for (int i = 0; i < threads-1; i++) {
workerThreads.emplace_back(std::thread(PlayGames, i, std::ref(positions), std::ref(stopFlag)));
} }
Stopwatch sw; Stopwatch sw;
@@ -173,11 +177,13 @@ void Run(int targetPositions, int threads) {
stopFlag = true; stopFlag = true;
for (auto& thread : workerThreads) { for (auto& thread : workerThreads) {
thread.join(); if (thread.joinable()) {
thread.join();
}
} }
#ifdef _WIN32 #ifdef _WIN32
ShowCursor(); ShowCursor();
#else #else
ShowCursorLinux(); ShowCursorLinux();