Reference documentation for deal.II version 9.1.1
\(\newcommand{\dealcoloneq}{\mathrel{\vcenter{:}}=}\)
Classes | Public Types | Public Member Functions | Static Public Member Functions | Private Types | Static Private Member Functions | Private Attributes | Static Private Attributes | List of all members
GrowingVectorMemory< VectorType > Class Template Reference

#include <deal.II/lac/vector_memory.h>

Inheritance diagram for GrowingVectorMemory< VectorType >:
[legend]

Classes

struct  Pool
 

Public Types

using size_type = types::global_dof_index
 

Public Member Functions

 GrowingVectorMemory (const size_type initial_size=0, const bool log_statistics=false)
 
virtual ~GrowingVectorMemory () override
 
virtual VectorType * alloc () override
 
virtual void free (const VectorType *const) override
 
virtual std::size_t memory_consumption () const
 
- Public Member Functions inherited from VectorMemory< VectorType >
virtual ~VectorMemory () override=default
 
- Public Member Functions inherited from Subscriptor
 Subscriptor ()
 
 Subscriptor (const Subscriptor &)
 
 Subscriptor (Subscriptor &&) noexcept
 
virtual ~Subscriptor ()
 
Subscriptoroperator= (const Subscriptor &)
 
Subscriptoroperator= (Subscriptor &&) noexcept
 
void subscribe (std::atomic< bool > *const validity, const std::string &identifier="") const
 
void unsubscribe (std::atomic< bool > *const validity, const std::string &identifier="") const
 
unsigned int n_subscriptions () const
 
template<typename StreamType >
void list_subscribers (StreamType &stream) const
 
void list_subscribers () const
 
template<class Archive >
void serialize (Archive &ar, const unsigned int version)
 

Static Public Member Functions

static void release_unused_memory ()
 
- Static Public Member Functions inherited from VectorMemory< VectorType >
static ::ExceptionBaseExcNotAllocatedHere ()
 
- Static Public Member Functions inherited from Subscriptor
static ::ExceptionBaseExcInUse (int arg1, std::string arg2, std::string arg3)
 
static ::ExceptionBaseExcNoSubscriber (std::string arg1, std::string arg2)
 

Private Types

using entry_type = std::pair< bool, std::unique_ptr< VectorType > >
 

Static Private Member Functions

static Poolget_pool ()
 

Private Attributes

size_type total_alloc
 
size_type current_alloc
 
bool log_statistics
 

Static Private Attributes

static Threads::Mutex mutex
 

Detailed Description

template<typename VectorType = ::Vector<double>>
class GrowingVectorMemory< VectorType >

A pool based memory management class. See the documentation of the base class for a description of its purpose.

Each time a vector is requested from this class, it checks if it has one available and returns its address, or allocates a new one on the heap. If a vector is returned from its user, through the GrowingVectorMemory::free() member function, it doesn't return the allocated memory to the operating system memory subsystem, but keeps it around unused for later use if GrowingVectorMemory::alloc() is called again. The class therefore avoid the overhead of repeatedly allocating memory on the heap if temporary vectors are required and released frequently; on the other hand, it doesn't release once-allocated memory at the earliest possible time and may therefore lead to an increased overall memory consumption.

All GrowingVectorMemory objects of the same vector type use the same memory pool. (In other words: The pool of vectors from which this class draws is global, rather than a regular member variable of the current class that is destroyed at the time that the surrounding GrowingVectorMemory object is destroyed.) Therefore, functions can create such a GrowingVectorMemory object whenever needed without the performance penalty of creating a new memory pool every time. A drawback of this policy is that vectors once allocated are only released at the end of the program run.

Author
Guido Kanschat, 1999, 2007; Wolfgang Bangerth, 2017.

Definition at line 320 of file vector_memory.h.

Member Typedef Documentation

◆ size_type

template<typename VectorType = ::Vector<double>>
using GrowingVectorMemory< VectorType >::size_type = types::global_dof_index

Declare type for container size.

Definition at line 326 of file vector_memory.h.

◆ entry_type

template<typename VectorType = ::Vector<double>>
using GrowingVectorMemory< VectorType >::entry_type = std::pair<bool, std::unique_ptr<VectorType> >
private

