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 "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();
}
+3 -2
View File
@@ -103,11 +103,12 @@ void PlayGame(std::atomic<int>& positions, U64 targetPositions, std::vector<Game
try {
PlayRandMoves(board);
int staticEval = HCE::Evaluate(board);
SEARCH::SearchResults safeResults;
SearchResults safeResults;
MOVEGEN::GenerateMoves<All>(board);
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;
safeResults = results;
+6 -7
View File
@@ -3,7 +3,6 @@
#include "evaluate.h"
#include <algorithm>
#include <cmath>
#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;
+4 -17
View File
@@ -5,6 +5,7 @@
#include "move.h"
#include <algorithm>
#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<U64, 1000> positionHistory{};
TTable TT;
Stopwatch sw;
SearchContext(){
pvLine.Clear();
history.Clear();
TT.Clear();
sw.Restart();
killerMoves = {};
positionHistory = {};
+3 -3
View File
@@ -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;
+22 -9
View File
@@ -1,8 +1,24 @@
#pragma once
#include "board.h"
#include "search.h"
#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 {
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<TTEntry> 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;
+5 -6
View File
@@ -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;
}