Datagen rewrite #55

Merged
rektdie merged 3 commits from datagen-rewrite into main 2025-04-15 18:56:07 +02:00
2 changed files with 44 additions and 90 deletions
Showing only changes of commit a608b357ca - Show all commits

No files matched your search

+43 -89
View File
@@ -5,7 +5,6 @@
#include <thread> #include <thread>
#include <atomic> #include <atomic>
#include <mutex> #include <mutex>
#include <condition_variable>
#include "datagen.h" #include "datagen.h"
#include "movegen.h" #include "movegen.h"
#include "evaluate.h" #include "evaluate.h"
@@ -44,9 +43,7 @@ static void ShowCursorLinux() {
namespace DATAGEN { namespace DATAGEN {
std::mutex bufferMutex; static void PlayRandMoves(Board &board, SEARCH::SearchContext& ctx) {
static void PlayRandMoves(Board &board) {
std::random_device dev; std::random_device dev;
std::mt19937 rng(dev()); std::mt19937 rng(dev());
@@ -57,6 +54,7 @@ static void PlayRandMoves(Board &board) {
bool isLegal = board.MakeMove(board.moveList[dist(rng)]); bool isLegal = board.MakeMove(board.moveList[dist(rng)]);
if (!isLegal) count--; if (!isLegal) count--;
ctx.positionHistory[board.positionIndex] = board.hashKey;
} }
MOVEGEN::GenerateMoves<All>(board); MOVEGEN::GenerateMoves<All>(board);
@@ -72,8 +70,7 @@ static bool IsGameOver(Board &board, SEARCH::SearchContext& ctx) {
bool isLegal = copy.MakeMove(board.moveList[i]); bool isLegal = copy.MakeMove(board.moveList[i]);
if (!isLegal) continue; if (!isLegal) continue;
moveSeen++; moveSeen++; }
}
if (moveSeen == 0) return true; if (moveSeen == 0) return true;
return false; return false;
@@ -89,56 +86,48 @@ static void WriteToFile(std::vector<Game> &gamesBuffer, std::ofstream &file) {
file.flush(); file.flush();
} }
void PlayGame(std::atomic<int>& positions, U64 targetPositions, std::vector<Game> &gamesBuffer, static void PlayGames(int id, std::atomic<int>& positions, std::atomic<bool>& stopFlag) {
std::ofstream &file, std::atomic<bool>& shouldStop) { std::vector<Game> gamesBuffer;
while (!shouldStop.load(std::memory_order_relaxed) && std::string fileName = "thread" + std::to_string(id) + ".binpack";
positions.load(std::memory_order_relaxed) < targetPositions) {
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; Game game;
Board board; Board board;
uint8_t wdl = 1;
SEARCH::SearchContext ctx; SEARCH::SearchContext ctx;
PlayRandMoves(board, ctx);
try {
PlayRandMoves(board);
int staticEval = HCE::Evaluate(board);
SearchResults safeResults; SearchResults safeResults;
MOVEGEN::GenerateMoves<All>(board);
int staticEval = UTILS::ConvertToWhiteRelative(board, HCE::Evaluate(board));
int wdl = 1;
while (!IsGameOver(board, ctx)) { while (!IsGameOver(board, ctx)) {
SearchResults results = SEARCH::SearchPosition<SEARCH::datagen>(board, SearchParams(), ctx); SearchResults results = SEARCH::SearchPosition<SEARCH::datagen>(board, SearchParams(), ctx);
results = SEARCH::SearchPosition<SEARCH::datagen>(board, SearchParams(), ctx);
if (!results.bestMove) break; if (!results.bestMove) break;
safeResults = results; 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))); game.moves.push_back(ScoredMove(results.bestMove.ConvertToViriMoveFormat(),
UTILS::ConvertToWhiteRelative(board, results.score)));
// Error checking for move making positions++;
if (!board.MakeMove(results.bestMove)) {
// Invalid move - break the loop
break;
}
MOVEGEN::GenerateMoves<All>(board); MOVEGEN::GenerateMoves<All>(board);
positions.fetch_add(1, std::memory_order_relaxed); }
if (std::abs(safeResults.score) >= std::abs(SEARCH::MATE_SCORE) - SEARCH::MAX_PLY) { if (std::abs(safeResults.score) >= std::abs(SEARCH::MATE_SCORE) - SEARCH::MAX_PLY) {
wdl = board.sideToMove ? 2 : 0; wdl = board.sideToMove ? 2 : 0;
} }
}
// Only add the game to buffer if we haven't been told to stop game.format.packFrom(board, staticEval, wdl);
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<std::mutex> lock(bufferMutex);
// Check again after acquiring the lock
if (!shouldStop.load(std::memory_order_relaxed)) {
gamesBuffer.push_back(game); gamesBuffer.push_back(game);
if (gamesBuffer.size() >= GAME_BUFFER) { if (gamesBuffer.size() >= GAME_BUFFER) {
@@ -146,12 +135,11 @@ void PlayGame(std::atomic<int>& positions, U64 targetPositions, std::vector<Game
gamesBuffer.clear(); gamesBuffer.clear();
} }
} }
}
} // Write remaining games to file
} catch (const std::exception& e) { if (gamesBuffer.size() > 0) {
// Log exception but allow thread to continue WriteToFile(gamesBuffer, file);
std::cerr << "Thread exception: " << e.what() << std::endl; gamesBuffer.clear();
}
} }
} }
@@ -164,64 +152,30 @@ void Run(int targetPositions, int threads) {
HideCursorLinux(); HideCursorLinux();
#endif #endif
Stopwatch stopwatch;
std::atomic<int> positions = 0; std::atomic<int> positions = 0;
std::atomic<bool> shouldStop(false); std::atomic<bool> stopFlag = false;
std::vector<Game> gamesBuffer;
std::vector<std::thread> workers;
try { std::vector<std::thread> workerThreads;
std::ofstream file("data.binpack", std::ios::app | std::ios::binary);
if (!file.is_open()) {
throw std::runtime_error("Failed to open output file");
}
// Create and start worker threads
for (int i = 0; i < threads; i++) { for (int i = 0; i < threads; i++) {
workers.push_back(std::thread(PlayGame, std::ref(positions), workerThreads.push_back(std::thread(PlayGames, i, std::ref(positions), std::ref(stopFlag)));
targetPositions * 1000, std::ref(gamesBuffer),
std::ref(file), std::ref(shouldStop)));
} }
// Monitor progress Stopwatch sw;
while (positions.load(std::memory_order_relaxed) < targetPositions * 1000) { while (positions < targetPositions) {
PrintProgress(positions, targetPositions * 1000, stopwatch, threads); PrintProgress(positions, targetPositions, sw, threads);
std::this_thread::sleep_for(std::chrono::milliseconds(1000)); std::this_thread::sleep_for(std::chrono::seconds(1));
} }
// Signal threads to stop
shouldStop.store(true, std::memory_order_relaxed);
// Wait for all threads to finish PrintProgress(positions, targetPositions, sw, threads);
for (auto& worker : workers) {
if (worker.joinable()) { stopFlag = true;
worker.join();
} for (auto& thread : workerThreads) {
thread.join();
} }
// Final progress update and buffer flush
PrintProgress(positions, targetPositions * 1000, stopwatch, threads);
// Final buffer flush with proper synchronization
{
std::lock_guard<std::mutex> 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();
}
}
}
#ifdef _WIN32 #ifdef _WIN32
ShowCursor(); ShowCursor();
+1 -1
View File
@@ -57,7 +57,7 @@ int main(int argc, char* argv[]) {
int positions = 1; int positions = 1;
int threads = 1; int threads = 1;
if (argc > 2) { if (argc > 2) {
positions = std::stoi(argv[2]); positions = std::stoi(argv[2]) * 1000;
if (argc > 3) { if (argc > 3) {
threads = std::stoi(argv[3]); threads = std::stoi(argv[3]);