41#include "../../traits/misc/enableIfHelpers.hpp"
43#include "dataInterface.hpp"
44#include "emptyData.hpp"
45#include "pointerStore.hpp"
46#include "position.hpp"
63 template<
typename T_Chunk,
typename T_NestedData = EmptyData,
typename T_Po
interInserter = Po
interStore<T_Chunk>>
75 template<
int selectedDepth>
77 typename std::conditional<selectedDepth == 0, TerminatingPointerStore<PointerInserter>,
83 std::vector<Chunk*> chunks;
84 std::vector<NestedPosition> positions;
97 : chunks(), positions(), curChunk(nullptr), curChunkIndex(0), chunkSize(chunkSize), nested(nullptr) {
103 : chunks(), positions(), curChunk(nullptr), curChunkIndex(0), chunkSize(chunkSize), nested(nullptr) {}
107 for (
size_t i = 0; i < chunks.size(); ++i) {
116 template<
typename... Data>
119 curChunk->pushData(data...);
123 template<
typename... Data>
126 curChunk->dataPointer(curChunk->getUsedSize(), pointers...);
132 curChunk->setUsedSize(curChunk->getUsedSize() + size);
140 if (chunkSize < curChunk->getUsedSize() + items) {
144 return curChunk->getUsedSize();
152 size_t noOfChunks = totalSize / chunkSize;
153 if (0 != totalSize % chunkSize) {
157 for (
size_t i = chunks.size(); i < noOfChunks; ++i) {
158 chunks.push_back(
new Chunk(chunkSize));
159 positions.push_back(nested->getPosition());
170 for (
size_t i = 1; i < chunks.size(); ++i) {
176 curChunk = chunks[0];
178 curChunk->setUsedSize(0);
189 for (
size_t i = pos.
chunk + 1; i <= curChunkIndex; i += 1) {
193 curChunk = chunks[pos.
chunk];
194 curChunk->setUsedSize(pos.
data);
195 curChunkIndex = pos.
chunk;
197 nested->resetTo(pos.
inner);
206 if (chunkRange == 0) {
210 chunks[start.
chunk]->erase(start.
data, chunks[start.
chunk]->getUsedSize());
216 chunks.erase(chunks.begin() + start.
chunk + 1, chunks.begin() + end.
chunk);
220 nested->erase(start.
inner, end.
inner, recursive);
230 for (
size_t i = 0; i < chunks.size(); ++i) {
231 size += chunks[i]->getUsedSize();
239 return Position(curChunkIndex, curChunk->getUsedSize(), nested->getPosition());
244 return curChunk->getUsedSize() - startPos;
249 return Position(0, 0, nested->getZeroPosition());
259 size_t numberOfChunks = chunks.size();
261 size_t entrySize = Chunk::EntrySize;
263 double memoryUsed = (double)dataEntries * (
double)entrySize;
264 double memoryAlloc = (double)numberOfChunks * (
double)chunkSize * (double)entrySize;
269 values.
addDoubleEntry(
"Memory allocated", memoryAlloc,
false,
true);
273 template<typename TargetPosition, typename = typename enable_if_not_same<TargetPosition, Position>::type>
275 return nested->template extractPosition<TargetPosition>(pos.
inner);
279 template<typename TargetPosition, typename = typename enable_if_same<TargetPosition, Position>::type>
288 codiAssert(v->getZeroPosition() == v->getPosition());
292 curChunk =
new Chunk(chunkSize);
293 chunks.push_back(curChunk);
294 positions.push_back(nested->getZeroPosition());
299 std::swap(chunks, other.chunks);
300 std::swap(positions, other.positions);
301 std::swap(curChunkIndex, other.curChunkIndex);
302 std::swap(chunkSize, other.chunkSize);
304 curChunk = chunks[curChunkIndex];
305 other.curChunk = other.chunks[other.curChunkIndex];
307 nested->swap(*other.nested);
314 template<
int selectedDepth = -1,
typename FunctionObject,
typename... Args>
319 size_t curDataPos = start.
data;
324 size_t curChunk = start.
chunk;
327 if (curChunk != end.
chunk) {
328 endInnerPos = positions[curChunk + 1];
329 endDataPos = chunks[curChunk]->getUsedSize();
331 endInnerPos = end.
inner;
332 endDataPos = end.
data;
335 pHandle.setPointers(0, chunks[curChunk]);
336 pHandle.template callNestedForward<selectedDepth - 1>(
338 nested, curDataPos, endDataPos,
340 curInnerPos, endInnerPos, function, std::forward<Args>(args)...);
345 if (curChunk != end.
chunk) {
347 curInnerPos = endInnerPos;
356 template<
int selectedDepth = -1,
typename FunctionObject,
typename... Args>
361 size_t curDataPos = start.
data;
366 size_t curChunk = start.
chunk;
369 if (curChunk != end.
chunk) {
370 endInnerPos = positions[curChunk];
373 endInnerPos = end.
inner;
374 endDataPos = end.
data;
377 pHandle.setPointers(0, chunks[curChunk]);
379 pHandle.template callNestedReverse<selectedDepth - 1>(
381 nested, curDataPos, endDataPos,
383 curInnerPos, endInnerPos, function, std::forward<Args>(args)...);
388 if (curChunk != end.
chunk) {
391 curInnerPos = endInnerPos;
392 curDataPos = chunks[curChunk]->getUsedSize();
400 template<
typename FunctionObject,
typename... Args>
402 for (
size_t chunkPos = 0; chunkPos < chunks.size(); chunkPos += 1) {
403 function(chunks[chunkPos], std::forward<Args>(args)...);
407 nested->forEachChunk(function, recursive, std::forward<Args>(args)...);
412 template<
typename FunctionObject,
typename... Args>
418 size_t dataStart = start.
data;
419 for (
size_t chunkPos = start.
chunk; chunkPos <= end.
chunk; chunkPos += 1) {
421 if (chunkPos != end.
chunk) {
422 dataEnd = chunks[chunkPos]->getUsedSize();
427 forEachChunkEntryForward(chunkPos, dataStart, dataEnd, function, std::forward<Args>(args)...);
434 template<
typename FunctionObject,
typename... Args>
440 size_t dataStart = start.
data;
441 size_t chunkPos = start.
chunk;
448 if (chunkPos != end.
chunk) {
454 forEachChunkEntryReverse(chunkPos, dataStart, dataEnd, function, std::forward<Args>(args)...);
456 if (chunkPos == end.
chunk) {
461 dataStart = chunks[chunkPos]->getUsedSize();
470 template<
typename FunctionObject,
typename... Args>
471 CODI_INLINE void forEachChunkEntryForward(
size_t const& chunkPos,
size_t const& start,
size_t const& end,
472 FunctionObject function, Args&&... args) {
478 for (
size_t dataPos = start; dataPos < end; dataPos += 1) {
479 pHandle.setPointers(dataPos, chunks[chunkPos]);
480 pHandle.call(function, std::forward<Args>(args)...);
484 template<
typename FunctionObject,
typename... Args>
485 CODI_INLINE void forEachChunkEntryReverse(
size_t const& chunkPos,
size_t const& start,
size_t const& end,
486 FunctionObject function, Args&&... args) {
495 for (
size_t dataPos = start; dataPos > end; ) {
498 pHandle.setPointers(dataPos, chunks[chunkPos]);
499 pHandle.call(function, std::forward<Args>(args)...);
506 if (chunks.size() == curChunkIndex) {
507 curChunk =
new Chunk(chunkSize);
508 chunks.push_back(curChunk);
509 positions.push_back(nested->getPosition());
511 curChunk = chunks[curChunkIndex];
513 positions[curChunkIndex] = nested->getPosition();
519 template<
typename Chunk,
typename NestedData = EmptyData>
#define CODI_NO_INLINE
See codi::Config::AvoidedInlines.
Definition config.h:417
#define CODI_INLINE
See codi::Config::ForcedInlines.
Definition config.h:457
#define codiAssert(x)
See codi::Config::EnableAssert.
Definition config.h:432
#define CODI_DD(Type, Default)
Abbreviation for CODI_DECLARE_DEFAULT.
Definition macros.hpp:94
#define CODI_T(...)
Abbreviation for CODI_TEMPLATE.
Definition macros.hpp:111
CoDiPack - Code Differentiation Package.
Definition codi.hpp:90
size_t data
Array position index.
Definition position.hpp:107
NestedPosition inner
Position of nested data.
Definition position.hpp:109
Position with two indices for e.g. chunked data access.
Definition position.hpp:166
size_t chunk
Chunk position index.
Definition position.hpp:172
Data is stored chunk-wise in this DataInterface implementation. If a chunk runs out of space,...
Definition chunkedData.hpp:64
typename std::conditional< selectedDepth==0, TerminatingPointerStore< PointerInserter >, PointerInserter >::type NestingDepthPointerInserter
For selectedDepth == 0 create a pointer inserter that calls the function object.
Definition chunkedData.hpp:76
void setNested(NestedData *v)
Definition chunkedData.hpp:285
ChunkPosition< NestedPosition > Position
Contains position data for this DataInterface and all nested interfaces.
Definition chunkedData.hpp:80
void forEachForward(Position const &start, Position const &end, FunctionObject function, Args &&... args)
Calls the function object for each item in the data stream. This call is not recursive.
Definition chunkedData.hpp:413
size_t getDataSize() const
Definition chunkedData.hpp:228
typename NestedData::Position NestedPosition
Position of NestedData.
Definition chunkedData.hpp:72
T_NestedData NestedData
See ChunkedData.
Definition chunkedData.hpp:68
void pushData(Data const &... data)
Add data to the storage allocated by the implementation. The method can only be called after a call t...
Definition chunkedData.hpp:117
Position getPosition() const
Definition chunkedData.hpp:238
void addToTapeValues(TapeValues &values) const
Add amount of stored data to the TapeValues object. Not called on the nested vector.
Definition chunkedData.hpp:258
void getDataPointers(Data *&... pointers)
Get pointers to the data from the storage implementation. The method can only be called after a call ...
Definition chunkedData.hpp:124
ChunkedData(size_t const &chunkSize, NestedData *nested)
Allocate chunkSize entries and set the nested DataInterface.
Definition chunkedData.hpp:96
InternalPosHandle reserveItems(size_t const &items)
Reserve this many items on the data stream. See pushData for details.
Definition chunkedData.hpp:137
TargetPosition extractPosition(Position const &pos) const
Extract the position of a nested DataInterface from the global position object provide by this interf...
Definition chunkedData.hpp:274
ChunkedData(size_t const &chunkSize)
Allocate chunkSize entries. Requires a call to setNested.
Definition chunkedData.hpp:102
void swap(ChunkedData< Chunk, NestedData > &other)
Definition chunkedData.hpp:298
void forEachChunk(FunctionObject &function, bool recursive, Args &&... args)
Calls the function object for each continuous segment of data.
Definition chunkedData.hpp:401
void erase(Position const &start, Position const &end, bool recursive=true)
Definition chunkedData.hpp:203
void evaluateReverse(Position const &start, Position const &end, FunctionObject function, Args &&... args)
Evaluates the function object with segments of continuous and valid data for all nested DataInterface...
Definition chunkedData.hpp:357
Position getZeroPosition() const
Definition chunkedData.hpp:248
void resetTo(Position const &pos)
Definition chunkedData.hpp:185
T_PointerInserter PointerInserter
See ChunkedData.
Definition chunkedData.hpp:69
void addDataSize(size_t size)
Add this many items to the data stream, after the data has been manipulated via pointers obtained fro...
Definition chunkedData.hpp:130
void reset()
Definition chunkedData.hpp:164
void resetHard()
Definition chunkedData.hpp:169
~ChunkedData()
Destructor.
Definition chunkedData.hpp:106
void evaluateForward(Position const &start, Position const &end, FunctionObject function, Args &&... args)
Evaluates the function object with segments of continuous and valid data for all nested DataInterface...
Definition chunkedData.hpp:315
void resize(size_t const &totalSize)
Definition chunkedData.hpp:151
Position extractPosition(Position const &pos) const
Extract the position of a nested DataInterface from the global position object provide by this interf...
Definition chunkedData.hpp:280
T_Chunk Chunk
See ChunkedData.
Definition chunkedData.hpp:67
size_t InternalPosHandle
Position in the chunk.
Definition chunkedData.hpp:71
size_t getPushedDataCount(InternalPosHandle const &startPos)
Definition chunkedData.hpp:243
void forEachReverse(Position const &start, Position const &end, FunctionObject function, Args &&... args)
Calls the function object for each item in the data stream. This call is not recursive.
Definition chunkedData.hpp:435
Data stream interface for tape data. Encapsulates data that is written e.g. for each statement or arg...
Definition dataInterface.hpp:149
Inserts data pointers at the back of all arguments in the nested call hierarchy.
Definition pointerStore.hpp:72
Tape information that can be printed in a pretty print format or a table format.
Definition tapeValues.hpp:73
void addDoubleEntry(std::string const &name, double const &value, bool usedMem=false, bool allocatedMem=false)
Add double entry. If it is a memory entry, it should be in bytes.
Definition tapeValues.hpp:126
void addUnsignedLongEntry(std::string const &name, unsigned long const &value)
Add unsigned long entry.
Definition tapeValues.hpp:150