From 6e905fdf2e1c4a1bec8972a8fdff3923d5318d2c Mon Sep 17 00:00:00 2001 From: rektdie Date: Sun, 6 Apr 2025 22:10:41 +0200 Subject: [PATCH] game buffering in datagen --- source/datagen.cpp | 221 ++++++++++++++++----------------------------- source/datagen.h | 4 + source/stopwatch.h | 1 + 3 files changed, 84 insertions(+), 142 deletions(-) diff --git a/source/datagen.cpp b/source/datagen.cpp index 0f300a0..3673e99 100644 --- a/source/datagen.cpp +++ b/source/datagen.cpp @@ -10,7 +10,6 @@ #include "evaluate.h" #include "search.h" #include "movegen.h" -#include "stopwatch.h" #ifdef _WIN32 #include @@ -80,79 +79,100 @@ static bool IsGameOver(Board &board) { return false; } -std::ostream &operator<<(std::ostream &os, const MarlinFormat &format) { - os.write(reinterpret_cast(&format), sizeof(MarlinFormat)); - return os; +static void WriteToFile(std::vector &gamesBuffer, std::ofstream &file) { + int32_t zeroes = 0; + for (const Game& game : gamesBuffer) { + file.write(reinterpret_cast(&game.format), sizeof(game.format)); + file.write(reinterpret_cast(game.moves.data()), sizeof(ScoredMove) * game.moves.size()); + file.write(reinterpret_cast(&zeroes), 4); + } + file.flush(); } -std::istream &operator>>(std::istream &is, MarlinFormat &format) { - is.read(reinterpret_cast(&format), sizeof(MarlinFormat)); - return is; +void PlayGame(std::atomic& positions, U64 targetPositions, std::vector &gamesBuffer, std::ofstream &file) { + while (positions.load(std::memory_order_relaxed) < targetPositions) { + Game game; + Board board; + + uint8_t wdl = 1; + positionIndex = 0; + std::memset(SEARCH::killerMoves, 0, sizeof(SEARCH::killerMoves)); + SEARCH::history.Clear(); + + PlayRandMoves(board); + + int staticEval = HCE::Evaluate(board); + + SEARCH::SearchResults safeResults; + + MOVEGEN::GenerateMoves(board); + + while (!IsGameOver(board)) { + SEARCH::SearchResults results = SEARCH::SearchPosition(board, SearchParams()); + + if (!results.bestMove) break; + safeResults = results; + + game.moves.push_back(ScoredMove(results.bestMove.ConvertToViriMoveFormat(), UTILS::ConvertToWhiteRelative(board, results.score))); + board.MakeMove(results.bestMove); + MOVEGEN::GenerateMoves(board); + + positions.fetch_add(1, std::memory_order_relaxed); + + if (std::abs(safeResults.score) >= std::abs(SEARCH::MATE_SCORE) - SEARCH::MAX_PLY) { + wdl = board.sideToMove ? 2 : 0; + } + } + game.format.packFrom(board, UTILS::ConvertToWhiteRelative(board, staticEval), wdl); + gamesBuffer.push_back(game); + + std::lock_guard guard(mutex); + if (gamesBuffer.size() >= GAME_BUFFER) { + WriteToFile(gamesBuffer, file); + gamesBuffer.clear(); + } + } } -std::ostream &operator<<(std::ostream &os, const ScoredMove &move) { - os.write(reinterpret_cast(&move), sizeof(ScoredMove)); - return os; -} +void Run(int targetPositions, int threads) { + #ifdef _WIN32 + system("cls"); + HideCursor(); + #else + std::cout << "\033[2J\033[H"; + HideCursorLinux(); + #endif -std::istream &operator>>(std::istream &is, ScoredMove &move) { - is.read(reinterpret_cast(&move), sizeof(ScoredMove)); - return is; -} + Stopwatch stopwatch; -std::ostream &operator<<(std::ostream &os, const Game &game) { - os << game.format; + std::atomic positions = 0; + std::vector gamesBuffer; - uint32_t moveCount = game.moves.size(); - os.write(reinterpret_cast(&moveCount), sizeof(moveCount)); + std::ofstream file("data.binpack", std::ios::app | std::ios::binary); - for (const auto &move : game.moves) { - os << move; + for (int i = 0; i < threads; i++) { + auto worker = std::thread(PlayGame, std::ref(positions), targetPositions * 1000, std::ref(gamesBuffer), std::ref(file)); + worker.detach(); } - return os; -} + int lastPrintedPosition = 0; + double lastPrintedTime = stopwatch.GetElapsedSec(); -std::istream &operator>>(std::istream &is, Game &game) { - is >> game.format; - - uint32_t moveCount; - is.read(reinterpret_cast(&moveCount), sizeof(moveCount)); - - game.moves.resize(moveCount); - for (auto &move : game.moves) { - is >> move; + while (positions.load(std::memory_order_relaxed) < targetPositions * 1000) { + // Print progress every second + PrintProgress(positions, targetPositions * 1000, stopwatch, threads); + std::this_thread::sleep_for(std::chrono::milliseconds(1000)); } - return is; -} + PrintProgress(positions, targetPositions * 1000, stopwatch, threads); + WriteToFile(gamesBuffer, file); -static void WriteToFile(const Game &game, const std::string &filename) { - std::lock_guard lock(mutex); - // Create a buffer to store the serialized data [prevents corruption] - std::vector buffer; - const size_t formatSize = sizeof(MarlinFormat); - buffer.resize(formatSize + sizeof(uint32_t) * game.moves.size() + sizeof(uint32_t)); - - std::memcpy(buffer.data(), &game.format, formatSize); - - size_t offset = formatSize; - - for (const auto &move : game.moves) { - std::memcpy(buffer.data() + offset, &move, sizeof(ScoredMove)); - offset += sizeof(ScoredMove); - } - - uint32_t zero = 0; - std::memcpy(buffer.data() + offset, &zero, sizeof(zero)); - - std::ofstream file(filename, std::ios::binary | std::ios::out | std::ios::app); - if (!file) throw std::runtime_error("Failed to open file for writing"); - - file.write(buffer.data(), buffer.size()); - - file.close(); + #ifdef _WIN32 + ShowCursor(); + #else + ShowCursorLinux(); + #endif } void PrintProgress(int positions, int targetPositions, Stopwatch &stopwatch, int threads) { @@ -225,87 +245,4 @@ void PrintProgress(int positions, int targetPositions, Stopwatch &stopwatch, int std::cout << std::string(width, '-') << std::endl; } -void PlayGame(std::atomic& positions, int targetPositions) { - while (positions.load(std::memory_order_relaxed) < targetPositions) { - Game game; - Board board; - - uint8_t wdl = 1; - positionIndex = 0; - std::memset(SEARCH::killerMoves, 0, sizeof(SEARCH::killerMoves)); - SEARCH::history.Clear(); - - PlayRandMoves(board); - - int staticEval = HCE::Evaluate(board); - - SEARCH::SearchResults safeResults; - - MOVEGEN::GenerateMoves(board); - - while (!IsGameOver(board)) { - SEARCH::SearchResults results = SEARCH::SearchPosition(board, SearchParams()); - - if (!results.bestMove) break; - safeResults = results; - - game.moves.push_back(ScoredMove(results.bestMove.ConvertToViriMoveFormat(), UTILS::ConvertToWhiteRelative(board, results.score))); - board.MakeMove(results.bestMove); - MOVEGEN::GenerateMoves(board); - - // Increment positions, but do not exceed target - if (positions.load(std::memory_order_relaxed) < targetPositions) { - positions.fetch_add(1, std::memory_order_relaxed); - - if (std::abs(safeResults.score) >= std::abs(SEARCH::MATE_SCORE) - SEARCH::MAX_PLY) { - wdl = board.sideToMove ? 2 : 0; - } - - - } - } - game.format.packFrom(board, UTILS::ConvertToWhiteRelative(board, staticEval), wdl); - WriteToFile(game, "data.binpack"); - } -} - -void Run(int targetPositions, int threads) { - #ifdef _WIN32 - system("cls"); - HideCursor(); - #else - std::cout << "\033[2J\033[H"; - HideCursorLinux(); - #endif - - Stopwatch stopwatch; - - std::atomic positions = 0; - - for (int i = 0; i < threads; i++) { - auto worker = std::thread(PlayGame, std::ref(positions), targetPositions * 1000); - worker.detach(); - } - - int lastPrintedPosition = 0; - double lastPrintedTime = stopwatch.GetElapsedSec(); - - while (positions.load(std::memory_order_relaxed) < targetPositions * 1000) { - // Print progress every second - double currentTime = stopwatch.GetElapsedSec(); - if (currentTime - lastPrintedTime >= 1) { - lastPrintedTime = currentTime; - PrintProgress(positions, targetPositions * 1000, stopwatch, threads); - } - } - - PrintProgress(positions, targetPositions * 1000, stopwatch, threads); - - #ifdef _WIN32 - ShowCursor(); - #else - ShowCursorLinux(); - #endif -} - } \ No newline at end of file diff --git a/source/datagen.h b/source/datagen.h index 28003ea..a60572f 100644 --- a/source/datagen.h +++ b/source/datagen.h @@ -2,9 +2,11 @@ #include "board.h" #include "utils.h" #include +#include "stopwatch.h" namespace DATAGEN { +constexpr int GAME_BUFFER = 1000; constexpr int SOFT_NODES = 5000; constexpr int HARD_NODES = 100000; constexpr int RAND_MOVES = 8; @@ -78,4 +80,6 @@ struct Game { }; void Run(int targetPositions, int threads); + +void PrintProgress(int positions, int targetPositions, Stopwatch &stopwatch, int threads); } \ No newline at end of file diff --git a/source/stopwatch.h b/source/stopwatch.h index 211f851..fe47086 100644 --- a/source/stopwatch.h +++ b/source/stopwatch.h @@ -1,3 +1,4 @@ +#pragma once #include class Stopwatch {