Datagen fix #47
+87
-70
@@ -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
|
game.moves.push_back(ScoredMove(results.bestMove.ConvertToViriMoveFormat(), UTILS::ConvertToWhiteRelative(board, results.score)));
|
||||||
if (!shouldStop.load(std::memory_order_relaxed)) {
|
|
||||||
game.format.packFrom(board, UTILS::ConvertToWhiteRelative(board, staticEval), wdl);
|
|
||||||
|
|
||||||
// Wait until the lock is available
|
// Error checking for move making
|
||||||
while (!shouldStop.load(std::memory_order_relaxed) &&
|
if (!board.MakeMove(results.bestMove)) {
|
||||||
bufferLockFlag.exchange(true, std::memory_order_acquire)) {
|
// Invalid move - break the loop
|
||||||
// Spin-wait
|
break;
|
||||||
std::this_thread::sleep_for(std::chrono::milliseconds(1));
|
}
|
||||||
}
|
|
||||||
|
|
||||||
// One final check before writing
|
MOVEGEN::GenerateMoves<All>(board);
|
||||||
if (!shouldStop.load(std::memory_order_relaxed)) {
|
positions.fetch_add(1, std::memory_order_relaxed);
|
||||||
gamesBuffer.push_back(game);
|
|
||||||
|
|
||||||
if (gamesBuffer.size() >= GAME_BUFFER) {
|
if (std::abs(safeResults.score) >= std::abs(SEARCH::MATE_SCORE) - SEARCH::MAX_PLY) {
|
||||||
WriteToFile(gamesBuffer, file);
|
wdl = board.sideToMove ? 2 : 0;
|
||||||
gamesBuffer.clear();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Unlock the buffer after writing
|
// Only add the game to buffer if we haven't been told to stop
|
||||||
bufferLockFlag.store(false, std::memory_order_release);
|
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);
|
||||||
|
|
||||||
|
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));
|
|
||||||
}
|
|
||||||
|
|
||||||
// Signal threads to stop
|
// Final buffer flush with proper synchronization
|
||||||
shouldStop.store(true, std::memory_order_relaxed);
|
{
|
||||||
|
std::lock_guard<std::mutex> lock(bufferMutex);
|
||||||
|
WriteToFile(gamesBuffer, file);
|
||||||
|
}
|
||||||
|
|
||||||
// Wait for all threads to finish
|
} catch (const std::exception& e) {
|
||||||
for (auto& worker : workers) {
|
std::cerr << "Error in data generation: " << e.what() << std::endl;
|
||||||
if (worker.joinable()) {
|
|
||||||
worker.join();
|
// 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();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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
|
||||||
|
|||||||
Reference in new issue
Block a user