41 std::map<std::chrono::high_resolution_clock::time_point, MemoryObject>
51 std::chrono::high_resolution_clock::time_point
init_;
58 return std::to_string(bytes) +
"b";
59 else if (bytes < 1024ull * 1024ull)
60 return std::to_string(bytes /
static_cast<double>(1024)) +
"kb";
61 else if (bytes < 1024ull * 1024ull * 1024ull)
62 return std::to_string(bytes /
static_cast<double>(1024 * 1024)) +
"mb";
63 else if (bytes < 1024ull * 1024ull * 1024ull * 1024ull)
64 return std::to_string(bytes /
static_cast<double>(1024 * 1024 * 1024)) +
67 return std::to_string(
68 bytes /
static_cast<double>(1024) /
static_cast<double>(1024) /
69 static_cast<double>(1024) /
static_cast<double>(1024)) +
77 init_(
std::chrono::high_resolution_clock::now()) {}
89 using namespace std::literals;
91 os <<
"Memory debugger (ID=" << std::to_string(
id) <<
")\n";
93 os <<
"[" << std::right << std::setw(10) << (obj.first -
init_) / 1ns
94 <<
"ns] " << std::right << std::setw(10) << obj.second.name_ <<
" "
95 << std::right << std::setw(10) <<
convert_bytes(obj.second.bytes_)
97 os <<
"[ Total ] " << std::right << std::setw(10) <<
counter_ <<
" "
105 template <
typename T>
106 void add(
const std::string &name, [[maybe_unused]]
const T &obj) {
110 std::pair<std::chrono::high_resolution_clock::time_point, MemoryObject>(
111 std::chrono::high_resolution_clock::now(),
118 void add(
const std::string &name,
const torch::Tensor &tensor) {
120 bytes_ += tensor.element_size() * tensor.numel();
122 std::pair<std::chrono::high_resolution_clock::time_point, MemoryObject>(
123 std::chrono::high_resolution_clock::now(),
124 MemoryObject(name, tensor.element_size() * tensor.numel())));
132 template <
typename T, std::
size_t N>
133 void add(
const std::string &name,
const std::array<T, N> &array) {
134 for (std::size_t i = 0; i < N; ++i)
135 add(name + std::to_string(i), array[i]);
144template <std::
size_t id>
152static MemoryDebugger<std::numeric_limits<std::size_t>::max()>
157#define register_memory(obj) ::iganet::global_memory_debugger.add(#obj, obj)
Memory debugger.
Definition memory.hpp:26
void add(const std::string &name, const std::array< T, N > &array)
Registers a std::array with the memory debugger.
Definition memory.hpp:133
void add(const std::string &name, const T &obj)
Registers a generic type with the memory debugger.
Definition memory.hpp:106
void clear()
Clears the memory debugger.
Definition memory.hpp:80
MemoryDebugger()
Default constructor.
Definition memory.hpp:75
std::chrono::high_resolution_clock::time_point init_
Reference time point.
Definition memory.hpp:51
int64_t counter_
Counter holding the number of registered objects.
Definition memory.hpp:45
std::string convert_bytes(int64_t bytes) const
Converts bytes into the best human-readable unit.
Definition memory.hpp:56
int64_t bytes_
Counter holding the memory of registered objects in bytes.
Definition memory.hpp:48
std::map< std::chrono::high_resolution_clock::time_point, MemoryObject > objects_
Map holding the list of registered objects.
Definition memory.hpp:42
void pretty_print(std::ostream &os=Log(log::info)) const
Returns a string representation of the memory debugger.
Definition memory.hpp:88
void add(const std::string &name, const torch::Tensor &tensor)
Registers a torch::Tensor with the memory debugger.
Definition memory.hpp:118
static MemoryDebugger< std::numeric_limits< std::size_t >::max()> global_memory_debugger
System-wide memory debugger.
Definition memory.hpp:153
std::ostream & operator<<(std::ostream &os, const MemoryDebugger< id > &obj)
Prints a memory debugger object.
Definition memory.hpp:145
struct iganet::@0 Log
Logger.
Memory object.
Definition memory.hpp:29
int64_t bytes_
Definition memory.hpp:31
std::string name_
Definition memory.hpp:30
MemoryObject(std::string name, int64_t bytes)
Constructs a registered-memory record.
Definition memory.hpp:36