Skip to content

Commit

Permalink
Add debug tools
Browse files Browse the repository at this point in the history
No functional change
  • Loading branch information
ruicoelhopedro committed Feb 11, 2023
1 parent f7172fb commit 527fe63
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 4 deletions.
2 changes: 0 additions & 2 deletions src/MoveOrder.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,6 @@ class Histories
};


constexpr int asd = sizeof(MoveStage);

class MoveOrder
{
MoveList m_moves;
Expand Down
9 changes: 8 additions & 1 deletion src/Thread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -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();
}
}

6 changes: 6 additions & 0 deletions src/Types.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "Types.hpp"
#include <array>
#include <sstream>
#include <string>
#include <vector>
Expand All @@ -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<Entry, NUM_DEBUG_SLOTS> debug_slots;
}
87 changes: 86 additions & 1 deletion src/Types.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
#include <string>
#include <vector>
#include <algorithm>
#include <array>
#include <iostream>
#include <atomic>
#include <cmath>


using Square = int8_t;
Expand Down Expand Up @@ -297,4 +301,85 @@ class PseudoRandom
z = (z ^ (z >> 27)) * 0x94d049bb133111eb;
return z ^ (z >> 31);
}
};
};


// Debug tools
namespace Debug
{
enum Slots
{
NUM_DEBUG_SLOTS
};


constexpr std::array<const char*, NUM_DEBUG_SLOTS> 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<Entry, NUM_DEBUG_SLOTS> 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<Slots Slot>
void debug_hit(int64_t value = 0)
{
static_assert(Slot < NUM_DEBUG_SLOTS && Slot >= 0, "Invalid debug slot");
debug_slots[Slot].hit(value);
}
}

0 comments on commit 527fe63

Please sign in to comment.