#include #include #include #include #include #include #include #include "datagen.h" #include "movegen.h" #include "evaluate.h" #include "search.h" #include "movegen.h" #ifdef _WIN32 #include #endif #ifdef _WIN32 static void HideCursor() { HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE); CONSOLE_CURSOR_INFO cursorInfo; GetConsoleCursorInfo(hConsole, &cursorInfo); cursorInfo.bVisible = FALSE; SetConsoleCursorInfo(hConsole, &cursorInfo); } static void ShowCursor() { HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE); CONSOLE_CURSOR_INFO cursorInfo; GetConsoleCursorInfo(hConsole, &cursorInfo); cursorInfo.bVisible = TRUE; SetConsoleCursorInfo(hConsole, &cursorInfo); } #endif static void HideCursorLinux() { std::cout << "\033[?25l"; } static void ShowCursorLinux() { std::cout << "\033[?25h"; } namespace DATAGEN { std::mutex mutex; static void PlayRandMoves(Board &board) { std::random_device dev; std::mt19937 rng(dev()); for (int count = 0; count < RAND_MOVES; count++) { MOVEGEN::GenerateMoves(board); std::uniform_int_distribution dist(0, board.currentMoveIndex - 1); bool isLegal = board.MakeMove(board.moveList[dist(rng)]); if (!isLegal) count--; } MOVEGEN::GenerateMoves(board); } static bool IsGameOver(Board &board) { bool isDraw = SEARCH::IsDraw(board); if (isDraw) return true; int moveSeen = 0; for (int i = 0; i < board.currentMoveIndex; i++) { Board copy = board; bool isLegal = copy.MakeMove(board.moveList[i]); if (!isLegal) continue; positionIndex--; moveSeen++; } if (moveSeen == 0) return true; return false; } 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(); } 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(); } } } 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; std::vector gamesBuffer; std::ofstream file("data.binpack", std::ios::app | std::ios::binary); 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(); } int lastPrintedPosition = 0; double lastPrintedTime = stopwatch.GetElapsedSec(); 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)); } PrintProgress(positions, targetPositions * 1000, stopwatch, threads); WriteToFile(gamesBuffer, file); #ifdef _WIN32 ShowCursor(); #else ShowCursorLinux(); #endif } void PrintProgress(int positions, int targetPositions, Stopwatch &stopwatch, int threads) { double elapsed = stopwatch.GetElapsedSec(); double positionsPerSec = elapsed > 0 ? positions / elapsed : 0; int positionsInThousands = static_cast(round(positions / 1000.0)); int targetInThousands = static_cast(round(targetPositions / 1000.0)); std::cout << "\033[H"; int width = 60; std::string title = "Eleanor - Datagen"; int titlePadding = (width - title.length()) / 2; std::cout << std::setw(titlePadding + title.length()) << title << std::endl; std::cout << std::string(width, '=') << std::endl; std::cout << std::endl; std::cout << std::fixed << std::setprecision(0); std::ostringstream datagenTextStream; 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; std::cout << std::setw(datagenPadding + datagenText.length()) << datagenText << std::endl; std::cout << std::endl; std::string elapsedText = "Elapsed Time: " + std::to_string(static_cast(round(elapsed))) + " sec"; int elapsedPadding = (width - elapsedText.length()) / 2; std::cout << std::setw(elapsedPadding + elapsedText.length()) << elapsedText << std::endl; std::string positionsPerSecText = "Positions/sec: " + std::to_string(static_cast(round(positionsPerSec))); int positionsPerSecPadding = (width - positionsPerSecText.length()) / 2; std::cout << std::setw(positionsPerSecPadding + positionsPerSecText.length()) << positionsPerSecText << std::endl; std::cout << std::endl; std::string threadsText = "Threads: " + std::to_string(static_cast(threads)); int threadsTextPadding = (width - threadsText.length()) / 2; std::cout << std::setw(threadsTextPadding + threadsText.length()) << threadsText << std::endl; std::cout << std::endl; int barWidth = 50; double progress = (double)positions / targetPositions; int pos = (int)(progress * barWidth); std::string progressBar = "["; for (int i = 0; i < barWidth; ++i) { if (i < pos) { progressBar += "="; } else if (i == pos) { progressBar += ">"; } else { progressBar += " "; } } progressBar += "] " + std::to_string(static_cast(round(progress * 100.0))) + " %"; int progressPadding = (width - progressBar.length()) / 2; std::cout << std::setw(progressPadding + progressBar.length()) << progressBar << std::endl; std::cout << std::endl; std::cout << std::string(width, '-') << std::endl; } }