Datagen fix #47

Merged
rektdie merged 3 commits from datagen_fix into main 2025-04-08 01:08:10 +02:00
2 changed files with 92 additions and 75 deletions
Showing only changes of commit ea81850a3d - Show all commits

No files matched your search

+91 -74
View File
@@ -5,6 +5,7 @@
#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"
@@ -43,8 +44,7 @@ static void ShowCursorLinux() {
namespace DATAGEN { namespace DATAGEN {
// Atomic bool for locking std::mutex bufferMutex;
std::atomic<bool> bufferLockFlag(false);
static void PlayRandMoves(Board &board) { static void PlayRandMoves(Board &board) {
std::random_device dev; std::random_device dev;
@@ -99,58 +99,62 @@ void PlayGame(std::atomic<int>& positions, U64 targetPositions, std::vector<Game
Board board; Board board;
uint8_t wdl = 1; uint8_t wdl = 1;
// Since positionIndex is thread-local, no need to protect it
positionIndex = 0; positionIndex = 0;
std::memset(SEARCH::killerMoves, 0, sizeof(SEARCH::killerMoves)); std::memset(SEARCH::killerMoves, 0, sizeof(SEARCH::killerMoves));
SEARCH::history.Clear(); SEARCH::history.Clear();
PlayRandMoves(board); try {
PlayRandMoves(board);
int staticEval = HCE::Evaluate(board); int staticEval = HCE::Evaluate(board);
SEARCH::SearchResults safeResults;
SEARCH::SearchResults safeResults;
MOVEGEN::GenerateMoves<All>(board);
while (!IsGameOver(board)) {
SEARCH::SearchResults results = SEARCH::SearchPosition<SEARCH::datagen>(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<All>(board); MOVEGEN::GenerateMoves<All>(board);
positions.fetch_add(1, std::memory_order_relaxed); while (!IsGameOver(board)) {
SEARCH::SearchResults results = SEARCH::SearchPosition<SEARCH::datagen>(board, SearchParams());
if (std::abs(safeResults.score) >= std::abs(SEARCH::MATE_SCORE) - SEARCH::MAX_PLY) { if (!results.bestMove) break;
wdl = board.sideToMove ? 2 : 0; safeResults = results;
}
}
// 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);
// Wait until the lock is available game.moves.push_back(ScoredMove(results.bestMove.ConvertToViriMoveFormat(), UTILS::ConvertToWhiteRelative(board, results.score)));
while (!shouldStop.load(std::memory_order_relaxed) &&
bufferLockFlag.exchange(true, std::memory_order_acquire)) { // Error checking for move making
// Spin-wait if (!board.MakeMove(results.bestMove)) {
std::this_thread::sleep_for(std::chrono::milliseconds(1)); // Invalid move - break the loop
} break;
}
MOVEGEN::GenerateMoves<All>(board);
positions.fetch_add(1, std::memory_order_relaxed);
// One final check before writing if (std::abs(safeResults.score) >= std::abs(SEARCH::MATE_SCORE) - SEARCH::MAX_PLY) {
if (!shouldStop.load(std::memory_order_relaxed)) { wdl = board.sideToMove ? 2 : 0;
gamesBuffer.push_back(game);
if (gamesBuffer.size() >= GAME_BUFFER) {
WriteToFile(gamesBuffer, file);
gamesBuffer.clear();
} }
} }
// 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);
// Unlock the buffer after writing // Use a proper lock instead of spin-waiting
bufferLockFlag.store(false, std::memory_order_release); {
std::lock_guard<std::mutex> 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;
} }
} }
} }
@@ -171,45 +175,58 @@ void Run(int targetPositions, int threads) {
std::vector<Game> gamesBuffer; std::vector<Game> gamesBuffer;
std::vector<std::thread> workers; std::vector<std::thread> workers;
std::ofstream file("data.binpack", std::ios::app | std::ios::binary); 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");
}
// Create and start worker threads // 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), workers.push_back(std::thread(PlayGame, std::ref(positions),
targetPositions * 1000, std::ref(gamesBuffer), targetPositions * 1000, std::ref(gamesBuffer),
std::ref(file), std::ref(shouldStop))); std::ref(file), std::ref(shouldStop)));
} }
int lastPrintedPosition = 0; // Monitor progress
double lastPrintedTime = stopwatch.GetElapsedSec(); while (positions.load(std::memory_order_relaxed) < targetPositions * 1000) {
PrintProgress(positions, targetPositions * 1000, stopwatch, threads);
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
}
// Monitor progress // Signal threads to stop
while (positions.load(std::memory_order_relaxed) < targetPositions * 1000) { 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); PrintProgress(positions, targetPositions * 1000, stopwatch, threads);
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
} // Final buffer flush with proper synchronization
{
std::lock_guard<std::mutex> lock(bufferMutex);
WriteToFile(gamesBuffer, file);
}
// Signal threads to stop } catch (const std::exception& e) {
shouldStop.store(true, std::memory_order_relaxed); std::cerr << "Error in data generation: " << e.what() << std::endl;
// Wait for all threads to finish // Signal threads to stop in case of error
for (auto& worker : workers) { shouldStop.store(true, std::memory_order_relaxed);
if (worker.joinable()) {
worker.join(); // Make sure to join any running threads
for (auto& worker : workers) {
if (worker.joinable()) {
worker.join();
}
} }
} }
// Final progress update and buffer flush
PrintProgress(positions, targetPositions * 1000, stopwatch, threads);
// Final check - lock buffer to ensure exclusive access
while (bufferLockFlag.exchange(true, std::memory_order_acquire)) {
std::this_thread::sleep_for(std::chrono::milliseconds(1));
}
WriteToFile(gamesBuffer, file);
bufferLockFlag.store(false, std::memory_order_release);
#ifdef _WIN32 #ifdef _WIN32
ShowCursor(); ShowCursor();
#else #else
@@ -298,4 +315,4 @@ void PrintProgress(int positions, int targetPositions, Stopwatch &stopwatch, int
std::cout << std::endl; std::cout << std::endl;
} }
} }
+1 -1
View File
@@ -82,4 +82,4 @@ struct Game {
void Run(int targetPositions, int threads); void Run(int targetPositions, int threads);
void PrintProgress(int positions, int targetPositions, Stopwatch &stopwatch, int threads); void PrintProgress(int positions, int targetPositions, Stopwatch &stopwatch, int threads);
} }