encapsulated TT | bench 1857933

This commit is contained in:
rektdie committed 2025-04-12 22:43:39 +02:00
1 parent c08c545335
commit 53f6ae17bc
7 files changed
+43 -49

No files matched your search

-5
View File
@@ -4,7 +4,6 @@
#include "benchmark.h" #include "benchmark.h"
#include "search.h" #include "search.h"
#include "stopwatch.h" #include "stopwatch.h"
#include "tt.h"
using namespace SEARCH; using namespace SEARCH;
@@ -66,9 +65,6 @@ void RunBenchmark() {
int depth = BENCH_DEPTH; int depth = BENCH_DEPTH;
int ply = 0; int ply = 0;
// Clearing
TT.Clear();
SearchContext ctx; SearchContext ctx;
Stopwatch sw; Stopwatch sw;
@@ -78,5 +74,4 @@ void RunBenchmark() {
} }
std::cout << ctx.nodes << " nodes " << int(ctx.nodes/sw.GetElapsedSec()) << " nps" << std::endl; std::cout << ctx.nodes << " nodes " << int(ctx.nodes/sw.GetElapsedSec()) << " nps" << std::endl;
TT.Clear();
} }
+3 -2
View File
@@ -103,11 +103,12 @@ void PlayGame(std::atomic<int>& positions, U64 targetPositions, std::vector<Game
try { try {
PlayRandMoves(board); PlayRandMoves(board);
int staticEval = HCE::Evaluate(board); int staticEval = HCE::Evaluate(board);
SEARCH::SearchResults safeResults; SearchResults safeResults;
MOVEGEN::GenerateMoves<All>(board); MOVEGEN::GenerateMoves<All>(board);
while (!IsGameOver(board, ctx)) { while (!IsGameOver(board, ctx)) {
SEARCH::SearchResults results = SEARCH::SearchPosition<SEARCH::datagen>(board, SearchParams(), ctx); SearchResults results = SEARCH::SearchPosition<SEARCH::datagen>(board, SearchParams(), ctx);
results = SEARCH::SearchPosition<SEARCH::datagen>(board, SearchParams(), ctx);
if (!results.bestMove) break; if (!results.bestMove) break;
safeResults = results; safeResults = results;
+6 -7
View File
@@ -3,7 +3,6 @@
#include "evaluate.h" #include "evaluate.h"
#include <algorithm> #include <algorithm>
#include <cmath> #include <cmath>
#include "tt.h"
#include "datagen.h" #include "datagen.h"
#include "benchmark.h" #include "benchmark.h"
@@ -25,7 +24,7 @@ void InitLMRTable() {
} }
static int ScoreMove(Board &board, Move &move, int ply, SearchContext& ctx) { 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) { if (current->bestMove == move) {
return 50000; return 50000;
} }
@@ -148,7 +147,7 @@ static SearchResults Quiescence(Board& board, int alpha, int beta, int ply, Sear
int bestScore = HCE::Evaluate(board); int bestScore = HCE::Evaluate(board);
TTEntry *entry = TT.GetRawEntry(board.hashKey); TTEntry *entry = ctx.TT.GetRawEntry(board.hashKey);
if (entry->hashKey == board.hashKey) { if (entry->hashKey == board.hashKey) {
bestScore = entry->score; 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 NOT PV node then we try to hit the TTable
if constexpr (!isPV) { 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) { if (entry.score != invalidEntry) {
return entry; 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; return score;
} }
@@ -363,7 +362,7 @@ SearchResults PVS(Board& board, int depth, int alpha, int beta, int ply, SearchC
} }
if (ctx.searchStopped) return 0; 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; return results;
} }
@@ -431,7 +430,7 @@ static SearchResults ID(Board &board, SearchParams params, SearchContext& ctx) {
std::cout << " time " << elapsed; std::cout << " time " << elapsed;
std::cout << " score cp " << UTILS::ConvertToWhiteRelative(board, safeResults.score); std::cout << " score cp " << UTILS::ConvertToWhiteRelative(board, safeResults.score);
std::cout << " nodes " << ctx.nodes << " nps " << int(ctx.nodes/ctx.sw.GetElapsedSec()); 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 "; std::cout << " pv ";
ctx.pvLine.Print(0); ctx.pvLine.Print(0);
std::cout << std::endl; std::cout << std::endl;
+4 -17
View File
@@ -5,6 +5,7 @@
#include "move.h" #include "move.h"
#include <algorithm> #include <algorithm>
#include "stopwatch.h" #include "stopwatch.h"
#include "tt.h"
namespace SEARCH { namespace SEARCH {
@@ -26,23 +27,6 @@ inline int lmrTable[MAX_DEPTH+1][MAX_MOVES];
void InitLMRTable(); 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 { class History {
private: private:
int historyMoves[2][64][64]; int historyMoves[2][64][64];
@@ -116,11 +100,14 @@ public:
std::array<U64, 1000> positionHistory{}; std::array<U64, 1000> positionHistory{};
TTable TT;
Stopwatch sw; Stopwatch sw;
SearchContext(){ SearchContext(){
pvLine.Clear(); pvLine.Clear();
history.Clear(); history.Clear();
TT.Clear();
sw.Restart(); sw.Restart();
killerMoves = {}; killerMoves = {};
positionHistory = {}; positionHistory = {};
+3 -3
View File
@@ -1,7 +1,7 @@
#include "tt.h" #include "tt.h"
SEARCH::SearchResults TTable::ReadEntry(U64 &hashKey, int depth, int alpha, int beta) { 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->hashKey == hashKey) {
if (current->depth >= depth) { 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) { 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->hashKey = hashKey;
current->nodeType = nodeType; current->nodeType = nodeType;
+22 -9
View File
@@ -1,8 +1,24 @@
#pragma once #pragma once
#include "board.h" #include "board.h"
#include "search.h"
#include <vector> #include <vector>
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 { class TTEntry {
public: public:
U64 hashKey; U64 hashKey;
@@ -21,27 +37,26 @@ constexpr U64 maxHash = (64* 1000000) / sizeof(TTEntry);
// 5 MB // 5 MB
constexpr U64 defaultHash = (5 * 1000000) / sizeof(TTEntry); constexpr U64 defaultHash = (5 * 1000000) / sizeof(TTEntry);
inline U64 hashSize = defaultHash;
constexpr int invalidEntry = 111111; constexpr int invalidEntry = 111111;
class TTable { class TTable {
private: private:
std::vector<TTEntry> table; std::vector<TTEntry> table;
public: public:
TTable() : table(hashSize) {}; TTable() : table(defaultHash) {};
void Resize(U64 size) { void Resize(U64 size) {
table.resize(size); table.resize(size);
} }
void Clear() { void Clear() {
U64 size = table.size();
table.clear(); table.clear();
table.resize(hashSize); table.resize(size);
} }
TTEntry* GetRawEntry(U64 &hashKey) { TTEntry* GetRawEntry(U64 &hashKey) {
TTEntry *current = &table[hashKey % hashSize]; TTEntry *current = &table[hashKey % table.size()];
return current; return current;
} }
@@ -56,9 +71,7 @@ public:
return count; 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); void WriteEntry(U64 &hashKey, int depth, int score, int nodeType, Move besteMove);
}; };
inline TTable TT;
+5 -6
View File
@@ -8,7 +8,6 @@
#include "movegen.h" #include "movegen.h"
#include "search.h" #include "search.h"
#include "benchmark.h" #include "benchmark.h"
#include "tt.h"
#include "perft.h" #include "perft.h"
#include "utils.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) { if (command.find("Hash") != std::string::npos) {
hashSize = (ReadParam("value", command) * 1000000) / sizeof(TTEntry); U64 hashSize = (ReadParam("value", command) * 1000000) / sizeof(TTEntry);
TT.Resize(hashSize); ctx.TT.Resize(hashSize);
} }
} }
@@ -135,7 +134,7 @@ void UCILoop(Board &board) {
board.SetByFen(StartingFen); board.SetByFen(StartingFen);
// Clearing // Clearing
TT.Clear(); ctx.TT.Clear();
ctx.killerMoves = {}; ctx.killerMoves = {};
ctx.history.Clear(); ctx.history.Clear();
@@ -171,7 +170,7 @@ void UCILoop(Board &board) {
} }
if (input.find("setoption") != std::string::npos) { if (input.find("setoption") != std::string::npos) {
SetOption(input); SetOption(input, ctx);
continue; continue;
} }