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