CoDiPack  2.3.0
A Code Differentiation Package
SciComp TU Kaiserslautern
Loading...
Searching...
No Matches
primalTextReaderWriter.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 "primalBaseReaderWriter.hpp"
39
41namespace codi {
62 template<typename T_Type>
64 using Type = CODI_DD(T_Type, CODI_DEFAULT_LHS_EXPRESSION);
65 using Tape = typename Type::Tape;
66
67 using Identifier = typename Type::Identifier;
68 using Real = typename Type::Real;
69 using EvalHandle = typename Tape::EvalHandle;
70
71 FILE* fileHandleTxt = nullptr;
72
73 bool printIO;
75
77 PrimalTextTapeWriter(std::string const& name, std::vector<Identifier> const& in,
78 std::vector<Identifier> const& out, bool ifIO, bool ifColumnNames)
79 : PrimalBaseTapeWriter<T_Type>(name, in, out), printIO(ifIO), printColumnNames(ifColumnNames) {}
80
82 void start(Tape& tape) {
83 if (Tape::TapeTypes::IsStaticIndexHandler) {
84 printPrimals(tape);
85 }
86
87 if (printIO) {
88 this->printIoText(tape);
89 }
90
91 this->openFile(fileHandleTxt, this->fileName, "w");
92
93 if (printColumnNames) {
94 fprintf(fileHandleTxt,
95 "| LHS Index | Primal Value | # of Passive Args | # of Active Args | RHS Indices | RHS "
96 "Primal Values | # of Constants | Constants | Statement Key |");
97 }
98 }
99
107 void writeStatement(WriteInfo const& info, Identifier const& curLhsIdentifier, Real const& primalValue,
108 Config::ArgumentSize const& nPassiveValues, size_t const& curRhsIdentifiersPos,
109 Identifier const* const rhsIdentifiers, size_t const& curPassiveValuePos,
110 Real const* const passiveValues, size_t& curConstantPos, Real const* const constantValues,
111 EvalHandle stmtEvalHandle) {
112 fprintf(fileHandleTxt, "\n%d %0.12e %hhu ", curLhsIdentifier, primalValue,
113 static_cast<Config::ArgumentSize>(nPassiveValues));
114
115 if (nPassiveValues == Config::StatementInputTag) CODI_Unlikely {
116 // Do nothing.
117 } else CODI_Likely {
118 // nActiveValues.
119 fprintf(fileHandleTxt, " %hhu [", static_cast<Config::ArgumentSize>(info.numberOfActiveArguments));
120
121 // Rhs Identifiers.
122 for (size_t rhsCount = 0; rhsCount < info.numberOfActiveArguments; rhsCount++) {
123 fprintf(fileHandleTxt, " %d ", rhsIdentifiers[curRhsIdentifiersPos + rhsCount]);
124 }
125 fprintf(fileHandleTxt, "] [");
126
127 // Rhs Passive Primal Values.
128 for (size_t passiveCount = 0; passiveCount < nPassiveValues; passiveCount++) {
129 fprintf(fileHandleTxt, " %0.12e ", passiveValues[curPassiveValuePos + passiveCount]);
130 }
131
132 // nConstants.
133 fprintf(fileHandleTxt, "] %hhu [", static_cast<Config::ArgumentSize>(info.numberOfConstantArguments));
134
135 // Rhs Constants.
136 for (size_t constantCount = 0; constantCount < info.numberOfConstantArguments; constantCount++) {
137 fprintf(fileHandleTxt, " %0.12e ", constantValues[curConstantPos + constantCount]);
138 }
139
140 fprintf(fileHandleTxt, "]");
141 }
142
143 size_t evalHandleIndex = this->getEvalHandleIndex(stmtEvalHandle, info.stmtExpression);
144
145 // Stmt handle.
146 fprintf(fileHandleTxt, " [ %zu ]", evalHandleIndex);
147 }
148
150 void printPrimals(Tape& tape) {
151 FILE* filePrimalTxt = nullptr;
152 size_t nPrimals = tape.getParameter(TapeParameters::PrimalSize);
153
154 std::string fileNamePrimals = this->modifyFileName("Primals.txt");
155
156 this->openFile(filePrimalTxt, fileNamePrimals, "w");
157
158 // Print out the primals in a sparse vector representation.
159 fprintf(filePrimalTxt, "%zu\n", nPrimals);
160 for (size_t indexCounter = 0; indexCounter < nPrimals; indexCounter++) {
161 Real const curPrimal = tape.getPrimal(indexCounter);
162 if (curPrimal != 0) {
163 fprintf(filePrimalTxt, "%zu ", indexCounter);
164 fprintf(filePrimalTxt, "%0.12e\n", curPrimal);
165 }
166 }
167
168 fclose(filePrimalTxt);
169 }
170
172 void finish() {
174 fclose(fileHandleTxt);
175 }
176 };
177
200 template<typename T_Type>
202 using Type = CODI_DD(T_Type, CODI_DEFAULT_LHS_EXPRESSION);
203 using Tape = typename Type::Tape;
204 using Identifier = typename Type::Identifier;
205 using Real = typename Type::Real;
206
207 std::vector<typename Tape::EvalHandle> evalHandles;
208
209 PrimalTextTapeReader(std::vector<typename Tape::EvalHandle> const& handles)
210 : CommonBaseTapeReader<T_Type>(), evalHandles(handles) {}
211
213 void readFile(std::string const& name) {
214 FILE* fileHandleReadTxt = nullptr;
215
216 Identifier lhsIdentifier;
217 Real primalValue;
218 Config::ArgumentSize nPassiveValues;
219 Config::ArgumentSize nActiveValues;
220 std::vector<Identifier> rhsIdentifiers(Config::MaxArgumentSize, 0);
221 std::vector<Real> rhsPrimalValues(Config::MaxArgumentSize, 0);
222 Config::ArgumentSize nConstants;
223 std::vector<Real> constants(Config::MaxArgumentSize, 0);
224
225 EvalHandleKey evalHandleKey;
226 this->fileName = name;
227
228 // Restore IO and Primals
229 this->restoreIoText();
230 this->tape.getIndexManager().updateLargestCreatedIndex(this->largestIndex);
231
232 if (Tape::TapeTypes::IsStaticIndexHandler) {
234 }
235 this->openFile(fileHandleReadTxt, this->fileName, "r+");
236
237 // Read and discard column titles
238 fscanf(fileHandleReadTxt,
239 "| LHS Index | Primal Value | # of Passive Args | # of Active Args | RHS Indices | RHS "
240 "Primal Values | # of Constants | Constants | Statement Key |");
241 // Read Statements
242 while (fscanf(fileHandleReadTxt, "\n%d %lf %hhu ", &lhsIdentifier, &primalValue, &nPassiveValues) == 3) {
244 // TODO.
245 } else if (nPassiveValues == Config::StatementInputTag) CODI_Unlikely {
246 // Do nothing.
247 } else CODI_Likely {
248 fscanf(fileHandleReadTxt, " %hhu [", &nActiveValues);
249
250 // Rhs identifiers
251 for (size_t argumentCount = 0; argumentCount < nActiveValues; argumentCount++) {
252 fscanf(fileHandleReadTxt, " %d ", &rhsIdentifiers[argumentCount]);
253 }
254 fscanf(fileHandleReadTxt, "] [");
255 // Rhs primals
256 for (size_t argumentCount = 0; argumentCount < nPassiveValues; argumentCount++) {
257 fscanf(fileHandleReadTxt, " %lf ", &rhsPrimalValues[argumentCount]);
258 }
259 fscanf(fileHandleReadTxt, "] %hhu [", &nConstants);
260 // Constants
261 for (size_t argumentCount = 0; argumentCount < nConstants; argumentCount++) {
262 fscanf(fileHandleReadTxt, " %lf ", &constants[argumentCount]);
263 }
264 fscanf(fileHandleReadTxt, "]");
265 }
266 // EvalHandleKey
267 fscanf(fileHandleReadTxt, " [ %zu ]", &evalHandleKey);
268 this->tape.createStatementManual(lhsIdentifier, primalValue, nActiveValues, rhsIdentifiers.data(),
269 nPassiveValues, rhsPrimalValues.data(), nConstants, constants.data(),
270 evalHandles[evalHandleKey]);
271 }
272
273 fclose(fileHandleReadTxt);
274 }
275
278 FILE* filePrimalTxt = nullptr;
279
280 size_t nPrimals;
281 Identifier curIdentifier;
282 Real curPrimal;
283
284 this->openFile(filePrimalTxt, this->modifyFileName("Primals.txt"), "r+");
285
286 // Restore the primal vector from a sparse vector.
287 fscanf(filePrimalTxt, "%zu\n", &nPrimals);
288
289 // Resize the primal vector.
290 this->tape.setParameter(TapeParameters::PrimalSize, nPrimals);
291
292 while (fscanf(filePrimalTxt, "%d ", &curIdentifier) == 1) {
293 fscanf(filePrimalTxt, "%lf\n", &curPrimal);
294 this->tape.setPrimal(curIdentifier, curPrimal);
295 }
296 }
297 };
298}
#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
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:91
@ PrimalSize
[A: RW] Number of primal vector entries in primal value tapes.
size_t EvalHandleKey
Key for the evalHandle lookup.
Definition tapeReaderWriterInterface.hpp:44
Used to restore the IO from the "filename"IO.dat or "filename"IO.txt files. Also provides the get met...
Definition commonReaderWriterBase.hpp:143
void restoreIoText()
Constructor.
Definition commonReaderWriterBase.hpp:161
Tape tape
The newly resorted tape.
Definition commonReaderWriterBase.hpp:153
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
std::string fileName
The base file name provided by the user.
Definition commonReaderWriterBase.hpp:51
std::string modifyFileName(std::string const &suffix)
Remove the file extension and replace it with a new suffix.
Definition commonReaderWriterBase.hpp:59
void openFile(FILE *&fileHandle, std::string const &name, std::string const &mode)
Definition commonReaderWriterBase.hpp:67
This base class is used to implement the automatic generation of the .hpp file that restores the eval...
Definition primalBaseReaderWriter.hpp:55
void generateHandleCreatorFile()
This method is used to generate an .hpp file which creates the necessary EvalHandles in the reading p...
Definition primalBaseReaderWriter.hpp:78
size_t getEvalHandleIndex(EvalHandle const evalHandle, std::string const &evalStatement)
Get the index for an evalHandle.
Definition primalBaseReaderWriter.hpp:118
Reads and restores a text file for a primal value tape.
Definition primalTextReaderWriter.hpp:201
typename Type::Identifier Identifier
See TapeReaderInterface.
Definition primalTextReaderWriter.hpp:204
PrimalTextTapeReader(std::vector< typename Tape::EvalHandle > const &handles)
Constructor.
Definition primalTextReaderWriter.hpp:209
typename Type::Real Real
See TapeReaderInterface.
Definition primalTextReaderWriter.hpp:205
void restorePrimals()
Read and record the primal values from the sparse primal vector stored in "filename"Primals....
Definition primalTextReaderWriter.hpp:277
void readFile(std::string const &name)
This method uses the the fileName to reproduce a valid tape.
Definition primalTextReaderWriter.hpp:213
std::vector< typename Tape::EvalHandle > evalHandles
Contains the unique evalHandles.
Definition primalTextReaderWriter.hpp:207
typename Type::Tape Tape
See TapeReaderInterface.
Definition primalTextReaderWriter.hpp:203
T_Type Type
See TapeWriterInterface.
Definition primalTextReaderWriter.hpp:202
Writes a primal value tape in a text format.
Definition primalTextReaderWriter.hpp:63
bool printColumnNames
Flag to enable column names.
Definition primalTextReaderWriter.hpp:74
PrimalTextTapeWriter(std::string const &name, std::vector< Identifier > const &in, std::vector< Identifier > const &out, bool ifIO, bool ifColumnNames)
Constructor.
Definition primalTextReaderWriter.hpp:77
typename Type::Tape Tape
See TapeWriterInterface.
Definition primalTextReaderWriter.hpp:65
bool printIO
Flag to enable the writing of the IO file.
Definition primalTextReaderWriter.hpp:73
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 primalTextReaderWriter.hpp:107
FILE * fileHandleTxt
File handle.
Definition primalTextReaderWriter.hpp:71
T_Type Type
See TapeWriterInterface.
Definition primalTextReaderWriter.hpp:64
void finish()
After all the statements have been written, the finish method finalizes the writing process.
Definition primalTextReaderWriter.hpp:172
typename Tape::EvalHandle EvalHandle
See TapeWriterInterface.
Definition primalTextReaderWriter.hpp:69
typename Type::Identifier Identifier
See TapeWriterInterface.
Definition primalTextReaderWriter.hpp:67
void printPrimals(Tape &tape)
Print the primal vector in sparse vector representation.
Definition primalTextReaderWriter.hpp:150
void start(Tape &tape)
Destructor.
Definition primalTextReaderWriter.hpp:82
typename Type::Real Real
See TapeWriterInterface.
Definition primalTextReaderWriter.hpp:68
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
size_t numberOfConstantArguments
Number of constant arguments.
Definition tapeReaderWriterInterface.hpp:71