Reference documentation for deal.II version GIT 0b65fff18a 2023-09-27 19:30: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_v<T>, std::size_t>
93  memory_consumption(const T &t);
94 
101  template <typename T>
102  inline std::enable_if_t<!(std::is_fundamental_v<T> || std::is_pointer_v<T>),
103  std::size_t>
104  memory_consumption(const T &t);
105 
112  inline std::size_t
113  memory_consumption(const char *string);
114 
119  template <typename T>
120  inline std::size_t
121  memory_consumption(const std::complex<T> &);
122 
127  template <typename T, std::size_t width>
128  inline std::size_t
130 
135  inline std::size_t
136  memory_consumption(const std::string &s);
137 
165  template <typename T>
166  inline std::size_t
167  memory_consumption(const std::vector<T> &v);
168 
189  template <typename T, std::size_t N>
190  inline std::size_t
191  memory_consumption(const std::array<T, N> &v);
192 
201  template <typename T, int N>
202  inline std::size_t
203  memory_consumption(const T (&v)[N]);
204 
212  inline std::size_t
213  memory_consumption(const std::vector<bool> &v);
214 
219  template <typename A, typename B>
220  inline std::size_t
221  memory_consumption(const std::pair<A, B> &p);
222 
232  template <typename T>
233  inline std::size_t
234  memory_consumption(const T *const);
235 
241  template <typename T>
242  inline std::size_t
243  memory_consumption(const std::shared_ptr<T> &);
244 
250  template <typename T>
251  inline std::size_t
252  memory_consumption(const std::unique_ptr<T> &);
253 } // namespace MemoryConsumption
254 
255 
256 
257 // now comes the implementation of these functions
258 
259 namespace MemoryConsumption
260 {
261  template <typename T>
262  inline std::enable_if_t<std::is_fundamental_v<T>, std::size_t>
264  {
265  return sizeof(T);
266  }
267 
268 
269 
270  inline std::size_t
271  memory_consumption(const char *string)
272  {
273  if (string == nullptr)
274  {
275  return 0;
276  }
277  else
278  {
279  return sizeof(char) * (strlen(string) /*Remember the NUL*/ + 1);
280  }
281  }
282 
283 
284 
285  template <typename T>
286  inline std::size_t
287  memory_consumption(const std::complex<T> &)
288  {
289  return sizeof(std::complex<T>);
290  }
291 
292 
293 
294  template <typename T, std::size_t width>
295  inline std::size_t
297  {
298  return sizeof(VectorizedArray<T, width>);
299  }
300 
301 
302 
303  inline std::size_t
304  memory_consumption(const std::string &s)
305  {
306  return sizeof(s) + s.size();
307  }
308 
309 
310 
311  template <typename T>
312  std::size_t
313  memory_consumption(const std::vector<T> &v)
314  {
315  // shortcut for types that do not allocate memory themselves
316  if (std::is_fundamental_v<T> || std::is_pointer_v<T>)
317  {
318  return v.capacity() * sizeof(T) + sizeof(v);
319  }
320  else
321  {
322  std::size_t mem = sizeof(std::vector<T>);
323  for (unsigned int i = 0; i < v.size(); ++i)
324  {
325  mem += memory_consumption(v[i]);
326  }
327  mem += (v.capacity() - v.size()) * sizeof(T);
328  return mem;
329  }
330  }
331 
332 
333 
334  template <typename T, std::size_t N>
335  std::size_t
336  memory_consumption(const std::array<T, N> &v)
337  {
338  // shortcut for types that do not allocate memory themselves
339  if (std::is_fundamental_v<T> || std::is_pointer_v<T>)
340  {
341  return sizeof(v);
342  }
343  else
344  {
345  std::size_t mem = 0;
346  for (std::size_t i = 0; i != N; ++i)
347  mem += memory_consumption(v[i]);
348  return mem;
349  }
350  }
351 
352 
353 
354  template <typename T, int N>
355  std::size_t
356  memory_consumption(const T (&v)[N])
357  {
358  std::size_t mem = 0;
359  for (unsigned int i = 0; i < N; ++i)
360  mem += memory_consumption(v[i]);
361  return mem;
362  }
363 
364 
365 
366  inline std::size_t
367  memory_consumption(const std::vector<bool> &v)
368  {
369  return v.capacity() / 8 + sizeof(v);
370  }
371 
372 
373 
374  template <typename A, typename B>
375  inline std::size_t
376  memory_consumption(const std::pair<A, B> &p)
377  {
378  return (memory_consumption(p.first) + memory_consumption(p.second));
379  }
380 
381 
382 
383  template <typename T>
384  inline std::size_t
385  memory_consumption(const T *const)
386  {
387  return sizeof(T *);
388  }
389 
390 
391 
392  template <typename T>
393  inline std::size_t
394  memory_consumption(const std::shared_ptr<T> &)
395  {
396  return sizeof(std::shared_ptr<T>);
397  }
398 
399 
400 
401  template <typename T>
402  inline std::size_t
403  memory_consumption(const std::unique_ptr<T> &)
404  {
405  return sizeof(std::unique_ptr<T>);
406  }
407 
408 
409 
410  template <typename T>
411  inline std::enable_if_t<!(std::is_fundamental_v<T> || std::is_pointer_v<T>),
412  std::size_t>
414  {
415  return t.memory_consumption();
416  }
417 } // namespace MemoryConsumption
418 
420 
421 #endif
#define DEAL_II_NAMESPACE_OPEN
Definition: config.h:477
#define DEAL_II_NAMESPACE_CLOSE
Definition: config.h:478
static const char T
static const char N
std::enable_if_t< std::is_fundamental_v< T >, std::size_t > memory_consumption(const T &t)