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.
fixed_size_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_FIXED_SIZE_MEMORY_POOL_H_INCLUDED )
16 #define KRATOS_FIXED_SIZE_MEMORY_POOL_H_INCLUDED
17 
18 #include <vector>
19 #include <list>
20 
22 
23 namespace Kratos
24 {
27 
30 
32 
40  {
41  public:
44 
46 
48 
49  static constexpr SizeType DefaultChunkSize = 1024*1024; // 1M byte
50 
53 
55  FixedSizeMemoryPool() = delete;
56 
58  FixedSizeMemoryPool(FixedSizeMemoryPool const& rOther) = delete;
59 
61  FixedSizeMemoryPool(std::size_t BlockSizeInBytes, SizeType ChunkSize = DefaultChunkSize)
62  : LockObject()
63  , mChunkSize(ChunkSize)
64  {
65  for (int i_thread = 0; i_thread < OpenMPUtils::GetCurrentNumberOfThreads(); i_thread++)
66  mThreadsPool.emplace_back(BlockSizeInBytes, ChunkSize, i_thread);
67  }
68 
71 
72  }
73 
77 
80 
84 
86  void* Allocate() {
87  void* p_result = mThreadsPool[OpenMPUtils::ThisThread()].Allocate();
88  return p_result;
89  }
90 
91  void Deallocate(void* pPointrerToRelease) {
92 
93  if (mThreadsPool[OpenMPUtils::ThisThread()].Deallocate(pPointrerToRelease))
94  {
95  return;
96  }
97 
98  for (int i_thread = 0; i_thread < OpenMPUtils::GetCurrentNumberOfThreads(); i_thread++)
99  if (i_thread != OpenMPUtils::ThisThread())
100  if (mThreadsPool[i_thread].Deallocate(pPointrerToRelease)) {
101  return;
102  }
103 
104  KRATOS_ERROR << "The Pointer with address " << pPointrerToRelease << " was not found in this pool" << std::endl;
105  }
106 
107  std::size_t MemoryUsed() const {
108  std::size_t memory_used = sizeof(FixedSizeMemoryPool);
109  for (int i_thread = 0; i_thread < OpenMPUtils::GetCurrentNumberOfThreads(); i_thread++)
110  memory_used += mThreadsPool[i_thread].MemoryUsed();
111  return memory_used;
112  }
113 
114  std::size_t MemoryOverhead() const {
115  std::size_t memory_overhead = sizeof(FixedSizeMemoryPool);
116  for (int i_thread = 0; i_thread < OpenMPUtils::GetCurrentNumberOfThreads(); i_thread++)
117  memory_overhead += mThreadsPool[i_thread].MemoryOverhead();
118  return memory_overhead;
119  }
120 
121  std::size_t GetNumberOfAllocatedChunks() const {
122  std::size_t number_of_allocated_chunks = 0;
123  for (int i_thread = 0; i_thread < OpenMPUtils::GetCurrentNumberOfThreads(); i_thread++)
124  number_of_allocated_chunks += mThreadsPool[i_thread].GetNumberOfChunks() - mThreadsPool[i_thread].GetNumberOfReleasedChunks();
125  return number_of_allocated_chunks;
126  }
127 
131 
132  SizeType ChunkSize() const {
133  return mChunkSize;
134  }
135 
139 
143 
145  std::string Info() const {
146  return "FixedSizeMemoryPool";
147  }
148 
150  void PrintInfo(std::ostream& rOStream) const {
151  rOStream << Info();
152  }
153 
155  void PrintData(std::ostream& rOStream) const {
156  std::size_t memory_used = MemoryUsed();
157  std::size_t memory_overhead = MemoryOverhead();
158  double overhead_percentage = memory_overhead;
159  if (memory_overhead < memory_used)
160  overhead_percentage = static_cast<double>(memory_overhead)/(memory_used - memory_overhead);
161  overhead_percentage *= 100.00;
162 
163  rOStream << GetNumberOfAllocatedChunks() << " Chunks of "
164  << SizeInBytesToString(ChunkSize()) << " bytes each. Total memory usage: "
165  << SizeInBytesToString(MemoryUsed()) << " bytes and memory overhead "
166  << SizeInBytesToString(MemoryOverhead()) << "(" << overhead_percentage << "%)" << std::endl;
167  }
168 
170 
171 
172  private:
175 
176  SizeType mChunkSize;
177  std::vector<ThreadFixedSizeMemoryPool> mThreadsPool;
178 
182 
183  std::string SizeInBytesToString(std::size_t Bytes) const {
184  std::stringstream buffer;
185  double result = Bytes;
186  constexpr int units_size = 5;
187  constexpr char units[units_size] = { ' ', 'k','M','G','T' };
188  int i = 0;
189  for (; i < units_size; i++)
190  if (result > 1024)
191  {
192  result /= 1024;
193  }
194  else
195  break;
196  buffer << result << units[i];
197 
198  return buffer.str();
199  }
200 
201 
203 
204  }; // Class FixedSizeMemoryPool
205 
209 
210 
212  inline std::ostream& operator << (std::ostream& rOStream,
213  const FixedSizeMemoryPool& rThis)
214  {
215  rThis.PrintInfo(rOStream);
216  rOStream << std::endl;
217  rThis.PrintData(rOStream);
218 
219  return rOStream;
220  }
222 
224 
225 } // namespace Kratos.
226 
227 #endif // KRATOS_FIXED_SIZE_MEMORY_POOL_H_INCLUDED defined
std::size_t SizeType
Definition: chunk.h:56
FixedSizeMemoryPool is the multi-thread manager of Kratos memory management.
Definition: fixed_size_memory_pool.h:40
std::size_t GetNumberOfAllocatedChunks() const
Definition: fixed_size_memory_pool.h:121
void PrintInfo(std::ostream &rOStream) const
Print information about this object.
Definition: fixed_size_memory_pool.h:150
void * Allocate()
This function does not throw and returns zero if cannot allocate.
Definition: fixed_size_memory_pool.h:86
FixedSizeMemoryPool()=delete
Default constructor is deleted.
void Deallocate(void *pPointrerToRelease)
Definition: fixed_size_memory_pool.h:91
std::string Info() const
Turn back information as a string.
Definition: fixed_size_memory_pool.h:145
FixedSizeMemoryPool(FixedSizeMemoryPool const &rOther)=delete
Copy constructor is deleted.
FixedSizeMemoryPool & operator=(FixedSizeMemoryPool const &rOther)=delete
Assignment operator is deleted.
Chunk::SizeType SizeType
Definition: fixed_size_memory_pool.h:45
FixedSizeMemoryPool(std::size_t BlockSizeInBytes, SizeType ChunkSize=DefaultChunkSize)
The constructor to be called.
Definition: fixed_size_memory_pool.h:61
std::size_t MemoryUsed() const
Definition: fixed_size_memory_pool.h:107
virtual ~FixedSizeMemoryPool()
Destructor.
Definition: fixed_size_memory_pool.h:70
void PrintData(std::ostream &rOStream) const
Print object's data.
Definition: fixed_size_memory_pool.h:155
SizeType ChunkSize() const
Definition: fixed_size_memory_pool.h:132
std::size_t MemoryOverhead() const
Definition: fixed_size_memory_pool.h:114
static constexpr SizeType DefaultChunkSize
Definition: fixed_size_memory_pool.h:49
This class defines and stores a lock and gives an interface to it.
Definition: lock_object.h:42
static int GetCurrentNumberOfThreads()
Wrapper for omp_get_num_threads().
Definition: openmp_utils.h:73
static int ThisThread()
Wrapper for omp_get_thread_num().
Definition: openmp_utils.h:108
#define KRATOS_ERROR
Definition: exception.h:161
REF: G. R. Cowper, GAUSSIAN QUADRATURE FORMULAS FOR TRIANGLES.
Definition: mesh_condition.cpp:21
std::size_t SizeType
The definition of the size type.
Definition: mortar_classes.h:43
std::ostream & operator<<(std::ostream &rOStream, const LinearMasterSlaveConstraint &rThis)
output stream function
Definition: linear_master_slave_constraint.h:432
integer i
Definition: TensorModule.f:17