Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions Chess/Concepts.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,6 @@ concept StopSearchCondition = requires(const T& condition) {

template <class T>
concept CountsNodes = requires(const T& condition) { condition.CountNode(); };

template <class T>
concept ReportsNodes = requires { typename T::NodeReportingTag; };
29 changes: 29 additions & 0 deletions Chess/ExitCondition.h
Original file line number Diff line number Diff line change
Expand Up @@ -221,4 +221,33 @@ struct Pondering {
}
};
static_assert(SearchCondition<Pondering>);

template <SearchCondition Condition>
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> {
condition_.CountNode();
}
void WaitForPonderEnd() const
requires requires(const Condition& condition) { condition.WaitForPonderEnd(); }
{
condition_.WaitForPonderEnd();
}

private:
Condition& condition_;
};
static_assert(SearchCondition<NodeReportingCondition<DepthCondition>>);
static_assert(ReportsNodes<NodeReportingCondition<DepthCondition>>);
static_assert(!ReportsNodes<DepthCondition>);
static_assert(!CountsNodes<NodeReportingCondition<DepthCondition>>);
static_assert(CountsNodes<NodeReportingCondition<NodeCondition>>);
} // namespace SimpleChessEngine
27 changes: 27 additions & 0 deletions Chess/NodeCounter.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#pragma once

#include <atomic>
#include <cstdint>

namespace SimpleChessEngine {
static_assert(std::atomic<std::uint64_t>::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<std::uint64_t> count_{0};
};
} // namespace SimpleChessEngine
23 changes: 23 additions & 0 deletions Chess/NodeReportingSearch.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#include "SimpleChessEngine.h"

namespace SimpleChessEngine {
void ChessEngine::ComputeBestMoveImpl(
NodeReportingCondition<TimeCondition>& condition) {
ComputeBestMoveImpl<NodeReportingCondition<TimeCondition>>(condition);
}

void ChessEngine::ComputeBestMoveImpl(
NodeReportingCondition<DepthCondition>& condition) {
ComputeBestMoveImpl<NodeReportingCondition<DepthCondition>>(condition);
}

void ChessEngine::ComputeBestMoveImpl(
NodeReportingCondition<NodeCondition>& condition) {
ComputeBestMoveImpl<NodeReportingCondition<NodeCondition>>(condition);
}

void ChessEngine::ComputeBestMoveImpl(
NodeReportingCondition<Pondering>& condition) {
ComputeBestMoveImpl<NodeReportingCondition<Pondering>>(condition);
}
}
29 changes: 29 additions & 0 deletions Chess/Quiescence.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -233,4 +233,33 @@ template SearchResult Quiescence<Pondering>::Search<false>(
template SearchResult Quiescence<Pondering>::Search<true>(
Position& current_position, Eval alpha, const Eval beta,
const Depth current_depth, bool pv_node);

template class Quiescence<NodeReportingCondition<TimeCondition>>;
template SearchResult Quiescence<NodeReportingCondition<TimeCondition>>::Search<false>(
Position& current_position, Eval alpha, const Eval beta,
const Depth current_depth, bool pv_node);
template SearchResult Quiescence<NodeReportingCondition<TimeCondition>>::Search<true>(
Position& current_position, Eval alpha, const Eval beta,
const Depth current_depth, bool pv_node);
template class Quiescence<NodeReportingCondition<DepthCondition>>;
template SearchResult Quiescence<NodeReportingCondition<DepthCondition>>::Search<false>(
Position& current_position, Eval alpha, const Eval beta,
const Depth current_depth, bool pv_node);
template SearchResult Quiescence<NodeReportingCondition<DepthCondition>>::Search<true>(
Position& current_position, Eval alpha, const Eval beta,
const Depth current_depth, bool pv_node);
template class Quiescence<NodeReportingCondition<NodeCondition>>;
template SearchResult Quiescence<NodeReportingCondition<NodeCondition>>::Search<false>(
Position& current_position, Eval alpha, const Eval beta,
const Depth current_depth, bool pv_node);
template SearchResult Quiescence<NodeReportingCondition<NodeCondition>>::Search<true>(
Position& current_position, Eval alpha, const Eval beta,
const Depth current_depth, bool pv_node);
template class Quiescence<NodeReportingCondition<Pondering>>;
template SearchResult Quiescence<NodeReportingCondition<Pondering>>::Search<false>(
Position& current_position, Eval alpha, const Eval beta,
const Depth current_depth, bool pv_node);
template SearchResult Quiescence<NodeReportingCondition<Pondering>>::Search<true>(
Position& current_position, Eval alpha, const Eval beta,
const Depth current_depth, bool pv_node);
} // namespace SimpleChessEngine
6 changes: 3 additions & 3 deletions Chess/SearchImplementation.h
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,7 @@ SearchResult SearchNode<node_type, ExitCondition>::operator()() {
}

searcher_.debug_info_.searched_nodes++;
if constexpr (ReportsNodes<ExitCondition>) searcher_.nodes_.Add(1);
if constexpr (CountsNodes<ExitCondition>) exit_condition_.CountNode();

