CoDiPack  3.1.0
A Code Differentiation Package
SciComp TU Kaiserslautern
Loading...
Searching...
No Matches
jacobianTextReaderWriter.hpp
1/*
2 * CoDiPack, a Code Differentiation Package
3 *
4 * Copyright (C) 2015-2026 Chair for Scientific Computing (SciComp), University of Kaiserslautern-Landau
5 * Homepage: http://scicomp.rptu.de
6 * Contact: Prof. Nicolas R. Gauger (codi@scicomp.uni-kl.de)
7 *
8 * Lead developers: Max Sagebaum, Johannes Blühdorn (SciComp, University of Kaiserslautern-Landau)
9 *
10 * This file is part of CoDiPack (http://scicomp.rptu.de/software/codi).
11 *
12 * CoDiPack is free software: you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation, either version 3 of the
15 * License, or (at your option) any later version.
16 *
17 * CoDiPack is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty
19 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
20 *
21 * See the GNU General Public License for more details.
22 * You should have received a copy of the GNU
23 * General Public License along with CoDiPack.
24 * If not, see <http://www.gnu.org/licenses/>.
25 *
26 * For other licensing options please contact us.
27 *
28 * Authors:
29 * - SciComp, University of Kaiserslautern-Landau:
30 * - Max Sagebaum
31 * - Johannes Blühdorn
32 * - Former members:
33 * - Tim Albring
34 */
35#pragma once
36
37#include "../../config.h"
38#include "jacobianBaseReaderWriter.hpp"
39
41namespace codi {
56 template<typename T_Type>
58 using Type = CODI_DD(T_Type, CODI_DEFAULT_LHS_EXPRESSION);
59 using Tape = typename Type::Tape;
60 using Identifier = typename Type::Identifier;
61 using Real = typename Type::Real;
62
63 FILE* fileHandleTxt = nullptr;
64 Tape* tape = nullptr;
65
66 bool printIO;
69
71 JacobianTextTapeWriter(std::string const& name, std::vector<Identifier> const& in,
72 std::vector<Identifier> const& out, bool ifIO, bool ifColumnNames)
73 : CommonBaseTapeWriter<T_Type>(name, in, out), printIO(ifIO), printColumnNames(ifColumnNames) {};
74
76 void setInputStatementOutput(bool value) {
78 }
79
81 void start(Tape& tape) {
82 this->tape = &tape;
83 if (printIO) {
84 this->printIoText(tape);
85 }
86
87 this->openFile(fileHandleTxt, this->fileName, "w");
88 if (printColumnNames) {
89 fprintf(fileHandleTxt, "| LHS Index | # of Args | RHS Indices | RHS Jacobian Values |");
90 }
91 }
92
97 void writeStatement(Identifier const& curLhsIdentifier, size_t& curJacobianPos, Real const* const rhsJacobians,
98 Identifier const* const rhsIdentifiers, Config::ArgumentSize const& nJacobians) {
99 if (nJacobians == Config::StatementInputTag) CODI_Unlikely {
101 fprintf(fileHandleTxt, "\n%d %hhu []", curLhsIdentifier, static_cast<Config::ArgumentSize>(nJacobians));
102 }
103 } else CODI_Likely {
104 fprintf(fileHandleTxt, "\n%d %hhu [", curLhsIdentifier, static_cast<Config::ArgumentSize>(nJacobians));
105
106 for (size_t argCount = 0; argCount < nJacobians; argCount++) {
107 fprintf(fileHandleTxt, " %d ", rhsIdentifiers[curJacobianPos + argCount]);
108 }
109
110 fprintf(fileHandleTxt, "] [");
111 for (size_t argCount = 0; argCount < nJacobians; argCount++) {
112 fprintf(fileHandleTxt, " %0.12e ", rhsJacobians[curJacobianPos + argCount]);
113 }
114
115 fprintf(fileHandleTxt, "]");
116 }
117 }
118
121 std::vector<Identifier> inputs;
122 std::vector<Identifier> outputs;
123
124 func->template call<LowLevelFunctionEntryCallKind::IterateInputs>(tape, data, addIdentifier, &inputs);
125 data.reset();
126 func->template call<LowLevelFunctionEntryCallKind::IterateOutputs>(tape, data, addIdentifier, &outputs);
127
128 fprintf(fileHandleTxt, "\n%d [", (int)outputs.size());
129 for (size_t i = 0; i < outputs.size(); i += 1) {
130 fprintf(fileHandleTxt, " %d ", outputs[i]);
131 }
132 fprintf(fileHandleTxt, "] %d [", (int)inputs.size());
133 for (size_t i = 0; i < inputs.size(); i += 1) {
134 fprintf(fileHandleTxt, " %d ", inputs[i]);
135 }
136 fprintf(fileHandleTxt, "]");
137 }
138
140 void finish() {
141 fclose(fileHandleTxt);
142 }
143
144 private:
146 static void addIdentifier(Identifier* id, void* userData) {
147 std::vector<Identifier>* vec = (std::vector<Identifier>*)userData;
148 vec->push_back(*id);
149 }
150 };
151
167 template<typename T_Type>
168 struct JacobianTextTapeReader : public JacobianBaseTapeReader<T_Type> {
169 using Type = CODI_DD(T_Type, CODI_DEFAULT_LHS_EXPRESSION);
170 using Tape = typename Type::Tape;
171 using Identifier = typename Type::Identifier;
172 using Real = typename Type::Real;
173
174 JacobianTextTapeReader() : JacobianBaseTapeReader<T_Type>() {}
175
177 void readFile(std::string const& name) {
178 FILE* fileHandleReadTxt = nullptr;
179
180 bool isFirstIdentifier = true;
181 Identifier lowestIndex = 0;
182
183 Identifier lhsIdentifier;
185
186 std::vector<Identifier> rhsIdentifiers(Config::MaxArgumentSize, 0);
187 std::vector<Real> rhsJacobians(Config::MaxArgumentSize, 0);
188 this->fileName = name;
189
190 this->restoreIoText();
191 this->tape.getIndexManager().updateLargestCreatedIndex(this->largestIndex);
192
193 this->openFile(fileHandleReadTxt, this->fileName, "r+");
194
195 // Read and discard column titles.
196 fscanf(fileHandleReadTxt, "| LHS Index | # of Args | RHS Indices | RHS Jacobian Values |");
197
198 // Read the statements.
199 while (fscanf(fileHandleReadTxt, "\n%d ", &lhsIdentifier) == 1) {
200 int next = getc(fileHandleReadTxt);
201 if (next == '[') {
202 CODI_EXCEPTION("Can not read tape files with low level functions.");
203 }
204 ungetc(next, fileHandleReadTxt);
205
206 fscanf(fileHandleReadTxt, "%hhu [", &nArgs);
208 // Will throw in previous if.
209 } else if (nArgs == Config::StatementInputTag) CODI_Unlikely {
210 // Do nothing.
211 } else CODI_Likely {
212 for (size_t argumentCount = 0; argumentCount < nArgs; argumentCount++) {
213 fscanf(fileHandleReadTxt, " %d ", &rhsIdentifiers[argumentCount]);
214 }
215 fscanf(fileHandleReadTxt, "] [");
216 for (size_t argumentCount = 0; argumentCount < nArgs; argumentCount++) {
217 fscanf(fileHandleReadTxt, " %lf ", &rhsJacobians[argumentCount]);
218 }
219 }
220 fscanf(fileHandleReadTxt, "]");
221
222 this->registerStatement(lhsIdentifier, nArgs, rhsIdentifiers, rhsJacobians, lowestIndex, isFirstIdentifier);
223 }
224
225 /* Update the user provided IO with a potential offset in the linear case. The registerStatement() returns
226 the offset.*/
227 this->updateUserIO(lowestIndex);
228
229 fclose(fileHandleReadTxt);
230 }
231 };
232}
#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:97
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
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:97
Definition byteDataView.hpp:51
inlinevoid reset()
Reset the data position to the start of the data.
Definition byteDataView.hpp:126
void restoreIoText()
Constructor.
Definition commonReaderWriterBase.hpp:161
void updateUserIO(Identifier const &linearOffset)
This method is used to remove any offset and to update the largest created index.
Definition commonReaderWriterBase.hpp:209
Identifier largestIndex
The largest index on the stored tape.
Definition commonReaderWriterBase.hpp:155
void printIoText(Tape &tape)
Generate the IO file in a text format.
Definition commonReaderWriterBase.hpp:100
CommonBaseTapeWriter(std::string const &name, std::vector< Identifier > const &in, std::vector< Identifier > const &out)
Constructor.
Definition commonReaderWriterBase.hpp:93
std::string fileName
The base file name provided by the user.
Definition commonReaderWriterBase.hpp:51
void openFile(FILE *&fileHandle, std::string const &name, std::string const &mode)
Definition commonReaderWriterBase.hpp:67
void registerStatement(Identifier const &lhsIdentifier, Config::ArgumentSize const &nArgs, std::vector< Identifier > const &rhsIdentifiers, std::vector< Real > const &rhsJacobians, Identifier &lowestIndex, bool &isFirstIdentifier)
Constructor.
Definition jacobianBaseReaderWriter.hpp:57
typename Type::Real Real
See TapeReaderInterface.
Definition jacobianTextReaderWriter.hpp:172
T_Type Type
See TapeReaderInterface.
Definition jacobianTextReaderWriter.hpp:169
typename Type::Identifier Identifier
See TapeReaderInterface.
Definition jacobianTextReaderWriter.hpp:171
typename Type::Tape Tape
See TapeReaderInterface.
Definition jacobianTextReaderWriter.hpp:170
JacobianTextTapeReader()
Constructor.
Definition jacobianTextReaderWriter.hpp:174
void readFile(std::string const &name)
This method uses the the fileName to reproduce a valid tape.
Definition jacobianTextReaderWriter.hpp:177
void writeLowLevelFunction(LowLevelFunctionEntry< Tape, Real, Identifier > const *func, ByteDataView &data)
Used for statements that contain a low level function.
Definition jacobianTextReaderWriter.hpp:120
typename Type::Identifier Identifier
See TapeWriterInterface.
Definition jacobianTextReaderWriter.hpp:60
typename Type::Real Real
See TapeWriterInterface.
Definition jacobianTextReaderWriter.hpp:61
void finish()
After all the statements have been written, the finish method finalizes the writing process.
Definition jacobianTextReaderWriter.hpp:140
bool printIO
Flag to enable the writing of the IO file.
Definition jacobianTextReaderWriter.hpp:66
bool printColumnNames
Flag to enable column names.
Definition jacobianTextReaderWriter.hpp:67
bool printInputStatements
Flag to enable the output of input statements.
Definition jacobianTextReaderWriter.hpp:68
T_Type Type
See TapeWriterInterface.
Definition jacobianTextReaderWriter.hpp:58
typename Type::Tape Tape
See TapeWriterInterface.
Definition jacobianTextReaderWriter.hpp:59
void start(Tape &tape)
Destructor.
Definition jacobianTextReaderWriter.hpp:81
void setInputStatementOutput(bool value)
Set if input statements should be printed.
Definition jacobianTextReaderWriter.hpp:76
JacobianTextTapeWriter(std::string const &name, std::vector< Identifier > const &in, std::vector< Identifier > const &out, bool ifIO, bool ifColumnNames)
Constructor.
Definition jacobianTextReaderWriter.hpp:71
Tape * tape
Tape handle.
Definition jacobianTextReaderWriter.hpp:64
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 jacobianTextReaderWriter.hpp:97
FILE * fileHandleTxt
File handle.
Definition jacobianTextReaderWriter.hpp:63
Low level function entry on the tape. See LowLevelFunctionTapeInterface for details.
Definition lowLevelFunctionEntry.hpp:69