Reference documentation for deal.II version 9.1.0-pre
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
388  release_unused_memory();
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 pool;
442 
448 
454 
459 
465 };
466 
467 
468 
469 namespace internal
470 {
471  namespace GrowingVectorMemoryImplementation
472  {
473  void
474  release_all_unused_memory();
475  }
476 } // namespace internal
477 
480 #ifndef DOXYGEN
481 /* --------------------- inline functions ---------------------- */
482 
483 
484 template <typename VectorType>
486  : std::unique_ptr<VectorType, std::function<void(VectorType *)>>(
487  mem.alloc(),
488  [&mem](VectorType *v) { mem.free(v); })
489 {}
490 
491 
492 
493 template <typename VectorType>
494 VectorType *
496 {
497  return new VectorType();
498 }
499 
500 
501 
502 template <typename VectorType>
503 void
504 PrimitiveVectorMemory<VectorType>::free(const VectorType *const v)
505 {
506  delete v;
507 }
508 
509 
510 
511 #endif // DOXYGEN
512 
513 DEAL_II_NAMESPACE_CLOSE
514 
515 #endif
virtual VectorType * alloc()=0
unsigned long long int global_dof_index
Definition: types.h:72
std::vector< entry_type > * data
virtual void free(const VectorType *const)=0
static::ExceptionBase & ExcNotAllocatedHere()
virtual ~VectorMemory() override=default
virtual void free(const VectorType *const v) override
#define DeclExceptionMsg(Exception, defaulttext)
Definition: exceptions.h:397
static Threads::Mutex mutex
std::pair< bool, std::unique_ptr< BlockVector< number > >> entry_type
virtual VectorType * alloc() override
Pointer & operator=(Pointer &&p) noexcept=default