Skip to content

Commit

Permalink
use a single info struct
Browse files Browse the repository at this point in the history
  • Loading branch information
bftjoe committed Jan 26, 2025
1 parent 86ceb98 commit 95bb6dd
Show file tree
Hide file tree
Showing 15 changed files with 145 additions and 140 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# net file
nn.net

# Prerequisites
*.d

Expand Down
5 changes: 2 additions & 3 deletions src/bench.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ const char* benchmarkfens[52] = {
void StartBench(int depth) {
// init all
UciOptions uciOptions;
ThreadData* td(new ThreadData());
ThreadData* td = &mainTD;
uint64_t totalNodes = 0;
InitTT(64);
InitNewGame(td);
Expand All @@ -75,13 +75,12 @@ void StartBench(int depth) {
ParseFen(benchmarkfens[positions], &td->pos);
std::cout << "\nPosition: " << positions + 1 << " fen: " << benchmarkfens[positions] << std::endl;
RootSearch(depth, td, &uciOptions);
totalNodes += td->info.nodes;
totalNodes += td->nodes;
}
auto end = std::chrono::steady_clock::now();
auto totalTime = std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count();
std::cout << "\n";
std::cout << totalNodes << " nodes " << signed(totalNodes / (totalTime + 1) * 1000) << " nps" << std::endl;
delete td;
}

void BenchInference() {
Expand Down
10 changes: 4 additions & 6 deletions src/init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,6 @@ void InitNewGame(ThreadData* td) {
// Extract data structures from ThreadData
Position* pos = &td->pos;
SearchData* sd = &td->sd;
SearchInfo* info = &td->info;

CleanHistories(sd);

Expand All @@ -232,11 +231,10 @@ void InitNewGame(ThreadData* td) {

std::memset(sd->counterMoves, NOMOVE, sizeof(sd->counterMoves));

// Reset plies and search info
info->starttime = GetTimeMs();
info->stopped = 0;
info->nodes = 0;
info->seldepth = 0;
// Reset plies and search info
info.starttime = GetTimeMs();
setStop(false);
info.seldepth = 0;
// Clear TT
ClearTT();

Expand Down
22 changes: 11 additions & 11 deletions src/io.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -165,27 +165,27 @@ std::string Pick_color(int score) {
}

// Prints the uci output
void PrintUciOutput(const int score, const int depth, const ThreadData* td, const UciOptions* options) {
void PrintUciOutput(const int score, const int depth, const UciOptions* options) {
// We are benching the engine and we don't care about the output
if (tryhardmode)
return;
// This handles the basic console output
long time = GetTimeMs() - td->info.starttime;
uint64_t nodes = td->info.nodes + GetTotalNodes();
long time = GetTimeMs() - info.starttime;
uint64_t nodes = mainTD.nodes + GetTotalNodes();

uint64_t nps = nodes / (time + !time) * 1000;
if (print_uci) {
if (score > -MATE_SCORE && score < -MATE_FOUND)
std::cout << "info score mate " << -(score + MATE_SCORE) / 2 << " depth " << depth << " seldepth " << td->info.seldepth << " multipv " << options->MultiPV << " nodes " << nodes <<
" nps " << nps << " time " << GetTimeMs() - td->info.starttime << " pv ";
std::cout << "info score mate " << -(score + MATE_SCORE) / 2 << " depth " << depth << " seldepth " << info.seldepth << " multipv " << options->MultiPV << " nodes " << nodes <<
" nps " << nps << " time " << GetTimeMs() - info.starttime << " pv ";

else if (score > MATE_FOUND && score < MATE_SCORE)
std::cout << "info score mate " << (MATE_SCORE - score) / 2 + 1 << " depth " << depth << " seldepth " << td->info.seldepth << " multipv " << options->MultiPV << " nodes " << nodes <<
" nps " << nps << " time " << GetTimeMs() - td->info.starttime << " pv ";
std::cout << "info score mate " << (MATE_SCORE - score) / 2 + 1 << " depth " << depth << " seldepth " << info.seldepth << " multipv " << options->MultiPV << " nodes " << nodes <<
" nps " << nps << " time " << GetTimeMs() - info.starttime << " pv ";

else
std::cout << "info score cp " << score << " depth " << depth << " seldepth " << td->info.seldepth << " multipv " << options->MultiPV << " nodes " << nodes <<
" nps " << nps << " hashfull "<< GetHashfull() << " time " << GetTimeMs() - td->info.starttime << " pv ";
std::cout << "info score cp " << score << " depth " << depth << " seldepth " << info.seldepth << " multipv " << options->MultiPV << " nodes " << nodes <<
" nps " << nps << " hashfull "<< GetHashfull() << " time " << GetTimeMs() - info.starttime << " pv ";

// loop over the moves within a PV line
for (int count = 0; count < std::max(pvTable.pvLength[0], 1); count++) {
Expand Down Expand Up @@ -262,7 +262,7 @@ void PrintUciOutput(const int score, const int depth, const ThreadData* td, cons

// Pretty print search info
std::cout << std::setw(3) << depth << "/";
std::cout << std::left << std::setw(3) << td->info.seldepth;
std::cout << std::left << std::setw(3) << info.seldepth;

std::cout << std::right << std::setw(8) << time_string;
std::cout << std::right << std::setw(10) << node_string;
Expand All @@ -279,4 +279,4 @@ void PrintUciOutput(const int score, const int depth, const ThreadData* td, cons
// print new line
std::cout << std::endl;
}
}
}
2 changes: 1 addition & 1 deletion src/io.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,4 @@ void PrintMoveList(const MoveList* list);

[[nodiscard]] char* FormatMove(const Move move);

void PrintUciOutput(const int score, const int depth, const ThreadData* td, const UciOptions* options);
void PrintUciOutput(const int score, const int depth, const UciOptions* options);
24 changes: 12 additions & 12 deletions src/position.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "magic.h"
#include "init.h"
#include "cuckoo.h"
#include "search.h"

NNUE nnue = NNUE();

Expand All @@ -27,17 +28,16 @@ void ResetBoard(Position* pos) {
pos->state.plyFromNull = 0;
}

void ResetInfo(SearchInfo* info) {
info->depth = 0;
info->nodes = 0;
info->starttime = 0;
info->stoptimeOpt = 0;
info->stoptimeMax = 0;
info->movestogo = -1;
info->stopped = false;
info->timeset = false;
info->movetimeset = false;
info->nodeset = false;
void ResetInfo() {
info.depth = 0;
info.starttime = 0;
info.stoptimeOpt = 0;
info.stoptimeMax = 0;
info.movestogo = -1;
info.timeset = false;
info.movetimeset = false;
info.nodeset = false;
setStop(false);
}

// Generates zobrist key from scratch
Expand Down Expand Up @@ -522,4 +522,4 @@ bool hasGameCycle(Position* pos, int ply) {
}
}
return false;
}
}
2 changes: 1 addition & 1 deletion src/position.h
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ void ParseFen(const std::string& command, Position* pos);
// Parse a string of moves in coordinate format and plays them
void parse_moves(const std::string& moves, Position* pos);

void ResetInfo(SearchInfo* info);
void ResetInfo();

// Retrieve a generic piece (useful when we don't know what type of piece we are dealing with
[[nodiscard]] Bitboard GetPieceBB(const Position* pos, const int piecetype);
Expand Down
70 changes: 31 additions & 39 deletions src/search.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#include <algorithm>
#include <cassert>
#include <iostream>
#include <cmath>
#include "bitboard.h"
#include "move.h"
#include "search.h"
Expand Down Expand Up @@ -80,23 +79,19 @@ bool IsDraw(Position* pos) {

// ClearForSearch handles the cleaning of the post and the info parameters to start search from a clean state
void ClearForSearch(ThreadData* td) {
// Extract data structures from ThreadData
SearchInfo* info = &td->info;

// Reset plies and search info
info->starttime = GetTimeMs();
info->nodes = 0;
info->seldepth = 0;
td->nodes = 0;

// Main thread clears pvTable, nodeSpentTable, and unpauses any eventual search thread
if (td->id == 0) {
// Reset plies and search info
info.starttime = GetTimeMs();
info.seldepth = 0;
// Clean the Pv array
std::memset(&pvTable, 0, sizeof(pvTable));
// Clean the node table
std::memset(nodeSpentTable, 0, sizeof(nodeSpentTable));

for (auto& helper_thread : threads_data)
helper_thread.info.stopped = false;
setStop(false);
}
}

Expand Down Expand Up @@ -211,7 +206,6 @@ void RootSearch(int depth, ThreadData* td, UciOptions* options) {

// Init thread_data objects
for (size_t i = 0; i < threads_data.size(); i++) {
threads_data[i].info = td->info;
threads_data[i].pos = td->pos;
}

Expand Down Expand Up @@ -252,7 +246,7 @@ void SearchPosition(int startDepth, int finalDepth, ThreadData* td, UciOptions*
averageScore = averageScore == SCORE_NONE ? score : (averageScore + score) / 2;

// If we stop (not at an exact depth) we print a final info string
printFinalInfoString = td->info.stopped;
printFinalInfoString = stop();

// Only the main thread handles time related tasks
if (td->id == 0) {
Expand All @@ -275,30 +269,30 @@ void SearchPosition(int startDepth, int finalDepth, ThreadData* td, UciOptions*

// use the previous search to adjust some of the time management parameters, do not scale movetime time controls
if ( td->RootDepth > 7
&& td->info.timeset) {
ScaleTm(td, bestMoveStabilityFactor, evalStabilityFactor);
&& info.timeset) {
ScaleTm(bestMoveStabilityFactor, evalStabilityFactor);
}

// check if we just cleared a depth and more than OptTime passed, or we used more than the give nodes
if (StopEarly(&td->info) || NodesOver(&td->info))
if (StopEarly() || NodesOver())
// Stop main-thread search
td->info.stopped = true;
setStop(true);
}

// Print a final info string if we have to
if (td->id == 0 && printFinalInfoString)
PrintUciOutput(prevScore, currentDepth - 1, td, options);
PrintUciOutput(prevScore, currentDepth - 1, options);

// stop calculating and return best move so far
if (td->info.stopped)
if (stop())
break;

// If it's the main thread print the uci output
if (td->id == 0)
PrintUciOutput(score, currentDepth, td, options);
PrintUciOutput(score, currentDepth, options);

// Seldepth should only be related to the current ID loop
td->info.seldepth = 0;
info.seldepth = 0;
prevScore = score;
}
}
Expand Down Expand Up @@ -337,14 +331,14 @@ int AspirationWindowSearch(int prev_eval, int depth, ThreadData* td) {
score = Negamax<true>(alpha, beta, depth, false, td, ss);

// Check if more than Maxtime passed and we have to stop
if (td->id == 0 && TimeOver(&td->info)) {
if (td->id == 0 && TimeOver()) {
StopHelperThreads();
td->info.stopped = true;
setStop(true);
break;
}

// Stop calculating and return best move so far
if (td->info.stopped) break;
if (stop()) break;

// We fell outside the window, so try again with a bigger window, since we failed low we can adjust beta to decrease the total window size
if (score <= alpha) {
Expand Down Expand Up @@ -372,7 +366,6 @@ int Negamax(int alpha, int beta, int depth, const bool cutNode, ThreadData* td,
// Extract data structures from ThreadData
Position* pos = &td->pos;
SearchData* sd = &td->sd;
SearchInfo* info = &td->info;
const bool mainT = td->id == 0;

// Initialize the node
Expand All @@ -390,8 +383,8 @@ int Negamax(int alpha, int beta, int depth, const bool cutNode, ThreadData* td,
pvTable.pvLength[ss->ply] = ss->ply;

// Check for the highest depth reached in search to report it to the cli
if (ss->ply > info->seldepth)
info->seldepth = ss->ply;
if (ss->ply > info.seldepth)
info.seldepth = ss->ply;

// Check for early return conditions
if (!rootNode) {
Expand All @@ -417,9 +410,9 @@ int Negamax(int alpha, int beta, int depth, const bool cutNode, ThreadData* td,
return Quiescence<pvNode>(alpha, beta, td, ss);

// check if more than Maxtime passed and we have to stop
if (mainT && TimeOver(&td->info)) {
if (mainT && TimeOver()) {
StopHelperThreads();
td->info.stopped = true;
setStop(true);
return 0;
}

Expand Down Expand Up @@ -596,7 +589,7 @@ int Negamax(int alpha, int beta, int depth, const bool cutNode, ThreadData* td,
ss->contHistEntry = &sd->contHist[PieceTo(move)];

// increment nodes count
info->nodes++;
td->nodes++;

// Play the move
MakeMove<true>(move, pos);
Expand Down Expand Up @@ -731,8 +724,8 @@ int Negamax(int alpha, int beta, int depth, const bool cutNode, ThreadData* td,
AddMove(move, isQuiet ? &quietMoves : &noisyMoves);

// increment nodes count
info->nodes++;
const uint64_t nodesBeforeSearch = info->nodes;
td->nodes++;
const uint64_t nodesBeforeSearch = td->nodes;
// Conditions to consider LMR. Calculate how much we should reduce the search depth.
if (totalMoves > 1 && depth >= 3 && (isQuiet || !ttPv)) {

Expand Down Expand Up @@ -809,9 +802,9 @@ int Negamax(int alpha, int beta, int depth, const bool cutNode, ThreadData* td,
// take move back
UnmakeMove(move, pos);
if (mainT && rootNode)
nodeSpentTable[FromTo(move)] += info->nodes - nodesBeforeSearch;
nodeSpentTable[FromTo(move)] += td->nodes - nodesBeforeSearch;

if (info->stopped)
if (stop())
return 0;

// If the score of the current move is the best we've found until now
Expand Down Expand Up @@ -882,17 +875,16 @@ template <bool pvNode>
int Quiescence(int alpha, int beta, ThreadData* td, SearchStack* ss) {
Position* pos = &td->pos;
SearchData* sd = &td->sd;
SearchInfo* info = &td->info;
const bool inCheck = pos->getCheckers();
// tte is an TT entry, it will store the values fetched from the TT
TTEntry tte;
int bestScore;
int rawEval;

// check if more than Maxtime passed and we have to stop
if (td->id == 0 && TimeOver(&td->info)) {
if (td->id == 0 && TimeOver()) {
StopHelperThreads();
td->info.stopped = true;
setStop(true);
return 0;
}

Expand Down Expand Up @@ -991,14 +983,14 @@ int Quiescence(int alpha, int beta, ThreadData* td, SearchStack* ss) {
// Play the move
MakeMove<true>(move, pos);
// increment nodes count
info->nodes++;
td->nodes++;
// Call Quiescence search recursively
const int score = -Quiescence<pvNode>(-beta, -alpha, td, ss + 1);

// take move back
UnmakeMove(move, pos);

if (info->stopped)
if (stop())
return 0;

// If the score of the current move is the best we've found until now
Expand Down Expand Up @@ -1030,4 +1022,4 @@ int Quiescence(int alpha, int beta, ThreadData* td, SearchStack* ss) {
StoreTTEntry(pos->posKey, MoveToTT(bestmove), ScoreToTT(bestScore, ss->ply), rawEval, bound, 0, pvNode, ttPv);

return bestScore;
}
}
Loading

0 comments on commit 95bb6dd

Please sign in to comment.