encapsualted position index and array | bench 1857933

This commit is contained in:
rektdie committed 2025-04-12 20:39:00 +02:00
1 parent b669fa329e
commit 3225b9c665
8 files changed
+19 -28

No files matched your search

-3
View File
@@ -65,14 +65,12 @@ void RunBenchmark() {
Board board; Board board;
int depth = BENCH_DEPTH; int depth = BENCH_DEPTH;
int ply = 0; int ply = 0;
positionIndex = 0;
// Clearing // Clearing
TT.Clear(); TT.Clear();
SearchContext ctx; SearchContext ctx;
Stopwatch sw; Stopwatch sw;
for (int i = 0; i < fenPositions.size(); i++) { for (int i = 0; i < fenPositions.size(); i++) {
board.SetByFen(fenPositions[i]); board.SetByFen(fenPositions[i]);
@@ -81,5 +79,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(); TT.Clear();
positionIndex = 0;
} }
+2 -1
View File
@@ -26,6 +26,8 @@ void Board::Reset() {
moveList = std::array<Move, MAX_MOVES>(); moveList = std::array<Move, MAX_MOVES>();
positionIndex = 0;
currentMoveIndex = 0; currentMoveIndex = 0;
hashKey = 0ULL; hashKey = 0ULL;
@@ -391,7 +393,6 @@ bool Board::MakeMove(Move move) {
} }
positionIndex++; positionIndex++;
positionHistory[positionIndex] = hashKey;
return true; return true;
} }
+2 -3
View File
@@ -10,9 +10,6 @@ constexpr int noEPTarget = -1;
constexpr int MAX_MOVES = 218; constexpr int MAX_MOVES = 218;
inline int positionIndex = 0;
inline U64 positionHistory[1000];
class Board { class Board {
public: public:
Board() { Board() {
@@ -37,6 +34,8 @@ public:
uint8_t castlingRights = 0; uint8_t castlingRights = 0;
int enPassantTarget = noEPTarget; int enPassantTarget = noEPTarget;
short positionIndex = 0;
int halfMoves = 0; int halfMoves = 0;
int fullMoves = 1; int fullMoves = 1;
+3 -7
View File
@@ -62,9 +62,8 @@ static void PlayRandMoves(Board &board) {
MOVEGEN::GenerateMoves<All>(board); MOVEGEN::GenerateMoves<All>(board);
} }
static bool IsGameOver(Board &board) { static bool IsGameOver(Board &board, SEARCH::SearchContext& ctx) {
bool isDraw = SEARCH::IsDraw(board); if (SEARCH::IsDraw(board, ctx)) return true;
if (isDraw) return true;
int moveSeen = 0; int moveSeen = 0;
@@ -73,7 +72,6 @@ static bool IsGameOver(Board &board) {
bool isLegal = copy.MakeMove(board.moveList[i]); bool isLegal = copy.MakeMove(board.moveList[i]);
if (!isLegal) continue; if (!isLegal) continue;
positionIndex--;
moveSeen++; moveSeen++;
} }
@@ -99,8 +97,6 @@ void PlayGame(std::atomic<int>& positions, U64 targetPositions, std::vector<Game
Board board; Board board;
uint8_t wdl = 1; uint8_t wdl = 1;
// Since positionIndex is thread-local, no need to protect it
positionIndex = 0;
SEARCH::SearchContext ctx; SEARCH::SearchContext ctx;
@@ -110,7 +106,7 @@ void PlayGame(std::atomic<int>& positions, U64 targetPositions, std::vector<Game
SEARCH::SearchResults safeResults; SEARCH::SearchResults safeResults;
MOVEGEN::GenerateMoves<All>(board); MOVEGEN::GenerateMoves<All>(board);
while (!IsGameOver(board)) { while (!IsGameOver(board, ctx)) {
SEARCH::SearchResults results = SEARCH::SearchPosition<SEARCH::datagen>(board, SearchParams(), ctx); SEARCH::SearchResults results = SEARCH::SearchPosition<SEARCH::datagen>(board, SearchParams(), ctx);
if (!results.bestMove) break; if (!results.bestMove) break;
-2
View File
@@ -14,7 +14,6 @@ static U64 HelperPerft(Board board, int depth) {
bool isLegal = copy.MakeMove(board.moveList[i]); bool isLegal = copy.MakeMove(board.moveList[i]);
if (!isLegal) continue; if (!isLegal) continue;
nodes += HelperPerft(copy, depth - 1); nodes += HelperPerft(copy, depth - 1);
positionIndex--;
} }
return nodes; return nodes;
} }
@@ -30,7 +29,6 @@ void Perft(Board &board, int depth) {
bool isLegal = copy.MakeMove(board.moveList[i]); bool isLegal = copy.MakeMove(board.moveList[i]);
if (!isLegal) continue; if (!isLegal) continue;
U64 nodeCount = HelperPerft(copy, depth - 1); U64 nodeCount = HelperPerft(copy, depth - 1);
positionIndex--;
totalNodes += nodeCount; totalNodes += nodeCount;
board.moveList[i].PrintMove(); board.moveList[i].PrintMove();
+8 -8
View File
@@ -92,9 +92,9 @@ void ListScores(Board &board, int ply, SearchContext& ctx) {
} }
} }
static bool IsThreefold(Board &board) { static bool IsThreefold(Board &board, SearchContext& ctx) {
for (int i = 0; i < positionIndex; i++) { for (int i = 0; i < board.positionIndex; i++) {
if (positionHistory[i] == board.hashKey) { if (ctx.positionHistory[i] == board.hashKey) {
// repetition found // repetition found
return true; return true;
} }
@@ -113,8 +113,8 @@ static bool IsInsuffMat(Board &board) {
&& !(board.pieces[Pawn] | board.pieces[Queen] | board.pieces[Rook])); && !(board.pieces[Pawn] | board.pieces[Queen] | board.pieces[Rook]));
} }
bool IsDraw(Board &board) { bool IsDraw(Board &board, SearchContext& ctx) {
return IsFifty(board) || IsInsuffMat(board) || IsThreefold(board); return IsFifty(board) || IsInsuffMat(board) || IsThreefold(board, ctx);
} }
static int GetReductions(Board &board, Move &move, int depth, int moveSeen, int ply) { static int GetReductions(Board &board, Move &move, int depth, int moveSeen, int ply) {
@@ -172,9 +172,9 @@ static SearchResults Quiescence(Board& board, int alpha, int beta, int ply, Sear
Board copy = board; Board copy = board;
int isLegal = copy.MakeMove(board.moveList[i]); int isLegal = copy.MakeMove(board.moveList[i]);
if (!isLegal) continue; if (!isLegal) continue;
ctx.positionHistory[copy.positionIndex] = copy.hashKey;
ctx.nodes++; ctx.nodes++;
int score = -Quiescence<mode>(copy, -beta, -alpha, ply + 1, ctx).score; int score = -Quiescence<mode>(copy, -beta, -alpha, ply + 1, ctx).score;
positionIndex--;
if (score >= beta) { if (score >= beta) {
return score; return score;
@@ -217,7 +217,7 @@ SearchResults PVS(Board& board, int depth, int alpha, int beta, int ply, SearchC
} }
ctx.pvLine.SetLength(ply); ctx.pvLine.SetLength(ply);
if (ply && (IsThreefold(board) || IsFifty(board) || IsInsuffMat(board))) return 0; if (ply && (IsDraw(board, ctx))) return 0;
// if NOT PV node then we try to hit the TTable // if NOT PV node then we try to hit the TTable
@@ -287,6 +287,7 @@ SearchResults PVS(Board& board, int depth, int alpha, int beta, int ply, SearchC
bool isLegal = copy.MakeMove(currMove); bool isLegal = copy.MakeMove(currMove);
if (!isLegal) continue; if (!isLegal) continue;
ctx.positionHistory[copy.positionIndex] = copy.hashKey;
ctx.nodes++; ctx.nodes++;
int reductions = GetReductions(board, currMove, depth, moveSeen, ply); int reductions = GetReductions(board, currMove, depth, moveSeen, ply);
@@ -316,7 +317,6 @@ SearchResults PVS(Board& board, int depth, int alpha, int beta, int ply, SearchC
} }
moveSeen++; moveSeen++;
positionIndex--;
if (ctx.searchStopped) return 0; if (ctx.searchStopped) return 0;
+4 -1
View File
@@ -114,6 +114,8 @@ public:
std::array<std::array<int, MAX_PLY>, 2> killerMoves{}; std::array<std::array<int, MAX_PLY>, 2> killerMoves{};
std::array<U64, 1000> positionHistory{};
Stopwatch sw; Stopwatch sw;
SearchContext(){ SearchContext(){
@@ -121,6 +123,7 @@ public:
history.Clear(); history.Clear();
sw.Restart(); sw.Restart();
killerMoves = {}; killerMoves = {};
positionHistory = {};
} }
}; };
@@ -130,7 +133,7 @@ SearchResults PVS(Board& board, int depth, int alpha, int beta, int ply, SearchC
template <searchMode mode> template <searchMode mode>
SearchResults SearchPosition(Board &board, SearchParams params, SearchContext& ctx); SearchResults SearchPosition(Board &board, SearchParams params, SearchContext& ctx);
bool IsDraw(Board &board); bool IsDraw(Board &board, SearchContext& ctx);
constexpr int moveScoreTable[6][6] = { constexpr int moveScoreTable[6][6] = {
105, 205, 305, 405, 505, 605, 105, 205, 305, 405, 505, 605,
-3
View File
@@ -125,7 +125,6 @@ void UCILoop(Board &board) {
// parse UCI "position" command // parse UCI "position" command
if (input.find("position") != std::string::npos) { if (input.find("position") != std::string::npos) {
ParsePosition(board, input); ParsePosition(board, input);
positionIndex = 0;
continue; continue;
} }
@@ -139,8 +138,6 @@ void UCILoop(Board &board) {
ctx.killerMoves = {}; ctx.killerMoves = {};
ctx.history.Clear(); ctx.history.Clear();
positionIndex = 0;
continue; continue;
} }