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())) {