made datagen shutdown-safe | bench 4699541
This commit is contained in:
1 parent
d35eb66dbc
commit
90bdd3cc96
2 files changed
+111
-32
No files matched your search
+110
-31
@@ -12,6 +12,7 @@
|
|||||||
#include <chrono>
|
#include <chrono>
|
||||||
#include <iomanip>
|
#include <iomanip>
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
|
#include <csignal>
|
||||||
#include "datagen.h"
|
#include "datagen.h"
|
||||||
#include "movegen.h"
|
#include "movegen.h"
|
||||||
#include "search.h"
|
#include "search.h"
|
||||||
@@ -21,8 +22,10 @@
|
|||||||
// OS-dependent threading includes
|
// OS-dependent threading includes
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
#include <windows.h>
|
#include <windows.h>
|
||||||
|
#include <io.h> // for _commit
|
||||||
#else
|
#else
|
||||||
#include <pthread.h>
|
#include <pthread.h>
|
||||||
|
#include <unistd.h> // for sync
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
@@ -53,7 +56,26 @@ static void ShowCursor() {
|
|||||||
|
|
||||||
namespace DATAGEN {
|
namespace DATAGEN {
|
||||||
|
|
||||||
static std::mutex fileOpenMutex;
|
// Signal handling globals
|
||||||
|
static std::atomic<bool> g_shutdownRequested(false);
|
||||||
|
|
||||||
|
// Signal handler for graceful shutdown
|
||||||
|
static void SignalHandler(int signal) {
|
||||||
|
if (signal == SIGINT || signal == SIGTERM) {
|
||||||
|
std::cout << "\n\nShutdown signal received. Saving data safely...\n" << std::flush;
|
||||||
|
g_shutdownRequested.store(true);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Setup signal handlers
|
||||||
|
static void SetupSignalHandlers() {
|
||||||
|
std::signal(SIGINT, SignalHandler);
|
||||||
|
std::signal(SIGTERM, SignalHandler);
|
||||||
|
|
||||||
|
#ifndef _WIN32
|
||||||
|
std::signal(SIGHUP, SignalHandler);
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
static bool IsGameOver(Board &board, SEARCH::SearchContext* ctx) {
|
static bool IsGameOver(Board &board, SEARCH::SearchContext* ctx) {
|
||||||
if (SEARCH::IsDraw(board, ctx)) return true;
|
if (SEARCH::IsDraw(board, ctx)) return true;
|
||||||
@@ -94,7 +116,9 @@ static void PlayRandMoves(Board &board, SEARCH::SearchContext* ctx) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static void WriteToFile(std::vector<Game> &gamesBuffer, std::ofstream &file) {
|
static void WriteToFile(std::vector<Game> &gamesBuffer, const std::string& basePath, int& fileCounter) {
|
||||||
|
if (gamesBuffer.empty()) return;
|
||||||
|
|
||||||
int32_t zeroes = 0;
|
int32_t zeroes = 0;
|
||||||
std::vector<char> buffer;
|
std::vector<char> buffer;
|
||||||
|
|
||||||
@@ -117,19 +141,56 @@ static void WriteToFile(std::vector<Game> &gamesBuffer, std::ofstream &file) {
|
|||||||
buffer.insert(buffer.end(), zeroesPtr, zeroesPtr + sizeof(zeroes));
|
buffer.insert(buffer.end(), zeroesPtr, zeroesPtr + sizeof(zeroes));
|
||||||
}
|
}
|
||||||
|
|
||||||
file.write(buffer.data(), buffer.size());
|
// Create temporary file name
|
||||||
file.flush();
|
std::string tmpFileName = basePath + std::to_string(fileCounter) + ".tmp";
|
||||||
|
std::string finalFileName = basePath + std::to_string(fileCounter) + ".binpack";
|
||||||
|
|
||||||
|
// Write to temporary file
|
||||||
|
std::ofstream tmpFile(tmpFileName, std::ios::binary);
|
||||||
|
if (!tmpFile.is_open()) {
|
||||||
|
std::cerr << "Failed to create temporary file: " << tmpFileName << std::endl;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
tmpFile.write(buffer.data(), buffer.size());
|
||||||
|
tmpFile.flush();
|
||||||
|
|
||||||
|
// Ensure data is written to disk (OS-dependent)
|
||||||
|
#ifdef _WIN32
|
||||||
|
// Windows: flush and close
|
||||||
|
tmpFile.close();
|
||||||
|
#else
|
||||||
|
// Linux: sync before closing
|
||||||
|
tmpFile.close();
|
||||||
|
sync();
|
||||||
|
#endif
|
||||||
|
|
||||||
|
// Atomically rename temp file to final file
|
||||||
|
std::error_code ec;
|
||||||
|
std::filesystem::rename(tmpFileName, finalFileName, ec);
|
||||||
|
if (ec) {
|
||||||
|
std::cerr << "Failed to rename " << tmpFileName << " to " << finalFileName
|
||||||
|
<< ": " << ec.message() << std::endl;
|
||||||
|
// Try to clean up the temp file
|
||||||
|
std::filesystem::remove(tmpFileName, ec);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
fileCounter++;
|
||||||
}
|
}
|
||||||
|
|
||||||
inline void MergeThreadFiles() {
|
inline void MergeThreadFiles() {
|
||||||
namespace fs = std::filesystem;
|
namespace fs = std::filesystem;
|
||||||
|
|
||||||
|
// Create data directory if it doesn't exist
|
||||||
|
fs::create_directories("data");
|
||||||
|
|
||||||
auto now = std::chrono::system_clock::now();
|
auto now = std::chrono::system_clock::now();
|
||||||
std::time_t now_time = std::chrono::system_clock::to_time_t(now);
|
std::time_t now_time = std::chrono::system_clock::to_time_t(now);
|
||||||
std::tm tm = *std::localtime(&now_time);
|
std::tm tm = *std::localtime(&now_time);
|
||||||
|
|
||||||
std::ostringstream oss;
|
std::ostringstream oss;
|
||||||
oss << "datagen_"
|
oss << "data/datagen_"
|
||||||
<< std::put_time(&tm, "%Y-%m-%d_%H-%M")
|
<< std::put_time(&tm, "%Y-%m-%d_%H-%M")
|
||||||
<< ".binpack";
|
<< ".binpack";
|
||||||
std::string finalFileName = oss.str();
|
std::string finalFileName = oss.str();
|
||||||
@@ -140,10 +201,12 @@ inline void MergeThreadFiles() {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
for (const auto& entry : fs::directory_iterator(fs::current_path())) {
|
fs::path dataDir("data");
|
||||||
|
for (const auto& entry : fs::directory_iterator(dataDir)) {
|
||||||
if (!entry.is_regular_file()) continue;
|
if (!entry.is_regular_file()) continue;
|
||||||
std::string filename = entry.path().filename().string();
|
std::string filename = entry.path().filename().string();
|
||||||
|
|
||||||
|
// Only process .binpack files (not .tmp files)
|
||||||
if (filename.find("thread") == 0 && filename.find(".binpack") != std::string::npos) {
|
if (filename.find("thread") == 0 && filename.find(".binpack") != std::string::npos) {
|
||||||
std::ifstream threadFile(entry.path(), std::ios::binary);
|
std::ifstream threadFile(entry.path(), std::ios::binary);
|
||||||
if (!threadFile.is_open()) {
|
if (!threadFile.is_open()) {
|
||||||
@@ -162,6 +225,20 @@ inline void MergeThreadFiles() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Clean up any remaining .tmp files
|
||||||
|
for (const auto& entry : fs::directory_iterator(dataDir)) {
|
||||||
|
if (!entry.is_regular_file()) continue;
|
||||||
|
std::string filename = entry.path().filename().string();
|
||||||
|
|
||||||
|
if (filename.find("thread") == 0 && filename.find(".tmp") != std::string::npos) {
|
||||||
|
std::error_code ec;
|
||||||
|
fs::remove(entry.path(), ec);
|
||||||
|
if (ec) {
|
||||||
|
std::cerr << "Failed to delete incomplete temp file " << filename << ": " << ec.message() << std::endl;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
finalFile.close();
|
finalFile.close();
|
||||||
std::cout << "Merged all thread files into: " << finalFileName << std::endl;
|
std::cout << "Merged all thread files into: " << finalFileName << std::endl;
|
||||||
}
|
}
|
||||||
@@ -170,31 +247,16 @@ static void PlayGames(int id, std::atomic<int>& positions, std::atomic<bool>& st
|
|||||||
std::vector<Game> gamesBuffer;
|
std::vector<Game> gamesBuffer;
|
||||||
gamesBuffer.reserve(GAME_BUFFER);
|
gamesBuffer.reserve(GAME_BUFFER);
|
||||||
|
|
||||||
std::string fileName = "thread" + std::to_string(id) + ".binpack";
|
// Create data directory if it doesn't exist
|
||||||
|
std::filesystem::create_directories("data");
|
||||||
|
|
||||||
std::ofstream file;
|
std::string basePath = "data/thread" + std::to_string(id) + "_";
|
||||||
{
|
int fileCounter = 0;
|
||||||
std::lock_guard<std::mutex> lock(fileOpenMutex);
|
|
||||||
file.open(fileName, std::ios::app | std::ios::binary);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!file.is_open()) {
|
|
||||||
std::cerr << "Failed to open file: " << fileName << std::endl;
|
|
||||||
exit(-1);
|
|
||||||
}
|
|
||||||
|
|
||||||
const int evenityMargin = 200;
|
const int evenityMargin = 200;
|
||||||
|
|
||||||
/*
|
try {
|
||||||
const int winAdjMoveCount = 3;
|
while (!stopFlag && !g_shutdownRequested) {
|
||||||
const int winAdjScore = 400;
|
|
||||||
|
|
||||||
const int drawAdjMoveNumber = 40;
|
|
||||||
const int drawAdjMoveCount = 8;
|
|
||||||
const int drawAdjScore = 10;
|
|
||||||
*/
|
|
||||||
|
|
||||||
while (!stopFlag) {
|
|
||||||
Board board;
|
Board board;
|
||||||
auto ctx = std::make_unique<SEARCH::SearchContext>();
|
auto ctx = std::make_unique<SEARCH::SearchContext>();
|
||||||
|
|
||||||
@@ -217,6 +279,9 @@ static void PlayGames(int id, std::atomic<int>& positions, std::atomic<bool>& st
|
|||||||
int wdl = 1;
|
int wdl = 1;
|
||||||
|
|
||||||
while (!IsGameOver(board, ctx.get())) {
|
while (!IsGameOver(board, ctx.get())) {
|
||||||
|
// Check for shutdown during game generation
|
||||||
|
if (g_shutdownRequested) break;
|
||||||
|
|
||||||
SearchResults results = SEARCH::SearchPosition<SEARCH::datagen>(board, SearchParams(), ctx.get());
|
SearchResults results = SEARCH::SearchPosition<SEARCH::datagen>(board, SearchParams(), ctx.get());
|
||||||
|
|
||||||
if (!results.bestMove) break;
|
if (!results.bestMove) break;
|
||||||
@@ -230,7 +295,6 @@ static void PlayGames(int id, std::atomic<int>& positions, std::atomic<bool>& st
|
|||||||
|
|
||||||
positions++;
|
positions++;
|
||||||
|
|
||||||
|
|
||||||
MOVEGEN::GenerateMoves<All>(board, true);
|
MOVEGEN::GenerateMoves<All>(board, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -242,14 +306,21 @@ static void PlayGames(int id, std::atomic<int>& positions, std::atomic<bool>& st
|
|||||||
gamesBuffer.emplace_back(game);
|
gamesBuffer.emplace_back(game);
|
||||||
|
|
||||||
if (gamesBuffer.size() >= GAME_BUFFER) {
|
if (gamesBuffer.size() >= GAME_BUFFER) {
|
||||||
WriteToFile(gamesBuffer, file);
|
WriteToFile(gamesBuffer, basePath, fileCounter);
|
||||||
gamesBuffer.clear();
|
gamesBuffer.clear();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
} catch (...) {
|
||||||
|
// Ensure we flush on any exception
|
||||||
|
if (!gamesBuffer.empty()) {
|
||||||
|
WriteToFile(gamesBuffer, basePath, fileCounter);
|
||||||
|
}
|
||||||
|
throw;
|
||||||
|
}
|
||||||
|
|
||||||
// Write remaining games to file
|
// Write remaining games to file
|
||||||
if (gamesBuffer.size() > 0) {
|
if (gamesBuffer.size() > 0) {
|
||||||
WriteToFile(gamesBuffer, file);
|
WriteToFile(gamesBuffer, basePath, fileCounter);
|
||||||
gamesBuffer.clear();
|
gamesBuffer.clear();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -270,6 +341,9 @@ static void* ThreadFunc(void* arg) {
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
void Run(int targetPositions, int threads) {
|
void Run(int targetPositions, int threads) {
|
||||||
|
// Setup signal handlers first
|
||||||
|
SetupSignalHandlers();
|
||||||
|
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
system("cls");
|
system("cls");
|
||||||
HideCursor();
|
HideCursor();
|
||||||
@@ -312,7 +386,7 @@ void Run(int targetPositions, int threads) {
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
Stopwatch sw;
|
Stopwatch sw;
|
||||||
while (positions < targetPositions) {
|
while (positions < targetPositions && !g_shutdownRequested) {
|
||||||
PrintProgress(positions, targetPositions, sw, threads);
|
PrintProgress(positions, targetPositions, sw, threads);
|
||||||
std::this_thread::sleep_for(std::chrono::seconds(1));
|
std::this_thread::sleep_for(std::chrono::seconds(1));
|
||||||
}
|
}
|
||||||
@@ -321,6 +395,8 @@ void Run(int targetPositions, int threads) {
|
|||||||
|
|
||||||
stopFlag = true;
|
stopFlag = true;
|
||||||
|
|
||||||
|
std::cout << "\n\nWaiting for threads to finish writing..." << std::endl;
|
||||||
|
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
// Join Windows threads
|
// Join Windows threads
|
||||||
for (std::thread& thread : workerThreads) {
|
for (std::thread& thread : workerThreads) {
|
||||||
@@ -335,6 +411,7 @@ void Run(int targetPositions, int threads) {
|
|||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
std::cout << "All threads completed. Merging files..." << std::endl;
|
||||||
MergeThreadFiles();
|
MergeThreadFiles();
|
||||||
|
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
@@ -342,6 +419,8 @@ void Run(int targetPositions, int threads) {
|
|||||||
#else
|
#else
|
||||||
ShowCursor();
|
ShowCursor();
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
std::cout << "\nShutdown complete. All data saved safely." << std::endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
void PrintProgress(int positions, int targetPositions, Stopwatch &stopwatch, int threads) {
|
void PrintProgress(int positions, int targetPositions, Stopwatch &stopwatch, int threads) {
|
||||||
|
|||||||
+1
-1
@@ -6,7 +6,7 @@
|
|||||||
|
|
||||||
namespace DATAGEN {
|
namespace DATAGEN {
|
||||||
|
|
||||||
constexpr int GAME_BUFFER = 1000;
|
constexpr int GAME_BUFFER = 500;
|
||||||
constexpr int SOFT_NODES = 5000;
|
constexpr int SOFT_NODES = 5000;
|
||||||
constexpr int HARD_NODES = 100000;
|
constexpr int HARD_NODES = 100000;
|
||||||
constexpr int RAND_MOVES = 8;
|
constexpr int RAND_MOVES = 8;
|
||||||
|
|||||||
Reference in new issue
Block a user