41#include "../misc/byteDataView.hpp"
42#include "../misc/eventSystem.hpp"
43#include "../misc/fileIo.hpp"
45#include "../misc/temporaryMemory.hpp"
46#include "data/dataInterface.hpp"
47#include "data/position.hpp"
48#include "indices/indexManagerInterface.hpp"
49#include "interfaces/fullTapeInterface.hpp"
50#include "io/tapeReaderWriterInterface.hpp"
51#include "misc/externalFunction.hpp"
52#include "misc/lowLevelFunctionEntry.hpp"
53#include "misc/vectorAccessInterface.hpp"
73 template<
typename Chunk,
typename Nested>
85 template<
typename T_TapeTypes>
91 template<
typename Chunk,
typename Nested>
106 using Position =
typename LowLevelFunctionByteData::Position;
127 template<
typename T_ImplTapeTypes,
typename T_Impl>
129 :
public FullTapeInterface<typename T_ImplTapeTypes::Real, typename T_ImplTapeTypes::Gradient,
130 typename T_ImplTapeTypes::Identifier,
131 typename CommonTapeTypes<T_ImplTapeTypes>::Position> {
137 using Real =
typename ImplTapeTypes::Real;
174 return static_cast<Impl const&
>(*this);
178 return static_cast<Impl&
>(*this);
186 cast().clearAdjoints(adjointsManagement);
194 cast().indexManager.get().reset();
201 codiAssert(this->manualPushGoal == this->manualPushCounter);
203 this->manualPushLhsValue = lhsValue;
204 this->manualPushLhsIdentifier = lhsIndex;
205 this->manualPushCounter = 0;
206 this->manualPushGoal = size;
215 this->manualPushCounter += 1;
251 if (token != EXTERNAL_FUNCTION_TOKEN) {
252 CODI_EXCEPTION(
"External function token is not zero.");
276 cast().gradient(identifier, adjointsManagement) =
gradient;
282 return cast().gradient(identifier, adjointsManagement);
296 impl.evaluate(impl.getPosition(), impl.getZeroPosition(), adjointsManagement);
300 template<
typename Lhs>
304 value.
cast().getIdentifier());
325 template<
typename Stream = std::ostream>
327 cast().getTapeValues().formatDefault(out);
331 template<
typename Stream = std::ostream>
333 cast().getTapeValues().formatHeader(out);
337 template<
typename Stream = std::ostream>
339 cast().getTapeValues().formatRow(out);
344 TapeValues values = cast().internalGetTapeValues();
346 values.
addSection(
"Low level function info data entries");
348 values.
addSection(
"Low level function byte data entries");
357 resetInternal(resetAdjoints, adjointsManagement, EventHints::Reset::Full);
369 std::swap(
active, other.active);
382 impl.deleteAdjointVector();
394 static void readFunction(ChunkBase* chunk, FileIo& handle) {
395 chunk->readData(handle);
398 static void deleteFunction(ChunkBase* chunk) {
407 FileIo io(filename,
true);
414 FileIo io(filename,
false);
441 "Tape parameter 'ExternalFunctionsSize' no longer supported. Use 'LLFInfoDataSize' and "
442 "'LLFByteDataSize' instead.");
446 CODI_EXCEPTION(
"Tried to get undefined parameter for tape.");
469 "Tape parameter 'ExternalFunctionsSize' is no longer supported. Use 'LLFInfoDataSize' and "
470 "'LLFByteDataSize' instead.");
473 CODI_EXCEPTION(
"Tried to set undefined parameter for tape.");
496 codiAssert((
size_t)token < lowLevelFunctionLookup->size());
499 "Requested size for low level function is to big. Increase "
500 "codi::Config::LowLevelFunctionDataSize or perform a dynamic memory allocation.");
508 char* dataPointer =
nullptr;
510 dataView.
init(dataPointer, 0, size);
519 size_t& curLLFByteDataPos,
char* dataPtr,
521 size_t& curLLFTInfoDataPos,
526 curLLFTInfoDataPos -= 1;
527 curLLFByteDataPos -= dataSizePtr[curLLFTInfoDataPos];
530 size_t endPos = curLLFByteDataPos + dataSizePtr[curLLFTInfoDataPos];
531 ByteDataView dataView(dataPtr, curLLFByteDataPos, endPos);
536 func.template call<callType>(&impl, dataView, std::forward<Args>(args)...);
539 }
else CODI_Unlikely if (LowLevelFunctionEntryCallKind::Delete == callType) {
542 CODI_EXCEPTION(
"Requested call is not supported for low level function with token '%d'.", (
int)
id);
546 curLLFByteDataPos += dataSizePtr[curLLFTInfoDataPos];
547 curLLFTInfoDataPos += 1;
554 size_t& curLLFByteDataPos,
char* dataPtr,
556 size_t& curLLFTInfoDataPos,
562 curLLFByteDataPos += dataSizePtr[curLLFTInfoDataPos];
563 curLLFTInfoDataPos += 1;
565 curLLFTInfoDataPos -= 1;
566 curLLFByteDataPos -= dataSizePtr[curLLFTInfoDataPos];
614 impl.evaluateForward(impl.getZeroPosition(), impl.getPosition(), adjointsManagement);
623 template<
typename Type>
626 impl.writeTape(writer.get(), impl.getZeroPosition(), impl.getPosition());
631 template<
typename Type>
634 impl.writeTape(writer.get(), start, end);
638 template<
typename Type>
641 impl.writeTape(&writeToFileRef, impl.getZeroPosition(), impl.getPosition());
646 template<
typename Type>
649 impl.writeTape(&writeToFileRef, start, end);
669 return index != cast().getPassiveIndex();
673 template<
typename Lhs>
700 auto deleteFunc = [
this](
702 size_t& curLLFByteDataPos,
size_t const& endLLFByteDataPos,
char* dataPtr,
704 size_t& curLLFInfoDataPos,
size_t const& endLLFInfoDataPos,
709 while (curLLFInfoDataPos > endLLFInfoDataPos) {
711 curLLFInfoDataPos, tokenPtr, dataSizePtr);
729 impl.clearAdjoints(impl.getPosition(), pos, adjointsManagement);
748 impl.evaluatePrimal(impl.getZeroPosition(), impl.getPosition());
753 cast().primal(identifier) =
primal;
758 return cast().primal(identifier);
770 void init(
typename ImplTapeTypes::NestedData* nested) {
778 template<
typename ImplTapeTypes,
typename Impl>
779 std::vector<LowLevelFunctionEntry<Impl, typename ImplTapeTypes::Real, typename ImplTapeTypes::Identifier>>*
#define CODI_Unlikely
Declare unlikely evaluation of an execution path.
Definition config.h:408
#define CODI_INLINE
See codi::Config::ForcedInlines.
Definition config.h:469
#define CODI_Likely
Declare likely evaluation of an execution path.
Definition config.h:406
#define codiAssert(x)
See codi::Config::EnableAssert.
Definition config.h:441
#define CODI_DD(Type, Default)
Abbreviation for CODI_DECLARE_DEFAULT.
Definition macros.hpp:96
#define CODI_ENABLE_CHECK(option, condition)
Definition macros.hpp:61
#define CODI_ANY
Used in default declarations of expression templates.
Definition macros.hpp:100
Configuration options for CoDiPack.
Definition config.h:65
uint16_t LowLevelFunctionDataSize
Size store type for a low level function.
Definition config.h:98
uint16_t LowLevelFunctionToken
Token type for low level functions in the tapes.
Definition config.h:108
bool constexpr EnableAssert
Enables asserts in CoDiPack for consistency checking.
Definition config.h:445
size_t constexpr LowLevelFunctionDataSizeMax
Maximum data size of a low level function.
Definition config.h:101
bool constexpr CheckTapeActivity
Makes it possible to ignore certain code parts. If turned of everything will be recorded.
Definition config.h:170
bool constexpr StatementEvents
Enable statement events. Disabled by default.
Definition config.h:328
size_t constexpr LowLevelFunctionTokenMaxSize
Maximum number of low level functions.
Definition config.h:111
Reset
Characterize a tape reset.
Definition eventSystem.hpp:72
CoDiPack - Code Differentiation Package.
Definition codi.hpp:94
TapeParameters
Configuration options for a tape.
Definition tapeParameters.hpp:52
@ LLFByteDataSize
Definition tapeParameters.hpp:70
@ LLFInfoDataSize
Definition tapeParameters.hpp:68
@ ExternalFunctionsSize
Definition tapeParameters.hpp:57
LowLevelFunctionEntryCallKind
All possible call types for a low level function entry.
Definition lowLevelFunctionEntry.hpp:51
inlinevoid CODI_UNUSED(Args const &...)
Disable unused warnings for an arbitrary number of arguments.
Definition macros.hpp:54
AdjointsManagement
Policies for management of the tape's interal adjoints.
Definition tapeParameters.hpp:100
@ Automatic
Manage internal adjoints automatically, including locking, bounds checking, and resizing.
Definition tapeParameters.hpp:102
Definition byteDataView.hpp:51
inlinesize_t getPosition()
Get the current data position.
Definition byteDataView.hpp:70
inlinevoid init(char *pointer, size_t pos, size_t end)
Initialize the object.
Definition byteDataView.hpp:75
A chunk stores a contiguous block of data in CoDiPack.
Definition chunk.hpp:76
inlinevirtual void writeData(FileIo &handle) const =0
Write data to the FileIo handle.
Gradient const & getGradient(Identifier const &identifier, AdjointsManagement adjointsManagement=AdjointsManagement::Automatic) const
Set the gradient.
Definition commonTapeImplementation.hpp:280
inlinevoid internalStoreLowLevelFunction(Config::LowLevelFunctionToken token, size_t size, ByteDataView &dataView)
Called by the implementing tapes to store a low level function. The size is reserved and allocated....
Definition commonTapeImplementation.hpp:494
void setPassive()
Stop/interrupt recording of statements.
Definition commonTapeImplementation.hpp:314
CommonTapeImplementation(CommonTapeImplementation &&)=delete
Do not allow move construction. Relevant use cases should be covered by swap.
inlinevoid initializeManualPushData(Real const &lhsValue, Identifier const &lhsIndex, size_t size)
Initialize all manual push data, including the counter. Check that a previous manual store is complet...
Definition commonTapeImplementation.hpp:200
Real manualPushLhsValue
Definition commonTapeImplementation.hpp:158
typename ImplTapeTypes::Real Real
See TapeTypesInterface.
Definition commonTapeImplementation.hpp:137
std::set< TapeParameters > const & getAvailableParameters() const
See Parameters functions.
Definition commonTapeImplementation.hpp:425
CommonTapeImplementation(CommonTapeImplementation const &)=delete
Do not allow copy construction.
Position getPosition() const
Current position of the tape.
Definition commonTapeImplementation.hpp:684
void writeTape(codi::TapeWriterInterface< Type > &writeToFileRef, Position const &start, Position const &end)
For partial-tape writers using a manually generated writers.
Definition commonTapeImplementation.hpp:647
void setParameter(TapeParameters parameter, size_t value)
See Parameters functions.
Definition commonTapeImplementation.hpp:459
void evaluateForward(AdjointsManagement adjointsManagement=AdjointsManagement::Automatic)
Perform a forward evaluation of a part of the tape. It has to hold start <= end.
Definition commonTapeImplementation.hpp:611
void resetHard()
Delete everything and return to the state after construction, as far as possible.
Definition commonTapeImplementation.hpp:375
void setPrimal(Identifier const &identifier, Real const &primal)
Set primal value.
Definition commonTapeImplementation.hpp:752
void setGradient(Identifier const &identifier, Gradient const &gradient, AdjointsManagement adjointsManagement=AdjointsManagement::Automatic)
Set the gradient.
Definition commonTapeImplementation.hpp:274
size_t manualPushCounter
Definition commonTapeImplementation.hpp:161
typename CommonTapeTypes< ImplTapeTypes >::LowLevelFunctionByteData LowLevelFunctionByteData
See CommonTapeTypes.
Definition commonTapeImplementation.hpp:144
void printStatistics(Stream &out=std::cout) const
Default formatting of TapeValues.
Definition commonTapeImplementation.hpp:326
LowLevelFunctionInfoData llfInfoData
Definition commonTapeImplementation.hpp:155
inlinevoid resetTo(Position const &pos, bool resetAdjoints=true, AdjointsManagement adjointsManagement=AdjointsManagement::Automatic)
Reset the tape to the provided position.
Definition commonTapeImplementation.hpp:723
typename ImplTapeTypes::Gradient Gradient
See TapeTypesInterface.
Definition commonTapeImplementation.hpp:138
void deactivateValue(LhsExpressionInterface< Real, Gradient, Impl, Lhs > &value)
Definition commonTapeImplementation.hpp:674
Position getZeroPosition() const
Initial position of the tape.
Definition commonTapeImplementation.hpp:689
Identifier manualPushLhsIdentifier
Definition commonTapeImplementation.hpp:159
void deleteData()
See File IO functions.
Definition commonTapeImplementation.hpp:420
void readFromFile(const std::string &filename)
See File IO functions.
Definition commonTapeImplementation.hpp:413
typename CommonTapeTypes< ImplTapeTypes >::LowLevelFunctionInfoData LowLevelFunctionInfoData
See CommonTapeTypes.
Definition commonTapeImplementation.hpp:142
void setActive()
Start/continue recording of statements.
Definition commonTapeImplementation.hpp:308
Real const & getPrimal(Identifier const &identifier) const
Get primal value.
Definition commonTapeImplementation.hpp:757
bool hasParameter(TapeParameters parameter) const
See Parameters functions.
Definition commonTapeImplementation.hpp:453
bool isActive() const
Check if the tape is recording.
Definition commonTapeImplementation.hpp:320
TapeValues internalGetTapeValues() const
Create tape values.
CommonTapeImplementation & operator=(CommonTapeImplementation &&)=delete
Do not allow move assignment. Relevant use cases should be covered by swap.
void deleteLowLevelFunctionData(Position const &pos)
Delete all external function data up to pos.
Definition commonTapeImplementation.hpp:698
inlinestatic void skipLowLevelFunction(bool forward, size_t &curLLFByteDataPos, char *dataPtr, size_t &curLLFTInfoDataPos, Config::LowLevelFunctionToken *const tokenPtr, Config::LowLevelFunctionDataSize *const dataSizePtr)
Skip the data for a low level function.
Definition commonTapeImplementation.hpp:552
size_t manualPushGoal
Definition commonTapeImplementation.hpp:160
Identifier getPassiveIndex() const
Identifier for passive values. Usually 0.
Definition commonTapeImplementation.hpp:658
inlineConfig::LowLevelFunctionToken registerLowLevelFunction(LowLevelFunctionEntry< Impl, Real, Identifier > const &entry)
Register a low level function on the tape.
Definition commonTapeImplementation.hpp:578
void init(typename ImplTapeTypes::NestedData *nested)
Initialize the base class.
Definition commonTapeImplementation.hpp:770
TemporaryMemory allocator
Definition commonTapeImplementation.hpp:163
static std::vector< LowLevelFunctionEntry< Impl, Real, Identifier > > * lowLevelFunctionLookup
Definition commonTapeImplementation.hpp:166
LowLevelFunctionByteData NestedData
Shorthand.
Definition commonTapeImplementation.hpp:147
typename LowLevelFunctionByteData::Position NestedPosition
Shorthand.
Definition commonTapeImplementation.hpp:148
bool active
Definition commonTapeImplementation.hpp:152
std::set< TapeParameters > options
Definition commonTapeImplementation.hpp:153
inlinevoid incrementManualPushCounter()
Increment the manual push counter. Check against the declared push goal.
Definition commonTapeImplementation.hpp:211
void printTableHeader(Stream &out=std::cout) const
Table header output of TapeValues.
Definition commonTapeImplementation.hpp:332
inlinestatic void callLowLevelFunction(Impl &impl, bool forward, size_t &curLLFByteDataPos, char *dataPtr, size_t &curLLFTInfoDataPos, Config::LowLevelFunctionToken *const tokenPtr, Config::LowLevelFunctionDataSize *const dataSizePtr, Args &&... args)
Called by the implementing tapes during a tape evaluation when a low level function statement has bee...
Definition commonTapeImplementation.hpp:517
CommonTapeImplementation & operator=(CommonTapeImplementation const &)=delete
Do not allow copy assignment.
Identifier getInvalidIndex() const
Invalid identifier.
Definition commonTapeImplementation.hpp:663
void printTableRow(Stream &out=std::cout) const
Table row output of TapeValues.
Definition commonTapeImplementation.hpp:338
inlineTemporaryMemory & getTemporaryMemory()
Temporary memory that can be used for dynamic data both during the evaluation and the recording.
Definition commonTapeImplementation.hpp:573
bool isIdentifierActive(Identifier const &index) const
Definition commonTapeImplementation.hpp:668
TapeValues getTapeValues() const
Get current tape values.
Definition commonTapeImplementation.hpp:343
T_ImplTapeTypes ImplTapeTypes
See CommonTapeImplementation.
Definition commonTapeImplementation.hpp:134
void writeTape(std::unique_ptr< TapeWriterInterface< Type > > writer)
For full-tape writers using a smart pointer.
Definition commonTapeImplementation.hpp:624
void writeTape(codi::TapeWriterInterface< Type > &writeToFileRef)
For full-tape writers using a manually generated writers.
Definition commonTapeImplementation.hpp:639
T_Impl Impl
See CommonTapeImplementation.
Definition commonTapeImplementation.hpp:135
void evaluate(AdjointsManagement adjointsManagement=AdjointsManagement::Automatic)
Perform a full reverse evaluation of the tape.
Definition commonTapeImplementation.hpp:293
void swap(Impl &other)
Swap all data with an other tape.
Definition commonTapeImplementation.hpp:368
inlinevoid reset(bool resetAdjoints=true, AdjointsManagement adjointsManagement=AdjointsManagement::Automatic)
Reset the tape to the initial state for a fresh recording.
Definition commonTapeImplementation.hpp:355
typename CommonTapeTypes< ImplTapeTypes >::Position Position
See TapeTypesInterface.
Definition commonTapeImplementation.hpp:145
void writeTape(std::unique_ptr< TapeWriterInterface< Type > > writer, Position const &start, Position const &end)
For partial-tape writers using a smart pointer.
Definition commonTapeImplementation.hpp:632
LowLevelFunctionByteData llfByteData
Definition commonTapeImplementation.hpp:156
void registerOutput(LhsExpressionInterface< Real, Gradient, Impl, Lhs > &value)
Definition commonTapeImplementation.hpp:301
typename ImplTapeTypes::Identifier Identifier
See TapeTypesInterface.
Definition commonTapeImplementation.hpp:139
CommonTapeImplementation()
Constructor.
Definition commonTapeImplementation.hpp:232
void pushExternalFunction(ExternalFunction< Impl > const &extFunc)
Definition commonTapeImplementation.hpp:597
size_t getParameter(TapeParameters parameter) const
See Parameters functions.
Definition commonTapeImplementation.hpp:431
void evaluatePrimal()
Perform a full (forward) reevaluation of the primals in the tape.
Definition commonTapeImplementation.hpp:745
void writeToFile(const std::string &filename)
See File IO functions.
Definition commonTapeImplementation.hpp:406
Declares all types used in the CommonTapeImplementation.
Definition commonTapeImplementation.hpp:86
typename TapeTypes::template Data< Chunk, Nested > Data
See TapeTypesInterface.
Definition commonTapeImplementation.hpp:92
typename TapeTypes::NestedData NestedData
See TapeTypesInterface.
Definition commonTapeImplementation.hpp:94
Data< LowLevelFunctionInfoChunk, NestedData > LowLevelFunctionInfoData
Token and size data for low level functions.
Definition commonTapeImplementation.hpp:99
Chunk1< char > LowLevelFunctionByteChunk
Byte data chunk.
Definition commonTapeImplementation.hpp:102
typename LowLevelFunctionByteData::Position Position
Global position of the tape.
Definition commonTapeImplementation.hpp:106
Data< LowLevelFunctionByteChunk, LowLevelFunctionInfoData > LowLevelFunctionByteData
Byte data for low level functions.
Definition commonTapeImplementation.hpp:104
T_TapeTypes TapeTypes
See CommonTapeTypes.
Definition commonTapeImplementation.hpp:89
Chunk2< Config::LowLevelFunctionToken, Config::LowLevelFunctionDataSize > LowLevelFunctionInfoChunk
Token and size data chunk.
Definition commonTapeImplementation.hpp:97
Data stream interface for tape data. Encapsulates data that is written e.g. for each statement or arg...
Definition dataInterface.hpp:149
static inlinevoid notifyTapeStartRecordingListeners(Tape &tape)
Invoke callbacks for TapeStartRecording events.
Definition eventSystem.hpp:370
static inlinevoid notifyTapeResetListeners(Tape &tape, Position const &position, EventHints::Reset kind, bool clearAdjoints)
Invoke callbacks for TapeReset events.
Definition eventSystem.hpp:521
static inlinevoid notifyTapeStopRecordingListeners(Tape &tape)
Invoke callbacks for TapeStopRecording events.
Definition eventSystem.hpp:396
static inlinevoid notifyTapeRegisterOutputListeners(Tape &tape, Real &value, Identifier &identifier)
Invoke callbacks for TapeRegisterOutput events.
Definition eventSystem.hpp:453
Base class for all CoDiPack expressions.
Definition expressionInterface.hpp:60
inlinestatic LowLevelFunctionEntry< Tape, Real, Identifier > create()
Create the function entry for the tape registration.
Definition externalFunction.hpp:220
inlinestatic void store(Tape &tape, Config::LowLevelFunctionToken token, ExtFunc const &extFunc)
Store an external function on the tape.
Definition externalFunction.hpp:212
User-defined evaluation functions for the taping process.
Definition externalFunction.hpp:102
Helper structure for writing binary data.
Definition fileIo.hpp:84
Full tape interface that supports all features of CoDiPack.
Definition fullTapeInterface.hpp:84
Gradient & gradient(Identifier const &identifier, AdjointsManagement adjointsManagement=AdjointsManagement::Automatic)
static Index constexpr InactiveIndex
Default inactive index for all index managers.
Definition indexManagerInterface.hpp:86
static Index constexpr InvalidIndex
Default invalid index for all index mangers (max value for unsigned types).
Definition indexManagerInterface.hpp:87
void store(Lhs &lhs, Rhs const &rhs)
Base class for all CoDiPack lvalue expression.
Definition lhsExpressionInterface.hpp:63
inlineReal const & getValue() const
Get the primal value of this lvalue.
Definition lhsExpressionInterface.hpp:125
inlineImpl & cast()
Cast to the implementation.
Definition lhsExpressionInterface.hpp:99
Low level function entry on the tape. See LowLevelFunctionTapeInterface for details.
Definition lowLevelFunctionEntry.hpp:67
Real & primal(Identifier const &identifier)
Interface for the definition of tape types.
Definition commonTapeImplementation.hpp:64
DataInterface< Nested > Data
Definition commonTapeImplementation.hpp:74
DataInterface<> NestedData
Definition commonTapeImplementation.hpp:76
int Gradient
Gradient computation type, e.g. double or Direction.
Definition commonTapeImplementation.hpp:68
int Real
Primal computation type, e.g. double.
Definition commonTapeImplementation.hpp:67
int Identifier
Identifier for the internal management, e.g. int.
Definition commonTapeImplementation.hpp:69
Tape information that can be printed in a pretty print format or a table format.
Definition tapeValues.hpp:75
void addSection(std::string const &name)
Add section. All further entries are added under this section.
Definition tapeValues.hpp:158
The interface used by all the tape writers. The tape writers are used to generate text,...
Definition tapeReaderWriterInterface.hpp:129
Allocator for temporary used memory.
Definition temporaryMemory.hpp:54