Commit 96146de7 authored by Martin Bauer's avatar Martin Bauer
Browse files

More general FieldAllocator

- allows the user to define arbitrary shape/stride based layouts
parent 617a3d25
......@@ -272,37 +272,15 @@ namespace field {
xSize_ = _xSize;
ySize_ = _ySize;
zSize_ = _zSize;
xAllocSize_ = yAllocSize_ = zAllocSize_ = fAllocSize_ = 0; // is set in alloc->allocate()
layout_ = l;
WALBERLA_ASSERT(layout_ == zyxf || layout_ == fzyx);
if (layout_ == fzyx ) {
values_ = allocator_->allocate(fSize_, zSize_, ySize_, xSize_, zAllocSize_, yAllocSize_, xAllocSize_);
fAllocSize_ = fSize_;
WALBERLA_CHECK_LESS_EQUAL( fSize_ * xAllocSize_ * yAllocSize_ * zAllocSize_ + xSize_ + ySize_ * xAllocSize_ + zSize_ * xAllocSize_ * yAllocSize_,
std::numeric_limits< cell_idx_t >::max(),
"The data type 'cell_idx_t' is too small for your field size! Your field is too large.\nYou may have to set 'cell_idx_t' to an 'int64_t'." );
ffact_ = cell_idx_c(xAllocSize_ * yAllocSize_ * zAllocSize_);
zfact_ = cell_idx_c(xAllocSize_ * yAllocSize_);
yfact_ = cell_idx_c(xAllocSize_);
xfact_ = 1;
} else {
values_ = allocator_->allocate(zSize_, ySize_, xSize_, fSize_, yAllocSize_, xAllocSize_, fAllocSize_);
zAllocSize_ = zSize_;
WALBERLA_CHECK_LESS_EQUAL( fSize_ + xSize_ * fAllocSize_ + ySize_ * fAllocSize_ * xAllocSize_ + zSize_ * fAllocSize_ * xAllocSize_ * yAllocSize_,
std::numeric_limits< cell_idx_t >::max(),
"The data type 'cell_idx_t' is too small for your field size! Your field is too large.\nYou may have to set 'cell_idx_t' to an 'int64_t'." );
zfact_ = cell_idx_c(fAllocSize_ * xAllocSize_ * yAllocSize_);
yfact_ = cell_idx_c(fAllocSize_ * xAllocSize_);
xfact_ = cell_idx_c(fAllocSize_);
ffact_ = 1;
}
xAllocSize_ = yAllocSize_ = zAllocSize_ = fAllocSize_ = 0; // is set in alloc->allocate()
values_ = allocator_->allocate(layout_, xSize_, ySize_, zSize_, fSize_,
xAllocSize_, yAllocSize_, zAllocSize_, fAllocSize_,
xfact_, yfact_, zfact_, ffact_);
WALBERLA_ASSERT(xAllocSize_ >= xSize_);
WALBERLA_ASSERT(yAllocSize_ >= ySize_);
......
......@@ -24,7 +24,9 @@
#include "AlignedMalloc.h"
#include "core/debug/Debug.h"
#include "core/debug/CheckFunctions.h"
#include "field/CMakeDefs.h"
#include "field/Layout.h"
#include <map>
#include <new>
......@@ -50,6 +52,40 @@ namespace field {
virtual ~FieldAllocator() = default;
virtual T * allocate(const Layout & layout,
uint_t xSize, uint_t ySize, uint_t zSize, uint_t fSize,
uint_t & xAllocSize, uint_t & yAllocSize,uint_t & zAllocSize,uint_t & fAllocSize,
cell_idx_t & xStride, cell_idx_t & yStride, cell_idx_t & zStride, cell_idx_t & fStride)
{
T * ptr;
if (layout == fzyx ) {
ptr = allocateField(fSize, zSize, ySize, xSize, fAllocSize, zAllocSize, yAllocSize, xAllocSize);
WALBERLA_CHECK_LESS_EQUAL( fSize * xAllocSize * yAllocSize * zAllocSize + xSize + ySize * xAllocSize + zSize * xAllocSize * yAllocSize,
std::numeric_limits< cell_idx_t >::max(),
"The data type 'cell_idx_t' is too small for your field size! Your field is too large.\nYou may have to set 'cell_idx_t' to an 'int64t'." );
fStride = cell_idx_c(xAllocSize * yAllocSize * zAllocSize);
zStride = cell_idx_c(xAllocSize * yAllocSize);
yStride = cell_idx_c(xAllocSize);
xStride = 1;
} else {
ptr = allocateField(zSize, ySize, xSize, fSize, zAllocSize, yAllocSize, xAllocSize, fAllocSize);
WALBERLA_CHECK_LESS_EQUAL( fSize + xSize * fAllocSize + ySize * fAllocSize * xAllocSize + zSize * fAllocSize * xAllocSize * yAllocSize,
std::numeric_limits< cell_idx_t >::max(),
"The data type 'cell_idx_t' is too small for your field size! Your field is too large.\nYou may have to set 'cell_idx_t' to an 'int64t'." );
zStride = cell_idx_c(fAllocSize * xAllocSize * yAllocSize);
yStride = cell_idx_c(fAllocSize * xAllocSize);
xStride = cell_idx_c(fAllocSize);
fStride = 1;
}
return ptr;
}
/**
* \brief Allocate memory for a field of given sizes and initializes reference counter with one
*
......@@ -61,10 +97,10 @@ namespace field {
* allocSize can be bigger than size if alignment >0. There is no allocSize0,
* since extra allocations (padding) for the outer coordinate do not make sense.
*/
T * allocate ( uint_t size0, uint_t size1, uint_t size2, uint_t size3,
uint_t & allocSize1, uint_t & allocSize2, uint_t & allocSize3)
T * allocateField ( uint_t size0, uint_t size1, uint_t size2, uint_t size3,
uint_t & allocSize0, uint_t & allocSize1, uint_t & allocSize2, uint_t & allocSize3)
{
T * mem = allocateMemory(size0,size1,size2,size3,allocSize1,allocSize2,allocSize3);
T * mem = allocateMemory( size0, size1, size2, size3, allocSize0, allocSize1, allocSize2, allocSize3 );
#ifdef WALBERLA_THREAD_SAFE_FIELD_ALLOCATION
#ifdef _OPENMP
#pragma omp critical( walberla_field_allocator_refcount )
......@@ -182,7 +218,7 @@ namespace field {
* \brief Same as allocated, without handling of reference counter
*/
virtual T * allocateMemory ( uint_t size0, uint_t size1, uint_t size2, uint_t size3,
uint_t & allocSize1, uint_t & allocSize2, uint_t & allocSize3 ) = 0;
uint_t & allocSize0, uint_t & allocSize1, uint_t & allocSize2, uint_t & allocSize3 ) = 0;
/**
......@@ -225,11 +261,12 @@ namespace field {
protected:
virtual T * allocateMemory ( uint_t size0, uint_t size1, uint_t size2, uint_t size3,
uint_t & allocSize1, uint_t & allocSize2, uint_t & allocSize3)
uint_t & allocSize0, uint_t & allocSize1, uint_t & allocSize2, uint_t & allocSize3)
{
allocSize1=size1;
allocSize2=size2;
allocSize3=size3;
allocSize0 = size0;
allocSize1 = size1;
allocSize2 = size2;
allocSize3 = size3;
uint_t lineLength = size3 * static_cast<uint_t>( sizeof(T) );
if(lineLength % alignment !=0 )
allocSize3 = ((lineLength + alignment) / alignment ) * (alignment / sizeof(T));
......@@ -315,11 +352,12 @@ namespace field {
{
public:
virtual T * allocateMemory ( uint_t size0, uint_t size1, uint_t size2, uint_t size3,
uint_t & allocSize1, uint_t & allocSize2, uint_t & allocSize3)
uint_t & allocSize0, uint_t & allocSize1, uint_t & allocSize2, uint_t & allocSize3 )
{
allocSize1=size1;
allocSize2=size2;
allocSize3=size3;
allocSize0 = size0;
allocSize1 = size1;
allocSize2 = size2;
allocSize3 = size3;
return new T[size0*size1*size2*size3];
}
......
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