No globals #50
No files matched your search
@@ -75,16 +75,16 @@ void RunBenchmark() {
|
|||||||
|
|
||||||
history.Clear();
|
history.Clear();
|
||||||
|
|
||||||
nodes = 0;
|
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]);
|
||||||
SearchPosition<bench>(board, SearchParams());
|
SearchPosition<bench>(board, SearchParams(), ctx);
|
||||||
}
|
}
|
||||||
|
|
||||||
std::cout << nodes << " nodes " << int(nodes/sw.GetElapsedSec()) << " nps" << std::endl;
|
std::cout << ctx.nodes << " nodes " << int(ctx.nodes/sw.GetElapsedSec()) << " nps" << std::endl;
|
||||||
nodes = 0;
|
|
||||||
TT.Clear();
|
TT.Clear();
|
||||||
benchStarted = false;
|
benchStarted = false;
|
||||||
std::memset(killerMoves, 0, sizeof(killerMoves));
|
std::memset(killerMoves, 0, sizeof(killerMoves));
|
||||||
|
|||||||
@@ -393,7 +393,6 @@ bool Board::MakeMove(Move move) {
|
|||||||
positionIndex++;
|
positionIndex++;
|
||||||
positionHistory[positionIndex] = hashKey;
|
positionHistory[positionIndex] = hashKey;
|
||||||
|
|
||||||
SEARCH::nodes++;
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+3
-1
@@ -105,6 +105,8 @@ void PlayGame(std::atomic<int>& positions, U64 targetPositions, std::vector<Game
|
|||||||
std::memset(SEARCH::killerMoves, 0, sizeof(SEARCH::killerMoves));
|
std::memset(SEARCH::killerMoves, 0, sizeof(SEARCH::killerMoves));
|
||||||
SEARCH::history.Clear();
|
SEARCH::history.Clear();
|
||||||
|
|
||||||
|
SEARCH::SearchContext ctx;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
PlayRandMoves(board);
|
PlayRandMoves(board);
|
||||||
int staticEval = HCE::Evaluate(board);
|
int staticEval = HCE::Evaluate(board);
|
||||||
@@ -112,7 +114,7 @@ void PlayGame(std::atomic<int>& positions, U64 targetPositions, std::vector<Game
|
|||||||
MOVEGEN::GenerateMoves<All>(board);
|
MOVEGEN::GenerateMoves<All>(board);
|
||||||
|
|
||||||
while (!IsGameOver(board)) {
|
while (!IsGameOver(board)) {
|
||||||
SEARCH::SearchResults results = SEARCH::SearchPosition<SEARCH::datagen>(board, SearchParams());
|
SEARCH::SearchResults results = SEARCH::SearchPosition<SEARCH::datagen>(board, SearchParams(), ctx);
|
||||||
|
|
||||||
if (!results.bestMove) break;
|
if (!results.bestMove) break;
|
||||||
safeResults = results;
|
safeResults = results;
|
||||||
|
|||||||
+34
-32
@@ -141,16 +141,16 @@ static int GetReductions(Board &board, Move &move, int depth, int moveSeen, int
|
|||||||
}
|
}
|
||||||
|
|
||||||
template <searchMode mode>
|
template <searchMode mode>
|
||||||
static SearchResults Quiescence(Board board, int alpha, int beta, int ply) {
|
static SearchResults Quiescence(Board board, int alpha, int beta, int ply, SearchContext& ctx) {
|
||||||
if constexpr (mode != nodesMode) {
|
if constexpr (mode != nodesMode) {
|
||||||
if (!benchStarted && nodes % 1024 == 0) {
|
if (!benchStarted && ctx.nodes % 1024 == 0) {
|
||||||
if (sw.GetElapsedMS() >= timeToSearch) {
|
if (sw.GetElapsedMS() >= timeToSearch) {
|
||||||
StopSearch();
|
StopSearch();
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if (nodes > nodesToGo) {
|
if (ctx.nodes > nodesToGo) {
|
||||||
StopSearch();
|
StopSearch();
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
@@ -182,7 +182,8 @@ static SearchResults Quiescence(Board board, int alpha, int beta, int ply) {
|
|||||||
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;
|
||||||
int score = -Quiescence<mode>(copy, -beta, -alpha, ply + 1).score;
|
ctx.nodes++;
|
||||||
|
int score = -Quiescence<mode>(copy, -beta, -alpha, ply + 1, ctx).score;
|
||||||
positionIndex--;
|
positionIndex--;
|
||||||
|
|
||||||
if (score >= beta) {
|
if (score >= beta) {
|
||||||
@@ -203,23 +204,23 @@ static SearchResults Quiescence(Board board, int alpha, int beta, int ply) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
template <bool isPV, searchMode mode>
|
template <bool isPV, searchMode mode>
|
||||||
SearchResults PVS(Board board, int depth, int alpha, int beta, int ply) {
|
SearchResults PVS(Board board, int depth, int alpha, int beta, int ply, SearchContext& ctx) {
|
||||||
if constexpr (mode != bench || mode != nodesMode) {
|
if constexpr (mode != bench || mode != nodesMode) {
|
||||||
if (nodes % 1024 == 0) {
|
if (ctx.nodes % 1024 == 0) {
|
||||||
if constexpr (mode == normal) {
|
if constexpr (mode == normal) {
|
||||||
if (sw.GetElapsedMS() >= timeToSearch) {
|
if (sw.GetElapsedMS() >= timeToSearch) {
|
||||||
StopSearch();
|
StopSearch();
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
} else if constexpr (mode == datagen) {
|
} else if constexpr (mode == datagen) {
|
||||||
if (nodes >= DATAGEN::HARD_NODES) {
|
if (ctx.nodes >= DATAGEN::HARD_NODES) {
|
||||||
StopSearch();
|
StopSearch();
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else if constexpr (mode == nodesMode) {
|
} else if constexpr (mode == nodesMode) {
|
||||||
if (nodes > nodesToGo) {
|
if (ctx.nodes > nodesToGo) {
|
||||||
StopSearch();
|
StopSearch();
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
@@ -237,7 +238,7 @@ SearchResults PVS(Board board, int depth, int alpha, int beta, int ply) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (depth <= 0) return Quiescence<mode>(board, alpha, beta, ply);
|
if (depth <= 0) return Quiescence<mode>(board, alpha, beta, ply, ctx);
|
||||||
|
|
||||||
const int staticEval = HCE::Evaluate(board);
|
const int staticEval = HCE::Evaluate(board);
|
||||||
|
|
||||||
@@ -257,7 +258,7 @@ SearchResults PVS(Board board, int depth, int alpha, int beta, int ply) {
|
|||||||
// Always legal so we dont check it
|
// Always legal so we dont check it
|
||||||
|
|
||||||
doingNullMove = true;
|
doingNullMove = true;
|
||||||
int score = -PVS<false, mode>(copy, depth - 3, -beta, -beta + 1, ply + 1).score;
|
int score = -PVS<false, mode>(copy, depth - 3, -beta, -beta + 1, ply + 1, ctx).score;
|
||||||
doingNullMove = false;
|
doingNullMove = false;
|
||||||
|
|
||||||
if (searchStopped) return 0;
|
if (searchStopped) return 0;
|
||||||
@@ -296,6 +297,7 @@ SearchResults PVS(Board board, int depth, int alpha, int beta, int ply) {
|
|||||||
bool isLegal = copy.MakeMove(currMove);
|
bool isLegal = copy.MakeMove(currMove);
|
||||||
|
|
||||||
if (!isLegal) continue;
|
if (!isLegal) continue;
|
||||||
|
ctx.nodes++;
|
||||||
|
|
||||||
int reductions = GetReductions(board, currMove, depth, moveSeen, ply);
|
int reductions = GetReductions(board, currMove, depth, moveSeen, ply);
|
||||||
|
|
||||||
@@ -304,23 +306,23 @@ SearchResults PVS(Board board, int depth, int alpha, int beta, int ply) {
|
|||||||
// First move (suspected PV node)
|
// First move (suspected PV node)
|
||||||
if (!moveSeen) {
|
if (!moveSeen) {
|
||||||
// Full search
|
// Full search
|
||||||
score = -PVS<isPV, mode>(copy, newDepth, -beta, -alpha, ply + 1).score;
|
score = -PVS<isPV, mode>(copy, newDepth, -beta, -alpha, ply + 1, ctx).score;
|
||||||
} else if (reductions) {
|
} else if (reductions) {
|
||||||
// Null-window search with reductions
|
// Null-window search with reductions
|
||||||
score = -PVS<false, mode>(copy, newDepth - reductions, -alpha-1, -alpha, ply + 1).score;
|
score = -PVS<false, mode>(copy, newDepth - reductions, -alpha-1, -alpha, ply + 1, ctx).score;
|
||||||
|
|
||||||
if (score > alpha) {
|
if (score > alpha) {
|
||||||
// Null-window search now without the reduction
|
// Null-window search now without the reduction
|
||||||
score = -PVS<false, mode>(copy, newDepth, -alpha-1, -alpha, ply + 1).score;
|
score = -PVS<false, mode>(copy, newDepth, -alpha-1, -alpha, ply + 1, ctx).score;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// Null-window search
|
// Null-window search
|
||||||
score = -PVS<false, mode>(copy, newDepth, -alpha-1, -alpha, ply + 1).score;
|
score = -PVS<false, mode>(copy, newDepth, -alpha-1, -alpha, ply + 1, ctx).score;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check if we need to do full window re-search
|
// Check if we need to do full window re-search
|
||||||
if (moveSeen && score > alpha && score < beta) {
|
if (moveSeen && score > alpha && score < beta) {
|
||||||
score = -PVS<isPV, mode>(copy, newDepth, -beta, -alpha, ply + 1).score;
|
score = -PVS<isPV, mode>(copy, newDepth, -beta, -alpha, ply + 1, ctx).score;
|
||||||
}
|
}
|
||||||
|
|
||||||
moveSeen++;
|
moveSeen++;
|
||||||
@@ -377,7 +379,7 @@ SearchResults PVS(Board board, int depth, int alpha, int beta, int ply) {
|
|||||||
|
|
||||||
// Iterative deepening
|
// Iterative deepening
|
||||||
template <searchMode mode>
|
template <searchMode mode>
|
||||||
static SearchResults ID(Board &board, SearchParams params) {
|
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;
|
||||||
|
|
||||||
@@ -405,7 +407,7 @@ static SearchResults ID(Board &board, SearchParams params) {
|
|||||||
timeToSearch = std::max((fullTime / 20) + (inc / 2), 4);
|
timeToSearch = std::max((fullTime / 20) + (inc / 2), 4);
|
||||||
int softTime = timeToSearch * 0.65;
|
int softTime = timeToSearch * 0.65;
|
||||||
|
|
||||||
SearchResults currentResults = PVS<true, mode>(board, depth, alpha, beta, ply);
|
SearchResults currentResults = PVS<true, mode>(board, depth, alpha, beta, ply, ctx);
|
||||||
elapsed = sw.GetElapsedMS();
|
elapsed = sw.GetElapsedMS();
|
||||||
|
|
||||||
// If we fell outside the window, try again with full width
|
// If we fell outside the window, try again with full width
|
||||||
@@ -438,7 +440,7 @@ static SearchResults ID(Board &board, SearchParams params) {
|
|||||||
std::cout << "depth " << depth;
|
std::cout << "depth " << depth;
|
||||||
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 " << nodes << " nps " << int(nodes/sw.GetElapsedSec());
|
std::cout << " nodes " << ctx.nodes << " nps " << int(ctx.nodes/sw.GetElapsedSec());
|
||||||
std::cout << " hashfull " << TT.GetUsedPercentage();
|
std::cout << " hashfull " << TT.GetUsedPercentage();
|
||||||
std::cout << " pv ";
|
std::cout << " pv ";
|
||||||
pvLine.Print(0);
|
pvLine.Print(0);
|
||||||
@@ -451,7 +453,7 @@ static SearchResults ID(Board &board, SearchParams params) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else if constexpr (mode == datagen) {
|
} else if constexpr (mode == datagen) {
|
||||||
if (nodes >= DATAGEN::SOFT_NODES) {
|
if (ctx.nodes >= DATAGEN::SOFT_NODES) {
|
||||||
StopSearch();
|
StopSearch();
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@@ -463,10 +465,10 @@ static SearchResults ID(Board &board, SearchParams params) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
template <searchMode mode>
|
template <searchMode mode>
|
||||||
SearchResults SearchPosition(Board &board, SearchParams params) {
|
SearchResults SearchPosition(Board &board, SearchParams params, SearchContext& ctx) {
|
||||||
searchStopped = false;
|
searchStopped = false;
|
||||||
if constexpr (mode != bench) {
|
if constexpr (mode != bench) {
|
||||||
nodes = 0;
|
ctx.nodes = 0;
|
||||||
|
|
||||||
if constexpr (mode == nodesMode) {
|
if constexpr (mode == nodesMode) {
|
||||||
nodesToGo = params.nodes;
|
nodesToGo = params.nodes;
|
||||||
@@ -474,7 +476,7 @@ SearchResults SearchPosition(Board &board, SearchParams params) {
|
|||||||
}
|
}
|
||||||
pvLine.Clear();
|
pvLine.Clear();
|
||||||
|
|
||||||
SearchResults results = ID<mode>(board, params);
|
SearchResults results = ID<mode>(board, params, ctx);
|
||||||
|
|
||||||
if constexpr (mode != normal) return results;
|
if constexpr (mode != normal) return results;
|
||||||
|
|
||||||
@@ -489,16 +491,16 @@ void StopSearch() {
|
|||||||
searchStopped = true;
|
searchStopped = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
template SearchResults PVS<true, searchMode::bench>(Board, int, int, int, int);
|
template SearchResults PVS<true, searchMode::bench>(Board, int, int, int, int, SearchContext& ctx);
|
||||||
template SearchResults PVS<false, searchMode::bench>(Board, int, int, int, int);
|
template SearchResults PVS<false, searchMode::bench>(Board, int, int, int, int, SearchContext& ctx);
|
||||||
template SearchResults PVS<true, searchMode::normal>(Board, int, int, int, int);
|
template SearchResults PVS<true, searchMode::normal>(Board, int, int, int, int, SearchContext& ctx);
|
||||||
template SearchResults PVS<false, searchMode::normal>(Board, int, int, int, int);
|
template SearchResults PVS<false, searchMode::normal>(Board, int, int, int, int, SearchContext& ctx);
|
||||||
template SearchResults PVS<true, searchMode::datagen>(Board, int, int, int, int);
|
template SearchResults PVS<true, searchMode::datagen>(Board, int, int, int, int, SearchContext& ctx);
|
||||||
template SearchResults PVS<false, searchMode::datagen>(Board, int, int, int, int);
|
template SearchResults PVS<false, searchMode::datagen>(Board, int, int, int, int, SearchContext& ctx);
|
||||||
|
|
||||||
template SearchResults SearchPosition<normal>(Board &board, SearchParams params);
|
template SearchResults SearchPosition<normal>(Board &board, SearchParams params, SearchContext& ctx);
|
||||||
template SearchResults SearchPosition<bench>(Board &board, SearchParams params);
|
template SearchResults SearchPosition<bench>(Board &board, SearchParams params, SearchContext& ctx);
|
||||||
template SearchResults SearchPosition<datagen>(Board &board, SearchParams params);
|
template SearchResults SearchPosition<datagen>(Board &board, SearchParams params, SearchContext& ctx);
|
||||||
template SearchResults SearchPosition<nodesMode>(Board &board, SearchParams params);
|
template SearchResults SearchPosition<nodesMode>(Board &board, SearchParams params, SearchContext& ctx);
|
||||||
|
|
||||||
}
|
}
|
||||||
+9
-3
@@ -19,7 +19,6 @@ constexpr int MAX_DEPTH = 64;
|
|||||||
constexpr int MAX_PLY = 128;
|
constexpr int MAX_PLY = 128;
|
||||||
constexpr int MAX_HISTORY = 16384;
|
constexpr int MAX_HISTORY = 16384;
|
||||||
|
|
||||||
extern U64 nodes;
|
|
||||||
extern bool benchStarted;
|
extern bool benchStarted;
|
||||||
|
|
||||||
constexpr int inf = 100000;
|
constexpr int inf = 100000;
|
||||||
@@ -109,11 +108,18 @@ public:
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
class SearchContext {
|
||||||
|
public:
|
||||||
|
U64 nodes = 0;
|
||||||
|
|
||||||
|
SearchContext(){}
|
||||||
|
};
|
||||||
|
|
||||||
template <bool isPV, searchMode mode>
|
template <bool isPV, searchMode mode>
|
||||||
SearchResults PVS(Board board, int depth, int alpha, int beta, int ply);
|
SearchResults PVS(Board board, int depth, int alpha, int beta, int ply, SearchContext& ctx);
|
||||||
|
|
||||||
template <searchMode mode>
|
template <searchMode mode>
|
||||||
SearchResults SearchPosition(Board &board, SearchParams params);
|
SearchResults SearchPosition(Board &board, SearchParams params, SearchContext& ctx);
|
||||||
void StopSearch();
|
void StopSearch();
|
||||||
|
|
||||||
bool IsDraw(Board &board);
|
bool IsDraw(Board &board);
|
||||||
|
|||||||
+6
-4
@@ -68,7 +68,7 @@ static int ReadParam(std::string param, std::string &command) {
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void ParseGo(Board &board, std::string &command) {
|
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);
|
||||||
@@ -84,10 +84,10 @@ static void ParseGo(Board &board, std::string &command) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (params.nodes) {
|
if (params.nodes) {
|
||||||
auto worker = std::thread(SEARCH::SearchPosition<SEARCH::nodesMode>, std::ref(board), params);
|
auto worker = std::thread(SEARCH::SearchPosition<SEARCH::nodesMode>, std::ref(board), params, std::ref(ctx));
|
||||||
worker.detach();
|
worker.detach();
|
||||||
} else {
|
} else {
|
||||||
auto worker = std::thread(SEARCH::SearchPosition<SEARCH::normal>, std::ref(board), params);
|
auto worker = std::thread(SEARCH::SearchPosition<SEARCH::normal>, std::ref(board), params, std::ref(ctx));
|
||||||
worker.detach();
|
worker.detach();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -110,6 +110,8 @@ static void PrintEngineInfo() {
|
|||||||
void UCILoop(Board &board) {
|
void UCILoop(Board &board) {
|
||||||
std::string input = "";
|
std::string input = "";
|
||||||
|
|
||||||
|
SEARCH::SearchContext ctx;
|
||||||
|
|
||||||
// main loop
|
// main loop
|
||||||
while (true) {
|
while (true) {
|
||||||
std::getline(std::cin, input);
|
std::getline(std::cin, input);
|
||||||
@@ -144,7 +146,7 @@ void UCILoop(Board &board) {
|
|||||||
|
|
||||||
// parse UCI "go" command
|
// parse UCI "go" command
|
||||||
if (input.find("go") != std::string::npos) {
|
if (input.find("go") != std::string::npos) {
|
||||||
ParseGo(board, input);
|
ParseGo(board, input, ctx);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user