Reference documentation for deal.II version 9.1.1
\(\newcommand{\dealcoloneq}{\mathrel{\vcenter{:}}=}\)
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.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>
23 #include <deal.II/base/smartpointer.h>
24 #include <deal.II/base/thread_management.h>
25 
26 #include <deal.II/lac/vector.h>
27 
28 #include <iostream>
29 #include <memory>
30 #include <vector>
31 
32 DEAL_II_NAMESPACE_OPEN
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
475  release_all_unused_memory();
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
505 PrimitiveVectorMemory<VectorType>::free(const VectorType *const v)
506 {
507  delete v;
508 }
509 
510 
511 
512 #endif // DOXYGEN
513 
514 DEAL_II_NAMESPACE_CLOSE
515 
516 #endif
virtual void free(const VectorType *const) override
virtual VectorType * alloc()=0
virtual ~GrowingVectorMemory() override
static void release_unused_memory()
std::pair< bool, std::unique_ptr< BlockVector< number > > > entry_type
STL namespace.
void initialize(const size_type size)
std::vector< entry_type > * data
virtual void free(const VectorType *const)=0
virtual VectorType * alloc() override
static ::ExceptionBase & ExcNotAllocatedHere()
virtual ~VectorMemory() override=default
GrowingVectorMemory(const size_type initial_size=0, const bool log_statistics=false)
virtual void free(const VectorType *const v) override
#define DeclExceptionMsg(Exception, defaulttext)
Definition: exceptions.h:496
static Threads::Mutex mutex
unsigned int global_dof_index
Definition: types.h:89
static Pool & get_pool()
virtual std::size_t memory_consumption() const
virtual VectorType * alloc() override
Pointer & operator=(Pointer &&p) noexcept=default