CoDiPack  3.1.0
A Code Differentiation Package
SciComp TU Kaiserslautern
Loading...
Searching...
No Matches
primalValueBaseTape.hpp
1/*
2 * CoDiPack, a Code Differentiation Package
3 *
4 * Copyright (C) 2015-2026 Chair for Scientific Computing (SciComp), University of Kaiserslautern-Landau
5 * Homepage: http://scicomp.rptu.de
6 * Contact: Prof. Nicolas R. Gauger (codi@scicomp.uni-kl.de)
7 *
8 * Lead developers: Max Sagebaum, Johannes Blühdorn (SciComp, University of Kaiserslautern-Landau)
9 *
10 * This file is part of CoDiPack (http://scicomp.rptu.de/software/codi).
11 *
12 * CoDiPack is free software: you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation, either version 3 of the
15 * License, or (at your option) any later version.
16 *
17 * CoDiPack is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty
19 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
20 *
21 * See the GNU General Public License for more details.
22 * You should have received a copy of the GNU
23 * General Public License along with CoDiPack.
24 * If not, see <http://www.gnu.org/licenses/>.
25 *
26 * For other licensing options please contact us.
27 *
28 * Authors:
29 * - SciComp, University of Kaiserslautern-Landau:
30 * - Max Sagebaum
31 * - Johannes Blühdorn
32 * - Former members:
33 * - Tim Albring
34 */
35#pragma once
36
37#include <algorithm>
38#include <cmath>
39#include <functional>
40#include <type_traits>
41#include <utility>
42
43#include "../config.h"
44#include "../expressions/aggregate/aggregatedActiveType.hpp"
45#include "../expressions/aggregate/arrayAccessExpression.hpp"
46#include "../expressions/emptyExpression.hpp"
47#include "../expressions/lhsExpressionInterface.hpp"
48#include "../expressions/logic/compileTimeTraversalLogic.hpp"
49#include "../expressions/logic/constructStaticContext.hpp"
50#include "../expressions/logic/helpers/forEachLeafLogic.hpp"
51#include "../expressions/logic/helpers/jacobianComputationLogic.hpp"
52#include "../expressions/logic/helpers/mathStatementGenLogic.hpp"
53#include "../expressions/logic/traversalLogic.hpp"
54#include "../misc/demangleName.hpp"
55#include "../misc/macros.hpp"
56#include "../misc/mathUtility.hpp"
57#include "../misc/memberStore.hpp"
58#include "../traits/expressionTraits.hpp"
59#include "commonTapeImplementation.hpp"
60#include "data/chunk.hpp"
61#include "data/chunkedData.hpp"
62#include "indices/indexManagerInterface.hpp"
63#include "misc/assignStatement.hpp"
64#include "misc/primalAdjointVectorAccess.hpp"
65#include "statementEvaluators/statementEvaluatorInterface.hpp"
66#include "statementEvaluators/statementEvaluatorTapeInterface.hpp"
68namespace codi {
69
80 template<typename T_Real, typename T_Gradient, typename T_IndexManager, typename T_StatementEvaluator,
81 template<typename, typename> class T_Data>
83 public:
84
85 using Real = CODI_DD(T_Real, double);
86 using Gradient = CODI_DD(T_Gradient, double);
88 using StatementEvaluator = CODI_DD(CODI_T(T_StatementEvaluator),
90 template<typename Chunk, typename Nested>
91 using Data = CODI_DD(CODI_T(T_Data<Chunk, Nested>),
93
94 using Identifier = typename IndexManager::Index;
96 typename IndexManager::ActiveTypeIndexData;
98
99 constexpr static bool IsLinearIndexHandler = IndexManager::IsLinear;
100 constexpr static bool IsStaticIndexHandler =
101 IndexManager::NeedsStaticStorage;
102
103 using EvalHandle = typename StatementEvaluator::Handle;
104
108
111
113 };
114
131 template<typename T_TapeTypes, typename T_Impl>
132 struct PrimalValueBaseTape : public CommonTapeImplementation<T_TapeTypes, T_Impl>,
135 public:
136
138 using TapeTypes = CODI_DD(T_TapeTypes,
143
145 friend Base;
146
147 using Real = typename TapeTypes::Real;
148 using Gradient = typename TapeTypes::Gradient;
149 using IndexManager = typename TapeTypes::IndexManager;
150 using StatementEvaluator = typename TapeTypes::StatementEvaluator;
151 using Identifier = typename TapeTypes::Identifier;
152 using ActiveTypeTapeData = typename TapeTypes::ActiveTypeTapeData;
153
154 using EvalHandle = typename TapeTypes::EvalHandle;
155
156 using StatementData = typename TapeTypes::StatementData;
157 using StatementByteData = typename TapeTypes::StatementByteData;
158
160
161 using NestedPosition = typename StatementData::Position;
162 using Position = typename Base::Position;
163
165 template<typename AdjointVector>
167
168 static bool constexpr AllowJacobianOptimization = false;
169 static bool constexpr HasPrimalValues = true;
170 static bool constexpr LinearIndexHandling =
171 TapeTypes::IsLinearIndexHandler;
172 static bool constexpr RequiresPrimalRestore =
173 !TapeTypes::IsLinearIndexHandler;
174
176 template<typename T>
177 using StackArray = std::array<T, Config::MaxArgumentSize>;
178
179 protected:
180
183
187
188 std::vector<Gradient> adjoints;
189 std::vector<Real> primals;
190 std::vector<Real> primalsCopy;
191
196
197 private:
198
199 CODI_INLINE Impl const& cast() const {
200 return static_cast<Impl const&>(*this);
201 }
202
203 CODI_INLINE Impl& cast() {
204 return static_cast<Impl&>(*this);
205 }
206
207 protected:
208
209 /*******************************************************************************/
212
214 template<typename... Args>
215 static void internalEvaluateForward_EvalStatements(Args&&... args);
216
218 template<typename... Args>
219 static void internalEvaluatePrimal_EvalStatements(Args&&... args);
220
222 template<typename... Args>
223 static void internalEvaluateReverse_EvalStatements(Args&&... args);
224
227
229
230 public:
231
234 : Base(),
235 indexManager(Config::MaxArgumentSize), // Reserve first items for passive values.
236 statementData(Config::ChunkSize),
237 statementByteData(Config::ByteDataChunkSize),
238 adjoints(1), // Ensure that adjoint[0] exists, see its use in gradient() const.
239 primals(0),
240 primalsCopy(0) {
241 checkPrimalSize(true);
242
243 statementData.setNested(&indexManager.get());
245
247
253 }
254
255 /*******************************************************************************/
258
264 if (AdjointsManagement::Automatic == adjointsManagement) {
265 checkAdjointSize(identifier);
266 }
267
268 codiAssert(identifier < (Identifier)adjoints.size());
269
270 return adjoints[identifier];
271 }
272
277 Identifier const& identifier, AdjointsManagement adjointsManagement = AdjointsManagement::Automatic) const {
278 codiAssert(identifier < (Identifier)adjoints.size());
279
280 if (AdjointsManagement::Automatic == adjointsManagement && identifier >= (Identifier)adjoints.size()) {
281 return adjoints[0];
282 } else {
283 return adjoints[identifier];
284 }
285 }
286
288 /*******************************************************************************/
291
293 template<typename Real>
295 CODI_UNUSED(value);
296
297 indexManager.get().initIndex(data);
298 }
299
301 template<typename Real>
303 CODI_UNUSED(value);
304
305 indexManager.get().template freeIndex<Impl>(data);
306 }
307
309
317
320 template<typename T>
321 CODI_INLINE void push(T* CODI_RESTRICT& array, T const& value) {
322 *array = value;
323 array += 1;
324 }
325
327 CODI_INLINE void populate(size_t lhsSize, size_t rhsSize, size_t passiveSize, size_t constantSize,
328 char* byteData) {
329 size_t reserverLhsSize = LinearIndexHandling ? 0 : lhsSize;
330
331 char* curPos = byteData;
332
333 rhsIdentifiers = reinterpret_cast<Identifier*>(curPos);
334 curPos += sizeof(Identifier) * rhsSize;
335
336 lhsIdentifiers = reinterpret_cast<Identifier*>(curPos);
337 curPos += sizeof(Identifier) * reserverLhsSize;
338
339 passiveValues = reinterpret_cast<Real*>(curPos);
340 curPos += sizeof(Real) * passiveSize;
341
342 oldLhsValues = reinterpret_cast<Real*>(curPos);
343 curPos += sizeof(Real) * reserverLhsSize;
344
345 constantValues = reinterpret_cast<PassiveReal*>(curPos);
346 curPos += sizeof(PassiveReal) * constantSize;
347
348 codiAssert(curPos == byteData + computeSize(lhsSize, rhsSize, passiveSize, constantSize));
349 }
350
352 CODI_INLINE Config::LowLevelFunctionDataSize computeSize(size_t lhsSize, size_t rhsSize, size_t passiveSize,
353 size_t constantSize) {
354 size_t reserverLhsSize = LinearIndexHandling ? 0 : lhsSize;
355
356 return sizeof(Identifier) * (rhsSize + reserverLhsSize) + sizeof(Real) * (passiveSize + reserverLhsSize) +
357 sizeof(PassiveReal) * (constantSize);
358 }
359 };
360
361 protected:
362
364 CODI_INLINE void pushLhsData(Identifier const& index, Real const& oldPrimalValue,
365 StatementDataPointers& pointers) {
366 if (!LinearIndexHandling) {
367 pointers.push(pointers.lhsIdentifiers, index);
368 pointers.push(pointers.oldLhsValues, oldPrimalValue);
369 }
370 }
371
374 size_t passiveSize, size_t constantSize,
375 StatementDataPointers& pointers) {
376 Config::LowLevelFunctionDataSize byteSize = pointers.computeSize(lhsSize, rhsSize, passiveSize, constantSize);
377 statementData.reserveItems(1);
378 statementByteData.reserveItems(byteSize);
379
380 char* byteData = nullptr;
381 statementByteData.getDataPointers(byteData);
382 statementByteData.addDataSize(byteSize);
383
384 pointers.populate(lhsSize, rhsSize, passiveSize, constantSize, byteData);
385
386 return byteSize;
387 }
388
390 template<typename Lhs, typename Rhs>
392 StatementDataPointers& pointers) {
393 size_t constexpr MaxActiveArgs = ExpressionTraits::NumberOfActiveTypeArguments<Rhs>::value;
394 size_t constexpr MaxConstantArgs = ExpressionTraits::NumberOfConstantTypeArguments<Rhs>::value;
395 size_t constexpr MaxOutputArgs = ExpressionTraits::NumberOfActiveTypeArguments<Lhs>::value;
396
397 return reserveStmtDataManual(MaxOutputArgs, MaxActiveArgs, MaxActiveArgs - activeArguments, MaxConstantArgs,
398 pointers);
399 }
400
402 struct CountActiveArguments : public ForEachLeafLogic<CountActiveArguments> {
403 public:
404
406 template<typename Node>
407 CODI_INLINE void handleActive(Node const& node, size_t& numberOfActiveArguments, IndexManager& indexManager) {
408 indexManager.validateRhsIndex(node.getTapeData());
409
410 if (CODI_ENABLE_CHECK(Config::CheckZeroIndex, IndexManager::InactiveIndex != node.getIdentifier())) {
411 numberOfActiveArguments += 1;
412 }
413 }
414 };
415
417 struct PushIdentifierPassiveAndConstant : public ForEachLeafLogic<PushIdentifierPassiveAndConstant> {
418 public:
419
421 template<typename Node>
422 CODI_INLINE void handleActive(Node const& node, StatementDataPointers& pointers, size_t& curPassiveArgument) {
423 Identifier rhsIndex = node.getIdentifier();
424 if (CODI_ENABLE_CHECK(Config::CheckZeroIndex, IndexManager::InactiveIndex == rhsIndex)) {
425 rhsIndex = curPassiveArgument;
426
427 curPassiveArgument += 1;
428 pointers.push(pointers.passiveValues, node.getValue());
429 }
430
431 pointers.push(pointers.rhsIdentifiers, rhsIndex);
432 }
433
435 template<typename Node>
437 size_t& curPassiveArgument) {
438 CODI_UNUSED(curPassiveArgument);
439
441 using ConversionOperator = typename Node::template ConversionOperator<PassiveReal>;
442
443 typename Node::Real v = node.getValue();
444
446 pointers.push(pointers.constantValues,
447 ConversionOperator::toDataStore(AggregatedTraits::template arrayAccess<i.value>(v)));
448 });
449 }
450 };
451
456 template<typename Lhs, typename RhsType, typename Rhs>
458 StatementDataPointers& pointers) {
459 using Stmt = AssignStatement<Lhs, Rhs>;
460
461 CountActiveArguments countActiveArguments;
462 PushIdentifierPassiveAndConstant pushStatement;
463
467
468 size_t activeArguments = 0;
469 countActiveArguments.eval(rhs.cast(), activeArguments, indexManager.get());
470
471 if (CODI_ENABLE_CHECK(Config::CheckEmptyStatements, 0 != activeArguments)) {
472 Config::LowLevelFunctionDataSize byteSize = reserveStmtData<Lhs, Rhs>(activeArguments, pointers);
473
474 size_t passiveArguments = 0;
475 pushStatement.eval(rhs.cast(), pointers, passiveArguments);
476 statementData.pushData((Config::ArgumentSize)passiveArguments,
477 StatementEvaluator::template createHandle<Impl, Impl, Stmt>(), byteSize);
478
479 return true;
480 } else {
481 return false;
482 }
483 }
484
486 struct JacobianExtractionLogic : public JacobianComputationLogic<JacobianExtractionLogic> {
487 private:
488 size_t pos;
489
490 public:
491
494
496 template<typename Node, typename Jacobian>
497 CODI_INLINE void handleJacobianOnActive(Node const& node, Jacobian jacobianExpr, Identifier* rhsIdentifiers,
498 Real* jacobians) {
499 rhsIdentifiers[pos] = node.getIdentifier();
500 jacobians[pos] = jacobianExpr;
501 pos++;
502 }
503 };
504
505 public:
506
511 template<typename Aggregated, typename Type, typename Lhs, typename Rhs>
514 using AggregatedTraits = RealTraits::AggregatedTypeTraits<Aggregated>;
515 int constexpr Elements = AggregatedTraits::Elements;
516
517 bool primalStored = false;
518 StatementDataPointers pointers = {};
519
520 if (storeArgumentAndStmtData<Lhs>(rhs, pointers)) {
521 bool generatedNewIndex = false;
523 generatedNewIndex |= indexManager.get().template assignIndex<Impl>(lhs.values[i.value].getTapeData());
524 });
525 checkPrimalSize(generatedNewIndex);
526
527 Aggregated real = rhs.cast().getValue();
529 Identifier lhsIdentifier = lhs.values[i.value].getIdentifier();
530 Real& primalEntry = primals[lhsIdentifier];
531
532 pushLhsData(lhsIdentifier, primalEntry, pointers);
533
535 size_t constexpr MaxActiveArgs = ExpressionTraits::NumberOfActiveTypeArguments<Rhs>::value;
536
537 JacobianExtractionLogic getRhsIdentifiersAndJacobians;
538 std::array<Identifier, MaxActiveArgs> rhsIdentifiers;
539 std::array<Real, MaxActiveArgs> jacobians;
540 getRhsIdentifiersAndJacobians.eval(ArrayAccessExpression<Aggregated, i.value, Rhs>(rhs), Real(1.0),
541 rhsIdentifiers.data(), jacobians.data());
542
544 cast(), lhsIdentifier, AggregatedTraits::template arrayAccess<i.value>(real), MaxActiveArgs,
545 rhsIdentifiers.data(), jacobians.data());
546 }
547
548 lhs.values[i.value].value() = AggregatedTraits::template arrayAccess<i.value>(real);
549 primalEntry = lhs.values[i.value].getValue();
550 });
551
552 primalStored = true;
553 }
554
555 if (!primalStored) {
556 Aggregated real = rhs.cast().getValue();
557
559 lhs.values[i.value].value() = AggregatedTraits::template arrayAccess<i.value>(real);
560 indexManager.get().template freeIndex<Impl>(lhs.values[i.value].getTapeData());
561 });
562 }
563 }
564
567 template<typename Aggregated, typename Type, typename Lhs, typename Rhs>
570 using AggregatedTraits = RealTraits::AggregatedTypeTraits<Aggregated>;
571
572 int constexpr Elements = AggregatedTraits::Elements;
573
575 if (IndexManager::CopyNeedsStatement || !Config::CopyOptimization) {
576 store(lhs, static_cast<ExpressionInterface<Aggregated, Rhs> const&>(rhs));
577 return;
578 } else {
580 indexManager.get().template copyIndex<Impl>(lhs.values[i.value].getTapeData(),
581 rhs.values[i.value].getTapeData());
582 });
583 }
584 } else {
586 indexManager.get().template freeIndex<Impl>(lhs.values[i.value].getTapeData());
587 });
588 }
589
591 lhs.values[i.value].value() = rhs.values[i.value].getValue();
592 });
593 }
594
596 template<typename Lhs, typename Rhs>
599 bool primalStored = false;
600 StatementDataPointers pointers = {};
601
602 if (storeArgumentAndStmtData<Lhs>(rhs, pointers)) {
603 bool generatedNewIndex = indexManager.get().template assignIndex<Impl>(lhs.cast().getTapeData());
604 checkPrimalSize(generatedNewIndex);
605
606 Real& primalEntry = primals[lhs.cast().getIdentifier()];
607 pushLhsData(lhs.cast().getIdentifier(), primalEntry, pointers);
608
609 primalEntry = rhs.cast().getValue();
610
612 size_t constexpr MaxActiveArgs = ExpressionTraits::NumberOfActiveTypeArguments<Rhs>::value;
613
614 JacobianExtractionLogic getRhsIdentifiersAndJacobians;
615 std::array<Identifier, MaxActiveArgs> rhsIdentifiers;
616 std::array<Real, MaxActiveArgs> jacobians;
617 getRhsIdentifiersAndJacobians.eval(rhs.cast(), Real(1.0), rhsIdentifiers.data(), jacobians.data());
618
620 rhs.cast().getValue(), MaxActiveArgs,
621 rhsIdentifiers.data(), jacobians.data());
622 }
623
624 primalStored = true;
625 }
626
627 if (!primalStored) {
628 indexManager.get().template freeIndex<Impl>(lhs.cast().getTapeData());
629 }
630
631 lhs.cast().value() = rhs.cast().getValue();
632 }
633
636 template<typename Lhs, typename Rhs>
640 if (IndexManager::CopyNeedsStatement || !Config::CopyOptimization) {
641 store<Lhs, Rhs>(lhs, static_cast<ExpressionInterface<Real, Rhs> const&>(rhs));
642 return;
643 } else {
644 indexManager.get().template copyIndex<Impl>(lhs.cast().getTapeData(), rhs.cast().getTapeData());
645 }
646 } else {
647 indexManager.get().template freeIndex<Impl>(lhs.cast().getTapeData());
648 }
649
650 lhs.cast().value() = rhs.cast().getValue();
651 }
652
655 template<typename Lhs>
657 indexManager.get().template freeIndex<Impl>(lhs.cast().getTapeData());
658
659 lhs.cast().value() = rhs;
660 }
661
663 /*******************************************************************************
664 * Protected helper function for ReverseTapeInterface
665 */
666
667 protected:
668
670 template<typename Lhs>
672 bool unusedIndex) {
673 if (TapeTypes::IsLinearIndexHandler) {
674 statementData.reserveItems(1);
675 }
676
677 bool generatedNewIndex;
678 if (unusedIndex) {
679 generatedNewIndex = indexManager.get().template assignUnusedIndex<Impl>(value.cast().getTapeData());
680 } else {
681 generatedNewIndex = indexManager.get().template assignIndex<Impl>(value.cast().getTapeData());
682 }
683 checkPrimalSize(generatedNewIndex);
684
685 Real& primalEntry = primals[value.cast().getIdentifier()];
686 if (TapeTypes::IsLinearIndexHandler) {
687 statementData.pushData(
689 StatementEvaluator::template createHandle<Impl, Impl, AssignStatement<Lhs, EmptyExpression<Real>>>(), 0);
690 }
691
692 Real oldValue = primalEntry;
693 primalEntry = value.cast().value();
694
695 return oldValue;
696 }
697
698 public:
699
702
704 template<typename Lhs>
709
714 CODI_UNUSED(adjointsManagement);
715
716 size_t maxSize = std::min((size_t)indexManager.get().getLargestCreatedIndex() + 1, adjoints.size());
717 for (size_t i = 0; i < maxSize; i += 1) {
718 adjoints[i] = Gradient();
719 }
720 }
721
725 CODI_INLINE void reset(bool resetAdjoints = true,
727 for (Real& primal : primals) {
728 primal = Real();
729 }
730
731 Base::reset(resetAdjoints, adjointsManagement);
732 }
733
735
736 protected:
737
741 std::string name;
742 if (TapeTypes::IsLinearIndexHandler) {
743 name = "CoDi Tape Statistics ( PrimalValueLinearTape )";
744 } else {
745 name = "CoDi Tape Statistics ( PrimalValueReuseTape )";
746 }
747 TapeValues values = TapeValues(name);
748
749 size_t nAdjoints = indexManager.get().getLargestCreatedIndex();
750 double memoryAdjoints = static_cast<double>(nAdjoints) * static_cast<double>(sizeof(Gradient));
751
752 size_t nPrimals = indexManager.get().getLargestCreatedIndex();
753 double memoryPrimals = static_cast<double>(nPrimals) * static_cast<double>(sizeof(Real));
754
755 values.addSection("Adjoint vector");
756 values.addUnsignedLongEntry("Number of adjoints", nAdjoints);
757 values.addDoubleEntry("Memory allocated", memoryAdjoints, TapeValues::LocalReductionOperation::Sum, true, true);
758
759 values.addSection("Primal vector");
760 values.addUnsignedLongEntry("Number of primals", nPrimals);
761 values.addDoubleEntry("Memory allocated", memoryPrimals, TapeValues::LocalReductionOperation::Sum, true, true);
762
763 values.addSection("Index manager");
764 indexManager.get().addToTapeValues(values);
765
766 values.addSection("Statement entries");
767 statementData.addToTapeValues(values);
768
769 values.addSection("Statement byte entries");
770 statementByteData.addToTapeValues(values);
771
772 return values;
773 }
774
775 /******************************************************************************
776 * Protected helper function for CustomAdjointVectorEvaluationTapeInterface
777 */
778
780 template<typename AdjointVector>
782 CODI_UNUSED(vectorAccess, data);
783
784#if CODI_VariableAdjointInterfaceInPrimalTapes
785 return vectorAccess;
786#else
787 CODI_STATIC_ASSERT(CODI_T(std::is_same<typename std::remove_reference<AdjointVector>::type, Gradient*>::value),
788 "Please enable 'CODI_VariableAdjointInterfaceInPrimalTapes' in order"
789 " to use custom adjoint vectors in the primal value tapes.");
790
791 return data;
792#endif
793 }
794
796 struct IncrementForwardLogic : public JacobianComputationLogic<IncrementForwardLogic> {
797 public:
798
800 template<typename Node>
801 CODI_INLINE void handleJacobianOnActive(Node const& node, Real jacobian, Gradient& lhsTangent,
802 ADJOINT_VECTOR_TYPE* adjointVector) {
803 CODI_UNUSED(lhsTangent);
804
806#if CODI_VariableAdjointInterfaceInPrimalTapes
807 adjointVector->updateTangentWithLhs(node.getIdentifier(), jacobian);
808#else
809 lhsTangent += jacobian * adjointVector[node.getIdentifier()];
810#endif
811 }
812 }
813 };
814
816 CODI_WRAP_FUNCTION(Wrap_internalEvaluateForward_EvalStatements, Impl::internalEvaluateForward_EvalStatements);
817
819 template<bool copyPrimal, typename AdjointVector>
820 CODI_NO_INLINE void internalEvaluateForward(Position const& start, Position const& end, AdjointVector&& data) {
823 CODI_T(std::is_same<typename std::remove_reference<AdjointVector>::type, Gradient*>::value),
824 "Please enable 'CODI_VariableAdjointInterfaceInPrimalTapes' in order"
825 " to use custom adjoint vectors in the primal value tapes.");
826
827 std::vector<Real> primalsCopy(0);
828 Real* primalData = primals.data();
829
830 if (copyPrimal) {
832 primalData = primalsCopy.data();
833 }
834
835 VectorAccess<AdjointVector> vectorAccess(data, primalData);
836
837 ADJOINT_VECTOR_TYPE* dataVector = selectAdjointVector<AdjointVector>(&vectorAccess, data);
838
840 cast(), start, end, &vectorAccess, EventHints::EvaluationKind::Forward, EventHints::Endpoint::Begin);
841
842 Wrap_internalEvaluateForward_EvalStatements evalFunc{};
843 Base::llfByteData.evaluateForward(start, end, evalFunc, cast(), primalData, dataVector);
844
845 EventSystem<Impl>::notifyTapeEvaluateListeners(cast(), start, end, &vectorAccess,
846 EventHints::EvaluationKind::Forward, EventHints::Endpoint::End);
847 }
848
850 struct IncrementReversalLogic : public JacobianComputationLogic<IncrementReversalLogic> {
851 public:
852
854 template<typename Node>
855 CODI_INLINE void handleJacobianOnActive(Node const& node, Real jacobian, Gradient const& lhsAdjoint,
856 ADJOINT_VECTOR_TYPE* adjointVector) {
857 CODI_UNUSED(lhsAdjoint);
858
860#if CODI_VariableAdjointInterfaceInPrimalTapes
861 adjointVector->updateAdjointWithLhs(node.getIdentifier(), jacobian);
862#else
863 adjointVector[node.getIdentifier()] += jacobian * lhsAdjoint;
864#endif
865 }
866 }
867 };
868
870 CODI_WRAP_FUNCTION(Wrap_internalEvaluateReverse_EvalStatements, Impl::internalEvaluateReverse_EvalStatements);
871
873 template<bool copyPrimal, typename AdjointVector>
874 CODI_INLINE void internalEvaluateReverse(Position const& start, Position const& end, AdjointVector&& data) {
877 CODI_T(std::is_same<typename std::remove_reference<AdjointVector>::type, Gradient*>::value),
878 "Please enable 'CODI_VariableAdjointInterfaceInPrimalTapes' in order"
879 " to use custom adjoint vectors in the primal value tapes.");
880
881 Real* primalData = primals.data();
882
883 if (copyPrimal) {
885 primalData = primalsCopy.data();
886 }
887
888 VectorAccess<AdjointVector> vectorAccess(data, primalData);
889
890 ADJOINT_VECTOR_TYPE* dataVector = selectAdjointVector<AdjointVector>(&vectorAccess, data);
891
893 cast(), start, end, &vectorAccess, EventHints::EvaluationKind::Reverse, EventHints::Endpoint::Begin);
894
895 Wrap_internalEvaluateReverse_EvalStatements evalFunc;
896 Base::llfByteData.evaluateReverse(start, end, evalFunc, cast(), primalData, dataVector);
897
898 EventSystem<Impl>::notifyTapeEvaluateListeners(cast(), start, end, &vectorAccess,
899 EventHints::EvaluationKind::Reverse, EventHints::Endpoint::End);
900 }
901
902 public:
903
906
907 using Base::evaluate;
908
910 template<typename AdjointVector>
911 CODI_INLINE void evaluate(Position const& start, Position const& end, AdjointVector&& data) {
912 internalEvaluateReverse<!TapeTypes::IsLinearIndexHandler>(start, end, std::forward<AdjointVector>(data));
913 }
914
916 template<typename AdjointVector>
917 CODI_INLINE void evaluateForward(Position const& start, Position const& end, AdjointVector&& data) {
918 internalEvaluateForward<!TapeTypes::IsLinearIndexHandler>(start, end, std::forward<AdjointVector>(data));
919 }
920
923 return adjoints.data();
924 }
925
927 /*******************************************************************************/
930
932 CODI_INLINE void swap(Impl& other) {
933 // Index manager does not need to be swapped, it is either static or swapped with the vector data.
934 // Vectors are swapped recursively in the base class.
935
936 std::swap(adjoints, other.adjoints);
937 std::swap(primals, other.primals);
938
939 Base::swap(other);
940
941 // Ensure that the primals vector of both tapes are sized according to the index manager.
942 checkPrimalSize(true);
943 other.checkPrimalSize(true);
944 }
945
948 adjoints.resize(1);
949 }
950
953 checkAdjointSize(indexManager.get().getLargestCreatedIndex());
954 }
955
959
963
965 size_t getParameter(TapeParameters parameter) const {
966 switch (parameter) {
968 return adjoints.size();
969 break;
971 return indexManager.get().getLargestCreatedIndex();
972 break;
974 return primals.size();
975 break;
977 return statementData.getDataSize();
978 break;
980 return statementByteData.getDataSize();
981 break;
982 default:
983 return Base::getParameter(parameter);
984 break;
985 }
986 }
987
989 void setParameter(TapeParameters parameter, size_t value) {
990 switch (parameter) {
992 adjoints.resize(value);
993 break;
995 CODI_EXCEPTION("Tried to set a get only option.");
996 break;
998 primals.resize(value);
999 break;
1001 return statementData.resize(value);
1002 break;
1004 return statementByteData.resize(value);
1005 break;
1006 default:
1007 Base::setParameter(parameter, value);
1008 break;
1009 }
1010 }
1011
1016
1018 template<typename AdjointVector>
1020 return new VectorAccess<AdjointVector>(data, primals.data());
1021 }
1022
1025 template<typename Adjoint>
1027 return new VectorAccess<Adjoint*>(data, primals.data());
1028 }
1029
1032 delete access;
1033 }
1034
1036 /*******************************************************************************/
1039
1041 template<typename Lhs>
1045
1047 /*******************************************************************************/
1050
1052
1056 void evaluateForward(Position const& start, Position const& end,
1058 if (AdjointsManagement::Automatic == adjointsManagement) {
1059 checkAdjointSize(indexManager.get().getLargestCreatedIndex());
1060 }
1061
1062 codiAssert(indexManager.get().getLargestCreatedIndex() < (Identifier)adjoints.size());
1063
1064 cast().evaluateForward(start, end, adjoints.data());
1065 }
1066
1068 /*******************************************************************************/
1071
1073 void pushJacobianManual(Real const& jacobian, Real const& value, ActiveTypeTapeData const& data) {
1074 CODI_UNUSED(value);
1075
1076 cast().incrementManualPushCounter();
1077
1078 *manualPushIdentifiers = indexManager.get().getIndex(data);
1079 *manualPushJacobians = jacobian;
1080
1083
1085 if (this->manualPushCounter == this->manualPushGoal) {
1086 // emit statement event
1089
1093 }
1094 }
1095 }
1096
1098 void storeManual(Real const& lhsValue, ActiveTypeTapeData& lhsData, Config::ArgumentSize const& size) {
1099 CODI_UNUSED(lhsValue);
1100
1102
1103 StatementDataPointers pointers = {};
1104 Config::LowLevelFunctionDataSize byteSize = reserveStmtDataManual(1, size, size, 0, pointers);
1105
1106 manualPushJacobians = pointers.passiveValues;
1107 manualPushIdentifiers = pointers.rhsIdentifiers;
1108
1109 indexManager.get().template assignIndex<Impl>(lhsData);
1110 Real& primalEntry = primals[indexManager.get().getIndex(lhsData)];
1111 statementData.pushData(size, PrimalValueBaseTape::jacobianExpressions[size], byteSize);
1112 pushLhsData(indexManager.get().getIndex(lhsData), primalEntry, pointers);
1113
1114 primalEntry = lhsValue;
1115
1116 cast().initializeManualPushData(lhsValue, indexManager.get().getIndex(lhsData), size);
1117 }
1118
1120 /*******************************************************************************/
1123
1129 void createStatementManual(Config::ArgumentSize const& nOutputValues, Identifier const* const lhsIndices,
1130 Real const* const lhsValues, Config::ArgumentSize const& nActiveValues,
1131 Identifier const* const rhsIdentifiers, Config::ArgumentSize const& nPassiveValues,
1132 Real const* const rhsPrimals, Config::ArgumentSize const& nConstants,
1133 Real const* const rhsConstant, EvalHandle const& evalHandle) {
1134 Impl& impl = cast();
1136 // TODO.
1137 } else if (Config::StatementInputTag == nPassiveValues && TapeTypes::IsLinearIndexHandler) CODI_Unlikely {
1138 statementData.reserveItems(1);
1139 primals[lhsIndices[0]] = lhsValues[0];
1140 statementData.pushData(Config::StatementInputTag, evalHandle, 0);
1141 } else CODI_Likely {
1142 StatementDataPointers pointers = {};
1144 reserveStmtDataManual(nOutputValues, nActiveValues, nPassiveValues, nConstants, pointers);
1145
1146 impl.initializeManualPushData(lhsValues[0], lhsIndices[0], nPassiveValues);
1147
1148 for (size_t activeCount = 0; activeCount < nActiveValues; activeCount++) {
1149 pointers.rhsIdentifiers[activeCount] = rhsIdentifiers[activeCount];
1150 }
1151 for (size_t passiveCount = 0; passiveCount < nPassiveValues; passiveCount++) {
1152 cast().incrementManualPushCounter();
1153 pointers.passiveValues[passiveCount] = rhsPrimals[passiveCount];
1154 }
1155 for (size_t constantCount = 0; constantCount < nConstants; constantCount++) {
1156 pointers.constantValues[constantCount] = rhsConstant[constantCount];
1157 }
1158
1159 statementData.pushData(nPassiveValues, evalHandle, byteSize);
1160
1161 for (Config::ArgumentSize i = 0; i < nOutputValues; i += 1) {
1162 pushLhsData(lhsIndices[i], lhsValues[i], pointers);
1163
1164 if (TapeTypes::IsLinearIndexHandler) {
1165 primals[lhsIndices[i]] = lhsValues[i];
1166 }
1167 }
1168 }
1169 }
1170
1171 using Base::writeTape;
1172
1177 template<typename Type>
1179 Position const& end) {
1180 Impl& impl = cast();
1181 writer->start(impl);
1182 Base::llfByteData.evaluateForward(start, end, Impl::template internalWriteTape<Type>, primals.data(), writer);
1183 writer->finish();
1184 }
1185
1187 /*******************************************************************************/
1190
1193 return indexManager.get();
1194 }
1195
1197 /*******************************************************************************/
1200
1203 statementData.reserveItems(1);
1204
1205 Base::internalStoreLowLevelFunction(token, size, data);
1206
1208 }
1209
1211 /*******************************************************************************/
1214
1218 CODI_INLINE void evaluate(Position const& start, Position const& end,
1220 if (AdjointsManagement::Automatic == adjointsManagement) {
1221 checkAdjointSize(indexManager.get().getLargestCreatedIndex());
1222 }
1223
1224 codiAssert(indexManager.get().getLargestCreatedIndex() < (Identifier)adjoints.size());
1225
1226 evaluate(start, end, adjoints.data());
1227 }
1228
1232 CODI_INLINE void resetTo(Position const& pos, bool resetAdjoints = true,
1234 cast().internalResetPrimalValues(pos);
1235
1236 Base::resetTo(pos, resetAdjoints, adjointsManagement);
1237 }
1238
1240 /*******************************************************************************/
1243
1247 void evaluateKeepState(Position const& start, Position const& end,
1249 if (AdjointsManagement::Automatic == adjointsManagement) {
1250 checkAdjointSize(indexManager.get().getLargestCreatedIndex());
1251 }
1252
1253 codiAssert(indexManager.get().getLargestCreatedIndex() < (Identifier)adjoints.size());
1254
1255 evaluateKeepState(start, end, adjoints.data());
1256 }
1257
1259 template<typename AdjointVector>
1260 void evaluateKeepState(Position const& start, Position const& end, AdjointVector&& data) {
1261 internalEvaluateReverse<false>(start, end, std::forward<AdjointVector>(data));
1262
1263 if (!TapeTypes::IsLinearIndexHandler) {
1264 evaluatePrimal(end, start);
1265 }
1266 }
1267
1271 void evaluateForwardKeepState(Position const& start, Position const& end,
1273 if (AdjointsManagement::Automatic == adjointsManagement) {
1274 checkAdjointSize(indexManager.get().getLargestCreatedIndex());
1275 }
1276
1277 codiAssert(indexManager.get().getLargestCreatedIndex() < (Identifier)adjoints.size());
1278
1279 evaluateForwardKeepState(start, end, adjoints.data());
1280 }
1281
1283 template<typename AdjointVector>
1284 void evaluateForwardKeepState(Position const& start, Position const& end, AdjointVector&& data) {
1285 if (!TapeTypes::IsLinearIndexHandler) {
1286 cast().internalResetPrimalValues(end);
1287 }
1288
1289 internalEvaluateForward<false>(start, end, std::forward<AdjointVector>(data));
1290 }
1291
1292 protected:
1293
1294 /******************************************************************************
1295 * Protected helper function for PrimalEvaluationTapeInterface
1296 */
1297
1299 CODI_WRAP_FUNCTION(Wrap_internalEvaluatePrimal_EvalStatements, Impl::internalEvaluatePrimal_EvalStatements);
1300
1301 public:
1302
1304 /*******************************************************************************/
1307
1309
1311 CODI_NO_INLINE void evaluatePrimal(Position const& start, Position const& end) {
1312 // TODO: implement primal value only accessor
1314
1315 EventSystem<Impl>::notifyTapeEvaluateListeners(cast(), start, end, &primalAdjointAccess,
1316 EventHints::EvaluationKind::Primal, EventHints::Endpoint::Begin);
1317
1318 Wrap_internalEvaluatePrimal_EvalStatements evalFunc{};
1319 Base::llfByteData.evaluateForward(start, end, evalFunc, cast(), primals.data());
1320
1321 EventSystem<Impl>::notifyTapeEvaluateListeners(cast(), start, end, &primalAdjointAccess,
1322 EventHints::EvaluationKind::Primal, EventHints::Endpoint::End);
1323 }
1324
1326 Real& primal(Identifier const& identifier) {
1327 return primals[identifier];
1328 }
1329
1331 Real const& primal(Identifier const& identifier) const {
1332 return primals[identifier];
1333 }
1334
1337 return primals.data();
1338 }
1339
1341 /*******************************************************************************/
1344
1346 template<typename Stmt, typename GenImpl>
1348 public:
1349 using Lhs = typename Stmt::Lhs;
1350 using Rhs = typename Stmt::Rhs;
1351 using LhsReal = typename Lhs::Real;
1353
1356
1357 template<size_t pos>
1360
1363 PassiveReal const* CODI_RESTRICT const constantValues,
1364 Identifier const* CODI_RESTRICT const identifiers) {
1365 return Constructor::construct(primalVector, identifiers, constantValues);
1366 }
1367
1369 template<typename... Args>
1370 CODI_INLINE static void internalEvaluate(Args&&... args) {
1371 size_t constexpr MaxActiveArgs = ExpressionTraits::NumberOfActiveTypeArguments<Rhs>::value;
1372 size_t constexpr MaxConstantArgs = ExpressionTraits::NumberOfConstantTypeArguments<Rhs>::value;
1373 size_t constexpr MaxOutputArgs = ExpressionTraits::NumberOfActiveTypeArguments<Lhs>::value;
1374
1375 GenImpl::evaluateFull(GenImpl::evaluateInner, MaxOutputArgs, MaxActiveArgs, MaxConstantArgs,
1376 std::forward<Args>(args)...);
1377 }
1378 };
1379
1381 template<StatementCall type, typename Stmt>
1383
1384// Define macros for the definition of the evaluate functions in the Statement call generators.
1385#define STMT_COMMON_ARGS Config::ArgumentSize numberOfPassiveArguments, char *CODI_RESTRICT byteData
1386#define STMT_COMMON_CALL numberOfPassiveArguments, byteData
1387
1388 /*******************************************************************************/
1390 template<typename Stmt>
1392 : public StatementCallGeneratorBase<Stmt, StatementCallGenerator<StatementCall::WriteInformation, Stmt>> {
1393 public:
1396 using StaticRhs = typename Base::StaticRhs;
1397
1399 CODI_INLINE static void evaluateInner(size_t const& CODI_RESTRICT maxOutputArgs,
1400 size_t const& CODI_RESTRICT maxActiveArgs,
1401 size_t const& CODI_RESTRICT maxConstantArgs,
1402 WriteInfo& CODI_RESTRICT writeInfo, Real* CODI_RESTRICT primalVector,
1403 STMT_COMMON_ARGS) {
1404 StatementDataPointers pointers = {};
1405 pointers.populate(maxOutputArgs, maxActiveArgs, numberOfPassiveArguments, maxConstantArgs, byteData);
1406
1407 if (Config::StatementInputTag != numberOfPassiveArguments) {
1408 for (Config::ArgumentSize curPos = 0; curPos < numberOfPassiveArguments; curPos += 1) {
1409 primalVector[curPos] = pointers.passiveValues[curPos];
1410 }
1411 }
1412
1413 StaticRhs staticRhs =
1414 Base::constructStaticRhs(primalVector, pointers.constantValues, pointers.rhsIdentifiers);
1415
1416 writeInfo.numberOfOutputArguments = maxOutputArgs;
1417 writeInfo.numberOfActiveArguments = maxActiveArgs;
1418 writeInfo.numberOfConstantArguments = maxConstantArgs;
1419
1420 // The Impl template name is added to simplify the writer and reader interfaces EvalHandles. It represents
1421 // the first and second template args in the createHandle method. For the manual push, the second Impl
1422 // template is instead replaced with JacobianGenerator<size>.
1423
1424 writeInfo.stmtExpression = "Impl, Impl, ";
1425 writeInfo.stmtExpression += demangleName<Stmt>();
1427 mathGen.eval(staticRhs, writeInfo.mathRepresentation);
1428 }
1429
1431 template<typename Func, typename... Args>
1432 CODI_INLINE static void evaluateFull(Func const& func, Args&&... args) {
1433 func(std::forward<Args>(args)...);
1434 }
1435
1437 CODI_INLINE static void evaluate(WriteInfo& CODI_RESTRICT writeInfo, Real* CODI_RESTRICT primalVector,
1438 STMT_COMMON_ARGS) {
1439 Base::internalEvaluate(writeInfo, primalVector, STMT_COMMON_CALL);
1440 }
1441 };
1442
1443 /*******************************************************************************/
1445 template<typename Stmt>
1446 struct StatementCallGenerator<StatementCall::IterateInputs, Stmt>
1447 : public StatementCallGeneratorBase<Stmt, StatementCallGenerator<StatementCall::IterateInputs, Stmt>> {
1448 public:
1451 using StaticRhs = typename Base::StaticRhs;
1452
1455
1457 CODI_INLINE static void evaluateInner(size_t const& CODI_RESTRICT maxOutputArgs,
1458 size_t const& CODI_RESTRICT maxActiveArgs,
1459 size_t const& CODI_RESTRICT maxConstantArgs,
1460 size_t& CODI_RESTRICT linearAdjointPos, IterCallback func,
1461 void* userData, STMT_COMMON_ARGS) {
1462 CODI_UNUSED(linearAdjointPos);
1463
1464 StatementDataPointers pointers = {};
1465 pointers.populate(maxOutputArgs, maxActiveArgs, numberOfPassiveArguments, maxConstantArgs, byteData);
1466
1467 for (Config::ArgumentSize i = 0; i < maxActiveArgs; i += 1) {
1469 func(&pointers.rhsIdentifiers[i], userData);
1470 }
1471 }
1472 }
1473
1475 template<typename Func, typename... Args>
1476 CODI_INLINE static void evaluateFull(Func const& func, Args&&... args) {
1477 func(std::forward<Args>(args)...);
1478 }
1479
1481 CODI_INLINE static void evaluate(size_t& CODI_RESTRICT linearAdjointPos, IterCallback func, void* userData,
1482 STMT_COMMON_ARGS) {
1483 Base::internalEvaluate(linearAdjointPos, func, userData, STMT_COMMON_CALL);
1484 }
1485 };
1486
1487 /*******************************************************************************/
1489 template<typename Stmt>
1490 struct StatementCallGenerator<StatementCall::IterateOutputs, Stmt>
1491 : public StatementCallGeneratorBase<Stmt, StatementCallGenerator<StatementCall::IterateOutputs, Stmt>> {
1492 public:
1495 using StaticRhs = typename Base::StaticRhs;
1496
1499
1501 CODI_INLINE static void evaluateInner(size_t const& CODI_RESTRICT maxOutputArgs,
1502 size_t const& CODI_RESTRICT maxActiveArgs,
1503 size_t const& CODI_RESTRICT maxConstantArgs,
1504 size_t& CODI_RESTRICT linearAdjointPos, IterCallback func,
1505 void* userData, STMT_COMMON_ARGS) {
1506 StatementDataPointers pointers = {};
1507 pointers.populate(maxOutputArgs, maxActiveArgs, numberOfPassiveArguments, maxConstantArgs, byteData);
1508
1509 for (size_t iLhs = 0; iLhs < maxOutputArgs; iLhs += 1) {
1510 if (LinearIndexHandling) {
1511 Identifier lhsIdentifier = linearAdjointPos + 1 + iLhs;
1512 func(&lhsIdentifier, userData);
1513
1514 codiAssert(lhsIdentifier == (Identifier)(linearAdjointPos + 1 + iLhs));
1515 } else {
1516 func(&pointers.lhsIdentifiers[iLhs], userData);
1517 }
1518 }
1519 }
1520
1522 template<typename Func, typename... Args>
1523 CODI_INLINE static void evaluateFull(Func const& func, Args&&... args) {
1524 func(std::forward<Args>(args)...);
1525 }
1526
1528 CODI_INLINE static void evaluate(size_t& CODI_RESTRICT linearAdjointPos, IterCallback func, void* userData,
1529 STMT_COMMON_ARGS) {
1530 Base::internalEvaluate(linearAdjointPos, func, userData, STMT_COMMON_CALL);
1531 }
1532 };
1533
1534 /*******************************************************************************/
1536 template<typename Stmt>
1538 : public StatementCallGeneratorBase<Stmt, StatementCallGenerator<StatementCall::ClearAdjoints, Stmt>> {
1539 public:
1542 using StaticRhs = typename Base::StaticRhs;
1543
1546
1548 template<typename Func>
1549 CODI_INLINE static void evaluateFull(Func const& evalInner, size_t const& CODI_RESTRICT maxOutputArgs,
1550 size_t const& CODI_RESTRICT maxActiveArgs,
1551 size_t const& CODI_RESTRICT maxConstantArgs,
1552 ADJOINT_VECTOR_TYPE* CODI_RESTRICT adjointVector, STMT_COMMON_ARGS) {
1553 CODI_UNUSED(evalInner);
1554
1555 StatementDataPointers pointers = {};
1556 pointers.populate(maxOutputArgs, maxActiveArgs, numberOfPassiveArguments, maxConstantArgs, byteData);
1557
1558 for (size_t iLhs = 0; iLhs < maxOutputArgs; iLhs += 1) {
1559 // Only called from reuse tape.
1560 Identifier lhsIdentifier = pointers.lhsIdentifiers[iLhs];
1561
1562#if CODI_VariableAdjointInterfaceInPrimalTapes
1563 adjointVector->resetAdjointVec(lhsIdentifier);
1564#else
1565 adjointVector[lhsIdentifier] = Gradient();
1566#endif
1567 }
1568 }
1569
1571 CODI_INLINE static void evaluate(ADJOINT_VECTOR_TYPE* CODI_RESTRICT adjointVector, STMT_COMMON_ARGS) {
1572 Base::internalEvaluate(adjointVector, STMT_COMMON_CALL);
1573 }
1574 };
1575
1576 /*******************************************************************************/
1578 template<typename Stmt>
1580 : public StatementCallGeneratorBase<Stmt, StatementCallGenerator<StatementCall::Forward, Stmt>> {
1581 public:
1584 using StaticRhs = typename Base::StaticRhs;
1585
1586 template<size_t pos>
1587 using ExtractExpr = typename Base::template ExtractExpr<pos>;
1588
1590 CODI_INLINE static void evaluateInner(Real* CODI_RESTRICT primalVector,
1591 ADJOINT_VECTOR_TYPE* CODI_RESTRICT adjointVector,
1592 Real* CODI_RESTRICT lhsPrimals, Gradient* lhsTangents,
1593 PassiveReal const* CODI_RESTRICT const constantValues,
1594 Identifier const* CODI_RESTRICT const rhsIdentifiers) {
1595 StaticRhs staticRhs = Base::constructStaticRhs(primalVector, constantValues, rhsIdentifiers);
1596
1597 IncrementForwardLogic incrementForward;
1598
1600#if CODI_VariableAdjointInterfaceInPrimalTapes
1601 adjointVector->setActiveVariableForIndirectAccess(i.value);
1602#else
1603 lhsTangents[i.value] = Gradient();
1604#endif
1605 ExtractExpr<i.value> expr(staticRhs);
1606
1607 incrementForward.eval(expr, Real(1.0), lhsTangents[i.value], adjointVector);
1608 lhsPrimals[i.value] = expr.getValue();
1609 });
1610 }
1611
1613 template<typename Func>
1614 CODI_INLINE static void evaluateFull(Func const& evalInner, size_t const& CODI_RESTRICT maxOutputArgs,
1615 size_t const& CODI_RESTRICT maxActiveArgs,
1616 size_t const& CODI_RESTRICT maxConstantArgs, Impl& CODI_RESTRICT tape,
1617 Real* CODI_RESTRICT lhsPrimals, Gradient* CODI_RESTRICT lhsTangents,
1618 Real* CODI_RESTRICT primalVector,
1619 ADJOINT_VECTOR_TYPE* CODI_RESTRICT adjointVector,
1620 size_t& CODI_RESTRICT linearAdjointPos, STMT_COMMON_ARGS) {
1621 StatementDataPointers pointers = {};
1622 pointers.populate(maxOutputArgs, maxActiveArgs, numberOfPassiveArguments, maxConstantArgs, byteData);
1623
1624 for (Config::ArgumentSize curPos = 0; curPos < numberOfPassiveArguments; curPos += 1) {
1625 primalVector[curPos] = pointers.passiveValues[curPos];
1626 }
1627
1628 evalInner(primalVector, adjointVector, lhsPrimals, lhsTangents, pointers.constantValues,
1629 pointers.rhsIdentifiers);
1630
1631 for (size_t iLhs = 0; iLhs < maxOutputArgs; iLhs += 1) {
1632 Identifier lhsIdentifier;
1633 if (LinearIndexHandling) {
1634 linearAdjointPos += 1;
1635 lhsIdentifier = linearAdjointPos;
1636 } else {
1637 lhsIdentifier = pointers.lhsIdentifiers[iLhs];
1638 }
1639
1640 if (!LinearIndexHandling) {
1641 pointers.oldLhsValues[iLhs] = primalVector[lhsIdentifier];
1642 }
1643
1644 primalVector[lhsIdentifier] = lhsPrimals[iLhs];
1645
1646#if CODI_VariableAdjointInterfaceInPrimalTapes
1647 adjointVector->setLhsTangent(lhsIdentifier);
1648 EventSystem<Impl>::notifyStatementEvaluateListeners(tape, lhsIdentifier, adjointVector->getVectorSize(),
1649 adjointVector->getAdjointVec(lhsIdentifier));
1650#else
1651 adjointVector[lhsIdentifier] = lhsTangents[iLhs];
1653 GradientTraits::toArray(lhsTangents[iLhs]).data());
1654#endif
1656 primalVector[lhsIdentifier]);
1657 }
1658 }
1659
1662 Gradient* CODI_RESTRICT lhsTangents, Real* CODI_RESTRICT primalVector,
1663 ADJOINT_VECTOR_TYPE* CODI_RESTRICT adjointVector,
1664 size_t& CODI_RESTRICT linearAdjointPos, STMT_COMMON_ARGS) {
1665 Base::internalEvaluate(tape, lhsPrimals, lhsTangents, primalVector, adjointVector, linearAdjointPos,
1666 STMT_COMMON_CALL);
1667 }
1668 };
1669
1670 /*******************************************************************************/
1672 template<typename Stmt>
1674 : public StatementCallGeneratorBase<Stmt, StatementCallGenerator<StatementCall::Primal, Stmt>> {
1675 public:
1678 using StaticRhs = typename Base::StaticRhs;
1679
1680 template<size_t pos>
1681 using ExtractExpr = typename Base::template ExtractExpr<pos>;
1682
1684 CODI_INLINE static void evaluateInner(Real* CODI_RESTRICT lhsPrimals, Real* CODI_RESTRICT primalVector,
1685 PassiveReal const* CODI_RESTRICT const constantValues,
1686 Identifier const* CODI_RESTRICT const rhsIdentifiers) {
1687 StaticRhs staticRhs = Base::constructStaticRhs(primalVector, constantValues, rhsIdentifiers);
1688
1690 ExtractExpr<i.value> expr(staticRhs);
1691
1692 lhsPrimals[i.value] = expr.getValue();
1693 });
1694 }
1695
1697 template<typename Func>
1698 CODI_INLINE static void evaluateFull(Func const& evalInner, size_t const& CODI_RESTRICT maxOutputArgs,
1699 size_t const& CODI_RESTRICT maxActiveArgs,
1700 size_t const& CODI_RESTRICT maxConstantArgs, Impl& CODI_RESTRICT tape,
1701 Real* CODI_RESTRICT lhsPrimals, Real* CODI_RESTRICT primalVector,
1702 size_t& CODI_RESTRICT linearAdjointPos, STMT_COMMON_ARGS) {
1703 StatementDataPointers pointers = {};
1704 pointers.populate(maxOutputArgs, maxActiveArgs, numberOfPassiveArguments, maxConstantArgs, byteData);
1705
1706 for (Config::ArgumentSize curPos = 0; curPos < numberOfPassiveArguments; curPos += 1) {
1707 primalVector[curPos] = pointers.passiveValues[curPos];
1708 }
1709
1710 evalInner(lhsPrimals, primalVector, pointers.constantValues, pointers.rhsIdentifiers);
1711
1712 for (size_t iLhs = 0; iLhs < maxOutputArgs; iLhs += 1) {
1713 Identifier lhsIdentifier;
1714 if (LinearIndexHandling) {
1715 linearAdjointPos += 1;
1716 lhsIdentifier = linearAdjointPos;
1717 } else {
1718 lhsIdentifier = pointers.lhsIdentifiers[iLhs];
1719 }
1720
1721 if (!LinearIndexHandling) {
1722 pointers.oldLhsValues[iLhs] = primalVector[lhsIdentifier];
1723 }
1724
1725 primalVector[lhsIdentifier] = lhsPrimals[iLhs];
1726
1728 primalVector[lhsIdentifier]);
1729 }
1730 }
1731
1734 Real* CODI_RESTRICT primalVector, size_t& CODI_RESTRICT linearAdjointPos,
1735 STMT_COMMON_ARGS) {
1736 Base::internalEvaluate(tape, lhsPrimals, primalVector, linearAdjointPos, STMT_COMMON_CALL);
1737 }
1738 };
1739
1740 /*******************************************************************************/
1742 template<typename Stmt>
1744 : public StatementCallGeneratorBase<Stmt, StatementCallGenerator<StatementCall::Reverse, Stmt>> {
1745 public:
1748 using StaticRhs = typename Base::StaticRhs;
1749
1750 template<size_t pos>
1751 using ExtractExpr = typename Base::template ExtractExpr<pos>;
1752
1754 CODI_INLINE static void evaluateInner(Real* CODI_RESTRICT primalVector,
1755 ADJOINT_VECTOR_TYPE* CODI_RESTRICT adjointVector,
1756 Gradient* CODI_RESTRICT lhsAdjoints,
1757 PassiveReal const* CODI_RESTRICT const constantValues,
1758 Identifier const* CODI_RESTRICT const rhsIdentifiers) {
1759 StaticRhs staticRhs = Base::constructStaticRhs(primalVector, constantValues, rhsIdentifiers);
1760
1761 IncrementReversalLogic incrementReverse;
1763#if CODI_VariableAdjointInterfaceInPrimalTapes
1764 adjointVector->setActiveVariableForIndirectAccess(i.value);
1765#endif
1766 ExtractExpr<i.value> expr(staticRhs);
1767 incrementReverse.eval(expr, Real(1.0), const_cast<Gradient const&>(lhsAdjoints[i.value]), adjointVector);
1768 });
1769 }
1770
1772 template<typename Func>
1773 CODI_INLINE static void evaluateFull(Func const& evalInner, size_t const& CODI_RESTRICT maxOutputArgs,
1774 size_t const& CODI_RESTRICT maxActiveArgs,
1775 size_t const& CODI_RESTRICT maxConstantArgs, Impl& CODI_RESTRICT tape,
1776 Gradient* CODI_RESTRICT lhsAdjoints, Real* CODI_RESTRICT primalVector,
1777 ADJOINT_VECTOR_TYPE* CODI_RESTRICT adjointVector,
1778 size_t& CODI_RESTRICT linearAdjointPos, STMT_COMMON_ARGS) {
1779 StatementDataPointers pointers = {};
1780 pointers.populate(maxOutputArgs, maxActiveArgs, numberOfPassiveArguments, maxConstantArgs, byteData);
1781
1782 if (LinearIndexHandling) {
1783 linearAdjointPos -= maxOutputArgs;
1784 }
1785
1786 bool allZero = true;
1787 for (size_t iLhs = 0; iLhs < maxOutputArgs; iLhs += 1) {
1788 Identifier lhsIdentifier;
1789 if (LinearIndexHandling) {
1790 lhsIdentifier = linearAdjointPos + 1 + iLhs;
1791 } else {
1792 lhsIdentifier = pointers.lhsIdentifiers[iLhs];
1793 }
1794
1795#if CODI_VariableAdjointInterfaceInPrimalTapes
1796 EventSystem<Impl>::notifyStatementEvaluateListeners(tape, lhsIdentifier, adjointVector->getVectorSize(),
1797 adjointVector->getAdjointVec(lhsIdentifier));
1798 adjointVector->setActiveVariableForIndirectAccess(iLhs);
1799 adjointVector->setLhsAdjoint(lhsIdentifier);
1800 allZero &= adjointVector->isLhsZero();
1801#else
1802 lhsAdjoints[iLhs] = adjointVector[lhsIdentifier];
1804 GradientTraits::toArray(lhsAdjoints[iLhs]).data());
1805 adjointVector[lhsIdentifier] = Gradient();
1806 allZero &= RealTraits::isTotalZero(lhsAdjoints[iLhs]);
1807#endif
1808
1810 primalVector[lhsIdentifier]);
1811 if (!LinearIndexHandling) {
1812 primalVector[lhsIdentifier] = pointers.oldLhsValues[iLhs];
1813 }
1814 }
1815
1817 for (Config::ArgumentSize curPos = 0; curPos < numberOfPassiveArguments; curPos += 1) {
1818 primalVector[curPos] = pointers.passiveValues[curPos];
1819 }
1820
1821 evalInner(primalVector, adjointVector, lhsAdjoints, pointers.constantValues, pointers.rhsIdentifiers);
1822 }
1823 }
1824
1827 Real* CODI_RESTRICT primalVector,
1828 ADJOINT_VECTOR_TYPE* CODI_RESTRICT adjointVector,
1829 size_t& CODI_RESTRICT linearAdjointPos, STMT_COMMON_ARGS) {
1830 Base::internalEvaluate(tape, lhsAdjoints, primalVector, adjointVector, linearAdjointPos, STMT_COMMON_CALL);
1831 }
1832 };
1833
1834 /*******************************************************************************/
1836 template<typename Stmt>
1838 : public StatementCallGeneratorBase<Stmt, StatementCallGenerator<StatementCall::ResetPrimals, Stmt>> {
1839 public:
1842 using StaticRhs = typename Base::StaticRhs;
1843
1846
1848 template<typename Func>
1849 CODI_INLINE static void evaluateFull(Func const& evalInner, size_t const& CODI_RESTRICT maxOutputArgs,
1850 size_t const& CODI_RESTRICT maxActiveArgs,
1851 size_t const& CODI_RESTRICT maxConstantArgs,
1852 Real* CODI_RESTRICT primalVector, STMT_COMMON_ARGS) {
1853 CODI_UNUSED(evalInner);
1854
1855 StatementDataPointers pointers = {};
1856 pointers.populate(maxOutputArgs, maxActiveArgs, numberOfPassiveArguments, maxConstantArgs, byteData);
1857
1858 // Only called from reuse tape.
1859 for (size_t iLhs = 0; iLhs < maxOutputArgs; iLhs += 1) {
1860 Identifier lhsIdentifier = pointers.lhsIdentifiers[iLhs];
1861
1862 primalVector[lhsIdentifier] = pointers.oldLhsValues[iLhs];
1863 }
1864 }
1865
1867 CODI_INLINE static void evaluate(Real* CODI_RESTRICT primalVector, STMT_COMMON_ARGS) {
1868 Base::internalEvaluate(primalVector, STMT_COMMON_CALL);
1869 }
1870 };
1871
1873
1874 private:
1875
1876 CODI_INLINE void checkAdjointSize(Identifier const& identifier) {
1877 if (identifier >= (Identifier)adjoints.size()) {
1878 resizeAdjointsVector();
1879 }
1880 }
1881
1882 CODI_INLINE void checkPrimalSize(bool generatedNewIndex) {
1883 if (generatedNewIndex && indexManager.get().getLargestCreatedIndex() >= (Identifier)primals.size()) {
1884 resizePrimalVector();
1885 }
1886 }
1887
1888 CODI_NO_INLINE void resizeAdjointsVector() {
1889 // overallocate as next multiple of Config::ChunkSize
1890 adjoints.resize(getNextMultiple((size_t)indexManager.get().getLargestCreatedIndex() + 1, Config::ChunkSize));
1891 }
1892
1893 CODI_NO_INLINE void resizePrimalVector() {
1894 // overallocate as next multiple of Config::ChunkSize
1895 primals.resize(getNextMultiple((size_t)indexManager.get().getLargestCreatedIndex() + 1, Config::ChunkSize));
1896 }
1897 };
1898
1900 template<size_t size>
1902
1904 template<size_t size>
1906 static size_t constexpr value = size;
1907 };
1908
1910 template<size_t size>
1912 static size_t constexpr value = 0;
1913 };
1914
1917 template<typename T_TapeImpl, size_t T_size>
1920 public:
1921
1925 void>));
1926 static size_t constexpr size = T_size;
1927
1928 using Real = typename TapeImpl::Real;
1929 using Gradient = typename TapeImpl::Gradient;
1930 using Identifier = typename TapeImpl::Identifier;
1931 using PassiveReal = typename TapeImpl::PassiveReal;
1932
1933 using StatementDataPointers = typename TapeImpl::StatementDataPointers;
1934
1935 static size_t constexpr LinearIndexHandling = TapeImpl::LinearIndexHandling;
1936
1938 template<typename Rhs, typename Impl>
1940
1941 /*******************************************************************************/
1944
1946 template<StatementCall type, typename Stmt>
1948
1950 template<typename Stmt>
1952 : public StatementCallGeneratorBase<Stmt, StatementCallGenerator<StatementCall::WriteInformation, Stmt>> {
1953 public:
1956
1959
1961 template<typename Func>
1962 CODI_INLINE static void evaluateFull(Func const& func, size_t const& maxOutputArgs,
1963 size_t const& maxActiveArgs, size_t const& maxConstantArgs,
1964 WriteInfo& writeInfo, Real* primalVector, STMT_COMMON_ARGS) {
1965 CODI_UNUSED(func, primalVector, numberOfPassiveArguments, byteData);
1966
1967 writeInfo.numberOfOutputArguments = maxOutputArgs;
1968 writeInfo.numberOfActiveArguments = maxActiveArgs;
1969 writeInfo.numberOfConstantArguments = maxConstantArgs;
1970 writeInfo.stmtExpression = "Impl, typename Impl::template JacobianStatementGenerator<" +
1971 std::to_string(size) + ">, codi::JacobianExpression<" + std::to_string(size) +
1972 ">";
1973 writeInfo.mathRepresentation = "Jacobian statement";
1974 }
1975
1977 CODI_INLINE static void evaluate(WriteInfo& CODI_RESTRICT writeInfo, Real* CODI_RESTRICT primalVector,
1978 STMT_COMMON_ARGS) {
1979 Base::internalEvaluate(writeInfo, primalVector, STMT_COMMON_CALL);
1980 }
1981 };
1982
1984 template<typename Stmt>
1986 : public StatementCallGeneratorBase<Stmt, StatementCallGenerator<StatementCall::ClearAdjoints, Stmt>> {
1987 public:
1990
1993
1995 template<typename Func>
1996 CODI_INLINE static void evaluateFull(Func const& func, size_t const& maxOutputArgs,
1997 size_t const& maxActiveArgs, size_t const& maxConstantArgs,
1998 ADJOINT_VECTOR_TYPE* adjointVector, STMT_COMMON_ARGS) {
1999 CODI_UNUSED(func);
2000
2001 StatementDataPointers pointers = {};
2002 pointers.populate(maxOutputArgs, maxActiveArgs, numberOfPassiveArguments, maxConstantArgs, byteData);
2003
2004 codiAssert(1 == maxOutputArgs);
2005
2006 // Only called from reuse tape.
2007 Identifier lhsIdentifier = pointers.lhsIdentifiers[0];
2008
2009#if CODI_VariableAdjointInterfaceInPrimalTapes
2010 adjointVector->resetAdjointVec(lhsIdentifier);
2011#else
2012 adjointVector[lhsIdentifier] = Gradient();
2013#endif
2014 }
2015
2017 CODI_INLINE static void evaluate(ADJOINT_VECTOR_TYPE* CODI_RESTRICT adjointVector, STMT_COMMON_ARGS) {
2018 Base::internalEvaluate(adjointVector, STMT_COMMON_CALL);
2019 }
2020 };
2021
2023 template<typename Stmt>
2024 struct StatementCallGenerator<StatementCall::Forward, Stmt> {
2026 static void evaluateInner() {
2027 CODI_EXCEPTION("Forward evaluation of jacobian statement not possible.");
2028 }
2029
2031 static void evaluate() {
2032 CODI_EXCEPTION("Forward evaluation of jacobian statement not possible.");
2033 }
2034 };
2035
2037 template<typename Stmt>
2040 static void evaluateInner() {
2041 CODI_EXCEPTION("Primal evaluation of jacobian statement not possible.");
2042 }
2043
2045 static void evaluate() {
2046 CODI_EXCEPTION("Primal evaluation of jacobian statement not possible.");
2047 }
2048 };
2049
2051 template<typename Stmt>
2053 : public StatementCallGeneratorBase<Stmt, StatementCallGenerator<StatementCall::Reverse, Stmt>> {
2054 public:
2057
2059 static void evaluateInner(Real* primalVector, ADJOINT_VECTOR_TYPE* adjointVector, Gradient* lhsAdjoints,
2060 PassiveReal const* const constantValues, Identifier const* const rhsIdentifiers) {
2061 CODI_UNUSED(constantValues);
2062
2063 evalJacobianReverse(adjointVector, lhsAdjoints[0], primalVector, rhsIdentifiers);
2064 }
2065
2067 static void evaluate(TapeImpl& tape, Gradient* lhsAdjoints, Real* primalVector,
2068 ADJOINT_VECTOR_TYPE* adjointVector, size_t& linearAdjointPos, STMT_COMMON_ARGS) {
2069 CODI_UNUSED(primalVector, lhsAdjoints, numberOfPassiveArguments);
2070
2071 StatementDataPointers pointers = {};
2072 pointers.populate(1, size, size, 0, byteData);
2073
2074 Identifier lhsIdentifier;
2075 if (LinearIndexHandling) {
2076 lhsIdentifier = linearAdjointPos;
2077 } else {
2078 lhsIdentifier = pointers.lhsIdentifiers[0];
2079 }
2080
2081#if CODI_VariableAdjointInterfaceInPrimalTapes
2082 Gradient const lhsAdjoint = {};
2083 EventSystem<TapeImpl>::notifyStatementEvaluateListeners(tape, lhsIdentifier, adjointVector->getVectorSize(),
2084 adjointVector->getAdjointVec(lhsIdentifier));
2085 adjointVector->setLhsAdjoint(lhsIdentifier);
2086#else
2087 Gradient const lhsAdjoint = adjointVector[lhsIdentifier];
2089 tape, lhsIdentifier, GradientTraits::dim<Gradient>(), GradientTraits::toArray(lhsAdjoint).data());
2090 adjointVector[lhsIdentifier] = Gradient();
2091#endif
2092
2094 primalVector[lhsIdentifier]);
2095 if (!LinearIndexHandling) {
2096 primalVector[lhsIdentifier] = pointers.oldLhsValues[0];
2097 }
2098
2099 evalJacobianReverse(adjointVector, lhsAdjoint, pointers.passiveValues, pointers.rhsIdentifiers);
2100 }
2101 };
2102
2104 template<typename Stmt>
2106 : public StatementCallGeneratorBase<Stmt, StatementCallGenerator<StatementCall::ResetPrimals, Stmt>> {
2107 public:
2110
2113
2115 template<typename Func>
2116 CODI_INLINE static void evaluateFull(Func const& evalInner, size_t const& maxOutputArgs,
2117 size_t const& maxActiveArgs, size_t const& maxConstantArgs,
2118 Real* primalVector, STMT_COMMON_ARGS) {
2119 CODI_UNUSED(evalInner, maxOutputArgs, maxActiveArgs, maxConstantArgs, numberOfPassiveArguments);
2120
2121 StatementDataPointers pointers = {};
2122 pointers.populate(1, size, size, 0, byteData);
2123
2124 codiAssert(1 == maxOutputArgs);
2125
2126 // Only called from reuse tape.
2127 Identifier lhsIdentifier = pointers.lhsIdentifiers[0];
2128 primalVector[lhsIdentifier] = pointers.oldLhsValues[0];
2129 }
2130
2132 CODI_INLINE static void evaluate(Real* CODI_RESTRICT primalVector, STMT_COMMON_ARGS) {
2133 Base::internalEvaluate(primalVector, STMT_COMMON_CALL);
2134 }
2135 };
2136
2138 template<typename Stmt>
2139 struct StatementCallGenerator<StatementCall::IterateInputs, Stmt>
2140 : public StatementCallGeneratorBase<Stmt, StatementCallGenerator<StatementCall::IterateInputs, Stmt>> {
2141 public:
2144
2147
2149 CODI_INLINE static void evaluateInner(size_t const& CODI_RESTRICT maxOutputArgs,
2150 size_t const& CODI_RESTRICT maxActiveArgs,
2151 size_t const& CODI_RESTRICT maxConstantArgs,
2152 size_t& CODI_RESTRICT linearAdjointPos, IterCallback func,
2153 void* userData, STMT_COMMON_ARGS) {
2154 CODI_UNUSED(linearAdjointPos);
2155
2156 StatementDataPointers pointers = {};
2157 pointers.populate(maxOutputArgs, maxActiveArgs, numberOfPassiveArguments, maxConstantArgs, byteData);
2158
2159 for (Config::ArgumentSize i = 0; i < maxActiveArgs; i += 1) {
2160 if (pointers.rhsIdentifiers[i] >= (Identifier)Config::MaxArgumentSize) {
2161 func(&pointers.rhsIdentifiers[i], userData);
2162 }
2163 }
2164 }
2165
2167 template<typename Func, typename... Args>
2168 CODI_INLINE static void evaluateFull(Func const& func, Args&&... args) {
2169 func(std::forward<Args>(args)...);
2170 }
2171
2173 CODI_INLINE static void evaluate(size_t& CODI_RESTRICT linearAdjointPos, IterCallback func, void* userData,
2174 STMT_COMMON_ARGS) {
2175 Base::internalEvaluate(linearAdjointPos, func, userData, STMT_COMMON_CALL);
2176 }
2177 };
2178
2179 /*******************************************************************************/
2181 template<typename Stmt>
2182 struct StatementCallGenerator<StatementCall::IterateOutputs, Stmt>
2183 : public StatementCallGeneratorBase<Stmt, StatementCallGenerator<StatementCall::IterateOutputs, Stmt>> {
2184 public:
2187 using StaticRhs = typename Base::StaticRhs;
2188
2191
2193 CODI_INLINE static void evaluateInner(size_t const& CODI_RESTRICT maxOutputArgs,
2194 size_t const& CODI_RESTRICT maxActiveArgs,
2195 size_t const& CODI_RESTRICT maxConstantArgs,
2196 size_t& CODI_RESTRICT linearAdjointPos, IterCallback func,
2197 void* userData, STMT_COMMON_ARGS) {
2198 StatementDataPointers pointers = {};
2199 pointers.populate(maxOutputArgs, maxActiveArgs, numberOfPassiveArguments, maxConstantArgs, byteData);
2200
2201 for (size_t iLhs = 0; iLhs < maxOutputArgs; iLhs += 1) {
2202 if (LinearIndexHandling) {
2203 Identifier lhsIdentifier = linearAdjointPos + 1 + iLhs;
2204 func(&lhsIdentifier, userData);
2205
2206 codiAssert(lhsIdentifier == (Identifier)(linearAdjointPos + 1 + iLhs));
2207 } else {
2208 func(&pointers.lhsIdentifiers[iLhs], userData);
2209 }
2210 }
2211 }
2212
2214 template<typename Func, typename... Args>
2215 CODI_INLINE static void evaluateFull(Func const& func, Args&&... args) {
2216 func(std::forward<Args>(args)...);
2217 }
2218
2220 CODI_INLINE static void evaluate(size_t& CODI_RESTRICT linearAdjointPos, IterCallback func, void* userData,
2221 STMT_COMMON_ARGS) {
2222 Base::internalEvaluate(linearAdjointPos, func, userData, STMT_COMMON_CALL);
2223 }
2224 };
2225
2227
2228 private:
2229 static void evalJacobianReverse(ADJOINT_VECTOR_TYPE* adjointVector, Gradient lhsAdjoint, Real const* const values,
2230 Identifier const* const rhsIdentifiers) {
2231#if CODI_VariableAdjointInterfaceInPrimalTapes
2232 CODI_UNUSED(lhsAdjoint);
2233 bool const lhsZero = adjointVector->isLhsZero();
2234#else
2235 bool const lhsZero = RealTraits::isTotalZero(lhsAdjoint);
2236#endif
2237
2239 for (size_t pos = 0; pos < size; pos += 1) {
2240 Real const& jacobian = values[pos];
2241#if CODI_VariableAdjointInterfaceInPrimalTapes
2242 adjointVector->updateAdjointWithLhs(rhsIdentifiers[pos], jacobian);
2243#else
2244 adjointVector[rhsIdentifiers[pos]] += jacobian * lhsAdjoint;
2245#endif
2246 }
2247 }
2248 }
2249 };
2250
2251#undef STMT_COMMON_ARGS
2252#undef STMT_COMMON_CALL
2253
2254#define CREATE_EXPRESSION(size) \
2255 TapeTypes::StatementEvaluator::template createHandle<Impl, JacobianStatementGenerator<Impl, size>, \
2256 AssignStatement<ActiveType<Impl>, JacobianExpression<size>>>()
2257
2259 template<typename TapeTypes, typename Impl>
2260 const CODI_DD(typename TapeTypes::EvalHandle,
2262 CREATE_EXPRESSION(0), CREATE_EXPRESSION(1), CREATE_EXPRESSION(2), CREATE_EXPRESSION(3),
2263 CREATE_EXPRESSION(4), CREATE_EXPRESSION(5), CREATE_EXPRESSION(6), CREATE_EXPRESSION(7),
2264 CREATE_EXPRESSION(8), CREATE_EXPRESSION(9), CREATE_EXPRESSION(10), CREATE_EXPRESSION(11),
2265 CREATE_EXPRESSION(12), CREATE_EXPRESSION(13), CREATE_EXPRESSION(14), CREATE_EXPRESSION(15),
2266 CREATE_EXPRESSION(16), CREATE_EXPRESSION(17), CREATE_EXPRESSION(18), CREATE_EXPRESSION(19),
2267 CREATE_EXPRESSION(20), CREATE_EXPRESSION(21), CREATE_EXPRESSION(22), CREATE_EXPRESSION(23),
2268 CREATE_EXPRESSION(24), CREATE_EXPRESSION(25), CREATE_EXPRESSION(26), CREATE_EXPRESSION(27),
2269 CREATE_EXPRESSION(28), CREATE_EXPRESSION(29), CREATE_EXPRESSION(30), CREATE_EXPRESSION(31),
2270 CREATE_EXPRESSION(32), CREATE_EXPRESSION(33), CREATE_EXPRESSION(34), CREATE_EXPRESSION(35),
2271 CREATE_EXPRESSION(36), CREATE_EXPRESSION(37), CREATE_EXPRESSION(38), CREATE_EXPRESSION(39),
2272 CREATE_EXPRESSION(40), CREATE_EXPRESSION(41), CREATE_EXPRESSION(42), CREATE_EXPRESSION(43),
2273 CREATE_EXPRESSION(44), CREATE_EXPRESSION(45), CREATE_EXPRESSION(46), CREATE_EXPRESSION(47),
2274 CREATE_EXPRESSION(48), CREATE_EXPRESSION(49), CREATE_EXPRESSION(50), CREATE_EXPRESSION(51),
2275 CREATE_EXPRESSION(52), CREATE_EXPRESSION(53), CREATE_EXPRESSION(54), CREATE_EXPRESSION(55),
2276 CREATE_EXPRESSION(56), CREATE_EXPRESSION(57), CREATE_EXPRESSION(58), CREATE_EXPRESSION(59),
2277 CREATE_EXPRESSION(60), CREATE_EXPRESSION(61), CREATE_EXPRESSION(62), CREATE_EXPRESSION(63),
2278 CREATE_EXPRESSION(64), CREATE_EXPRESSION(65), CREATE_EXPRESSION(66), CREATE_EXPRESSION(67),
2279 CREATE_EXPRESSION(68), CREATE_EXPRESSION(69), CREATE_EXPRESSION(70), CREATE_EXPRESSION(71),
2280 CREATE_EXPRESSION(72), CREATE_EXPRESSION(73), CREATE_EXPRESSION(74), CREATE_EXPRESSION(75),
2281 CREATE_EXPRESSION(76), CREATE_EXPRESSION(77), CREATE_EXPRESSION(78), CREATE_EXPRESSION(79),
2282 CREATE_EXPRESSION(80), CREATE_EXPRESSION(81), CREATE_EXPRESSION(82), CREATE_EXPRESSION(83),
2283 CREATE_EXPRESSION(84), CREATE_EXPRESSION(85), CREATE_EXPRESSION(86), CREATE_EXPRESSION(87),
2284 CREATE_EXPRESSION(88), CREATE_EXPRESSION(89), CREATE_EXPRESSION(90), CREATE_EXPRESSION(91),
2285 CREATE_EXPRESSION(92), CREATE_EXPRESSION(93), CREATE_EXPRESSION(94), CREATE_EXPRESSION(95),
2286 CREATE_EXPRESSION(96), CREATE_EXPRESSION(97), CREATE_EXPRESSION(98), CREATE_EXPRESSION(99),
2287 CREATE_EXPRESSION(100), CREATE_EXPRESSION(101), CREATE_EXPRESSION(102), CREATE_EXPRESSION(103),
2288 CREATE_EXPRESSION(104), CREATE_EXPRESSION(105), CREATE_EXPRESSION(106), CREATE_EXPRESSION(107),
2289 CREATE_EXPRESSION(108), CREATE_EXPRESSION(109), CREATE_EXPRESSION(110), CREATE_EXPRESSION(111),
2290 CREATE_EXPRESSION(112), CREATE_EXPRESSION(113), CREATE_EXPRESSION(114), CREATE_EXPRESSION(115),
2291 CREATE_EXPRESSION(116), CREATE_EXPRESSION(117), CREATE_EXPRESSION(118), CREATE_EXPRESSION(119),
2292 CREATE_EXPRESSION(120), CREATE_EXPRESSION(121), CREATE_EXPRESSION(122), CREATE_EXPRESSION(123),
2293 CREATE_EXPRESSION(124), CREATE_EXPRESSION(125), CREATE_EXPRESSION(126), CREATE_EXPRESSION(127),
2294 CREATE_EXPRESSION(128), CREATE_EXPRESSION(129), CREATE_EXPRESSION(130), CREATE_EXPRESSION(131),
2295 CREATE_EXPRESSION(132), CREATE_EXPRESSION(133), CREATE_EXPRESSION(134), CREATE_EXPRESSION(135),
2296 CREATE_EXPRESSION(136), CREATE_EXPRESSION(137), CREATE_EXPRESSION(138), CREATE_EXPRESSION(139),
2297 CREATE_EXPRESSION(140), CREATE_EXPRESSION(141), CREATE_EXPRESSION(142), CREATE_EXPRESSION(143),
2298 CREATE_EXPRESSION(144), CREATE_EXPRESSION(145), CREATE_EXPRESSION(146), CREATE_EXPRESSION(147),
2299 CREATE_EXPRESSION(148), CREATE_EXPRESSION(149), CREATE_EXPRESSION(150), CREATE_EXPRESSION(151),
2300 CREATE_EXPRESSION(152), CREATE_EXPRESSION(153), CREATE_EXPRESSION(154), CREATE_EXPRESSION(155),
2301 CREATE_EXPRESSION(156), CREATE_EXPRESSION(157), CREATE_EXPRESSION(158), CREATE_EXPRESSION(159),
2302 CREATE_EXPRESSION(160), CREATE_EXPRESSION(161), CREATE_EXPRESSION(162), CREATE_EXPRESSION(163),
2303 CREATE_EXPRESSION(164), CREATE_EXPRESSION(165), CREATE_EXPRESSION(166), CREATE_EXPRESSION(167),
2304 CREATE_EXPRESSION(168), CREATE_EXPRESSION(169), CREATE_EXPRESSION(170), CREATE_EXPRESSION(171),
2305 CREATE_EXPRESSION(172), CREATE_EXPRESSION(173), CREATE_EXPRESSION(174), CREATE_EXPRESSION(175),
2306 CREATE_EXPRESSION(176), CREATE_EXPRESSION(177), CREATE_EXPRESSION(178), CREATE_EXPRESSION(179),
2307 CREATE_EXPRESSION(180), CREATE_EXPRESSION(181), CREATE_EXPRESSION(182), CREATE_EXPRESSION(183),
2308 CREATE_EXPRESSION(184), CREATE_EXPRESSION(185), CREATE_EXPRESSION(186), CREATE_EXPRESSION(187),
2309 CREATE_EXPRESSION(188), CREATE_EXPRESSION(189), CREATE_EXPRESSION(190), CREATE_EXPRESSION(191),
2310 CREATE_EXPRESSION(192), CREATE_EXPRESSION(193), CREATE_EXPRESSION(194), CREATE_EXPRESSION(195),
2311 CREATE_EXPRESSION(196), CREATE_EXPRESSION(197), CREATE_EXPRESSION(198), CREATE_EXPRESSION(199),
2312 CREATE_EXPRESSION(200), CREATE_EXPRESSION(201), CREATE_EXPRESSION(202), CREATE_EXPRESSION(203),
2313 CREATE_EXPRESSION(204), CREATE_EXPRESSION(205), CREATE_EXPRESSION(206), CREATE_EXPRESSION(207),
2314 CREATE_EXPRESSION(208), CREATE_EXPRESSION(209), CREATE_EXPRESSION(210), CREATE_EXPRESSION(211),
2315 CREATE_EXPRESSION(212), CREATE_EXPRESSION(213), CREATE_EXPRESSION(214), CREATE_EXPRESSION(215),
2316 CREATE_EXPRESSION(216), CREATE_EXPRESSION(217), CREATE_EXPRESSION(218), CREATE_EXPRESSION(219),
2317 CREATE_EXPRESSION(220), CREATE_EXPRESSION(221), CREATE_EXPRESSION(222), CREATE_EXPRESSION(223),
2318 CREATE_EXPRESSION(224), CREATE_EXPRESSION(225), CREATE_EXPRESSION(226), CREATE_EXPRESSION(227),
2319 CREATE_EXPRESSION(228), CREATE_EXPRESSION(229), CREATE_EXPRESSION(230), CREATE_EXPRESSION(231),
2320 CREATE_EXPRESSION(232), CREATE_EXPRESSION(233), CREATE_EXPRESSION(234), CREATE_EXPRESSION(235),
2321 CREATE_EXPRESSION(236), CREATE_EXPRESSION(237), CREATE_EXPRESSION(238), CREATE_EXPRESSION(239),
2322 CREATE_EXPRESSION(240), CREATE_EXPRESSION(241), CREATE_EXPRESSION(242), CREATE_EXPRESSION(243),
2323 CREATE_EXPRESSION(244), CREATE_EXPRESSION(245), CREATE_EXPRESSION(246), CREATE_EXPRESSION(247),
2324 CREATE_EXPRESSION(248), CREATE_EXPRESSION(249), CREATE_EXPRESSION(250), CREATE_EXPRESSION(251),
2325 CREATE_EXPRESSION(252)};
2326
2327#undef CREATE_EXPRESSION
2328}
#define CODI_Unlikely
Declare unlikely evaluation of an execution path.
Definition config.h:408
#define CODI_LAMBDA_INLINE
See codi::Config::ForcedInlines.
Definition config.h:473
#define CODI_NO_INLINE
See codi::Config::AvoidedInlines.
Definition config.h:426
#define CODI_INLINE
See codi::Config::ForcedInlines.
Definition config.h:469
#define ADJOINT_VECTOR_TYPE
See codi::Config::VariableAdjointInterfaceInPrimalTapes.
Definition config.h:286
#define CODI_Likely
Declare likely evaluation of an execution path.
Definition config.h:406
#define CODI_RESTRICT
See codi::Config::Restrict.
Definition config.h:491
#define codiAssert(x)
See codi::Config::EnableAssert.
Definition config.h:441
#define CODI_DD(Type, Default)
Abbreviation for CODI_DECLARE_DEFAULT.
Definition macros.hpp:97
#define CODI_ENABLE_CHECK(option, condition)
Definition macros.hpp:62
#define CODI_WRAP_FUNCTION(NAME, FUNC)
Wrap a function in a function object. Used for performance optimizations.
Definition macros.hpp:167
#define CODI_ANY
Used in default declarations of expression templates.
Definition macros.hpp:101
#define CODI_STATIC_ASSERT(cond, message)
Static assert definition for CoDiPack. Not evaluated in IDE mode.
Definition macros.hpp:130
#define CODI_T(...)
Abbreviation for CODI_TEMPLATE.
Definition macros.hpp:117
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
const bool CheckEmptyStatements
Tapes push statements only if at least one Jacobian was pushed.
Definition config.h:154
size_t constexpr ChunkSize
Default size of chunks (ChunkBase) used in ChunkedData in reverse tape implementations.
Definition config.h:94
bool constexpr CheckZeroIndex
Ignore active types that are not dependent on any input value in Jacobian tapes.
Definition config.h:178
bool constexpr IgnoreInvalidJacobians
Ignore invalid Jacobians like NaN or Inf.
Definition config.h:240
bool constexpr CheckTapeActivity
Makes it possible to ignore certain code parts. If turned of everything will be recorded.
Definition config.h:170
size_t constexpr StatementLowLevelFunctionTag
Statement tag for low level functions.
Definition config.h:126
size_t constexpr MaxArgumentSize
Maximum number of arguments in a statement.
Definition config.h:120
bool constexpr StatementEvents
Enable statement events. Disabled by default.
Definition config.h:328
bool constexpr CopyOptimization
Do not store copy statements like a = b; if the identity handler allows it.
Definition config.h:186
bool constexpr VariableAdjointInterfaceInPrimalTapes
Allow custom adjoint vector in primal values tapes.
Definition config.h:281
uint8_t ArgumentSize
Type for the number of arguments in statements.
Definition config.h:117
size_t constexpr StatementInputTag
Tag for statements that are inputs. Used in linear index management context.
Definition config.h:123
bool constexpr SkipZeroAdjointEvaluation
Do not perform a reverse evaluation of a statement if the seeding adjoint is zero.
Definition config.h:256
inlinestd::array< AtomicTraits::RemoveAtomic< typename TraitsImplementation< Gradient >::Real >, TraitsImplementation< Gradient >::dim > toArray(Gradient const &gradient)
Converts the (possibly multi-component) gradient to an array of Reals.
Definition gradientTraits.hpp:116
inlinesize_t constexpr dim()
Number of dimensions this gradient value has.
Definition gradientTraits.hpp:96
inlinebool isTotalFinite(Type const &v)
Function for checking if all values of the type are finite.
Definition realTraits.hpp:139
inlinebool isTotalZero(Type const &v)
Function for checking if the value of the type is completely zero.
Definition realTraits.hpp:145
typename TraitsImplementation< Type >::PassiveReal PassiveReal
The original computation type that was used in the application.
Definition realTraits.hpp:123
CoDiPack - Code Differentiation Package.
Definition codi.hpp:97
IntegralType getNextMultiple(IntegralType const &targetSize, IntegralType const &chunkSize)
Helper function for overallocation in multiples of a given chunk size.
Definition mathUtility.hpp:49
std::string demangleName()
Return a string of the provided template name.
Definition demangleName.hpp:47
TapeParameters
Configuration options for a tape.
Definition tapeParameters.hpp:52
@ StatementByteSize
[A: RW] Allocated size of the byte data for the statement data.
Definition tapeParameters.hpp:67
@ LargestIdentifier
[A: R] Largest identifier distributed by the index manger.
Definition tapeParameters.hpp:60
@ PrimalSize
[A: RW] Number of primal vector entries in primal value tapes.
Definition tapeParameters.hpp:63
@ StatementSize
[A: RW] Allocated number of entries in the statement vector in all tapes.
Definition tapeParameters.hpp:66
@ AdjointSize
Definition tapeParameters.hpp:53
inlinevoid static_for(F func, Args &&... args)
Static for with i = 0 .. (N - 1). See CompileTimeLoop for details.
Definition compileTimeLoop.hpp:110
ComputeExpression< Real, EmptyOperation > EmptyExpression
Expression with no arguments for register input statements.
Definition emptyExpression.hpp:70
StatementCall
Defines all the operations which can be evaluated on a statement by a tape.
Definition statementEvaluatorTapeInterface.hpp:45
@ WriteInformation
Get write information.
Definition statementEvaluatorTapeInterface.hpp:51
@ ResetPrimals
Restore the primal values.
Definition statementEvaluatorTapeInterface.hpp:49
@ ClearAdjoints
Clear the adjoint values.
Definition statementEvaluatorTapeInterface.hpp:46
typename ArrayAccessExpressionImpl< Aggregated, element >::template Expression< Arg > ArrayAccessExpression
Expression that performs a[element] in a compile time context.
Definition arrayAccessExpression.hpp:98
inlineauto real(ExpressionInterface< std::complex< Real >, Arg > const &arg)
Function overload for FUNCTION.
Definition allOperators.hpp:75
inlinevoid CODI_UNUSED(Args const &...)
Disable unused warnings for an arbitrary number of arguments.
Definition macros.hpp:55
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
ChunkedData< Chunk, NestedData > DefaultChunkedData
ChunkData DataInterface used in all regular tapes.
Definition chunkedData.hpp:520
InnerActiveType values[Elements]
Array representation.
Definition aggregatedActiveType.hpp:75
Represents a concrete aggregated lvalue int the CoDiPack expression tree.
Definition aggregatedActiveType.hpp:164
Definition assignStatement.hpp:50
Definition byteDataView.hpp:51
Definition chunk.hpp:185
Definition chunk.hpp:400
inlinevoid internalStoreLowLevelFunction(Config::LowLevelFunctionToken token, size_t size, ByteDataView &dataView)
Definition commonTapeImplementation.hpp:519
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:202
Real manualPushLhsValue
For storeManual, remember the value assigned to the lhs.
Definition commonTapeImplementation.hpp:160
void setParameter(TapeParameters parameter, size_t value)
Definition commonTapeImplementation.hpp:484
void evaluateForward(AdjointsManagement adjointsManagement=AdjointsManagement::Automatic)
Definition commonTapeImplementation.hpp:652
size_t manualPushCounter
Count the pushes after storeManual, to identify the last push.
Definition commonTapeImplementation.hpp:163
inlinevoid resetTo(Position const &pos, bool resetAdjoints=true, AdjointsManagement adjointsManagement=AdjointsManagement::Automatic)
Definition commonTapeImplementation.hpp:776
Identifier manualPushLhsIdentifier
For storeManual, remember the identifier assigned to the lhs.
Definition commonTapeImplementation.hpp:161
bool isActive() const
Definition commonTapeImplementation.hpp:322
size_t manualPushGoal
Store the number of expected pushes after a storeManual call.
Definition commonTapeImplementation.hpp:162
void init(typename ImplTapeTypes::NestedData *nested)
Definition commonTapeImplementation.hpp:823
std::set< TapeParameters > options
All options.
Definition commonTapeImplementation.hpp:155
void writeTape(std::unique_ptr< TapeWriterInterface< Type > > writer)
Definition commonTapeImplementation.hpp:665
void evaluate(AdjointsManagement adjointsManagement=AdjointsManagement::Automatic)
Definition commonTapeImplementation.hpp:295
void swap(Impl &other)
Definition commonTapeImplementation.hpp:393
inlinevoid reset(bool resetAdjoints=true, AdjointsManagement adjointsManagement=AdjointsManagement::Automatic)
Definition commonTapeImplementation.hpp:357
typename CommonTapeTypes< ImplTapeTypes >::Position Position
Definition commonTapeImplementation.hpp:147
LowLevelFunctionByteData llfByteData
Byte data for low level functions.
Definition commonTapeImplementation.hpp:158
CommonTapeImplementation()
Definition commonTapeImplementation.hpp:234
size_t getParameter(TapeParameters parameter) const
Definition commonTapeImplementation.hpp:456
void evaluatePrimal()
Definition commonTapeImplementation.hpp:798
Helper class for the construction of an expression in a different context.
Definition constructStaticContext.hpp:70
Rhs ResultType
Definition constructStaticContext.hpp:86
static ResultType construct(Real *primalVector, Identifier const *const identifiers, PassiveReal const *const constantData)
Data stream interface for tape data. Encapsulates data that is written e.g. for each statement or arg...
Definition dataInterface.hpp:149
static inlinevoid notifyTapeRegisterInputListeners(Tape &tape, Real &value, Identifier &identifier)
Invoke callbacks for TapeRegisterInput events.
Definition eventSystem.hpp:424
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 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 inlinevoid notifyStatementEvaluatePrimalListeners(Tape &tape, Identifier const &lhsIdentifier, Real const &lhsValue)
Invoke callbacks for StatementEvaluatePrimal events.
Definition eventSystem.hpp:745
static inlinevoid notifyStatementEvaluateListeners(Tape &tape, Identifier const &lhsIdentifier, size_t sizeLhsAdjoint, Real const *lhsAdjoint)
Invoke callbacks for StatementEvaluate events.
Definition eventSystem.hpp:712
Base class for all CoDiPack expressions.
Definition expressionInterface.hpp:60
inlineImpl const & cast() const
Cast to the implementation.
Definition expressionInterface.hpp:76
static size_t constexpr value
Number of arguments.
Definition primalValueBaseTape.hpp:1906
Counts the number of nodes that inherit from LhsExpressionInterface in the expression.
Definition expressionTraits.hpp:258
static size_t constexpr value
See NumberOfActiveTypeArguments.
Definition expressionTraits.hpp:272
static size_t constexpr value
Always zero.
Definition primalValueBaseTape.hpp:1912
Counts the number of types that inherit from ConstantExpression in the expression.
Definition expressionTraits.hpp:282
static size_t constexpr value
See NumberOfConstantTypeArguments.
Definition expressionTraits.hpp:296
Implement logic for leaf nodes only.
Definition forEachLeafLogic.hpp:60
Indices enable the mapping of primal values to their adjoint counterparts.
Definition indexManagerInterface.hpp:78
Definition jacobianComputationLogic.hpp:54
Specialized for NumberOfActiveTypeArguments and NumberOfConstantTypeArguments.
Definition primalValueBaseTape.hpp:1901
inlinestatic void evaluate(Gradient *adjointVector, Config::ArgumentSize numberOfPassiveArguments, char *byteData)
Evaluate the full expression.
Definition primalValueBaseTape.hpp:2017
StatementCallGeneratorBase< Stmt, StatementCallGenerator< StatementCall::ClearAdjoints, Stmt > > Base
Base class abbreviation.
Definition primalValueBaseTape.hpp:1989
inlinestatic void evaluateInner()
Evaluate expression in a forward mode.
Definition primalValueBaseTape.hpp:1992
inlinestatic void evaluateFull(Func const &func, size_t const &maxOutputArgs, size_t const &maxActiveArgs, size_t const &maxConstantArgs, Gradient *adjointVector, Config::ArgumentSize numberOfPassiveArguments, char *byteData)
Load the expression data and evaluate the expression.
Definition primalValueBaseTape.hpp:1996
static void evaluateInner()
Throws exception.
Definition primalValueBaseTape.hpp:2026
static void evaluate()
Throws exception.
Definition primalValueBaseTape.hpp:2031
inlinestatic void evaluate(size_t &linearAdjointPos, IterCallback func, void *userData, Config::ArgumentSize numberOfPassiveArguments, char *byteData)
Evaluate the full expression.
Definition primalValueBaseTape.hpp:2173
StatementCallGeneratorBase< Stmt, StatementCallGenerator< StatementCall::IterateInputs, Stmt > > Base
Base class abbreviation.
Definition primalValueBaseTape.hpp:2143
inlinestatic void evaluateFull(Func const &func, Args &&... args)
Load the expression data and evaluate the expression.
Definition primalValueBaseTape.hpp:2168
typename LowLevelFunctionEntry< TapeImpl, Real, Identifier >::IterCallback IterCallback
Callback for identifier iteration.
Definition primalValueBaseTape.hpp:2146
inlinestatic void evaluateInner(size_t const &maxOutputArgs, size_t const &maxActiveArgs, size_t const &maxConstantArgs, size_t &linearAdjointPos, IterCallback func, void *userData, Config::ArgumentSize numberOfPassiveArguments, char *byteData)
Evaluate expression in a forward mode.
Definition primalValueBaseTape.hpp:2149
typename Base::StaticRhs StaticRhs
See StatementCallGeneratorBase.
Definition primalValueBaseTape.hpp:2187
typename LowLevelFunctionEntry< TapeImpl, Real, Identifier >::IterCallback IterCallback
Callback for identifier iteration.
Definition primalValueBaseTape.hpp:2190
inlinestatic void evaluateFull(Func const &func, Args &&... args)
Load the expression data and evaluate the expression.
Definition primalValueBaseTape.hpp:2215
inlinestatic void evaluateInner(size_t const &maxOutputArgs, size_t const &maxActiveArgs, size_t const &maxConstantArgs, size_t &linearAdjointPos, IterCallback func, void *userData, Config::ArgumentSize numberOfPassiveArguments, char *byteData)
Evaluate expression in a forward mode.
Definition primalValueBaseTape.hpp:2193
StatementCallGeneratorBase< Stmt, StatementCallGenerator< StatementCall::IterateOutputs, Stmt > > Base
Base class abbreviation.
Definition primalValueBaseTape.hpp:2186
inlinestatic void evaluate(size_t &linearAdjointPos, IterCallback func, void *userData, Config::ArgumentSize numberOfPassiveArguments, char *byteData)
Evaluate the full expression.
Definition primalValueBaseTape.hpp:2220
static void evaluateInner()
Throws exception.
Definition primalValueBaseTape.hpp:2040
static void evaluate()
Throws exception.
Definition primalValueBaseTape.hpp:2045
inlinestatic void evaluateInner()
Evaluate expression in a forward mode.
Definition primalValueBaseTape.hpp:2112
inlinestatic void evaluateFull(Func const &evalInner, size_t const &maxOutputArgs, size_t const &maxActiveArgs, size_t const &maxConstantArgs, Real *primalVector, Config::ArgumentSize numberOfPassiveArguments, char *byteData)
Load the expression data and evaluate the expression.
Definition primalValueBaseTape.hpp:2116
StatementCallGeneratorBase< Stmt, StatementCallGenerator< StatementCall::ResetPrimals, Stmt > > Base
Base class abbreviation.
Definition primalValueBaseTape.hpp:2109
inlinestatic void evaluate(Real *primalVector, Config::ArgumentSize numberOfPassiveArguments, char *byteData)
Evaluate the full expression.
Definition primalValueBaseTape.hpp:2132
StatementCallGeneratorBase< Stmt, StatementCallGenerator< StatementCall::Reverse, Stmt > > Base
Base class abbreviation.
Definition primalValueBaseTape.hpp:2056
static void evaluateInner(Real *primalVector, Gradient *adjointVector, Gradient *lhsAdjoints, PassiveReal const *const constantValues, Identifier const *const rhsIdentifiers)
Evaluate expression in a forward mode.
Definition primalValueBaseTape.hpp:2059
static void evaluate(TapeImpl &tape, Gradient *lhsAdjoints, Real *primalVector, Gradient *adjointVector, size_t &linearAdjointPos, Config::ArgumentSize numberOfPassiveArguments, char *byteData)
Evaluate the full expression.
Definition primalValueBaseTape.hpp:2067
StatementCallGeneratorBase< Stmt, StatementCallGenerator< StatementCall::WriteInformation, Stmt > > Base
Base class abbreviation.
Definition primalValueBaseTape.hpp:1955
inlinestatic void evaluateInner()
Evaluate expression in a forward mode.
Definition primalValueBaseTape.hpp:1958
inlinestatic void evaluateFull(Func const &func, size_t const &maxOutputArgs, size_t const &maxActiveArgs, size_t const &maxConstantArgs, WriteInfo &writeInfo, Real *primalVector, Config::ArgumentSize numberOfPassiveArguments, char *byteData)
Load the expression data and evaluate the expression.
Definition primalValueBaseTape.hpp:1962
inlinestatic void evaluate(WriteInfo &writeInfo, Real *primalVector, Config::ArgumentSize numberOfPassiveArguments, char *byteData)
Evaluate the full expression.
Definition primalValueBaseTape.hpp:1977
This structure is accessed by the StatementEvaluatorInterface.
Definition primalValueBaseTape.hpp:1947
Definition primalValueBaseTape.hpp:1919
typename TapeImpl::Identifier Identifier
See PrimalValueBaseTape.
Definition primalValueBaseTape.hpp:1930
typename TapeImpl::Real Real
See PrimalValueBaseTape.
Definition primalValueBaseTape.hpp:1928
typename TapeImpl::StatementDataPointers StatementDataPointers
Defined in PrimalValueBaseTape.
Definition primalValueBaseTape.hpp:1933
static size_t constexpr size
See JacobianStatementGenerator.
Definition primalValueBaseTape.hpp:1926
T_TapeImpl TapeImpl
PrimalValueBaseTape.
Definition primalValueBaseTape.hpp:1922
typename TapeImpl::PassiveReal PassiveReal
See PrimalValueBaseTape.
Definition primalValueBaseTape.hpp:1931
typename TapeImpl::template StatementCallGeneratorBase< Rhs, Impl > StatementCallGeneratorBase
Base for statement generators.
Definition primalValueBaseTape.hpp:1939
static size_t constexpr LinearIndexHandling
See PrimalValueBaseTape.
Definition primalValueBaseTape.hpp:1935
typename TapeImpl::Gradient Gradient
See PrimalValueBaseTape.
Definition primalValueBaseTape.hpp:1929
Default implementation of the Jacobian interface.
Definition jacobian.hpp:60
Base class for all CoDiPack lvalue expression.
Definition lhsExpressionInterface.hpp:63
inlineImpl & cast()
Cast to the implementation.
Definition lhsExpressionInterface.hpp:103
void(*)(Identifier *id, void *userData) IterCallback
Callback function for the identifier iteration.
Definition lowLevelFunctionEntry.hpp:81
Creates a math representation from a given rhs in the form of a string.
Definition mathStatementGenLogic.hpp:57
inlinevoid eval(NodeInterface< Node > const &node, std::string &mathRep)
Produces a math representation string for a given statement.
Definition mathStatementGenLogic.hpp:66
Defines a member that can either be static or local to the struct.
Definition memberStore.hpp:56
Implementation of VectorAccessInterface for adjoint and primal vectors.
Definition primalAdjointVectorAccess.hpp:59
Count all arguments that have non-zero index.
Definition primalValueBaseTape.hpp:402
inlinevoid handleActive(Node const &node, size_t &numberOfActiveArguments, IndexManager &indexManager)
Called for leaf nodes which implement LhsExpressionInterface.
Definition primalValueBaseTape.hpp:407
Perform the adjoint update based on the configuration in codi::Config::VariableAdjointInterfaceInPrim...
Definition primalValueBaseTape.hpp:796
inlinevoid handleJacobianOnActive(Node const &node, Real jacobian, Gradient &lhsTangent, Gradient *adjointVector)
Called for leaf nodes which implement LhsExpressionInterface.
Definition primalValueBaseTape.hpp:801
Perform the adjoint update based on the configuration in codi::Config::VariableAdjointInterfaceInPrim...
Definition primalValueBaseTape.hpp:850
inlinevoid handleJacobianOnActive(Node const &node, Real jacobian, Gradient const &lhsAdjoint, Gradient *adjointVector)
See IncrementReversalLogic.
Definition primalValueBaseTape.hpp:855
JacobianExtractionLogic()
Constructor.
Definition primalValueBaseTape.hpp:493
inlinevoid handleJacobianOnActive(Node const &node, Jacobian jacobianExpr, Identifier *rhsIdentifiers, Real *jacobians)
Stores the identifiers and Jacobians.
Definition primalValueBaseTape.hpp:497
Push all data for each argument.
Definition primalValueBaseTape.hpp:417
inlinevoid handleActive(Node const &node, StatementDataPointers &pointers, size_t &curPassiveArgument)
Called for leaf nodes which implement LhsExpressionInterface.
Definition primalValueBaseTape.hpp:422
inlinevoid handleConstant(Node const &node, StatementDataPointers &pointers, size_t &curPassiveArgument)
Called for leaf nodes which implement ConstantExpression.
Definition primalValueBaseTape.hpp:436
Base class for statement call generators. Prepares the static construction context.
Definition primalValueBaseTape.hpp:1347
ArrayAccessExpression< LhsReal, pos, StaticRhs > ExtractExpr
Extract expressions for aggregated types.
Definition primalValueBaseTape.hpp:1358
inlinestatic StaticRhs constructStaticRhs(Real *primalVector, PassiveReal const *const constantValues, Identifier const *const identifiers)
Construct the statement in the static context.
Definition primalValueBaseTape.hpp:1362
typename Lhs::Real LhsReal
Primal value of lhs.
Definition primalValueBaseTape.hpp:1351
typename Stmt::Lhs Lhs
See AssignStatement.
Definition primalValueBaseTape.hpp:1349
typename Constructor::ResultType StaticRhs
Static right hand side.
Definition primalValueBaseTape.hpp:1355
inlinestatic void internalEvaluate(Args &&... args)
Evaluate the full expression.
Definition primalValueBaseTape.hpp:1370
RealTraits::AggregatedTypeTraits< LhsReal > AggregateTraits
Traits for aggregated type.
Definition primalValueBaseTape.hpp:1352
typename Stmt::Rhs Rhs
See AssignStatement.
Definition primalValueBaseTape.hpp:1350
ConstructStaticContextLogic< Rhs, Impl, 0, 0 > Constructor
Static construction context.
Definition primalValueBaseTape.hpp:1354
inlinestatic void evaluate(Gradient *adjointVector, Config::ArgumentSize numberOfPassiveArguments, char *byteData)
Evaluate the full expression.
Definition primalValueBaseTape.hpp:1571
typename Base::StaticRhs StaticRhs
See StatementCallGeneratorBase.
Definition primalValueBaseTape.hpp:1542
StatementCallGeneratorBase< Stmt, StatementCallGenerator< StatementCall::ClearAdjoints, Stmt > > Base
Base class abbreviation.
Definition primalValueBaseTape.hpp:1541
inlinestatic void evaluateInner()
Evaluate expression in a forward mode.
Definition primalValueBaseTape.hpp:1545
inlinestatic void evaluateFull(Func const &evalInner, size_t const &maxOutputArgs, size_t const &maxActiveArgs, size_t const &maxConstantArgs, Gradient *adjointVector, Config::ArgumentSize numberOfPassiveArguments, char *byteData)
Load the expression data and evaluate the expression.
Definition primalValueBaseTape.hpp:1549
inlinestatic void evaluate(Impl &tape, Real *lhsPrimals, Gradient *lhsTangents, Real *primalVector, Gradient *adjointVector, size_t &linearAdjointPos, Config::ArgumentSize numberOfPassiveArguments, char *byteData)
Evaluate the full expression.
Definition primalValueBaseTape.hpp:1661
inlinestatic void evaluateInner(Real *primalVector, Gradient *adjointVector, Real *lhsPrimals, Gradient *lhsTangents, PassiveReal const *const constantValues, Identifier const *const rhsIdentifiers)
Evaluate expression in a forward mode.
Definition primalValueBaseTape.hpp:1590
inlinestatic void evaluateFull(Func const &evalInner, size_t const &maxOutputArgs, size_t const &maxActiveArgs, size_t const &maxConstantArgs, Impl &tape, Real *lhsPrimals, Gradient *lhsTangents, Real *primalVector, Gradient *adjointVector, size_t &linearAdjointPos, Config::ArgumentSize numberOfPassiveArguments, char *byteData)
Load the expression data and evaluate the expression.
Definition primalValueBaseTape.hpp:1614
StatementCallGeneratorBase< Stmt, StatementCallGenerator< StatementCall::Forward, Stmt > > Base
Base class abbreviation.
Definition primalValueBaseTape.hpp:1583
typename Base::template ExtractExpr< pos > ExtractExpr
See StatementCallGeneratorBase.
Definition primalValueBaseTape.hpp:1587
typename Base::StaticRhs StaticRhs
See StatementCallGeneratorBase.
Definition primalValueBaseTape.hpp:1584
typename Base::StaticRhs StaticRhs
See StatementCallGeneratorBase.
Definition primalValueBaseTape.hpp:1451
typename LowLevelFunctionEntry< Impl, Real, Identifier >::IterCallback IterCallback
See LowLevelFunctionEntry::IterCallback.
Definition primalValueBaseTape.hpp:1454
inlinestatic void evaluateInner(size_t const &maxOutputArgs, size_t const &maxActiveArgs, size_t const &maxConstantArgs, size_t &linearAdjointPos, IterCallback func, void *userData, Config::ArgumentSize numberOfPassiveArguments, char *byteData)
Evaluate expression in a forward mode.
Definition primalValueBaseTape.hpp:1457
inlinestatic void evaluate(size_t &linearAdjointPos, IterCallback func, void *userData, Config::ArgumentSize numberOfPassiveArguments, char *byteData)
Evaluate the full expression.
Definition primalValueBaseTape.hpp:1481
inlinestatic void evaluateFull(Func const &func, Args &&... args)
Load the expression data and evaluate the expression.
Definition primalValueBaseTape.hpp:1476
StatementCallGeneratorBase< Stmt, StatementCallGenerator< StatementCall::IterateInputs, Stmt > > Base
Base class abbreviation.
Definition primalValueBaseTape.hpp:1450
inlinestatic void evaluate(size_t &linearAdjointPos, IterCallback func, void *userData, Config::ArgumentSize numberOfPassiveArguments, char *byteData)
Evaluate the full expression.
Definition primalValueBaseTape.hpp:1528
typename Base::StaticRhs StaticRhs
See StatementCallGeneratorBase.
Definition primalValueBaseTape.hpp:1495
inlinestatic void evaluateInner(size_t const &maxOutputArgs, size_t const &maxActiveArgs, size_t const &maxConstantArgs, size_t &linearAdjointPos, IterCallback func, void *userData, Config::ArgumentSize numberOfPassiveArguments, char *byteData)
Evaluate expression in a forward mode.
Definition primalValueBaseTape.hpp:1501
typename LowLevelFunctionEntry< Impl, Real, Identifier >::IterCallback IterCallback
See LowLevelFunctionEntry::IterCallback.
Definition primalValueBaseTape.hpp:1498
StatementCallGeneratorBase< Stmt, StatementCallGenerator< StatementCall::IterateOutputs, Stmt > > Base
Base class abbreviation.
Definition primalValueBaseTape.hpp:1494
inlinestatic void evaluateFull(Func const &func, Args &&... args)
Load the expression data and evaluate the expression.
Definition primalValueBaseTape.hpp:1523
typename Base::template ExtractExpr< pos > ExtractExpr
See StatementCallGeneratorBase.
Definition primalValueBaseTape.hpp:1681
inlinestatic void evaluate(Impl &tape, Real *lhsPrimals, Real *primalVector, size_t &linearAdjointPos, Config::ArgumentSize numberOfPassiveArguments, char *byteData)
Evaluate the full expression.
Definition primalValueBaseTape.hpp:1733
StatementCallGeneratorBase< Stmt, StatementCallGenerator< StatementCall::Primal, Stmt > > Base
Base class abbreviation.
Definition primalValueBaseTape.hpp:1677
inlinestatic void evaluateFull(Func const &evalInner, size_t const &maxOutputArgs, size_t const &maxActiveArgs, size_t const &maxConstantArgs, Impl &tape, Real *lhsPrimals, Real *primalVector, size_t &linearAdjointPos, Config::ArgumentSize numberOfPassiveArguments, char *byteData)
Load the expression data and evaluate the expression.
Definition primalValueBaseTape.hpp:1698
inlinestatic void evaluateInner(Real *lhsPrimals, Real *primalVector, PassiveReal const *const constantValues, Identifier const *const rhsIdentifiers)
Evaluate expression in a forward mode.
Definition primalValueBaseTape.hpp:1684
typename Base::StaticRhs StaticRhs
See StatementCallGeneratorBase.
Definition primalValueBaseTape.hpp:1678
typename Base::StaticRhs StaticRhs
See StatementCallGeneratorBase.
Definition primalValueBaseTape.hpp:1842
inlinestatic void evaluate(Real *primalVector, Config::ArgumentSize numberOfPassiveArguments, char *byteData)
Evaluate the full expression.
Definition primalValueBaseTape.hpp:1867
inlinestatic void evaluateInner()
Evaluate expression in a forward mode.
Definition primalValueBaseTape.hpp:1845
StatementCallGeneratorBase< Stmt, StatementCallGenerator< StatementCall::ResetPrimals, Stmt > > Base
Base class abbreviation.
Definition primalValueBaseTape.hpp:1841
inlinestatic void evaluateFull(Func const &evalInner, size_t const &maxOutputArgs, size_t const &maxActiveArgs, size_t const &maxConstantArgs, Real *primalVector, Config::ArgumentSize numberOfPassiveArguments, char *byteData)
Load the expression data and evaluate the expression.
Definition primalValueBaseTape.hpp:1849
inlinestatic void evaluateInner(Real *primalVector, Gradient *adjointVector, Gradient *lhsAdjoints, PassiveReal const *const constantValues, Identifier const *const rhsIdentifiers)
Evaluate expression in a forward mode.
Definition primalValueBaseTape.hpp:1754
inlinestatic void evaluate(Impl &tape, Gradient *lhsAdjoints, Real *primalVector, Gradient *adjointVector, size_t &linearAdjointPos, Config::ArgumentSize numberOfPassiveArguments, char *byteData)
Evaluate the full expression.
Definition primalValueBaseTape.hpp:1826
inlinestatic void evaluateFull(Func const &evalInner, size_t const &maxOutputArgs, size_t const &maxActiveArgs, size_t const &maxConstantArgs, Impl &tape, Gradient *lhsAdjoints, Real *primalVector, Gradient *adjointVector, size_t &linearAdjointPos, Config::ArgumentSize numberOfPassiveArguments, char *byteData)
Load the expression data and evaluate the expression.
Definition primalValueBaseTape.hpp:1773
typename Base::template ExtractExpr< pos > ExtractExpr
See StatementCallGeneratorBase.
Definition primalValueBaseTape.hpp:1751
typename Base::StaticRhs StaticRhs
See StatementCallGeneratorBase.
Definition primalValueBaseTape.hpp:1748
StatementCallGeneratorBase< Stmt, StatementCallGenerator< StatementCall::Reverse, Stmt > > Base
Base class abbreviation.
Definition primalValueBaseTape.hpp:1747
inlinestatic void evaluate(WriteInfo &writeInfo, Real *primalVector, Config::ArgumentSize numberOfPassiveArguments, char *byteData)
Evaluate the full expression.
Definition primalValueBaseTape.hpp:1437
inlinestatic void evaluateFull(Func const &func, Args &&... args)
Load the expression data and evaluate the expression.
Definition primalValueBaseTape.hpp:1432
inlinestatic void evaluateInner(size_t const &maxOutputArgs, size_t const &maxActiveArgs, size_t const &maxConstantArgs, WriteInfo &writeInfo, Real *primalVector, Config::ArgumentSize numberOfPassiveArguments, char *byteData)
Evaluate expression in a forward mode.
Definition primalValueBaseTape.hpp:1399
StatementCallGeneratorBase< Stmt, StatementCallGenerator< StatementCall::WriteInformation, Stmt > > Base
Base class abbreviation.
Definition primalValueBaseTape.hpp:1395
typename Base::StaticRhs StaticRhs
See StatementCallGeneratorBase.
Definition primalValueBaseTape.hpp:1396
This structure is accessed by the StatementEvaluatorInterface.
Definition primalValueBaseTape.hpp:1382
Definition of the data for a statement. The pointers a populated from a byte data stream.
Definition primalValueBaseTape.hpp:311
inlinevoid populate(size_t lhsSize, size_t rhsSize, size_t passiveSize, size_t constantSize, char *byteData)
Set all the pointer from the given byte data. The sizes are used to compute the offsets.
Definition primalValueBaseTape.hpp:327
Real * passiveValues
Array for the passive values of a statement.
Definition primalValueBaseTape.hpp:314
inlineConfig::LowLevelFunctionDataSize computeSize(size_t lhsSize, size_t rhsSize, size_t passiveSize, size_t constantSize)
Computes the byte size for the data of the statement.
Definition primalValueBaseTape.hpp:352
PassiveReal * constantValues
Array for the constant values in the statement.
Definition primalValueBaseTape.hpp:316
Identifier * rhsIdentifiers
Array for the rhs identifiers.
Definition primalValueBaseTape.hpp:312
inlinevoid push(T *&array, T const &value)
Definition primalValueBaseTape.hpp:321
Real * oldLhsValues
Array for the old values of the lhs values.
Definition primalValueBaseTape.hpp:315
Identifier * lhsIdentifiers
Array for the lhs identifiers.
Definition primalValueBaseTape.hpp:313
Additional wrapper that triggers compiler optimizations.
Definition primalValueBaseTape.hpp:816
Wrapper helper for improved compiler optimizations.
Definition primalValueBaseTape.hpp:1299
Additional wrapper that triggers compiler optimizations.
Definition primalValueBaseTape.hpp:870
Base class for all standard Primal value tape implementations.
Definition primalValueBaseTape.hpp:134
MemberStore< IndexManager, Impl, TapeTypes::IsStaticIndexHandler > indexManager
Definition primalValueBaseTape.hpp:184
static EvalHandle const jacobianExpressions[Config::MaxArgumentSize]
Definition primalValueBaseTape.hpp:182
inlineGradient * getInternalAdjoints()
Obtain a representation of the tape's internal adjoint vector that can be used as custom adjoints.
Definition primalValueBaseTape.hpp:922
std::vector< Real > primalsCopy
Definition primalValueBaseTape.hpp:190
typename TapeTypes::EvalHandle EvalHandle
See PrimalValueTapeTypes.
Definition primalValueBaseTape.hpp:154
inlineConfig::LowLevelFunctionDataSize reserveStmtDataManual(size_t lhsSize, size_t rhsSize, size_t passiveSize, size_t constantSize, StatementDataPointers &pointers)
Reserve all the data for a statement.
Definition primalValueBaseTape.hpp:373
static bool constexpr RequiresPrimalRestore
Definition primalValueBaseTape.hpp:172
void beginUseAdjointVector()
Declare that the adjoint vector is being used. See Adjoint vector management.
Definition primalValueBaseTape.hpp:958
inlineReal internalRegisterInput(LhsExpressionInterface< Real, Gradient, Impl, Lhs > &value, bool unusedIndex)
Add a new input to the tape and update the primal value vector.
Definition primalValueBaseTape.hpp:671
inlinevoid store(LhsExpressionInterface< Real, Gradient, Impl, Lhs > &lhs, ExpressionInterface< Real, Rhs > const &rhs)
Has to be called by an AD variable every time it is assigned.
Definition primalValueBaseTape.hpp:597
inlinevoid evaluateForward(Position const &start, Position const &end, AdjointVector &&data)
Perform a forward evaluation of a part of the tape. It has to hold start <= end.
Definition primalValueBaseTape.hpp:917
inlinevoid registerInput(LhsExpressionInterface< Real, Gradient, Impl, Lhs > &value)
Definition primalValueBaseTape.hpp:705
PrimalValueBaseTape()
Constructor.
Definition primalValueBaseTape.hpp:233
inlinevoid destroyTapeData(Real &value, ActiveTypeTapeData &data)
Has to be called for each tape data, before it is deallocated.
Definition primalValueBaseTape.hpp:302
inlinebool storeArgumentAndStmtData(ExpressionInterface< RhsType, Rhs > const &rhs, StatementDataPointers &pointers)
Definition primalValueBaseTape.hpp:457
Gradient * selectAdjointVector(VectorAccess< AdjointVector > *vectorAccess, AdjointVector data)
Select the configured adjoint vector, see codi::Config::VariableAdjointInterfaceInPrimalTapes.
Definition primalValueBaseTape.hpp:781
T_Impl Impl
See PrimalValueBaseTape.
Definition primalValueBaseTape.hpp:142
void deleteAdjointVector()
Delete the adjoint vector. See Adjoint vector management.
Definition primalValueBaseTape.hpp:947
Real registerExternalFunctionOutput(LhsExpressionInterface< Real, Gradient, Impl, Lhs > &value)
Definition primalValueBaseTape.hpp:1042
StatementByteData statementByteData
Definition primalValueBaseTape.hpp:186
Real const & primal(Identifier const &identifier) const
Read only reference to primal value.
Definition primalValueBaseTape.hpp:1331
static bool constexpr HasPrimalValues
Definition primalValueBaseTape.hpp:169
CommonTapeImplementation< T_TapeTypes, T_Impl > Base
Base class abbreviation.
Definition primalValueBaseTape.hpp:144
typename TapeTypes::StatementEvaluator StatementEvaluator
See PrimalValueTapeTypes.
Definition primalValueBaseTape.hpp:150
void evaluateForwardKeepState(Position const &start, Position const &end, AdjointVector &&data)
Perform a tape evaluation but restore the state afterwards such that it is the same as when the evalu...
Definition primalValueBaseTape.hpp:1284
inlinevoid store(AggregatedActiveType< Aggregated, Type, Lhs > &lhs, ExpressionInterface< Aggregated, Rhs > const &rhs)
Definition primalValueBaseTape.hpp:512
inlinevoid resetTo(Position const &pos, bool resetAdjoints=true, AdjointsManagement adjointsManagement=AdjointsManagement::Automatic)
Reset the tape to the provided position.
Definition primalValueBaseTape.hpp:1232
std::vector< Real > primals
Definition primalValueBaseTape.hpp:189
typename TapeTypes::Real Real
See TapeTypesInterface.
Definition primalValueBaseTape.hpp:147
static void internalEvaluateReverse_EvalStatements(Args &&... args)
Perform a reverse evaluation of the tape. Arguments are from the recursive eval methods of the DataIn...
typename TapeTypes::StatementByteData StatementByteData
See PrimalValueTapeTypes.
Definition primalValueBaseTape.hpp:157
inlinevoid evaluate(Position const &start, Position const &end, AdjointVector &&data)
Perform a reverse evaluation for a part of the tape. It hast to hold start >= end.
Definition primalValueBaseTape.hpp:911
std::vector< Gradient > adjoints
Definition primalValueBaseTape.hpp:188
void evaluateForwardKeepState(Position const &start, Position const &end, AdjointsManagement adjointsManagement=AdjointsManagement::Automatic)
Perform a tape evaluation but restore the state afterwards such that it is the same as when the evalu...
Definition primalValueBaseTape.hpp:1271
typename TapeTypes::Identifier Identifier
See PrimalValueTapeTypes.
Definition primalValueBaseTape.hpp:151
inlinevoid clearAdjoints(AdjointsManagement adjointsManagement=AdjointsManagement::Automatic)
Clear all adjoint values, that is, set them to zero.
Definition primalValueBaseTape.hpp:713
T_TapeTypes TapeTypes
See PrimalValueBaseTape.
Definition primalValueBaseTape.hpp:138
Identifier * manualPushIdentifiers
Definition primalValueBaseTape.hpp:194
static bool constexpr AllowJacobianOptimization
Definition primalValueBaseTape.hpp:168
void evaluateKeepState(Position const &start, Position const &end, AdjointVector &&data)
Perform a tape evaluation but restore the state afterwards such that it is the same as when the evalu...
Definition primalValueBaseTape.hpp:1260
VectorAccess< Adjoint * > * createVectorAccessCustomAdjoints(Adjoint *data)
Definition primalValueBaseTape.hpp:1026
void evaluateKeepState(Position const &start, Position const &end, AdjointsManagement adjointsManagement=AdjointsManagement::Automatic)
Perform a tape evaluation but restore the state afterwards such that it is the same as when the evalu...
Definition primalValueBaseTape.hpp:1247
inlinevoid initTapeData(Real &value, ActiveTypeTapeData &data)
< Has to be called for each tape data, after it is allocated.
Definition primalValueBaseTape.hpp:294
inlinevoid store(AggregatedActiveType< Aggregated, Type, Lhs > &lhs, AggregatedActiveType< Aggregated, Type, Rhs > const &rhs)
Has to be called by an AD variable every time it is assigned.
Definition primalValueBaseTape.hpp:568
void evaluateForward(Position const &start, Position const &end, AdjointsManagement adjointsManagement=AdjointsManagement::Automatic)
Perform a forward evaluation of a part of the tape. It has to hold start <= end.
Definition primalValueBaseTape.hpp:1056
void deleteVectorAccess(VectorAccessInterface< Real, Identifier > *access)
See Adjoint vector access.
Definition primalValueBaseTape.hpp:1031
void evaluatePrimal(Position const &start, Position const &end)
Perform a full (forward) reevaluation of the primals in the tape.
Definition primalValueBaseTape.hpp:1311
void evaluatePrimal()
Perform a full (forward) reevaluation of the primals in the tape.
Definition commonTapeImplementation.hpp:798
void storeManual(Real const &lhsValue, ActiveTypeTapeData &lhsData, Config::ArgumentSize const &size)
Definition primalValueBaseTape.hpp:1098
std::array< T, Config::MaxArgumentSize > StackArray
Used for generating arrays for lhs handling.
Definition primalValueBaseTape.hpp:177
Real * getPrimalVector()
Get the pointer to the internal primal value vector.
Definition primalValueBaseTape.hpp:1336
void writeTape(codi::TapeWriterInterface< Type > *writer, Position const &start, Position const &end)
For full or partial tapes using a pointer to the writer.
Definition primalValueBaseTape.hpp:1178
static void internalEvaluatePrimal_EvalStatements(Args &&... args)
Perform a primal evaluation of the tape. Arguments are from the recursive eval methods of the DataInt...
inlineGradient & gradient(Identifier const &identifier, AdjointsManagement adjointsManagement=AdjointsManagement::Automatic)
Reference access to gradient.
Definition primalValueBaseTape.hpp:262
void setParameter(TapeParameters parameter, size_t value)
See Parameters functions.
Definition primalValueBaseTape.hpp:989
Real & primal(Identifier const &identifier)
Definition primalValueBaseTape.hpp:1326
VectorAccess< AdjointVector > * createVectorAccessCustomAdjoints(AdjointVector &&data)
Definition primalValueBaseTape.hpp:1019
static void internalEvaluateForward_EvalStatements(Args &&... args)
Perform a forward evaluation of the tape. Arguments are from the recursive eval methods of the DataIn...
static bool constexpr LinearIndexHandling
Definition primalValueBaseTape.hpp:170
void internalEvaluateForward(Position const &start, Position const &end, AdjointVector &&data)
Internal method for the forward evaluation of the whole tape.
Definition primalValueBaseTape.hpp:820
void internalResetPrimalValues(Position const &pos)
Reset the primal values to the given position.
inlinevoid store(LhsExpressionInterface< Real, Gradient, Impl, Lhs > &lhs, LhsExpressionInterface< Real, Gradient, Impl, Rhs > const &rhs)
Has to be called by an AD variable every time it is assigned.
Definition primalValueBaseTape.hpp:637
typename TapeTypes::ActiveTypeTapeData ActiveTypeTapeData
See TapeTypesInterface.
Definition primalValueBaseTape.hpp:152
inlinevoid pushLowLevelFunction(Config::LowLevelFunctionToken token, size_t size, ByteDataView &data)
Push a low level function to the tape.
Definition primalValueBaseTape.hpp:1202
inlineGradient const & gradient(Identifier const &identifier, AdjointsManagement adjointsManagement=AdjointsManagement::Automatic) const
Constant reference access to gradient.
Definition primalValueBaseTape.hpp:276
inlinevoid internalEvaluateReverse(Position const &start, Position const &end, AdjointVector &&data)
Internal method for the reverse evaluation of the whole tape.
Definition primalValueBaseTape.hpp:874
RealTraits::PassiveReal< Real > PassiveReal
Basic computation type.
Definition primalValueBaseTape.hpp:159
typename Base::Position Position
See TapeTypesInterface.
Definition primalValueBaseTape.hpp:162
void pushJacobianManual(Real const &jacobian, Real const &value, ActiveTypeTapeData const &data)
Definition primalValueBaseTape.hpp:1073
typename TapeTypes::Gradient Gradient
See TapeTypesInterface.
Definition primalValueBaseTape.hpp:148
VectorAccess< Gradient * > * createVectorAccess()
See Adjoint vector access.
Definition primalValueBaseTape.hpp:1013
typename StatementData::Position NestedPosition
See PrimalValueTapeTypes.
Definition primalValueBaseTape.hpp:161
void createStatementManual(Config::ArgumentSize const &nOutputValues, Identifier const *const lhsIndices, Real const *const lhsValues, Config::ArgumentSize const &nActiveValues, Identifier const *const rhsIdentifiers, Config::ArgumentSize const &nPassiveValues, Real const *const rhsPrimals, Config::ArgumentSize const &nConstants, Real const *const rhsConstant, EvalHandle const &evalHandle)
Definition primalValueBaseTape.hpp:1129
inlineTapeValues internalGetTapeValues() const
Definition primalValueBaseTape.hpp:740
void resizeAdjointVector()
Explicitly trigger resizing of the adjoint vector. See Adjoint vector management.
Definition primalValueBaseTape.hpp:952
typename TapeTypes::StatementData StatementData
See PrimalValueTapeTypes.
Definition primalValueBaseTape.hpp:156
void endUseAdjointVector()
Declare that the adjoint vector is no longer used. See Adjoint vector management.
Definition primalValueBaseTape.hpp:962
inlinevoid evaluate(Position const &start, Position const &end, AdjointsManagement adjointsManagement=AdjointsManagement::Automatic)
Perform a reverse evaluation for a part of the tape. It hast to hold start >= end.
Definition primalValueBaseTape.hpp:1218
PrimalAdjointVectorAccess< Real, Identifier, AdjointVector > VectorAccess
Vector access type generated by this tape.
Definition primalValueBaseTape.hpp:166
inlinevoid reset(bool resetAdjoints=true, AdjointsManagement adjointsManagement=AdjointsManagement::Automatic)
Reset the tape to the initial state for a fresh recording.
Definition primalValueBaseTape.hpp:725
inlineConfig::LowLevelFunctionDataSize reserveStmtData(size_t activeArguments, StatementDataPointers &pointers)
Reserve all the data for a statement.
Definition primalValueBaseTape.hpp:391
inlinevoid swap(Impl &other)
Swap all data with an other tape.
Definition primalValueBaseTape.hpp:932
StatementData statementData
Definition primalValueBaseTape.hpp:185
inlinevoid store(LhsExpressionInterface< Real, Gradient, Impl, Lhs > &lhs, Real const &rhs)
Has to be called by an AD variable every time it is assigned.
Definition primalValueBaseTape.hpp:656
size_t getParameter(TapeParameters parameter) const
See Parameters functions.
Definition primalValueBaseTape.hpp:965
IndexManager & getIndexManager()
Returns a reference to the Index Manager.
Definition primalValueBaseTape.hpp:1192
inlinevoid pushLhsData(Identifier const &index, Real const &oldPrimalValue, StatementDataPointers &pointers)
Add data for a lhs entry to the data streams.
Definition primalValueBaseTape.hpp:364
typename TapeTypes::IndexManager IndexManager
See TapeTypesInterface.
Definition primalValueBaseTape.hpp:149
Type definitions for the primal value tapes.
Definition primalValueBaseTape.hpp:82
T_Gradient Gradient
See PrimalValueTapeTypes.
Definition primalValueBaseTape.hpp:86
T_StatementEvaluator StatementEvaluator
See PrimalValueTapeTypes.
Definition primalValueBaseTape.hpp:88
typename IndexManager::Index Identifier
See IndexManagerInterface.
Definition primalValueBaseTape.hpp:94
Data< StatementByteChunk, StatementData > StatementByteData
Statement byte data vector.
Definition primalValueBaseTape.hpp:110
T_Data< Chunk, Nested > Data
See PrimalValueTapeTypes.
Definition primalValueBaseTape.hpp:91
static constexpr bool IsLinearIndexHandler
True if the index manager is linear.
Definition primalValueBaseTape.hpp:99
RealTraits::PassiveReal< Real > PassiveReal
Basic computation type.
Definition primalValueBaseTape.hpp:97
typename IndexManager::ActiveTypeIndexData ActiveTypeTapeData
Take the active real data from the index manager.
Definition primalValueBaseTape.hpp:95
T_Real Real
See PrimalValueTapeTypes.
Definition primalValueBaseTape.hpp:85
Chunk1< char > StatementByteChunk
Binary data for the statements.
Definition primalValueBaseTape.hpp:109
Data< StatementChunk, IndexManager > StatementData
Statement data vector.
Definition primalValueBaseTape.hpp:107
T_IndexManager IndexManager
See PrimalValueTapeTypes.
Definition primalValueBaseTape.hpp:87
static constexpr bool IsStaticIndexHandler
True if the index manager must be stored statically in the tape.
Definition primalValueBaseTape.hpp:100
typename StatementEvaluator::Handle EvalHandle
Handle type returned by the statement generator.
Definition primalValueBaseTape.hpp:103
Chunk3< Config::ArgumentSize, EvalHandle, Config::LowLevelFunctionDataSize > StatementChunk
Statement chunk contains <argument size, eval handle, data size of statement in bytes>.
Definition primalValueBaseTape.hpp:106
StatementByteData NestedData
See TapeTypesInterface.
Definition primalValueBaseTape.hpp:112
Methods that access inner values of aggregated types that contain CoDiPack active types.
Definition realTraits.hpp:233
Tape side interface for StatementEvaluatorInterface.
Definition statementEvaluatorTapeInterface.hpp:100
Creation of handles for the evaluation of expressions in a context where the expression type is not a...
Definition statementEvaluatorInterface.hpp:103
Tape side interface for StatementEvaluatorInterface.
Definition statementEvaluatorTapeInterface.hpp:73
Interface for the definition of tape types.
Definition commonTapeImplementation.hpp:64
Tape information that can be printed in a pretty print format or a table format.
Definition tapeValues.hpp:75
void addUnsignedLongEntry(std::string const &name, unsigned long const &value, LocalReductionOperation operation=LocalReductionOperation::Sum)
Add unsigned long entry.
Definition tapeValues.hpp:163
void addDoubleEntry(std::string const &name, double const &value, LocalReductionOperation operation=LocalReductionOperation::Sum, bool usedMem=false, bool allocatedMem=false)
Add double entry. If it is a memory entry, it should be in bytes.
Definition tapeValues.hpp:137
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
virtual void start(Tape &tape)
Destructor.
Definition tapeReaderWriterInterface.hpp:144
virtual void finish()
After all the statements have been written, the finish method finalizes the writing process.
Definition tapeReaderWriterInterface.hpp:177
inlinevoid eval(NodeInterface< Node > const &node, Args &&... args)
Start the evaluation of the logic on the given expression.
Definition traversalLogic.hpp:71
inlinevoid node(Node const &node, Args &&... args)
Definition traversalLogic.hpp:87
Unified access to the adjoint vector and primal vector in a tape evaluation.
Definition vectorAccessInterface.hpp:94
This class is used during the writing process of a primal value tape. The WriteInfo is returned by St...
Definition tapeReaderWriterInterface.hpp:69
size_t numberOfActiveArguments
Number of active arguments.
Definition tapeReaderWriterInterface.hpp:71
std::string stmtExpression
Used to generate a .hpp file for reading back a primal value tape.
Definition tapeReaderWriterInterface.hpp:73
size_t numberOfOutputArguments
Number of output arguments.
Definition tapeReaderWriterInterface.hpp:70
std::string mathRepresentation
Definition tapeReaderWriterInterface.hpp:74
size_t numberOfConstantArguments
Number of constant arguments.
Definition tapeReaderWriterInterface.hpp:72