Merge pull request #55 from rektdie/datagen-rewrite
Datagen rewrite bench 1422551
This commit was merged in pull request #55.
This commit is contained in:
4 files changed
+113
-138
No files matched your search
+93
-130
@@ -4,8 +4,6 @@
|
|||||||
#include <iomanip>
|
#include <iomanip>
|
||||||
#include <thread>
|
#include <thread>
|
||||||
#include <atomic>
|
#include <atomic>
|
||||||
#include <mutex>
|
|
||||||
#include <condition_variable>
|
|
||||||
#include "datagen.h"
|
#include "datagen.h"
|
||||||
#include "movegen.h"
|
#include "movegen.h"
|
||||||
#include "evaluate.h"
|
#include "evaluate.h"
|
||||||
@@ -44,24 +42,6 @@ static void ShowCursorLinux() {
|
|||||||
|
|
||||||
namespace DATAGEN {
|
namespace DATAGEN {
|
||||||
|
|
||||||
std::mutex bufferMutex;
|
|
||||||
|
|
||||||
static void PlayRandMoves(Board &board) {
|
|
||||||
std::random_device dev;
|
|
||||||
std::mt19937 rng(dev());
|
|
||||||
|
|
||||||
for (int count = 0; count < RAND_MOVES; count++) {
|
|
||||||
MOVEGEN::GenerateMoves<All>(board);
|
|
||||||
std::uniform_int_distribution<std::mt19937::result_type> dist(0, board.currentMoveIndex - 1);
|
|
||||||
|
|
||||||
bool isLegal = board.MakeMove(board.moveList[dist(rng)]);
|
|
||||||
|
|
||||||
if (!isLegal) count--;
|
|
||||||
}
|
|
||||||
|
|
||||||
MOVEGEN::GenerateMoves<All>(board);
|
|
||||||
}
|
|
||||||
|
|
||||||
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;
|
||||||
|
|
||||||
@@ -72,11 +52,30 @@ 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;
|
return moveSeen == 0;
|
||||||
return false;
|
}
|
||||||
|
|
||||||
|
static void PlayRandMoves(Board &board, SEARCH::SearchContext& ctx) {
|
||||||
|
std::random_device dev;
|
||||||
|
std::mt19937_64 rng(dev());
|
||||||
|
|
||||||
|
for (int count = 0; count < RAND_MOVES; count++) {
|
||||||
|
MOVEGEN::GenerateMoves<All>(board);
|
||||||
|
std::uniform_int_distribution<int> dist(0, board.currentMoveIndex - 1);
|
||||||
|
|
||||||
|
bool isLegal = board.MakeMove(board.moveList[dist(rng)]);
|
||||||
|
|
||||||
|
if (!isLegal) {
|
||||||
|
count--;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
ctx.positionHistory[board.positionIndex] = board.hashKey;
|
||||||
|
MOVEGEN::GenerateMoves<All>(board);
|
||||||
|
if (IsGameOver(board, ctx)) break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static void WriteToFile(std::vector<Game> &gamesBuffer, std::ofstream &file) {
|
static void WriteToFile(std::vector<Game> &gamesBuffer, std::ofstream &file) {
|
||||||
@@ -89,69 +88,64 @@ 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) &&
|
gamesBuffer.reserve(GAME_BUFFER);
|
||||||
positions.load(std::memory_order_relaxed) < targetPositions) {
|
|
||||||
|
std::string fileName = "thread" + std::to_string(id) + ".binpack";
|
||||||
|
|
||||||
|
|
||||||
|
std::ofstream file(fileName, std::ios::app | std::ios::binary);
|
||||||
|
if (!file.is_open()) {
|
||||||
|
std::cerr << "Failed to open file: " << fileName << std::endl;
|
||||||
|
exit(-1);
|
||||||
|
}
|
||||||
|
|
||||||
|
while (!stopFlag) {
|
||||||
Game game;
|
Game game;
|
||||||
Board board;
|
Board board;
|
||||||
|
|
||||||
uint8_t wdl = 1;
|
|
||||||
|
|
||||||
SEARCH::SearchContext ctx;
|
SEARCH::SearchContext ctx;
|
||||||
|
PlayRandMoves(board, ctx);
|
||||||
|
if (IsGameOver(board, ctx)) continue;
|
||||||
|
|
||||||
|
SearchResults safeResults;
|
||||||
|
|
||||||
|
int staticEval = UTILS::ConvertToWhiteRelative(board, HCE::Evaluate(board));
|
||||||
|
int wdl = 1;
|
||||||
|
|
||||||
|
while (!IsGameOver(board, ctx)) {
|
||||||
|
SearchResults results = SEARCH::SearchPosition<SEARCH::datagen>(board, SearchParams(), ctx);
|
||||||
|
|
||||||
|
if (!results.bestMove) break;
|
||||||
|
safeResults = results;
|
||||||
|
board.MakeMove(results.bestMove);
|
||||||
|
ctx.positionHistory[board.positionIndex] = board.hashKey;
|
||||||
|
|
||||||
|
game.moves.emplace_back(ScoredMove(results.bestMove.ConvertToViriMoveFormat(),
|
||||||
|
UTILS::ConvertToWhiteRelative(board, results.score)));
|
||||||
|
|
||||||
|
positions++;
|
||||||
|
|
||||||
try {
|
|
||||||
PlayRandMoves(board);
|
|
||||||
int staticEval = HCE::Evaluate(board);
|
|
||||||
SearchResults safeResults;
|
|
||||||
MOVEGEN::GenerateMoves<All>(board);
|
MOVEGEN::GenerateMoves<All>(board);
|
||||||
|
|
||||||
while (!IsGameOver(board, ctx)) {
|
|
||||||
SearchResults results = SEARCH::SearchPosition<SEARCH::datagen>(board, SearchParams(), ctx);
|
|
||||||
results = SEARCH::SearchPosition<SEARCH::datagen>(board, SearchParams(), ctx);
|
|
||||||
|
|
||||||
if (!results.bestMove) break;
|
|
||||||
safeResults = results;
|
|
||||||
|
|
||||||
game.moves.push_back(ScoredMove(results.bestMove.ConvertToViriMoveFormat(), UTILS::ConvertToWhiteRelative(board, results.score)));
|
|
||||||
|
|
||||||
// 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) {
|
|
||||||
wdl = board.sideToMove ? 2 : 0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// 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);
|
|
||||||
|
|
||||||
// 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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (std::abs(safeResults.score) >= std::abs(SEARCH::MATE_SCORE) - SEARCH::MAX_PLY) {
|
||||||
|
wdl = board.sideToMove ? 2 : 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
game.format.packFrom(board, staticEval, wdl);
|
||||||
|
gamesBuffer.emplace_back(game);
|
||||||
|
|
||||||
|
if (gamesBuffer.size() >= GAME_BUFFER) {
|
||||||
|
WriteToFile(gamesBuffer, file);
|
||||||
|
gamesBuffer.clear();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Write remaining games to file
|
||||||
|
if (gamesBuffer.size() > 0) {
|
||||||
|
WriteToFile(gamesBuffer, file);
|
||||||
|
gamesBuffer.clear();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -164,66 +158,35 @@ 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
|
// Start N-1 threads
|
||||||
for (int i = 0; i < threads; i++) {
|
for (int i = 0; i < threads-1; i++) {
|
||||||
workers.push_back(std::thread(PlayGame, std::ref(positions),
|
workerThreads.emplace_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
|
|
||||||
for (auto& worker : workers) {
|
|
||||||
if (worker.joinable()) {
|
|
||||||
worker.join();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Final progress update and buffer flush
|
PrintProgress(positions, targetPositions, sw, threads);
|
||||||
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) {
|
stopFlag = true;
|
||||||
std::cerr << "Error in data generation: " << e.what() << std::endl;
|
|
||||||
|
for (auto& thread : workerThreads) {
|
||||||
// Signal threads to stop in case of error
|
if (thread.joinable()) {
|
||||||
shouldStop.store(true, std::memory_order_relaxed);
|
thread.join();
|
||||||
|
|
||||||
// Make sure to join any running threads
|
|
||||||
for (auto& worker : workers) {
|
|
||||||
if (worker.joinable()) {
|
|
||||||
worker.join();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifdef _WIN32
|
|
||||||
|
#ifdef _WIN32
|
||||||
ShowCursor();
|
ShowCursor();
|
||||||
#else
|
#else
|
||||||
ShowCursorLinux();
|
ShowCursorLinux();
|
||||||
|
|||||||
+1
-1
@@ -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]);
|
||||||
|
|||||||
+12
-7
@@ -138,11 +138,16 @@ static SearchResults Quiescence(Board& board, int alpha, int beta, int ply, Sear
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else if constexpr (mode == nodesMode) {
|
||||||
if (ctx.nodes > ctx.nodesToGo) {
|
if (ctx.nodes > ctx.nodesToGo) {
|
||||||
ctx.searchStopped = true;
|
ctx.searchStopped = true;
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
} else if constexpr (mode == datagen) {
|
||||||
|
if (ctx.nodes > DATAGEN::HARD_NODES) {
|
||||||
|
ctx.searchStopped = true;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
int bestScore = HCE::Evaluate(board);
|
int bestScore = HCE::Evaluate(board);
|
||||||
@@ -194,18 +199,13 @@ static SearchResults Quiescence(Board& board, int alpha, int beta, int ply, Sear
|
|||||||
|
|
||||||
template <bool isPV, searchMode mode>
|
template <bool isPV, searchMode mode>
|
||||||
SearchResults PVS(Board& board, int depth, int alpha, int beta, int ply, SearchContext& ctx) {
|
SearchResults PVS(Board& board, int depth, int alpha, int beta, int ply, SearchContext& ctx) {
|
||||||
if constexpr (mode != bench || mode != nodesMode) {
|
if constexpr (mode != bench && mode != nodesMode) {
|
||||||
if (ctx.nodes % 1024 == 0) {
|
if (ctx.nodes % 1024 == 0) {
|
||||||
if constexpr (mode == normal) {
|
if constexpr (mode == normal) {
|
||||||
if (ctx.sw.GetElapsedMS() >= ctx.timeToSearch) {
|
if (ctx.sw.GetElapsedMS() >= ctx.timeToSearch) {
|
||||||
ctx.searchStopped = true;
|
ctx.searchStopped = true;
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
} else if constexpr (mode == datagen) {
|
|
||||||
if (ctx.nodes >= DATAGEN::HARD_NODES) {
|
|
||||||
ctx.searchStopped = true;
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else if constexpr (mode == nodesMode) {
|
} else if constexpr (mode == nodesMode) {
|
||||||
@@ -213,6 +213,11 @@ SearchResults PVS(Board& board, int depth, int alpha, int beta, int ply, SearchC
|
|||||||
ctx.searchStopped = true;
|
ctx.searchStopped = true;
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
} else if constexpr (mode == datagen) {
|
||||||
|
if (ctx.nodes > DATAGEN::HARD_NODES) {
|
||||||
|
ctx.searchStopped = true;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
ctx.pvLine.SetLength(ply);
|
ctx.pvLine.SetLength(ply);
|
||||||
|
|||||||
@@ -10,6 +10,7 @@
|
|||||||
#include "benchmark.h"
|
#include "benchmark.h"
|
||||||
#include "perft.h"
|
#include "perft.h"
|
||||||
#include "utils.h"
|
#include "utils.h"
|
||||||
|
#include "datagen.h"
|
||||||
|
|
||||||
static void ParsePosition(Board &board, std::string_view command, SEARCH::SearchContext& ctx) {
|
static void ParsePosition(Board &board, std::string_view command, SEARCH::SearchContext& ctx) {
|
||||||
if (command.find("startpos") != std::string::npos) {
|
if (command.find("startpos") != std::string::npos) {
|
||||||
@@ -197,6 +198,12 @@ void UCILoop(Board &board) {
|
|||||||
|
|
||||||
if (input.find("nnue") != std::string::npos) {
|
if (input.find("nnue") != std::string::npos) {
|
||||||
board.PrintNNUE();
|
board.PrintNNUE();
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (input.find("datagen") != std::string::npos) {
|
||||||
|
DATAGEN::Run(500000*2, 22);
|
||||||
|
continue;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user