KratosMultiphysics
KRATOS Multiphysics (Kratos) is a framework for building parallel, multi-disciplinary simulation software, aiming at modularity, extensibility, and high performance. Kratos is written in C++, and counts with an extensive Python interface.
memory_pool.h
Go to the documentation of this file.
1 // | / |
2 // ' / __| _` | __| _ \ __|
3 // . \ | ( | | ( |\__ `
4 // _|\_\_| \__,_|\__|\___/ ____/
5 // Multi-Physics
6 //
7 // License: BSD License
8 // Kratos default license: kratos/license.txt
9 //
10 // Main authors: Pooyan Dadvand
11 //
12 //
13 
14 
15 #if !defined(KRATOS_MEMORY_POOL_H_INCLUDED )
16 #define KRATOS_MEMORY_POOL_H_INCLUDED
17 
18 #include <memory>
19 #include <vector>
20 #include <sstream>
21 
23 
24 namespace Kratos
25 {
28 
31 
33 
37  class MemoryPool
38  {
39  public:
42 
43  using PoolsContainerType = std::vector<FixedSizeMemoryPool*>;
44 
48 
50  MemoryPool(MemoryPool const& rOther) = delete;
51 
53  virtual ~MemoryPool() {
54  for (auto i_pool = GetInstance().mPools.begin(); i_pool != GetInstance().mPools.end(); i_pool++)
55  delete *i_pool;
56  }
57 
61 
63  MemoryPool& operator=(MemoryPool const& rOther) = delete;
64 
68 
70  //template <typename TPoolType = FixedSizeMemoryPool>
71  //static FixedSizeMemoryPool* AddPool(std::string const& PoolName, TPoolType* pHeapAllocatedPool) {
72  // KRATOS_ERROR_IF(HasPool(PoolName)) << "A duplicated memory pool found! The pool \"" << PoolName << "\" is already added." << std::endl;
73  // GetInstance().mPools[PoolName] = pHeapAllocatedPool;
74  // return pHeapAllocatedPool;
75  //}
76 
77  static void* Allocate(std::size_t ObjectSizeInBytes) {
78  return GetPoolWithBlockSize(ObjectSizeInBytes)->Allocate();
79  }
80 
81  static void Deallocate(void* pPointrerToRelease, std::size_t ObjectSizeInBytes) {
82  GetPoolWithBlockSize(ObjectSizeInBytes)->Deallocate(pPointrerToRelease);
83  }
84 
85 
89 
90  static MemoryPool& GetInstance() {
91  static MemoryPool instance;
92  return instance;
93  }
94 
95  static std::size_t GetNumberOfPools() {
96  return GetInstance().mPools.size();
97  }
98 
99  static FixedSizeMemoryPool* GetPoolWithBlockSize(std::size_t BlockSize) {
100  PoolsContainerType& r_pools = GetInstance().mPools;
101 
102  // I would avoid it by defining a max block size, but before that I should profile to see if it really slows down. Pooyan.
103  if (r_pools.size() <= BlockSize) { // This check is extra but is to avoid critical each time
104 #pragma omp critical
105  {
106  if (r_pools.size() <= BlockSize) // checking again to be sure some other thread doesn't change it meanwhile
107  r_pools.resize(BlockSize + 1, nullptr);
108  }
109  }
110 
111  if (r_pools[BlockSize] == nullptr) { // This check is extra but is to avoid critical each time
112 #pragma omp critical
113  {
114  if (r_pools[BlockSize] == nullptr) // checking again to be sure some other thread doesn't change it meanwhile
115  r_pools[BlockSize] = new FixedSizeMemoryPool(BlockSize);
116  }
117  }
118  return r_pools[BlockSize];
119  }
120 
124 
125  //static bool HasPool(std::string const& PoolName) {
126  // return (GetInstance().mPools.find(PoolName) != GetInstance().mPools.end());
127  //}
128 
129  static std::size_t MemoryUsed() {
130  std::size_t result = sizeof(MemoryPool);
131  for (auto i_pool = GetInstance().mPools.begin(); i_pool != GetInstance().mPools.end(); i_pool++)
132  if(*i_pool != nullptr)
133  result += (*i_pool)->MemoryUsed();
134  return result;
135  }
136 
137  static std::size_t MemoryOverhead() {
138  std::size_t result = sizeof(MemoryPool);
139  for (auto i_pool = GetInstance().mPools.begin(); i_pool != GetInstance().mPools.end(); i_pool++)
140  if (*i_pool != nullptr)
141  result += (*i_pool)->MemoryOverhead();
142  return result;
143  }
144 
148 
150  static std::string Info() {
151  std::stringstream buffer("MemoryPool");
152  std::size_t memory_used = MemoryUsed();
153  std::size_t memory_overhead = MemoryOverhead();
154  double overhead_percentage = memory_overhead;
155  if (memory_overhead < memory_used)
156  overhead_percentage = static_cast<double>(memory_overhead)/(memory_used - memory_overhead);
157  overhead_percentage *= 100.00;
158 
159  buffer << "Total memory usage: "
160  << SizeInBytesToString(MemoryUsed()) << " bytes and memory overhead "
161  << SizeInBytesToString(MemoryOverhead()) << "(" << overhead_percentage << "%)" << std::endl;
162 
163  return buffer.str();
164  }
165 
166 
168 
169 
170  private:
171 
174 
176  MemoryPool(){}
177 
181 
182  PoolsContainerType mPools;
183 
187 
188  static std::string SizeInBytesToString(std::size_t Bytes) {
189  std::stringstream buffer;
190  double result = Bytes;
191  constexpr int units_size = 5;
192  constexpr char units[units_size+1] = { ' ', 'k','M','G','T', 'T'};
193  int i = 0;
194  for (; i < units_size; i++)
195  if (result > 1024)
196  {
197  result /= 1024;
198  }
199  else
200  break;
201  buffer << result << units[i];
202 
203  return buffer.str();
204  }
205 
207 
208  }; // Class MemoryPool
209 
213 
215 
217 
218 } // namespace Kratos.
219 
220 #endif // KRATOS_MEMORY_POOL_H_INCLUDED defined
FixedSizeMemoryPool is the multi-thread manager of Kratos memory management.
Definition: fixed_size_memory_pool.h:40
void * Allocate()
This function does not throw and returns zero if cannot allocate.
Definition: fixed_size_memory_pool.h:86
void Deallocate(void *pPointrerToRelease)
Definition: fixed_size_memory_pool.h:91
MemoryPool is the smallest building block of Kratos memory management.
Definition: memory_pool.h:38
static FixedSizeMemoryPool * GetPoolWithBlockSize(std::size_t BlockSize)
Definition: memory_pool.h:99
virtual ~MemoryPool()
Destructor.
Definition: memory_pool.h:53
std::vector< FixedSizeMemoryPool * > PoolsContainerType
Definition: memory_pool.h:43
static std::size_t GetNumberOfPools()
Definition: memory_pool.h:95
static std::size_t MemoryOverhead()
Definition: memory_pool.h:137
MemoryPool & operator=(MemoryPool const &rOther)=delete
Assignment operator is deleted.
MemoryPool(MemoryPool const &rOther)=delete
Copy constructor is deleted.
static void Deallocate(void *pPointrerToRelease, std::size_t ObjectSizeInBytes)
Definition: memory_pool.h:81
static std::string Info()
Turn back information as a string.
Definition: memory_pool.h:150
static MemoryPool & GetInstance()
Definition: memory_pool.h:90
static std::size_t MemoryUsed()
Definition: memory_pool.h:129
static void * Allocate(std::size_t ObjectSizeInBytes)
Definition: memory_pool.h:77
REF: G. R. Cowper, GAUSSIAN QUADRATURE FORMULAS FOR TRIANGLES.
Definition: mesh_condition.cpp:21
integer i
Definition: TensorModule.f:17