better thread handling
This commit is contained in:
1 parent
52df753eff
commit
ea81850a3d
1 file changed
+42
-25
+42
-25
@@ -5,6 +5,7 @@
|
||||
#include <thread>
|
||||
#include <atomic>
|
||||
#include <mutex>
|
||||
#include <condition_variable>
|
||||
#include "datagen.h"
|
||||
#include "movegen.h"
|
||||
#include "evaluate.h"
|
||||
@@ -43,8 +44,7 @@ static void ShowCursorLinux() {
|
||||
|
||||
namespace DATAGEN {
|
||||
|
||||
// Atomic bool for locking
|
||||
std::atomic<bool> bufferLockFlag(false);
|
||||
std::mutex bufferMutex;
|
||||
|
||||
static void PlayRandMoves(Board &board) {
|
||||
std::random_device dev;
|
||||
@@ -99,16 +99,16 @@ void PlayGame(std::atomic<int>& positions, U64 targetPositions, std::vector<Game
|
||||
Board board;
|
||||
|
||||
uint8_t wdl = 1;
|
||||
// Since positionIndex is thread-local, no need to protect it
|
||||
positionIndex = 0;
|
||||
|
||||
std::memset(SEARCH::killerMoves, 0, sizeof(SEARCH::killerMoves));
|
||||
SEARCH::history.Clear();
|
||||
|
||||
try {
|
||||
PlayRandMoves(board);
|
||||
|
||||
int staticEval = HCE::Evaluate(board);
|
||||
|
||||
SEARCH::SearchResults safeResults;
|
||||
|
||||
MOVEGEN::GenerateMoves<All>(board);
|
||||
|
||||
while (!IsGameOver(board)) {
|
||||
@@ -118,9 +118,14 @@ void PlayGame(std::atomic<int>& positions, U64 targetPositions, std::vector<Game
|
||||
safeResults = results;
|
||||
|
||||
game.moves.push_back(ScoredMove(results.bestMove.ConvertToViriMoveFormat(), UTILS::ConvertToWhiteRelative(board, results.score)));
|
||||
board.MakeMove(results.bestMove);
|
||||
MOVEGEN::GenerateMoves<All>(board);
|
||||
|
||||
// Error checking for move making
|
||||
if (!board.MakeMove(results.bestMove)) {
|
||||
// Invalid move - break the loop
|
||||
break;
|
||||
}
|
||||
|
||||
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) {
|
||||
@@ -132,14 +137,11 @@ void PlayGame(std::atomic<int>& positions, U64 targetPositions, std::vector<Game
|
||||
if (!shouldStop.load(std::memory_order_relaxed)) {
|
||||
game.format.packFrom(board, UTILS::ConvertToWhiteRelative(board, staticEval), wdl);
|
||||
|
||||
// Wait until the lock is available
|
||||
while (!shouldStop.load(std::memory_order_relaxed) &&
|
||||
bufferLockFlag.exchange(true, std::memory_order_acquire)) {
|
||||
// Spin-wait
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(1));
|
||||
}
|
||||
// Use a proper lock instead of spin-waiting
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(bufferMutex);
|
||||
|
||||
// One final check before writing
|
||||
// Check again after acquiring the lock
|
||||
if (!shouldStop.load(std::memory_order_relaxed)) {
|
||||
gamesBuffer.push_back(game);
|
||||
|
||||
@@ -148,9 +150,11 @@ void PlayGame(std::atomic<int>& positions, U64 targetPositions, std::vector<Game
|
||||
gamesBuffer.clear();
|
||||
}
|
||||
}
|
||||
|
||||
// Unlock the buffer after writing
|
||||
bufferLockFlag.store(false, std::memory_order_release);
|
||||
}
|
||||
}
|
||||
} catch (const std::exception& e) {
|
||||
// Log exception but allow thread to continue
|
||||
std::cerr << "Thread exception: " << e.what() << std::endl;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -171,7 +175,11 @@ void Run(int targetPositions, int threads) {
|
||||
std::vector<Game> gamesBuffer;
|
||||
std::vector<std::thread> workers;
|
||||
|
||||
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
|
||||
for (int i = 0; i < threads; i++) {
|
||||
@@ -180,9 +188,6 @@ void Run(int targetPositions, int threads) {
|
||||
std::ref(file), std::ref(shouldStop)));
|
||||
}
|
||||
|
||||
int lastPrintedPosition = 0;
|
||||
double lastPrintedTime = stopwatch.GetElapsedSec();
|
||||
|
||||
// Monitor progress
|
||||
while (positions.load(std::memory_order_relaxed) < targetPositions * 1000) {
|
||||
PrintProgress(positions, targetPositions * 1000, stopwatch, threads);
|
||||
@@ -202,13 +207,25 @@ void Run(int targetPositions, int threads) {
|
||||
// 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));
|
||||
// Final buffer flush with proper synchronization
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(bufferMutex);
|
||||
WriteToFile(gamesBuffer, file);
|
||||
}
|
||||
|
||||
WriteToFile(gamesBuffer, file);
|
||||
bufferLockFlag.store(false, std::memory_order_release);
|
||||
} 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
|
||||
ShowCursor();
|
||||
|
||||
Reference in new issue
Block a user