CoDiPack  2.3.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-2024 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/lhsExpressionInterface.hpp"
45#include "../expressions/logic/compileTimeTraversalLogic.hpp"
46#include "../expressions/logic/constructStaticContext.hpp"
47#include "../expressions/logic/helpers/forEachLeafLogic.hpp"
48#include "../expressions/logic/helpers/jacobianComputationLogic.hpp"
49#include "../expressions/logic/helpers/mathStatementGenLogic.hpp"
50#include "../expressions/logic/traversalLogic.hpp"
51#include "../misc/demangleName.hpp"
52#include "../misc/macros.hpp"
53#include "../misc/mathUtility.hpp"
54#include "../misc/memberStore.hpp"
55#include "../traits/expressionTraits.hpp"
56#include "commonTapeImplementation.hpp"
57#include "data/chunk.hpp"
58#include "data/chunkedData.hpp"
59#include "indices/indexManagerInterface.hpp"
60#include "misc/primalAdjointVectorAccess.hpp"
61#include "statementEvaluators/statementEvaluatorInterface.hpp"
62#include "statementEvaluators/statementEvaluatorTapeInterface.hpp"
64namespace codi {
65
76 template<typename T_Real, typename T_Gradient, typename T_IndexManager, template<typename> class T_StatementEvaluator,
77 template<typename, typename> class T_Data>
79 public:
80
81 using Real = CODI_DD(T_Real, double);
82 using Gradient = CODI_DD(T_Gradient, double);
84 using StatementEvaluator = CODI_DD(CODI_T(T_StatementEvaluator<Real>),
86 template<typename Chunk, typename Nested>
87 using Data = CODI_DD(CODI_T(T_Data<Chunk, Nested>),
89
90 using Identifier = typename IndexManager::Index;
92
93 constexpr static bool IsLinearIndexHandler = IndexManager::IsLinear;
94 constexpr static bool IsStaticIndexHandler =
95 IndexManager::NeedsStaticStorage;
96
97 using EvalHandle = typename StatementEvaluator::Handle;
98
102 typename std::conditional<IsLinearIndexHandler, Chunk2<Config::ArgumentSize, EvalHandle>,
105
108
111
114
116 };
117
134 template<typename T_TapeTypes, typename T_Impl>
135 struct PrimalValueBaseTape : public CommonTapeImplementation<T_TapeTypes, T_Impl>,
136 public StatementEvaluatorTapeInterface<typename T_TapeTypes::Real>,
137 public StatementEvaluatorInnerTapeInterface<typename T_TapeTypes::Real> {
138 public:
139
141 using TapeTypes = CODI_DD(T_TapeTypes,
146
148 friend Base;
149
150 using Real = typename TapeTypes::Real;
151 using Gradient = typename TapeTypes::Gradient;
152 using IndexManager = typename TapeTypes::IndexManager;
153 using StatementEvaluator = typename TapeTypes::StatementEvaluator;
154 using Identifier = typename TapeTypes::Identifier;
155
156 using EvalHandle = typename TapeTypes::EvalHandle;
157
158 using StatementData = typename TapeTypes::StatementData;
159 using RhsIdentifierData = typename TapeTypes::RhsIdentifierData;
160 using PassiveValueData = typename TapeTypes::PassiveValueData;
161 using ConstantValueData = typename TapeTypes::ConstantValueData;
162
164
165 using NestedPosition = typename ConstantValueData::Position;
166 using Position = typename Base::Position;
167
169 template<typename AdjointVector>
171
172 static bool constexpr AllowJacobianOptimization = false;
173 static bool constexpr HasPrimalValues = true;
174 static bool constexpr LinearIndexHandling =
175 TapeTypes::IsLinearIndexHandler;
176 static bool constexpr RequiresPrimalRestore =
177 !TapeTypes::IsLinearIndexHandler;
178
179 protected:
180
182
188
189 std::vector<Gradient> adjoints;
190 std::vector<Real> primals;
191 std::vector<Real> primalsCopy;
192
193 private:
194
195 CODI_INLINE Impl const& cast() const {
196 return static_cast<Impl const&>(*this);
197 }
198
199 CODI_INLINE Impl& cast() {
200 return static_cast<Impl&>(*this);
201 }
202
203 protected:
204
205 /*******************************************************************************/
208
210 template<typename... Args>
211 static void internalEvaluateForward_EvalStatements(Args&&... args);
212
214 template<typename... Args>
215 static void internalEvaluatePrimal_EvalStatements(Args&&... args);
216
218 template<typename... Args>
219 static void internalEvaluateReverse_EvalStatements(Args&&... args);
220
223
225 void pushStmtData(Identifier const& index, Config::ArgumentSize const& numberOfPassiveArguments,
226 Real const& oldPrimalValue, EvalHandle evalHandle);
227
229
230 public:
231
234 : Base(),
235 indexManager(Config::MaxArgumentSize), // Reserve first items for passive values.
236 statementData(Config::ChunkSize),
237 // The following chunks must be large enough to store data for all arguments of one statement.
238 rhsIdentifierData(std::max(Config::ChunkSize, Config::MaxArgumentSize)),
239 passiveValueData(std::max(Config::ChunkSize, Config::MaxArgumentSize)),
240 constantValueData(std::max(Config::ChunkSize, Config::MaxArgumentSize)),
241 adjoints(1), // Ensure that adjoint[0] exists, see its use in gradient() const.
242 primals(0),
243 primalsCopy(0) {
244 checkPrimalSize(true);
245
246 statementData.setNested(&indexManager.get());
250
252
260 }
261
262 /*******************************************************************************/
265
271 if (AdjointsManagement::Automatic == adjointsManagement) {
272 checkAdjointSize(identifier);
273 }
274
275 codiAssert(identifier < (Identifier)adjoints.size());
276
277 return adjoints[identifier];
278 }
279
284 Identifier const& identifier, AdjointsManagement adjointsManagement = AdjointsManagement::Automatic) const {
285 codiAssert(identifier < (Identifier)adjoints.size());
286
287 if (AdjointsManagement::Automatic == adjointsManagement && identifier >= (Identifier)adjoints.size()) {
288 return adjoints[0];
289 } else {
290 return adjoints[identifier];
291 }
292 }
293
295 /*******************************************************************************/
298
300 template<typename Real>
301 CODI_INLINE void initIdentifier(Real& value, Identifier& identifier) {
302 CODI_UNUSED(value);
303
304 identifier = IndexManager::InactiveIndex;
305 }
306
308 template<typename Real>
309 CODI_INLINE void destroyIdentifier(Real& value, Identifier& identifier) {
310 CODI_UNUSED(value);
311
312 indexManager.get().template freeIndex<Impl>(identifier);
313 }
314
316
317 protected:
318
320 struct CountActiveArguments : public ForEachLeafLogic<CountActiveArguments> {
321 public:
322
324 template<typename Node>
325 CODI_INLINE void handleActive(Node const& node, size_t& numberOfActiveArguments) {
326 if (CODI_ENABLE_CHECK(Config::CheckZeroIndex, IndexManager::InactiveIndex != node.getIdentifier())) {
327 numberOfActiveArguments += 1;
328 }
329 }
330 };
331
333 struct PushIdentifierPassiveAndConstant : public ForEachLeafLogic<PushIdentifierPassiveAndConstant> {
334 public:
335
337 template<typename Node>
340 size_t& curPassiveArgument) {
342
343 Identifier rhsIndex = node.getIdentifier();
344 if (CODI_ENABLE_CHECK(Config::CheckZeroIndex, IndexManager::InactiveIndex == rhsIndex)) {
345 rhsIndex = curPassiveArgument;
346
347 curPassiveArgument += 1;
348 passiveValueData.pushData(node.getValue());
349 }
350
351 rhsIdentifierData.pushData(rhsIndex);
352 }
353
355 template<typename Node>
358 size_t& curPassiveArgument) {
359 CODI_UNUSED(rhsIdentifierData, passiveValueData, curPassiveArgument);
360
361 using ConversionOperator = typename Node::template ConversionOperator<PassiveReal>;
362
363 constantValueData.pushData(ConversionOperator::toDataStore(node.getValue()));
364 }
365 };
366
368 struct JacobianExtractionLogic : public JacobianComputationLogic<JacobianExtractionLogic> {
369 private:
370 size_t pos;
371
372 public:
373
376
378 template<typename Node, typename Jacobian>
379 CODI_INLINE void handleJacobianOnActive(Node const& node, Jacobian jacobianExpr, Identifier* rhsIdentifiers,
380 Real* jacobians) {
381 rhsIdentifiers[pos] = node.getIdentifier();
382 jacobians[pos] = ComputationTraits::adjointConversion<Real>(jacobianExpr);
383 pos++;
384 }
385 };
386
387 public:
388
390
392 template<typename Lhs, typename Rhs>
396 CountActiveArguments countActiveArguments;
398
399 size_t constexpr MaxActiveArgs = ExpressionTraits::NumberOfActiveTypeArguments<Rhs>::value;
400 size_t constexpr MaxConstantArgs = ExpressionTraits::NumberOfConstantTypeArguments<Rhs>::value;
401
402 codiAssert(MaxActiveArgs < Config::MaxArgumentSize);
403 codiAssert(MaxConstantArgs < Config::MaxArgumentSize);
404
405 size_t activeArguments = 0;
406 countActiveArguments.eval(rhs.cast(), activeArguments);
407
408 if (CODI_ENABLE_CHECK(Config::CheckEmptyStatements, 0 != activeArguments)) {
409 statementData.reserveItems(1);
410 rhsIdentifierData.reserveItems(MaxActiveArgs);
411 passiveValueData.reserveItems(MaxActiveArgs - activeArguments);
412 constantValueData.reserveItems(MaxConstantArgs);
413
414 size_t passiveArguments = 0;
415 pushAll.eval(rhs.cast(), rhsIdentifierData, passiveValueData, constantValueData, passiveArguments);
416
417 bool generatedNewIndex = indexManager.get().template assignIndex<Impl>(lhs.cast().getIdentifier());
418 checkPrimalSize(generatedNewIndex);
419
420 Real& primalEntry = primals[lhs.cast().getIdentifier()];
421 cast().pushStmtData(lhs.cast().getIdentifier(), passiveArguments, primalEntry,
422 StatementEvaluator::template createHandle<Impl, Impl, Rhs>());
423
424 primalEntry = rhs.cast().getValue();
425
427 JacobianExtractionLogic getRhsIdentifiersAndJacobians;
428 std::array<Identifier, MaxActiveArgs> rhsIdentifiers;
429 std::array<Real, MaxActiveArgs> jacobians;
430 getRhsIdentifiersAndJacobians.eval(rhs.cast(), Real(1.0), rhsIdentifiers.data(), jacobians.data());
431
433 rhs.cast().getValue(), MaxActiveArgs,
434 rhsIdentifiers.data(), jacobians.data());
435 }
436 } else {
437 indexManager.get().template freeIndex<Impl>(lhs.cast().getIdentifier());
438 }
439 } else {
440 indexManager.get().template freeIndex<Impl>(lhs.cast().getIdentifier());
441 }
442
443 lhs.cast().value() = rhs.cast().getValue();
444 }
445
448 template<typename Lhs, typename Rhs>
452 if (IndexManager::CopyNeedsStatement || !Config::CopyOptimization) {
453 store<Lhs, Rhs>(lhs, static_cast<ExpressionInterface<Real, Rhs> const&>(rhs));
454 return;
455 } else {
456 indexManager.get().template copyIndex<Impl>(lhs.cast().getIdentifier(), rhs.cast().getIdentifier());
457 }
458 } else {
459 indexManager.get().template freeIndex<Impl>(lhs.cast().getIdentifier());
460 }
461
462 lhs.cast().value() = rhs.cast().getValue();
463 }
464
467 template<typename Lhs>
469 indexManager.get().template freeIndex<Impl>(lhs.cast().getIdentifier());
470
471 lhs.cast().value() = rhs;
472 }
473
475 /*******************************************************************************
476 * Protected helper function for ReverseTapeInterface
477 */
478
479 protected:
480
482 template<typename Lhs>
484 bool unusedIndex) {
485 if (TapeTypes::IsLinearIndexHandler) {
486 statementData.reserveItems(1);
487 }
488
489 bool generatedNewIndex;
490 if (unusedIndex) {
491 generatedNewIndex = indexManager.get().template assignUnusedIndex<Impl>(value.cast().getIdentifier());
492 } else {
493 generatedNewIndex = indexManager.get().template assignIndex<Impl>(value.cast().getIdentifier());
494 }
495 checkPrimalSize(generatedNewIndex);
496
497 Real& primalEntry = primals[value.cast().getIdentifier()];
498 if (TapeTypes::IsLinearIndexHandler) {
499 cast().pushStmtData(value.cast().getIdentifier(), Config::StatementInputTag, primalEntry,
500 StatementEvaluator::template createHandle<Impl, Impl, Lhs>());
501 }
502
503 Real oldValue = primalEntry;
504 primalEntry = value.cast().value();
505
506 return oldValue;
507 }
508
509 public:
510
513
515 template<typename Lhs>
520
525 CODI_UNUSED(adjointsManagement);
526
527 size_t maxSize = std::min((size_t)indexManager.get().getLargestCreatedIndex() + 1, adjoints.size());
528 for (size_t i = 0; i < maxSize; i += 1) {
529 adjoints[i] = Gradient();
530 }
531 }
532
536 CODI_INLINE void reset(bool resetAdjoints = true,
538 for (Real& primal : primals) {
539 primal = Real();
540 }
541
542 Base::reset(resetAdjoints, adjointsManagement);
543 }
544
546
547 protected:
548
552 std::string name;
553 if (TapeTypes::IsLinearIndexHandler) {
554 name = "CoDi Tape Statistics ( PrimalValueLinearTape )";
555 } else {
556 name = "CoDi Tape Statistics ( PrimalValueReuseTape )";
557 }
558 TapeValues values = TapeValues(name);
559
560 size_t nAdjoints = indexManager.get().getLargestCreatedIndex();
561 double memoryAdjoints = static_cast<double>(nAdjoints) * static_cast<double>(sizeof(Gradient));
562
563 size_t nPrimals = indexManager.get().getLargestCreatedIndex();
564 double memoryPrimals = static_cast<double>(nPrimals) * static_cast<double>(sizeof(Real));
565
566 values.addSection("Adjoint vector");
567 values.addUnsignedLongEntry("Number of adjoints", nAdjoints);
568 values.addDoubleEntry("Memory allocated", memoryAdjoints, TapeValues::LocalReductionOperation::Sum, true, true);
569
570 values.addSection("Primal vector");
571 values.addUnsignedLongEntry("Number of primals", nPrimals);
572 values.addDoubleEntry("Memory allocated", memoryPrimals, TapeValues::LocalReductionOperation::Sum, true, true);
573
574 values.addSection("Index manager");
575 indexManager.get().addToTapeValues(values);
576
577 values.addSection("Statement entries");
578 statementData.addToTapeValues(values);
579 values.addSection("Rhs identifiers entries");
580 rhsIdentifierData.addToTapeValues(values);
581 values.addSection("Passive value entries");
582 passiveValueData.addToTapeValues(values);
583 values.addSection("Constant value entries");
584 constantValueData.addToTapeValues(values);
585
586 return values;
587 }
588
589 /******************************************************************************
590 * Protected helper function for CustomAdjointVectorEvaluationTapeInterface
591 */
592
594 template<typename AdjointVector>
596 CODI_UNUSED(vectorAccess, data);
597
598#if CODI_VariableAdjointInterfaceInPrimalTapes
599 return vectorAccess;
600#else
601 CODI_STATIC_ASSERT(CODI_T(std::is_same<typename std::remove_reference<AdjointVector>::type, Gradient*>::value),
602 "Please enable 'CODI_VariableAdjointInterfaceInPrimalTapes' in order"
603 " to use custom adjoint vectors in the primal value tapes.");
604
605 return data;
606#endif
607 }
608
610 struct IncrementForwardLogic : public JacobianComputationLogic<IncrementForwardLogic> {
611 public:
612
614 template<typename Node>
615 CODI_INLINE void handleJacobianOnActive(Node const& node, Real jacobian, Gradient& lhsTangent,
616 ADJOINT_VECTOR_TYPE* adjointVector) {
617 CODI_UNUSED(lhsTangent);
618
620#if CODI_VariableAdjointInterfaceInPrimalTapes
621 adjointVector->updateTangentWithLhs(node.getIdentifier(), jacobian);
622#else
623 lhsTangent += jacobian * adjointVector[node.getIdentifier()];
624#endif
625 }
626 }
627 };
628
630 CODI_WRAP_FUNCTION(Wrap_internalEvaluateForward_EvalStatements, Impl::internalEvaluateForward_EvalStatements);
631
633 template<bool copyPrimal, typename AdjointVector>
634 CODI_NO_INLINE void internalEvaluateForward(Position const& start, Position const& end, AdjointVector&& data) {
637 CODI_T(std::is_same<typename std::remove_reference<AdjointVector>::type, Gradient*>::value),
638 "Please enable 'CODI_VariableAdjointInterfaceInPrimalTapes' in order"
639 " to use custom adjoint vectors in the primal value tapes.");
640
641 std::vector<Real> primalsCopy(0);
642 Real* primalData = primals.data();
643
644 if (copyPrimal) {
646 primalData = primalsCopy.data();
647 }
648
649 VectorAccess<AdjointVector> vectorAccess(data, primalData);
650
651 ADJOINT_VECTOR_TYPE* dataVector = selectAdjointVector<AdjointVector>(&vectorAccess, data);
652
654 cast(), start, end, &vectorAccess, EventHints::EvaluationKind::Forward, EventHints::Endpoint::Begin);
655
657 Base::llfByteData.evaluateForward(start, end, evalFunc, cast(), primalData, dataVector);
658
659 EventSystem<Impl>::notifyTapeEvaluateListeners(cast(), start, end, &vectorAccess,
660 EventHints::EvaluationKind::Forward, EventHints::Endpoint::End);
661 }
662
664 struct IncrementReversalLogic : public JacobianComputationLogic<IncrementReversalLogic> {
665 public:
666
668 template<typename Node>
669 CODI_INLINE void handleJacobianOnActive(Node const& node, Real jacobian, Gradient const& lhsAdjoint,
670 ADJOINT_VECTOR_TYPE* adjointVector) {
671 CODI_UNUSED(lhsAdjoint);
672
674#if CODI_VariableAdjointInterfaceInPrimalTapes
675 adjointVector->updateAdjointWithLhs(node.getIdentifier(), jacobian);
676#else
677 adjointVector[node.getIdentifier()] += jacobian * lhsAdjoint;
678#endif
679 }
680 }
681 };
682
684 CODI_WRAP_FUNCTION(Wrap_internalEvaluateReverse_EvalStatements, Impl::internalEvaluateReverse_EvalStatements);
685
687 template<bool copyPrimal, typename AdjointVector>
688 CODI_INLINE void internalEvaluateReverse(Position const& start, Position const& end, AdjointVector&& data) {
691 CODI_T(std::is_same<typename std::remove_reference<AdjointVector>::type, Gradient*>::value),
692 "Please enable 'CODI_VariableAdjointInterfaceInPrimalTapes' in order"
693 " to use custom adjoint vectors in the primal value tapes.");
694
695 Real* primalData = primals.data();
696
697 if (copyPrimal) {
699 primalData = primalsCopy.data();
700 }
701
702 VectorAccess<AdjointVector> vectorAccess(data, primalData);
703
704 ADJOINT_VECTOR_TYPE* dataVector = selectAdjointVector<AdjointVector>(&vectorAccess, data);
705
707 cast(), start, end, &vectorAccess, EventHints::EvaluationKind::Reverse, EventHints::Endpoint::Begin);
708
710 Base::llfByteData.evaluateReverse(start, end, evalFunc, cast(), primalData, dataVector);
711
712 EventSystem<Impl>::notifyTapeEvaluateListeners(cast(), start, end, &vectorAccess,
713 EventHints::EvaluationKind::Reverse, EventHints::Endpoint::End);
714 }
715
716 public:
717
720
721 using Base::evaluate;
722
724 template<typename AdjointVector>
725 CODI_INLINE void evaluate(Position const& start, Position const& end, AdjointVector&& data) {
726 internalEvaluateReverse<!TapeTypes::IsLinearIndexHandler>(start, end, std::forward<AdjointVector>(data));
727 }
728
730 template<typename AdjointVector>
731 CODI_INLINE void evaluateForward(Position const& start, Position const& end, AdjointVector&& data) {
732 internalEvaluateForward<!TapeTypes::IsLinearIndexHandler>(start, end, std::forward<AdjointVector>(data));
733 }
734
737 return adjoints.data();
738 }
739
741 /*******************************************************************************/
744
746 CODI_INLINE void swap(Impl& other) {
747 // Index manager does not need to be swapped, it is either static or swapped with the vector data.
748 // Vectors are swapped recursively in the base class.
749
750 std::swap(adjoints, other.adjoints);
751 std::swap(primals, other.primals);
752
753 Base::swap(other);
754
755 // Ensure that the primals vector of both tapes are sized according to the index manager.
756 checkPrimalSize(true);
757 other.checkPrimalSize(true);
758 }
759
762 adjoints.resize(1);
763 }
764
767 checkAdjointSize(indexManager.get().getLargestCreatedIndex());
768 }
769
773
777
779 size_t getParameter(TapeParameters parameter) const {
780 switch (parameter) {
782 return adjoints.size();
783 break;
785 return constantValueData.getDataSize();
786 break;
788 return indexManager.get().getLargestCreatedIndex();
789 break;
791 return passiveValueData.getDataSize();
792 break;
794 return rhsIdentifierData.getDataSize();
795 break;
797 return primals.size();
798 break;
800 return statementData.getDataSize();
801 default:
802 return Base::getParameter(parameter);
803 break;
804 }
805 }
806
808 void setParameter(TapeParameters parameter, size_t value) {
809 switch (parameter) {
811 adjoints.resize(value);
812 break;
814 constantValueData.resize(value);
815 break;
817 CODI_EXCEPTION("Tried to set a get only option.");
818 break;
820 passiveValueData.resize(value);
821 break;
823 rhsIdentifierData.resize(value);
824 break;
826 primals.resize(value);
827 break;
829 return statementData.resize(value);
830 default:
831 Base::setParameter(parameter, value);
832 break;
833 }
834 }
835
840
842 template<typename AdjointVector>
844 return new VectorAccess<AdjointVector>(data, primals.data());
845 }
846
849 template<typename Adjoint>
851 return new VectorAccess<Adjoint*>(data, primals.data());
852 }
853
856 delete access;
857 }
858
860 /*******************************************************************************/
863
865 template<typename Lhs>
869
871 /*******************************************************************************/
874
876
880 void evaluateForward(Position const& start, Position const& end,
882 if (AdjointsManagement::Automatic == adjointsManagement) {
883 checkAdjointSize(indexManager.get().getLargestCreatedIndex());
884 }
885
886 codiAssert(indexManager.get().getLargestCreatedIndex() < (Identifier)adjoints.size());
887
888 cast().evaluateForward(start, end, adjoints.data());
889 }
890
892 /******************************************************************************
893 * Public helper function for ManualStatementPushTapeInterface
894 */
895
896 public:
897
900 template<size_t T_size>
902 public:
903
904 static size_t constexpr size = T_size;
905
906 /*******************************************************************************/
909
911 template<typename Expr, typename... Args>
912 static Real statementEvaluateForward(Args&&... args) {
913 CODI_UNUSED(args...);
914 CODI_EXCEPTION("Forward evaluation of jacobian statement not possible.");
915 }
916
918 template<typename Expr, typename... Args>
919 static Real statementEvaluatePrimal(Args&&... args) {
920 CODI_UNUSED(args...);
921 CODI_EXCEPTION("Primal evaluation of jacobian statement not possible.");
922 }
923
925 template<typename Expr>
926 static void statementEvaluateReverse(Real* primalVector, ADJOINT_VECTOR_TYPE* adjointVector,
927 Gradient lhsAdjoint, Config::ArgumentSize numberOfPassiveArguments,
928 size_t& curConstantPos, PassiveReal const* const constantValues,
929 size_t& curPassivePos, Real const* const passiveValues,
930 size_t& curRhsIdentifiersPos, Identifier const* const rhsIdentifiers) {
931 CODI_UNUSED(primalVector, curConstantPos, constantValues);
932
933 size_t endPos = curRhsIdentifiersPos - numberOfPassiveArguments;
934
935 bool const lhsZero = evalJacobianReverse(adjointVector, lhsAdjoint, curPassivePos, passiveValues,
936 curRhsIdentifiersPos, rhsIdentifiers, endPos);
937
938 if (Config::SkipZeroAdjointEvaluation && lhsZero) {
939 curPassivePos -= numberOfPassiveArguments;
940 curRhsIdentifiersPos -= numberOfPassiveArguments;
941 }
942 }
943
945 template<typename Expr, typename... Args>
947 CODI_UNUSED(args...);
948
949 WriteInfo writeInfo;
952 writeInfo.stmtExpression = "Impl, typename Impl::template JacobianStatementGenerator<" +
953 std::to_string(size) + ">, codi::JacobianExpression<" + std::to_string(size) +
954 ">";
955 writeInfo.mathRepresentation = "Jacobian statement";
956 return writeInfo;
957 }
958
960 /*******************************************************************************/
963
965 template<typename Expr, typename... Args>
966 static Real statementEvaluateForwardInner(Args&&... args) {
967 CODI_UNUSED(args...);
968 CODI_EXCEPTION("Forward evaluation of jacobian statement not possible.");
969
970 return Real();
971 }
972
974 template<typename Expr, typename... Args>
975 static Real statementEvaluatePrimalInner(Args&&... args) {
976 CODI_UNUSED(args...);
977 CODI_EXCEPTION("Primal evaluation of jacobian statement not possible.");
978
979 return Real();
980 }
981
983 template<typename Expr>
984 static void statementEvaluateReverseInner(Real* primalVector, ADJOINT_VECTOR_TYPE* adjointVector,
985 Gradient lhsAdjoint, size_t& curConstantPos,
986 PassiveReal const* const constantValues,
987 size_t& curRhsIdentifiersPos,
988 Identifier const* const rhsIdentifiers) {
989 CODI_UNUSED(primalVector, curConstantPos, constantValues);
990
991 size_t passivePos = size;
992 size_t rhsPos = curRhsIdentifiersPos + size;
993 size_t endPos = curRhsIdentifiersPos;
994
995 evalJacobianReverse(adjointVector, lhsAdjoint, passivePos, primalVector, rhsPos, rhsIdentifiers, endPos);
996 }
997
999
1000 private:
1001
1002 static bool evalJacobianReverse(ADJOINT_VECTOR_TYPE* adjointVector, Gradient lhsAdjoint,
1003 size_t& curPassivePos, Real const* const passiveValues,
1004 size_t& curRhsIdentifiersPos, Identifier const* const rhsIdentifiers,
1005 size_t endRhsIdentifiersPos) {
1006#if CODI_VariableAdjointInterfaceInPrimalTapes
1007 CODI_UNUSED(lhsAdjoint);
1008 bool const lhsZero = adjointVector->isLhsZero();
1009#else
1010 bool const lhsZero = RealTraits::isTotalZero(lhsAdjoint);
1011#endif
1012
1014 while (curRhsIdentifiersPos > endRhsIdentifiersPos) {
1015 curPassivePos -= 1;
1016 curRhsIdentifiersPos -= 1;
1017
1018 Real const& jacobian = passiveValues[curPassivePos];
1019#if CODI_VariableAdjointInterfaceInPrimalTapes
1020 adjointVector->updateAdjointWithLhs(rhsIdentifiers[curRhsIdentifiersPos], jacobian);
1021#else
1022 adjointVector[rhsIdentifiers[curRhsIdentifiersPos]] += jacobian * lhsAdjoint;
1023#endif
1024 }
1025 }
1026
1027 return lhsZero;
1028 }
1029 };
1030
1031 /*******************************************************************************/
1034
1036 void pushJacobianManual(Real const& jacobian, Real const& value, Identifier const& index) {
1037 CODI_UNUSED(value);
1038
1039 cast().incrementManualPushCounter();
1040
1041 passiveValueData.pushData(jacobian);
1042 rhsIdentifierData.pushData(index);
1043
1045 if (this->manualPushCounter == this->manualPushGoal) {
1046 // emit statement event
1047 Real* jacobians;
1048 Identifier* rhsIdentifiers;
1049 passiveValueData.getDataPointers(jacobians);
1050 rhsIdentifierData.getDataPointers(rhsIdentifiers);
1051 jacobians -= this->manualPushGoal;
1052 rhsIdentifiers -= this->manualPushGoal;
1053
1056 rhsIdentifiers, jacobians);
1057 }
1058 }
1059 }
1060
1062 void storeManual(Real const& lhsValue, Identifier& lhsIndex, Config::ArgumentSize const& size) {
1063 CODI_UNUSED(lhsValue);
1064
1066
1067 statementData.reserveItems(1);
1068 rhsIdentifierData.reserveItems(size);
1069 passiveValueData.reserveItems(size);
1070
1071 indexManager.get().template assignIndex<Impl>(lhsIndex);
1072 Real& primalEntry = primals[lhsIndex];
1073 cast().pushStmtData(lhsIndex, size, primalEntry, PrimalValueBaseTape::jacobianExpressions[size]);
1074
1075 primalEntry = lhsValue;
1076
1077 cast().initializeManualPushData(lhsValue, lhsIndex, size);
1078 }
1079
1081 /*******************************************************************************/
1084
1090 void createStatementManual(Identifier const& lhsIndex, Real const& lhsValue,
1091 Config::ArgumentSize const& nActiveValues, Identifier const* const rhsIdentifiers,
1092 Config::ArgumentSize const& nPassiveValues, Real const* const rhsPrimals,
1093 Config::ArgumentSize const& nConstants, Real const* const rhsConstant,
1094 EvalHandle const& evalHandle) {
1095 Impl& impl = cast();
1097 // TODO.
1098 } else if (Config::StatementInputTag == nPassiveValues && TapeTypes::IsLinearIndexHandler) CODI_Unlikely {
1099 statementData.reserveItems(1);
1100 if (TapeTypes::IsLinearIndexHandler) {
1101 primals[lhsIndex] = lhsValue;
1102 }
1103 impl.pushStmtData(lhsIndex, Config::StatementInputTag, lhsValue, evalHandle);
1104
1105 } else CODI_Likely {
1106 statementData.reserveItems(1);
1107 rhsIdentifierData.reserveItems(nActiveValues);
1108 passiveValueData.reserveItems(nPassiveValues);
1109 constantValueData.reserveItems(nConstants);
1110
1111 if (TapeTypes::IsLinearIndexHandler) {
1112 primals[lhsIndex] = lhsValue;
1113 }
1114
1115 impl.pushStmtData(lhsIndex, nPassiveValues, lhsValue, evalHandle);
1116 impl.initializeManualPushData(lhsValue, lhsIndex, nPassiveValues);
1117
1118 for (size_t activeCount = 0; activeCount < nActiveValues; activeCount++) {
1119 rhsIdentifierData.pushData(rhsIdentifiers[activeCount]);
1120 }
1121 for (size_t passiveCount = 0; passiveCount < nPassiveValues; passiveCount++) {
1122 cast().incrementManualPushCounter();
1123 passiveValueData.pushData(rhsPrimals[passiveCount]);
1124 }
1125 for (size_t constantCount = 0; constantCount < nConstants; constantCount++) {
1126 constantValueData.pushData(rhsConstant[constantCount]);
1127 }
1128 }
1129 }
1130
1131 using Base::writeTape;
1132
1137 template<typename Type>
1139 Position const& end) {
1140 Impl& impl = cast();
1141 writer->start(impl);
1142 Base::llfByteData.evaluateForward(start, end, Impl::template internalWriteTape<Type>, primals.data(),
1143 writer);
1144 writer->finish();
1145 }
1146
1148 /*******************************************************************************/
1151
1154 return indexManager.get();
1155 }
1156
1158 /*******************************************************************************/
1161
1164 statementData.reserveItems(1);
1165
1166 Base::internalStoreLowLevelFunction(token, size, data);
1167
1168 Identifier lhsIndex = Identifier();
1169 if (LinearIndexHandling) {
1170 indexManager.get().template assignIndex<Impl>(lhsIndex);
1171 }
1172 cast().pushStmtData(lhsIndex, Config::StatementLowLevelFunctionTag, Real(), EvalHandle());
1173 }
1174
1176 /*******************************************************************************/
1179
1183 CODI_INLINE void evaluate(Position const& start, Position const& end,
1185 if (AdjointsManagement::Automatic == adjointsManagement) {
1186 checkAdjointSize(indexManager.get().getLargestCreatedIndex());
1187 }
1188
1189 codiAssert(indexManager.get().getLargestCreatedIndex() < (Identifier)adjoints.size());
1190
1191 evaluate(start, end, adjoints.data());
1192 }
1193
1197 CODI_INLINE void resetTo(Position const& pos, bool resetAdjoints = true,
1199 cast().internalResetPrimalValues(pos);
1200
1201 Base::resetTo(pos, resetAdjoints, adjointsManagement);
1202 }
1203
1205 /*******************************************************************************/
1208
1212 void evaluateKeepState(Position const& start, Position const& end,
1214 if (AdjointsManagement::Automatic == adjointsManagement) {
1215 checkAdjointSize(indexManager.get().getLargestCreatedIndex());
1216 }
1217
1218 codiAssert(indexManager.get().getLargestCreatedIndex() < (Identifier)adjoints.size());
1219
1220 evaluateKeepState(start, end, adjoints.data());
1221 }
1222
1224 template<typename AdjointVector>
1225 void evaluateKeepState(Position const& start, Position const& end, AdjointVector&& data) {
1226 internalEvaluateReverse<false>(start, end, std::forward<AdjointVector>(data));
1227
1228 if (!TapeTypes::IsLinearIndexHandler) {
1229 evaluatePrimal(end, start);
1230 }
1231 }
1232
1236 void evaluateForwardKeepState(Position const& start, Position const& end,
1238 if (AdjointsManagement::Automatic == adjointsManagement) {
1239 checkAdjointSize(indexManager.get().getLargestCreatedIndex());
1240 }
1241
1242 codiAssert(indexManager.get().getLargestCreatedIndex() < (Identifier)adjoints.size());
1243
1244 evaluateForwardKeepState(start, end, adjoints.data());
1245 }
1246
1248 template<typename AdjointVector>
1249 void evaluateForwardKeepState(Position const& start, Position const& end, AdjointVector&& data) {
1250 if (!TapeTypes::IsLinearIndexHandler) {
1251 cast().internalResetPrimalValues(end);
1252 }
1253
1254 internalEvaluateForward<false>(start, end, std::forward<AdjointVector>(data));
1255 }
1256
1257 protected:
1258
1259 /******************************************************************************
1260 * Protected helper function for PrimalEvaluationTapeInterface
1261 */
1262
1264 CODI_WRAP_FUNCTION(Wrap_internalEvaluatePrimal_EvalStatements, Impl::internalEvaluatePrimal_EvalStatements);
1265
1266 public:
1267
1269 /*******************************************************************************/
1272
1274
1276 CODI_NO_INLINE void evaluatePrimal(Position const& start, Position const& end) {
1277 // TODO: implement primal value only accessor
1279
1280 EventSystem<Impl>::notifyTapeEvaluateListeners(cast(), start, end, &primalAdjointAccess,
1281 EventHints::EvaluationKind::Primal, EventHints::Endpoint::Begin);
1282
1284 Base::llfByteData.evaluateForward(start, end, evalFunc, cast(), primals.data());
1285
1286 EventSystem<Impl>::notifyTapeEvaluateListeners(cast(), start, end, &primalAdjointAccess,
1287 EventHints::EvaluationKind::Primal, EventHints::Endpoint::End);
1288 }
1289
1291 Real& primal(Identifier const& identifier) {
1292 return primals[identifier];
1293 }
1294
1296 Real const& primal(Identifier const& identifier) const {
1297 return primals[identifier];
1298 }
1299
1301 /*******************************************************************************/
1304
1306 template<typename Expr>
1307 static WriteInfo statementGetWriteInformation(Real* primalVector, Config::ArgumentSize numberOfPassiveArguments,
1308 size_t& curConstantPos, PassiveReal const* const constantValues,
1309 size_t& curPassivePos, Real const* const passiveValues,
1310 size_t& curRhsIdentifiersPos,
1311 Identifier const* const rhsIdentifiers) {
1312 if (Config::StatementInputTag != numberOfPassiveArguments) {
1313 for (Config::ArgumentSize curPos = 0; curPos < numberOfPassiveArguments; curPos += 1) {
1314 primalVector[curPos] = passiveValues[curPassivePos + curPos];
1315 }
1316 }
1317
1319 using StaticRhs = typename Constructor::ResultType;
1320
1321 StaticRhs staticRhs = Constructor::construct(primalVector, &rhsIdentifiers[curRhsIdentifiersPos],
1322 &constantValues[curConstantPos]);
1323
1324 WriteInfo writeInfo;
1327
1328 // The Impl template name is added to simplify the writer and reader interfaces EvalHandles. It represents the
1329 // first and second template args in the createHandle method. For the manual push, the second Impl template is
1330 // instead replaced with JacobianGenerator<size>.
1331
1332 writeInfo.stmtExpression = "Impl, Impl, ";
1333 writeInfo.stmtExpression += demangleName<Expr>();
1335 mathGen.eval(staticRhs, writeInfo.mathRepresentation);
1336
1337 return writeInfo;
1338 }
1339
1341 template<typename Rhs>
1342 static Real statementEvaluateForwardInner(Real* primalVector, ADJOINT_VECTOR_TYPE* adjointVector,
1343 Gradient& lhsTangent, size_t& curConstantPos,
1344 PassiveReal const* const constantValues, size_t& curRhsIdentifiersPos,
1345 Identifier const* const rhsIdentifiers) {
1347 using StaticRhs = typename Constructor::ResultType;
1348
1349 StaticRhs staticsRhs = Constructor::construct(primalVector, &rhsIdentifiers[curRhsIdentifiersPos],
1350 &constantValues[curConstantPos]);
1351
1352 IncrementForwardLogic incrementForward;
1353
1354 incrementForward.eval(staticsRhs, Real(1.0), lhsTangent, adjointVector);
1355 return staticsRhs.getValue();
1356 }
1357
1359 template<typename Func>
1360 static Real statementEvaluateForwardFull(Func const& evalInner, size_t const& maxActiveArgs,
1361 size_t const& maxConstantArgs, Real* primalVector,
1362 ADJOINT_VECTOR_TYPE* adjointVector, Gradient& lhsTangent,
1363 Config::ArgumentSize numberOfPassiveArguments, size_t& curConstantPos,
1364 PassiveReal const* const constantValues, size_t& curPassivePos,
1365 Real const* const passiveValues, size_t& curRhsIdentifiersPos,
1366 Identifier const* const rhsIdentifiers) {
1367 for (Config::ArgumentSize curPos = 0; curPos < numberOfPassiveArguments; curPos += 1) {
1368 primalVector[curPos] = passiveValues[curPassivePos + curPos];
1369 }
1370
1371 Real ret = evalInner(primalVector, adjointVector, lhsTangent, curConstantPos, constantValues,
1372 curRhsIdentifiersPos, rhsIdentifiers);
1373
1374 // Adapt vector positions.
1375 curConstantPos += maxConstantArgs;
1376 curPassivePos += numberOfPassiveArguments;
1377 curRhsIdentifiersPos += maxActiveArgs;
1378
1379 return ret;
1380 }
1381
1383 template<typename Rhs>
1384 static Real statementEvaluatePrimalInner(Real* primalVector, size_t& curConstantPos,
1385 PassiveReal const* const constantValues, size_t& curRhsIdentifiersPos,
1386 Identifier const* const rhsIdentifiers) {
1388 using StaticRhs = typename Constructor::ResultType;
1389
1390 StaticRhs staticsRhs = Constructor::construct(primalVector, &rhsIdentifiers[curRhsIdentifiersPos],
1391 &constantValues[curConstantPos]);
1392
1393 return staticsRhs.getValue();
1394 }
1395
1397 template<typename Func>
1398 static Real statementEvaluatePrimalFull(Func const& evalInner, size_t const& maxActiveArgs,
1399 size_t const& maxConstantArgs, Real* primalVector,
1400 Config::ArgumentSize numberOfPassiveArguments, size_t& curConstantPos,
1401 PassiveReal const* const constantValues, size_t& curPassivePos,
1402 Real const* const passiveValues, size_t& curRhsIdentifiersPos,
1403 Identifier const* const rhsIdentifiers) {
1404 for (Config::ArgumentSize curPos = 0; curPos < numberOfPassiveArguments; curPos += 1) {
1405 primalVector[curPos] = passiveValues[curPassivePos + curPos];
1406 }
1407
1408 Real ret = evalInner(primalVector, curConstantPos, constantValues, curRhsIdentifiersPos, rhsIdentifiers);
1409
1410 // Adapt vector positions.
1411 curConstantPos += maxConstantArgs;
1412 curPassivePos += numberOfPassiveArguments;
1413 curRhsIdentifiersPos += maxActiveArgs;
1414
1415 return ret;
1416 }
1417
1419 template<typename Rhs>
1420 CODI_INLINE static void statementEvaluateReverseInner(Real* primalVector, ADJOINT_VECTOR_TYPE* adjointVector,
1421 Gradient lhsAdjoint, size_t& curConstantPos,
1422 PassiveReal const* const constantValues,
1423 size_t& curRhsIdentifiersPos,
1424 Identifier const* const rhsIdentifiers) {
1426 using StaticRhs = typename Constructor::ResultType;
1427
1428 StaticRhs staticsRhs = Constructor::construct(primalVector, &rhsIdentifiers[curRhsIdentifiersPos],
1429 &constantValues[curConstantPos]);
1430
1431 IncrementReversalLogic incrementReverse;
1432
1433 incrementReverse.eval(staticsRhs, Real(1.0), const_cast<Gradient const&>(lhsAdjoint), adjointVector);
1434 }
1435
1437 template<typename Func>
1439 Func const& evalInner, size_t const& maxActiveArgs, size_t const& maxConstantArgs, Real* primalVector,
1440 ADJOINT_VECTOR_TYPE* adjointVector, Gradient lhsAdjoint, Config::ArgumentSize numberOfPassiveArguments,
1441 size_t& curConstantPos, PassiveReal const* const constantValues, size_t& curPassivePos,
1442 Real const* const passiveValues, size_t& curRhsIdentifiersPos, Identifier const* const rhsIdentifiers) {
1443 // Adapt vector positions.
1444 curConstantPos -= maxConstantArgs;
1445 curPassivePos -= numberOfPassiveArguments;
1446 curRhsIdentifiersPos -= maxActiveArgs;
1447
1448#if CODI_VariableAdjointInterfaceInPrimalTapes
1449 if (CODI_ENABLE_CHECK(Config::SkipZeroAdjointEvaluation, !adjointVector->isLhsZero())) CODI_Likely {
1450#else
1452#endif
1453 for (Config::ArgumentSize curPos = 0; curPos < numberOfPassiveArguments; curPos += 1) {
1454 primalVector[curPos] = passiveValues[curPassivePos + curPos];
1455 }
1456
1457 evalInner(primalVector, adjointVector, lhsAdjoint, curConstantPos, constantValues, curRhsIdentifiersPos,
1458 rhsIdentifiers);
1459 }
1460 }
1461
1463 /*******************************************************************************/
1466
1468 template<typename Rhs>
1469 static Real statementEvaluateForward(Real* primalVector, ADJOINT_VECTOR_TYPE* adjointVector, Gradient& lhsTangent,
1470 Config::ArgumentSize numberOfPassiveArguments, size_t& curConstantPos,
1471 PassiveReal const* const constantValues, size_t& curPassivePos,
1472 Real const* const passiveValues, size_t& curRhsIdentifiersPos,
1473 Identifier const* const rhsIdentifiers) {
1474 size_t constexpr MaxActiveArgs = ExpressionTraits::NumberOfActiveTypeArguments<Rhs>::value;
1475 size_t constexpr MaxConstantArgs = ExpressionTraits::NumberOfConstantTypeArguments<Rhs>::value;
1476
1477 return statementEvaluateForwardFull(statementEvaluateForwardInner<Rhs>, MaxActiveArgs, MaxConstantArgs,
1478 primalVector, adjointVector, lhsTangent, numberOfPassiveArguments,
1479 curConstantPos, constantValues, curPassivePos, passiveValues,
1480 curRhsIdentifiersPos, rhsIdentifiers);
1481 }
1482
1484 template<typename Rhs>
1485 static Real statementEvaluatePrimal(Real* primalVector, Config::ArgumentSize numberOfPassiveArguments,
1486 size_t& curConstantPos, PassiveReal const* const constantValues,
1487 size_t& curPassivePos, Real const* const passiveValues,
1488 size_t& curRhsIdentifiersPos, Identifier const* const rhsIdentifiers) {
1489 size_t constexpr MaxActiveArgs = ExpressionTraits::NumberOfActiveTypeArguments<Rhs>::value;
1490 size_t constexpr MaxConstantArgs = ExpressionTraits::NumberOfConstantTypeArguments<Rhs>::value;
1491
1492 return statementEvaluatePrimalFull(statementEvaluatePrimalInner<Rhs>, MaxActiveArgs, MaxConstantArgs,
1493 primalVector, numberOfPassiveArguments, curConstantPos, constantValues,
1494 curPassivePos, passiveValues, curRhsIdentifiersPos, rhsIdentifiers);
1495 }
1496
1498 template<typename Rhs>
1499 CODI_INLINE static void statementEvaluateReverse(Real* primalVector, ADJOINT_VECTOR_TYPE* adjointVector,
1500 Gradient lhsAdjoint,
1501 Config::ArgumentSize numberOfPassiveArguments,
1502 size_t& curConstantPos, PassiveReal const* const constantValues,
1503 size_t& curPassivePos, Real const* const passiveValues,
1504 size_t& curRhsIdentifiersPos,
1505 Identifier const* const rhsIdentifiers) {
1506 size_t constexpr maxActiveArgs = ExpressionTraits::NumberOfActiveTypeArguments<Rhs>::value;
1507 size_t constexpr maxConstantArgs = ExpressionTraits::NumberOfConstantTypeArguments<Rhs>::value;
1508 statementEvaluateReverseFull(statementEvaluateReverseInner<Rhs>, maxActiveArgs, maxConstantArgs, primalVector,
1509 adjointVector, lhsAdjoint, numberOfPassiveArguments, curConstantPos,
1510 constantValues, curPassivePos, passiveValues, curRhsIdentifiersPos,
1511 rhsIdentifiers);
1512 }
1513
1514 private:
1515
1516 CODI_INLINE void checkAdjointSize(Identifier const& identifier) {
1517 if (identifier >= (Identifier)adjoints.size()) {
1518 resizeAdjointsVector();
1519 }
1520 }
1521
1522 CODI_INLINE void checkPrimalSize(bool generatedNewIndex) {
1523 if (generatedNewIndex && indexManager.get().getLargestCreatedIndex() >= (Identifier)primals.size()) {
1524 resizePrimalVector();
1525 }
1526 }
1527
1528 CODI_NO_INLINE void resizeAdjointsVector() {
1529 // overallocate as next multiple of Config::ChunkSize
1530 adjoints.resize(getNextMultiple((size_t)indexManager.get().getLargestCreatedIndex() + 1, Config::ChunkSize));
1531 }
1532
1533 CODI_NO_INLINE void resizePrimalVector() {
1534 // overallocate as next multiple of Config::ChunkSize
1535 primals.resize(getNextMultiple((size_t)indexManager.get().getLargestCreatedIndex() + 1, Config::ChunkSize));
1536 }
1537 };
1538
1540 template<size_t size>
1542
1544 template<size_t size>
1546 static size_t constexpr value = size;
1547 };
1548
1550 template<size_t size>
1552 static size_t constexpr value = 0;
1553 };
1554
1555#define CREATE_EXPRESSION(size) \
1556 TapeTypes::StatementEvaluator::template createHandle<Impl, typename Impl::template JacobianStatementGenerator<size>, \
1557 JacobianExpression<size>>()
1558
1560 template<typename TapeTypes, typename Impl>
1561 const CODI_DD(typename TapeTypes::EvalHandle,
1563 CREATE_EXPRESSION(0), CREATE_EXPRESSION(1), CREATE_EXPRESSION(2), CREATE_EXPRESSION(3),
1564 CREATE_EXPRESSION(4), CREATE_EXPRESSION(5), CREATE_EXPRESSION(6), CREATE_EXPRESSION(7),
1565 CREATE_EXPRESSION(8), CREATE_EXPRESSION(9), CREATE_EXPRESSION(10), CREATE_EXPRESSION(11),
1566 CREATE_EXPRESSION(12), CREATE_EXPRESSION(13), CREATE_EXPRESSION(14), CREATE_EXPRESSION(15),
1567 CREATE_EXPRESSION(16), CREATE_EXPRESSION(17), CREATE_EXPRESSION(18), CREATE_EXPRESSION(19),
1568 CREATE_EXPRESSION(20), CREATE_EXPRESSION(21), CREATE_EXPRESSION(22), CREATE_EXPRESSION(23),
1569 CREATE_EXPRESSION(24), CREATE_EXPRESSION(25), CREATE_EXPRESSION(26), CREATE_EXPRESSION(27),
1570 CREATE_EXPRESSION(28), CREATE_EXPRESSION(29), CREATE_EXPRESSION(30), CREATE_EXPRESSION(31),
1571 CREATE_EXPRESSION(32), CREATE_EXPRESSION(33), CREATE_EXPRESSION(34), CREATE_EXPRESSION(35),
1572 CREATE_EXPRESSION(36), CREATE_EXPRESSION(37), CREATE_EXPRESSION(38), CREATE_EXPRESSION(39),
1573 CREATE_EXPRESSION(40), CREATE_EXPRESSION(41), CREATE_EXPRESSION(42), CREATE_EXPRESSION(43),
1574 CREATE_EXPRESSION(44), CREATE_EXPRESSION(45), CREATE_EXPRESSION(46), CREATE_EXPRESSION(47),
1575 CREATE_EXPRESSION(48), CREATE_EXPRESSION(49), CREATE_EXPRESSION(50), CREATE_EXPRESSION(51),
1576 CREATE_EXPRESSION(52), CREATE_EXPRESSION(53), CREATE_EXPRESSION(54), CREATE_EXPRESSION(55),
1577 CREATE_EXPRESSION(56), CREATE_EXPRESSION(57), CREATE_EXPRESSION(58), CREATE_EXPRESSION(59),
1578 CREATE_EXPRESSION(60), CREATE_EXPRESSION(61), CREATE_EXPRESSION(62), CREATE_EXPRESSION(63),
1579 CREATE_EXPRESSION(64), CREATE_EXPRESSION(65), CREATE_EXPRESSION(66), CREATE_EXPRESSION(67),
1580 CREATE_EXPRESSION(68), CREATE_EXPRESSION(69), CREATE_EXPRESSION(70), CREATE_EXPRESSION(71),
1581 CREATE_EXPRESSION(72), CREATE_EXPRESSION(73), CREATE_EXPRESSION(74), CREATE_EXPRESSION(75),
1582 CREATE_EXPRESSION(76), CREATE_EXPRESSION(77), CREATE_EXPRESSION(78), CREATE_EXPRESSION(79),
1583 CREATE_EXPRESSION(80), CREATE_EXPRESSION(81), CREATE_EXPRESSION(82), CREATE_EXPRESSION(83),
1584 CREATE_EXPRESSION(84), CREATE_EXPRESSION(85), CREATE_EXPRESSION(86), CREATE_EXPRESSION(87),
1585 CREATE_EXPRESSION(88), CREATE_EXPRESSION(89), CREATE_EXPRESSION(90), CREATE_EXPRESSION(91),
1586 CREATE_EXPRESSION(92), CREATE_EXPRESSION(93), CREATE_EXPRESSION(94), CREATE_EXPRESSION(95),
1587 CREATE_EXPRESSION(96), CREATE_EXPRESSION(97), CREATE_EXPRESSION(98), CREATE_EXPRESSION(99),
1588 CREATE_EXPRESSION(100), CREATE_EXPRESSION(101), CREATE_EXPRESSION(102), CREATE_EXPRESSION(103),
1589 CREATE_EXPRESSION(104), CREATE_EXPRESSION(105), CREATE_EXPRESSION(106), CREATE_EXPRESSION(107),
1590 CREATE_EXPRESSION(108), CREATE_EXPRESSION(109), CREATE_EXPRESSION(110), CREATE_EXPRESSION(111),
1591 CREATE_EXPRESSION(112), CREATE_EXPRESSION(113), CREATE_EXPRESSION(114), CREATE_EXPRESSION(115),
1592 CREATE_EXPRESSION(116), CREATE_EXPRESSION(117), CREATE_EXPRESSION(118), CREATE_EXPRESSION(119),
1593 CREATE_EXPRESSION(120), CREATE_EXPRESSION(121), CREATE_EXPRESSION(122), CREATE_EXPRESSION(123),
1594 CREATE_EXPRESSION(124), CREATE_EXPRESSION(125), CREATE_EXPRESSION(126), CREATE_EXPRESSION(127),
1595 CREATE_EXPRESSION(128), CREATE_EXPRESSION(129), CREATE_EXPRESSION(130), CREATE_EXPRESSION(131),
1596 CREATE_EXPRESSION(132), CREATE_EXPRESSION(133), CREATE_EXPRESSION(134), CREATE_EXPRESSION(135),
1597 CREATE_EXPRESSION(136), CREATE_EXPRESSION(137), CREATE_EXPRESSION(138), CREATE_EXPRESSION(139),
1598 CREATE_EXPRESSION(140), CREATE_EXPRESSION(141), CREATE_EXPRESSION(142), CREATE_EXPRESSION(143),
1599 CREATE_EXPRESSION(144), CREATE_EXPRESSION(145), CREATE_EXPRESSION(146), CREATE_EXPRESSION(147),
1600 CREATE_EXPRESSION(148), CREATE_EXPRESSION(149), CREATE_EXPRESSION(150), CREATE_EXPRESSION(151),
1601 CREATE_EXPRESSION(152), CREATE_EXPRESSION(153), CREATE_EXPRESSION(154), CREATE_EXPRESSION(155),
1602 CREATE_EXPRESSION(156), CREATE_EXPRESSION(157), CREATE_EXPRESSION(158), CREATE_EXPRESSION(159),
1603 CREATE_EXPRESSION(160), CREATE_EXPRESSION(161), CREATE_EXPRESSION(162), CREATE_EXPRESSION(163),
1604 CREATE_EXPRESSION(164), CREATE_EXPRESSION(165), CREATE_EXPRESSION(166), CREATE_EXPRESSION(167),
1605 CREATE_EXPRESSION(168), CREATE_EXPRESSION(169), CREATE_EXPRESSION(170), CREATE_EXPRESSION(171),
1606 CREATE_EXPRESSION(172), CREATE_EXPRESSION(173), CREATE_EXPRESSION(174), CREATE_EXPRESSION(175),
1607 CREATE_EXPRESSION(176), CREATE_EXPRESSION(177), CREATE_EXPRESSION(178), CREATE_EXPRESSION(179),
1608 CREATE_EXPRESSION(180), CREATE_EXPRESSION(181), CREATE_EXPRESSION(182), CREATE_EXPRESSION(183),
1609 CREATE_EXPRESSION(184), CREATE_EXPRESSION(185), CREATE_EXPRESSION(186), CREATE_EXPRESSION(187),
1610 CREATE_EXPRESSION(188), CREATE_EXPRESSION(189), CREATE_EXPRESSION(190), CREATE_EXPRESSION(191),
1611 CREATE_EXPRESSION(192), CREATE_EXPRESSION(193), CREATE_EXPRESSION(194), CREATE_EXPRESSION(195),
1612 CREATE_EXPRESSION(196), CREATE_EXPRESSION(197), CREATE_EXPRESSION(198), CREATE_EXPRESSION(199),
1613 CREATE_EXPRESSION(200), CREATE_EXPRESSION(201), CREATE_EXPRESSION(202), CREATE_EXPRESSION(203),
1614 CREATE_EXPRESSION(204), CREATE_EXPRESSION(205), CREATE_EXPRESSION(206), CREATE_EXPRESSION(207),
1615 CREATE_EXPRESSION(208), CREATE_EXPRESSION(209), CREATE_EXPRESSION(210), CREATE_EXPRESSION(211),
1616 CREATE_EXPRESSION(212), CREATE_EXPRESSION(213), CREATE_EXPRESSION(214), CREATE_EXPRESSION(215),
1617 CREATE_EXPRESSION(216), CREATE_EXPRESSION(217), CREATE_EXPRESSION(218), CREATE_EXPRESSION(219),
1618 CREATE_EXPRESSION(220), CREATE_EXPRESSION(221), CREATE_EXPRESSION(222), CREATE_EXPRESSION(223),
1619 CREATE_EXPRESSION(224), CREATE_EXPRESSION(225), CREATE_EXPRESSION(226), CREATE_EXPRESSION(227),
1620 CREATE_EXPRESSION(228), CREATE_EXPRESSION(229), CREATE_EXPRESSION(230), CREATE_EXPRESSION(231),
1621 CREATE_EXPRESSION(232), CREATE_EXPRESSION(233), CREATE_EXPRESSION(234), CREATE_EXPRESSION(235),
1622 CREATE_EXPRESSION(236), CREATE_EXPRESSION(237), CREATE_EXPRESSION(238), CREATE_EXPRESSION(239),
1623 CREATE_EXPRESSION(240), CREATE_EXPRESSION(241), CREATE_EXPRESSION(242), CREATE_EXPRESSION(243),
1624 CREATE_EXPRESSION(244), CREATE_EXPRESSION(245), CREATE_EXPRESSION(246), CREATE_EXPRESSION(247),
1625 CREATE_EXPRESSION(248), CREATE_EXPRESSION(249), CREATE_EXPRESSION(250), CREATE_EXPRESSION(251),
1626 CREATE_EXPRESSION(252)};
1627
1628#undef CREATE_EXPRESSION
1629}
#define CODI_Unlikely
Declare unlikely evaluation of an execution path.
Definition config.h:399
#define CODI_NO_INLINE
See codi::Config::AvoidedInlines.
Definition config.h:417
#define CODI_INLINE
See codi::Config::ForcedInlines.
Definition config.h:457
#define ADJOINT_VECTOR_TYPE
See codi::Config::VariableAdjointInterfaceInPrimalTapes.
Definition config.h:277
#define CODI_Likely
Declare likely evaluation of an execution path.
Definition config.h:397
#define codiAssert(x)
See codi::Config::EnableAssert.
Definition config.h:432
#define CODI_DD(Type, Default)
Abbreviation for CODI_DECLARE_DEFAULT.
Definition macros.hpp:94
#define CODI_ENABLE_CHECK(option, condition)
Definition macros.hpp:53
#define CODI_WRAP_FUNCTION(NAME, FUNC)
Wrap a function in a function object. Used for performance optimizations.
Definition macros.hpp:156
#define CODI_ANY
Used in default declarations of expression templates.
Definition macros.hpp:98
#define CODI_STATIC_ASSERT(cond, message)
Static assert in CoDiPack.
Definition macros.hpp:123
#define CODI_T(...)
Abbreviation for CODI_TEMPLATE.
Definition macros.hpp:111
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:319
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:272
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
bool isTotalFinite(Type const &v)
Function for checking if all values of the type are finite.
Definition realTraits.hpp:133
typename TraitsImplementation< Type >::PassiveReal PassiveReal
The original computation type, that was used in the application.
Definition realTraits.hpp:117
bool isTotalZero(Type const &v)
Function for checking if the value of the type is completely zero.
Definition realTraits.hpp:139
CoDiPack - Code Differentiation Package.
Definition codi.hpp:91
void CODI_UNUSED(Args const &...)
Disable unused warnings for an arbitrary number of arguments.
Definition macros.hpp:46
IntegralType getNextMultiple(IntegralType const &targetSize, IntegralType const &chunkSize)
Helper function for overallocation in multiples of a given chunk size.
Definition mathUtility.hpp:49
TapeParameters
Configuration options for a tape.
Definition tapeParameters.hpp:52
@ PassiveValuesSize
[A: RW] Allocated number of entries in the passive value vector in primal value tapes.
@ LargestIdentifier
[A: R] Largest identifier distributed by the index manger.
@ PrimalSize
[A: RW] Number of primal vector entries in primal value tapes.
@ StatementSize
[A: RW] Allocated number of entries in the statement vector in all tapes.
AdjointsManagement
Policies for management of the tape's interal adjoints.
Definition tapeParameters.hpp:98
@ Automatic
Manage internal adjoints automatically, including locking, bounds checking, and resizing.
Definition byteDataView.hpp:51
Definition chunk.hpp:185
Definition chunk.hpp:537
Data is stored chunk-wise in this DataInterface implementation. If a chunk runs out of space,...
Definition chunkedData.hpp:64
Implementation of all common tape functionality.
Definition commonTapeImplementation.hpp:131
Real manualPushLhsValue
For storeManual, remember the value assigned to the lhs.
Definition commonTapeImplementation.hpp:158
void setParameter(TapeParameters parameter, size_t value)
See Parameters functions.
Definition commonTapeImplementation.hpp:459
void evaluateForward(AdjointsManagement adjointsManagement=AdjointsManagement::Automatic)
Perform a forward evaluation of a part of the tape. It has to hold start <= end.
Definition commonTapeImplementation.hpp:592
size_t manualPushCounter
Count the pushes after storeManual, to identify the last push.
Definition commonTapeImplementation.hpp:161
Identifier manualPushLhsIdentifier
For storeManual, remember the identifier assigned to the lhs.
Definition commonTapeImplementation.hpp:159
bool isActive() const
Check if the tape is recording.
Definition commonTapeImplementation.hpp:320
size_t manualPushGoal
Store the number of expected pushes after a storeManual call.
Definition commonTapeImplementation.hpp:160
void init(typename ImplTapeTypes::NestedData *nested)
Initialize the base class.
Definition commonTapeImplementation.hpp:751
void reset(bool resetAdjoints=true, AdjointsManagement adjointsManagement=AdjointsManagement::Automatic)
Reset the tape to the initial state for a fresh recording.
Definition commonTapeImplementation.hpp:355
std::set< TapeParameters > options
All options.
Definition commonTapeImplementation.hpp:153
void writeTape(std::unique_ptr< TapeWriterInterface< Type > > writer)
For full-tape writers using a smart pointer.
Definition commonTapeImplementation.hpp:605
void evaluate(AdjointsManagement adjointsManagement=AdjointsManagement::Automatic)
Perform a full reverse evaluation of the tape.
Definition commonTapeImplementation.hpp:293
void swap(Impl &other)
Swap all data with an other tape.
Definition commonTapeImplementation.hpp:368
typename CommonTapeTypes< ImplTapeTypes >::Position Position
See TapeTypesInterface.
Definition commonTapeImplementation.hpp:145
LowLevelFunctionByteData llfByteData
Byte data for low level functions.
Definition commonTapeImplementation.hpp:156
void resetTo(Position const &pos, bool resetAdjoints=true, AdjointsManagement adjointsManagement=AdjointsManagement::Automatic)
Reset the tape to the provided position.
Definition commonTapeImplementation.hpp:704
size_t getParameter(TapeParameters parameter) const
See Parameters functions.
Definition commonTapeImplementation.hpp:431
void evaluatePrimal()
Perform a full (forward) reevaluation of the primals in the tape.
Definition commonTapeImplementation.hpp:726
void internalStoreLowLevelFunction(Config::LowLevelFunctionToken token, size_t size, ByteDataView &dataView)
Called by the implementing tapes to store a low level function. The size is reserved and allocated....
Definition commonTapeImplementation.hpp:494
Helper class for the construction of an expression in a different context.
Definition constructStaticContext.hpp:70
Data stream interface for tape data. Encapsulates data that is written e.g. for each statement or arg...
Definition dataInterface.hpp:149
static void notifyStatementStoreOnTapeListeners(Tape &tape, Identifier const &lhsIdentifier, Real const &newValue, size_t numActiveVariables, Identifier const *rhsIdentifiers, Real const *jacobians)
Invoke callbacks for StatementStoreOnTape events.
Definition eventSystem.hpp:676
static void notifyTapeEvaluateListeners(Tape &tape, Position const &start, Position const &end, VectorAccess *adjoint, EventHints::EvaluationKind evalKind, EventHints::Endpoint endpoint)
Invoke callbacks for TapeEvaluate events.
Definition eventSystem.hpp:486
static void notifyTapeRegisterInputListeners(Tape &tape, Real &value, Identifier &identifier)
Invoke callbacks for TapeRegisterInput events.
Definition eventSystem.hpp:424
Base class for all CoDiPack expressions.
Definition expressionInterface.hpp:59
Impl const & cast() const
Cast to the implementation.
Definition expressionInterface.hpp:75
Counts the number of nodes that inherit from LhsExpressionInterface in the expression.
Definition expressionTraits.hpp:184
Counts the number of types that inherit from ConstantExpression in the expression.
Definition expressionTraits.hpp:210
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:53
Specialized for NumberOfActiveTypeArguments and NumberOfConstantTypeArguments.
Definition primalValueBaseTape.hpp:1541
Default implementation of the Jacobian interface.
Definition jacobian.hpp:60
Base class for all CoDiPack lvalue expression.
Definition lhsExpressionInterface.hpp:63
Impl & cast()
Cast to the implementation.
Definition lhsExpressionInterface.hpp:99
Creates a math representation from a given rhs in the form of a string.
Definition mathStatementGenLogic.hpp:56
void eval(NodeInterface< Node > const &node, std::string &mathRep)
Produces a math representation string for a given statement.
Definition mathStatementGenLogic.hpp:65
Defines a member that can either be static or local to the struct.
Definition memberStore.hpp:56
Type & get()
Get a reference to the actual member.
Definition memberStore.hpp:76
Implementation of VectorAccessInterface for adjoint and primal vectors.
Definition primalAdjointVectorAccess.hpp:59
Count all arguments that have non-zero index.
Definition primalValueBaseTape.hpp:320
void handleActive(Node const &node, size_t &numberOfActiveArguments)
Called for leaf nodes which implement LhsExpressionInterface.
Definition primalValueBaseTape.hpp:325
Perform the adjoint update based on the configuration in codi::Config::VariableAdjointInterfaceInPrim...
Definition primalValueBaseTape.hpp:610
void handleJacobianOnActive(Node const &node, Real jacobian, Gradient &lhsTangent, Gradient *adjointVector)
Called for leaf nodes which implement LhsExpressionInterface.
Definition primalValueBaseTape.hpp:615
Perform the adjoint update based on the configuration in codi::Config::VariableAdjointInterfaceInPrim...
Definition primalValueBaseTape.hpp:664
void handleJacobianOnActive(Node const &node, Real jacobian, Gradient const &lhsAdjoint, Gradient *adjointVector)
See IncrementReversalLogic.
Definition primalValueBaseTape.hpp:669
Computes Jacobian entries for the event system.
Definition primalValueBaseTape.hpp:368
JacobianExtractionLogic()
Constructor.
Definition primalValueBaseTape.hpp:375
void handleJacobianOnActive(Node const &node, Jacobian jacobianExpr, Identifier *rhsIdentifiers, Real *jacobians)
Stores the identifiers and Jacobians.
Definition primalValueBaseTape.hpp:379
Definition primalValueBaseTape.hpp:901
static Real statementEvaluatePrimalInner(Args &&... args)
Throws exception.
Definition primalValueBaseTape.hpp:975
static Real statementEvaluateForwardInner(Args &&... args)
Throws exception.
Definition primalValueBaseTape.hpp:966
static Real statementEvaluateForward(Args &&... args)
Throws exception.
Definition primalValueBaseTape.hpp:912
static void statementEvaluateReverseInner(Real *primalVector, Gradient *adjointVector, Gradient lhsAdjoint, size_t &curConstantPos, PassiveReal const *const constantValues, size_t &curRhsIdentifiersPos, Identifier const *const rhsIdentifiers)
Evaluate expression in a reverse mode.
Definition primalValueBaseTape.hpp:984
static size_t constexpr size
See JacobianStatementGenerator.
Definition primalValueBaseTape.hpp:904
static void statementEvaluateReverse(Real *primalVector, Gradient *adjointVector, Gradient lhsAdjoint, Config::ArgumentSize numberOfPassiveArguments, size_t &curConstantPos, PassiveReal const *const constantValues, size_t &curPassivePos, Real const *const passiveValues, size_t &curRhsIdentifiersPos, Identifier const *const rhsIdentifiers)
Evaluate expression in a reverse mode.
Definition primalValueBaseTape.hpp:926
static WriteInfo statementGetWriteInformation(Args &&... args)
Get write information.
Definition primalValueBaseTape.hpp:946
static Real statementEvaluatePrimal(Args &&... args)
Throws exception.
Definition primalValueBaseTape.hpp:919
Push all data for each argument.
Definition primalValueBaseTape.hpp:333
void handleActive(Node const &node, RhsIdentifierData &rhsIdentifierData, PassiveValueData &passiveValueData, ConstantValueData &constantValueData, size_t &curPassiveArgument)
Called for leaf nodes which implement LhsExpressionInterface.
Definition primalValueBaseTape.hpp:338
void handleConstant(Node const &node, RhsIdentifierData &rhsIdentifierData, PassiveValueData &passiveValueData, ConstantValueData &constantValueData, size_t &curPassiveArgument)
Called for leaf nodes which implement ConstantExpression.
Definition primalValueBaseTape.hpp:356
Additional wrapper that triggers compiler optimizations.
Definition primalValueBaseTape.hpp:630
Wrapper helper for improved compiler optimizations.
Definition primalValueBaseTape.hpp:1264
Additional wrapper that triggers compiler optimizations.
Definition primalValueBaseTape.hpp:684
Base class for all standard Primal value tape implementations.
Definition primalValueBaseTape.hpp:137
MemberStore< IndexManager, Impl, TapeTypes::IsStaticIndexHandler > indexManager
Index manager.
Definition primalValueBaseTape.hpp:183
static WriteInfo statementGetWriteInformation(Real *primalVector, Config::ArgumentSize numberOfPassiveArguments, size_t &curConstantPos, PassiveReal const *const constantValues, size_t &curPassivePos, Real const *const passiveValues, size_t &curRhsIdentifiersPos, Identifier const *const rhsIdentifiers)
Get write information.
Definition primalValueBaseTape.hpp:1307
Gradient * getInternalAdjoints()
Obtain a representation of the tape's internal adjoint vector that can be used as custom adjoints.
Definition primalValueBaseTape.hpp:736
typename ConstantValueData::Position NestedPosition
See PrimalValueTapeTypes.
Definition primalValueBaseTape.hpp:165
std::vector< Real > primalsCopy
Copy of primal values for AD evaluations.
Definition primalValueBaseTape.hpp:191
typename TapeTypes::EvalHandle EvalHandle
See PrimalValueTapeTypes.
Definition primalValueBaseTape.hpp:156
static bool constexpr RequiresPrimalRestore
See PrimalEvaluationTapeInterface.
Definition primalValueBaseTape.hpp:176
void beginUseAdjointVector()
Declare that the adjoint vector is being used. See Adjoint vector management.
Definition primalValueBaseTape.hpp:772
void internalEvaluateReverse(Position const &start, Position const &end, AdjointVector &&data)
Internal method for the reverse evaluation of the whole tape.
Definition primalValueBaseTape.hpp:688
static Real statementEvaluateForwardFull(Func const &evalInner, size_t const &maxActiveArgs, size_t const &maxConstantArgs, Real *primalVector, Gradient *adjointVector, Gradient &lhsTangent, Config::ArgumentSize numberOfPassiveArguments, size_t &curConstantPos, PassiveReal const *const constantValues, size_t &curPassivePos, Real const *const passiveValues, size_t &curRhsIdentifiersPos, Identifier const *const rhsIdentifiers)
Load the expression data and evaluate the expression in a forward mode.
Definition primalValueBaseTape.hpp:1360
void resetTo(Position const &pos, bool resetAdjoints=true, AdjointsManagement adjointsManagement=AdjointsManagement::Automatic)
Reset the tape to the provided position.
Definition primalValueBaseTape.hpp:1197
PrimalValueBaseTape()
Constructor.
Definition primalValueBaseTape.hpp:233
void createStatementManual(Identifier const &lhsIndex, Real const &lhsValue, 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:1090
void 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:725
static void statementEvaluateReverseFull(Func const &evalInner, size_t const &maxActiveArgs, size_t const &maxConstantArgs, Real *primalVector, Gradient *adjointVector, Gradient lhsAdjoint, Config::ArgumentSize numberOfPassiveArguments, size_t &curConstantPos, PassiveReal const *const constantValues, size_t &curPassivePos, Real const *const passiveValues, size_t &curRhsIdentifiersPos, Identifier const *const rhsIdentifiers)
Load the expression data and evaluate the expression in a reverse mode.
Definition primalValueBaseTape.hpp:1438
PassiveValueData passiveValueData
Data stream for passive argument value data.
Definition primalValueBaseTape.hpp:186
Gradient * selectAdjointVector(VectorAccess< AdjointVector > *vectorAccess, AdjointVector data)
Select the configured adjoint vector, see codi::Config::VariableAdjointInterfaceInPrimalTapes.
Definition primalValueBaseTape.hpp:595
T_Impl Impl
See PrimalValueBaseTape.
Definition primalValueBaseTape.hpp:145
static void statementEvaluateReverseInner(Real *primalVector, Gradient *adjointVector, Gradient lhsAdjoint, size_t &curConstantPos, PassiveReal const *const constantValues, size_t &curRhsIdentifiersPos, Identifier const *const rhsIdentifiers)
Evaluate expression in a reverse mode.
Definition primalValueBaseTape.hpp:1420
void registerInput(LhsExpressionInterface< Real, Gradient, Impl, Lhs > &value)
Definition primalValueBaseTape.hpp:516
void deleteAdjointVector()
Delete the adjoint vector. See Adjoint vector management.
Definition primalValueBaseTape.hpp:761
Real registerExternalFunctionOutput(LhsExpressionInterface< Real, Gradient, Impl, Lhs > &value)
Definition primalValueBaseTape.hpp:866
Real const & primal(Identifier const &identifier) const
Read only reference to primal value.
Definition primalValueBaseTape.hpp:1296
static bool constexpr HasPrimalValues
See PrimalEvaluationTapeInterface.
Definition primalValueBaseTape.hpp:173
typename TapeTypes::StatementEvaluator StatementEvaluator
See PrimalValueTapeTypes.
Definition primalValueBaseTape.hpp:153
static Real statementEvaluatePrimalInner(Real *primalVector, size_t &curConstantPos, PassiveReal const *const constantValues, size_t &curRhsIdentifiersPos, Identifier const *const rhsIdentifiers)
Evaluate expression in a primal setting.
Definition primalValueBaseTape.hpp:1384
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:1249
typename TapeTypes::PassiveValueData PassiveValueData
See PrimalValueTapeTypes.
Definition primalValueBaseTape.hpp:160
std::vector< Real > primals
Current state of primal values in the program.
Definition primalValueBaseTape.hpp:190
typename TapeTypes::Real Real
See TapeTypesInterface.
Definition primalValueBaseTape.hpp:150
static void internalEvaluateReverse_EvalStatements(Args &&... args)
Perform a reverse evaluation of the tape. Arguments are from the recursive eval methods of the DataIn...
std::vector< Gradient > adjoints
Evaluation vector for AD.
Definition primalValueBaseTape.hpp:189
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:1236
void 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:1183
static Real statementEvaluateForwardInner(Real *primalVector, Gradient *adjointVector, Gradient &lhsTangent, size_t &curConstantPos, PassiveReal const *const constantValues, size_t &curRhsIdentifiersPos, Identifier const *const rhsIdentifiers)
Evaluate expression in a forward mode.
Definition primalValueBaseTape.hpp:1342
typename TapeTypes::Identifier Identifier
See PrimalValueTapeTypes.
Definition primalValueBaseTape.hpp:154
T_TapeTypes TapeTypes
See PrimalValueBaseTape.
Definition primalValueBaseTape.hpp:141
void pushStmtData(Identifier const &index, Config::ArgumentSize const &numberOfPassiveArguments, Real const &oldPrimalValue, EvalHandle evalHandle)
Add statement specific data to the data streams.
static bool constexpr AllowJacobianOptimization
See InternalStatementRecordingTapeInterface.
Definition primalValueBaseTape.hpp:172
RhsIdentifierData rhsIdentifierData
Data stream for argument identifier data.
Definition primalValueBaseTape.hpp:185
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:1225
VectorAccess< Adjoint * > * createVectorAccessCustomAdjoints(Adjoint *data)
Definition primalValueBaseTape.hpp:850
static void statementEvaluateReverse(Real *primalVector, Gradient *adjointVector, Gradient lhsAdjoint, Config::ArgumentSize numberOfPassiveArguments, size_t &curConstantPos, PassiveReal const *const constantValues, size_t &curPassivePos, Real const *const passiveValues, size_t &curRhsIdentifiersPos, Identifier const *const rhsIdentifiers)
Evaluate expression in a reverse mode.
Definition primalValueBaseTape.hpp:1499
typename TapeTypes::RhsIdentifierData RhsIdentifierData
See PrimalValueTapeTypes.
Definition primalValueBaseTape.hpp:159
Gradient const & gradient(Identifier const &identifier, AdjointsManagement adjointsManagement=AdjointsManagement::Automatic) const
Constant reference access to gradient.
Definition primalValueBaseTape.hpp:283
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:1212
TapeValues internalGetTapeValues() const
Definition primalValueBaseTape.hpp:551
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:880
void initIdentifier(Real &value, Identifier &identifier)
Definition primalValueBaseTape.hpp:301
void deleteVectorAccess(VectorAccessInterface< Real, Identifier > *access)
See Adjoint vector access.
Definition primalValueBaseTape.hpp:855
void evaluatePrimal(Position const &start, Position const &end)
Perform a full (forward) reevaluation of the primals in the tape.
Definition primalValueBaseTape.hpp:1276
void reset(bool resetAdjoints=true, AdjointsManagement adjointsManagement=AdjointsManagement::Automatic)
Reset the tape to the initial state for a fresh recording.
Definition primalValueBaseTape.hpp:536
void 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:468
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:1138
static void internalEvaluatePrimal_EvalStatements(Args &&... args)
Perform a primal evaluation of the tape. Arguments are from the recursive eval methods of the DataInt...
void setParameter(TapeParameters parameter, size_t value)
See Parameters functions.
Definition primalValueBaseTape.hpp:808
Real & primal(Identifier const &identifier)
Writable reference to primal value.
Definition primalValueBaseTape.hpp:1291
VectorAccess< AdjointVector > * createVectorAccessCustomAdjoints(AdjointVector &&data)
Definition primalValueBaseTape.hpp:843
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
See IdentifierInformationTapeInterface.
Definition primalValueBaseTape.hpp:174
void internalEvaluateForward(Position const &start, Position const &end, AdjointVector &&data)
Internal method for the forward evaluation of the whole tape.
Definition primalValueBaseTape.hpp:634
void internalResetPrimalValues(Position const &pos)
Reset the primal values to the given position.
void swap(Impl &other)
Swap all data with an other tape.
Definition primalValueBaseTape.hpp:746
void 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:449
RealTraits::PassiveReal< Real > PassiveReal
Basic computation type.
Definition primalValueBaseTape.hpp:163
typename Base::Position Position
See TapeTypesInterface.
Definition primalValueBaseTape.hpp:166
typename TapeTypes::Gradient Gradient
See TapeTypesInterface.
Definition primalValueBaseTape.hpp:151
VectorAccess< Gradient * > * createVectorAccess()
See Adjoint vector access.
Definition primalValueBaseTape.hpp:837
void 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:393
void resizeAdjointVector()
Explicitly trigger resizing of the adjoint vector. See Adjoint vector management.
Definition primalValueBaseTape.hpp:766
typename TapeTypes::StatementData StatementData
See PrimalValueTapeTypes.
Definition primalValueBaseTape.hpp:158
void endUseAdjointVector()
Declare that the adjoint vector is no longer used. See Adjoint vector management.
Definition primalValueBaseTape.hpp:776
void destroyIdentifier(Real &value, Identifier &identifier)
Has to be called for each identifier, before it is deallocated.
Definition primalValueBaseTape.hpp:309
static Real statementEvaluateForward(Real *primalVector, Gradient *adjointVector, Gradient &lhsTangent, Config::ArgumentSize numberOfPassiveArguments, size_t &curConstantPos, PassiveReal const *const constantValues, size_t &curPassivePos, Real const *const passiveValues, size_t &curRhsIdentifiersPos, Identifier const *const rhsIdentifiers)
Evaluate expression in a forward mode.
Definition primalValueBaseTape.hpp:1469
void 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:731
void clearAdjoints(AdjointsManagement adjointsManagement=AdjointsManagement::Automatic)
Clear all adjoint values, that is, set them to zero.
Definition primalValueBaseTape.hpp:524
static EvalHandle const jacobianExpressions[Config::MaxArgumentSize]
Expressions for manual statement pushes.
Definition primalValueBaseTape.hpp:181
Gradient & gradient(Identifier const &identifier, AdjointsManagement adjointsManagement=AdjointsManagement::Automatic)
Reference access to gradient.
Definition primalValueBaseTape.hpp:269
Real 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:483
void storeManual(Real const &lhsValue, Identifier &lhsIndex, Config::ArgumentSize const &size)
Definition primalValueBaseTape.hpp:1062
void pushJacobianManual(Real const &jacobian, Real const &value, Identifier const &index)
Definition primalValueBaseTape.hpp:1036
StatementData statementData
Data stream for statement specific data.
Definition primalValueBaseTape.hpp:184
static Real statementEvaluatePrimal(Real *primalVector, Config::ArgumentSize numberOfPassiveArguments, size_t &curConstantPos, PassiveReal const *const constantValues, size_t &curPassivePos, Real const *const passiveValues, size_t &curRhsIdentifiersPos, Identifier const *const rhsIdentifiers)
Evaluate primal expression.
Definition primalValueBaseTape.hpp:1485
void pushLowLevelFunction(Config::LowLevelFunctionToken token, size_t size, ByteDataView &data)
Push a low level function to the tape.
Definition primalValueBaseTape.hpp:1163
typename TapeTypes::ConstantValueData ConstantValueData
See PrimalValueTapeTypes.
Definition primalValueBaseTape.hpp:161
friend Base
Allow the base class to call protected and private methods.
Definition primalValueBaseTape.hpp:148
size_t getParameter(TapeParameters parameter) const
See Parameters functions.
Definition primalValueBaseTape.hpp:779
IndexManager & getIndexManager()
Returns a reference to the Index Manager.
Definition primalValueBaseTape.hpp:1153
static Real statementEvaluatePrimalFull(Func const &evalInner, size_t const &maxActiveArgs, size_t const &maxConstantArgs, Real *primalVector, Config::ArgumentSize numberOfPassiveArguments, size_t &curConstantPos, PassiveReal const *const constantValues, size_t &curPassivePos, Real const *const passiveValues, size_t &curRhsIdentifiersPos, Identifier const *const rhsIdentifiers)
Load the expression data and evaluate the expression in a primal setting.
Definition primalValueBaseTape.hpp:1398
typename TapeTypes::IndexManager IndexManager
See TapeTypesInterface.
Definition primalValueBaseTape.hpp:152
ConstantValueData constantValueData
Data stream for constant argument data.
Definition primalValueBaseTape.hpp:187
Type definitions for the primal value tapes.
Definition primalValueBaseTape.hpp:78
T_Gradient Gradient
See PrimalValueTapeTypes.
Definition primalValueBaseTape.hpp:82
typename IndexManager::Index Identifier
See IndexManagerInterface.
Definition primalValueBaseTape.hpp:90
Data< PassiveValueChunk, RhsIdentifierData > PassiveValueData
Passive values data vector.
Definition primalValueBaseTape.hpp:110
typename std::conditional< IsLinearIndexHandler, Chunk2< Config::ArgumentSize, EvalHandle >, Chunk4< Identifier, Config::ArgumentSize, Real, EvalHandle > >::type StatementChunk
Definition primalValueBaseTape.hpp:101
T_Data< Chunk, Nested > Data
See PrimalValueTapeTypes.
Definition primalValueBaseTape.hpp:87
static constexpr bool IsLinearIndexHandler
True if the index manager is linear.
Definition primalValueBaseTape.hpp:93
RealTraits::PassiveReal< Real > PassiveReal
Basic computation type.
Definition primalValueBaseTape.hpp:91
T_Real Real
See PrimalValueTapeTypes.
Definition primalValueBaseTape.hpp:81
Data< IdentifierChunk, StatementData > RhsIdentifierData
Rhs identifiers data vector.
Definition primalValueBaseTape.hpp:107
Data< StatementChunk, IndexManager > StatementData
Statement data vector.
Definition primalValueBaseTape.hpp:104
ConstantValueData NestedData
See TapeTypesInterface.
Definition primalValueBaseTape.hpp:115
T_IndexManager IndexManager
See PrimalValueTapeTypes.
Definition primalValueBaseTape.hpp:83
Data< ConstantValueChunk, PassiveValueData > ConstantValueData
Constant values data vector.
Definition primalValueBaseTape.hpp:113
static constexpr bool IsStaticIndexHandler
True if the index manager must be stored statically in the tape.
Definition primalValueBaseTape.hpp:94
typename StatementEvaluator::Handle EvalHandle
Handle type returned by the statement generator.
Definition primalValueBaseTape.hpp:97
T_StatementEvaluator< Real > StatementEvaluator
See PrimalValueTapeTypes.
Definition primalValueBaseTape.hpp:84
Tape side interface for StatementEvaluatorInterface.
Definition statementEvaluatorTapeInterface.hpp:95
Creation of handles for the evaluation of expressions in a context where the expression type is not a...
Definition statementEvaluatorInterface.hpp:104
Tape side interface for StatementEvaluatorInterface.
Definition statementEvaluatorTapeInterface.hpp:55
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:128
virtual void start(Tape &tape)
Destructor.
Definition tapeReaderWriterInterface.hpp:143
virtual void finish()
After all the statements have been written, the finish method finalizes the writing process.
Definition tapeReaderWriterInterface.hpp:176
void eval(NodeInterface< Node > const &node, Args &&... args)
Start the evaluation of the logic on the given expression.
Definition traversalLogic.hpp:70
void node(Node const &node, Args &&... args)
Called for each node in the expression.
Definition traversalLogic.hpp:86
Unified access to the adjoint vector and primal vector in a tape evaluation.
Definition vectorAccessInterface.hpp:91
This class is used during the writing process of a primal value tape. The WriteInfo is returned by co...
Definition tapeReaderWriterInterface.hpp:69
size_t numberOfActiveArguments
Number of active arguments.
Definition tapeReaderWriterInterface.hpp:70
std::string stmtExpression
Used to generate a .hpp file for reading back a primal value tape.
Definition tapeReaderWriterInterface.hpp:72
std::string mathRepresentation
Definition tapeReaderWriterInterface.hpp:73
size_t numberOfConstantArguments
Number of constant arguments.
Definition tapeReaderWriterInterface.hpp:71