16#ifndef HYPERDEAL_MEMORY_CONSUMPTION
17#define HYPERDEAL_MEMORY_CONSUMPTION
19#include <hyper.deal/base/config.h>
21#include <deal.II/base/utilities.h>
30 template <
typename StreamType,
long unsigned int N>
34 const MPI_Comm & comm,
35 const std::vector<std::pair<std::string, std::array<double, N>>> &list,
36 const std::vector<std::string> & labels,
37 const unsigned int mm)
39 std::vector<double> temp1(list.size() * N);
41 for (
unsigned int i = 0, k = 0; i < list.size(); i++)
42 for (
unsigned int j = 0; j < N; j++, k++)
43 temp1[k] = list[i].second[j];
46 const auto temp2 = dealii::Utilities::MPI::min_max_avg(temp1, comm);
48 std::vector<std::pair<std::string,
49 std::array<dealii::Utilities::MPI::MinMaxAvg, N>>>
50 list_min_max(list.size());
52 for (
unsigned int i = 0, k = 0; i < list.size(); i++)
54 list_min_max[i].first = list[i].first;
55 for (
unsigned int j = 0; j < N; j++, k++)
56 list_min_max[i].second[j] = temp2[k];
60 const auto max_width =
61 std::max_element(list_min_max.begin(),
63 [](
const auto &a,
const auto &b) {
64 return a.first.size() < b.first.size();
70 const auto print_lines = [&]() {
71 stream <<
"+-" << std::string(max_width + 2,
'-') <<
"+";
73 for (
unsigned int i = 0; i < labels.size(); i++)
75 auto j = list_min_max[mm].second[i];
76 auto label = labels[i];
79 const unsigned int width =
static_cast<unsigned int>(4 + std::log(j.sum) / log(10.0))+
80 static_cast<unsigned int>(8)+
81 static_cast<unsigned int>(4 + std::log(j.min) / log(10.0))+
82 static_cast<unsigned int>(4 + std::log(j.avg) / log(10.0))+
83 static_cast<unsigned int>(4 + std::log(j.max) / log(10.0));
85 stream << std::string(width,
'-');
94 stream <<
"| " << std::left << std::setw(max_width + 2) <<
""
97 for (
unsigned int i = 0; i < labels.size(); i++)
99 auto j = list_min_max[mm].second[i];
100 auto label = labels[i];
103 const unsigned int width =
static_cast<unsigned int>(4 + std::log(j.sum) / log(10.0))+
104 static_cast<unsigned int>(8)+
105 static_cast<unsigned int>(4 + std::log(j.min) / log(10.0))+
106 static_cast<unsigned int>(4 + std::log(j.avg) / log(10.0))+
107 static_cast<unsigned int>(4 + std::log(j.max) / log(10.0));
109 stream << std::setw((width-label.size())/2) <<
"" << label << std::setw(width - label.size() - (width-label.size())/2) <<
"" ;
118 stream <<
"| " << std::left << std::setw(max_width + 2) <<
""
121 for (
auto j : list_min_max[mm].second)
124 stream << std::setw(4 + std::log(j.sum) / log(10.0)) << std::right <<
"total";
125 stream << std::setw(8) <<
"%";
126 stream << std::setw(4 + std::log(j.min) / log(10.0)) << std::right <<
"min";
127 stream << std::setw(4 + std::log(j.avg) / log(10.0)) << std::right <<
"avg";
128 stream << std::setw(4 + std::log(j.max) / log(10.0)) << std::right <<
"max";
137 for (
auto j : list_min_max)
139 stream <<
"| " << std::left << std::setw(max_width + 2) << j.first
142 for (
unsigned int col = 0; col < j.second.size(); col++)
144 auto i = j.second[col];
145 auto m = list_min_max[mm].second[col];
147 stream << std::fixed << std::setprecision(0) << std::setw(4 + std::log(m.sum) / log(10.0)) << std::right << i.sum;
148 stream << std::fixed << std::setprecision(2) << std::setw(8) << std::right << (i.sum * 100 / m.sum);
149 stream << std::fixed << std::setprecision(0) << std::setw(4 + std::log(m.min) / log(10.0)) << std::right << i.min;
150 stream << std::fixed << std::setprecision(0) << std::setw(4 + std::log(m.avg) / log(10.0)) << std::right << i.avg;
151 stream << std::fixed << std::setprecision(0) << std::setw(4 + std::log(m.max) / log(10.0)) << std::right << i.max;
158 stream << std::endl << std::endl;
162 template <
typename StreamType,
long unsigned int N>
166 const MPI_Comm & comm,
167 const std::vector<std::pair<std::string, std::array<double, N>>> &list,
168 const std::vector<std::pair<std::string, unsigned int>> &list_count,
169 const std::vector<std::string> & labels,
170 const unsigned int mm)
172 unsigned int max_count = 0;
174 for (
const auto &i : list_count)
175 max_count = std::max(max_count, i.second);
177 std::vector<double> temp1(list.size() * N);
179 for (
unsigned int i = 0, k = 0; i < list.size(); i++)
180 for (
unsigned int j = 0; j < N; j++, k++)
181 temp1[k] = list[i].second[j];
184 const auto temp2 = dealii::Utilities::MPI::min_max_avg(temp1, comm);
186 std::vector<std::pair<std::string,
187 std::array<dealii::Utilities::MPI::MinMaxAvg, N>>>
188 list_min_max(list.size());
190 for (
unsigned int i = 0, k = 0; i < list.size(); i++)
192 list_min_max[i].first = list[i].first;
193 for (
unsigned int j = 0; j < N; j++, k++)
194 list_min_max[i].second[j] = temp2[k];
198 const auto max_width =
199 std::max_element(list_min_max.begin(),
201 [](
const auto &a,
const auto &b) {
202 return a.first.size() < b.first.size();
208 const auto print_lines = [&]() {
209 stream <<
"+-" << std::string(max_width + 2,
'-') <<
"+";
211 for (
unsigned int i = 0; i < labels.size(); i++)
213 auto j = list_min_max[mm].second[i];
214 auto label = labels[i];
217 const unsigned int width =
static_cast<unsigned int>(3 + std::log(max_count) / log(10.0))+
218 static_cast<unsigned int>(8)+
219 static_cast<unsigned int>(8 + std::log(j.min) / log(10.0))+
220 static_cast<unsigned int>(8 + std::log(j.avg) / log(10.0))+
221 static_cast<unsigned int>(8 + std::log(j.max) / log(10.0));
223 stream << std::string(width,
'-');
232 stream <<
"| " << std::left << std::setw(max_width + 2) <<
""
235 for (
unsigned int i = 0; i < labels.size(); i++)
237 auto j = list_min_max[mm].second[i];
238 auto label = labels[i];
241 const unsigned int width =
static_cast<unsigned int>(3 + std::log(max_count) / log(10.0))+
242 static_cast<unsigned int>(8)+
243 static_cast<unsigned int>(8 + std::log(j.min) / log(10.0))+
244 static_cast<unsigned int>(8 + std::log(j.avg) / log(10.0))+
245 static_cast<unsigned int>(8 + std::log(j.max) / log(10.0));
247 stream << std::setw((width-label.size())/2) <<
"" << label << std::setw(width - label.size() - (width-label.size())/2) <<
"" ;
256 stream <<
"| " << std::left << std::setw(max_width + 2) <<
""
259 for (
auto j : list_min_max[mm].second)
262 stream << std::setw(3 + std::log(max_count) / log(10.0)) << std::right <<
"#";
263 stream << std::setw(8) <<
"%";
264 stream << std::setw(8 + std::log(j.min) / log(10.0)) << std::right <<
"min";
265 stream << std::setw(8 + std::log(j.avg) / log(10.0)) << std::right <<
"avg";
266 stream << std::setw(8 + std::log(j.max) / log(10.0)) << std::right <<
"max";
275 unsigned int row = 0;
276 for (
auto j : list_min_max)
278 stream <<
"| " << std::left << std::setw(max_width + 2) << j.first
281 for (
unsigned int col = 0; col < j.second.size(); col++)
283 auto i = j.second[col];
284 auto m = list_min_max[mm].second[col];
286 stream << std::fixed << std::setw(3 + std::log(max_count) / log(10.0)) << std::right << list_count[row].second;
287 stream << std::fixed << std::setprecision(2) << std::setw(8) << std::right << (i.sum * 100 / m.sum);
288 stream << std::fixed << std::setprecision(2) << std::setw(8 + std::log(m.min) / log(10.0)) << std::right << i.min;
289 stream << std::fixed << std::setprecision(2) << std::setw(8 + std::log(m.avg) / log(10.0)) << std::right << i.avg;
290 stream << std::fixed << std::setprecision(2) << std::setw(8 + std::log(m.max) / log(10.0)) << std::right << i.max;
299 stream << std::endl << std::endl;
311 monitor(
const std::string &label)
314 dealii::Utilities::System::MemoryStats stats;
315 dealii::Utilities::System::get_memory_stats(stats);
317 list.emplace_back(label,
318 std::array<double, 4>{
319 {
static_cast<double>(stats.VmPeak),
320 static_cast<double>(stats.VmSize),
321 static_cast<double>(stats.VmHWM),
322 static_cast<double>(stats.VmRSS)}});
327 template <
typename StreamType>
329 print(StreamType &stream,
const bool do_monitor =
true)
338 {
"VmPeak [kB]",
"VmSize [kB]",
"VmHWM [kB]",
"VmRSS [kB]"},
343 const MPI_Comm &comm;
345 std::vector<std::pair<std::string, std::array<double, 4>>> list;
353 , memory_consumpition_leaf(val)
363 insert(
const std::string &label,
const std::size_t &val)
365 vec.emplace_back(label, val);
368 template <
typename StreamType>
370 print(
const MPI_Comm &comm, StreamType &stream)
const
372 const std::vector<std::pair<std::string, std::size_t>> collection =
375 std::vector<std::pair<std::string, std::array<double, 1>>> collection_(
378 for (
unsigned int i = 0; i < collection.size(); i++)
379 collection_[i] = std::pair<std::string, std::array<double, 1>>{
381 std::array<double, 1>{{
static_cast<double>(collection[i].second)}}};
384 stream, comm, collection_, {
"Memory consumption [Byte]"}, 0);
388 memory_consumption()
const
390 return std::accumulate(vec.begin(),
392 memory_consumpition_leaf,
393 [](
const auto &a,
const auto &b) {
394 return a + b.memory_consumption();
400 std::vector<std::pair<std::string, std::size_t>>
403 std::vector<std::pair<std::string, std::size_t>> all;
405 all.emplace_back(label, this->memory_consumption());
407 for (
const auto &v : vec)
408 for (
const auto &i : v.collect())
409 all.emplace_back(label +
":" + i.first, i.second);
414 const std::string label;
415 const std::size_t memory_consumpition_leaf;
417 std::vector<MemoryConsumption> vec;
Definition memory_consumption.h:349
Definition memory_consumption.h:304