Commit 0248a0fb authored by Michael Kuron's avatar Michael Kuron 🔬

Restore per-type refcount maps in FieldAllocator

parent 005a9cb5
Pipeline #22576 passed with stages
in 254 minutes and 2 seconds
......@@ -25,6 +25,7 @@
#include "AlignedMalloc.h"
#include "core/debug/Debug.h"
#include "field/CMakeDefs.h"
#include "core/VectorTrait.h"
#include <map>
#include <new>
......@@ -51,9 +52,10 @@ namespace field {
*/
//*******************************************************************************************************************
template<typename T>
class FieldAllocator : FieldAllocatorBase<void>
class FieldAllocator : FieldAllocatorBase<typename std::conditional<VectorTrait<T>::F_SIZE!=0, typename VectorTrait<T>::OutputType, T>::type>
{
public:
using BaseType = typename std::conditional<VectorTrait<T>::F_SIZE!=0, typename VectorTrait<T>::OutputType, T>::type;
virtual ~FieldAllocator() = default;
......@@ -72,14 +74,15 @@ namespace field {
uint_t & allocSize1, uint_t & allocSize2, uint_t & allocSize3)
{
T * mem = allocateMemory(size0,size1,size2,size3,allocSize1,allocSize2,allocSize3);
BaseType * bmem = reinterpret_cast<BaseType *>(mem);
#ifdef WALBERLA_THREAD_SAFE_FIELD_ALLOCATION
#ifdef _OPENMP
#pragma omp critical( walberla_field_allocator_refcount )
#endif
#endif
{
WALBERLA_ASSERT( referenceCounts_.find(mem) == referenceCounts_.end() || referenceCounts_[mem] == 0 );
referenceCounts_[mem] = 1;
WALBERLA_ASSERT( referenceCounts_.find(bmem) == referenceCounts_.end() || referenceCounts_[bmem] == 0 );
referenceCounts_[bmem] = 1;
}
return mem;
}
......@@ -96,6 +99,7 @@ namespace field {
T * allocate ( uint_t allocSize )
{
T * mem = allocateMemory( allocSize );
BaseType * bmem = reinterpret_cast<BaseType *>(mem);
#ifdef WALBERLA_THREAD_SAFE_FIELD_ALLOCATION
#ifdef _OPENMP
......@@ -103,8 +107,8 @@ namespace field {
#endif
#endif
{
WALBERLA_ASSERT( referenceCounts_.find(mem) == referenceCounts_.end() || referenceCounts_[mem] == 0 );
referenceCounts_[mem] = 1;
WALBERLA_ASSERT( referenceCounts_.find(bmem) == referenceCounts_.end() || referenceCounts_[bmem] == 0 );
referenceCounts_[bmem] = 1;
}
return mem;
}
......@@ -119,15 +123,17 @@ namespace field {
*/
void incrementReferenceCount( T * mem )
{
BaseType * bmem = reinterpret_cast<BaseType *>(mem);
#ifdef WALBERLA_THREAD_SAFE_FIELD_ALLOCATION
#ifdef _OPENMP
#pragma omp critical( walberla_field_allocator_refcount )
#endif
#endif
{
WALBERLA_ASSERT( referenceCounts_.find(mem) != referenceCounts_.end() );
WALBERLA_ASSERT_GREATER( referenceCounts_[mem], 0 );
referenceCounts_[mem]++;
WALBERLA_ASSERT( referenceCounts_.find(bmem) != referenceCounts_.end() );
WALBERLA_ASSERT_GREATER( referenceCounts_[bmem], 0 );
referenceCounts_[bmem]++;
}
}
......@@ -143,6 +149,7 @@ namespace field {
*/
bool decrementReferenceCount( T * mem )
{
BaseType * bmem = reinterpret_cast<BaseType *>(mem);
bool memoryFreed = false;
uint_t refCount = 0;
......@@ -153,10 +160,10 @@ namespace field {
#endif
#endif
{
WALBERLA_ASSERT( referenceCounts_.find(mem) != referenceCounts_.end() );
WALBERLA_ASSERT_GREATER( referenceCounts_[mem], 0 );
WALBERLA_ASSERT( referenceCounts_.find(bmem) != referenceCounts_.end() );
WALBERLA_ASSERT_GREATER( referenceCounts_[bmem], 0 );
refCount = --referenceCounts_[mem];
refCount = --referenceCounts_[bmem];
}
if( refCount == 0 ) {
......@@ -170,6 +177,7 @@ namespace field {
uint_t referenceCount ( T * mem ) const
{
BaseType * bmem = reinterpret_cast<BaseType *>(mem);
uint_t refCount = 0;
#ifdef WALBERLA_THREAD_SAFE_FIELD_ALLOCATION
#ifdef _OPENMP
......@@ -177,8 +185,8 @@ namespace field {
#endif
#endif
{
WALBERLA_ASSERT( referenceCounts_.find(mem) != referenceCounts_.end() );
refCount = referenceCounts_[mem];
WALBERLA_ASSERT( referenceCounts_.find(bmem) != referenceCounts_.end() );
refCount = referenceCounts_[bmem];
}
return refCount;
......@@ -203,6 +211,9 @@ namespace field {
* \param values Return value of allocate function()
*/
virtual void deallocate( T *& values ) = 0;
private:
using FieldAllocatorBase<BaseType>::referenceCounts_;
};
template<typename T>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment