From 2d274d72f731571161361717a701f2d852ec7e01 Mon Sep 17 00:00:00 2001 From: kanschat Date: Fri, 21 Sep 2007 01:24:32 +0000 Subject: [PATCH] add default memory pool git-svn-id: https://svn.dealii.org/trunk@15228 0785d39b-7218-0410-832d-ea1e28bc413d --- deal.II/lac/include/lac/vector_memory.h | 43 ++++++++++-- deal.II/lac/source/vector_memory.cc | 92 +++++++++++++++++++++++++ 2 files changed, 131 insertions(+), 4 deletions(-) create mode 100644 deal.II/lac/source/vector_memory.cc diff --git a/deal.II/lac/include/lac/vector_memory.h b/deal.II/lac/include/lac/vector_memory.h index 774592e157..852eb042f6 100644 --- a/deal.II/lac/include/lac/vector_memory.h +++ b/deal.II/lac/include/lac/vector_memory.h @@ -99,6 +99,21 @@ class VectorMemory : public Subscriptor */ virtual void free (const VECTOR * const) = 0; + /** + * Access the default memory + * space deal.II is offering for + * vectors of this kind. + * + * This function accesses static + * VectorMemory objects used by + * deal.II classes to allocate + * vectors. It is good practice + * to use these in your program + * as well, in order to optimize + * memory usage. + */ + static VectorMemory& default_pool(); + /** @addtogroup Exceptions * @{ */ @@ -111,6 +126,16 @@ class VectorMemory : public Subscriptor * this memory pool. */ DeclException0(ExcNotAllocatedHere); + + /** + * You tried to access the + * deal.II + * VectorMemory::default_pool(), + * but the vector class you are + * using is not a standard + * deal.II vector class. + */ + DeclException0(ExcNoDefaultMemoryForThisVectorClass); //@} }; @@ -251,6 +276,12 @@ class GrowingVectorMemory : public VectorMemory */ unsigned int memory_consumption() const; + /** + * A flag controlling the logging + * of statistics at the end. + */ + bool log_statistics; + private: /** * Type to enter into the @@ -292,6 +323,7 @@ class GrowingVectorMemory : public VectorMemory template GrowingVectorMemory::GrowingVectorMemory (const unsigned int initial_size) : + log_statistics(false), pool(initial_size) { Threads::ThreadMutex::ScopedLock lock(mutex); @@ -326,10 +358,13 @@ GrowingVectorMemory::~GrowingVectorMemory() ++n; delete i->second; } - deallog << "GrowingVectorMemory:Overall allocated vectors: " - << n_alloc << std::endl; - deallog << "GrowingVectorMemory:Maximum allocated vectors: " - << pool.size() << std::endl; + if (log_statistics) + { + deallog << "GrowingVectorMemory:Overall allocated vectors: " + << n_alloc << std::endl; + deallog << "GrowingVectorMemory:Maximum allocated vectors: " + << pool.size() << std::endl; + } pool.clear (); // write out warning if memory leak diff --git a/deal.II/lac/source/vector_memory.cc b/deal.II/lac/source/vector_memory.cc new file mode 100644 index 0000000000..d77f5b1198 --- /dev/null +++ b/deal.II/lac/source/vector_memory.cc @@ -0,0 +1,92 @@ +//--------------------------------------------------------------------------- +// $Id$ +// Version: $Name$ +// +// Copyright (C) 2007 by the deal.II authors +// +// This file is subject to QPL and may not be distributed +// without copyright and license information. Please refer +// to the file deal.II/doc/license.html for the text and +// further information on this license. +// +//--------------------------------------------------------------------------- + + +#include +#include +#include + +DEAL_II_NAMESPACE_OPEN + + +namespace +{ + GrowingVectorMemory > default_pool_Vector_double; + GrowingVectorMemory > default_pool_Vector_float; + GrowingVectorMemory > default_pool_BlockVector_double; + GrowingVectorMemory > default_pool_BlockVector_float; + + template + inline + VectorMemory* + default_pool_select() + { + Assert(false, typename VectorMemory::ExcNoDefaultMemoryForThisVectorClass()); + return 0; + } + + + template <> + inline + VectorMemory >* + default_pool_select >() + { + return &default_pool_Vector_double; + } + + + template <> + inline + VectorMemory >* + default_pool_select >() + { + return &default_pool_Vector_float; + } + + + + template <> + inline + VectorMemory >* + default_pool_select >() + { + return &default_pool_BlockVector_double; + } + + + template <> + inline + VectorMemory >* + default_pool_select >() + { + return &default_pool_BlockVector_float; + } + +} + + +template +VectorMemory& +VectorMemory::default_pool() +{ + return *default_pool_select(); +} + + +template class VectorMemory >; +template class VectorMemory >; +template class VectorMemory >; +template class VectorMemory >; + + +DEAL_II_NAMESPACE_CLOSE -- 2.39.5