Merge pull request #74 from rektdie/conthist
storing searchcontext on the heap instead of the stack | bench 3415609
This commit was merged in pull request #74.
This commit is contained in:
6 files changed
+142
-123
No files matched your search
Vendored
+23
@@ -0,0 +1,23 @@
|
|||||||
|
// Stolen from Ciekce - Stormphrax
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <array>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
namespace internal
|
||||||
|
{
|
||||||
|
template <typename T, size_t N, size_t ... Ns>
|
||||||
|
struct MultiArrayImpl
|
||||||
|
{
|
||||||
|
using Type = std::array<typename MultiArrayImpl<T, Ns...>::Type, N>;
|
||||||
|
};
|
||||||
|
|
||||||
|
template <typename T, size_t N>
|
||||||
|
struct MultiArrayImpl<T, N>
|
||||||
|
{
|
||||||
|
using Type = std::array<T, N>;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename T, size_t ... Ns>
|
||||||
|
using MultiArray = typename internal::MultiArrayImpl<T, Ns...>::Type;
|
||||||
@@ -64,7 +64,7 @@ void RunBenchmark() {
|
|||||||
Board board;
|
Board board;
|
||||||
int depth = BENCH_DEPTH;
|
int depth = BENCH_DEPTH;
|
||||||
|
|
||||||
SearchContext ctx;
|
SEARCH::SearchContext *ctx = new SearchContext();
|
||||||
|
|
||||||
Stopwatch sw;
|
Stopwatch sw;
|
||||||
for (int i = 0; i < fenPositions.size(); i++) {
|
for (int i = 0; i < fenPositions.size(); i++) {
|
||||||
@@ -72,5 +72,5 @@ void RunBenchmark() {
|
|||||||
SearchPosition<bench>(board, SearchParams(), ctx);
|
SearchPosition<bench>(board, SearchParams(), ctx);
|
||||||
}
|
}
|
||||||
|
|
||||||
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;
|
||||||
}
|
}
|
||||||
+5
-5
@@ -44,7 +44,7 @@ namespace DATAGEN {
|
|||||||
|
|
||||||
static std::mutex fileOpenMutex;
|
static std::mutex fileOpenMutex;
|
||||||
|
|
||||||
static bool IsGameOver(Board &board, SEARCH::SearchContext& ctx) {
|
static bool IsGameOver(Board &board, SEARCH::SearchContext* ctx) {
|
||||||
if (SEARCH::IsDraw(board, ctx)) return true;
|
if (SEARCH::IsDraw(board, ctx)) return true;
|
||||||
|
|
||||||
int moveSeen = 0;
|
int moveSeen = 0;
|
||||||
@@ -60,7 +60,7 @@ static bool IsGameOver(Board &board, SEARCH::SearchContext& ctx) {
|
|||||||
return moveSeen == 0;
|
return moveSeen == 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void PlayRandMoves(Board &board, SEARCH::SearchContext& ctx) {
|
static void PlayRandMoves(Board &board, SEARCH::SearchContext* ctx) {
|
||||||
std::random_device dev;
|
std::random_device dev;
|
||||||
std::mt19937_64 rng(dev());
|
std::mt19937_64 rng(dev());
|
||||||
|
|
||||||
@@ -77,7 +77,7 @@ static void PlayRandMoves(Board &board, SEARCH::SearchContext& ctx) {
|
|||||||
count--;
|
count--;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
ctx.positionHistory[board.positionIndex] = board.hashKey;
|
ctx->positionHistory[board.positionIndex] = board.hashKey;
|
||||||
MOVEGEN::GenerateMoves<All>(board);
|
MOVEGEN::GenerateMoves<All>(board);
|
||||||
if (IsGameOver(board, ctx)) break;
|
if (IsGameOver(board, ctx)) break;
|
||||||
}
|
}
|
||||||
@@ -130,7 +130,7 @@ static void PlayGames(int id, std::atomic<int>& positions, std::atomic<bool>& st
|
|||||||
while (!stopFlag) {
|
while (!stopFlag) {
|
||||||
Game game;
|
Game game;
|
||||||
Board board;
|
Board board;
|
||||||
SEARCH::SearchContext ctx;
|
SEARCH::SearchContext *ctx = new SEARCH::SearchContext();
|
||||||
PlayRandMoves(board, ctx);
|
PlayRandMoves(board, ctx);
|
||||||
if (IsGameOver(board, ctx)) continue;
|
if (IsGameOver(board, ctx)) continue;
|
||||||
|
|
||||||
@@ -151,7 +151,7 @@ static void PlayGames(int id, std::atomic<int>& positions, std::atomic<bool>& st
|
|||||||
UTILS::ConvertToWhiteRelative(board, results.score)));
|
UTILS::ConvertToWhiteRelative(board, results.score)));
|
||||||
|
|
||||||
board.MakeMove(results.bestMove);
|
board.MakeMove(results.bestMove);
|
||||||
ctx.positionHistory[board.positionIndex] = board.hashKey;
|
ctx->positionHistory[board.positionIndex] = board.hashKey;
|
||||||
|
|
||||||
positions++;
|
positions++;
|
||||||
|
|
||||||
|
|||||||
+93
-93
@@ -22,8 +22,8 @@ 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 = ctx.TT.GetRawEntry(board.hashKey);
|
TTEntry *current = ctx->TT.GetRawEntry(board.hashKey);
|
||||||
if (current->hashKey == board.hashKey && current->bestMove == move) {
|
if (current->hashKey == board.hashKey && current->bestMove == move) {
|
||||||
return 50000;
|
return 50000;
|
||||||
}
|
}
|
||||||
@@ -38,20 +38,20 @@ static int ScoreMove(Board &board, Move &move, int ply, SearchContext& ctx) {
|
|||||||
|
|
||||||
return 30000 * ((SEE(board, move, -100))) + moveScoreTable[attackerType][targetType];
|
return 30000 * ((SEE(board, move, -100))) + moveScoreTable[attackerType][targetType];
|
||||||
} else {
|
} else {
|
||||||
if (ctx.killerMoves[0][ply] == move) {
|
if (ctx->killerMoves[0][ply] == move) {
|
||||||
return 20000;
|
return 20000;
|
||||||
} else if (ctx.killerMoves[1][ply] == move) {
|
} else if (ctx->killerMoves[1][ply] == move) {
|
||||||
return 18000;
|
return 18000;
|
||||||
} else {
|
} else {
|
||||||
// Max 16384
|
// Max 16384
|
||||||
return ctx.history[board.sideToMove][move.MoveFrom()][move.MoveTo()];
|
return ctx->history[board.sideToMove][move.MoveFrom()][move.MoveTo()];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void SortMoves(Board &board, int ply, SearchContext& ctx) {
|
static void SortMoves(Board &board, int ply, SearchContext* ctx) {
|
||||||
if (board.currentMoveIndex <= 1) {
|
if (board.currentMoveIndex <= 1) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -78,7 +78,7 @@ static void SortMoves(Board &board, int ply, SearchContext& ctx) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void ListScores(Board &board, int ply, SearchContext& ctx) {
|
void ListScores(Board &board, int ply, SearchContext* ctx) {
|
||||||
SortMoves(board, ply, ctx);
|
SortMoves(board, ply, ctx);
|
||||||
|
|
||||||
for (int i = 0; i < board.currentMoveIndex; i++) {
|
for (int i = 0; i < board.currentMoveIndex; i++) {
|
||||||
@@ -89,9 +89,9 @@ void ListScores(Board &board, int ply, SearchContext& ctx) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool IsThreefold(Board &board, SearchContext& ctx) {
|
static bool IsThreefold(Board &board, SearchContext* ctx) {
|
||||||
for (int i = 0; i < board.positionIndex; i++) {
|
for (int i = 0; i < board.positionIndex; i++) {
|
||||||
if (ctx.positionHistory[i] == board.hashKey) {
|
if (ctx->positionHistory[i] == board.hashKey) {
|
||||||
// repetition found
|
// repetition found
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@@ -110,11 +110,11 @@ 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, SearchContext& ctx) {
|
bool IsDraw(Board &board, SearchContext* ctx) {
|
||||||
return IsFifty(board) || IsInsuffMat(board) || IsThreefold(board, ctx);
|
return IsFifty(board) || IsInsuffMat(board) || IsThreefold(board, ctx);
|
||||||
}
|
}
|
||||||
|
|
||||||
static int GetReductions(Board &board, Move &move, int depth, int moveSeen, int ply, bool cutnode, SearchContext& ctx) {
|
static int GetReductions(Board &board, Move &move, int depth, int moveSeen, int ply, bool cutnode, SearchContext* ctx) {
|
||||||
int reduction = 0;
|
int reduction = 0;
|
||||||
|
|
||||||
// Late Move Reduction
|
// Late Move Reduction
|
||||||
@@ -125,7 +125,7 @@ static int GetReductions(Board &board, Move &move, int depth, int moveSeen, int
|
|||||||
reduction += 2;
|
reduction += 2;
|
||||||
|
|
||||||
// History LMR
|
// History LMR
|
||||||
int historyReduction = ctx.history[board.sideToMove][move.MoveFrom()][move.MoveTo()] / 8192;
|
int historyReduction = ctx->history[board.sideToMove][move.MoveFrom()][move.MoveTo()] / 8192;
|
||||||
reduction -= historyReduction;
|
reduction -= historyReduction;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -205,35 +205,35 @@ bool SEE(Board& board, Move& move, int threshold) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
template <searchMode mode>
|
template <searchMode mode>
|
||||||
static SearchResults Quiescence(Board& board, int alpha, int beta, int ply, SearchContext& ctx) {
|
static SearchResults Quiescence(Board& board, int alpha, int beta, int ply, SearchContext* ctx) {
|
||||||
if constexpr (mode != nodesMode) {
|
if constexpr (mode != nodesMode) {
|
||||||
if constexpr (mode != bench) {
|
if constexpr (mode != bench) {
|
||||||
if (ctx.nodes % 1024 == 0) {
|
if (ctx->nodes % 1024 == 0) {
|
||||||
if (ctx.sw.GetElapsedMS() >= ctx.timeToSearch) {
|
if (ctx->sw.GetElapsedMS() >= ctx->timeToSearch) {
|
||||||
ctx.searchStopped = true;
|
ctx->searchStopped = true;
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else if constexpr (mode == nodesMode) {
|
} else if constexpr (mode == nodesMode) {
|
||||||
if (ctx.nodes > ctx.nodesToGo) {
|
if (ctx->nodes > ctx->nodesToGo) {
|
||||||
ctx.searchStopped = true;
|
ctx->searchStopped = true;
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
} else if constexpr (mode == datagen) {
|
} else if constexpr (mode == datagen) {
|
||||||
if (ctx.nodes > DATAGEN::HARD_NODES) {
|
if (ctx->nodes > DATAGEN::HARD_NODES) {
|
||||||
ctx.searchStopped = true;
|
ctx->searchStopped = true;
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (ply > ctx.seldepth)
|
if (ply > ctx->seldepth)
|
||||||
ctx.seldepth = ply;
|
ctx->seldepth = ply;
|
||||||
|
|
||||||
int bestScore = NNUE::net.Evaluate(board);
|
int bestScore = NNUE::net.Evaluate(board);
|
||||||
ctx.ss[ply].eval = bestScore;
|
ctx->ss[ply].eval = bestScore;
|
||||||
|
|
||||||
TTEntry *entry = ctx.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;
|
||||||
}
|
}
|
||||||
@@ -261,11 +261,11 @@ static SearchResults Quiescence(Board& board, int alpha, int beta, int ply, Sear
|
|||||||
if (!SEE(board, board.moveList[i], 0))
|
if (!SEE(board, board.moveList[i], 0))
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
if (copy.positionIndex >= ctx.positionHistory.size()) {
|
if (copy.positionIndex >= ctx->positionHistory.size()) {
|
||||||
ctx.positionHistory.resize(copy.positionIndex + 100);
|
ctx->positionHistory.resize(copy.positionIndex + 100);
|
||||||
}
|
}
|
||||||
ctx.positionHistory[copy.positionIndex] = copy.hashKey;
|
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;
|
||||||
|
|
||||||
if (score >= beta) {
|
if (score >= beta) {
|
||||||
@@ -281,44 +281,44 @@ static SearchResults Quiescence(Board& board, int alpha, int beta, int ply, Sear
|
|||||||
}
|
}
|
||||||
|
|
||||||
results.score = bestScore;
|
results.score = bestScore;
|
||||||
if (ctx.searchStopped) return 0;
|
if (ctx->searchStopped) return 0;
|
||||||
return results;
|
return results;
|
||||||
}
|
}
|
||||||
|
|
||||||
template <bool isPV, searchMode mode>
|
template <bool isPV, searchMode mode>
|
||||||
SearchResults PVS(Board& board, int depth, int alpha, int beta, int ply, SearchContext& ctx, bool cutnode) {
|
SearchResults PVS(Board& board, int depth, int alpha, int beta, int ply, SearchContext* ctx, bool cutnode) {
|
||||||
if constexpr (mode != bench && mode != nodesMode) {
|
if constexpr (mode != bench && mode != nodesMode) {
|
||||||
if (ctx.nodes % 1024 == 0) {
|
if (ctx->nodes % 1024 == 0) {
|
||||||
if constexpr (mode == normal) {
|
if constexpr (mode == normal) {
|
||||||
if (ctx.sw.GetElapsedMS() >= ctx.timeToSearch) {
|
if (ctx->sw.GetElapsedMS() >= ctx->timeToSearch) {
|
||||||
ctx.searchStopped = true;
|
ctx->searchStopped = true;
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else if constexpr (mode == nodesMode) {
|
} else if constexpr (mode == nodesMode) {
|
||||||
if (ctx.nodes > ctx.nodesToGo) {
|
if (ctx->nodes > ctx->nodesToGo) {
|
||||||
ctx.searchStopped = true;
|
ctx->searchStopped = true;
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
} else if constexpr (mode == datagen) {
|
} else if constexpr (mode == datagen) {
|
||||||
if (ctx.nodes > DATAGEN::HARD_NODES) {
|
if (ctx->nodes > DATAGEN::HARD_NODES) {
|
||||||
ctx.searchStopped = true;
|
ctx->searchStopped = true;
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (ply > ctx.seldepth)
|
if (ply > ctx->seldepth)
|
||||||
ctx.seldepth = ply;
|
ctx->seldepth = ply;
|
||||||
|
|
||||||
|
|
||||||
ctx.pvLine.SetLength(ply);
|
ctx->pvLine.SetLength(ply);
|
||||||
if (ply && (IsDraw(board, ctx))) 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
|
||||||
if constexpr (!isPV) {
|
if constexpr (!isPV) {
|
||||||
SearchResults entry = ctx.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;
|
||||||
}
|
}
|
||||||
@@ -327,16 +327,16 @@ SearchResults PVS(Board& board, int depth, int alpha, int beta, int ply, SearchC
|
|||||||
if (depth <= 0) return Quiescence<mode>(board, alpha, beta, ply, ctx);
|
if (depth <= 0) return Quiescence<mode>(board, alpha, beta, ply, ctx);
|
||||||
|
|
||||||
const int staticEval = NNUE::net.Evaluate(board);
|
const int staticEval = NNUE::net.Evaluate(board);
|
||||||
ctx.ss[ply].eval = staticEval;
|
ctx->ss[ply].eval = staticEval;
|
||||||
|
|
||||||
const bool improving = [&]
|
const bool improving = [&]
|
||||||
{
|
{
|
||||||
if (board.InCheck())
|
if (board.InCheck())
|
||||||
return false;
|
return false;
|
||||||
if (ply > 1 && ctx.ss[ply - 2].eval != ScoreNone)
|
if (ply > 1 && ctx->ss[ply - 2].eval != ScoreNone)
|
||||||
return staticEval > ctx.ss[ply - 2].eval;
|
return staticEval > ctx->ss[ply - 2].eval;
|
||||||
if (ply > 3 && ctx.ss[ply - 4].eval != ScoreNone)
|
if (ply > 3 && ctx->ss[ply - 4].eval != ScoreNone)
|
||||||
return staticEval > ctx.ss[ply - 4].eval;
|
return staticEval > ctx->ss[ply - 4].eval;
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}();
|
}();
|
||||||
@@ -350,17 +350,17 @@ SearchResults PVS(Board& board, int depth, int alpha, int beta, int ply, SearchC
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Null Move Pruning
|
// Null Move Pruning
|
||||||
if (!ctx.doingNullMove && staticEval >= beta) {
|
if (!ctx->doingNullMove && staticEval >= beta) {
|
||||||
if (depth >= 3 && !board.InPossibleZug()) {
|
if (depth >= 3 && !board.InPossibleZug()) {
|
||||||
Board copy = board;
|
Board copy = board;
|
||||||
bool isLegal = copy.MakeMove(Move());
|
bool isLegal = copy.MakeMove(Move());
|
||||||
// Always legal so we dont check it
|
// Always legal so we dont check it
|
||||||
|
|
||||||
ctx.doingNullMove = true;
|
ctx->doingNullMove = true;
|
||||||
int score = -PVS<false, mode>(copy, depth - 3, -beta, -beta + 1, ply + 1, ctx, !cutnode).score;
|
int score = -PVS<false, mode>(copy, depth - 3, -beta, -beta + 1, ply + 1, ctx, !cutnode).score;
|
||||||
ctx.doingNullMove = false;
|
ctx->doingNullMove = false;
|
||||||
|
|
||||||
if (ctx.searchStopped) return 0;
|
if (ctx->searchStopped) return 0;
|
||||||
if (score >= beta) return score;
|
if (score >= beta) return score;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -412,11 +412,11 @@ 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;
|
||||||
if (copy.positionIndex >= ctx.positionHistory.size()) {
|
if (copy.positionIndex >= ctx->positionHistory.size()) {
|
||||||
ctx.positionHistory.resize(copy.positionIndex + 100);
|
ctx->positionHistory.resize(copy.positionIndex + 100);
|
||||||
}
|
}
|
||||||
ctx.positionHistory[copy.positionIndex] = copy.hashKey;
|
ctx->positionHistory[copy.positionIndex] = copy.hashKey;
|
||||||
ctx.nodes++;
|
ctx->nodes++;
|
||||||
|
|
||||||
// PVS SEE
|
// PVS SEE
|
||||||
int SEEThreshold = currMove.IsQuiet() ? -80 * depth : -30 * depth * depth;
|
int SEEThreshold = currMove.IsQuiet() ? -80 * depth : -30 * depth * depth;
|
||||||
@@ -457,7 +457,7 @@ SearchResults PVS(Board& board, int depth, int alpha, int beta, int ply, SearchC
|
|||||||
|
|
||||||
moveSeen++;
|
moveSeen++;
|
||||||
|
|
||||||
if (ctx.searchStopped) return 0;
|
if (ctx->searchStopped) return 0;
|
||||||
|
|
||||||
if (currMove != 0 && currMove.IsQuiet()) {
|
if (currMove != 0 && currMove.IsQuiet()) {
|
||||||
seenQuiets[seenQuietsCount] = currMove;
|
seenQuiets[seenQuietsCount] = currMove;
|
||||||
@@ -467,20 +467,20 @@ SearchResults PVS(Board& board, int depth, int alpha, int beta, int ply, SearchC
|
|||||||
// Fail high (beta cutoff)
|
// Fail high (beta cutoff)
|
||||||
if (score >= beta) {
|
if (score >= beta) {
|
||||||
if (!currMove.IsCapture()) {
|
if (!currMove.IsCapture()) {
|
||||||
ctx.killerMoves[1][ply] = ctx.killerMoves[0][ply];
|
ctx->killerMoves[1][ply] = ctx->killerMoves[0][ply];
|
||||||
ctx.killerMoves[0][ply] = currMove;
|
ctx->killerMoves[0][ply] = currMove;
|
||||||
|
|
||||||
int bonus = 300 * depth - 250;
|
int bonus = 300 * depth - 250;
|
||||||
|
|
||||||
ctx.history.Update(board.sideToMove, currMove, bonus);
|
ctx->history.Update(board.sideToMove, currMove, bonus);
|
||||||
|
|
||||||
// Malus
|
// Malus
|
||||||
for (int moveIndex = 0; moveIndex < seenQuietsCount - 1; moveIndex++) {
|
for (int moveIndex = 0; moveIndex < seenQuietsCount - 1; moveIndex++) {
|
||||||
ctx.history.Update(board.sideToMove, seenQuiets[moveIndex], -bonus);
|
ctx->history.Update(board.sideToMove, seenQuiets[moveIndex], -bonus);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
ctx.TT.WriteEntry(board.hashKey, depth, score, CutNode, Move());
|
ctx->TT.WriteEntry(board.hashKey, depth, score, CutNode, Move());
|
||||||
return score;
|
return score;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -490,7 +490,7 @@ SearchResults PVS(Board& board, int depth, int alpha, int beta, int ply, SearchC
|
|||||||
nodeType = PV;
|
nodeType = PV;
|
||||||
alpha = score;
|
alpha = score;
|
||||||
results.bestMove = currMove;
|
results.bestMove = currMove;
|
||||||
ctx.pvLine.SetMove(ply, currMove);
|
ctx->pvLine.SetMove(ply, currMove);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -502,8 +502,8 @@ SearchResults PVS(Board& board, int depth, int alpha, int beta, int ply, SearchC
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (ctx.searchStopped) return 0;
|
if (ctx->searchStopped) return 0;
|
||||||
ctx.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;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -533,7 +533,7 @@ public:
|
|||||||
|
|
||||||
// Iterative deepening
|
// Iterative deepening
|
||||||
template <searchMode mode>
|
template <searchMode mode>
|
||||||
static SearchResults ID(Board &board, SearchParams params, SearchContext& ctx) {
|
static SearchResults ID(Board &board, SearchParams params, SearchContext* ctx) {
|
||||||
int fullTime = board.sideToMove ? params.btime : params.wtime;
|
int fullTime = board.sideToMove ? params.btime : params.wtime;
|
||||||
int inc = board.sideToMove ? params.binc : params.winc;
|
int inc = board.sideToMove ? params.binc : params.winc;
|
||||||
int movesToGo = params.movesToGo ? params.movesToGo : 20;
|
int movesToGo = params.movesToGo ? params.movesToGo : 20;
|
||||||
@@ -551,14 +551,14 @@ static SearchResults ID(Board &board, SearchParams params, SearchContext& ctx) {
|
|||||||
|
|
||||||
int elapsed = 0;
|
int elapsed = 0;
|
||||||
|
|
||||||
ctx.sw.Restart();
|
ctx->sw.Restart();
|
||||||
|
|
||||||
for (int depth = 1; depth <= toDepth; depth++) {
|
for (int depth = 1; depth <= toDepth; depth++) {
|
||||||
ctx.timeToSearch = std::max((fullTime / movesToGo) + (inc / 2), 4);
|
ctx->timeToSearch = std::max((fullTime / movesToGo) + (inc / 2), 4);
|
||||||
int softTime = ctx.timeToSearch * 0.65;
|
int softTime = ctx->timeToSearch * 0.65;
|
||||||
|
|
||||||
SearchResults currentResults = PVS<true, mode>(board, depth, aw.alpha, aw.beta, 0, ctx, false);
|
SearchResults currentResults = PVS<true, mode>(board, depth, aw.alpha, aw.beta, 0, ctx, false);
|
||||||
elapsed = ctx.sw.GetElapsedMS();
|
elapsed = ctx->sw.GetElapsedMS();
|
||||||
|
|
||||||
if (aw.alpha != -inf && currentResults.score <= aw.alpha) {
|
if (aw.alpha != -inf && currentResults.score <= aw.alpha) {
|
||||||
aw.WidenDown();
|
aw.WidenDown();
|
||||||
@@ -573,7 +573,7 @@ static SearchResults ID(Board &board, SearchParams params, SearchContext& ctx) {
|
|||||||
|
|
||||||
aw.Set(currentResults.score);
|
aw.Set(currentResults.score);
|
||||||
|
|
||||||
if (ctx.searchStopped) {
|
if (ctx->searchStopped) {
|
||||||
break;
|
break;
|
||||||
} else {
|
} else {
|
||||||
if (currentResults.bestMove) {
|
if (currentResults.bestMove) {
|
||||||
@@ -583,24 +583,24 @@ static SearchResults ID(Board &board, SearchParams params, SearchContext& ctx) {
|
|||||||
if constexpr (mode == normal || mode == nodesMode) {
|
if constexpr (mode == normal || mode == nodesMode) {
|
||||||
std::cout << "info ";
|
std::cout << "info ";
|
||||||
std::cout << "depth " << depth;
|
std::cout << "depth " << depth;
|
||||||
std::cout << " seldepth " << ctx.seldepth;
|
std::cout << " seldepth " << ctx->seldepth;
|
||||||
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 " << ctx.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;
|
||||||
|
|
||||||
if constexpr (mode == normal) {
|
if constexpr (mode == normal) {
|
||||||
if (ctx.sw.GetElapsedMS() >= softTime) {
|
if (ctx->sw.GetElapsedMS() >= softTime) {
|
||||||
ctx.searchStopped = true;
|
ctx->searchStopped = true;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else if constexpr (mode == datagen) {
|
} else if constexpr (mode == datagen) {
|
||||||
if (ctx.nodes >= DATAGEN::SOFT_NODES) {
|
if (ctx->nodes >= DATAGEN::SOFT_NODES) {
|
||||||
ctx.searchStopped = true;
|
ctx->searchStopped = true;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -611,17 +611,17 @@ static SearchResults ID(Board &board, SearchParams params, SearchContext& ctx) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
template <searchMode mode>
|
template <searchMode mode>
|
||||||
SearchResults SearchPosition(Board &board, SearchParams params, SearchContext& ctx) {
|
SearchResults SearchPosition(Board &board, SearchParams params, SearchContext* ctx) {
|
||||||
ctx.searchStopped = false;
|
ctx->searchStopped = false;
|
||||||
ctx.seldepth = 0;
|
ctx->seldepth = 0;
|
||||||
if constexpr (mode != bench) {
|
if constexpr (mode != bench) {
|
||||||
ctx.nodes = 0;
|
ctx->nodes = 0;
|
||||||
|
|
||||||
if constexpr (mode == nodesMode) {
|
if constexpr (mode == nodesMode) {
|
||||||
ctx.nodesToGo = params.nodes;
|
ctx->nodesToGo = params.nodes;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
ctx.pvLine.Clear();
|
ctx->pvLine.Clear();
|
||||||
|
|
||||||
SearchResults results = ID<mode>(board, params, ctx);
|
SearchResults results = ID<mode>(board, params, ctx);
|
||||||
|
|
||||||
@@ -656,16 +656,16 @@ int MoveEstimatedValue(Board& board, Move& move) {
|
|||||||
return value;
|
return value;
|
||||||
}
|
}
|
||||||
|
|
||||||
template SearchResults PVS<true, searchMode::bench>(Board&, int, int, int, int, SearchContext& ctx, bool cutnode);
|
template SearchResults PVS<true, searchMode::bench>(Board&, int, int, int, int, SearchContext* ctx, bool cutnode);
|
||||||
template SearchResults PVS<false, searchMode::bench>(Board&, int, int, int, int, SearchContext& ctx, bool cutnode);
|
template SearchResults PVS<false, searchMode::bench>(Board&, int, int, int, int, SearchContext* ctx, bool cutnode);
|
||||||
template SearchResults PVS<true, searchMode::normal>(Board&, int, int, int, int, SearchContext& ctx, bool cutnode);
|
template SearchResults PVS<true, searchMode::normal>(Board&, int, int, int, int, SearchContext* ctx, bool cutnode);
|
||||||
template SearchResults PVS<false, searchMode::normal>(Board&, int, int, int, int, SearchContext& ctx, bool cutnode);
|
template SearchResults PVS<false, searchMode::normal>(Board&, int, int, int, int, SearchContext* ctx, bool cutnode);
|
||||||
template SearchResults PVS<true, searchMode::datagen>(Board&, int, int, int, int, SearchContext& ctx, bool cutnode);
|
template SearchResults PVS<true, searchMode::datagen>(Board&, int, int, int, int, SearchContext* ctx, bool cutnode);
|
||||||
template SearchResults PVS<false, searchMode::datagen>(Board&, int, int, int, int, SearchContext& ctx, bool cutnode);
|
template SearchResults PVS<false, searchMode::datagen>(Board&, int, int, int, int, SearchContext* ctx, bool cutnode);
|
||||||
|
|
||||||
template SearchResults SearchPosition<normal>(Board &board, SearchParams params, SearchContext& ctx);
|
template SearchResults SearchPosition<normal>(Board &board, SearchParams params, SearchContext* ctx);
|
||||||
template SearchResults SearchPosition<bench>(Board &board, SearchParams params, SearchContext& ctx);
|
template SearchResults SearchPosition<bench>(Board &board, SearchParams params, SearchContext* ctx);
|
||||||
template SearchResults SearchPosition<datagen>(Board &board, SearchParams params, SearchContext& ctx);
|
template SearchResults SearchPosition<datagen>(Board &board, SearchParams params, SearchContext* ctx);
|
||||||
template SearchResults SearchPosition<nodesMode>(Board &board, SearchParams params, SearchContext& ctx);
|
template SearchResults SearchPosition<nodesMode>(Board &board, SearchParams params, SearchContext* ctx);
|
||||||
|
|
||||||
}
|
}
|
||||||
+8
-12
@@ -7,6 +7,8 @@
|
|||||||
#include "stopwatch.h"
|
#include "stopwatch.h"
|
||||||
#include "tt.h"
|
#include "tt.h"
|
||||||
|
|
||||||
|
#include "../external/multi_array.h"
|
||||||
|
|
||||||
namespace SEARCH {
|
namespace SEARCH {
|
||||||
|
|
||||||
enum searchMode {
|
enum searchMode {
|
||||||
@@ -34,7 +36,7 @@ void InitLMRTable();
|
|||||||
|
|
||||||
class History {
|
class History {
|
||||||
private:
|
private:
|
||||||
int historyMoves[2][64][64];
|
MultiArray<int, 2, 64, 64> historyMoves;
|
||||||
public:
|
public:
|
||||||
void Update(bool stm, Move move, int bonus) {
|
void Update(bool stm, Move move, int bonus) {
|
||||||
int clampedBonus = std::clamp(bonus, -MAX_HISTORY, MAX_HISTORY);
|
int clampedBonus = std::clamp(bonus, -MAX_HISTORY, MAX_HISTORY);
|
||||||
@@ -43,13 +45,7 @@ public:
|
|||||||
}
|
}
|
||||||
|
|
||||||
void Clear() {
|
void Clear() {
|
||||||
for (auto &side : historyMoves) {
|
std::fill(&historyMoves[0][0][0], &historyMoves[0][0][0] + sizeof(historyMoves) / sizeof(int), 0);
|
||||||
for (auto &from : side) {
|
|
||||||
for (auto &to : from) {
|
|
||||||
to = 0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
auto& operator[](int index) {
|
auto& operator[](int index) {
|
||||||
@@ -131,12 +127,12 @@ public:
|
|||||||
bool SEE(Board& board, Move& move, int threshold);
|
bool SEE(Board& board, Move& move, int threshold);
|
||||||
|
|
||||||
template <bool isPV, searchMode mode>
|
template <bool isPV, searchMode mode>
|
||||||
SearchResults PVS(Board& board, int depth, int alpha, int beta, int ply, SearchContext& ctx, bool cutnode);
|
SearchResults PVS(Board& board, int depth, int alpha, int beta, int ply, SearchContext* ctx, bool cutnode);
|
||||||
|
|
||||||
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, SearchContext& ctx);
|
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,
|
||||||
@@ -147,7 +143,7 @@ constexpr int moveScoreTable[6][6] = {
|
|||||||
100, 200, 300, 400, 500, 600
|
100, 200, 300, 400, 500, 600
|
||||||
};
|
};
|
||||||
|
|
||||||
void ListScores(Board &board, SearchContext& ctx);
|
void ListScores(Board &board, SearchContext* ctx);
|
||||||
|
|
||||||
int MoveEstimatedValue(Board& board, Move& move);
|
int MoveEstimatedValue(Board& board, Move& move);
|
||||||
}
|
}
|
||||||
+11
-11
@@ -12,7 +12,7 @@
|
|||||||
#include "utils.h"
|
#include "utils.h"
|
||||||
#include "datagen.h"
|
#include "datagen.h"
|
||||||
|
|
||||||
static void ParsePosition(Board &board, std::string_view command, SEARCH::SearchContext& ctx) {
|
static void ParsePosition(Board &board, std::string_view command, SEARCH::SearchContext* ctx) {
|
||||||
if (command.find("startpos") != std::string::npos) {
|
if (command.find("startpos") != std::string::npos) {
|
||||||
board.SetByFen(StartingFen);
|
board.SetByFen(StartingFen);
|
||||||
}
|
}
|
||||||
@@ -33,7 +33,7 @@ static void ParsePosition(Board &board, std::string_view command, SEARCH::Search
|
|||||||
|
|
||||||
for (std::string_view move : moves) {
|
for (std::string_view move : moves) {
|
||||||
bool isLegal = board.MakeMove(UTILS::parseMove(board, move));
|
bool isLegal = board.MakeMove(UTILS::parseMove(board, move));
|
||||||
ctx.positionHistory[board.positionIndex] = board.hashKey;
|
ctx->positionHistory[board.positionIndex] = board.hashKey;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -69,7 +69,7 @@ static int ReadParam(std::string param, std::string &command) {
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void ParseGo(Board &board, std::string &command, SEARCH::SearchContext& ctx) {
|
static void ParseGo(Board &board, std::string &command, SEARCH::SearchContext* ctx) {
|
||||||
SearchParams params;
|
SearchParams params;
|
||||||
|
|
||||||
params.wtime = ReadParam("wtime", command);
|
params.wtime = ReadParam("wtime", command);
|
||||||
@@ -93,10 +93,10 @@ static void ParseGo(Board &board, std::string &command, SEARCH::SearchContext& c
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static void SetOption(std::string &command, SEARCH::SearchContext& ctx) {
|
static void SetOption(std::string &command, SEARCH::SearchContext* ctx) {
|
||||||
if (command.find("Hash") != std::string::npos) {
|
if (command.find("Hash") != std::string::npos) {
|
||||||
U64 hashSize = (ReadParam("value", command) * 1000000) / sizeof(TTEntry);
|
U64 hashSize = (ReadParam("value", command) * 1000000) / sizeof(TTEntry);
|
||||||
ctx.TT.Resize(hashSize);
|
ctx->TT.Resize(hashSize);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -111,7 +111,7 @@ static void PrintEngineInfo() {
|
|||||||
void UCILoop(Board &board) {
|
void UCILoop(Board &board) {
|
||||||
std::string input = "";
|
std::string input = "";
|
||||||
|
|
||||||
SEARCH::SearchContext ctx;
|
SEARCH::SearchContext *ctx = new SEARCH::SearchContext();
|
||||||
|
|
||||||
// main loop
|
// main loop
|
||||||
while (true) {
|
while (true) {
|
||||||
@@ -135,11 +135,11 @@ void UCILoop(Board &board) {
|
|||||||
board.SetByFen(StartingFen);
|
board.SetByFen(StartingFen);
|
||||||
|
|
||||||
// Clearing
|
// Clearing
|
||||||
ctx.TT.Clear();
|
ctx->TT.Clear();
|
||||||
|
|
||||||
ctx.killerMoves = {};
|
ctx->killerMoves = {};
|
||||||
ctx.history.Clear();
|
ctx->history.Clear();
|
||||||
ctx.positionHistory = {};
|
ctx->positionHistory = {};
|
||||||
|
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
@@ -159,7 +159,7 @@ void UCILoop(Board &board) {
|
|||||||
// parse UCI "stop" command
|
// parse UCI "stop" command
|
||||||
if (input.find("stop") != std::string::npos) {
|
if (input.find("stop") != std::string::npos) {
|
||||||
// stop the loop
|
// stop the loop
|
||||||
ctx.searchStopped = true;
|
ctx->searchStopped = true;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user