37#include "../../../config.h"
38#include "../../../expressions/lhsExpressionInterface.hpp"
39#include "../../../misc/byteDataView.hpp"
40#include "../../../misc/macros.hpp"
41#include "../../../misc/temporaryMemory.hpp"
42#include "../../../tapes/data/position.hpp"
43#include "../../../tapes/interfaces/fullTapeInterface.hpp"
44#include "../storeAndRestoreActions.hpp"
60 template<
typename T_Real,
typename T_Identifier,
typename T_Gradient>
82 template<
typename T_Real,
typename T_Identifier,
typename T_Gradient>
115 template<
typename T_Po
interStore>
120 using Real =
typename PointerStore::Real;
203 template<
typename T_T,
typename =
void>
205 using T =
CODI_DD(T_T, CODI_DEFAULT_LHS_EXPRESSION);
275#ifndef DOXYGEN_DISABLE
278 template<
typename T_T>
280 using T =
CODI_DD(T_T, CODI_DEFAULT_LHS_EXPRESSION);
284 using Real =
typename PointerTraits::Real;
285 using Identifier =
typename PointerTraits::Identifier;
286 using Gradient =
typename PointerTraits::Gradient;
295 return PointerTraits::countSize(&value, 1, actions);
303 PointerTraits::restore(
store, allocator, 1, actions, data.base);
307 CODI_INLINE static void store(ByteDataView* dataStore, TemporaryMemory& allocator,
T const& value,
size_t size,
311 PointerTraits::store(dataStore, allocator, value, 1, actions, data.base);
318 return PointerTraits::isActive(&value, 1);
326 PointerTraits::setExternalFunctionOutput(tapeActive, &value, 1, &identifier, &primal, &oldPrimals);
331 Identifier& identifier,
Real& primal) {
334 PointerTraits::getPrimalsFromVector(data, 1, &identifier, &primal);
339 Identifier& identifier,
Real& primal) {
342 PointerTraits::setPrimalsIntoVector(data, 1, &identifier, &primal);
347 Identifier& identifier, Gradient& gradient,
size_t dim) {
350 PointerTraits::getGradients(data, 1, reset & identifier, &gradient, dim);
355 Identifier& identifier, Gradient& gradient,
size_t dim) {
358 PointerTraits::setGradients(data, 1, update, &identifier, &gradient, dim);
363 template<
typename T_T>
364 struct ActiveArgumentStoreTraits<T_T*, ExpressionTraits::EnableIfLhsExpression<T_T>> {
365 using T =
CODI_DD(T_T, CODI_DEFAULT_LHS_EXPRESSION);
367 using Tape =
typename T::Tape;
369 using Real =
typename T::Real;
370 using Identifier =
typename T::Identifier;
371 using Gradient =
Real;
374 using ArgumentStore = ActiveArgumentPointerStore<Real, Identifier, Gradient>;
381 if (actions.test(StoreAction::InputIdentifierCreateAndStore)) {
382 dataSize += size *
sizeof(
typename T::Identifier);
384 if (actions.test(StoreAction::PrimalCreateOnTape)) {
385 if (Tape::HasPrimalValues) {
387 int passiveIdentifiers = countPassive(value, size);
388 dataSize +=
sizeof(int);
389 dataSize += passiveIdentifiers *
sizeof(
typename T::Real);
392 dataSize += size *
sizeof(
typename T::Real);
395 if (actions.test(StoreAction::OutputIdentifierCreate)) {
396 dataSize += size *
sizeof(
typename T::Identifier);
398 if (Tape::HasPrimalValues && Tape::HasPrimalValues && !Tape::LinearIndexHandling) {
399 dataSize += size *
sizeof(
Real);
407 CODI_INLINE static void restore(ByteDataView* store, TemporaryMemory& allocator,
size_t size,
409 Real* passiveValues =
nullptr;
411 if (Tape::HasPrimalValues && actions.test(RestoreAction::PrimalRestore)) {
412 data.passiveValuesCount = store->template read<int>();
415 if (actions.test(RestoreAction::PrimalRestore)) {
416 restoreValue(store, allocator, size, data, passiveValues);
418 if (actions.test(RestoreAction::InputIdentifierRestore)) {
419 data.value_i_in = store->template read<Identifier>(size);
421 if (actions.test(RestoreAction::OutputIdentifierRestore)) {
422 data.value_i_out = store->template read<Identifier>(size);
423 if (Tape::HasPrimalValues && !Tape::LinearIndexHandling) {
424 data.oldPrimals = store->template read<Real>(size);
428 if (actions.test(RestoreAction::PrimalRestore)) {
429 restorePassiveValues(size, data, passiveValues);
432 if (actions.test(RestoreAction::PrimalCreate)) {
433 data.value_v = allocator.template alloc<Real>(size);
436 if (actions.test(RestoreAction::InputGradientCreate)) {
437 data.value_deriv_in = allocator.template alloc<Gradient>(size);
439 if (actions.test(RestoreAction::OutputGradientCreate)) {
440 data.value_deriv_out = allocator.template alloc<Gradient>(size);
445 CODI_INLINE static void restoreValue(ByteDataView* store, TemporaryMemory& allocator,
size_t size,
446 ArgumentStore& data,
Real*& passiveValues) {
447 if (Tape::HasPrimalValues) {
449 passiveValues = store->template read<Real>(data.passiveValuesCount);
450 if ((
size_t)data.passiveValuesCount == size) {
451 data.value_v = passiveValues;
454 data.value_v = allocator.template alloc<Real>(size);
460 data.value_v = store->template read<Real>(size);
465 CODI_INLINE static void restorePassiveValues(
size_t size, ArgumentStore& data,
Real* passiveValues) {
466 typename T::Tape& tape = T::getTape();
468 if (Tape::HasPrimalValues && (
size_t)data.passiveValuesCount != size) {
471 for (
size_t i = 0; i < size; i += 1) {
472 if (!tape.isIdentifierActive(data.value_i_in[i])) {
473 data.value_v[i] = passiveValues[passivePos];
481 CODI_INLINE static void store(ByteDataView* dataStore, TemporaryMemory& allocator, T
const* value,
size_t size,
483 if (dataStore ==
nullptr) {
484 codiAssert(!actions.test(StoreAction::PrimalCreateOnTape) &&
485 !actions.test(StoreAction::InputIdentifierCreateAndStore) &&
486 !actions.test(StoreAction::OutputIdentifierCreate));
489 Real* passiveValues =
nullptr;
490 if (actions.test(StoreAction::PrimalCreateOnTape)) {
491 if (Tape::HasPrimalValues) {
492 int passiveIdentifiers = countPassive(value, size);
493 dataStore->write(passiveIdentifiers);
494 passiveValues = dataStore->template reserve<Real>(passiveIdentifiers);
495 data.value_v = allocator.template alloc<Real>(size);
498 data.value_v = dataStore->template reserve<Real>(size);
502 data.value_v = allocator.template alloc<Real>(size);
505 if (actions.test(StoreAction::PrimalExtract)) {
506 typename T::Tape& tape = T::getTape();
507 int passiveValuesPos = 0;
509 for (
size_t i = 0; i < size; i += 1) {
510 (data.value_v)[i] = value[i].
getValue();
512 if (actions.test(StoreAction::PrimalCreateOnTape) && Tape::HasPrimalValues &&
514 passiveValues[passiveValuesPos] = value[i].
getValue();
515 passiveValuesPos += 1;
520 if (actions.test(StoreAction::InputIdentifierCreateAndStore)) {
521 data.value_i_in = dataStore->template reserve<Identifier>(size);
522 for (
size_t i = 0; i < size; i += 1) {
523 data.value_i_in[i] = value[i].getIdentifier();
527 if (actions.test(StoreAction::OutputIdentifierCreate)) {
528 data.value_i_out = dataStore->template reserve<Identifier>(size);
529 for (
size_t i = 0; i < size; i += 1) {
530 data.value_i_out[i] = -1;
533 if (Tape::HasPrimalValues && !Tape::LinearIndexHandling) {
534 data.oldPrimals = dataStore->template reserve<Real>(size);
540 CODI_INLINE static bool isActive(T
const* value,
size_t size) {
541 typename T::Tape& tape = T::getTape();
543 for (
size_t i = 0; i < size && !active; i += 1) {
550 CODI_INLINE static void setExternalFunctionOutput(
bool tapeActive, T* value,
size_t size, Identifier* identifier,
552 typename T::Tape& tape = T::getTape();
553 for (
size_t i = 0; i < size; i += 1) {
554 value[i].setValue(primal[i]);
556 if (tapeActive && 0 != identifier[i]) {
557 Real oldValue = tape.registerExternalFunctionOutput(value[i]);
560 if (Tape::HasPrimalValues && !Tape::LinearIndexHandling) {
561 oldPrimals[i] = oldValue;
568 CODI_INLINE static void getPrimalsFromVector(VectorAccessInterface<Real, Identifier>* data,
size_t size,
569 Identifier* identifier,
Real* primal) {
570 typename T::Tape& tape = T::getTape();
572 for (
size_t i = 0; i < size; i += 1) {
573 if (tape.isIdentifierActive(identifier[i])) {
574 primal[i] = data->getPrimal(identifier[i]);
580 CODI_INLINE static void setPrimalsIntoVector(VectorAccessInterface<Real, Identifier>* data,
size_t size,
581 Identifier* identifier,
Real* primal) {
582 typename T::Tape& tape = T::getTape();
584 for (
size_t i = 0; i < size; i += 1) {
585 if (tape.isIdentifierActive(identifier[i])) {
586 data->setPrimal(identifier[i], primal[i]);
592 CODI_INLINE static void getGradients(VectorAccessInterface<Real, Identifier>* data,
size_t size,
bool reset,
593 Identifier* identifier, Gradient* gradient,
size_t dim) {
594 for (
size_t i = 0; i < size; i += 1) {
595 gradient[i] = data->getAdjoint(identifier[i], dim);
597 data->resetAdjoint(identifier[i], dim);
603 CODI_INLINE static void setGradients(VectorAccessInterface<Real, Identifier>* data,
size_t size,
bool update,
604 Identifier* identifier,
Real* gradient,
size_t dim) {
605 for (
size_t i = 0; i < size; i += 1) {
607 data->resetAdjoint(identifier[i], dim);
609 data->updateAdjoint(identifier[i], dim, gradient[i]);
614 CODI_INLINE static int countPassive(T
const* value,
size_t size) {
615 typename T::Tape& tape = T::getTape();
618 for (
size_t i = 0; i < size; i += 1) {
619 if (!tape.isIdentifierActive(value[i].getIdentifier())) {
#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_UNUSED_ARG(arg)
Used in a constexpr context, where using CODI_UNUSED spoils the constexpr.
Definition: macros.hpp:49
#define CODI_T(...)
Abbreviation for CODI_TEMPLATE.
Definition: macros.hpp:111
DataExtraction< Type >::Identifier getIdentifier(Type const &v)
Extract the identifiers from a type of aggregated active types.
Definition: realTraits.hpp:216
DataExtraction< Type >::Real getValue(Type const &v)
Extract the primal values from a type of aggregated active types.
Definition: realTraits.hpp:210
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
EnumBitset< StoreAction > StoreActions
Action set for a variable during a store. See Store actions for a detailed description.
Definition: storeAndRestoreActions.hpp:71
EnumBitset< RestoreAction > RestoreActions
Action set for a variable during a store. See Restore actions for a detailed description.
Definition: storeAndRestoreActions.hpp:56
Declares all variables that may be needed to store/restore an active argument which has a pointer typ...
Definition: activeArgumentStoreTraits.hpp:83
Real * primal()
Get the primal values.
Definition: activeArgumentStoreTraits.hpp:100
T_Real Real
See ActiveArgumentPointerStore.
Definition: activeArgumentStoreTraits.hpp:85
Identifier * identifierIn()
Get the input identifiers.
Definition: activeArgumentStoreTraits.hpp:101
T_Gradient Gradient
See ActiveArgumentPointerStore.
Definition: activeArgumentStoreTraits.hpp:87
Gradient * value_deriv_out
Gradient vector of an output argument.
Definition: activeArgumentStoreTraits.hpp:93
Gradient * value_deriv_in
Gradient vector of an input argument.
Definition: activeArgumentStoreTraits.hpp:92
Identifier * value_i_out
Identifier vector of an output argument.
Definition: activeArgumentStoreTraits.hpp:91
Gradient * gradientIn()
Get the input gradients.
Definition: activeArgumentStoreTraits.hpp:103
int passiveValuesCount
Number of passive values.
Definition: activeArgumentStoreTraits.hpp:97
Identifier * value_i_in
Identifier vector of an input argument.
Definition: activeArgumentStoreTraits.hpp:90
Identifier * identifierOut()
Get the output identifiers.
Definition: activeArgumentStoreTraits.hpp:102
Real * value_v
Primal value vector.
Definition: activeArgumentStoreTraits.hpp:89
Real * oldPrimal()
Get old primal values.
Definition: activeArgumentStoreTraits.hpp:106
Real * oldPrimals
Old primal values in primal value tape setting.
Definition: activeArgumentStoreTraits.hpp:95
Gradient * gradientOut()
Get the output gradients.
Definition: activeArgumentStoreTraits.hpp:104
T_Identifier Identifier
See ActiveArgumentPointerStore.
Definition: activeArgumentStoreTraits.hpp:86
Interface for restored data for an argument. The functions should return a compatible type that can b...
Definition: activeArgumentStoreTraits.hpp:61
virtual Real primal()=0
Get the primal values.
virtual Identifier identifierOut()=0
Get the output identifiers.
T_Identifier Identifier
See ActiveArgumentStoreInterface.
Definition: activeArgumentStoreTraits.hpp:63
virtual Gradient gradientOut()=0
Get the output gradients.
T_Gradient Gradient
See ActiveArgumentStoreInterface.
Definition: activeArgumentStoreTraits.hpp:64
T_Real Real
See ActiveArgumentStoreInterface.
Definition: activeArgumentStoreTraits.hpp:62
virtual Gradient gradientIn()=0
Get the input gradients.
virtual Identifier identifierIn()=0
Get the input identifiers.
virtual Real oldPrimal()=0
Get old primal values.
Traits for storing active arguments in byte streams.
Definition: activeArgumentStoreTraits.hpp:204
static void store(ByteDataView *dataStore, TemporaryMemory &allocator, T const &value, size_t size, StoreActions const &actions, ArgumentStore &data)
Store all data for this type.
double Real
The type with no CoDiPack values.
Definition: activeArgumentStoreTraits.hpp:207
static void getGradients(VectorAccessInterface< Real, Identifier > *data, size_t size, bool reset, Identifier &identifier, Gradient &gradient, size_t dim)
Get the gradients from data and store them in gradient.
ActiveArgumentStoreInterface< Real, Identifier, Gradient > ArgumentStore
Data for holding all necessary values.
Definition: activeArgumentStoreTraits.hpp:212
static bool isActive(T const &value, size_t size)
Should return true when one element in the type is active.
int Identifier
The type for holding the identifiers.
Definition: activeArgumentStoreTraits.hpp:208
static void restore(ByteDataView *store, TemporaryMemory &allocator, size_t size, RestoreActions const &actions, ArgumentStore &data)
Restore the data for this type.
static void setPrimalsIntoVector(VectorAccessInterface< Real, Identifier > *data, size_t size, Identifier &identifier, Real &primal)
Extract the primal values from primal and store them in data.
static void setGradients(VectorAccessInterface< Real, Identifier > *data, size_t size, bool update, Identifier &identifier, Gradient &gradient, size_t dim)
Extract the gradients from gradient and store them in data.
static size_t countSize(T const &value, size_t size, StoreActions const &actions)
Counts the binary size for the data stream.
Real Gradient
The type that can represent the gradient values.
Definition: activeArgumentStoreTraits.hpp:209
T_T T
See ActiveArgumentStoreTraits.
Definition: activeArgumentStoreTraits.hpp:205
static void setExternalFunctionOutput(bool tapeActive, T &value, size_t size, Identifier &identifier, Real &primal, Real &oldPrimals)
static void getPrimalsFromVector(VectorAccessInterface< Real, Identifier > *data, size_t size, Identifier &identifier, Real &primal)
Get the primal values from data and store them in primal.
Declares all variables that may be needed to store/restore an active argument which has a value type.
Definition: activeArgumentStoreTraits.hpp:116
typename PointerStore::Identifier Identifier
See ActiveArgumentPointerStore.
Definition: activeArgumentStoreTraits.hpp:121
Identifier & identifierIn()
Get the input identifiers.
Definition: activeArgumentStoreTraits.hpp:128
PointerStore base
Declaration of base.
Definition: activeArgumentStoreTraits.hpp:124
typename PointerStore::Real Real
See ActiveArgumentPointerStore.
Definition: activeArgumentStoreTraits.hpp:120
Gradient & gradientOut()
Get the output gradients.
Definition: activeArgumentStoreTraits.hpp:131
Real & primal()
Get the primal values.
Definition: activeArgumentStoreTraits.hpp:127
Identifier & identifierOut()
Get the output identifiers.
Definition: activeArgumentStoreTraits.hpp:129
Real & oldPrimal()
Get old primal values.
Definition: activeArgumentStoreTraits.hpp:133
T_PointerStore PointerStore
See ActiveArgumentValueStore.
Definition: activeArgumentStoreTraits.hpp:118
Gradient & gradientIn()
Get the input gradients.
Definition: activeArgumentStoreTraits.hpp:130
typename PointerStore::Gradient Gradient
See ActiveArgumentPointerStore.
Definition: activeArgumentStoreTraits.hpp:122
Identifier & getIdentifier()
Definition: activeTypeBase.hpp:156
Represents a concrete lvalue in the CoDiPack expression tree.
Definition: activeType.hpp:52
Definition: byteDataView.hpp:51
A bitset with enum items as flags.
Definition: enumBitset.hpp:64
Real const & getValue() const
Get the primal value of this lvalue.
Definition: lhsExpressionInterface.hpp:125
Allocator for temporary used memory.
Definition: temporaryMemory.hpp:54
Unified access to the adjoint vector and primal vector in a tape evaluation.
Definition: vectorAccessInterface.hpp:91