From 36f6d344a626bfe19aea503eb153594f1b04a186 Mon Sep 17 00:00:00 2001 From: nook0110 Date: Tue, 10 Nov 2026 16:12:23 +0300 Subject: [PATCH] [uci] Report nodes and nps summed over all search threads `info nodes` and `nps` counted only the main thread, so with Lazy SMP they showed roughly one thread's share of the work. Each searcher now owns a NodeCounter that only its thread writes, with relaxed atomic loads and stores that compile to a plain add, and the main engine sums its helpers' counters when it prints info, as Stockfish does. Time management and EBF still use the main thread's own iteration counts, so the search is unchanged. Co-Authored-By: Claude Opus 5.5 --- Chess/NodeCounter.h | 25 +++++++++++++++++++++++++ Chess/SearchImplementation.h | 6 ++++-- Chess/Searcher.h | 4 ++++ Chess/SimpleChessEngine.h | 28 +++++++++++++++++++++++++--- Chess/UciCommunicator.h | 3 +++ Tests/ChessEngineTests.cpp | 33 +++++++++++++++++++++++++++++++++ 6 files changed, 94 insertions(+), 5 deletions(-) create mode 100644 Chess/NodeCounter.h diff --git a/Chess/NodeCounter.h b/Chess/NodeCounter.h new file mode 100644 index 0000000..4625441 --- /dev/null +++ b/Chess/NodeCounter.h @@ -0,0 +1,25 @@ +#pragma once + +#include +#include + +namespace SimpleChessEngine { +class NodeCounter { + public: + [[gnu::always_inline]] void Add(const std::uint64_t nodes) noexcept { + count_.store(count_.load(std::memory_order_relaxed) + nodes, + std::memory_order_relaxed); + } + + [[gnu::always_inline]] void Reset() noexcept { + count_.store(0, std::memory_order_relaxed); + } + + [[gnu::always_inline]] [[nodiscard]] std::uint64_t Get() const noexcept { + return count_.load(std::memory_order_relaxed); + } + + private: + std::atomic count_{0}; +}; +} // namespace SimpleChessEngine diff --git a/Chess/SearchImplementation.h b/Chess/SearchImplementation.h index 553637b..e7c6a43 100644 --- a/Chess/SearchImplementation.h +++ b/Chess/SearchImplementation.h @@ -217,6 +217,7 @@ SearchResult SearchNode::operator()() { } searcher_.debug_info_.searched_nodes++; + searcher_.nodes_.Add(1); if constexpr (CountsNodes) exit_condition_.CountNode(); if (ProbeTranspositionTable()) { @@ -340,8 +341,9 @@ SearchResult SearchNode::QuiescenceSearch() { .template Search( current_position, state_.alpha, state_.beta, state_.ply); - searcher_.debug_info_.quiescence_nodes += - quiescence_searcher.GetSearchedNodes(); + const auto quiescence_nodes = quiescence_searcher.GetSearchedNodes(); + searcher_.debug_info_.quiescence_nodes += quiescence_nodes; + searcher_.nodes_.Add(quiescence_nodes); return eval; } diff --git a/Chess/Searcher.h b/Chess/Searcher.h index b8abeef..946b68e 100644 --- a/Chess/Searcher.h +++ b/Chess/Searcher.h @@ -15,6 +15,7 @@ #include "KillerTable.h" #include "Move.h" #include "MoveGenerator.h" +#include "NodeCounter.h" #include "NodeType.h" #include "PositionFactory.h" #include "SyzygyTablebase.h" @@ -114,6 +115,7 @@ class Searcher { return singular_info_; } [[nodiscard]] const DebugInfo &GetInfo() const { return debug_info_; } + [[nodiscard]] std::uint64_t GetNodes() const { return nodes_.Get(); } [[nodiscard]] const auto &GetKillers() const { return killers_; } [[nodiscard]] const auto &GetHistory() const { return history_; } [[nodiscard]] std::int64_t GetQuietScore(const Move &move, Depth ply) const; @@ -160,6 +162,7 @@ class Searcher { std::vector> root_effort_; DebugInfo debug_info_; + NodeCounter nodes_; SingularInfo singular_info_; }; @@ -272,6 +275,7 @@ inline void Searcher::AddRootEffort(const Move &move, std::uint64_t nodes) { } inline void Searcher::InitStartOfSearch() { + nodes_.Reset(); killers_.Clear(); root_effort_.clear(); std::ranges::fill(continuation_history_, 0); diff --git a/Chess/SimpleChessEngine.h b/Chess/SimpleChessEngine.h index f3fe92e..b60c5c2 100644 --- a/Chess/SimpleChessEngine.h +++ b/Chess/SimpleChessEngine.h @@ -4,9 +4,14 @@ #include #include #include +#include +#include #include +#include #include #include +#include +#include #include #include "Evaluation.h" @@ -102,6 +107,20 @@ class ChessEngine { } void GoPonder(Pondering& conditions) { ComputeBestMove(conditions); } + + void SetHelperEngines( + std::span> helper_engines) { + helper_engines_ = helper_engines; + } + + [[nodiscard]] std::uint64_t GetNodes() const { return searcher_.GetNodes(); } + + [[nodiscard]] std::uint64_t GetNodesOfAllThreads() const { + return std::ranges::fold_left( + helper_engines_ | std::views::transform(&ChessEngine::GetNodes), + GetNodes(), std::plus{}); + } + void NewGame() { previous_score_.reset(); previous_time_reduction_ = kInitialTimeReduction; @@ -159,9 +178,11 @@ class ChessEngine { PrincipalVariationInfo pv{current_depth, searcher_.GetPrincipalVariation( current_depth, position_)}; PrintInfo(pv); - PrintInfo(NodesInfo{info.searched_nodes + info.quiescence_nodes}); - PrintInfo(NodePerSecondInfo{static_cast( - (info.searched_nodes + info.quiescence_nodes) / search_time.count())}); + const auto nodes = GetNodesOfAllThreads(); + PrintInfo(NodesInfo{nodes}); + PrintInfo(NodePerSecondInfo{ + static_cast(static_cast(nodes) / + search_time.count())}); for (const auto& [name, counter] : kDebugCounters) { if (counter != &DebugInfo::searched_nodes && counter != &DebugInfo::quiescence_nodes) { @@ -240,6 +261,7 @@ class ChessEngine { Depth initial_depth_; std::optional previous_score_; double previous_time_reduction_ = kInitialTimeReduction; + std::span> helper_engines_; }; } // namespace SimpleChessEngine diff --git a/Chess/UciCommunicator.h b/Chess/UciCommunicator.h index 22aefe8..778af42 100644 --- a/Chess/UciCommunicator.h +++ b/Chess/UciCommunicator.h @@ -77,6 +77,7 @@ class SearchThread { ponder_start_ = std::chrono::steady_clock::now(); pondering_.emplace(stop_signal_); StartPonderHelpers(info); + engine_.SetHelperEngines(helper_engines_); thread_ = std::thread([this, stop_signal = stop_signal_] { engine_.GoPonder(*pondering_); stop_signal->store(true, std::memory_order_relaxed); @@ -1161,6 +1162,7 @@ inline void SearchThread::Start(const Info& info) { condition); }); } + engine_.SetHelperEngines(helper_engines_); thread_ = std::thread([this, stop_signal = stop_signal_, condition = GetCondition(info)]() mutable { @@ -1205,6 +1207,7 @@ inline void SearchThread::StopThread() { if (helper_thread.joinable()) helper_thread.join(); } helper_threads_.clear(); + engine_.SetHelperEngines({}); helper_engines_.clear(); helper_pondering_.clear(); pondering_ = std::nullopt; diff --git a/Tests/ChessEngineTests.cpp b/Tests/ChessEngineTests.cpp index c9f6a5f..916168e 100644 --- a/Tests/ChessEngineTests.cpp +++ b/Tests/ChessEngineTests.cpp @@ -1,7 +1,9 @@ #include #include +#include #include +#include #include #include #include @@ -141,6 +143,37 @@ TEST(BestMove, PrintsNullMoveWithoutLegalMoves) { EXPECT_NE(ss.str().find("bestmove 0000"), std::string::npos); } +TEST(NodeCounting, ReportsNodesOfAllThreads) { + const auto position = PositionFactory{}(); + const auto transposition_table = + std::make_shared(); + + std::stringstream helper_output; + std::vector> helpers; + helpers.push_back(std::make_unique( + position, helper_output, transposition_table, false)); + DepthCondition helper_condition{6}; + helpers.front()->ComputeBestMove(helper_condition); + + std::stringstream output; + ChessEngine engine(position, output, transposition_table); + engine.SetHelperEngines(helpers); + DepthCondition condition{6}; + engine.ComputeBestMove(condition); + + std::uint64_t reported_nodes = 0; + for (std::string line; std::getline(output, line);) { + std::istringstream words(line); + std::string prefix; + std::string field; + if (words >> prefix >> field && prefix == "info" && field == "nodes") { + words >> reported_nodes; + } + } + EXPECT_GT(helpers.front()->GetNodes(), 0U); + EXPECT_EQ(reported_nodes, engine.GetNodes() + helpers.front()->GetNodes()); +} + TEST_P(BestMoveTest, FindBestMove) { auto position = GetPosition();