From 91ec9282a0f95eb2428b21c89caa3618efb7a9f6 Mon Sep 17 00:00:00 2001 From: rektdie Date: Fri, 11 Apr 2025 02:49:10 +0200 Subject: [PATCH] encapsulated history --- source/benchmark.cpp | 3 --- source/datagen.cpp | 1 - source/search.cpp | 22 +++++++++++----------- source/search.h | 7 ++++--- source/uci.cpp | 2 +- 5 files changed, 16 insertions(+), 19 deletions(-) diff --git a/source/benchmark.cpp b/source/benchmark.cpp index b0bffcf..9dcf0fa 100644 --- a/source/benchmark.cpp +++ b/source/benchmark.cpp @@ -71,8 +71,6 @@ void RunBenchmark() { // Clearing TT.Clear(); std::memset(killerMoves, 0, sizeof(killerMoves)); - - history.Clear(); SearchContext ctx; @@ -86,6 +84,5 @@ void RunBenchmark() { std::cout << ctx.nodes << " nodes " << int(ctx.nodes/sw.GetElapsedSec()) << " nps" << std::endl; TT.Clear(); std::memset(killerMoves, 0, sizeof(killerMoves)); - history.Clear(); positionIndex = 0; } diff --git a/source/datagen.cpp b/source/datagen.cpp index d594422..6668f07 100644 --- a/source/datagen.cpp +++ b/source/datagen.cpp @@ -103,7 +103,6 @@ void PlayGame(std::atomic& positions, U64 targetPositions, std::vectorbestMove == move) { return 50000; @@ -51,20 +51,20 @@ static int ScoreMove(Board &board, Move &move, int ply) { return 18000; } else { // Max 16384 - return history[board.sideToMove][move.MoveFrom()][move.MoveTo()]; + return ctx.history[board.sideToMove][move.MoveFrom()][move.MoveTo()]; } } return 0; } -static void SortMoves(Board &board, int ply) { +static void SortMoves(Board &board, int ply, SearchContext& ctx) { std::array scores; std::array indices; // Initialize scores and indices for (int i = 0; i < board.currentMoveIndex; i++) { - scores[i] = ScoreMove(board, board.moveList[i], ply); + scores[i] = ScoreMove(board, board.moveList[i], ply, ctx); indices[i] = i; } @@ -86,14 +86,14 @@ static void SortMoves(Board &board, int ply) { } } -void ListScores(Board &board, int ply) { - SortMoves(board, ply); +void ListScores(Board &board, int ply, SearchContext& ctx) { + SortMoves(board, ply, ctx); for (int i = 0; i < board.currentMoveIndex; i++) { Move currentMove = board.moveList[i]; std::cout << squareCoords[currentMove.MoveFrom()] << squareCoords[currentMove.MoveTo()]; - std::cout << ": " << ScoreMove(board, currentMove, ply) << '\n'; + std::cout << ": " << ScoreMove(board, currentMove, ply, ctx) << '\n'; } } @@ -169,7 +169,7 @@ static SearchResults Quiescence(Board board, int alpha, int beta, int ply, Searc MOVEGEN::GenerateMoves(board); - SortMoves(board, ply); + SortMoves(board, ply, ctx); SearchResults results; @@ -265,7 +265,7 @@ SearchResults PVS(Board board, int depth, int alpha, int beta, int ply, SearchCo MOVEGEN::GenerateMoves(board); - SortMoves(board, ply); + SortMoves(board, ply, ctx); int score = -inf; int nodeType = AllNode; @@ -338,10 +338,10 @@ SearchResults PVS(Board board, int depth, int alpha, int beta, int ply, SearchCo int bonus = depth * depth; - history.Update(board.sideToMove, currMove, bonus); + ctx.history.Update(board.sideToMove, currMove, bonus); for (int moveIndex = 0; moveIndex < seenQuietsCount - 1; moveIndex++) { - history.Update(board.sideToMove, seenQuiets[moveIndex], -bonus); + ctx.history.Update(board.sideToMove, seenQuiets[moveIndex], -bonus); } } diff --git a/source/search.h b/source/search.h index 3753af3..9769acf 100644 --- a/source/search.h +++ b/source/search.h @@ -72,8 +72,6 @@ public: } }; -inline History history; - class PVLine { private: int length[MAX_PLY] = {}; @@ -112,10 +110,13 @@ public: U64 nodesToGo = 0; bool doingNullMove = false; + PVLine pvLine; + History history; SearchContext(){ pvLine.Clear(); + history.Clear(); } }; @@ -137,5 +138,5 @@ constexpr int moveScoreTable[6][6] = { 100, 200, 300, 400, 500, 600 }; -void ListScores(Board &board); +void ListScores(Board &board, SearchContext& ctx); } diff --git a/source/uci.cpp b/source/uci.cpp index 60d0f50..ad676b6 100644 --- a/source/uci.cpp +++ b/source/uci.cpp @@ -138,7 +138,7 @@ void UCILoop(Board &board) { TT.Clear(); std::memset(SEARCH::killerMoves, 0, sizeof(SEARCH::killerMoves)); - SEARCH::history.Clear(); + ctx.history.Clear(); positionIndex = 0; continue;