Reference documentation for deal.II version GIT d7aca55de5 2022-08-10 12:50:02+00:00
\(\newcommand{\dealvcentcolon}{\mathrel{\mathop{:}}}\) \(\newcommand{\dealcoloneq}{\dealvcentcolon\mathrel{\mkern-1.2mu}=}\) \(\newcommand{\jump}[1]{\left[\!\left[ #1 \right]\!\right]}\) \(\newcommand{\average}[1]{\left\{\!\left\{ #1 \right\}\!\right\}}\)
memory_consumption.h
Go to the documentation of this file.
1 // ---------------------------------------------------------------------
2 //
3 // Copyright (C) 2000 - 2022 by the deal.II authors
4 //
5 // This file is part of the deal.II library.
6 //
7 // The deal.II library is free software; you can use it, redistribute
8 // it, and/or modify it under the terms of the GNU Lesser General
9 // Public License as published by the Free Software Foundation; either
10 // version 2.1 of the License, or (at your option) any later version.
11 // The full text of the license can be found in the file LICENSE.md at
12 // the top level directory of deal.II.
13 //
14 // ---------------------------------------------------------------------
15 
16 #ifndef dealii_memory_consumption_h
17 #define dealii_memory_consumption_h
18 
19 
20 #include <deal.II/base/config.h>
21 
22 #include <array>
23 #include <complex>
24 #include <cstddef>
25 #include <cstring>
26 #include <memory>
27 #include <string>
28 #include <type_traits>
29 #include <vector>
30 
32 
85 {
91  template <typename T>
92  inline std::enable_if_t<std::is_fundamental<T>::value, std::size_t>
93  memory_consumption(const T &t);
94 
101  template <typename T>
102  inline std::enable_if_t<!(std::is_fundamental<T>::value ||
103  std::is_pointer<T>::value),
104  std::size_t>
105  memory_consumption(const T &t);
106 
113  inline std::size_t
114  memory_consumption(const char *string);
115 
120  template <typename T>
121  inline std::size_t
122  memory_consumption(const std::complex<T> &);
123 
128  template <typename T, std::size_t width>
129  inline std::size_t
131 
136  inline std::size_t
137  memory_consumption(const std::string &s);
138 
166  template <typename T>
167  inline std::size_t
168  memory_consumption(const std::vector<T> &v);
169 
190  template <typename T, std::size_t N>
191  inline std::size_t
192  memory_consumption(const std::array<T, N> &v);
193 
202  template <typename T, int N>
203  inline std::size_t
204  memory_consumption(const T (&v)[N]);
205 
213  inline std::size_t
214  memory_consumption(const std::vector<bool> &v);
215 
220  template <typename A, typename B>
221  inline std::size_t
222  memory_consumption(const std::pair<A, B> &p);
223 
233  template <typename T>
234  inline std::size_t
235  memory_consumption(const T *const);
236 
242  template <typename T>
243  inline std::size_t
244  memory_consumption(const std::shared_ptr<T> &);
245 
251  template <typename T>
252  inline std::size_t
253  memory_consumption(const std::unique_ptr<T> &);
254 } // namespace MemoryConsumption
255 
256 
257 
258 // now comes the implementation of these functions
259 
260 namespace MemoryConsumption
261 {
262  template <typename T>
263  inline std::enable_if_t<std::is_fundamental<T>::value, std::size_t>
265  {
266  return sizeof(T);
267  }
268 
269 
270 
271  inline std::size_t
272  memory_consumption(const char *string)
273  {
274  if (string == nullptr)
275  {
276  return 0;
277  }
278  else
279  {
280  return sizeof(char) * (strlen(string) /*Remember the NUL*/ + 1);
281  }
282  }
283 
284 
285 
286  template <typename T>
287  inline std::size_t
288  memory_consumption(const std::complex<T> &)
289  {
290  return sizeof(std::complex<T>);
291  }
292 
293 
294 
295  template <typename T, std::size_t width>
296  inline std::size_t
298  {
299  return sizeof(VectorizedArray<T, width>);
300  }
301 
302 
303 
304  inline std::size_t
305  memory_consumption(const std::string &s)
306  {
307  return sizeof(s) + s.size();
308  }
309 
310 
311 
312  template <typename T>
313  std::size_t
314  memory_consumption(const std::vector<T> &v)
315  {
316  // shortcut for types that do not allocate memory themselves
317  if (std::is_fundamental<T>::value || std::is_pointer<T>::value)
318  {
319  return v.capacity() * sizeof(T) + sizeof(v);
320  }
321  else
322  {
323  std::size_t mem = sizeof(std::vector<T>);
324  for (unsigned int i = 0; i < v.size(); ++i)
325  {
326  mem += memory_consumption(v[i]);
327  }
328  mem += (v.capacity() - v.size()) * sizeof(T);
329  return mem;
330  }
331  }
332 
333 
334 
335  template <typename T, std::size_t N>
336  std::size_t
337  memory_consumption(const std::array<T, N> &v)
338  {
339  // shortcut for types that do not allocate memory themselves
340  if (std::is_fundamental<T>::value || std::is_pointer<T>::value)
341  {
342  return sizeof(v);
343  }
344  else
345  {
346  std::size_t mem = 0;
347  for (std::size_t i = 0; i != N; ++i)
348  mem += memory_consumption(v[i]);
349  return mem;
350  }
351  }
352 
353 
354 
355  template <typename T, int N>
356  std::size_t
357  memory_consumption(const T (&v)[N])
358  {
359  std::size_t mem = 0;
360  for (unsigned int i = 0; i < N; ++i)
361  mem += memory_consumption(v[i]);
362  return mem;
363  }
364 
365 
366 
367  inline std::size_t
368  memory_consumption(const std::vector<bool> &v)
369  {
370  return v.capacity() / 8 + sizeof(v);
371  }
372 
373 
374 
375  template <typename A, typename B>
376  inline std::size_t
377  memory_consumption(const std::pair<A, B> &p)
378  {
379  return (memory_consumption(p.first) + memory_consumption(p.second));
380  }
381 
382 
383 
384  template <typename T>
385  inline std::size_t
386  memory_consumption(const T *const)
387  {
388  return sizeof(T *);
389  }
390 
391 
392 
393  template <typename T>
394  inline std::size_t
395  memory_consumption(const std::shared_ptr<T> &)
396  {
397  return sizeof(std::shared_ptr<T>);
398  }
399 
400 
401 
402  template <typename T>
403  inline std::size_t
404  memory_consumption(const std::unique_ptr<T> &)
405  {
406  return sizeof(std::unique_ptr<T>);
407  }
408 
409 
410 
411  template <typename T>
412  inline std::enable_if_t<!(std::is_fundamental<T>::value ||
413  std::is_pointer<T>::value),
414  std::size_t>
416  {
417  return t.memory_consumption();
418  }
419 } // namespace MemoryConsumption
420 
422 
423 #endif
#define DEAL_II_NAMESPACE_OPEN
Definition: config.h:442
#define DEAL_II_NAMESPACE_CLOSE
Definition: config.h:443
static const char T
static const char N
std::enable_if_t< std::is_fundamental< T >::value, std::size_t > memory_consumption(const T &t)