27 #ifndef SCHNEK_GRID_GRIDSTORAGE_SINGLEARRAYALLOCATION_HPP_ 28 #define SCHNEK_GRID_GRIDSTORAGE_SINGLEARRAYALLOCATION_HPP_ 30 #include "../array.hpp" 57 template <
typename T,
typename SizeInfo>
61 typedef std::function<void(const SizeInfo&)> UpdaterType;
63 std::map<void*, UpdaterType> updaters;
71 void addUpdater(
void* key,
const UpdaterType &updater) {
72 updaters[key] = updater;
75 void removeUpdater(
void* key) {
79 void update(
const SizeInfo& sizeInfo) {
80 for (
auto& updater: updaters) {
81 updater.second(sizeInfo);
95 template <
typename T,
size_t rank>
110 typedef std::function<void()> UpdaterType;
113 std::shared_ptr<internal::SingleArrayAllocationData<T, SizeInfo> >
data;
148 void resizeImpl(
const IndexType &lo,
const IndexType &hi);
155 void onUpdate(
const UpdaterType &updater);
163 void updateSizeInfo(
const SizeInfo& sizeInfo);
169 void newData(
const IndexType &lo,
const IndexType &hi);
184 template <
typename T,
size_t rank>
204 typedef std::function<void()> UpdaterType;
207 std::shared_ptr<internal::SingleArrayAllocationData<T, SizeInfo> >
data;
252 void resizeImpl(
const IndexType &lo,
const IndexType &hi);
259 void onUpdate(
const UpdaterType &updater);
269 void updateSizeInfo(
const SizeInfo& sizeInfo);
275 void newData(
size_t size);
282 template <
typename T,
size_t rank>
284 : data(new internal::SingleArrayAllocationData<T,
SizeInfo>()), size(0)
286 this->
data->addUpdater(
this, [
this](
const SizeInfo& sizeInfo) { this->updateSizeInfo(sizeInfo); });
289 template <
typename T,
size_t rank>
293 this->
data->addUpdater(
this, [
this](
const SizeInfo& sizeInfo) { this->updateSizeInfo(sizeInfo); });
296 template <
typename T,
size_t rank>
303 this->
data->addUpdater(
this, [
this](
const SizeInfo& sizeInfo) { this->updateSizeInfo(sizeInfo); });
307 template <
typename T,
size_t rank>
310 this->
data->removeUpdater(
this);
313 template <
typename T,
size_t rank>
316 this->updater = updater;
319 template <
typename T,
size_t rank>
324 for (
size_t d = 0; d < rank; ++d)
326 dims[d] = sizeInfo.hi[d] - sizeInfo.lo[d] + 1;
335 template <
typename T,
size_t rank>
339 this->newData(lo, hi);
343 template <
typename T,
size_t rank>
354 template <
typename T,
size_t rank>
363 for (
size_t d = 0; d < rank; ++d)
365 dims[d] = hi[d] - lo[d] + 1;
375 template <
typename T,
size_t rank>
377 :
data(new internal::SingleArrayAllocationData<T,
SizeInfo>()),
384 this->
data->addUpdater(
this, [
this](
const SizeInfo& sizeInfo) { this->updateSizeInfo(sizeInfo); });
387 template <
typename T,
size_t rank>
393 bufSize(other.bufSize),
394 avgSize(other.avgSize),
395 avgVar(other.avgVar),
398 this->
data->addUpdater(
this, [
this](
const SizeInfo& sizeInfo) { this->updateSizeInfo(sizeInfo); });
401 template <
typename T,
size_t rank>
408 this->
data->addUpdater(
this, [
this](
const SizeInfo& sizeInfo) { this->updateSizeInfo(sizeInfo); });
413 template <
typename T,
size_t rank>
416 this->
data->removeUpdater(
this);
419 template <
typename T,
size_t rank>
422 this->updater = updater;
425 template <
typename T,
size_t rank>
430 for (
size_t d = 0; d < rank; ++d)
432 dims[d] = sizeInfo.hi[d] - sizeInfo.lo[d] + 1;
435 bufSize = sizeInfo.bufSize;
436 avgSize = sizeInfo.avgSize;
437 avgVar = sizeInfo.avgVar;
444 template <
typename T,
size_t rank>
450 for (
size_t d = 0; d < rank; d++)
452 dims[d] = hi[d] - lo[d] + 1;
456 avgSize = r * newSize + (1 - r) * avgSize;
457 ptrdiff_t diff = newSize - avgSize;
458 avgVar = r * diff * diff + (1 - r) * avgVar;
460 if ((newSize > bufSize) || (((newSize + 32.0 * sqrt(avgVar)) < bufSize) && (bufSize > 100)))
463 this->newData(newSize);
469 template <
typename T,
size_t rank>
472 SCHNEK_TRACE_LOG(5,
"Deleting pointer (" << (
void *)
data <<
"): size=" <<
size <<
" avgSize=" << avgSize <<
" avgVar=" << avgVar <<
" bufSize=" << bufSize);
482 template <
typename T,
size_t rank>
486 bufSize = newSize + (size_t)(4 * sqrt(avgVar));
491 data->ptr =
new T[bufSize];
495 #endif // SCHNEK_GRID_GRIDSTORAGE_SINGLEARRAYALLOCATION_HPP_ SingleArrayInstantAllocation()
Default constructor.
Definition: single-array-allocation.hpp:283
void resizeImpl(const IndexType &lo, const IndexType &hi)
resizes to grid with lower indices lo[0],...,lo[rank-1] and upper indices hi[0],...,hi[rank-1]
Definition: single-array-allocation.hpp:336
Array< size_t, rank > IndexType
The grid index type.
Definition: single-array-allocation.hpp:189
void onUpdate(const UpdaterType &updater)
Add an updater to the data.
Definition: single-array-allocation.hpp:314
Allocate a single array for multidimensional grids in C ordering.
Definition: single-array-allocation.hpp:96
SingleArrayInstantAllocation< T, rank > & operator=(const SingleArrayInstantAllocation< T, rank > &)
Assignment operator.
Definition: single-array-allocation.hpp:297
size_t size
The length of the allocated array.
Definition: single-array-allocation.hpp:116
void resizeImpl(const IndexType &lo, const IndexType &hi)
resizes to grid with lower indices lo[0],...,lo[rank-1] and upper indices hi[0],...,hi[rank-1]
Definition: single-array-allocation.hpp:445
std::shared_ptr< internal::SingleArrayAllocationData< T, SizeInfo > > data
The pointer to the data.
Definition: single-array-allocation.hpp:113
~SingleArrayInstantAllocation()
destructor
Definition: single-array-allocation.hpp:308
std::shared_ptr< internal::SingleArrayAllocationData< T, SizeInfo > > data
The pointer to the data.
Definition: single-array-allocation.hpp:207
SingleArrayLazyAllocation()
Default constructor.
Definition: single-array-allocation.hpp:376
size_t size
The length of the array.
Definition: single-array-allocation.hpp:210
RangeType range
The lowest and highest coordinates in the grid (inclusive)
Definition: single-array-allocation.hpp:213
Range< int, rank > RangeType
The grid range type.
Definition: single-array-allocation.hpp:103
Array< int, rank > IndexType
The grid index type.
Definition: single-array-allocation.hpp:100
IndexType dims
The dimensions of the grid dims = high - low + 1
Definition: single-array-allocation.hpp:216
Range< size_t, rank > RangeType
The grid range type.
Definition: single-array-allocation.hpp:192
#define SCHNEK_TRACE_LOG(i, x)
Definition: logger.hpp:54
IndexType dims
The dimensions of the grid dims = high - low + 1
Definition: single-array-allocation.hpp:122
Definition: single-array-allocation.hpp:105
~SingleArrayLazyAllocation()
destructor
Definition: single-array-allocation.hpp:414
RangeType range
The lowest and highest coordinates in the grid (inclusive)
Definition: single-array-allocation.hpp:119
The data for a single array allocation.
Definition: single-array-allocation.hpp:58
SingleArrayLazyAllocation< T, rank > & operator=(const SingleArrayLazyAllocation< T, rank > &)
Assignment operator.
Definition: single-array-allocation.hpp:402
Definition: single-array-allocation.hpp:194
Allocate a single array for multidimensional grids in C ordering.
Definition: single-array-allocation.hpp:185
void onUpdate(const UpdaterType &updater)
Add an updater to the data.
Definition: single-array-allocation.hpp:420