Reference documentation for deal.II version 9.2.0
\(\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\}}\)
vector_memory.h
Go to the documentation of this file.
1 // ---------------------------------------------------------------------
2 //
3 // Copyright (C) 1998 - 2020 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_vector_memory_h
17 #define dealii_vector_memory_h
18 
19 
20 #include <deal.II/base/config.h>
21 
22 #include <deal.II/base/logstream.h>
25 
26 #include <deal.II/lac/vector.h>
27 
28 #include <iostream>
29 #include <memory>
30 #include <vector>
31 
33 
34 
37 
106 template <typename VectorType = ::Vector<double>>
107 class VectorMemory : public Subscriptor
108 {
109 public:
115  virtual ~VectorMemory() override = default;
116 
135  virtual VectorType *
136  alloc() = 0;
137 
152  virtual void
153  free(const VectorType *const) = 0;
154 
165  "You are trying to deallocate a vector from a memory pool, but this "
166  "vector has not actually been allocated by the same pool before.");
167 
169 
192  class Pointer
193  : public std::unique_ptr<VectorType, std::function<void(VectorType *)>>
194  {
195  public:
203  Pointer() = default;
204 
209  Pointer(Pointer &&p) noexcept = default;
210 
215  Pointer &
216  operator=(Pointer &&p) noexcept = default;
217 
223 
227  ~Pointer() = default;
228  };
229 };
230 
231 
232 
240 template <typename VectorType = ::Vector<double>>
241 class PrimitiveVectorMemory : public VectorMemory<VectorType>
242 {
243 public:
266  virtual VectorType *
267  alloc() override;
268 
286  virtual void
287  free(const VectorType *const v) override;
288 };
289 
290 
291 
319 template <typename VectorType = ::Vector<double>>
320 class GrowingVectorMemory : public VectorMemory<VectorType>
321 {
322 public:
327 
332  GrowingVectorMemory(const size_type initial_size = 0,
333  const bool log_statistics = false);
334 
341  virtual ~GrowingVectorMemory() override;
342 
361  virtual VectorType *
362  alloc() override;
363 
381  virtual void
382  free(const VectorType *const) override;
383 
387  static void
389 
393  virtual std::size_t
394  memory_consumption() const;
395 
396 private:
403  using entry_type = std::pair<bool, std::unique_ptr<VectorType>>;
404 
414  struct Pool
415  {
419  Pool();
420 
424  ~Pool();
425 
429  void
430  initialize(const size_type size);
431 
435  std::vector<entry_type> *data;
436  };
437 
441  static Pool &
442  get_pool();
443 
449 
455 
460 
466 };
467 
468 
469 
470 namespace internal
471 {
472  namespace GrowingVectorMemoryImplementation
473  {
474  void
476  }
477 } // namespace internal
478 
481 #ifndef DOXYGEN
482 /* --------------------- inline functions ---------------------- */
483 
484 
485 template <typename VectorType>
487  : std::unique_ptr<VectorType, std::function<void(VectorType *)>>(
488  mem.alloc(),
489  [&mem](VectorType *v) { mem.free(v); })
490 {}
491 
492 
493 
494 template <typename VectorType>
495 VectorType *
497 {
498  return new VectorType();
499 }
500 
501 
502 
503 template <typename VectorType>
504 void
506 {
507  delete v;
508 }
509 
510 
511 
512 #endif // DOXYGEN
513 
515 
516 #endif
DeclExceptionMsg
#define DeclExceptionMsg(Exception, defaulttext)
Definition: exceptions.h:496
GrowingVectorMemory::~GrowingVectorMemory
virtual ~GrowingVectorMemory() override
VectorMemory::Pointer::Pointer
Pointer()=default
GrowingVectorMemory::free
virtual void free(const VectorType *const) override
PrimitiveVectorMemory::alloc
virtual VectorType * alloc() override
Threads::Mutex
Definition: thread_management.h:91
GrowingVectorMemory::release_unused_memory
static void release_unused_memory()
VectorMemory::alloc
virtual VectorType * alloc()=0
VectorType
GrowingVectorMemory::memory_consumption
virtual std::size_t memory_consumption() const
GrowingVectorMemory::total_alloc
size_type total_alloc
Definition: vector_memory.h:448
PrimitiveVectorMemory::free
virtual void free(const VectorType *const v) override
GrowingVectorMemory::GrowingVectorMemory
GrowingVectorMemory(const size_type initial_size=0, const bool log_statistics=false)
VectorMemory::Pointer::~Pointer
~Pointer()=default
VectorMemory::free
virtual void free(const VectorType *const)=0
VectorMemory::~VectorMemory
virtual ~VectorMemory() override=default
GrowingVectorMemory< BlockVector< number > >::entry_type
std::pair< bool, std::unique_ptr< BlockVector< number > > > entry_type
Definition: vector_memory.h:403
GrowingVectorMemory::alloc
virtual VectorType * alloc() override
GrowingVectorMemory
Definition: vector_memory.h:320
Subscriptor
Definition: subscriptor.h:62
GrowingVectorMemory::Pool::data
std::vector< entry_type > * data
Definition: vector_memory.h:435
thread_management.h
GrowingVectorMemory::Pool::~Pool
~Pool()
GrowingVectorMemory::Pool::Pool
Pool()
GrowingVectorMemory::Pool
Definition: vector_memory.h:414
VectorMemory::ExcNotAllocatedHere
static ::ExceptionBase & ExcNotAllocatedHere()
types::global_dof_index
unsigned int global_dof_index
Definition: types.h:76
VectorMemory::Pointer
Definition: vector_memory.h:192
DEAL_II_NAMESPACE_OPEN
#define DEAL_II_NAMESPACE_OPEN
Definition: config.h:358
smartpointer.h
internal::GrowingVectorMemoryImplementation::release_all_unused_memory
void release_all_unused_memory()
Definition: vector_memory.cc:44
GrowingVectorMemory::log_statistics
bool log_statistics
Definition: vector_memory.h:459
GrowingVectorMemory::mutex
static Threads::Mutex mutex
Definition: vector_memory.h:465
unsigned int
PrimitiveVectorMemory
Definition: vector_memory.h:241
vector.h
VectorMemory::Pointer::operator=
Pointer & operator=(Pointer &&p) noexcept=default
GrowingVectorMemory::Pool::initialize
void initialize(const size_type size)
config.h
GrowingVectorMemory::current_alloc
size_type current_alloc
Definition: vector_memory.h:454
internal
Definition: aligned_vector.h:369
DEAL_II_NAMESPACE_CLOSE
#define DEAL_II_NAMESPACE_CLOSE
Definition: config.h:359
logstream.h
GrowingVectorMemory::get_pool
static Pool & get_pool()
VectorMemory
Definition: vector_memory.h:107