CoDiPack  2.3.0
A Code Differentiation Package
SciComp TU Kaiserslautern
Loading...
Searching...
No Matches
graphWriters.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 "../../config.h"
38#include "../../traits/tapeTraits.hpp"
39#include "commonReaderWriterBase.hpp"
40
42namespace codi {
59 template<typename T_Type>
61 using Type = CODI_DD(T_Type, CODI_DEFAULT_LHS_EXPRESSION);
63
64 using Tape = typename Type::Tape;
65 using Identifier = typename Type::Identifier;
66 using Real = typename Type::Real;
67
69
71 JacobianGraphTapeWriter(std::string const& name, std::vector<Identifier> const& in,
72 std::vector<Identifier> const& out, bool ifJacobians)
73 : Base(true, name, in, out), printJacobians(ifJacobians) {};
74
79 void writeStatement(Identifier const& curLhsIdentifier, size_t& curJacobianPos, Real const* const rhsJacobians,
80 Identifier const* const rhsIdentifiers, Config::ArgumentSize const& nJacobians) {
81 std::string node;
82 if (nJacobians == Config::StatementInputTag) CODI_Unlikely {
83 // Do nothing.
84 } else CODI_Likely {
85 // Ensure that the all the rhsIdentifiers have been added before connecting edges to them.
86 Base::placeUnusedRhsNodes(&rhsIdentifiers[curJacobianPos], nJacobians);
87
88 // Add the curLhsIdentifier node.
89 Base::createNode(curLhsIdentifier, this->formatNodeLabel(curLhsIdentifier));
90
91 // Loop through rhsIdentifiers and create the edges. The this->identifierExtensions is used to find the
92 // current extension for each of the rhsIdentifiers.
93 for (size_t argCount = 0; argCount < nJacobians; argCount++) {
94 std::string labelText = "";
95 if (printJacobians) {
96 labelText = std::to_string(rhsJacobians[curJacobianPos + argCount]);
97 }
98 Base::createEdge(rhsIdentifiers[curJacobianPos + argCount], curLhsIdentifier, labelText);
99 }
100
101 this->identifierExtensions[curLhsIdentifier] += 1;
102 }
103 }
104 };
105
123 template<typename T_Type>
125 using Type = CODI_DD(T_Type, CODI_DEFAULT_LHS_EXPRESSION);
127
128 using Tape = typename Type::Tape;
129 using Identifier = typename Type::Identifier;
130 using Real = typename Type::Real;
131 using EvalHandle = typename Tape::EvalHandle;
132
134 PrimalGraphTapeWriter(std::string const& name, std::vector<Identifier> const& in,
135 std::vector<Identifier> const& out)
136 : Base(true, name, in, out) {};
137
145 void writeStatement(WriteInfo const& info, Identifier const& curLhsIdentifier, Real const& primalValue,
146 Config::ArgumentSize const& nPassiveValues, size_t const& curRhsIdentifiersPos,
147 Identifier const* const rhsIdentifiers, size_t const& curPassiveValuePos,
148 Real const* const passiveValues, size_t& curConstantPos, Real const* const constantValues,
149 EvalHandle stmtEvalHandle) {
150 CODI_UNUSED(primalValue, curPassiveValuePos, passiveValues, curConstantPos, constantValues, stmtEvalHandle);
151
152 std::string node;
153 if (nPassiveValues == Config::StatementInputTag) CODI_Unlikely {
154 // Do nothing.
155 } else CODI_Likely {
156 // Ensure that the all the rhsIdentifiers have been added before connecting edges to them. The mathRep string
157 // is also modified to include the identifier and value.
158 Base::placeUnusedRhsNodes(&rhsIdentifiers[curRhsIdentifiersPos], info.numberOfActiveArguments);
159
160 // The mathRep string is modified to include the identifier and value.
161 std::string mathRep =
162 Base::modifyMathRep(info.mathRepresentation, curLhsIdentifier, &rhsIdentifiers[curRhsIdentifiersPos],
164 // Add the curLhsIdentifier node.
165 Base::createNode(curLhsIdentifier, mathRep);
166
167 // Loop through rhsIdentifiers and create the edges. The identifierExtensions is used to find the current
168 // extension for each of the rhsIdentifiers.
169 for (size_t argCount = 0; argCount < info.numberOfActiveArguments; argCount++) {
170 Base::createEdge(rhsIdentifiers[curRhsIdentifiersPos + argCount], curLhsIdentifier);
171 }
172
173 // Only record the increased extension after recording the edges. This ensure that if the lhs equals the rhs,
174 // that it results in tow unique nodes.
175 this->identifierExtensions[curLhsIdentifier] += 1;
176 }
177 }
178 };
179}
#define CODI_Unlikely
Declare unlikely evaluation of an execution path.
Definition config.h:399
#define CODI_Likely
Declare likely evaluation of an execution path.
Definition config.h:397
#define CODI_DD(Type, Default)
Abbreviation for CODI_DECLARE_DEFAULT.
Definition macros.hpp:94
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
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
This base class is used to modify the math representation of a statement.
Definition commonReaderWriterBase.hpp:231
std::vector< Identifier > identifierExtensions
Definition commonReaderWriterBase.hpp:243
std::string formatNodeLabel(Identifier const &identifier)
Return a string with the current identifier type and the identifier value.
Definition commonReaderWriterBase.hpp:316
void createNode(Identifier const &identifier, std::string const &label)
Creates a new node for a given Identifier and label.
Definition commonReaderWriterBase.hpp:309
void createEdge(Identifier const &from, Identifier const &to, std::string const &label="")
Return a string that creates an edge between two nodes in the .dot language.
Definition commonReaderWriterBase.hpp:372
std::string modifyMathRep(std::string const &mathRep, Identifier const &lhsIdentifier, Identifier const *const rhsIdentifiers, size_t const &nActiveValues)
Replaces all general identifiers in the math representation with the input, output or temporary annot...
Definition commonReaderWriterBase.hpp:344
void placeUnusedRhsNodes(Identifier const *const rhsIdentifiers, Config::ArgumentSize const &nArguments)
Ensure that all the nodes on the rhs have been placed in the .dot file before creating edges to them.
Definition commonReaderWriterBase.hpp:359
Generates a graphical .dot file for a Jacobian tape.
Definition graphWriters.hpp:60
void writeStatement(Identifier const &curLhsIdentifier, size_t &curJacobianPos, Real const *const rhsJacobians, Identifier const *const rhsIdentifiers, Config::ArgumentSize const &nJacobians)
Called for each statement. The method writes the current statement to the file. This overload is used...
Definition graphWriters.hpp:79
typename Type::Real Real
See TapeWriterInterface.
Definition graphWriters.hpp:66
typename Type::Tape Tape
See TapeWriterInterface.
Definition graphWriters.hpp:64
JacobianGraphTapeWriter(std::string const &name, std::vector< Identifier > const &in, std::vector< Identifier > const &out, bool ifJacobians)
Constructor.
Definition graphWriters.hpp:71
T_Type Type
See TapeWriterInterface.
Definition graphWriters.hpp:61
bool printJacobians
Flag that enables the jacobians on the edges in the graph.
Definition graphWriters.hpp:68
typename Type::Identifier Identifier
See TapeWriterInterface.
Definition graphWriters.hpp:65
Generates a graphical .dot file for a primal value tape. The writer adds the math representation of s...
Definition graphWriters.hpp:124
typename Type::Tape Tape
See TapeWriterInterface.
Definition graphWriters.hpp:128
T_Type Type
See TapeWriterInterface.
Definition graphWriters.hpp:125
typename Type::Identifier Identifier
See TapeWriterInterface.
Definition graphWriters.hpp:129
void writeStatement(WriteInfo const &info, Identifier const &curLhsIdentifier, Real const &primalValue, Config::ArgumentSize const &nPassiveValues, size_t const &curRhsIdentifiersPos, Identifier const *const rhsIdentifiers, size_t const &curPassiveValuePos, Real const *const passiveValues, size_t &curConstantPos, Real const *const constantValues, EvalHandle stmtEvalHandle)
Called for each statement. The method writes the current statement to the file. This overload is used...
Definition graphWriters.hpp:145
typename Tape::EvalHandle EvalHandle
See TapeWriterInterface.
Definition graphWriters.hpp:131
PrimalGraphTapeWriter(std::string const &name, std::vector< Identifier > const &in, std::vector< Identifier > const &out)
Constructor.
Definition graphWriters.hpp:134
typename Type::Real Real
See TapeWriterInterface.
Definition graphWriters.hpp:130
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 mathRepresentation
Definition tapeReaderWriterInterface.hpp:73