From eff4f50ee8f8ae169417996e6655092b53c53b67 Mon Sep 17 00:00:00 2001 From: rektdie Date: Fri, 11 Apr 2025 02:03:53 +0200 Subject: [PATCH 01/23] ensapsulated nodes | bench 1857933 --- source/benchmark.cpp | 8 +++--- source/board.cpp | 1 - source/datagen.cpp | 4 ++- source/search.cpp | 66 +++++++++++++++++++++++--------------------- source/search.h | 12 ++++++-- source/uci.cpp | 10 ++++--- 6 files changed, 56 insertions(+), 45 deletions(-) diff --git a/source/benchmark.cpp b/source/benchmark.cpp index 120024e..78c9511 100644 --- a/source/benchmark.cpp +++ b/source/benchmark.cpp @@ -75,16 +75,16 @@ void RunBenchmark() { history.Clear(); - nodes = 0; + SearchContext ctx; + Stopwatch sw; for (int i = 0; i < fenPositions.size(); i++) { board.SetByFen(fenPositions[i]); - SearchPosition(board, SearchParams()); + SearchPosition(board, SearchParams(), ctx); } - std::cout << nodes << " nodes " << int(nodes/sw.GetElapsedSec()) << " nps" << std::endl; - nodes = 0; + std::cout << ctx.nodes << " nodes " << int(ctx.nodes/sw.GetElapsedSec()) << " nps" << std::endl; TT.Clear(); benchStarted = false; std::memset(killerMoves, 0, sizeof(killerMoves)); diff --git a/source/board.cpp b/source/board.cpp index e796708..eabfae5 100644 --- a/source/board.cpp +++ b/source/board.cpp @@ -393,7 +393,6 @@ bool Board::MakeMove(Move move) { positionIndex++; positionHistory[positionIndex] = hashKey; - SEARCH::nodes++; return true; } diff --git a/source/datagen.cpp b/source/datagen.cpp index 1bc1425..d594422 100644 --- a/source/datagen.cpp +++ b/source/datagen.cpp @@ -105,6 +105,8 @@ void PlayGame(std::atomic& positions, U64 targetPositions, std::vector& positions, U64 targetPositions, std::vector(board); while (!IsGameOver(board)) { - SEARCH::SearchResults results = SEARCH::SearchPosition(board, SearchParams()); + SEARCH::SearchResults results = SEARCH::SearchPosition(board, SearchParams(), ctx); if (!results.bestMove) break; safeResults = results; diff --git a/source/search.cpp b/source/search.cpp index ebe253c..af4ac17 100644 --- a/source/search.cpp +++ b/source/search.cpp @@ -141,16 +141,16 @@ static int GetReductions(Board &board, Move &move, int depth, int moveSeen, int } template -static SearchResults Quiescence(Board board, int alpha, int beta, int ply) { +static SearchResults Quiescence(Board board, int alpha, int beta, int ply, SearchContext& ctx) { if constexpr (mode != nodesMode) { - if (!benchStarted && nodes % 1024 == 0) { + if (!benchStarted && ctx.nodes % 1024 == 0) { if (sw.GetElapsedMS() >= timeToSearch) { StopSearch(); return 0; } } } else { - if (nodes > nodesToGo) { + if (ctx.nodes > nodesToGo) { StopSearch(); return 0; } @@ -182,7 +182,8 @@ static SearchResults Quiescence(Board board, int alpha, int beta, int ply) { Board copy = board; int isLegal = copy.MakeMove(board.moveList[i]); if (!isLegal) continue; - int score = -Quiescence(copy, -beta, -alpha, ply + 1).score; + ctx.nodes++; + int score = -Quiescence(copy, -beta, -alpha, ply + 1, ctx).score; positionIndex--; if (score >= beta) { @@ -203,23 +204,23 @@ static SearchResults Quiescence(Board board, int alpha, int beta, int ply) { } template -SearchResults PVS(Board board, int depth, int alpha, int beta, int ply) { +SearchResults PVS(Board board, int depth, int alpha, int beta, int ply, SearchContext& ctx) { if constexpr (mode != bench || mode != nodesMode) { - if (nodes % 1024 == 0) { + if (ctx.nodes % 1024 == 0) { if constexpr (mode == normal) { if (sw.GetElapsedMS() >= timeToSearch) { StopSearch(); return 0; } } else if constexpr (mode == datagen) { - if (nodes >= DATAGEN::HARD_NODES) { + if (ctx.nodes >= DATAGEN::HARD_NODES) { StopSearch(); return 0; } } } } else if constexpr (mode == nodesMode) { - if (nodes > nodesToGo) { + if (ctx.nodes > nodesToGo) { StopSearch(); return 0; } @@ -237,7 +238,7 @@ SearchResults PVS(Board board, int depth, int alpha, int beta, int ply) { } } - if (depth <= 0) return Quiescence(board, alpha, beta, ply); + if (depth <= 0) return Quiescence(board, alpha, beta, ply, ctx); const int staticEval = HCE::Evaluate(board); @@ -257,7 +258,7 @@ SearchResults PVS(Board board, int depth, int alpha, int beta, int ply) { // Always legal so we dont check it doingNullMove = true; - int score = -PVS(copy, depth - 3, -beta, -beta + 1, ply + 1).score; + int score = -PVS(copy, depth - 3, -beta, -beta + 1, ply + 1, ctx).score; doingNullMove = false; if (searchStopped) return 0; @@ -296,6 +297,7 @@ SearchResults PVS(Board board, int depth, int alpha, int beta, int ply) { bool isLegal = copy.MakeMove(currMove); if (!isLegal) continue; + ctx.nodes++; int reductions = GetReductions(board, currMove, depth, moveSeen, ply); @@ -304,23 +306,23 @@ SearchResults PVS(Board board, int depth, int alpha, int beta, int ply) { // First move (suspected PV node) if (!moveSeen) { // Full search - score = -PVS(copy, newDepth, -beta, -alpha, ply + 1).score; + score = -PVS(copy, newDepth, -beta, -alpha, ply + 1, ctx).score; } else if (reductions) { // Null-window search with reductions - score = -PVS(copy, newDepth - reductions, -alpha-1, -alpha, ply + 1).score; + score = -PVS(copy, newDepth - reductions, -alpha-1, -alpha, ply + 1, ctx).score; if (score > alpha) { // Null-window search now without the reduction - score = -PVS(copy, newDepth, -alpha-1, -alpha, ply + 1).score; + score = -PVS(copy, newDepth, -alpha-1, -alpha, ply + 1, ctx).score; } } else { // Null-window search - score = -PVS(copy, newDepth, -alpha-1, -alpha, ply + 1).score; + score = -PVS(copy, newDepth, -alpha-1, -alpha, ply + 1, ctx).score; } // Check if we need to do full window re-search if (moveSeen && score > alpha && score < beta) { - score = -PVS(copy, newDepth, -beta, -alpha, ply + 1).score; + score = -PVS(copy, newDepth, -beta, -alpha, ply + 1, ctx).score; } moveSeen++; @@ -377,7 +379,7 @@ SearchResults PVS(Board board, int depth, int alpha, int beta, int ply) { // Iterative deepening template -static SearchResults ID(Board &board, SearchParams params) { +static SearchResults ID(Board &board, SearchParams params, SearchContext& ctx) { int fullTime = board.sideToMove ? params.btime : params.wtime; int inc = board.sideToMove ? params.binc : params.winc; @@ -405,7 +407,7 @@ static SearchResults ID(Board &board, SearchParams params) { timeToSearch = std::max((fullTime / 20) + (inc / 2), 4); int softTime = timeToSearch * 0.65; - SearchResults currentResults = PVS(board, depth, alpha, beta, ply); + SearchResults currentResults = PVS(board, depth, alpha, beta, ply, ctx); elapsed = sw.GetElapsedMS(); // If we fell outside the window, try again with full width @@ -438,7 +440,7 @@ static SearchResults ID(Board &board, SearchParams params) { std::cout << "depth " << depth; std::cout << " time " << elapsed; std::cout << " score cp " << UTILS::ConvertToWhiteRelative(board, safeResults.score); - std::cout << " nodes " << nodes << " nps " << int(nodes/sw.GetElapsedSec()); + std::cout << " nodes " << ctx.nodes << " nps " << int(ctx.nodes/sw.GetElapsedSec()); std::cout << " hashfull " << TT.GetUsedPercentage(); std::cout << " pv "; pvLine.Print(0); @@ -451,7 +453,7 @@ static SearchResults ID(Board &board, SearchParams params) { } } } else if constexpr (mode == datagen) { - if (nodes >= DATAGEN::SOFT_NODES) { + if (ctx.nodes >= DATAGEN::SOFT_NODES) { StopSearch(); break; } @@ -463,10 +465,10 @@ static SearchResults ID(Board &board, SearchParams params) { } template -SearchResults SearchPosition(Board &board, SearchParams params) { +SearchResults SearchPosition(Board &board, SearchParams params, SearchContext& ctx) { searchStopped = false; if constexpr (mode != bench) { - nodes = 0; + ctx.nodes = 0; if constexpr (mode == nodesMode) { nodesToGo = params.nodes; @@ -474,7 +476,7 @@ SearchResults SearchPosition(Board &board, SearchParams params) { } pvLine.Clear(); - SearchResults results = ID(board, params); + SearchResults results = ID(board, params, ctx); if constexpr (mode != normal) return results; @@ -489,16 +491,16 @@ void StopSearch() { searchStopped = true; } -template SearchResults PVS(Board, int, int, int, int); -template SearchResults PVS(Board, int, int, int, int); -template SearchResults PVS(Board, int, int, int, int); -template SearchResults PVS(Board, int, int, int, int); -template SearchResults PVS(Board, int, int, int, int); -template SearchResults PVS(Board, int, int, int, int); +template SearchResults PVS(Board, int, int, int, int, SearchContext& ctx); +template SearchResults PVS(Board, int, int, int, int, SearchContext& ctx); +template SearchResults PVS(Board, int, int, int, int, SearchContext& ctx); +template SearchResults PVS(Board, int, int, int, int, SearchContext& ctx); +template SearchResults PVS(Board, int, int, int, int, SearchContext& ctx); +template SearchResults PVS(Board, int, int, int, int, SearchContext& ctx); -template SearchResults SearchPosition(Board &board, SearchParams params); -template SearchResults SearchPosition(Board &board, SearchParams params); -template SearchResults SearchPosition(Board &board, SearchParams params); -template SearchResults SearchPosition(Board &board, SearchParams params); +template SearchResults SearchPosition(Board &board, SearchParams params, SearchContext& ctx); +template SearchResults SearchPosition(Board &board, SearchParams params, SearchContext& ctx); +template SearchResults SearchPosition(Board &board, SearchParams params, SearchContext& ctx); +template SearchResults SearchPosition(Board &board, SearchParams params, SearchContext& ctx); } diff --git a/source/search.h b/source/search.h index 4b31cdb..fb206be 100644 --- a/source/search.h +++ b/source/search.h @@ -19,7 +19,6 @@ constexpr int MAX_DEPTH = 64; constexpr int MAX_PLY = 128; constexpr int MAX_HISTORY = 16384; -extern U64 nodes; extern bool benchStarted; constexpr int inf = 100000; @@ -109,11 +108,18 @@ public: } }; +class SearchContext { +public: + U64 nodes = 0; + + SearchContext(){} +}; + template -SearchResults PVS(Board board, int depth, int alpha, int beta, int ply); +SearchResults PVS(Board board, int depth, int alpha, int beta, int ply, SearchContext& ctx); template -SearchResults SearchPosition(Board &board, SearchParams params); +SearchResults SearchPosition(Board &board, SearchParams params, SearchContext& ctx); void StopSearch(); bool IsDraw(Board &board); diff --git a/source/uci.cpp b/source/uci.cpp index 5055f65..60d0f50 100644 --- a/source/uci.cpp +++ b/source/uci.cpp @@ -68,7 +68,7 @@ static int ReadParam(std::string param, std::string &command) { return 0; } -static void ParseGo(Board &board, std::string &command) { +static void ParseGo(Board &board, std::string &command, SEARCH::SearchContext& ctx) { SearchParams params; params.wtime = ReadParam("wtime", command); @@ -84,10 +84,10 @@ static void ParseGo(Board &board, std::string &command) { } if (params.nodes) { - auto worker = std::thread(SEARCH::SearchPosition, std::ref(board), params); + auto worker = std::thread(SEARCH::SearchPosition, std::ref(board), params, std::ref(ctx)); worker.detach(); } else { - auto worker = std::thread(SEARCH::SearchPosition, std::ref(board), params); + auto worker = std::thread(SEARCH::SearchPosition, std::ref(board), params, std::ref(ctx)); worker.detach(); } } @@ -110,6 +110,8 @@ static void PrintEngineInfo() { void UCILoop(Board &board) { std::string input = ""; + SEARCH::SearchContext ctx; + // main loop while (true) { std::getline(std::cin, input); @@ -144,7 +146,7 @@ void UCILoop(Board &board) { // parse UCI "go" command if (input.find("go") != std::string::npos) { - ParseGo(board, input); + ParseGo(board, input, ctx); continue; } -- 2.47.3 From bd5f5ac602e70f72f656a944cb1f7e23d3c41401 Mon Sep 17 00:00:00 2001 From: rektdie Date: Fri, 11 Apr 2025 02:16:49 +0200 Subject: [PATCH 02/23] removed benchStarted bool --- source/benchmark.cpp | 2 -- source/search.cpp | 11 ++++++----- source/search.h | 2 -- 3 files changed, 6 insertions(+), 9 deletions(-) diff --git a/source/benchmark.cpp b/source/benchmark.cpp index 78c9511..b0bffcf 100644 --- a/source/benchmark.cpp +++ b/source/benchmark.cpp @@ -65,7 +65,6 @@ void RunBenchmark() { Board board; int depth = BENCH_DEPTH; int ply = 0; - benchStarted = true; searchStopped = false; positionIndex = 0; @@ -86,7 +85,6 @@ void RunBenchmark() { std::cout << ctx.nodes << " nodes " << int(ctx.nodes/sw.GetElapsedSec()) << " nps" << std::endl; TT.Clear(); - benchStarted = false; std::memset(killerMoves, 0, sizeof(killerMoves)); history.Clear(); positionIndex = 0; diff --git a/source/search.cpp b/source/search.cpp index af4ac17..e651747 100644 --- a/source/search.cpp +++ b/source/search.cpp @@ -12,7 +12,6 @@ namespace SEARCH { U64 nodes = 0; U64 nodesToGo = 0; -bool benchStarted = false; static inline int timeToSearch = 0; static inline bool doingNullMove = false; @@ -143,10 +142,12 @@ static int GetReductions(Board &board, Move &move, int depth, int moveSeen, int template static SearchResults Quiescence(Board board, int alpha, int beta, int ply, SearchContext& ctx) { if constexpr (mode != nodesMode) { - if (!benchStarted && ctx.nodes % 1024 == 0) { - if (sw.GetElapsedMS() >= timeToSearch) { - StopSearch(); - return 0; + if constexpr (mode != bench) { + if (ctx.nodes % 1024 == 0) { + if (sw.GetElapsedMS() >= timeToSearch) { + StopSearch(); + return 0; + } } } } else { diff --git a/source/search.h b/source/search.h index fb206be..3446fb8 100644 --- a/source/search.h +++ b/source/search.h @@ -19,8 +19,6 @@ constexpr int MAX_DEPTH = 64; constexpr int MAX_PLY = 128; constexpr int MAX_HISTORY = 16384; -extern bool benchStarted; - constexpr int inf = 100000; inline bool searchStopped = false; -- 2.47.3 From 3f9ad117acf5dd4b85fb5edeb75c76daa537234b Mon Sep 17 00:00:00 2001 From: rektdie Date: Fri, 11 Apr 2025 02:25:05 +0200 Subject: [PATCH 03/23] encapsulated doingNullMove --- source/search.cpp | 8 +++----- source/search.h | 1 + 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/source/search.cpp b/source/search.cpp index e651747..444f147 100644 --- a/source/search.cpp +++ b/source/search.cpp @@ -10,11 +10,9 @@ namespace SEARCH { -U64 nodes = 0; U64 nodesToGo = 0; static inline int timeToSearch = 0; -static inline bool doingNullMove = false; inline PVLine pvLine; @@ -252,15 +250,15 @@ SearchResults PVS(Board board, int depth, int alpha, int beta, int ply, SearchCo } // Null Move Pruning - if (!doingNullMove && staticEval >= beta) { + if (!ctx.doingNullMove && staticEval >= beta) { if (depth >= 3 && !board.InPossibleZug()) { Board copy = board; bool isLegal = copy.MakeMove(Move()); // Always legal so we dont check it - doingNullMove = true; + ctx.doingNullMove = true; int score = -PVS(copy, depth - 3, -beta, -beta + 1, ply + 1, ctx).score; - doingNullMove = false; + ctx.doingNullMove = false; if (searchStopped) return 0; if (score >= beta) return score; diff --git a/source/search.h b/source/search.h index 3446fb8..f42e8c4 100644 --- a/source/search.h +++ b/source/search.h @@ -109,6 +109,7 @@ public: class SearchContext { public: U64 nodes = 0; + bool doingNullMove = false; SearchContext(){} }; -- 2.47.3 From 48b8934861f3de557b838839ccdb7c69d0698605 Mon Sep 17 00:00:00 2001 From: rektdie Date: Fri, 11 Apr 2025 02:37:27 +0200 Subject: [PATCH 04/23] encapsulated pvLine --- source/search.cpp | 10 ++++------ source/search.h | 5 ++++- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/source/search.cpp b/source/search.cpp index 444f147..34dff4b 100644 --- a/source/search.cpp +++ b/source/search.cpp @@ -14,8 +14,6 @@ U64 nodesToGo = 0; static inline int timeToSearch = 0; -inline PVLine pvLine; - Stopwatch sw; void InitLMRTable() { @@ -225,7 +223,7 @@ SearchResults PVS(Board board, int depth, int alpha, int beta, int ply, SearchCo } } - pvLine.SetLength(ply); + ctx.pvLine.SetLength(ply); if (ply && (IsThreefold(board) || IsFifty(board) || IsInsuffMat(board))) return 0; @@ -359,7 +357,7 @@ SearchResults PVS(Board board, int depth, int alpha, int beta, int ply, SearchCo nodeType = PV; alpha = score; results.bestMove = currMove; - pvLine.SetMove(ply, currMove); + ctx.pvLine.SetMove(ply, currMove); } } @@ -442,7 +440,7 @@ static SearchResults ID(Board &board, SearchParams params, SearchContext& ctx) { std::cout << " nodes " << ctx.nodes << " nps " << int(ctx.nodes/sw.GetElapsedSec()); std::cout << " hashfull " << TT.GetUsedPercentage(); std::cout << " pv "; - pvLine.Print(0); + ctx.pvLine.Print(0); std::cout << std::endl; if constexpr (mode == normal) { @@ -473,7 +471,7 @@ SearchResults SearchPosition(Board &board, SearchParams params, SearchContext& c nodesToGo = params.nodes; } } - pvLine.Clear(); + ctx.pvLine.Clear(); SearchResults results = ID(board, params, ctx); diff --git a/source/search.h b/source/search.h index f42e8c4..c1e1c48 100644 --- a/source/search.h +++ b/source/search.h @@ -110,8 +110,11 @@ class SearchContext { public: U64 nodes = 0; bool doingNullMove = false; + PVLine pvLine; - SearchContext(){} + SearchContext(){ + pvLine.Clear(); + } }; template -- 2.47.3 From 818b549cba5244d042195ef7e6df53789700f497 Mon Sep 17 00:00:00 2001 From: rektdie Date: Fri, 11 Apr 2025 02:43:43 +0200 Subject: [PATCH 05/23] encapsulated nodesToGo --- source/search.cpp | 8 +++----- source/search.h | 2 ++ 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/source/search.cpp b/source/search.cpp index 34dff4b..b8d8e6b 100644 --- a/source/search.cpp +++ b/source/search.cpp @@ -10,8 +10,6 @@ namespace SEARCH { -U64 nodesToGo = 0; - static inline int timeToSearch = 0; Stopwatch sw; @@ -147,7 +145,7 @@ static SearchResults Quiescence(Board board, int alpha, int beta, int ply, Searc } } } else { - if (ctx.nodes > nodesToGo) { + if (ctx.nodes > ctx.nodesToGo) { StopSearch(); return 0; } @@ -217,7 +215,7 @@ SearchResults PVS(Board board, int depth, int alpha, int beta, int ply, SearchCo } } } else if constexpr (mode == nodesMode) { - if (ctx.nodes > nodesToGo) { + if (ctx.nodes > ctx.nodesToGo) { StopSearch(); return 0; } @@ -468,7 +466,7 @@ SearchResults SearchPosition(Board &board, SearchParams params, SearchContext& c ctx.nodes = 0; if constexpr (mode == nodesMode) { - nodesToGo = params.nodes; + ctx.nodesToGo = params.nodes; } } ctx.pvLine.Clear(); diff --git a/source/search.h b/source/search.h index c1e1c48..3753af3 100644 --- a/source/search.h +++ b/source/search.h @@ -109,6 +109,8 @@ public: class SearchContext { public: U64 nodes = 0; + U64 nodesToGo = 0; + bool doingNullMove = false; PVLine pvLine; -- 2.47.3 From 91ec9282a0f95eb2428b21c89caa3618efb7a9f6 Mon Sep 17 00:00:00 2001 From: rektdie Date: Fri, 11 Apr 2025 02:49:10 +0200 Subject: [PATCH 06/23] 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; -- 2.47.3 From ff811784fb28758de132db097e2d834e40e3449e Mon Sep 17 00:00:00 2001 From: rektdie Date: Fri, 11 Apr 2025 03:02:28 +0200 Subject: [PATCH 07/23] encapsulated sw --- source/search.cpp | 15 ++++++--------- source/search.h | 4 ++++ 2 files changed, 10 insertions(+), 9 deletions(-) diff --git a/source/search.cpp b/source/search.cpp index a7ed83c..590fd3c 100644 --- a/source/search.cpp +++ b/source/search.cpp @@ -4,7 +4,6 @@ #include #include #include "tt.h" -#include "stopwatch.h" #include "datagen.h" #include "benchmark.h" @@ -12,8 +11,6 @@ namespace SEARCH { static inline int timeToSearch = 0; -Stopwatch sw; - void InitLMRTable() { for (int depth = 0; depth <= MAX_DEPTH; depth++) { for (int move = 0; move < MAX_MOVES; move++) { @@ -138,7 +135,7 @@ static SearchResults Quiescence(Board board, int alpha, int beta, int ply, Searc if constexpr (mode != nodesMode) { if constexpr (mode != bench) { if (ctx.nodes % 1024 == 0) { - if (sw.GetElapsedMS() >= timeToSearch) { + if (ctx.sw.GetElapsedMS() >= timeToSearch) { StopSearch(); return 0; } @@ -203,7 +200,7 @@ SearchResults PVS(Board board, int depth, int alpha, int beta, int ply, SearchCo if constexpr (mode != bench || mode != nodesMode) { if (ctx.nodes % 1024 == 0) { if constexpr (mode == normal) { - if (sw.GetElapsedMS() >= timeToSearch) { + if (ctx.sw.GetElapsedMS() >= timeToSearch) { StopSearch(); return 0; } @@ -396,14 +393,14 @@ static SearchResults ID(Board &board, SearchParams params, SearchContext& ctx) { int elapsed = 0; - sw.Restart(); + ctx.sw.Restart(); for (int depth = 1; depth <= toDepth; depth++) { timeToSearch = std::max((fullTime / 20) + (inc / 2), 4); int softTime = timeToSearch * 0.65; SearchResults currentResults = PVS(board, depth, alpha, beta, ply, ctx); - elapsed = sw.GetElapsedMS(); + elapsed = ctx.sw.GetElapsedMS(); // If we fell outside the window, try again with full width if ((currentResults.score <= alpha) @@ -435,14 +432,14 @@ static SearchResults ID(Board &board, SearchParams params, SearchContext& ctx) { std::cout << "depth " << depth; std::cout << " time " << elapsed; std::cout << " score cp " << UTILS::ConvertToWhiteRelative(board, safeResults.score); - std::cout << " nodes " << ctx.nodes << " nps " << int(ctx.nodes/sw.GetElapsedSec()); + std::cout << " nodes " << ctx.nodes << " nps " << int(ctx.nodes/ctx.sw.GetElapsedSec()); std::cout << " hashfull " << TT.GetUsedPercentage(); std::cout << " pv "; ctx.pvLine.Print(0); std::cout << std::endl; if constexpr (mode == normal) { - if (sw.GetElapsedMS() >= softTime) { + if (ctx.sw.GetElapsedMS() >= softTime) { StopSearch(); break; } diff --git a/source/search.h b/source/search.h index 9769acf..dbc2ba2 100644 --- a/source/search.h +++ b/source/search.h @@ -4,6 +4,7 @@ #include #include "move.h" #include +#include "stopwatch.h" namespace SEARCH { @@ -114,9 +115,12 @@ public: PVLine pvLine; History history; + Stopwatch sw; + SearchContext(){ pvLine.Clear(); history.Clear(); + sw.Restart(); } }; -- 2.47.3 From 7543a0d23120e97e90355d2e42c022404f7e474d Mon Sep 17 00:00:00 2001 From: rektdie Date: Fri, 11 Apr 2025 03:12:41 +0200 Subject: [PATCH 08/23] encapsulated timeToSearch --- source/search.cpp | 10 ++++------ source/search.h | 1 + 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/source/search.cpp b/source/search.cpp index 590fd3c..3239318 100644 --- a/source/search.cpp +++ b/source/search.cpp @@ -9,8 +9,6 @@ namespace SEARCH { -static inline int timeToSearch = 0; - void InitLMRTable() { for (int depth = 0; depth <= MAX_DEPTH; depth++) { for (int move = 0; move < MAX_MOVES; move++) { @@ -135,7 +133,7 @@ static SearchResults Quiescence(Board board, int alpha, int beta, int ply, Searc if constexpr (mode != nodesMode) { if constexpr (mode != bench) { if (ctx.nodes % 1024 == 0) { - if (ctx.sw.GetElapsedMS() >= timeToSearch) { + if (ctx.sw.GetElapsedMS() >= ctx.timeToSearch) { StopSearch(); return 0; } @@ -200,7 +198,7 @@ SearchResults PVS(Board board, int depth, int alpha, int beta, int ply, SearchCo if constexpr (mode != bench || mode != nodesMode) { if (ctx.nodes % 1024 == 0) { if constexpr (mode == normal) { - if (ctx.sw.GetElapsedMS() >= timeToSearch) { + if (ctx.sw.GetElapsedMS() >= ctx.timeToSearch) { StopSearch(); return 0; } @@ -396,8 +394,8 @@ static SearchResults ID(Board &board, SearchParams params, SearchContext& ctx) { ctx.sw.Restart(); for (int depth = 1; depth <= toDepth; depth++) { - timeToSearch = std::max((fullTime / 20) + (inc / 2), 4); - int softTime = timeToSearch * 0.65; + ctx.timeToSearch = std::max((fullTime / 20) + (inc / 2), 4); + int softTime = ctx.timeToSearch * 0.65; SearchResults currentResults = PVS(board, depth, alpha, beta, ply, ctx); elapsed = ctx.sw.GetElapsedMS(); diff --git a/source/search.h b/source/search.h index dbc2ba2..98d4c47 100644 --- a/source/search.h +++ b/source/search.h @@ -109,6 +109,7 @@ class SearchContext { public: U64 nodes = 0; U64 nodesToGo = 0; + int timeToSearch = 0; bool doingNullMove = false; -- 2.47.3 From 31de991dc4d0ba1bd34f8573bb35f8e4ba0544d8 Mon Sep 17 00:00:00 2001 From: rektdie Date: Fri, 11 Apr 2025 03:33:36 +0200 Subject: [PATCH 09/23] removed -fuse-ld=lld from compiling --- Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Makefile b/Makefile index 9d74e96..b30ddc8 100644 --- a/Makefile +++ b/Makefile @@ -30,7 +30,7 @@ $(EXE)$(EXE_EXT): $(OBJS) # Compile .cpp -> .o $(OBJ_DIR)/%.o: $(SRC_DIR)/%.cpp | $(OBJ_DIR) - $(CXX) -std=c++20 -c -o $@ $< -O3 -march=native -fuse-ld=lld -flto -static + $(CXX) -std=c++20 -c -o $@ $< -O3 -march=native -flto -static # Create object directory $(OBJ_DIR): -- 2.47.3 From ac7325f295d22ad6c9ada90c54c9213dc6961c1b Mon Sep 17 00:00:00 2001 From: rektdie Date: Fri, 11 Apr 2025 03:48:45 +0200 Subject: [PATCH 10/23] encapsulated searchStopped --- source/benchmark.cpp | 1 - source/search.cpp | 32 ++++++++++++++------------------ source/search.h | 4 +--- source/uci.cpp | 2 +- 4 files changed, 16 insertions(+), 23 deletions(-) diff --git a/source/benchmark.cpp b/source/benchmark.cpp index 9dcf0fa..72e702c 100644 --- a/source/benchmark.cpp +++ b/source/benchmark.cpp @@ -65,7 +65,6 @@ void RunBenchmark() { Board board; int depth = BENCH_DEPTH; int ply = 0; - searchStopped = false; positionIndex = 0; // Clearing diff --git a/source/search.cpp b/source/search.cpp index 3239318..f065a9e 100644 --- a/source/search.cpp +++ b/source/search.cpp @@ -134,14 +134,14 @@ static SearchResults Quiescence(Board board, int alpha, int beta, int ply, Searc if constexpr (mode != bench) { if (ctx.nodes % 1024 == 0) { if (ctx.sw.GetElapsedMS() >= ctx.timeToSearch) { - StopSearch(); + ctx.searchStopped = true; return 0; } } } } else { if (ctx.nodes > ctx.nodesToGo) { - StopSearch(); + ctx.searchStopped = true; return 0; } } @@ -189,7 +189,7 @@ static SearchResults Quiescence(Board board, int alpha, int beta, int ply, Searc } results.score = bestScore; - if (searchStopped) return 0; + if (ctx.searchStopped) return 0; return results; } @@ -199,19 +199,19 @@ SearchResults PVS(Board board, int depth, int alpha, int beta, int ply, SearchCo if (ctx.nodes % 1024 == 0) { if constexpr (mode == normal) { if (ctx.sw.GetElapsedMS() >= ctx.timeToSearch) { - StopSearch(); + ctx.searchStopped = true; return 0; } } else if constexpr (mode == datagen) { if (ctx.nodes >= DATAGEN::HARD_NODES) { - StopSearch(); + ctx.searchStopped = true; return 0; } } } } else if constexpr (mode == nodesMode) { if (ctx.nodes > ctx.nodesToGo) { - StopSearch(); + ctx.searchStopped = true; return 0; } } @@ -251,7 +251,7 @@ SearchResults PVS(Board board, int depth, int alpha, int beta, int ply, SearchCo int score = -PVS(copy, depth - 3, -beta, -beta + 1, ply + 1, ctx).score; ctx.doingNullMove = false; - if (searchStopped) return 0; + if (ctx.searchStopped) return 0; if (score >= beta) return score; } } @@ -318,7 +318,7 @@ SearchResults PVS(Board board, int depth, int alpha, int beta, int ply, SearchCo moveSeen++; positionIndex--; - if (searchStopped) return 0; + if (ctx.searchStopped) return 0; if (currMove.IsQuiet()) { seenQuiets[seenQuietsCount] = currMove; @@ -362,7 +362,7 @@ SearchResults PVS(Board board, int depth, int alpha, int beta, int ply, SearchCo } } - if (searchStopped) return 0; + if (ctx.searchStopped) return 0; TT.WriteEntry(board.hashKey, depth, results.score, nodeType, results.bestMove); return results; } @@ -410,7 +410,7 @@ static SearchResults ID(Board &board, SearchParams params, SearchContext& ctx) { depth--; - if (searchStopped) break; + if (ctx.searchStopped) break; continue; } @@ -418,7 +418,7 @@ static SearchResults ID(Board &board, SearchParams params, SearchContext& ctx) { beta = currentResults.score + delta; delta = 50; - if (searchStopped) { + if (ctx.searchStopped) { break; } else { if (currentResults.bestMove) { @@ -438,13 +438,13 @@ static SearchResults ID(Board &board, SearchParams params, SearchContext& ctx) { if constexpr (mode == normal) { if (ctx.sw.GetElapsedMS() >= softTime) { - StopSearch(); + ctx.searchStopped = true; break; } } } else if constexpr (mode == datagen) { if (ctx.nodes >= DATAGEN::SOFT_NODES) { - StopSearch(); + ctx.searchStopped = true; break; } } @@ -456,7 +456,7 @@ static SearchResults ID(Board &board, SearchParams params, SearchContext& ctx) { template SearchResults SearchPosition(Board &board, SearchParams params, SearchContext& ctx) { - searchStopped = false; + ctx.searchStopped = false; if constexpr (mode != bench) { ctx.nodes = 0; @@ -477,10 +477,6 @@ SearchResults SearchPosition(Board &board, SearchParams params, SearchContext& c return results; } -void StopSearch() { - searchStopped = true; -} - template SearchResults PVS(Board, int, int, int, int, SearchContext& ctx); template SearchResults PVS(Board, int, int, int, int, SearchContext& ctx); template SearchResults PVS(Board, int, int, int, int, SearchContext& ctx); diff --git a/source/search.h b/source/search.h index 98d4c47..e897898 100644 --- a/source/search.h +++ b/source/search.h @@ -22,8 +22,6 @@ constexpr int MAX_HISTORY = 16384; constexpr int inf = 100000; -inline bool searchStopped = false; - // [id][ply] inline int killerMoves[2][MAX_PLY]; @@ -112,6 +110,7 @@ public: int timeToSearch = 0; bool doingNullMove = false; + bool searchStopped = false; PVLine pvLine; History history; @@ -130,7 +129,6 @@ SearchResults PVS(Board board, int depth, int alpha, int beta, int ply, SearchCo template SearchResults SearchPosition(Board &board, SearchParams params, SearchContext& ctx); -void StopSearch(); bool IsDraw(Board &board); diff --git a/source/uci.cpp b/source/uci.cpp index ad676b6..3097a92 100644 --- a/source/uci.cpp +++ b/source/uci.cpp @@ -159,7 +159,7 @@ void UCILoop(Board &board) { // parse UCI "stop" command if (input.find("stop") != std::string::npos) { // stop the loop - SEARCH::StopSearch(); + ctx.searchStopped = true; continue; } -- 2.47.3 From dd6f0b94f52230adf38a5e63c6e31fef8aec1584 Mon Sep 17 00:00:00 2001 From: rektdie Date: Fri, 11 Apr 2025 03:59:05 +0200 Subject: [PATCH 11/23] encapsulated killerMoves and changed it to std::array --- source/benchmark.cpp | 2 -- source/datagen.cpp | 2 -- source/search.cpp | 8 ++++---- source/search.h | 6 +++--- source/uci.cpp | 3 ++- 5 files changed, 9 insertions(+), 12 deletions(-) diff --git a/source/benchmark.cpp b/source/benchmark.cpp index 72e702c..4c24ac1 100644 --- a/source/benchmark.cpp +++ b/source/benchmark.cpp @@ -69,7 +69,6 @@ void RunBenchmark() { // Clearing TT.Clear(); - std::memset(killerMoves, 0, sizeof(killerMoves)); SearchContext ctx; @@ -82,6 +81,5 @@ void RunBenchmark() { std::cout << ctx.nodes << " nodes " << int(ctx.nodes/sw.GetElapsedSec()) << " nps" << std::endl; TT.Clear(); - std::memset(killerMoves, 0, sizeof(killerMoves)); positionIndex = 0; } diff --git a/source/datagen.cpp b/source/datagen.cpp index 6668f07..573f47a 100644 --- a/source/datagen.cpp +++ b/source/datagen.cpp @@ -102,8 +102,6 @@ void PlayGame(std::atomic& positions, U64 targetPositions, std::vector= beta) { if (!currMove.IsCapture()) { - killerMoves[1][ply] = killerMoves[0][ply]; - killerMoves[0][ply] = currMove; + ctx.killerMoves[1][ply] = ctx.killerMoves[0][ply]; + ctx.killerMoves[0][ply] = currMove; int bonus = depth * depth; diff --git a/source/search.h b/source/search.h index e897898..dd0ce78 100644 --- a/source/search.h +++ b/source/search.h @@ -22,9 +22,6 @@ constexpr int MAX_HISTORY = 16384; constexpr int inf = 100000; -// [id][ply] -inline int killerMoves[2][MAX_PLY]; - inline int lmrTable[MAX_DEPTH+1][MAX_MOVES]; void InitLMRTable(); @@ -115,12 +112,15 @@ public: PVLine pvLine; History history; + std::array, 2> killerMoves{}; + Stopwatch sw; SearchContext(){ pvLine.Clear(); history.Clear(); sw.Restart(); + killerMoves = {}; } }; diff --git a/source/uci.cpp b/source/uci.cpp index 3097a92..01e131a 100644 --- a/source/uci.cpp +++ b/source/uci.cpp @@ -136,9 +136,10 @@ void UCILoop(Board &board) { // Clearing TT.Clear(); - std::memset(SEARCH::killerMoves, 0, sizeof(SEARCH::killerMoves)); + ctx.killerMoves = {}; ctx.history.Clear(); + positionIndex = 0; continue; -- 2.47.3 From aa0d34eb8e358195609e37e35c8d069ab087f766 Mon Sep 17 00:00:00 2001 From: rektdie Date: Fri, 11 Apr 2025 06:19:17 +0200 Subject: [PATCH 12/23] encapsulated positionIndex and moved isdraw to board class --- source/benchmark.cpp | 4 ++-- source/board.cpp | 27 +++++++++++++++++++++++++++ source/board.h | 5 ++++- source/datagen.cpp | 6 +----- source/perft.cpp | 2 -- source/search.cpp | 29 +---------------------------- source/uci.cpp | 3 --- 7 files changed, 35 insertions(+), 41 deletions(-) diff --git a/source/benchmark.cpp b/source/benchmark.cpp index 4c24ac1..f18c0ab 100644 --- a/source/benchmark.cpp +++ b/source/benchmark.cpp @@ -65,7 +65,7 @@ void RunBenchmark() { Board board; int depth = BENCH_DEPTH; int ply = 0; - positionIndex = 0; + board.positionIndex = 0; // Clearing TT.Clear(); @@ -81,5 +81,5 @@ void RunBenchmark() { std::cout << ctx.nodes << " nodes " << int(ctx.nodes/sw.GetElapsedSec()) << " nps" << std::endl; TT.Clear(); - positionIndex = 0; + board.positionIndex = 0; } diff --git a/source/board.cpp b/source/board.cpp index eabfae5..96f8228 100644 --- a/source/board.cpp +++ b/source/board.cpp @@ -28,6 +28,8 @@ void Board::Reset() { currentMoveIndex = 0; + positionIndex = 0; + hashKey = 0ULL; } @@ -441,4 +443,29 @@ void Board::ResetAccPair() { blackPieces.PopBit(square); } +} + +static bool IsThreefold(Board& board) { + for (int i = 0; i < board.positionIndex; i++) { + if (positionHistory[i] == board.hashKey) { + // repetition found + return true; + } + } + + // no repetition + return false; +} + +static bool IsFifty(Board& board) { + return (board.halfMoves >= 100); +} + +static bool IsInsuffMat(Board& board) { + return (board.occupied.PopCount() <= 3 + && !(board.pieces[Pawn] | board.pieces[Queen] | board.pieces[Rook])); +} + +bool Board::IsDraw() { + return IsFifty(*this) || IsInsuffMat(*this) || IsThreefold(*this); } \ No newline at end of file diff --git a/source/board.h b/source/board.h index 5ac1cbc..ed841a9 100644 --- a/source/board.h +++ b/source/board.h @@ -10,7 +10,6 @@ constexpr int noEPTarget = -1; constexpr int MAX_MOVES = 218; -inline int positionIndex = 0; inline U64 positionHistory[1000]; class Board { @@ -40,6 +39,8 @@ public: int halfMoves = 0; int fullMoves = 1; + int positionIndex = 0; + U64 hashKey = 0ULL; void Reset(); @@ -66,4 +67,6 @@ public: bool MakeMove(Move move); bool InPossibleZug(); + + bool IsDraw(); }; diff --git a/source/datagen.cpp b/source/datagen.cpp index 573f47a..d4bd4f2 100644 --- a/source/datagen.cpp +++ b/source/datagen.cpp @@ -63,8 +63,7 @@ static void PlayRandMoves(Board &board) { } static bool IsGameOver(Board &board) { - bool isDraw = SEARCH::IsDraw(board); - if (isDraw) return true; + if (board.IsDraw()) return true; int moveSeen = 0; @@ -73,7 +72,6 @@ static bool IsGameOver(Board &board) { bool isLegal = copy.MakeMove(board.moveList[i]); if (!isLegal) continue; - positionIndex--; moveSeen++; } @@ -99,8 +97,6 @@ void PlayGame(std::atomic& positions, U64 targetPositions, std::vector= 100); -} - -static bool IsInsuffMat(Board &board) { - return (board.occupied.PopCount() <= 3 - && !(board.pieces[Pawn] | board.pieces[Queen] | board.pieces[Rook])); -} - -bool IsDraw(Board &board) { - return IsFifty(board) || IsInsuffMat(board) || IsThreefold(board); -} - static int GetReductions(Board &board, Move &move, int depth, int moveSeen, int ply) { int reduction = 0; @@ -174,7 +149,6 @@ static SearchResults Quiescence(Board board, int alpha, int beta, int ply, Searc if (!isLegal) continue; ctx.nodes++; int score = -Quiescence(copy, -beta, -alpha, ply + 1, ctx).score; - positionIndex--; if (score >= beta) { return score; @@ -217,7 +191,7 @@ SearchResults PVS(Board board, int depth, int alpha, int beta, int ply, SearchCo } ctx.pvLine.SetLength(ply); - if (ply && (IsThreefold(board) || IsFifty(board) || IsInsuffMat(board))) return 0; + if (ply && (board.IsDraw())) return 0; // if NOT PV node then we try to hit the TTable @@ -316,7 +290,6 @@ SearchResults PVS(Board board, int depth, int alpha, int beta, int ply, SearchCo } moveSeen++; - positionIndex--; if (ctx.searchStopped) return 0; diff --git a/source/uci.cpp b/source/uci.cpp index 01e131a..11b1370 100644 --- a/source/uci.cpp +++ b/source/uci.cpp @@ -125,7 +125,6 @@ void UCILoop(Board &board) { // parse UCI "position" command if (input.find("position") != std::string::npos) { ParsePosition(board, input); - positionIndex = 0; continue; } @@ -139,8 +138,6 @@ void UCILoop(Board &board) { ctx.killerMoves = {}; ctx.history.Clear(); - - positionIndex = 0; continue; } -- 2.47.3 From 2e8c23103fad7903ce9938ea002cb3eaab925c09 Mon Sep 17 00:00:00 2001 From: rektdie Date: Fri, 11 Apr 2025 19:49:03 +0200 Subject: [PATCH 13/23] encapsulated positionHistory | bench 1857933 --- source/benchmark.cpp | 2 -- source/board.cpp | 26 -------------------------- source/board.h | 4 ---- source/datagen.cpp | 6 +++--- source/search.cpp | 29 ++++++++++++++++++++++++++++- source/search.h | 4 +++- 6 files changed, 34 insertions(+), 37 deletions(-) diff --git a/source/benchmark.cpp b/source/benchmark.cpp index f18c0ab..3b1d635 100644 --- a/source/benchmark.cpp +++ b/source/benchmark.cpp @@ -65,7 +65,6 @@ void RunBenchmark() { Board board; int depth = BENCH_DEPTH; int ply = 0; - board.positionIndex = 0; // Clearing TT.Clear(); @@ -81,5 +80,4 @@ void RunBenchmark() { std::cout << ctx.nodes << " nodes " << int(ctx.nodes/sw.GetElapsedSec()) << " nps" << std::endl; TT.Clear(); - board.positionIndex = 0; } diff --git a/source/board.cpp b/source/board.cpp index 96f8228..695fc0c 100644 --- a/source/board.cpp +++ b/source/board.cpp @@ -393,7 +393,6 @@ bool Board::MakeMove(Move move) { } positionIndex++; - positionHistory[positionIndex] = hashKey; return true; } @@ -444,28 +443,3 @@ void Board::ResetAccPair() { blackPieces.PopBit(square); } } - -static bool IsThreefold(Board& board) { - for (int i = 0; i < board.positionIndex; i++) { - if (positionHistory[i] == board.hashKey) { - // repetition found - return true; - } - } - - // no repetition - return false; -} - -static bool IsFifty(Board& board) { - return (board.halfMoves >= 100); -} - -static bool IsInsuffMat(Board& board) { - return (board.occupied.PopCount() <= 3 - && !(board.pieces[Pawn] | board.pieces[Queen] | board.pieces[Rook])); -} - -bool Board::IsDraw() { - return IsFifty(*this) || IsInsuffMat(*this) || IsThreefold(*this); -} \ No newline at end of file diff --git a/source/board.h b/source/board.h index ed841a9..8c3667e 100644 --- a/source/board.h +++ b/source/board.h @@ -10,8 +10,6 @@ constexpr int noEPTarget = -1; constexpr int MAX_MOVES = 218; -inline U64 positionHistory[1000]; - class Board { public: Board() { @@ -67,6 +65,4 @@ public: bool MakeMove(Move move); bool InPossibleZug(); - - bool IsDraw(); }; diff --git a/source/datagen.cpp b/source/datagen.cpp index d4bd4f2..2e33f63 100644 --- a/source/datagen.cpp +++ b/source/datagen.cpp @@ -62,8 +62,8 @@ static void PlayRandMoves(Board &board) { MOVEGEN::GenerateMoves(board); } -static bool IsGameOver(Board &board) { - if (board.IsDraw()) return true; +static bool IsGameOver(Board &board, SEARCH::SearchContext& ctx) { + if (SEARCH::IsDraw(board, ctx)) return true; int moveSeen = 0; @@ -106,7 +106,7 @@ void PlayGame(std::atomic& positions, U64 targetPositions, std::vector(board); - while (!IsGameOver(board)) { + while (!IsGameOver(board, ctx)) { SEARCH::SearchResults results = SEARCH::SearchPosition(board, SearchParams(), ctx); if (!results.bestMove) break; diff --git a/source/search.cpp b/source/search.cpp index 60d7647..3104a68 100644 --- a/source/search.cpp +++ b/source/search.cpp @@ -92,6 +92,31 @@ void ListScores(Board &board, int ply, SearchContext& ctx) { } } +static bool IsThreefold(Board& board, SearchContext& ctx) { + for (int i = 0; i < board.positionIndex; i++) { + if (ctx.positionHistory[i] == board.hashKey) { + // repetition found + return true; + } + } + + // no repetition + return false; +} + +static bool IsFifty(Board& board) { + return (board.halfMoves >= 100); +} + +static bool IsInsuffMat(Board& board) { + return (board.occupied.PopCount() <= 3 + && !(board.pieces[Pawn] | board.pieces[Queen] | board.pieces[Rook])); +} + +static bool IsDraw(Board &board, SearchContext& ctx) { + return IsFifty(board) || IsInsuffMat(board) || IsThreefold(board, ctx); +} + static int GetReductions(Board &board, Move &move, int depth, int moveSeen, int ply) { int reduction = 0; @@ -147,6 +172,7 @@ static SearchResults Quiescence(Board board, int alpha, int beta, int ply, Searc Board copy = board; int isLegal = copy.MakeMove(board.moveList[i]); if (!isLegal) continue; + ctx.positionHistory[copy.positionIndex] = copy.hashKey; ctx.nodes++; int score = -Quiescence(copy, -beta, -alpha, ply + 1, ctx).score; @@ -191,7 +217,7 @@ SearchResults PVS(Board board, int depth, int alpha, int beta, int ply, SearchCo } ctx.pvLine.SetLength(ply); - if (ply && (board.IsDraw())) return 0; + if (ply && (IsDraw(board, ctx))) return 0; // if NOT PV node then we try to hit the TTable @@ -261,6 +287,7 @@ SearchResults PVS(Board board, int depth, int alpha, int beta, int ply, SearchCo bool isLegal = copy.MakeMove(currMove); if (!isLegal) continue; + ctx.positionHistory[copy.positionIndex] = copy.hashKey; ctx.nodes++; int reductions = GetReductions(board, currMove, depth, moveSeen, ply); diff --git a/source/search.h b/source/search.h index dd0ce78..5eaea00 100644 --- a/source/search.h +++ b/source/search.h @@ -113,6 +113,7 @@ public: History history; std::array, 2> killerMoves{}; + std::array positionHistory; Stopwatch sw; @@ -121,6 +122,7 @@ public: history.Clear(); sw.Restart(); killerMoves = {}; + positionHistory = {}; } }; @@ -130,7 +132,7 @@ SearchResults PVS(Board board, int depth, int alpha, int beta, int ply, SearchCo template SearchResults SearchPosition(Board &board, SearchParams params, SearchContext& ctx); -bool IsDraw(Board &board); +bool IsDraw(Board &board, SearchContext& ctx); constexpr int moveScoreTable[6][6] = { 105, 205, 305, 405, 505, 605, -- 2.47.3 From 7253d4f978f0693c28f44cb048e91f4abb6bc772 Mon Sep 17 00:00:00 2001 From: rektdie Date: Fri, 11 Apr 2025 21:16:11 +0200 Subject: [PATCH 14/23] passing board as reference in PVS --- source/search.cpp | 16 ++++++++-------- source/search.h | 2 +- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/source/search.cpp b/source/search.cpp index 3104a68..ec655df 100644 --- a/source/search.cpp +++ b/source/search.cpp @@ -129,7 +129,7 @@ static int GetReductions(Board &board, Move &move, int depth, int moveSeen, int } template -static SearchResults Quiescence(Board board, int alpha, int beta, int ply, SearchContext& ctx) { +static SearchResults Quiescence(Board& board, int alpha, int beta, int ply, SearchContext& ctx) { if constexpr (mode != nodesMode) { if constexpr (mode != bench) { if (ctx.nodes % 1024 == 0) { @@ -194,7 +194,7 @@ static SearchResults Quiescence(Board board, int alpha, int beta, int ply, Searc } template -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 (ctx.nodes % 1024 == 0) { if constexpr (mode == normal) { @@ -477,12 +477,12 @@ SearchResults SearchPosition(Board &board, SearchParams params, SearchContext& c return results; } -template SearchResults PVS(Board, int, int, int, int, SearchContext& ctx); -template SearchResults PVS(Board, int, int, int, int, SearchContext& ctx); -template SearchResults PVS(Board, int, int, int, int, SearchContext& ctx); -template SearchResults PVS(Board, int, int, int, int, SearchContext& ctx); -template SearchResults PVS(Board, int, int, int, int, SearchContext& ctx); -template SearchResults PVS(Board, int, int, int, int, SearchContext& ctx); +template SearchResults PVS(Board&, int, int, int, int, SearchContext& ctx); +template SearchResults PVS(Board&, int, int, int, int, SearchContext& ctx); +template SearchResults PVS(Board&, int, int, int, int, SearchContext& ctx); +template SearchResults PVS(Board&, int, int, int, int, SearchContext& ctx); +template SearchResults PVS(Board&, int, int, int, int, SearchContext& ctx); +template SearchResults PVS(Board&, int, int, int, int, SearchContext& ctx); template SearchResults SearchPosition(Board &board, SearchParams params, SearchContext& ctx); template SearchResults SearchPosition(Board &board, SearchParams params, SearchContext& ctx); diff --git a/source/search.h b/source/search.h index 5eaea00..047d3b3 100644 --- a/source/search.h +++ b/source/search.h @@ -127,7 +127,7 @@ public: }; template -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); template SearchResults SearchPosition(Board &board, SearchParams params, SearchContext& ctx); -- 2.47.3 From e7079c3c736c75965fca68fa11e05af7b8291a0e Mon Sep 17 00:00:00 2001 From: rektdie Date: Fri, 11 Apr 2025 21:32:03 +0200 Subject: [PATCH 15/23] bugfix related to function declaration --- source/search.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/source/search.cpp b/source/search.cpp index ec655df..7edce3c 100644 --- a/source/search.cpp +++ b/source/search.cpp @@ -113,7 +113,7 @@ static bool IsInsuffMat(Board& board) { && !(board.pieces[Pawn] | board.pieces[Queen] | board.pieces[Rook])); } -static bool IsDraw(Board &board, SearchContext& ctx) { +bool IsDraw(Board &board, SearchContext& ctx) { return IsFifty(board) || IsInsuffMat(board) || IsThreefold(board, ctx); } -- 2.47.3 From 8ca878275a9557c3d6effede693927aaa46f8ee1 Mon Sep 17 00:00:00 2001 From: rektdie Date: Fri, 11 Apr 2025 22:28:16 +0200 Subject: [PATCH 16/23] encapsulated TT --- source/benchmark.cpp | 6 ------ source/board.cpp | 1 - source/search.cpp | 25 +++++++++++++++++++------ source/search.h | 12 +++++------- source/tt.h | 2 -- source/uci.cpp | 8 ++++---- 6 files changed, 28 insertions(+), 26 deletions(-) diff --git a/source/benchmark.cpp b/source/benchmark.cpp index 3b1d635..2ff4618 100644 --- a/source/benchmark.cpp +++ b/source/benchmark.cpp @@ -4,7 +4,6 @@ #include "benchmark.h" #include "search.h" #include "stopwatch.h" -#include "tt.h" using namespace SEARCH; @@ -66,12 +65,8 @@ void RunBenchmark() { int depth = BENCH_DEPTH; int ply = 0; - // Clearing - TT.Clear(); - SearchContext ctx; - Stopwatch sw; for (int i = 0; i < fenPositions.size(); i++) { board.SetByFen(fenPositions[i]); @@ -79,5 +74,4 @@ void RunBenchmark() { } std::cout << ctx.nodes << " nodes " << int(ctx.nodes/sw.GetElapsedSec()) << " nps" << std::endl; - TT.Clear(); } diff --git a/source/board.cpp b/source/board.cpp index 695fc0c..8f2449b 100644 --- a/source/board.cpp +++ b/source/board.cpp @@ -1,6 +1,5 @@ #include "board.h" #include "movegen.h" -#include "tt.h" #include #include #include diff --git a/source/search.cpp b/source/search.cpp index 7edce3c..40393ea 100644 --- a/source/search.cpp +++ b/source/search.cpp @@ -9,6 +9,19 @@ namespace SEARCH { +SearchContext::SearchContext() { + pvLine.Clear(); + history.Clear(); + TT = new TTable(); + sw.Restart(); + killerMoves = {}; + positionHistory = {}; +} + +SearchContext::~SearchContext() { + delete TT; +} + void InitLMRTable() { for (int depth = 0; depth <= MAX_DEPTH; depth++) { for (int move = 0; move < MAX_MOVES; move++) { @@ -25,7 +38,7 @@ void InitLMRTable() { } static int ScoreMove(Board &board, Move &move, int ply, SearchContext& ctx) { - TTEntry *current = TT.GetRawEntry(board.hashKey); + TTEntry *current = ctx.TT->GetRawEntry(board.hashKey); if (current->bestMove == move) { return 50000; } @@ -148,7 +161,7 @@ static SearchResults Quiescence(Board& board, int alpha, int beta, int ply, Sear int bestScore = HCE::Evaluate(board); - TTEntry *entry = TT.GetRawEntry(board.hashKey); + TTEntry *entry = ctx.TT->GetRawEntry(board.hashKey); if (entry->hashKey == board.hashKey) { bestScore = entry->score; } @@ -222,7 +235,7 @@ SearchResults PVS(Board& board, int depth, int alpha, int beta, int ply, SearchC // if NOT PV node then we try to hit the TTable if constexpr (!isPV) { - SearchResults entry = TT.ReadEntry(board.hashKey, depth, alpha, beta); + SearchResults entry = ctx.TT->ReadEntry(board.hashKey, depth, alpha, beta); if (entry.score != invalidEntry) { return entry; } @@ -340,7 +353,7 @@ SearchResults PVS(Board& board, int depth, int alpha, int beta, int ply, SearchC } } - TT.WriteEntry(board.hashKey, depth, score, CutNode, Move()); + ctx.TT->WriteEntry(board.hashKey, depth, score, CutNode, Move()); return score; } @@ -363,7 +376,7 @@ SearchResults PVS(Board& board, int depth, int alpha, int beta, int ply, SearchC } if (ctx.searchStopped) return 0; - TT.WriteEntry(board.hashKey, depth, results.score, nodeType, results.bestMove); + ctx.TT->WriteEntry(board.hashKey, depth, results.score, nodeType, results.bestMove); return results; } @@ -431,7 +444,7 @@ static SearchResults ID(Board &board, SearchParams params, SearchContext& ctx) { std::cout << " time " << elapsed; std::cout << " score cp " << UTILS::ConvertToWhiteRelative(board, safeResults.score); std::cout << " nodes " << ctx.nodes << " nps " << int(ctx.nodes/ctx.sw.GetElapsedSec()); - std::cout << " hashfull " << TT.GetUsedPercentage(); + std::cout << " hashfull " << ctx.TT->GetUsedPercentage(); std::cout << " pv "; ctx.pvLine.Print(0); std::cout << std::endl; diff --git a/source/search.h b/source/search.h index 047d3b3..9c16162 100644 --- a/source/search.h +++ b/source/search.h @@ -6,6 +6,8 @@ #include #include "stopwatch.h" +class TTable; + namespace SEARCH { enum searchMode { @@ -111,19 +113,15 @@ public: PVLine pvLine; History history; + TTable* TT; std::array, 2> killerMoves{}; std::array positionHistory; Stopwatch sw; - SearchContext(){ - pvLine.Clear(); - history.Clear(); - sw.Restart(); - killerMoves = {}; - positionHistory = {}; - } + SearchContext(); + ~SearchContext(); }; template diff --git a/source/tt.h b/source/tt.h index e8607c4..7b9897e 100644 --- a/source/tt.h +++ b/source/tt.h @@ -60,5 +60,3 @@ public: void WriteEntry(U64 &hashKey, int depth, int score, int nodeType, Move besteMove); }; - -inline TTable TT; diff --git a/source/uci.cpp b/source/uci.cpp index 11b1370..8943d0f 100644 --- a/source/uci.cpp +++ b/source/uci.cpp @@ -92,10 +92,10 @@ static void ParseGo(Board &board, std::string &command, SEARCH::SearchContext& c } } -static void SetOption(std::string &command) { +static void SetOption(std::string &command, SEARCH::SearchContext& ctx) { if (command.find("Hash") != std::string::npos) { hashSize = (ReadParam("value", command) * 1000000) / sizeof(TTEntry); - TT.Resize(hashSize); + ctx.TT->Resize(hashSize); } } @@ -134,7 +134,7 @@ void UCILoop(Board &board) { board.SetByFen(StartingFen); // Clearing - TT.Clear(); + ctx.TT->Clear(); ctx.killerMoves = {}; ctx.history.Clear(); @@ -169,7 +169,7 @@ void UCILoop(Board &board) { } if (input.find("setoption") != std::string::npos) { - SetOption(input); + SetOption(input, ctx); continue; } -- 2.47.3 From 3aad0d90ee7e5ae7936bf247c470091ae1e20d4f Mon Sep 17 00:00:00 2001 From: rektdie Date: Fri, 11 Apr 2025 23:41:26 +0200 Subject: [PATCH 17/23] removed hashSize global | bench 1857933 --- source/tt.cpp | 4 ++-- source/tt.h | 9 ++++----- source/uci.cpp | 2 +- 3 files changed, 7 insertions(+), 8 deletions(-) diff --git a/source/tt.cpp b/source/tt.cpp index 3df1649..85d83c6 100644 --- a/source/tt.cpp +++ b/source/tt.cpp @@ -1,7 +1,7 @@ #include "tt.h" SEARCH::SearchResults TTable::ReadEntry(U64 &hashKey, int depth, int alpha, int beta) { - TTEntry *current = &table[hashKey % hashSize]; + TTEntry *current = &table[hashKey % table.size()]; if (current->hashKey == hashKey) { if (current->depth >= depth) { @@ -20,7 +20,7 @@ SEARCH::SearchResults TTable::ReadEntry(U64 &hashKey, int depth, int alpha, int } void TTable::WriteEntry(U64 &hashKey, int depth, int score, int nodeType, Move bestMove) { - TTEntry *current = &table[hashKey % hashSize]; + TTEntry *current = &table[hashKey % table.size()]; current->hashKey = hashKey; current->nodeType = nodeType; diff --git a/source/tt.h b/source/tt.h index 7b9897e..2e8e468 100644 --- a/source/tt.h +++ b/source/tt.h @@ -21,27 +21,26 @@ constexpr U64 maxHash = (64* 1000000) / sizeof(TTEntry); // 5 MB constexpr U64 defaultHash = (5 * 1000000) / sizeof(TTEntry); -inline U64 hashSize = defaultHash; - constexpr int invalidEntry = 111111; class TTable { private: std::vector table; public: - TTable() : table(hashSize) {}; + TTable() : table(defaultHash) {}; void Resize(U64 size) { table.resize(size); } void Clear() { + U64 size = table.size(); table.clear(); - table.resize(hashSize); + table.resize(size); } TTEntry* GetRawEntry(U64 &hashKey) { - TTEntry *current = &table[hashKey % hashSize]; + TTEntry *current = &table[hashKey % table.size()]; return current; } diff --git a/source/uci.cpp b/source/uci.cpp index 8943d0f..ba89003 100644 --- a/source/uci.cpp +++ b/source/uci.cpp @@ -94,7 +94,7 @@ static void ParseGo(Board &board, std::string &command, SEARCH::SearchContext& c static void SetOption(std::string &command, SEARCH::SearchContext& ctx) { if (command.find("Hash") != std::string::npos) { - hashSize = (ReadParam("value", command) * 1000000) / sizeof(TTEntry); + U64 hashSize = (ReadParam("value", command) * 1000000) / sizeof(TTEntry); ctx.TT->Resize(hashSize); } } -- 2.47.3 From 0242eec87d23acf6f5b0275bbce4fe03899b7114 Mon Sep 17 00:00:00 2001 From: rektdie Date: Sat, 12 Apr 2025 00:35:30 +0200 Subject: [PATCH 18/23] Revert "removed hashSize global | bench 1857933" This reverts commit 3aad0d90ee7e5ae7936bf247c470091ae1e20d4f. --- source/tt.cpp | 4 ++-- source/tt.h | 9 +++++---- source/uci.cpp | 2 +- 3 files changed, 8 insertions(+), 7 deletions(-) diff --git a/source/tt.cpp b/source/tt.cpp index 85d83c6..3df1649 100644 --- a/source/tt.cpp +++ b/source/tt.cpp @@ -1,7 +1,7 @@ #include "tt.h" SEARCH::SearchResults TTable::ReadEntry(U64 &hashKey, int depth, int alpha, int beta) { - TTEntry *current = &table[hashKey % table.size()]; + TTEntry *current = &table[hashKey % hashSize]; if (current->hashKey == hashKey) { if (current->depth >= depth) { @@ -20,7 +20,7 @@ SEARCH::SearchResults TTable::ReadEntry(U64 &hashKey, int depth, int alpha, int } void TTable::WriteEntry(U64 &hashKey, int depth, int score, int nodeType, Move bestMove) { - TTEntry *current = &table[hashKey % table.size()]; + TTEntry *current = &table[hashKey % hashSize]; current->hashKey = hashKey; current->nodeType = nodeType; diff --git a/source/tt.h b/source/tt.h index 2e8e468..7b9897e 100644 --- a/source/tt.h +++ b/source/tt.h @@ -21,26 +21,27 @@ constexpr U64 maxHash = (64* 1000000) / sizeof(TTEntry); // 5 MB constexpr U64 defaultHash = (5 * 1000000) / sizeof(TTEntry); +inline U64 hashSize = defaultHash; + constexpr int invalidEntry = 111111; class TTable { private: std::vector table; public: - TTable() : table(defaultHash) {}; + TTable() : table(hashSize) {}; void Resize(U64 size) { table.resize(size); } void Clear() { - U64 size = table.size(); table.clear(); - table.resize(size); + table.resize(hashSize); } TTEntry* GetRawEntry(U64 &hashKey) { - TTEntry *current = &table[hashKey % table.size()]; + TTEntry *current = &table[hashKey % hashSize]; return current; } diff --git a/source/uci.cpp b/source/uci.cpp index ba89003..8943d0f 100644 --- a/source/uci.cpp +++ b/source/uci.cpp @@ -94,7 +94,7 @@ static void ParseGo(Board &board, std::string &command, SEARCH::SearchContext& c static void SetOption(std::string &command, SEARCH::SearchContext& ctx) { if (command.find("Hash") != std::string::npos) { - U64 hashSize = (ReadParam("value", command) * 1000000) / sizeof(TTEntry); + hashSize = (ReadParam("value", command) * 1000000) / sizeof(TTEntry); ctx.TT->Resize(hashSize); } } -- 2.47.3 From 43dc63cdf3425c47d8304c46997282dc1ee16299 Mon Sep 17 00:00:00 2001 From: rektdie Date: Sat, 12 Apr 2025 00:52:52 +0200 Subject: [PATCH 19/23] Revert "encapsulated TT" This reverts commit 8ca878275a9557c3d6effede693927aaa46f8ee1. --- source/benchmark.cpp | 6 ++++++ source/board.cpp | 1 + source/search.cpp | 25 ++++++------------------- source/search.h | 12 +++++++----- source/tt.h | 2 ++ source/uci.cpp | 8 ++++---- 6 files changed, 26 insertions(+), 28 deletions(-) diff --git a/source/benchmark.cpp b/source/benchmark.cpp index 2ff4618..3b1d635 100644 --- a/source/benchmark.cpp +++ b/source/benchmark.cpp @@ -4,6 +4,7 @@ #include "benchmark.h" #include "search.h" #include "stopwatch.h" +#include "tt.h" using namespace SEARCH; @@ -65,8 +66,12 @@ void RunBenchmark() { int depth = BENCH_DEPTH; int ply = 0; + // Clearing + TT.Clear(); + SearchContext ctx; + Stopwatch sw; for (int i = 0; i < fenPositions.size(); i++) { board.SetByFen(fenPositions[i]); @@ -74,4 +79,5 @@ void RunBenchmark() { } std::cout << ctx.nodes << " nodes " << int(ctx.nodes/sw.GetElapsedSec()) << " nps" << std::endl; + TT.Clear(); } diff --git a/source/board.cpp b/source/board.cpp index 8f2449b..695fc0c 100644 --- a/source/board.cpp +++ b/source/board.cpp @@ -1,5 +1,6 @@ #include "board.h" #include "movegen.h" +#include "tt.h" #include #include #include diff --git a/source/search.cpp b/source/search.cpp index 40393ea..7edce3c 100644 --- a/source/search.cpp +++ b/source/search.cpp @@ -9,19 +9,6 @@ namespace SEARCH { -SearchContext::SearchContext() { - pvLine.Clear(); - history.Clear(); - TT = new TTable(); - sw.Restart(); - killerMoves = {}; - positionHistory = {}; -} - -SearchContext::~SearchContext() { - delete TT; -} - void InitLMRTable() { for (int depth = 0; depth <= MAX_DEPTH; depth++) { for (int move = 0; move < MAX_MOVES; move++) { @@ -38,7 +25,7 @@ void InitLMRTable() { } static int ScoreMove(Board &board, Move &move, int ply, SearchContext& ctx) { - TTEntry *current = ctx.TT->GetRawEntry(board.hashKey); + TTEntry *current = TT.GetRawEntry(board.hashKey); if (current->bestMove == move) { return 50000; } @@ -161,7 +148,7 @@ static SearchResults Quiescence(Board& board, int alpha, int beta, int ply, Sear int bestScore = HCE::Evaluate(board); - TTEntry *entry = ctx.TT->GetRawEntry(board.hashKey); + TTEntry *entry = TT.GetRawEntry(board.hashKey); if (entry->hashKey == board.hashKey) { bestScore = entry->score; } @@ -235,7 +222,7 @@ SearchResults PVS(Board& board, int depth, int alpha, int beta, int ply, SearchC // if NOT PV node then we try to hit the TTable if constexpr (!isPV) { - SearchResults entry = ctx.TT->ReadEntry(board.hashKey, depth, alpha, beta); + SearchResults entry = TT.ReadEntry(board.hashKey, depth, alpha, beta); if (entry.score != invalidEntry) { return entry; } @@ -353,7 +340,7 @@ SearchResults PVS(Board& board, int depth, int alpha, int beta, int ply, SearchC } } - ctx.TT->WriteEntry(board.hashKey, depth, score, CutNode, Move()); + TT.WriteEntry(board.hashKey, depth, score, CutNode, Move()); return score; } @@ -376,7 +363,7 @@ SearchResults PVS(Board& board, int depth, int alpha, int beta, int ply, SearchC } if (ctx.searchStopped) return 0; - ctx.TT->WriteEntry(board.hashKey, depth, results.score, nodeType, results.bestMove); + TT.WriteEntry(board.hashKey, depth, results.score, nodeType, results.bestMove); return results; } @@ -444,7 +431,7 @@ static SearchResults ID(Board &board, SearchParams params, SearchContext& ctx) { std::cout << " time " << elapsed; std::cout << " score cp " << UTILS::ConvertToWhiteRelative(board, safeResults.score); std::cout << " nodes " << ctx.nodes << " nps " << int(ctx.nodes/ctx.sw.GetElapsedSec()); - std::cout << " hashfull " << ctx.TT->GetUsedPercentage(); + std::cout << " hashfull " << TT.GetUsedPercentage(); std::cout << " pv "; ctx.pvLine.Print(0); std::cout << std::endl; diff --git a/source/search.h b/source/search.h index 9c16162..047d3b3 100644 --- a/source/search.h +++ b/source/search.h @@ -6,8 +6,6 @@ #include #include "stopwatch.h" -class TTable; - namespace SEARCH { enum searchMode { @@ -113,15 +111,19 @@ public: PVLine pvLine; History history; - TTable* TT; std::array, 2> killerMoves{}; std::array positionHistory; Stopwatch sw; - SearchContext(); - ~SearchContext(); + SearchContext(){ + pvLine.Clear(); + history.Clear(); + sw.Restart(); + killerMoves = {}; + positionHistory = {}; + } }; template diff --git a/source/tt.h b/source/tt.h index 7b9897e..e8607c4 100644 --- a/source/tt.h +++ b/source/tt.h @@ -60,3 +60,5 @@ public: void WriteEntry(U64 &hashKey, int depth, int score, int nodeType, Move besteMove); }; + +inline TTable TT; diff --git a/source/uci.cpp b/source/uci.cpp index 8943d0f..11b1370 100644 --- a/source/uci.cpp +++ b/source/uci.cpp @@ -92,10 +92,10 @@ static void ParseGo(Board &board, std::string &command, SEARCH::SearchContext& c } } -static void SetOption(std::string &command, SEARCH::SearchContext& ctx) { +static void SetOption(std::string &command) { if (command.find("Hash") != std::string::npos) { hashSize = (ReadParam("value", command) * 1000000) / sizeof(TTEntry); - ctx.TT->Resize(hashSize); + TT.Resize(hashSize); } } @@ -134,7 +134,7 @@ void UCILoop(Board &board) { board.SetByFen(StartingFen); // Clearing - ctx.TT->Clear(); + TT.Clear(); ctx.killerMoves = {}; ctx.history.Clear(); @@ -169,7 +169,7 @@ void UCILoop(Board &board) { } if (input.find("setoption") != std::string::npos) { - SetOption(input, ctx); + SetOption(input); continue; } -- 2.47.3 From dc729a3bd5a1579389f2a274c89b3af4da4ac278 Mon Sep 17 00:00:00 2001 From: rektdie Date: Sat, 12 Apr 2025 10:58:32 +0200 Subject: [PATCH 20/23] Revert "bugfix related to function declaration" This reverts commit e7079c3c736c75965fca68fa11e05af7b8291a0e. --- source/search.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/source/search.cpp b/source/search.cpp index 7edce3c..ec655df 100644 --- a/source/search.cpp +++ b/source/search.cpp @@ -113,7 +113,7 @@ static bool IsInsuffMat(Board& board) { && !(board.pieces[Pawn] | board.pieces[Queen] | board.pieces[Rook])); } -bool IsDraw(Board &board, SearchContext& ctx) { +static bool IsDraw(Board &board, SearchContext& ctx) { return IsFifty(board) || IsInsuffMat(board) || IsThreefold(board, ctx); } -- 2.47.3 From ce02841a5c6ee759b881e63e3495dac46dca2d77 Mon Sep 17 00:00:00 2001 From: rektdie Date: Sat, 12 Apr 2025 10:58:43 +0200 Subject: [PATCH 21/23] Revert "passing board as reference in PVS" This reverts commit 7253d4f978f0693c28f44cb048e91f4abb6bc772. --- source/search.cpp | 16 ++++++++-------- source/search.h | 2 +- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/source/search.cpp b/source/search.cpp index ec655df..3104a68 100644 --- a/source/search.cpp +++ b/source/search.cpp @@ -129,7 +129,7 @@ static int GetReductions(Board &board, Move &move, int depth, int moveSeen, int } template -static SearchResults Quiescence(Board& board, int alpha, int beta, int ply, SearchContext& ctx) { +static SearchResults Quiescence(Board board, int alpha, int beta, int ply, SearchContext& ctx) { if constexpr (mode != nodesMode) { if constexpr (mode != bench) { if (ctx.nodes % 1024 == 0) { @@ -194,7 +194,7 @@ static SearchResults Quiescence(Board& board, int alpha, int beta, int ply, Sear } template -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 (ctx.nodes % 1024 == 0) { if constexpr (mode == normal) { @@ -477,12 +477,12 @@ SearchResults SearchPosition(Board &board, SearchParams params, SearchContext& c return results; } -template SearchResults PVS(Board&, int, int, int, int, SearchContext& ctx); -template SearchResults PVS(Board&, int, int, int, int, SearchContext& ctx); -template SearchResults PVS(Board&, int, int, int, int, SearchContext& ctx); -template SearchResults PVS(Board&, int, int, int, int, SearchContext& ctx); -template SearchResults PVS(Board&, int, int, int, int, SearchContext& ctx); -template SearchResults PVS(Board&, int, int, int, int, SearchContext& ctx); +template SearchResults PVS(Board, int, int, int, int, SearchContext& ctx); +template SearchResults PVS(Board, int, int, int, int, SearchContext& ctx); +template SearchResults PVS(Board, int, int, int, int, SearchContext& ctx); +template SearchResults PVS(Board, int, int, int, int, SearchContext& ctx); +template SearchResults PVS(Board, int, int, int, int, SearchContext& ctx); +template SearchResults PVS(Board, int, int, int, int, SearchContext& ctx); template SearchResults SearchPosition(Board &board, SearchParams params, SearchContext& ctx); template SearchResults SearchPosition(Board &board, SearchParams params, SearchContext& ctx); diff --git a/source/search.h b/source/search.h index 047d3b3..5eaea00 100644 --- a/source/search.h +++ b/source/search.h @@ -127,7 +127,7 @@ public: }; template -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); template SearchResults SearchPosition(Board &board, SearchParams params, SearchContext& ctx); -- 2.47.3 From 3275cee01e3b753ab9690664570a2c7c3bc30c61 Mon Sep 17 00:00:00 2001 From: rektdie Date: Sat, 12 Apr 2025 10:58:56 +0200 Subject: [PATCH 22/23] Revert "encapsulated positionHistory | bench 1857933" This reverts commit 2e8c23103fad7903ce9938ea002cb3eaab925c09. --- source/benchmark.cpp | 2 ++ source/board.cpp | 26 ++++++++++++++++++++++++++ source/board.h | 4 ++++ source/datagen.cpp | 6 +++--- source/search.cpp | 29 +---------------------------- source/search.h | 4 +--- 6 files changed, 37 insertions(+), 34 deletions(-) diff --git a/source/benchmark.cpp b/source/benchmark.cpp index 3b1d635..f18c0ab 100644 --- a/source/benchmark.cpp +++ b/source/benchmark.cpp @@ -65,6 +65,7 @@ void RunBenchmark() { Board board; int depth = BENCH_DEPTH; int ply = 0; + board.positionIndex = 0; // Clearing TT.Clear(); @@ -80,4 +81,5 @@ void RunBenchmark() { std::cout << ctx.nodes << " nodes " << int(ctx.nodes/sw.GetElapsedSec()) << " nps" << std::endl; TT.Clear(); + board.positionIndex = 0; } diff --git a/source/board.cpp b/source/board.cpp index 695fc0c..96f8228 100644 --- a/source/board.cpp +++ b/source/board.cpp @@ -393,6 +393,7 @@ bool Board::MakeMove(Move move) { } positionIndex++; + positionHistory[positionIndex] = hashKey; return true; } @@ -443,3 +444,28 @@ void Board::ResetAccPair() { blackPieces.PopBit(square); } } + +static bool IsThreefold(Board& board) { + for (int i = 0; i < board.positionIndex; i++) { + if (positionHistory[i] == board.hashKey) { + // repetition found + return true; + } + } + + // no repetition + return false; +} + +static bool IsFifty(Board& board) { + return (board.halfMoves >= 100); +} + +static bool IsInsuffMat(Board& board) { + return (board.occupied.PopCount() <= 3 + && !(board.pieces[Pawn] | board.pieces[Queen] | board.pieces[Rook])); +} + +bool Board::IsDraw() { + return IsFifty(*this) || IsInsuffMat(*this) || IsThreefold(*this); +} \ No newline at end of file diff --git a/source/board.h b/source/board.h index 8c3667e..ed841a9 100644 --- a/source/board.h +++ b/source/board.h @@ -10,6 +10,8 @@ constexpr int noEPTarget = -1; constexpr int MAX_MOVES = 218; +inline U64 positionHistory[1000]; + class Board { public: Board() { @@ -65,4 +67,6 @@ public: bool MakeMove(Move move); bool InPossibleZug(); + + bool IsDraw(); }; diff --git a/source/datagen.cpp b/source/datagen.cpp index 2e33f63..d4bd4f2 100644 --- a/source/datagen.cpp +++ b/source/datagen.cpp @@ -62,8 +62,8 @@ static void PlayRandMoves(Board &board) { MOVEGEN::GenerateMoves(board); } -static bool IsGameOver(Board &board, SEARCH::SearchContext& ctx) { - if (SEARCH::IsDraw(board, ctx)) return true; +static bool IsGameOver(Board &board) { + if (board.IsDraw()) return true; int moveSeen = 0; @@ -106,7 +106,7 @@ void PlayGame(std::atomic& positions, U64 targetPositions, std::vector(board); - while (!IsGameOver(board, ctx)) { + while (!IsGameOver(board)) { SEARCH::SearchResults results = SEARCH::SearchPosition(board, SearchParams(), ctx); if (!results.bestMove) break; diff --git a/source/search.cpp b/source/search.cpp index 3104a68..60d7647 100644 --- a/source/search.cpp +++ b/source/search.cpp @@ -92,31 +92,6 @@ void ListScores(Board &board, int ply, SearchContext& ctx) { } } -static bool IsThreefold(Board& board, SearchContext& ctx) { - for (int i = 0; i < board.positionIndex; i++) { - if (ctx.positionHistory[i] == board.hashKey) { - // repetition found - return true; - } - } - - // no repetition - return false; -} - -static bool IsFifty(Board& board) { - return (board.halfMoves >= 100); -} - -static bool IsInsuffMat(Board& board) { - return (board.occupied.PopCount() <= 3 - && !(board.pieces[Pawn] | board.pieces[Queen] | board.pieces[Rook])); -} - -static bool IsDraw(Board &board, SearchContext& ctx) { - return IsFifty(board) || IsInsuffMat(board) || IsThreefold(board, ctx); -} - static int GetReductions(Board &board, Move &move, int depth, int moveSeen, int ply) { int reduction = 0; @@ -172,7 +147,6 @@ static SearchResults Quiescence(Board board, int alpha, int beta, int ply, Searc Board copy = board; int isLegal = copy.MakeMove(board.moveList[i]); if (!isLegal) continue; - ctx.positionHistory[copy.positionIndex] = copy.hashKey; ctx.nodes++; int score = -Quiescence(copy, -beta, -alpha, ply + 1, ctx).score; @@ -217,7 +191,7 @@ SearchResults PVS(Board board, int depth, int alpha, int beta, int ply, SearchCo } ctx.pvLine.SetLength(ply); - if (ply && (IsDraw(board, ctx))) return 0; + if (ply && (board.IsDraw())) return 0; // if NOT PV node then we try to hit the TTable @@ -287,7 +261,6 @@ SearchResults PVS(Board board, int depth, int alpha, int beta, int ply, SearchCo bool isLegal = copy.MakeMove(currMove); if (!isLegal) continue; - ctx.positionHistory[copy.positionIndex] = copy.hashKey; ctx.nodes++; int reductions = GetReductions(board, currMove, depth, moveSeen, ply); diff --git a/source/search.h b/source/search.h index 5eaea00..dd0ce78 100644 --- a/source/search.h +++ b/source/search.h @@ -113,7 +113,6 @@ public: History history; std::array, 2> killerMoves{}; - std::array positionHistory; Stopwatch sw; @@ -122,7 +121,6 @@ public: history.Clear(); sw.Restart(); killerMoves = {}; - positionHistory = {}; } }; @@ -132,7 +130,7 @@ SearchResults PVS(Board board, int depth, int alpha, int beta, int ply, SearchCo template SearchResults SearchPosition(Board &board, SearchParams params, SearchContext& ctx); -bool IsDraw(Board &board, SearchContext& ctx); +bool IsDraw(Board &board); constexpr int moveScoreTable[6][6] = { 105, 205, 305, 405, 505, 605, -- 2.47.3 From ce01c7756b1cd2afb9377bca81f8d4cc766f4d57 Mon Sep 17 00:00:00 2001 From: rektdie Date: Sat, 12 Apr 2025 10:59:04 +0200 Subject: [PATCH 23/23] Revert "encapsulated positionIndex and moved isdraw to board class" This reverts commit aa0d34eb8e358195609e37e35c8d069ab087f766. --- source/benchmark.cpp | 4 ++-- source/board.cpp | 27 --------------------------- source/board.h | 5 +---- source/datagen.cpp | 6 +++++- source/perft.cpp | 2 ++ source/search.cpp | 29 ++++++++++++++++++++++++++++- source/uci.cpp | 3 +++ 7 files changed, 41 insertions(+), 35 deletions(-) diff --git a/source/benchmark.cpp b/source/benchmark.cpp index f18c0ab..4c24ac1 100644 --- a/source/benchmark.cpp +++ b/source/benchmark.cpp @@ -65,7 +65,7 @@ void RunBenchmark() { Board board; int depth = BENCH_DEPTH; int ply = 0; - board.positionIndex = 0; + positionIndex = 0; // Clearing TT.Clear(); @@ -81,5 +81,5 @@ void RunBenchmark() { std::cout << ctx.nodes << " nodes " << int(ctx.nodes/sw.GetElapsedSec()) << " nps" << std::endl; TT.Clear(); - board.positionIndex = 0; + positionIndex = 0; } diff --git a/source/board.cpp b/source/board.cpp index 96f8228..eabfae5 100644 --- a/source/board.cpp +++ b/source/board.cpp @@ -28,8 +28,6 @@ void Board::Reset() { currentMoveIndex = 0; - positionIndex = 0; - hashKey = 0ULL; } @@ -443,29 +441,4 @@ void Board::ResetAccPair() { blackPieces.PopBit(square); } -} - -static bool IsThreefold(Board& board) { - for (int i = 0; i < board.positionIndex; i++) { - if (positionHistory[i] == board.hashKey) { - // repetition found - return true; - } - } - - // no repetition - return false; -} - -static bool IsFifty(Board& board) { - return (board.halfMoves >= 100); -} - -static bool IsInsuffMat(Board& board) { - return (board.occupied.PopCount() <= 3 - && !(board.pieces[Pawn] | board.pieces[Queen] | board.pieces[Rook])); -} - -bool Board::IsDraw() { - return IsFifty(*this) || IsInsuffMat(*this) || IsThreefold(*this); } \ No newline at end of file diff --git a/source/board.h b/source/board.h index ed841a9..5ac1cbc 100644 --- a/source/board.h +++ b/source/board.h @@ -10,6 +10,7 @@ constexpr int noEPTarget = -1; constexpr int MAX_MOVES = 218; +inline int positionIndex = 0; inline U64 positionHistory[1000]; class Board { @@ -39,8 +40,6 @@ public: int halfMoves = 0; int fullMoves = 1; - int positionIndex = 0; - U64 hashKey = 0ULL; void Reset(); @@ -67,6 +66,4 @@ public: bool MakeMove(Move move); bool InPossibleZug(); - - bool IsDraw(); }; diff --git a/source/datagen.cpp b/source/datagen.cpp index d4bd4f2..573f47a 100644 --- a/source/datagen.cpp +++ b/source/datagen.cpp @@ -63,7 +63,8 @@ static void PlayRandMoves(Board &board) { } static bool IsGameOver(Board &board) { - if (board.IsDraw()) return true; + bool isDraw = SEARCH::IsDraw(board); + if (isDraw) return true; int moveSeen = 0; @@ -72,6 +73,7 @@ static bool IsGameOver(Board &board) { bool isLegal = copy.MakeMove(board.moveList[i]); if (!isLegal) continue; + positionIndex--; moveSeen++; } @@ -97,6 +99,8 @@ void PlayGame(std::atomic& positions, U64 targetPositions, std::vector= 100); +} + +static bool IsInsuffMat(Board &board) { + return (board.occupied.PopCount() <= 3 + && !(board.pieces[Pawn] | board.pieces[Queen] | board.pieces[Rook])); +} + +bool IsDraw(Board &board) { + return IsFifty(board) || IsInsuffMat(board) || IsThreefold(board); +} + static int GetReductions(Board &board, Move &move, int depth, int moveSeen, int ply) { int reduction = 0; @@ -149,6 +174,7 @@ static SearchResults Quiescence(Board board, int alpha, int beta, int ply, Searc if (!isLegal) continue; ctx.nodes++; int score = -Quiescence(copy, -beta, -alpha, ply + 1, ctx).score; + positionIndex--; if (score >= beta) { return score; @@ -191,7 +217,7 @@ SearchResults PVS(Board board, int depth, int alpha, int beta, int ply, SearchCo } ctx.pvLine.SetLength(ply); - if (ply && (board.IsDraw())) return 0; + if (ply && (IsThreefold(board) || IsFifty(board) || IsInsuffMat(board))) return 0; // if NOT PV node then we try to hit the TTable @@ -290,6 +316,7 @@ SearchResults PVS(Board board, int depth, int alpha, int beta, int ply, SearchCo } moveSeen++; + positionIndex--; if (ctx.searchStopped) return 0; diff --git a/source/uci.cpp b/source/uci.cpp index 11b1370..01e131a 100644 --- a/source/uci.cpp +++ b/source/uci.cpp @@ -125,6 +125,7 @@ void UCILoop(Board &board) { // parse UCI "position" command if (input.find("position") != std::string::npos) { ParsePosition(board, input); + positionIndex = 0; continue; } @@ -138,6 +139,8 @@ void UCILoop(Board &board) { ctx.killerMoves = {}; ctx.history.Clear(); + + positionIndex = 0; continue; } -- 2.47.3