41#include "../tapes/interfaces/fullTapeInterface.hpp"
50 namespace EventHints {
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>
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) {
425 Base::template internalNotifyListeners<void (*)(Tape&, Real&, Identifier&, void*)>(
438 void* customData =
nullptr) {
454 Base::template internalNotifyListeners<void (*)(Tape&, Real&, Identifier&, void*)>(
469 void* customData =
nullptr) {
504 void* customData =
nullptr) {
522 bool clearAdjoints) {
523 Base::template internalNotifyListeners<void (*)(Tape&, Position const&, EventHints::Reset, bool, void*)>(
541 void* customData =
nullptr) {
554 Base::template internalNotifyListeners<void (*)(Tape&, void*)>(
Config::PreaccEvents, Event::PreaccStart, tape);
566 void* customData =
nullptr) {
580 Base::template internalNotifyListeners<void (*)(Tape&, void*)>(
Config::PreaccEvents, Event::PreaccFinish, tape);
593 void* customData =
nullptr) {
609 Base::template internalNotifyListeners<void (*)(Tape&, Real const&, Identifier const&, void*)>(
622 void* customData =
nullptr) {
637 Base::template internalNotifyListeners<void (*)(Tape&, Real&, Identifier&, void*)>(
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) {
714 Base::template internalNotifyListeners<void (*)(Tape&, Identifier const&, size_t, Real const*, void*)>(
728 void* customData =
nullptr) {
746 Real const& lhsValue) {
747 Base::template internalNotifyListeners<void (*)(Tape&, Identifier const&, Real const&, void*)>(
765 void* customData =
nullptr) {
779 Base::template internalNotifyListeners<void (*)(Index const&, void*)>(
Config::IndexEvents, Event::IndexAssign,
792 void* customData =
nullptr) {
807 Base::template internalNotifyListeners<void (*)(Index const&, void*)>(
Config::IndexEvents, Event::IndexFree,
820 void* customData =
nullptr) {
834 Base::template internalNotifyListeners<void (*)(Index const&, void*)>(
Config::IndexEvents, Event::IndexCopy,
842 template<
typename Real,
typename Gradient>
843 struct ForwardEvaluation;
854 template<
typename Real,
typename Gradient>
865 template<
typename T_Real,
typename T_Gradient>
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: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
bool constexpr PreaccEvents
Enable preaccumulation events. Disabled by default.
Definition config.h:311
bool constexpr ADWorkflowEvents
Enable AD workflow events, also known as Tape* events. Enabled by default.
Definition config.h:303
bool constexpr IndexEvents
Enable index management events. Disabled by default.
Definition config.h:327
bool constexpr StatementEvents
Enable statement events. Disabled by default.
Definition config.h:319
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:90
void CODI_UNUSED(Args const &...)
Disable unused warnings for an arbitrary number of arguments.
Definition macros.hpp:46
Represents a concrete lvalue in the CoDiPack expression tree.
Definition activeType.hpp:52
Base class for the CoDiPack event system.
Definition eventSystem.hpp:108
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
static Handle 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
static void notifyStatementPrimalListeners(Tape &tape, Real const &lhsValue, Identifier const &lhsIdentifier, Real const &newValue, EventHints::Statement statement)
Invoke callbacks for StatementPrimal events.
Definition eventSystem.hpp:264
T_Tape Tape
See EventSystemBase.
Definition eventSystem.hpp:110
static void deregisterListener(Handle const &handle)
Deregister a listener.
Definition eventSystem.hpp:297
typename Tape::Identifier Identifier
Identifier type used by the tape.
Definition eventSystem.hpp:112
void * Callback
Definition eventSystem.hpp:145
static void 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 void internalNotifyListeners(bool const enabled, Event event, Args &&... args)
Internal method for callback invocation.
Definition eventSystem.hpp:219
static EventListenerMap & getListeners()
Access the static EventListenerMap.
Definition eventSystem.hpp:157
typename Tape::Real Real
Floating point type the tape is based on.
Definition eventSystem.hpp:111
static Handle internalRegisterListener(bool const enabled, Event event, TypedCallback callback, void *customData)
Internal method for callback registration.
Definition eventSystem.hpp:194
Full EventSystem implementation for reverse tapes.
Definition eventSystem.hpp:329
typename Tape::Identifier Identifier
Identifier type used by the tape.
Definition eventSystem.hpp:334
T_Tape Tape
See EventSystem.
Definition eventSystem.hpp:331
static Handle registerTapeStopRecordingListener(void(*callback)(Tape &, void *), void *customData=nullptr)
Register callbacks for TapeStopRecording events.
Definition eventSystem.hpp:383
static void notifyIndexAssignListeners(Index const &index)
Invoke callbacks for IndexAssign events.
Definition eventSystem.hpp:778
static Handle registerIndexAssignListener(void(*callback)(Index const &, void *), void *customData=nullptr)
Register callbacks for IndexAssign events.
Definition eventSystem.hpp:764
static Handle registerIndexFreeListener(void(*callback)(Index const &, void *), void *customData=nullptr)
Register callbacks for IndexFree events.
Definition eventSystem.hpp:791
typename Base::Event Event
See EventSystemBase.
Definition eventSystem.hpp:341
static Handle registerTapeRegisterInputListener(void(*callback)(Tape &, Real &, Identifier &, void *), void *customData=nullptr)
Register callbacks for TapeRegisterInput events.
Definition eventSystem.hpp:409
static Handle registerStatementEvaluatePrimalListener(void(*callback)(Tape &, Identifier const &, Real const &, void *), void *customData=nullptr)
Register callbacks for StatementEvaluatePrimal events.
Definition eventSystem.hpp:726
static Handle registerPreaccAddOutputListener(void(*callback)(Tape &, Real &, Identifier &, void *), void *customData=nullptr)
Register callbacks for PreaccAddOutput events.
Definition eventSystem.hpp:621
static void 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 void notifyTapeRegisterOutputListeners(Tape &tape, Real &value, Identifier &identifier)
Invoke callbacks for TapeRegisterOutput events.
Definition eventSystem.hpp:453
static Handle registerPreaccFinishListener(void(*callback)(Tape &, void *), void *customData=nullptr)
Register callbacks for PreaccFinish events.
Definition eventSystem.hpp:565
static void notifyTapeResetListeners(Tape &tape, Position const &position, EventHints::Reset kind, bool clearAdjoints)
Invoke callbacks for TapeReset events.
Definition eventSystem.hpp:521
static void notifyIndexFreeListeners(Index const &index)
Invoke callbacks for IndexFree events.
Definition eventSystem.hpp:806
static Handle registerStatementEvaluateListener(void(*callback)(Tape &, Identifier const &, size_t, Real const *, void *), void *customData=nullptr)
Register callbacks for StatementEvaluate events.
Definition eventSystem.hpp:694
static void notifyPreaccFinishListeners(Tape &tape)
Invoke callbacks for PreaccFinish events.
Definition eventSystem.hpp:579
static Handle registerIndexCopyListener(void(*callback)(Index const &, void *), void *customData=nullptr)
Register callbacks for IndexCopy events.
Definition eventSystem.hpp:819
static void notifyTapeStopRecordingListeners(Tape &tape)
Invoke callbacks for TapeStopRecording events.
Definition eventSystem.hpp:396
static Handle registerTapeStartRecordingListener(void(*callback)(Tape &, void *), void *customData=nullptr)
Register callbacks for TapeStartRecording events.
Definition eventSystem.hpp:356
static void notifyStatementEvaluatePrimalListeners(Tape &tape, Identifier const &lhsIdentifier, Real const &lhsValue)
Invoke callbacks for StatementEvaluatePrimal events.
Definition eventSystem.hpp:745
static Handle registerTapeRegisterOutputListener(void(*callback)(Tape &, Real &, Identifier &, void *), void *customData=nullptr)
Register callbacks for TapeRegisterOutput events.
Definition eventSystem.hpp:437
static void notifyIndexCopyListeners(Index const &index)
Invoke callbacks for IndexCopy events.
Definition eventSystem.hpp:833
typename Tape::Gradient Gradient
Gradient type used by the tape.
Definition eventSystem.hpp:333
static Handle registerTapeResetListener(void(*callback)(Tape &, Position const &, EventHints::Reset, bool, void *), void *customData=nullptr)
Register callbacks for TapeReset events.
Definition eventSystem.hpp:502
static void 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 void notifyPreaccAddOutputListeners(Tape &tape, Real &value, Identifier &identifier)
Invoke callbacks for PreaccAddOutput events.
Definition eventSystem.hpp:636
static Handle 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
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 void notifyPreaccAddInputListeners(Tape &tape, Real const &value, Identifier const &identifier)
Invoke callbacks for PreaccAddInput events.
Definition eventSystem.hpp:607
static void notifyPreaccStartListeners(Tape &tape)
Invoke callbacks for PreaccStart events.
Definition eventSystem.hpp:553
static void notifyStatementEvaluateListeners(Tape &tape, Identifier const &lhsIdentifier, size_t sizeLhsAdjoint, Real const *lhsAdjoint)
Invoke callbacks for StatementEvaluate events.
Definition eventSystem.hpp:712
static void notifyTapeStartRecordingListeners(Tape &tape)
Invoke callbacks for TapeStartRecording events.
Definition eventSystem.hpp:370
static void notifyTapeRegisterInputListeners(Tape &tape, Real &value, Identifier &identifier)
Invoke callbacks for TapeRegisterInput events.
Definition eventSystem.hpp:424
static Handle 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 Handle registerPreaccStartListener(void(*callback)(Tape &, void *), void *customData=nullptr)
Register callbacks for PreaccStart events.
Definition eventSystem.hpp:540
typename Tape::Position Position
Definition eventSystem.hpp:336
typename Tape::Identifier Index
Index type used by the tape.
Definition eventSystem.hpp:335
static Handle registerPreaccAddInputListener(void(*callback)(Tape &, Real const &, Identifier const &, void *), void *customData=nullptr)
Register callbacks for PreaccAddInput events.
Definition eventSystem.hpp:591
Implementation of a tape-free forward AD mode through the internal expression interfaces.
Definition forwardEvaluation.hpp:72