From 15a8e1c1340c7e923b5de1c192689e359d0fe07a Mon Sep 17 00:00:00 2001 From: rektdie Date: Mon, 31 Mar 2025 02:11:42 +0200 Subject: [PATCH] changed search functions to templates --- source/benchmark.cpp | 4 +- source/benchmark.h | 2 + source/search.cpp | 103 +++++++++++++++++++++++++++++++------------ source/search.h | 9 +++- source/uci.cpp | 2 +- 5 files changed, 87 insertions(+), 33 deletions(-) diff --git a/source/benchmark.cpp b/source/benchmark.cpp index fce2932..120024e 100644 --- a/source/benchmark.cpp +++ b/source/benchmark.cpp @@ -63,7 +63,7 @@ constexpr std::array fenPositions = { void RunBenchmark() { Board board; - int depth = 8; + int depth = BENCH_DEPTH; int ply = 0; benchStarted = true; searchStopped = false; @@ -80,7 +80,7 @@ void RunBenchmark() { Stopwatch sw; for (int i = 0; i < fenPositions.size(); i++) { board.SetByFen(fenPositions[i]); - PVS(board, depth, -inf, inf, ply); + SearchPosition(board, SearchParams()); } std::cout << nodes << " nodes " << int(nodes/sw.GetElapsedSec()) << " nps" << std::endl; diff --git a/source/benchmark.h b/source/benchmark.h index 69c9c98..b40f633 100644 --- a/source/benchmark.h +++ b/source/benchmark.h @@ -1,4 +1,6 @@ #pragma once #include +constexpr int BENCH_DEPTH = 8; + void RunBenchmark(); diff --git a/source/search.cpp b/source/search.cpp index e8ef5a1..c208b2a 100644 --- a/source/search.cpp +++ b/source/search.cpp @@ -5,6 +5,8 @@ #include #include "tt.h" #include "stopwatch.h" +#include "datagen.h" +#include "benchmark.h" namespace SEARCH { @@ -187,12 +189,21 @@ static SearchResults Quiescence(Board board, int alpha, int beta, int ply) { return results; } -template +template SearchResults PVS(Board board, int depth, int alpha, int beta, int ply) { - if (!benchStarted && nodes % 1024 == 0) { - if (sw.GetElapsedMS() >= timeToSearch) { - StopSearch(); - return 0; + if constexpr (mode != bench) { + if (nodes % 1024 == 0) { + if constexpr (mode == normal) { + if (sw.GetElapsedMS() >= timeToSearch) { + StopSearch(); + return 0; + } + } else { + if (nodes >= DATAGEN::HARD_NODES) { + StopSearch(); + return 0; + } + } } } @@ -228,7 +239,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).score; doingNullMove = false; if (searchStopped) return 0; @@ -275,23 +286,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).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).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).score; } } else { // Null-window search - score = -PVS(copy, newDepth, -alpha-1, -alpha, ply + 1).score; + score = -PVS(copy, newDepth, -alpha-1, -alpha, ply + 1).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).score; } if (searchStopped) return 0; @@ -347,6 +358,7 @@ SearchResults PVS(Board board, int depth, int alpha, int beta, int ply) { } // Iterative deepening +template static SearchResults ID(Board &board, SearchParams params) { int fullTime = board.sideToMove ? params.btime : params.wtime; int inc = board.sideToMove ? params.binc : params.winc; @@ -354,6 +366,12 @@ static SearchResults ID(Board &board, SearchParams params) { SearchResults safeResults; safeResults.score = -inf; + int toDepth = MAX_DEPTH; + + if constexpr (mode == bench) { + toDepth = BENCH_DEPTH; + } + int alpha = -inf; int beta = inf; @@ -362,13 +380,16 @@ static SearchResults ID(Board &board, SearchParams params) { int ply = 0; int elapsed = 0; - sw.Restart(); - for (int depth = 1; depth <= MAX_DEPTH; depth++) { + if constexpr (mode == normal) { + 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); + SearchResults currentResults = PVS(board, depth, alpha, beta, ply); elapsed = sw.GetElapsedMS(); // If we fell outside the window, try again with full width @@ -396,19 +417,26 @@ static SearchResults ID(Board &board, SearchParams params) { safeResults = currentResults; } - std::cout << "info "; - std::cout << "depth " << depth; - std::cout << " time " << elapsed; - std::cout << " score cp " << safeResults.score; - std::cout << " nodes " << nodes << " nps " << int(nodes/sw.GetElapsedSec()); - std::cout << " hashfull " << TT.GetUsedPercentage(); - std::cout << " pv "; - pvLine.Print(0); - std::cout << std::endl; - - if (sw.GetElapsedMS() >= softTime) { - StopSearch(); - break; + if constexpr (mode == normal) { + std::cout << "info "; + std::cout << "depth " << depth; + std::cout << " time " << elapsed; + std::cout << " score cp " << safeResults.score; + std::cout << " nodes " << nodes << " nps " << int(nodes/sw.GetElapsedSec()); + std::cout << " hashfull " << TT.GetUsedPercentage(); + std::cout << " pv "; + pvLine.Print(0); + std::cout << std::endl; + + if (sw.GetElapsedMS() >= softTime) { + StopSearch(); + break; + } + } else if constexpr (mode == datagen) { + if (nodes >= DATAGEN::SOFT_NODES) { + StopSearch(); + break; + } } } } @@ -416,12 +444,17 @@ static SearchResults ID(Board &board, SearchParams params) { return safeResults; } +template void SearchPosition(Board &board, SearchParams params) { searchStopped = false; - nodes = 0; + if constexpr (mode != bench) { + nodes = 0; + } pvLine.Clear(); - SearchResults results = ID(board, params); + SearchResults results = ID(board, params); + + if constexpr (mode != normal) return; std::cout << "bestmove "; results.bestMove.PrintMove(); @@ -431,4 +464,16 @@ void SearchPosition(Board &board, SearchParams params) { 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 void SearchPosition(Board &board, SearchParams params); +template void SearchPosition(Board &board, SearchParams params); +template void SearchPosition(Board &board, SearchParams params); + } diff --git a/source/search.h b/source/search.h index f286246..a4fea4a 100644 --- a/source/search.h +++ b/source/search.h @@ -7,6 +7,12 @@ namespace SEARCH { +enum searchMode { + normal, + bench, + datagen +}; + constexpr int MATE_SCORE = -99000; constexpr int MAX_DEPTH = 64; constexpr int MAX_HISTORY = 16384; @@ -101,9 +107,10 @@ public: } }; -template +template SearchResults PVS(Board board, int depth, int alpha, int beta, int ply); +template void SearchPosition(Board &board, SearchParams params); void StopSearch(); diff --git a/source/uci.cpp b/source/uci.cpp index d1ae052..948fbd6 100644 --- a/source/uci.cpp +++ b/source/uci.cpp @@ -82,7 +82,7 @@ static void ParseGo(Board &board, std::string &command) { params.btime = ReadParam("movetime", command); } - auto worker = std::thread(SEARCH::SearchPosition, std::ref(board), params); + auto worker = std::thread(SEARCH::SearchPosition, std::ref(board), params); worker.detach(); }