diff --git a/source/datagen.cpp b/source/datagen.cpp index bcc8847..cfd2ce9 100644 --- a/source/datagen.cpp +++ b/source/datagen.cpp @@ -5,6 +5,13 @@ #include #include #include +#include +#include +#include +#include +#include +#include +#include #include "datagen.h" #include "movegen.h" #include "search.h" @@ -114,6 +121,51 @@ static void WriteToFile(std::vector &gamesBuffer, std::ofstream &file) { file.flush(); } +inline void MergeThreadFiles() { + namespace fs = std::filesystem; + + auto now = std::chrono::system_clock::now(); + std::time_t now_time = std::chrono::system_clock::to_time_t(now); + std::tm tm = *std::localtime(&now_time); + + std::ostringstream oss; + oss << "datagen_" + << std::put_time(&tm, "%Y-%m-%d_%H-%M") + << ".binpack"; + std::string finalFileName = oss.str(); + + std::ofstream finalFile(finalFileName, std::ios::binary); + if (!finalFile.is_open()) { + std::cerr << "Failed to create final merged file: " << finalFileName << std::endl; + return; + } + + for (const auto& entry : fs::directory_iterator(fs::current_path())) { + if (!entry.is_regular_file()) continue; + std::string filename = entry.path().filename().string(); + + if (filename.find("thread") == 0 && filename.find(".binpack") != std::string::npos) { + std::ifstream threadFile(entry.path(), std::ios::binary); + if (!threadFile.is_open()) { + std::cerr << "Failed to open " << filename << std::endl; + continue; + } + + finalFile << threadFile.rdbuf(); + threadFile.close(); + + std::error_code ec; + fs::remove(entry.path(), ec); + if (ec) { + std::cerr << "Failed to delete " << filename << ": " << ec.message() << std::endl; + } + } + } + + finalFile.close(); + std::cout << "Merged all thread files into: " << finalFileName << std::endl; +} + static void PlayGames(int id, std::atomic& positions, std::atomic& stopFlag) { std::vector gamesBuffer; gamesBuffer.reserve(GAME_BUFFER); @@ -283,6 +335,8 @@ void Run(int targetPositions, int threads) { } #endif + MergeThreadFiles(); + #ifdef _WIN32 ShowCursor(); #else diff --git a/source/datagen.h b/source/datagen.h index 208d4ff..bc88c80 100644 --- a/source/datagen.h +++ b/source/datagen.h @@ -34,25 +34,6 @@ struct MarlinFormat { this->wdl = wdl; extra = 0; } - - void writeToFile(const std::string& filename) const { - std::ofstream outFile(filename, std::ios::binary); - if (!outFile) { - std::cerr << "Error opening file for writing!" << std::endl; - return; - } - - outFile.write(reinterpret_cast(&occupancy), sizeof(occupancy)); - outFile.write(reinterpret_cast(&pieces), sizeof(pieces)); - outFile.write(reinterpret_cast(&stmEPSquare), sizeof(stmEPSquare)); - outFile.write(reinterpret_cast(&halfmoveClock), sizeof(halfmoveClock)); - outFile.write(reinterpret_cast(&fullmoveNumber), sizeof(fullmoveNumber)); - outFile.write(reinterpret_cast(&eval), sizeof(eval)); - outFile.write(reinterpret_cast(&wdl), sizeof(wdl)); - outFile.write(reinterpret_cast(&extra), sizeof(extra)); - - outFile.close(); - } }; struct ScoredMove {