From a608b357ca7dbdaec642ca742de8726e4ad62c69 Mon Sep 17 00:00:00 2001 From: rektdie Date: Mon, 14 Apr 2025 10:59:53 +0200 Subject: [PATCH] rewrote datagen (possible bugs) --- source/datagen.cpp | 188 +++++++++++++++++---------------------------- source/main.cpp | 2 +- 2 files changed, 72 insertions(+), 118 deletions(-) diff --git a/source/datagen.cpp b/source/datagen.cpp index 732e973..e0513f7 100644 --- a/source/datagen.cpp +++ b/source/datagen.cpp @@ -5,7 +5,6 @@ #include #include #include -#include #include "datagen.h" #include "movegen.h" #include "evaluate.h" @@ -44,9 +43,7 @@ static void ShowCursorLinux() { namespace DATAGEN { -std::mutex bufferMutex; - -static void PlayRandMoves(Board &board) { +static void PlayRandMoves(Board &board, SEARCH::SearchContext& ctx) { std::random_device dev; std::mt19937 rng(dev()); @@ -57,6 +54,7 @@ static void PlayRandMoves(Board &board) { bool isLegal = board.MakeMove(board.moveList[dist(rng)]); if (!isLegal) count--; + ctx.positionHistory[board.positionIndex] = board.hashKey; } MOVEGEN::GenerateMoves(board); @@ -72,8 +70,7 @@ static bool IsGameOver(Board &board, SEARCH::SearchContext& ctx) { bool isLegal = copy.MakeMove(board.moveList[i]); if (!isLegal) continue; - moveSeen++; - } + moveSeen++; } if (moveSeen == 0) return true; return false; @@ -89,69 +86,60 @@ static void WriteToFile(std::vector &gamesBuffer, std::ofstream &file) { file.flush(); } -void PlayGame(std::atomic& positions, U64 targetPositions, std::vector &gamesBuffer, - std::ofstream &file, std::atomic& shouldStop) { - while (!shouldStop.load(std::memory_order_relaxed) && - positions.load(std::memory_order_relaxed) < targetPositions) { +static void PlayGames(int id, std::atomic& positions, std::atomic& stopFlag) { + std::vector gamesBuffer; + std::string fileName = "thread" + std::to_string(id) + ".binpack"; + + + std::ofstream file(fileName, std::ios::app | std::ios::binary); + if (!file.is_open()) { + std::cerr << "Failed to open file: " << fileName << std::endl; + } + + while (!stopFlag) { Game game; Board board; - - uint8_t wdl = 1; - SEARCH::SearchContext ctx; + PlayRandMoves(board, ctx); + + SearchResults safeResults; + + int staticEval = UTILS::ConvertToWhiteRelative(board, HCE::Evaluate(board)); + int wdl = 1; + + while (!IsGameOver(board, ctx)) { + SearchResults results = SEARCH::SearchPosition(board, SearchParams(), ctx); + + if (!results.bestMove) break; + safeResults = results; + board.MakeMove(results.bestMove); + ctx.positionHistory[board.positionIndex] = board.hashKey; + + game.moves.push_back(ScoredMove(results.bestMove.ConvertToViriMoveFormat(), + UTILS::ConvertToWhiteRelative(board, results.score))); + + positions++; - try { - PlayRandMoves(board); - int staticEval = HCE::Evaluate(board); - SearchResults safeResults; MOVEGEN::GenerateMoves(board); - - while (!IsGameOver(board, ctx)) { - SearchResults results = SEARCH::SearchPosition(board, SearchParams(), ctx); - results = SEARCH::SearchPosition(board, SearchParams(), ctx); - - if (!results.bestMove) break; - safeResults = results; - - game.moves.push_back(ScoredMove(results.bestMove.ConvertToViriMoveFormat(), UTILS::ConvertToWhiteRelative(board, results.score))); - - // Error checking for move making - if (!board.MakeMove(results.bestMove)) { - // Invalid move - break the loop - break; - } - - 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; - } - } - - // Only add the game to buffer if we haven't been told to stop - if (!shouldStop.load(std::memory_order_relaxed)) { - game.format.packFrom(board, UTILS::ConvertToWhiteRelative(board, staticEval), wdl); - - // Use a proper lock instead of spin-waiting - { - std::lock_guard lock(bufferMutex); - - // Check again after acquiring the lock - if (!shouldStop.load(std::memory_order_relaxed)) { - gamesBuffer.push_back(game); - - if (gamesBuffer.size() >= GAME_BUFFER) { - WriteToFile(gamesBuffer, file); - gamesBuffer.clear(); - } - } - } - } - } catch (const std::exception& e) { - // Log exception but allow thread to continue - std::cerr << "Thread exception: " << e.what() << std::endl; } + + if (std::abs(safeResults.score) >= std::abs(SEARCH::MATE_SCORE) - SEARCH::MAX_PLY) { + wdl = board.sideToMove ? 2 : 0; + } + + game.format.packFrom(board, staticEval, wdl); + gamesBuffer.push_back(game); + + if (gamesBuffer.size() >= GAME_BUFFER) { + WriteToFile(gamesBuffer, file); + gamesBuffer.clear(); + } + } + + // Write remaining games to file + if (gamesBuffer.size() > 0) { + WriteToFile(gamesBuffer, file); + gamesBuffer.clear(); } } @@ -164,65 +152,31 @@ void Run(int targetPositions, int threads) { HideCursorLinux(); #endif - Stopwatch stopwatch; - std::atomic positions = 0; - std::atomic shouldStop(false); - std::vector gamesBuffer; - std::vector workers; + std::atomic stopFlag = false; - try { - std::ofstream file("data.binpack", std::ios::app | std::ios::binary); - if (!file.is_open()) { - throw std::runtime_error("Failed to open output file"); - } + std::vector workerThreads; - // Create and start worker threads - for (int i = 0; i < threads; i++) { - workers.push_back(std::thread(PlayGame, std::ref(positions), - targetPositions * 1000, std::ref(gamesBuffer), - std::ref(file), std::ref(shouldStop))); - } - - // Monitor progress - while (positions.load(std::memory_order_relaxed) < targetPositions * 1000) { - PrintProgress(positions, targetPositions * 1000, stopwatch, threads); - std::this_thread::sleep_for(std::chrono::milliseconds(1000)); - } - - // Signal threads to stop - shouldStop.store(true, std::memory_order_relaxed); - - // Wait for all threads to finish - for (auto& worker : workers) { - if (worker.joinable()) { - worker.join(); - } - } - - // Final progress update and buffer flush - PrintProgress(positions, targetPositions * 1000, stopwatch, threads); - - // Final buffer flush with proper synchronization - { - std::lock_guard lock(bufferMutex); - WriteToFile(gamesBuffer, file); - } - - } catch (const std::exception& e) { - std::cerr << "Error in data generation: " << e.what() << std::endl; - - // Signal threads to stop in case of error - shouldStop.store(true, std::memory_order_relaxed); - - // Make sure to join any running threads - for (auto& worker : workers) { - if (worker.joinable()) { - worker.join(); - } - } + for (int i = 0; i < threads; i++) { + workerThreads.push_back(std::thread(PlayGames, i, std::ref(positions), std::ref(stopFlag))); } + Stopwatch sw; + while (positions < targetPositions) { + PrintProgress(positions, targetPositions, sw, threads); + std::this_thread::sleep_for(std::chrono::seconds(1)); + } + + + PrintProgress(positions, targetPositions, sw, threads); + + stopFlag = true; + + for (auto& thread : workerThreads) { + thread.join(); + } + + #ifdef _WIN32 ShowCursor(); #else diff --git a/source/main.cpp b/source/main.cpp index ad1a0e1..dda668a 100644 --- a/source/main.cpp +++ b/source/main.cpp @@ -57,7 +57,7 @@ int main(int argc, char* argv[]) { int positions = 1; int threads = 1; if (argc > 2) { - positions = std::stoi(argv[2]); + positions = std::stoi(argv[2]) * 1000; if (argc > 3) { threads = std::stoi(argv[3]);