From c0b76d81bbeaa0db03fa76db9e9888c87a29363d Mon Sep 17 00:00:00 2001 From: rektdie Date: Thu, 3 Apr 2025 03:15:45 +0200 Subject: [PATCH] separated playing games from run function --- source/board.h | 4 ++-- source/datagen.cpp | 57 +++++++++++++++++++++++++++------------------- source/search.cpp | 14 ++++++------ source/search.h | 12 +++++----- source/tt.h | 4 ++-- 5 files changed, 50 insertions(+), 41 deletions(-) diff --git a/source/board.h b/source/board.h index 4223eca..9b5f369 100644 --- a/source/board.h +++ b/source/board.h @@ -9,8 +9,8 @@ constexpr int noEPTarget = -1; constexpr int MAX_MOVES = 218; -inline int positionIndex = 0; -inline U64 positionHistory[1000]; +thread_local inline int positionIndex = 0; +thread_local inline U64 positionHistory[1000]; class Board { public: diff --git a/source/datagen.cpp b/source/datagen.cpp index c2702cd..e5265bc 100644 --- a/source/datagen.cpp +++ b/source/datagen.cpp @@ -3,6 +3,7 @@ #include #include #include +#include #include "datagen.h" #include "movegen.h" #include "evaluate.h" @@ -220,23 +221,8 @@ void PrintProgress(int positions, int targetPositions, Stopwatch &stopwatch, int std::cout << std::string(width, '-') << std::endl; } -void Run(int targetPositions, int threads) { - #ifdef _WIN32 - system("cls"); - HideCursor(); - #else - std::cout << "\033[2J\033[H"; - HideCursorLinux(); - #endif - - Stopwatch stopwatch; - stopwatch.Restart(); - - int positions = 0; - int lastPrintedPosition = 0; - double lastPrintedTime = stopwatch.GetElapsedSec(); - - while (positions < targetPositions * 1000) { +void PlayGame(std::atomic& positions, int targetPositions) { + while (positions.load(std::memory_order_relaxed) < targetPositions) { Game game; Board board; @@ -264,18 +250,41 @@ void Run(int targetPositions, int threads) { MOVEGEN::GenerateMoves(board); // Increment positions, but do not exceed target - if (positions < targetPositions * 1000) { - positions++; + if (positions.load(std::memory_order_relaxed) < targetPositions) { + 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; + } + + } } - - if (std::abs(safeResults.score) >= std::abs(SEARCH::MATE_SCORE) - SEARCH::MAX_PLY) { - wdl = board.sideToMove ? 2 : 0; - } - game.format.packFrom(board, staticEval, wdl); WriteToFile(game, "data.binpack"); + } +} +void Run(int targetPositions, int threads) { + #ifdef _WIN32 + system("cls"); + HideCursor(); + #else + std::cout << "\033[2J\033[H"; + HideCursorLinux(); + #endif + + Stopwatch stopwatch; + + std::atomic positions = 0; + + auto worker = std::thread(PlayGame, std::ref(positions), targetPositions * 1000); + worker.detach(); + + int lastPrintedPosition = 0; + double lastPrintedTime = stopwatch.GetElapsedSec(); + + while (positions.load(std::memory_order_relaxed) < targetPositions * 1000) { // Print progress every second double currentTime = stopwatch.GetElapsedSec(); if (currentTime - lastPrintedTime >= 1) { diff --git a/source/search.cpp b/source/search.cpp index 9ab0558..4784f86 100644 --- a/source/search.cpp +++ b/source/search.cpp @@ -10,16 +10,16 @@ namespace SEARCH { -U64 nodes = 0; -U64 nodesToGo = 0; -bool benchStarted = false; +thread_local U64 nodes = 0; +thread_local U64 nodesToGo = 0; +thread_local bool benchStarted = false; -static inline int timeToSearch = 0; -static inline bool doingNullMove = false; +thread_local static inline int timeToSearch = 0; +thread_local static inline bool doingNullMove = false; -inline PVLine pvLine; +thread_local inline PVLine pvLine; -Stopwatch sw; +thread_local Stopwatch sw; void InitLMRTable() { for (int depth = 0; depth <= MAX_DEPTH; depth++) { diff --git a/source/search.h b/source/search.h index 485070f..71336a4 100644 --- a/source/search.h +++ b/source/search.h @@ -19,15 +19,15 @@ constexpr int MAX_DEPTH = 64; constexpr int MAX_PLY = 128; constexpr int MAX_HISTORY = 16384; -extern U64 nodes; -extern bool benchStarted; +thread_local extern U64 nodes; +thread_local extern bool benchStarted; constexpr int inf = 100000; -inline bool searchStopped = false; +thread_local inline bool searchStopped = false; -// [id][ply] -inline int killerMoves[2][MAX_PLY]; +// [id][ply] +thread_local inline int killerMoves[2][MAX_PLY]; inline int lmrTable[MAX_DEPTH+1][MAX_MOVES]; @@ -75,7 +75,7 @@ public: } }; -inline History history; +thread_local inline History history; class PVLine { private: diff --git a/source/tt.h b/source/tt.h index e8607c4..a6e4136 100644 --- a/source/tt.h +++ b/source/tt.h @@ -21,7 +21,7 @@ constexpr U64 maxHash = (64* 1000000) / sizeof(TTEntry); // 5 MB constexpr U64 defaultHash = (5 * 1000000) / sizeof(TTEntry); -inline U64 hashSize = defaultHash; +thread_local inline U64 hashSize = defaultHash; constexpr int invalidEntry = 111111; @@ -61,4 +61,4 @@ public: void WriteEntry(U64 &hashKey, int depth, int score, int nodeType, Move besteMove); }; -inline TTable TT; +thread_local inline TTable TT;