CoDiPack  3.0.0
A Code Differentiation Package
SciComp TU Kaiserslautern
Loading...
Searching...
No Matches
mathRepWriter.hpp
1/*
2 * CoDiPack, a Code Differentiation Package
3 *
4 * Copyright (C) 2015-2025 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 "commonReaderWriterBase.hpp"
39
41namespace codi {
55 template<typename T_Type>
56 struct MathRepWriter : public CommonTextTapeWriter<T_Type> {
57 using Type = CODI_DD(T_Type, CODI_DEFAULT_LHS_EXPRESSION);
59
60 using Tape = typename Type::Tape;
61 using Identifier = typename Type::Identifier;
62 using Real = typename Type::Real;
63 using EvalHandle = typename Tape::EvalHandle;
64
66 MathRepWriter(std::string const& name, std::vector<Identifier> const& in, std::vector<Identifier> const& out)
67 : Base(false, name, in, out) {};
68
74 void writeStatement(WriteInfo const& info, Identifier const* lhsIdentifiers, Real const* primalValues,
75 Config::ArgumentSize const& nPassiveValues, Identifier const* const rhsIdentifiers,
76 Real const* const passiveValues, Real const* const constantValues,
77 EvalHandle stmtEvalHandle) {
78 CODI_UNUSED(primalValues, passiveValues, constantValues, stmtEvalHandle);
79
80 if (nPassiveValues == Config::StatementInputTag) CODI_Unlikely {
81 // Do nothing.
82 } else CODI_Likely {
83 // The mathRep string is modified to include the identifier and value.
84 std::string mathRep =
85 formatLhs(lhsIdentifiers, info.numberOfOutputArguments) + " = " +
87 // Print the statement string.
88 fprintf(this->fileHandleGraph, "%s\n", mathRep.c_str());
89 }
90 }
91
92 private:
93 std::string formatLhs(Identifier const* lhsIdentifiers, size_t const& nOutputValues) {
94 std::string result = "";
95 if (1 != nOutputValues) {
96 result += "[";
97 }
98 for (size_t curArg = 0; curArg < nOutputValues; curArg++) {
99 if (0 != curArg) {
100 result += ", ";
101 }
102 result += Base::formatNodeLabel(lhsIdentifiers[curArg]);
103 }
104 if (1 != nOutputValues) {
105 result += "]";
106 }
107
108 return result;
109 }
110 };
111}
#define CODI_Unlikely
Declare unlikely evaluation of an execution path.
Definition config.h:408
#define CODI_Likely
Declare likely evaluation of an execution path.
Definition config.h:406
#define CODI_DD(Type, Default)
Abbreviation for CODI_DECLARE_DEFAULT.
Definition macros.hpp:96
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:94
inlinevoid CODI_UNUSED(Args const &...)
Disable unused warnings for an arbitrary number of arguments.
Definition macros.hpp:54
FILE * fileHandleGraph
The handle for the writer.
Definition commonReaderWriterBase.hpp:241
std::string modifyMathRep(std::string const &mathRep, 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:401
std::string formatNodeLabel(Identifier const &identifier)
Return a string with the current identifier type and the identifier value.
Definition commonReaderWriterBase.hpp:372
CommonTextTapeWriter(bool writeDotHeaderFooter, std::string const &name, std::vector< Identifier > const &in, std::vector< Identifier > const &out)
Constructor.
Definition commonReaderWriterBase.hpp:251
typename Type::Real Real
See TapeWriterInterface.
Definition mathRepWriter.hpp:62
MathRepWriter(std::string const &name, std::vector< Identifier > const &in, std::vector< Identifier > const &out)
Constructor.
Definition mathRepWriter.hpp:66
T_Type Type
See TapeWriterInterface.
Definition mathRepWriter.hpp:57
typename Type::Tape Tape
See TapeWriterInterface.
Definition mathRepWriter.hpp:60
typename Tape::EvalHandle EvalHandle
See TapeWriterInterface.
Definition mathRepWriter.hpp:63
void writeStatement(WriteInfo const &info, Identifier const *lhsIdentifiers, Real const *primalValues, Config::ArgumentSize const &nPassiveValues, Identifier const *const rhsIdentifiers, Real const *const passiveValues, Real const *const constantValues, EvalHandle stmtEvalHandle)
Called for each statement. The method writes the current statement to the file. This overload is used...
Definition mathRepWriter.hpp:74
CommonTextTapeWriter< T_Type > Base
See CommonTextTapeWriter.
Definition mathRepWriter.hpp:58
typename Type::Identifier Identifier
See TapeWriterInterface.
Definition mathRepWriter.hpp:61
This class is used during the writing process of a primal value tape. The WriteInfo is returned by St...
Definition tapeReaderWriterInterface.hpp:69
size_t numberOfActiveArguments
Number of active arguments.
Definition tapeReaderWriterInterface.hpp:71
size_t numberOfOutputArguments
Number of output arguments.
Definition tapeReaderWriterInterface.hpp:70
std::string mathRepresentation
Definition tapeReaderWriterInterface.hpp:74