diff --git a/source/benchmark.cpp b/source/benchmark.cpp index 4eef752..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,9 +65,6 @@ void RunBenchmark() { int depth = BENCH_DEPTH; int ply = 0; - // Clearing - TT.Clear(); - SearchContext ctx; Stopwatch sw; @@ -78,5 +74,4 @@ void RunBenchmark() { } std::cout << ctx.nodes << " nodes " << int(ctx.nodes/sw.GetElapsedSec()) << " nps" << std::endl; - TT.Clear(); } diff --git a/source/datagen.cpp b/source/datagen.cpp index 2e33f63..732e973 100644 --- a/source/datagen.cpp +++ b/source/datagen.cpp @@ -103,11 +103,12 @@ void PlayGame(std::atomic& positions, U64 targetPositions, std::vector(board); while (!IsGameOver(board, ctx)) { - SEARCH::SearchResults results = SEARCH::SearchPosition(board, SearchParams(), ctx); + SearchResults results = SEARCH::SearchPosition(board, SearchParams(), ctx); + results = SEARCH::SearchPosition(board, SearchParams(), ctx); if (!results.bestMove) break; safeResults = results; diff --git a/source/search.cpp b/source/search.cpp index a15d302..5b6f8f3 100644 --- a/source/search.cpp +++ b/source/search.cpp @@ -3,7 +3,6 @@ #include "evaluate.h" #include #include -#include "tt.h" #include "datagen.h" #include "benchmark.h" @@ -25,7 +24,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 +147,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 +221,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 +339,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 +362,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 +430,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 5a39544..b5121c9 100644 --- a/source/search.h +++ b/source/search.h @@ -5,6 +5,7 @@ #include "move.h" #include #include "stopwatch.h" +#include "tt.h" namespace SEARCH { @@ -26,23 +27,6 @@ inline int lmrTable[MAX_DEPTH+1][MAX_MOVES]; void InitLMRTable(); -class SearchResults { -public: - int score = 0; - Move bestMove = Move(); - - SearchResults(){} - - SearchResults(int pScore, Move pMove) { - score = pScore; - bestMove = pMove; - } - - SearchResults(int pScore) { - score = pScore; - } -}; - class History { private: int historyMoves[2][64][64]; @@ -116,11 +100,14 @@ public: std::array positionHistory{}; + TTable TT; + Stopwatch sw; SearchContext(){ pvLine.Clear(); history.Clear(); + TT.Clear(); sw.Restart(); killerMoves = {}; positionHistory = {}; diff --git a/source/tt.cpp b/source/tt.cpp index 3df1649..6f6b638 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]; +SearchResults TTable::ReadEntry(U64 &hashKey, int depth, int alpha, int beta) { + 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 e8607c4..5091227 100644 --- a/source/tt.h +++ b/source/tt.h @@ -1,8 +1,24 @@ #pragma once #include "board.h" -#include "search.h" #include +class SearchResults { +public: + int score = 0; + Move bestMove = Move(); + + SearchResults(){} + + SearchResults(int pScore, Move pMove) { + score = pScore; + bestMove = pMove; + } + + SearchResults(int pScore) { + score = pScore; + } +}; + class TTEntry { public: U64 hashKey; @@ -21,27 +37,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; } @@ -56,9 +71,7 @@ public: return count; } - SEARCH::SearchResults ReadEntry(U64 &hashKey, int depth, int alpha, int beta); + SearchResults ReadEntry(U64 &hashKey, int depth, int alpha, int beta); 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 4fc61b7..357105c 100644 --- a/source/uci.cpp +++ b/source/uci.cpp @@ -8,7 +8,6 @@ #include "movegen.h" #include "search.h" #include "benchmark.h" -#include "tt.h" #include "perft.h" #include "utils.h" @@ -93,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); + U64 hashSize = (ReadParam("value", command) * 1000000) / sizeof(TTEntry); + ctx.TT.Resize(hashSize); } } @@ -135,7 +134,7 @@ void UCILoop(Board &board) { board.SetByFen(StartingFen); // Clearing - TT.Clear(); + ctx.TT.Clear(); ctx.killerMoves = {}; ctx.history.Clear(); @@ -171,7 +170,7 @@ void UCILoop(Board &board) { } if (input.find("setoption") != std::string::npos) { - SetOption(input); + SetOption(input, ctx); continue; }