diff --git a/src/MoveOrder.hpp b/src/MoveOrder.hpp index afbb9a6..6d62ce6 100644 --- a/src/MoveOrder.hpp +++ b/src/MoveOrder.hpp @@ -98,8 +98,6 @@ class Histories }; -constexpr int asd = sizeof(MoveStage); - class MoveOrder { MoveList m_moves; diff --git a/src/Thread.cpp b/src/Thread.cpp index 6d94b11..32f1241 100644 --- a/src/Thread.cpp +++ b/src/Thread.cpp @@ -158,6 +158,10 @@ void ThreadPool::search(const Search::Timer& timer, const Search::Limits& limits // Estimate search time update_time(timer, limits); + // Clear debug data (if any) + if constexpr (Debug::Enabled) + Debug::clear_debug_data(); + // Wake threads send_signal(ThreadStatus::SEARCHING); } @@ -473,6 +477,9 @@ void Thread::search() if (pondermove != MOVE_NULL) std::cout << " ponder " << pondermove; std::cout << std::endl; + + // Debug prints + if constexpr (Debug::Enabled) + Debug::print_debug_data(); } } - diff --git a/src/Types.cpp b/src/Types.cpp index 4c2578e..3ef4d48 100644 --- a/src/Types.cpp +++ b/src/Types.cpp @@ -1,4 +1,5 @@ #include "Types.hpp" +#include #include #include #include @@ -11,3 +12,8 @@ std::string get_square(Square square) char name[2] = { files[file(square)], ranks[rank(square)] }; return std::string(name, 2); } + +namespace Debug +{ + std::array debug_slots; +} diff --git a/src/Types.hpp b/src/Types.hpp index 7c4423e..775d509 100644 --- a/src/Types.hpp +++ b/src/Types.hpp @@ -3,6 +3,10 @@ #include #include #include +#include +#include +#include +#include using Square = int8_t; @@ -297,4 +301,85 @@ class PseudoRandom z = (z ^ (z >> 27)) * 0x94d049bb133111eb; return z ^ (z >> 31); } -}; \ No newline at end of file +}; + + +// Debug tools +namespace Debug +{ + enum Slots + { + NUM_DEBUG_SLOTS + }; + + + constexpr std::array Names + { + }; + + + constexpr bool Enabled = NUM_DEBUG_SLOTS > 0; + + + class Entry + { + std::atomic_int64_t m_hits; + std::atomic_int64_t m_sums; + std::atomic_int64_t m_sums_squared; + + public: + inline Entry() : m_hits(0), m_sums(0), m_sums_squared(0) {} + inline void clear() + { + m_hits.store(0); + m_sums.store(0); + m_sums_squared.store(0); + } + inline void hit(int64_t value = 0) + { + m_hits.fetch_add(1, std::memory_order_relaxed); + m_sums.fetch_add(value, std::memory_order_relaxed); + m_sums_squared.fetch_add(value * value, std::memory_order_relaxed); + } + inline int64_t get_hits() const { return m_hits.load(); } + inline int64_t get_sums() const { return m_sums.load(); } + inline double get_avg() const { return double(m_sums.load()) / m_hits.load(); } + inline double get_std() const + { + double avg = get_avg(); + return sqrt(double(m_sums_squared.load()) / m_hits.load() - avg * avg); + } + }; + + + extern std::array debug_slots; + + + inline void clear_debug_data() + { + for (auto& slot : debug_slots) + slot.clear(); + } + + + inline void print_debug_data() + { + std::cout << "Debug info:" << std::endl; + for (std::size_t i = 0; i < NUM_DEBUG_SLOTS; i++) + std::cout << " Slot: " << i + << " (" << Names[i] << ")" + << " Hits: " << debug_slots[i].get_hits() + << " Sums: " << debug_slots[i].get_sums() + << " Avg: " << debug_slots[i].get_avg() + << " Std: " << debug_slots[i].get_std() + << std::endl; + } + + + template + void debug_hit(int64_t value = 0) + { + static_assert(Slot < NUM_DEBUG_SLOTS && Slot >= 0, "Invalid debug slot"); + debug_slots[Slot].hit(value); + } +}