IgANet
IGAnets - Isogeometric Analysis Networks
Loading...
Searching...
No Matches
memory.hpp
Go to the documentation of this file.
1
15#pragma once
16
17#include <iganet/core/core.hpp>
18
19#include <chrono>
20#include <utility>
21
22namespace iganet {
23
26template <std::size_t id = 0> class MemoryDebugger {
27private:
29 struct MemoryObject {
30 std::string name_;
31 int64_t bytes_;
32
36 MemoryObject(std::string name, int64_t bytes)
37 : name_(std::move(name)), bytes_(bytes) {}
38 };
39
41 std::map<std::chrono::high_resolution_clock::time_point, MemoryObject>
43
45 int64_t counter_;
46
48 int64_t bytes_;
49
51 std::chrono::high_resolution_clock::time_point init_;
52
56 [[nodiscard]] std::string convert_bytes(int64_t bytes) const {
57 if (bytes < 1024ull)
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)) +
65 "gb";
66 else
67 return std::to_string(
68 bytes / static_cast<double>(1024) / static_cast<double>(1024) /
69 static_cast<double>(1024) / static_cast<double>(1024)) +
70 "tb";
71 }
72
73public:
76 : counter_(0), bytes_(0),
77 init_(std::chrono::high_resolution_clock::now()) {}
78
80 void clear() {
81 counter_ = 0;
82 bytes_ = 0;
83 objects_.clear();
84 }
85
88 inline void pretty_print(std::ostream &os = Log(log::info)) const {
89 using namespace std::literals;
90
91 os << "Memory debugger (ID=" << std::to_string(id) << ")\n";
92 for (const auto &obj : objects_)
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_)
96 << "\n";
97 os << "[ Total ] " << std::right << std::setw(10) << counter_ << " "
98 << std::right << std::setw(10) << convert_bytes(bytes_) << "\n";
99 }
100
105 template <typename T>
106 void add(const std::string &name, [[maybe_unused]] const T &obj) {
107 counter_++;
108 bytes_ += sizeof(obj);
109 objects_.insert(
110 std::pair<std::chrono::high_resolution_clock::time_point, MemoryObject>(
111 std::chrono::high_resolution_clock::now(),
112 MemoryObject(name, sizeof(obj))));
113 }
114
118 void add(const std::string &name, const torch::Tensor &tensor) {
119 counter_++;
120 bytes_ += tensor.element_size() * tensor.numel();
121 objects_.insert(
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())));
125 }
126
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]);
136 }
137};
138
144template <std::size_t id>
145inline std::ostream &operator<<(std::ostream &os,
146 const MemoryDebugger<id> &obj) {
147 obj.pretty_print(os);
148 return os;
149}
150
152static MemoryDebugger<std::numeric_limits<std::size_t>::max()>
154
157#define register_memory(obj) ::iganet::global_memory_debugger.add(#obj, obj)
158
159} // namespace iganet
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
Core components.
Definition core.hpp:73
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.
STL namespace.
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