A type that describes this entries of an array that represents the vectors stored by this object. The first component of the pair is be a flag telling whether the vector is used, the second a pointer to the vector itself.

Definition at line 403 of file vector_memory.h.

Constructor & Destructor Documentation

◆ GrowingVectorMemory()

template<typename VectorType = ::Vector<double>>
GrowingVectorMemory< VectorType >::GrowingVectorMemory ( const size_type  initial_size = 0,
const bool  log_statistics = false 
)

Constructor. The argument allows to preallocate a certain number of vectors. The default is not to do this.

◆ ~GrowingVectorMemory()

template<typename VectorType = ::Vector<double>>
virtual GrowingVectorMemory< VectorType >::~GrowingVectorMemory ( )
overridevirtual

Destructor. The destructor also checks that all vectors that have been allocated through the current object have all been released again. However, as discussed in the class documentation, this does not imply that their memory is returned to the operating system.

Member Function Documentation

◆ alloc()

template<typename VectorType = ::Vector<double>>
virtual VectorType* GrowingVectorMemory< VectorType >::alloc ( )
overridevirtual

Return a pointer to a new vector. The number of elements or their subdivision into blocks (if applicable) is unspecified and users of this function should reset vectors to their proper size. The same holds for the contents of vectors: they are unspecified. In other words, the place that calls this function will need to resize or reinitialize it appropriately.

Warning
Just like using new and delete explicitly in code invites bugs where memory is leaked (either because the corresponding delete is forgotten altogether, or because of exception safety issues), using the alloc() and free() functions explicitly invites writing code that accidentally leaks memory. You should consider using the VectorMemory::Pointer class instead, which provides the same kind of service that std::unique provides for arbitrary memory allocated on the heap.

Implements VectorMemory< VectorType >.

◆ free()

template<typename VectorType = ::Vector<double>>
virtual void GrowingVectorMemory< VectorType >::free ( const VectorType *  const)
overridevirtual

Return a vector and indicate that it is not going to be used any further by the instance that called alloc() to get a pointer to it.

For the present class, this means retaining the vector for later reuse by the alloc() method.

Warning
Just like using new and delete explicitly in code invites bugs where memory is leaked (either because the corresponding delete is forgotten altogether, or because of exception safety issues), using the alloc() and free() functions explicitly invites writing code that accidentally leaks memory. You should consider using the VectorMemory::Pointer class instead, which provides the same kind of service that std::unique provides for arbitrary memory allocated on the heap.

Implements VectorMemory< VectorType >.

◆ release_unused_memory()

template<typename VectorType = ::Vector<double>>
static void GrowingVectorMemory< VectorType >::release_unused_memory ( )
static

Release all vectors that are not currently in use.

◆ memory_consumption()

template<typename VectorType = ::Vector<double>>
virtual std::size_t GrowingVectorMemory< VectorType >::memory_consumption ( ) const
virtual

Memory consumed by this class and all currently allocated vectors.

◆ get_pool()

template<typename VectorType = ::Vector<double>>
static Pool& GrowingVectorMemory< VectorType >::get_pool ( )
staticprivate

Return an array of allocated vectors.

Member Data Documentation

◆ total_alloc

template<typename VectorType = ::Vector<double>>
size_type GrowingVectorMemory< VectorType >::total_alloc
private

Overall number of allocations. Only used for bookkeeping and to generate output at the end of an object's lifetime.

Definition at line 448 of file vector_memory.h.

◆ current_alloc

template<typename VectorType = ::Vector<double>>
size_type GrowingVectorMemory< VectorType >::current_alloc
private

Number of vectors currently allocated in this object; used for detecting memory leaks.

Definition at line 454 of file vector_memory.h.

◆ log_statistics

template<typename VectorType = ::Vector<double>>
bool GrowingVectorMemory< VectorType >::log_statistics
private

A flag controlling the logging of statistics by the destructor.

Definition at line 459 of file vector_memory.h.

◆ mutex

template<typename VectorType = ::Vector<double>>
Threads::Mutex GrowingVectorMemory< VectorType >::mutex
staticprivate

Mutex to synchronize access to internal data of this object from multiple threads.

Definition at line 465 of file vector_memory.h.


The documentation for this class was generated from the following file: