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 <core/core.hpp>
18
19#include <chrono>
20#include <utility>
21
22namespace iganet {
23
25template <std::size_t id = 0> class MemoryDebugger {
26private:
28 struct MemoryObject {
29 std::string name_;
30 int64_t bytes_;
31
32 MemoryObject(std::string name, int64_t bytes)
33 : name_(std::move(name)), bytes_(bytes) {}
34 };
35
37 std::map<std::chrono::high_resolution_clock::time_point, MemoryObject>
39
41 int64_t counter_;
42
44 int64_t bytes_;
45
47 std::chrono::high_resolution_clock::time_point init_;
48
50 [[nodiscard]] std::string convert_bytes(int64_t bytes) const {
51 if (bytes < 1024ull)
52 return std::to_string(bytes) + "b";
53 else if (bytes < 1024ull * 1024ull)
54 return std::to_string(bytes / static_cast<double>(1024)) + "kb";
55 else if (bytes < 1024ull * 1024ull * 1024ull)
56 return std::to_string(bytes / static_cast<double>(1024 * 1024)) + "mb";
57 else if (bytes < 1024ull * 1024ull * 1024ull * 1024ull)
58 return std::to_string(bytes / static_cast<double>(1024 * 1024 * 1024)) +
59 "gb";
60 else
61 return std::to_string(
62 bytes / static_cast<double>(1024) / static_cast<double>(1024) /
63 static_cast<double>(1024) / static_cast<double>(1024)) +
64 "tb";
65 }
66
67public:
70 : counter_(0), bytes_(0),
71 init_(std::chrono::high_resolution_clock::now()) {}
72
74 void clear() {
75 counter_ = 0;
76 bytes_ = 0;
77 objects_.clear();
78 }
79
81 inline void pretty_print(std::ostream &os = Log(log::info)) const {
82 using namespace std::literals;
83
84 os << "Memory debugger (ID=" << std::to_string(id) << ")\n";
85 for (const auto &obj : objects_)
86 os << "[" << std::right << std::setw(10) << (obj.first - init_) / 1ns
87 << "ns] " << std::right << std::setw(10) << obj.second.name_ << " "
88 << std::right << std::setw(10) << convert_bytes(obj.second.bytes_)
89 << "\n";
90 os << "[ Total ] " << std::right << std::setw(10) << counter_ << " "
91 << std::right << std::setw(10) << convert_bytes(bytes_) << "\n";
92 }
93
95 template <typename T>
96 void add(const std::string &name, [[maybe_unused]] const T &obj) {
97 counter_++;
98 bytes_ += sizeof(obj);
99 objects_.insert(
100 std::pair<std::chrono::high_resolution_clock::time_point, MemoryObject>(
101 std::chrono::high_resolution_clock::now(),
102 MemoryObject(name, sizeof(obj))));
103 }
104
106 void add(const std::string &name, const torch::Tensor &tensor) {
107 counter_++;
108 bytes_ += tensor.element_size() * tensor.numel();
109 objects_.insert(
110 std::pair<std::chrono::high_resolution_clock::time_point, MemoryObject>(
111 std::chrono::high_resolution_clock::now(),
112 MemoryObject(name, tensor.element_size() * tensor.numel())));
113 }
114
116 template <typename T, std::size_t N>
117 void add(const std::string &name, const std::array<T, N> &array) {
118 for (std::size_t i = 0; i < N; ++i)
119 add(name + std::to_string(i), array[i]);
120 }
121};
122
124template <std::size_t id>
125inline std::ostream &operator<<(std::ostream &os,
126 const MemoryDebugger<id> &obj) {
127 obj.pretty_print(os);
128 return os;
129}
130
132static MemoryDebugger<std::numeric_limits<std::size_t>::max()>
134
135#define register_memory(obj) ::iganet::global_memory_debugger.add(#obj, obj)
136
137} // namespace iganet
Memory debugger.
Definition memory.hpp:25
void add(const std::string &name, const std::array< T, N > &array)
Registers std::array to memory debugger.
Definition memory.hpp:117
void add(const std::string &name, const T &obj)
Registers generic type to memory debugger.
Definition memory.hpp:96
void clear()
Clears memory debugger.
Definition memory.hpp:74
MemoryDebugger()
Default constructor.
Definition memory.hpp:69
std::chrono::high_resolution_clock::time_point init_
Reference time point.
Definition memory.hpp:47
int64_t counter_
Counter holding the number of registered objects.
Definition memory.hpp:41
std::string convert_bytes(int64_t bytes) const
Converts bytes into best human-readable unit.
Definition memory.hpp:50
int64_t bytes_
Counter holding the memory of registered objects in bytes.
Definition memory.hpp:44
std::map< std::chrono::high_resolution_clock::time_point, MemoryObject > objects_
Map holding the list of registered objects.
Definition memory.hpp:38
void pretty_print(std::ostream &os=Log(log::info)) const
Returns a string representation of the memory debugger.
Definition memory.hpp:81
void add(const std::string &name, const torch::Tensor &tensor)
Registers torch::Tensor to memory debugger.
Definition memory.hpp:106
Core components.
Definition core.hpp:72
static MemoryDebugger< std::numeric_limits< std::size_t >::max()> global_memory_debugger
System-wide memory debugger.
Definition memory.hpp:133
std::ostream & operator<<(std::ostream &os, const MemoryDebugger< id > &obj)
Print (as string) a memory debugger object.
Definition memory.hpp:125
struct iganet::@0 Log
Logger.
STL namespace.
Memory object.
Definition memory.hpp:28
int64_t bytes_
Definition memory.hpp:30
std::string name_
Definition memory.hpp:29
MemoryObject(std::string name, int64_t bytes)
Definition memory.hpp:32