41#include "../tapes/interfaces/fullTapeInterface.hpp"
107 template<
typename T_Tape>
111 using Real =
typename Tape::Real;
133 StatementStoreOnTape,
135 StatementEvaluatePrimal,
147 using EventListenerMap = std::map<Event, std::list<std::pair<Handle, std::pair<Callback, void*>>>>;
159 new EventListenerMap{{Event::TapeStartRecording, {}}, {Event::TapeStopRecording, {}},
160 {Event::TapeRegisterInput, {}}, {Event::TapeRegisterOutput, {}},
161 {Event::TapeEvaluate, {}}, {Event::TapeReset, {}},
162 {Event::PreaccStart, {}}, {Event::PreaccFinish, {}},
163 {Event::PreaccAddInput, {}}, {Event::PreaccAddOutput, {}},
164 {Event::StatementPrimal, {}}, {Event::StatementStoreOnTape, {}},
165 {Event::StatementEvaluate, {}}, {Event::StatementEvaluatePrimal, {}},
166 {Event::IndexCreate, {}}, {Event::IndexAssign, {}},
167 {Event::IndexFree, {}}, {Event::IndexCopy, {}}};
169 codiAssert(listeners->size() == (
size_t)Event::Count);
193 template<
typename TypedCallback>
197 nextHandle = nextHandle + 1;
198 Handle handle = nextHandle;
199 getListeners()[event].push_back(std::make_pair(handle, std::make_pair((
void*)callback, customData)));
218 template<
typename TypedCallback,
typename... Args>
222 ((TypedCallback)listener.second.first)(std::forward<Args>(args)..., listener.second.second);
244 void* customData =
nullptr) {
269 lhsIdentifier, newValue, statement);
281 Tape localTape = tape;
299 auto iterator = listenersForEvent.second.begin();
300 for (; listenersForEvent.second.end() != iterator; ++iterator) {
301 if (handle == iterator->first) {
306 if (listenersForEvent.second.end() != iterator) {
307 listenersForEvent.second.erase(iterator);
316 template<
typename Tape>
317 typename EventSystemBase<Tape>::Handle EventSystemBase<Tape>::nextHandle = 0;
328 template<
typename T_Tape>
332 using Real =
typename Tape::Real;
335 using Index =
typename Tape::Identifier;
357 void* customData =
nullptr) {
372 Event::TapeStartRecording, tape);
384 void* customData =
nullptr) {
398 Event::TapeStopRecording, tape);
410 void* customData =
nullptr) {
438 void* customData =
nullptr) {
469 void* customData =
nullptr) {
504 void* customData =
nullptr) {
522 bool clearAdjoints) {
541 void* customData =
nullptr) {
566 void* customData =
nullptr) {
593 void* customData =
nullptr) {
622 void* customData =
nullptr) {
656 void* customData =
nullptr) {
677 Real const& newValue,
size_t numActiveVariables,
679 Real const* jacobians) {
683 rhsIdentifiers, jacobians);
696 void* customData =
nullptr) {
713 size_t sizeLhsAdjoint,
Real const* lhsAdjoint) {
728 void* customData =
nullptr) {
746 Real const& lhsValue) {
765 void* customData =
nullptr) {
792 void* customData =
nullptr) {
820 void* customData =
nullptr) {
842 template<
typename Real,
typename Gradient>
843 struct ForwardEvaluation;
854 template<
typename Real,
typename Gradient>
865 template<
typename T_Real,
typename T_Gradient>
869 using Real =
CODI_DD(T_Real,
double);
870 using Identifier =
CODI_DD(T_Gradient,
double);
878 void* customData =
nullptr) {
886 Identifier
const& lhsIdentifier, Real
const& newValue,
888 CODI_UNUSED(tape, lhsValue, lhsIdentifier, newValue, statement);
#define CODI_INLINE
See codi::Config::ForcedInlines.
Definition config.h:469
#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_T(...)
Abbreviation for CODI_TEMPLATE.
Definition macros.hpp:116
bool constexpr PreaccEvents
Enable preaccumulation events. Disabled by default.
Definition config.h:320
bool constexpr ADWorkflowEvents
Enable AD workflow events, also known as Tape* events. Enabled by default.
Definition config.h:312
bool constexpr IndexEvents
Enable index management events. Disabled by default.
Definition config.h:336
bool constexpr StatementEvents
Enable statement events. Disabled by default.
Definition config.h:328
Definition eventSystem.hpp:50
Statement
Classify statements.
Definition eventSystem.hpp:65
Reset
Characterize a tape reset.
Definition eventSystem.hpp:72
EvaluationKind
Classify tape evaluations.
Definition eventSystem.hpp:52
Endpoint
Distinguish between beginning and end of tape evaluations.
Definition eventSystem.hpp:59
CoDiPack - Code Differentiation Package.
Definition codi.hpp:94
inlinevoid CODI_UNUSED(Args const &...)
Disable unused warnings for an arbitrary number of arguments.
Definition macros.hpp:54
Base class for the CoDiPack event system.
Definition eventSystem.hpp:108
static inlineHandle internalRegisterListener(bool const enabled, Event event, TypedCallback callback, void *customData)
Internal method for callback registration.
Definition eventSystem.hpp:194
size_t Handle
Handle that identifies a registered callback.
Definition eventSystem.hpp:114
std::map< Event, std::list< std::pair< Handle, std::pair< Callback, void * > > > > EventListenerMap
Map that links events to registered callbacks and their associated custom data.
Definition eventSystem.hpp:147
Event
Full set of events.
Definition eventSystem.hpp:118
T_Tape Tape
See EventSystemBase.
Definition eventSystem.hpp:110
static inlinevoid internalNotifyListeners(bool const enabled, Event event, Args &&... args)
Internal method for callback invocation.
Definition eventSystem.hpp:219
typename Tape::Identifier Identifier
Identifier type used by the tape.
Definition eventSystem.hpp:112
void * Callback
Definition eventSystem.hpp:145
static inlineEventListenerMap & getListeners()
Access the static EventListenerMap.
Definition eventSystem.hpp:157
static inlinevoid notifyStatementPrimalListeners(Tape &tape, Real const &lhsValue, Identifier const &lhsIdentifier, Real const &newValue, EventHints::Statement statement)
Invoke callbacks for StatementPrimal events.
Definition eventSystem.hpp:264
static inlinevoid notifyStatementPrimalListeners(Tape &&tape, Real const &lhsValue, Identifier const &lhsIdentifier, Real const &newValue, EventHints::Statement statement)
Invoke callbacks for StatementPrimal events.
Definition eventSystem.hpp:278
static inlinevoid deregisterListener(Handle const &handle)
Deregister a listener.
Definition eventSystem.hpp:297
typename Tape::Real Real
Floating point type the tape is based on.
Definition eventSystem.hpp:111
static inlineHandle registerStatementPrimalListener(void(*callback)(Tape &, Real const &, Identifier const &, Real const &, EventHints::Statement, void *), void *customData=nullptr)
Register callbacks for StatementPrimal events.
Definition eventSystem.hpp:241
Full EventSystem implementation for reverse tapes.
Definition eventSystem.hpp:329
static inlinevoid notifyTapeRegisterInputListeners(Tape &tape, Real &value, Identifier &identifier)
Invoke callbacks for TapeRegisterInput events.
Definition eventSystem.hpp:424
typename Tape::Identifier Identifier
Identifier type used by the tape.
Definition eventSystem.hpp:334
static inlineHandle registerIndexFreeListener(void(*callback)(Index const &, void *), void *customData=nullptr)
Register callbacks for IndexFree events.
Definition eventSystem.hpp:791
static inlineHandle registerIndexCopyListener(void(*callback)(Index const &, void *), void *customData=nullptr)
Register callbacks for IndexCopy events.
Definition eventSystem.hpp:819
T_Tape Tape
See EventSystem.
Definition eventSystem.hpp:331
static inlineHandle registerStatementEvaluatePrimalListener(void(*callback)(Tape &, Identifier const &, Real const &, void *), void *customData=nullptr)
Register callbacks for StatementEvaluatePrimal events.
Definition eventSystem.hpp:726
static inlineHandle registerPreaccStartListener(void(*callback)(Tape &, void *), void *customData=nullptr)
Register callbacks for PreaccStart events.
Definition eventSystem.hpp:540
static inlinevoid notifyTapeEvaluateListeners(Tape &tape, Position const &start, Position const &end, VectorAccess *adjoint, EventHints::EvaluationKind evalKind, EventHints::Endpoint endpoint)
Invoke callbacks for TapeEvaluate events.
Definition eventSystem.hpp:486
static inlineHandle registerTapeRegisterOutputListener(void(*callback)(Tape &, Real &, Identifier &, void *), void *customData=nullptr)
Register callbacks for TapeRegisterOutput events.
Definition eventSystem.hpp:437
typename Base::Event Event
See EventSystemBase.
Definition eventSystem.hpp:341
static inlinevoid notifyPreaccAddInputListeners(Tape &tape, Real const &value, Identifier const &identifier)
Invoke callbacks for PreaccAddInput events.
Definition eventSystem.hpp:607
static inlinevoid notifyPreaccAddOutputListeners(Tape &tape, Real &value, Identifier &identifier)
Invoke callbacks for PreaccAddOutput events.
Definition eventSystem.hpp:636
static inlineHandle registerIndexAssignListener(void(*callback)(Index const &, void *), void *customData=nullptr)
Register callbacks for IndexAssign events.
Definition eventSystem.hpp:764
static inlinevoid notifyIndexCopyListeners(Index const &index)
Invoke callbacks for IndexCopy events.
Definition eventSystem.hpp:833
static inlineHandle registerTapeRegisterInputListener(void(*callback)(Tape &, Real &, Identifier &, void *), void *customData=nullptr)
Register callbacks for TapeRegisterInput events.
Definition eventSystem.hpp:409
VectorAccessInterface< Real, Identifier > VectorAccess
Vector access interface that is compatible with the Tape.
Definition eventSystem.hpp:338
static inlinevoid notifyIndexAssignListeners(Index const &index)
Invoke callbacks for IndexAssign events.
Definition eventSystem.hpp:778
static inlinevoid notifyTapeStartRecordingListeners(Tape &tape)
Invoke callbacks for TapeStartRecording events.
Definition eventSystem.hpp:370
typename Tape::Gradient Gradient
Gradient type used by the tape.
Definition eventSystem.hpp:333
static inlineHandle registerPreaccFinishListener(void(*callback)(Tape &, void *), void *customData=nullptr)
Register callbacks for PreaccFinish events.
Definition eventSystem.hpp:565
static inlineHandle registerTapeStartRecordingListener(void(*callback)(Tape &, void *), void *customData=nullptr)
Register callbacks for TapeStartRecording events.
Definition eventSystem.hpp:356
static inlinevoid notifyTapeResetListeners(Tape &tape, Position const &position, EventHints::Reset kind, bool clearAdjoints)
Invoke callbacks for TapeReset events.
Definition eventSystem.hpp:521
static inlineHandle registerTapeStopRecordingListener(void(*callback)(Tape &, void *), void *customData=nullptr)
Register callbacks for TapeStopRecording events.
Definition eventSystem.hpp:383
static inlinevoid notifyStatementStoreOnTapeListeners(Tape &tape, Identifier const &lhsIdentifier, Real const &newValue, size_t numActiveVariables, Identifier const *rhsIdentifiers, Real const *jacobians)
Invoke callbacks for StatementStoreOnTape events.
Definition eventSystem.hpp:676
static inlineHandle registerStatementStoreOnTapeListener(void(*callback)(Tape &, Identifier const &, Real const &, size_t, Identifier const *, Real const *, void *), void *customData=nullptr)
Register callbacks for StatementStoreOnTape events.
Definition eventSystem.hpp:654
static inlineHandle registerStatementEvaluateListener(void(*callback)(Tape &, Identifier const &, size_t, Real const *, void *), void *customData=nullptr)
Register callbacks for StatementEvaluate events.
Definition eventSystem.hpp:694
typename Base::Handle Handle
See EventSystemBase.
Definition eventSystem.hpp:342
typename Tape::Real Real
Floating point type the tape is based on.
Definition eventSystem.hpp:332
static inlineHandle registerTapeEvaluateListener(void(*callback)(Tape &, Position const &, Position const &, VectorAccess *, EventHints::EvaluationKind, EventHints::Endpoint, void *), void *customData=nullptr)
Register callbacks for TapeEvaluate events.
Definition eventSystem.hpp:466
static inlinevoid notifyIndexFreeListeners(Index const &index)
Invoke callbacks for IndexFree events.
Definition eventSystem.hpp:806
static inlineHandle registerPreaccAddInputListener(void(*callback)(Tape &, Real const &, Identifier const &, void *), void *customData=nullptr)
Register callbacks for PreaccAddInput events.
Definition eventSystem.hpp:591
EventSystemBase< Tape > Base
Base class abbreviation.
Definition eventSystem.hpp:340
static inlinevoid notifyTapeStopRecordingListeners(Tape &tape)
Invoke callbacks for TapeStopRecording events.
Definition eventSystem.hpp:396
static inlinevoid notifyPreaccFinishListeners(Tape &tape)
Invoke callbacks for PreaccFinish events.
Definition eventSystem.hpp:579
static inlinevoid notifyStatementEvaluatePrimalListeners(Tape &tape, Identifier const &lhsIdentifier, Real const &lhsValue)
Invoke callbacks for StatementEvaluatePrimal events.
Definition eventSystem.hpp:745
static inlineHandle registerPreaccAddOutputListener(void(*callback)(Tape &, Real &, Identifier &, void *), void *customData=nullptr)
Register callbacks for PreaccAddOutput events.
Definition eventSystem.hpp:621
static inlinevoid notifyTapeRegisterOutputListeners(Tape &tape, Real &value, Identifier &identifier)
Invoke callbacks for TapeRegisterOutput events.
Definition eventSystem.hpp:453
static inlineHandle registerTapeResetListener(void(*callback)(Tape &, Position const &, EventHints::Reset, bool, void *), void *customData=nullptr)
Register callbacks for TapeReset events.
Definition eventSystem.hpp:502
static inlinevoid notifyStatementEvaluateListeners(Tape &tape, Identifier const &lhsIdentifier, size_t sizeLhsAdjoint, Real const *lhsAdjoint)
Invoke callbacks for StatementEvaluate events.
Definition eventSystem.hpp:712
typename Tape::Position Position
Definition eventSystem.hpp:336
typename Tape::Identifier Index
Index type used by the tape.
Definition eventSystem.hpp:335
static inlinevoid notifyPreaccStartListeners(Tape &tape)
Invoke callbacks for PreaccStart events.
Definition eventSystem.hpp:553
Implementation of a tape-free forward AD mode through the internal expression interfaces.
Definition forwardEvaluation.hpp:74
Unified access to the adjoint vector and primal vector in a tape evaluation.
Definition vectorAccessInterface.hpp:94