15 #if !defined(KRATOS_THREAD_FIXED_SIZE_MEMORY_POOL_H_INCLUDED )
16 #define KRATOS_THREAD_FIXED_SIZE_MEMORY_POOL_H_INCLUDED
23 #include "concurrentqueue/concurrentqueue.h"
73 : mBlockSizeInBytes(BlockSizeInBytes)
75 , mThreadNumber(ThreadNumber)
78 , mNumberOfReleasedChunks(0)
79 , mpCurrentChunk(nullptr)
101 void* p_result =
nullptr;
102 if(mAvailablePointers.try_dequeue(p_result))
105 if(mpCurrentChunk ==
nullptr){
109 if (mpCurrentChunk->
IsFull())
110 if (!mAvailableChunks.try_dequeue(mpCurrentChunk))
117 p_result = mpCurrentChunk->
Allocate();
126 mAvailablePointers.enqueue(pPointerToRelease);
131 if (mpCurrentChunk->
Has(pPointerToRelease)) {
132 DeallocateFromAvailableChunk(pPointerToRelease, mpCurrentChunk);
136 for (
auto i_chunk = mChunks.begin(); i_chunk != mChunks.end(); i_chunk++)
138 if (i_chunk->Has(pPointerToRelease)) {
139 if (i_chunk->IsFull())
140 DeallocateFromFullChunk(pPointerToRelease, &(*i_chunk));
142 DeallocateFromAvailableChunk(pPointerToRelease, &(*i_chunk));
154 mNumberOfReleasedChunks += mChunks.size();
156 while(mAvailableChunks.try_dequeue(p_dummy));
165 for (
auto i_chunk = mChunks.begin(); i_chunk != mChunks.end(); i_chunk++)
166 memory_used += i_chunk->MemoryUsed();
171 std::size_t memory_overhead =
sizeof(
ThreadFixedSizeMemoryPool) + (mAvailableChunks.size_approx() *
sizeof(std::size_t));
172 for (
auto i_chunk = mChunks.begin(); i_chunk != mChunks.end(); i_chunk++)
173 memory_overhead += i_chunk->MemoryOverhead();
174 return memory_overhead;
191 return mChunks.size();
195 return mAvailableChunks.size_approx();
199 return mNumberOfReleasedChunks;
207 return !(mAvailableChunks.size_approx() == 0);
217 return "ThreadFixedSizeMemoryPool";
229 double overhead_percentage = memory_overhead;
230 if (memory_overhead < memory_used)
231 overhead_percentage =
static_cast<double>(memory_overhead)/(memory_used - memory_overhead);
232 overhead_percentage *= 100.00;
235 << SizeInBytesToString(
ChunkSize()) <<
" bytes each. Total memory usage: "
236 << SizeInBytesToString(
MemoryUsed()) <<
" bytes and memory overhead "
237 << SizeInBytesToString(
MemoryOverhead()) <<
"(" << overhead_percentage <<
"%)" << std::endl;
247 std::size_t mBlockSizeInBytes;
249 std::size_t mThreadNumber;
250 std::list<Chunk> mChunks;
251 moodycamel::ConcurrentQueue<Chunk*> mAvailableChunks;
252 std::size_t mNumberOfReleasedChunks;
253 moodycamel::ConcurrentQueue<void*> mAvailablePointers;
254 Chunk* mpCurrentChunk=
nullptr;
267 mChunks.emplace_back(mBlockSizeInBytes, mChunkSize);
268 mpCurrentChunk = &(mChunks.back());
273 void DeallocateFromAvailableChunk(
void* pPointrerToRelease, Chunk* pChunk) {
274 pChunk->Deallocate(pPointrerToRelease);
275 if (pChunk->IsEmpty())
277 ReleaseChunk(pChunk);
280 void DeallocateFromFullChunk(
void* pPointrerToRelease, Chunk* pChunk) {
282 mAvailableChunks.enqueue(pChunk);
283 pChunk->Deallocate(pPointrerToRelease);
284 if (pChunk->IsEmpty())
286 ReleaseChunk(pChunk);
289 void ReleaseChunk(Chunk* pChunk) {
292 mNumberOfReleasedChunks++;
299 std::string SizeInBytesToString(std::size_t Bytes)
const {
300 std::stringstream buffer;
301 double result = Bytes;
302 constexpr
int units_size = 5;
303 constexpr
char units[units_size] = {
' ',
'k',
'M',
'G',
'T' };
305 for (;
i < units_size;
i++)
312 buffer << result << units[
i];
332 rOStream << std::endl;
Chunk is the smallest building block of Kratos memory management.
Definition: chunk.h:45
bool IsFull()
Definition: chunk.h:230
void * Allocate()
This function does not throw and returns zero if cannot allocate.
Definition: chunk.h:110
bool IsReleased() const
Definition: chunk.h:238
std::size_t SizeType
Definition: chunk.h:56
bool Has(const void *pThePointer) const
Definition: chunk.h:222
void Initialize()
Definition: chunk.h:97
static int ThisThread()
Wrapper for omp_get_thread_num().
Definition: openmp_utils.h:108
ThreadFixedSizeMemoryPool holds chunks belong to a certain thread and operate over them.
Definition: thread_fixed_size_memory_pool.h:45
std::size_t GetNumberOfReleasedChunks() const
Definition: thread_fixed_size_memory_pool.h:198
ThreadFixedSizeMemoryPool(ThreadFixedSizeMemoryPool &&rOther)=default
Move constructor to be used in STL containers.
void PrintData(std::ostream &rOStream) const
Print object's data.
Definition: thread_fixed_size_memory_pool.h:226
ThreadFixedSizeMemoryPool(std::size_t BlockSizeInBytes, SizeType ChunkSize, std::size_t ThreadNumber)
The constructor to be called.
Definition: thread_fixed_size_memory_pool.h:72
void Release()
Definition: thread_fixed_size_memory_pool.h:152
void lock()
Definition: thread_fixed_size_memory_pool.h:177
std::size_t MemoryUsed() const
Definition: thread_fixed_size_memory_pool.h:163
ThreadFixedSizeMemoryPool(ThreadFixedSizeMemoryPool const &rOther)=delete
Copy constructor is deleted.
std::size_t GetNumberOfChunks() const
Definition: thread_fixed_size_memory_pool.h:190
bool HasAvailableChunk()
Definition: thread_fixed_size_memory_pool.h:206
virtual ~ThreadFixedSizeMemoryPool()
Destructor.
Definition: thread_fixed_size_memory_pool.h:84
std::string Info() const
Turn back information as a string.
Definition: thread_fixed_size_memory_pool.h:216
static constexpr SizeType MaximumEmptyChunksToKeep
Definition: thread_fixed_size_memory_pool.h:56
void PrintInfo(std::ostream &rOStream) const
Print information about this object.
Definition: thread_fixed_size_memory_pool.h:221
bool Deallocate(void *pPointerToRelease)
Definition: thread_fixed_size_memory_pool.h:124
Chunk::SizeType SizeType
Definition: thread_fixed_size_memory_pool.h:50
std::size_t GetNumberOfAvailableChunks() const
Definition: thread_fixed_size_memory_pool.h:194
static constexpr SizeType MaximumPointersToKeep
Definition: thread_fixed_size_memory_pool.h:57
SizeType ChunkSize() const
Definition: thread_fixed_size_memory_pool.h:159
std::size_t MemoryOverhead() const
Definition: thread_fixed_size_memory_pool.h:170
ThreadFixedSizeMemoryPool()=delete
Default constructor is deleted.
void * Allocate()
This function does not throw and returns zero if cannot allocate.
Definition: thread_fixed_size_memory_pool.h:100
ThreadFixedSizeMemoryPool & operator=(ThreadFixedSizeMemoryPool const &rOther)=delete
Assignment operator is deleted.
void unlock()
Definition: thread_fixed_size_memory_pool.h:182
std::list< Chunk * > ChunkList
Definition: thread_fixed_size_memory_pool.h:52
#define KRATOS_DEBUG_CHECK_EQUAL(a, b)
Definition: checks.h:217
#define KRATOS_ERROR
Definition: exception.h:161
#define KRATOS_CHECK_IS_FALSE(IsFalse)
Definition: checks.h:32
#define KRATOS_DEBUG_CHECK_NOT_EQUAL(a, b)
Definition: checks.h:218
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
std::atomic_flag ThreadFixedSizeMemoryPoolLock
Definition: thread_fixed_size_memory_pool.h:29
integer i
Definition: TensorModule.f:17