diff --git a/CMakeLists.txt b/CMakeLists.txt index ad80f91..a3de88f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -49,6 +49,11 @@ target_include_directories(fathom PUBLIC ${fathom_SOURCE_DIR}/src) target_compile_definitions(fathom PRIVATE TB_NO_HELPER_API) target_link_libraries(fathom PRIVATE Threads::Threads) +set_source_files_properties( + Chess/NodeReportingSearch.cpp + PROPERTIES COMPILE_OPTIONS -fno-lto +) + FILE(GLOB CHESS_SOURCES CONFIGURE_DEPENDS Chess/*.cpp Chess/Nnue/*.cpp) list(APPEND CHESS_SOURCES Chess/Nnue/Stockfish/attacks.cpp diff --git a/Chess/Concepts.h b/Chess/Concepts.h index 6e8ff3c..3c4952c 100644 --- a/Chess/Concepts.h +++ b/Chess/Concepts.h @@ -8,3 +8,6 @@ concept StopSearchCondition = requires(const T& condition) { template concept CountsNodes = requires(const T& condition) { condition.CountNode(); }; + +template +concept ReportsNodes = requires { typename T::NodeReportingTag; }; diff --git a/Chess/ExitCondition.h b/Chess/ExitCondition.h index 0495e89..eaac1d7 100644 --- a/Chess/ExitCondition.h +++ b/Chess/ExitCondition.h @@ -221,4 +221,33 @@ struct Pondering { } }; static_assert(SearchCondition); + +template +class NodeReportingCondition { + public: + using NodeReportingTag = void; + + explicit NodeReportingCondition(Condition& condition) : condition_(condition) {} + bool IsTimeToExit() const { return condition_.IsTimeToExit(); } + bool ShouldContinueIteration() const { + return condition_.ShouldContinueIteration(); + } + void Update(const IterationInfo& info) { condition_.Update(info); } + void CountNode() const requires CountsNodes { + condition_.CountNode(); + } + void WaitForPonderEnd() const + requires requires(const Condition& condition) { condition.WaitForPonderEnd(); } + { + condition_.WaitForPonderEnd(); + } + + private: + Condition& condition_; +}; +static_assert(SearchCondition>); +static_assert(ReportsNodes>); +static_assert(!ReportsNodes); +static_assert(!CountsNodes>); +static_assert(CountsNodes>); } // namespace SimpleChessEngine diff --git a/Chess/NodeCounter.h b/Chess/NodeCounter.h new file mode 100644 index 0000000..17a99d8 --- /dev/null +++ b/Chess/NodeCounter.h @@ -0,0 +1,27 @@ +#pragma once + +#include +#include + +namespace SimpleChessEngine { +static_assert(std::atomic::is_always_lock_free); + +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/NodeReportingSearch.cpp b/Chess/NodeReportingSearch.cpp new file mode 100644 index 0000000..89c721b --- /dev/null +++ b/Chess/NodeReportingSearch.cpp @@ -0,0 +1,23 @@ +#include "SimpleChessEngine.h" + +namespace SimpleChessEngine { +void ChessEngine::ComputeBestMoveImpl( + NodeReportingCondition& condition) { + ComputeBestMoveImpl>(condition); +} + +void ChessEngine::ComputeBestMoveImpl( + NodeReportingCondition& condition) { + ComputeBestMoveImpl>(condition); +} + +void ChessEngine::ComputeBestMoveImpl( + NodeReportingCondition& condition) { + ComputeBestMoveImpl>(condition); +} + +void ChessEngine::ComputeBestMoveImpl( + NodeReportingCondition& condition) { + ComputeBestMoveImpl>(condition); +} +} diff --git a/Chess/Quiescence.cpp b/Chess/Quiescence.cpp index b6bb85b..b8904c2 100644 --- a/Chess/Quiescence.cpp +++ b/Chess/Quiescence.cpp @@ -233,4 +233,33 @@ template SearchResult Quiescence::Search( template SearchResult Quiescence::Search( Position& current_position, Eval alpha, const Eval beta, const Depth current_depth, bool pv_node); + +template class Quiescence>; +template SearchResult Quiescence>::Search( + Position& current_position, Eval alpha, const Eval beta, + const Depth current_depth, bool pv_node); +template SearchResult Quiescence>::Search( + Position& current_position, Eval alpha, const Eval beta, + const Depth current_depth, bool pv_node); +template class Quiescence>; +template SearchResult Quiescence>::Search( + Position& current_position, Eval alpha, const Eval beta, + const Depth current_depth, bool pv_node); +template SearchResult Quiescence>::Search( + Position& current_position, Eval alpha, const Eval beta, + const Depth current_depth, bool pv_node); +template class Quiescence>; +template SearchResult Quiescence>::Search( + Position& current_position, Eval alpha, const Eval beta, + const Depth current_depth, bool pv_node); +template SearchResult Quiescence>::Search( + Position& current_position, Eval alpha, const Eval beta, + const Depth current_depth, bool pv_node); +template class Quiescence>; +template SearchResult Quiescence>::Search( + Position& current_position, Eval alpha, const Eval beta, + const Depth current_depth, bool pv_node); +template SearchResult Quiescence>::Search( + Position& current_position, Eval alpha, const Eval beta, + const Depth current_depth, bool pv_node); } // namespace SimpleChessEngine diff --git a/Chess/SearchImplementation.h b/Chess/SearchImplementation.h index 7095f48..5985582 100644 --- a/Chess/SearchImplementation.h +++ b/Chess/SearchImplementation.h @@ -223,6 +223,7 @@ SearchResult SearchNode::operator()() { } searcher_.debug_info_.searched_nodes++; + if constexpr (ReportsNodes) searcher_.nodes_.Add(1); if constexpr (CountsNodes) exit_condition_.CountNode(); if (ProbeTranspositionTable()) { @@ -365,7 +366,7 @@ SearchResult SearchNode::SearchProbCutMove( const auto qvalue = quiescence.template Search( position, -probcut_beta, -probcut_beta + 1, static_cast(state_.ply + 1), false); - searcher_.debug_info_.quiescence_nodes += quiescence.GetSearchedNodes(); + searcher_.template CountQuiescenceNodes(quiescence.GetSearchedNodes()); searcher_.debug_info_.probcut_qsearch_nodes += quiescence.GetSearchedNodes(); if (!qvalue || -*qvalue < probcut_beta) return qvalue; @@ -392,8 +393,7 @@ SearchResult SearchNode::QuiescenceSearch() { current_position, state_.alpha, state_.beta, state_.ply, kIsPrincipalVariation); - searcher_.debug_info_.quiescence_nodes += - quiescence_searcher.GetSearchedNodes(); + searcher_.template CountQuiescenceNodes(quiescence_searcher.GetSearchedNodes()); return eval; } diff --git a/Chess/Searcher.h b/Chess/Searcher.h index f97265e..506d70a 100644 --- a/Chess/Searcher.h +++ b/Chess/Searcher.h @@ -18,6 +18,7 @@ #include "KillerTable.h" #include "Move.h" #include "MoveGenerator.h" +#include "NodeCounter.h" #include "NodeType.h" #include "PositionFactory.h" #include "Settings.h" @@ -129,6 +130,8 @@ class Searcher { return null_move_info_; } [[nodiscard]] const DebugInfo &GetInfo() const { return debug_info_; } + [[nodiscard]] std::uint64_t GetNodes() const { return nodes_.Get(); } + void ResetNodes() noexcept { nodes_.Reset(); } [[nodiscard]] const auto &GetKillers() const { return killers_; } [[nodiscard]] const auto &GetHistory() const { return history_; } [[nodiscard]] const auto &GetContinuationHistory() const { @@ -186,6 +189,12 @@ class Searcher { void AddRootEffort(const Move &move, std::uint64_t nodes, std::optional score); + template + void CountQuiescenceNodes(std::size_t nodes) noexcept { + debug_info_.quiescence_nodes += nodes; + if constexpr (ReportsNodes) nodes_.Add(nodes); + } + template using PieceToHistory = ByPiece>; using ContinuationHistory = PieceToHistory>; @@ -202,6 +211,7 @@ class Searcher { std::vector root_effort_; DebugInfo debug_info_; + NodeCounter nodes_; SingularInfo singular_info_; NullMoveInfo null_move_info_; Depth nmp_min_ply_ = 0; @@ -336,6 +346,7 @@ inline void Searcher::AddRootEffort(const Move &move, std::uint64_t nodes, } inline void Searcher::InitStartOfSearch() { + nodes_.Reset(); nmp_min_ply_ = 0; killers_.Clear(); root_effort_.clear(); @@ -367,9 +378,11 @@ SearchResult SimpleChessEngine::Searcher::Search( null_move_info_ = {}; ++age_; - return SearchNode{ + const auto result = SearchNode{ *this, {max_depth, remaining_depth, alpha, beta}, stop_search_condition}(); + if constexpr (!ReportsNodes) nodes_.Add(debug_info_.TotalNodes()); + return result; } } // namespace SimpleChessEngine diff --git a/Chess/SimpleChessEngine.h b/Chess/SimpleChessEngine.h index f29b846..80e72ae 100644 --- a/Chess/SimpleChessEngine.h +++ b/Chess/SimpleChessEngine.h @@ -4,9 +4,13 @@ #include #include #include +#include +#include #include +#include #include #include +#include #include #include @@ -162,6 +166,23 @@ class ChessEngine { } void GoPonder(Pondering& conditions) { ComputeBestMove(conditions); } + + void SetHelperEngines( + std::span> helper_engines) { + helper_engines_ = helper_engines; + publishes_nodes_ = !helper_engines.empty(); + } + + [[nodiscard]] std::uint64_t GetNodes() const { return searcher_.GetNodes(); } + void ResetNodes() noexcept { searcher_.ResetNodes(); } + void SetNodePublication(bool enabled) noexcept { publishes_nodes_ = enabled; } + + [[nodiscard]] std::uint64_t GetNodesOfAllThreads() const { + return std::ranges::fold_left( + helper_engines_ | std::views::transform(&ChessEngine::GetNodes), + GetNodes(), std::plus{}); + } + void NewGame() { searcher_.ClearHistories(); previous_score_.reset(); @@ -183,6 +204,12 @@ class ChessEngine { } private: + void ComputeBestMoveImpl(NodeReportingCondition& conditions); + void ComputeBestMoveImpl(NodeReportingCondition& conditions); + void ComputeBestMoveImpl(NodeReportingCondition& conditions); + void ComputeBestMoveImpl(NodeReportingCondition& conditions); + void ComputeBestMoveImpl(SearchCondition auto& conditions); + class EBFsInfo { public: void Update(std::size_t searched_nodes) { @@ -221,9 +248,11 @@ class ChessEngine { PrintInfo(ScoreInfo{eval}); PrincipalVariationInfo pv{current_depth, principal_variation}; PrintInfo(pv); - PrintInfo(NodesInfo{info.TotalNodes()}); + const auto nodes = GetNodesOfAllThreads(); + PrintInfo(NodesInfo{nodes}); PrintInfo(NodePerSecondInfo{ - static_cast(info.TotalNodes() / search_time.count())}); + static_cast(static_cast(nodes) / + search_time.count())}); for (const auto& [name, counter] : kDebugCounters) { if (counter != &DebugInfo::searched_nodes && counter != &DebugInfo::quiescence_nodes) { @@ -250,17 +279,27 @@ class ChessEngine { Move best_move_{Move::None()}; std::optional ponder_move_; bool reporting_; + bool publishes_nodes_ = false; Depth initial_depth_; std::size_t thread_index_; std::optional previous_score_; double previous_time_reduction_ = TimeManagementParameters::kInitialTimeReduction; + std::span> helper_engines_; }; } // namespace SimpleChessEngine namespace SimpleChessEngine { -inline void SimpleChessEngine::ChessEngine::ComputeBestMove( - SearchCondition auto& condition) { +inline void ChessEngine::ComputeBestMove(SearchCondition auto& condition) { + if (publishes_nodes_) { + NodeReportingCondition reporting_condition{condition}; + ComputeBestMoveImpl(reporting_condition); + } else { + ComputeBestMoveImpl(condition); + } +} + +inline void ChessEngine::ComputeBestMoveImpl(SearchCondition auto& condition) { const TimePoint start_time = std::chrono::steady_clock::now(); searcher_.InitStartOfSearch(); diff --git a/Chess/UciCommunicator.h b/Chess/UciCommunicator.h index 4e0d073..160db80 100644 --- a/Chess/UciCommunicator.h +++ b/Chess/UciCommunicator.h @@ -78,6 +78,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); @@ -1163,6 +1164,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 { @@ -1184,7 +1186,10 @@ inline ChessEngine& SearchThread::AddHelperEngine(const Info& info, } else { helper_engines_[worker - 1]->SetPosition(info.position); } - return *helper_engines_[worker - 1]; + auto& helper = *helper_engines_[worker - 1]; + helper.ResetNodes(); + helper.SetNodePublication(true); + return helper; } inline void SearchThread::StartPonderHelpers(const Info& info) { @@ -1211,6 +1216,7 @@ inline void SearchThread::StopThread() { if (helper_thread.joinable()) helper_thread.join(); } helper_threads_.clear(); + engine_.SetHelperEngines({}); helper_pondering_.clear(); pondering_ = std::nullopt; } diff --git a/Tests/ChessEngineTests.cpp b/Tests/ChessEngineTests.cpp index 9ac7908..dc400a0 100644 --- a/Tests/ChessEngineTests.cpp +++ b/Tests/ChessEngineTests.cpp @@ -1,7 +1,9 @@ #include #include +#include #include +#include #include #include #include @@ -142,6 +144,64 @@ 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)); + helpers.front()->SetNodePublication(true); + 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()); + + const auto history = helpers.front()->GetSearcher().GetHistory(); + helpers.front()->ResetNodes(); + EXPECT_EQ(helpers.front()->GetNodes(), 0u); + EXPECT_EQ(helpers.front()->GetSearcher().GetHistory(), history); + engine.SetHelperEngines({}); + EXPECT_EQ(engine.GetNodesOfAllThreads(), engine.GetNodes()); +} + +TEST(NodeCounting, AccumulatesAcrossSearchAttemptsUntilTheNextGo) { + Searcher searcher(PositionFactory{}()); + searcher.InitStartOfSearch(); + DepthCondition condition{3}; + std::uint64_t nodes = 0; + for (const auto& [alpha, beta] : + {std::pair{-10001, -10000}, + std::pair{-Score::kMate, Score::kMate}, + std::pair{10000, 10001}}) { + ASSERT_TRUE(searcher.Search(condition, 3, 3, alpha, beta)); + const auto& info = searcher.GetInfo(); + nodes += info.searched_nodes + info.quiescence_nodes; + EXPECT_EQ(searcher.GetNodes(), nodes); + } + EXPECT_GT(nodes, 0u); + searcher.InitStartOfSearch(); + EXPECT_EQ(searcher.GetNodes(), 0u); +} + TEST_P(BestMoveTest, FindBestMove) { auto position = GetPosition(); diff --git a/Tests/NullMovePruningTests.cpp b/Tests/NullMovePruningTests.cpp index d6b90ef..57b4872 100644 --- a/Tests/NullMovePruningTests.cpp +++ b/Tests/NullMovePruningTests.cpp @@ -266,8 +266,9 @@ TEST_F(NullMovePruning, UncappedReductionCanVerifyInQuiescence) { const auto beta = position.Evaluate() - 707; ASSERT_GE(beta, -788); auto budget = std::make_shared(4); - NodeCondition stop{16, {}, budget}; - const auto result = SearchNode{ + NodeCondition underlying_condition{16, {}, budget}; + NodeReportingCondition stop{underlying_condition}; + const auto result = SearchNode{ searcher, {16, 16, beta - 1, beta, 1}, stop}(); ASSERT_TRUE(result); EXPECT_EQ(*result, beta); @@ -276,6 +277,8 @@ TEST_F(NullMovePruning, UncappedReductionCanVerifyInQuiescence) { EXPECT_EQ(searcher.GetInfo().nmp_tries, 1u); EXPECT_EQ(searcher.GetInfo().nmp_cuts, 1u); EXPECT_EQ(searcher.GetNullMoveInfo().verifications, 1u); + EXPECT_EQ(searcher.GetNodes(), searcher.GetInfo().searched_nodes + + searcher.GetInfo().quiescence_nodes); EXPECT_EQ(searcher.GetPosition().GetHash(), position.GetHash()); EXPECT_FALSE(budget->IsSpent()); budget->Spend(); diff --git a/Tests/QuiescenceTTTests.cpp b/Tests/QuiescenceTTTests.cpp index a8f696d..779a824 100644 --- a/Tests/QuiescenceTTTests.cpp +++ b/Tests/QuiescenceTTTests.cpp @@ -62,6 +62,7 @@ TEST(QuiescenceTT, MainSearchPassesPvFlagAndCountsQuiescenceCutoffs) { EXPECT_EQ(searcher.Search(condition, 0, 0, -1, 0), Score::MateIn(5)); EXPECT_EQ(searcher.GetInfo().quiescence_nodes, 1u); + EXPECT_EQ(searcher.GetNodes(), 1u); EXPECT_EQ(searcher.GetInfo().searched_nodes, 0u); EXPECT_EQ(searcher.Search(condition, 0, 0, -Score::kMate, Score::kMate), diff --git a/Tests/SearchCorrectnessTests.cpp b/Tests/SearchCorrectnessTests.cpp index 84c4ac0..1f8f758 100644 --- a/Tests/SearchCorrectnessTests.cpp +++ b/Tests/SearchCorrectnessTests.cpp @@ -264,7 +264,8 @@ void ExpectProbCutPositionRestored(const Position& actual, TEST(ProbCut, BothSearchesPassAndReturnOriginalBetaWithoutParentEntry) { const auto position = PositionFactory{}(kProbCutFen); - DepthCondition condition{8}; + DepthCondition underlying_condition{8}; + NodeReportingCondition condition{underlying_condition}; for (const auto node_type : {NodeType::kAll, NodeType::kCut}) { auto table = std::make_shared(); Searcher searcher(position, table); @@ -279,6 +280,7 @@ TEST(ProbCut, BothSearchesPassAndReturnOriginalBetaWithoutParentEntry) { EXPECT_EQ(info.probcut_qsearch_passes, 1u); EXPECT_EQ(info.probcut_cuts, 1u); EXPECT_GT(info.probcut_verification_nodes, 0u); + EXPECT_EQ(searcher.GetNodes(), info.searched_nodes + info.quiescence_nodes); EXPECT_LE(info.probcut_qsearch_nodes, info.quiescence_nodes); EXPECT_FALSE(table->GetNode(position).IsOccupied()); ExpectProbCutPositionRestored(searcher.GetPosition(), position);