if (ProbeTranspositionTable()) {
Expand Down Expand Up @@ -365,7 +366,7 @@ SearchResult SearchNode<node_type, ExitCondition>::SearchProbCutMove(
const auto qvalue = quiescence.template Search<true>(
position, -probcut_beta, -probcut_beta + 1,
static_cast<Depth>(state_.ply + 1), false);
searcher_.debug_info_.quiescence_nodes += quiescence.GetSearchedNodes();
searcher_.template CountQuiescenceNodes<ExitCondition>(quiescence.GetSearchedNodes());
searcher_.debug_info_.probcut_qsearch_nodes += quiescence.GetSearchedNodes();
if (!qvalue || -*qvalue < probcut_beta) return qvalue;

Expand All @@ -392,8 +393,7 @@ SearchResult SearchNode<node_type, ExitCondition>::QuiescenceSearch() {
current_position, state_.alpha, state_.beta, state_.ply,
kIsPrincipalVariation);

searcher_.debug_info_.quiescence_nodes +=
quiescence_searcher.GetSearchedNodes();
searcher_.template CountQuiescenceNodes<ExitCondition>(quiescence_searcher.GetSearchedNodes());

return eval;
}
Expand Down
15 changes: 14 additions & 1 deletion Chess/Searcher.h
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -186,6 +189,12 @@ class Searcher {
void AddRootEffort(const Move &move, std::uint64_t nodes,
std::optional<Eval> score);

template <class ExitCondition>
void CountQuiescenceNodes(std::size_t nodes) noexcept {
debug_info_.quiescence_nodes += nodes;
if constexpr (ReportsNodes<ExitCondition>) nodes_.Add(nodes);
}

template <class Value>
using PieceToHistory = ByPiece<std::array<Value, kBoardArea + 1>>;
using ContinuationHistory = PieceToHistory<PieceToHistory<std::int32_t>>;
Expand All @@ -202,6 +211,7 @@ class Searcher {
std::vector<RootMoveInfo> root_effort_;

DebugInfo debug_info_;
NodeCounter nodes_;
SingularInfo singular_info_;
NullMoveInfo null_move_info_;
Depth nmp_min_ply_ = 0;
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -367,9 +378,11 @@ SearchResult SimpleChessEngine::Searcher::Search(
null_move_info_ = {};
++age_;

return SearchNode<node_type, ExitCondition>{
const auto result = SearchNode<node_type, ExitCondition>{
*this,
{max_depth, remaining_depth, alpha, beta},
stop_search_condition}();
if constexpr (!ReportsNodes<ExitCondition>) nodes_.Add(debug_info_.TotalNodes());
return result;
}
} // namespace SimpleChessEngine
47 changes: 43 additions & 4 deletions Chess/SimpleChessEngine.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,13 @@
#include <cassert>
#include <chrono>
#include <cmath>
#include <cstdint>
#include <functional>
#include <limits>
#include <memory>
#include <numeric>
#include <optional>
#include <ranges>
#include <span>
#include <syncstream>

Expand Down Expand Up @@ -162,6 +166,23 @@ class ChessEngine {
}

void GoPonder(Pondering& conditions) { ComputeBestMove(conditions); }

void SetHelperEngines(
std::span<const std::unique_ptr<ChessEngine>> 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();
Expand All @@ -183,6 +204,12 @@ class ChessEngine {
}

private:
void ComputeBestMoveImpl(NodeReportingCondition<TimeCondition>& conditions);
void ComputeBestMoveImpl(NodeReportingCondition<DepthCondition>& conditions);
void ComputeBestMoveImpl(NodeReportingCondition<NodeCondition>& conditions);
void ComputeBestMoveImpl(NodeReportingCondition<Pondering>& conditions);
void ComputeBestMoveImpl(SearchCondition auto& conditions);

class EBFsInfo {
public:
void Update(std::size_t searched_nodes) {
Expand Down Expand Up @@ -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<std::size_t>(info.TotalNodes() / search_time.count())});
static_cast<std::size_t>(static_cast<double>(nodes) /
search_time.count())});
for (const auto& [name, counter] : kDebugCounters) {
if (counter != &DebugInfo::searched_nodes &&
counter != &DebugInfo::quiescence_nodes) {
Expand All @@ -250,17 +279,27 @@ class ChessEngine {
Move best_move_{Move::None()};
std::optional<Move> ponder_move_;
bool reporting_;
bool publishes_nodes_ = false;
Depth initial_depth_;
std::size_t thread_index_;
std::optional<Eval> previous_score_;
double previous_time_reduction_ =
TimeManagementParameters::kInitialTimeReduction;
std::span<const std::unique_ptr<ChessEngine>> 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();

Expand Down
8 changes: 7 additions & 1 deletion Chess/UciCommunicator.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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 {
Expand All @@ -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) {
Expand All @@ -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;
}
Expand Down
Loading
Loading