Reference documentation for deal.II version 9.0.0
vector_memory.h
1 // ---------------------------------------------------------------------
2 //
3 // Copyright (C) 1998 - 2018 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 at
12 // the top level of the deal.II distribution.
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 #include <deal.II/base/smartpointer.h>
22 #include <deal.II/base/logstream.h>
23 #include <deal.II/base/thread_management.h>
24 #include <deal.II/lac/vector.h>
25 
26 #include <vector>
27 #include <iostream>
28 #include <memory>
29 
30 DEAL_II_NAMESPACE_OPEN
31 
32 
35 
103 template <typename VectorType = ::Vector<double> >
104 class VectorMemory : public Subscriptor
105 {
106 public:
107 
113  virtual ~VectorMemory () = default;
114 
133  virtual VectorType *alloc () = 0;
134 
149  virtual void free (const VectorType *const) = 0;
150 
160  "You are trying to deallocate a vector from a memory pool, but this "
161  "vector has not actually been allocated by the same pool before.");
162 
164 
187  class Pointer : public std::unique_ptr<VectorType, std::function<void (VectorType *)> >
188  {
189  public:
197  Pointer() = default;
198 
203  Pointer(Pointer &&p) noexcept = default;
204 
209  Pointer &operator = (Pointer &&p) noexcept = default;
210 
216 
220  ~Pointer() = default;
221  };
222 };
223 
224 
225 
233 template <typename VectorType = ::Vector<double> >
234 class PrimitiveVectorMemory : public VectorMemory<VectorType>
235 {
236 public:
259  virtual VectorType *alloc ();
260 
278  virtual void free (const VectorType *const v);
279 };
280 
281 
282 
310 template <typename VectorType = ::Vector<double> >
311 class GrowingVectorMemory : public VectorMemory<VectorType>
312 {
313 public:
318 
323  GrowingVectorMemory (const size_type initial_size = 0,
324  const bool log_statistics = false);
325 
332  virtual ~GrowingVectorMemory();
333 
352  virtual VectorType *alloc ();
353 
371  virtual void free (const VectorType *const);
372 
376  static void release_unused_memory ();
377 
381  virtual std::size_t memory_consumption() const;
382 
383 private:
390  typedef std::pair<bool, std::unique_ptr<VectorType> > entry_type;
391 
401  struct Pool
402  {
406  Pool();
407 
411  ~Pool();
412 
416  void initialize(const size_type size);
417 
421  std::vector<entry_type> *data;
422  };
423 
427  static Pool pool;
428 
434 
440 
445 
451 };
452 
453 
454 
455 namespace internal
456 {
457  namespace GrowingVectorMemoryImplementation
458  {
459  void release_all_unused_memory();
460  }
461 }
462 
465 #ifndef DOXYGEN
466 /* --------------------- inline functions ---------------------- */
467 
468 
469 template <typename VectorType>
470 inline
472  :
473  std::unique_ptr<VectorType, std::function<void (VectorType *)> >
474  (mem.alloc(), [&mem](VectorType *v)
475 {
476  mem.free(v);
477 })
478 {}
479 
480 
481 
482 template <typename VectorType>
483 VectorType *
485 {
486  return new VectorType();
487 }
488 
489 
490 
491 template <typename VectorType>
492 void
493 PrimitiveVectorMemory<VectorType>::free (const VectorType *const v)
494 {
495  delete v;
496 }
497 
498 
499 
500 
501 #endif // DOXYGEN
502 
503 DEAL_II_NAMESPACE_CLOSE
504 
505 #endif
virtual VectorType * alloc()=0
static void release_unused_memory()
virtual VectorType * alloc()
STL namespace.
void initialize(const size_type size)
std::vector< entry_type > * data
virtual void free(const VectorType *const)=0
static ::ExceptionBase & ExcNotAllocatedHere()
GrowingVectorMemory(const size_type initial_size=0, const bool log_statistics=false)
unsigned int global_dof_index
Definition: types.h:88
virtual VectorType * alloc()
#define DeclExceptionMsg(Exception, defaulttext)
Definition: exceptions.h:335
static Threads::Mutex mutex
virtual void free(const VectorType *const)
virtual ~VectorMemory()=default
virtual ~GrowingVectorMemory()
std::pair< bool, std::unique_ptr< VectorType > > entry_type
virtual void free(const VectorType *const v)
virtual std::size_t memory_consumption() const
types::global_dof_index size_type
Pointer & operator=(Pointer &&p) noexcept=default