CoDiPack  2.3.0
A Code Differentiation Package
SciComp TU Kaiserslautern
Loading...
Searching...
No Matches
primalBinaryReaderWriter.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* fileHandleBin = nullptr;
72
74 PrimalBinaryTapeWriter(std::string const& name, std::vector<Identifier> const& in,
75 std::vector<Identifier> const& out)
76 : PrimalBaseTapeWriter<T_Type>(name, in, out) {};
77
79 void start(Tape& tape) {
80 if (Tape::TapeTypes::IsStaticIndexHandler) {
81 printPrimals(tape);
82 }
83
84 this->printIoBinary(tape);
85 this->openFile(fileHandleBin, this->fileName, "wb");
86 }
87
95 void writeStatement(WriteInfo const& info, Identifier const& curLhsIdentifier, Real const& primalValue,
96 Config::ArgumentSize const& nPassiveValues, size_t const& curRhsIdentifiersPos,
97 Identifier const* const rhsIdentifiers, size_t const& curPassiveValuePos,
98 Real const* const passiveValues, size_t& curConstantPos, Real const* const constantValues,
99 EvalHandle stmtEvalHandle) {
100 fwrite(&curLhsIdentifier, sizeof(Identifier), 1, fileHandleBin);
101 fwrite(&primalValue, sizeof(Real), 1, fileHandleBin);
102 fwrite(&nPassiveValues, sizeof(Config::ArgumentSize), 1, fileHandleBin);
103
104 if (nPassiveValues == Config::StatementInputTag) CODI_Unlikely {
105 // Do nothing.
106 } else CODI_Likely {
108 fwrite(&rhsIdentifiers[curRhsIdentifiersPos], sizeof(Identifier), info.numberOfActiveArguments,
110 fwrite(&passiveValues[curPassiveValuePos], sizeof(Real), nPassiveValues, fileHandleBin);
112 fwrite(&constantValues[curConstantPos], sizeof(Real), info.numberOfConstantArguments, fileHandleBin);
113 }
114 // Register the statement expression (in the form of a string) for the evalHandleKey.
115
116 size_t evalHandleIndex = this->getEvalHandleIndex(stmtEvalHandle, info.stmtExpression);
117 fwrite(&evalHandleIndex, sizeof(size_t), 1, fileHandleBin);
118 }
119
121 void printPrimals(Tape& tape) {
122 FILE* filePrimalBin = nullptr;
123 size_t nPrimals = tape.getParameter(TapeParameters::PrimalSize);
124
125 std::string fileNamePrimals = this->modifyFileName("Primals.dat");
126
127 this->openFile(filePrimalBin, fileNamePrimals, "wb");
128
129 // Print out the primals in a sparse vector representation.
130 fwrite(&nPrimals, sizeof(size_t), 1, filePrimalBin);
131 for (size_t indexCounter = 0; indexCounter < nPrimals; indexCounter++) {
132 Real const curPrimal = tape.getPrimal(indexCounter);
133 if (curPrimal != 0) {
134 fwrite(&indexCounter, sizeof(Identifier), 1, filePrimalBin);
135 fwrite(&curPrimal, sizeof(Real), 1, filePrimalBin);
136 }
137 }
138
139 fclose(filePrimalBin);
140 }
141
143 void finish() {
144 // Generate the handleCreator .hpp file.
146
147 fclose(fileHandleBin);
148 }
149 };
150
173 template<typename T_Type>
175 using Type = CODI_DD(T_Type, CODI_DEFAULT_LHS_EXPRESSION);
176 using Tape = typename Type::Tape;
177 using Identifier = typename Type::Identifier;
178 using Real = typename Type::Real;
179
180 std::vector<typename Tape::EvalHandle> evalHandles;
182 PrimalBinaryTapeReader(std::vector<typename Tape::EvalHandle> const& handles)
183 : CommonBaseTapeReader<T_Type>(), evalHandles(handles) {}
184
186 void readFile(std::string const& name) {
187 FILE* fileHandleReadBin = nullptr;
188
189 Identifier lhsIdentifier;
190 Real primalValue;
191 Config::ArgumentSize nPassiveValues;
192 Config::ArgumentSize nActiveValues;
193 std::vector<Identifier> rhsIdentifiers(Config::MaxArgumentSize, 0);
194 std::vector<Real> rhsPrimalValues(Config::MaxArgumentSize, 0);
195 Config::ArgumentSize nConstants;
196 std::vector<Real> constants(Config::MaxArgumentSize, 0);
197
198 EvalHandleKey evalHandleKey;
199 this->fileName = name;
200
201 // Restore IO and the primal vector, if the tape is a reuseType.
202 this->restoreIoBinary();
203 this->tape.getIndexManager().updateLargestCreatedIndex(this->largestIndex);
204
205 if (Tape::TapeTypes::IsStaticIndexHandler) {
207 }
208
209 this->openFile(fileHandleReadBin, this->fileName, "rb");
210
211 while (fread(&lhsIdentifier, sizeof(Identifier), 1, fileHandleReadBin) == 1) {
212 fread(&primalValue, sizeof(Real), 1, fileHandleReadBin);
213 fread(&nPassiveValues, sizeof(Config::ArgumentSize), 1, fileHandleReadBin);
214
216 // TODO.
217 } else if (nPassiveValues == Config::StatementInputTag) CODI_Unlikely {
218 // Do nothing.
219 } else CODI_Likely {
220 fread(&nActiveValues, sizeof(Config::ArgumentSize), 1, fileHandleReadBin);
221 fread(rhsIdentifiers.data(), sizeof(Identifier), nActiveValues, fileHandleReadBin);
222 fread(rhsPrimalValues.data(), sizeof(Real), nPassiveValues, fileHandleReadBin);
223 fread(&nConstants, sizeof(Config::ArgumentSize), 1, fileHandleReadBin);
224 fread(constants.data(), sizeof(Real), nConstants, fileHandleReadBin);
225 }
226
227 fread(&evalHandleKey, sizeof(EvalHandleKey), 1, fileHandleReadBin);
228 this->tape.createStatementManual(lhsIdentifier, primalValue, nActiveValues, rhsIdentifiers.data(),
229 nPassiveValues, rhsPrimalValues.data(), nConstants, constants.data(),
230 evalHandles[evalHandleKey]);
231 }
232
233 fclose(fileHandleReadBin);
234 }
235
238 FILE* filePrimalBin = nullptr;
239
240 size_t nPrimals;
241 Identifier curIdentifier;
242 Real curPrimal;
243
244 this->openFile(filePrimalBin, this->modifyFileName("Primals.dat"), "rb");
245
246 // Restore the primal vector from a sparse vector.
247 fread(&nPrimals, sizeof(size_t), 1, filePrimalBin);
248 // Resize the primal vector.
249 this->tape.setParameter(TapeParameters::PrimalSize, nPrimals);
250
251 while (fread(&curIdentifier, sizeof(Identifier), 1, filePrimalBin) == 1) {
252 fread(&curPrimal, sizeof(Real), 1, filePrimalBin);
253 this->tape.setPrimal(curIdentifier, curPrimal);
254 }
255 }
256 };
257}
#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
Tape tape
The newly resorted tape.
Definition commonReaderWriterBase.hpp:153
Identifier largestIndex
The largest index on the stored tape.
Definition commonReaderWriterBase.hpp:155
void restoreIoBinary()
Restore the IO for the binary readers.
Definition commonReaderWriterBase.hpp:180
void printIoBinary(Tape &tape)
Generate the IO file in a binary format.
Definition commonReaderWriterBase.hpp:117
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 binary file for a Primal tape.
Definition primalBinaryReaderWriter.hpp:174
typename Type::Real Real
See TapeReaderInterface.
Definition primalBinaryReaderWriter.hpp:178
typename Type::Identifier Identifier
See TapeReaderInterface.
Definition primalBinaryReaderWriter.hpp:177
T_Type Type
See TapeReaderInterface.
Definition primalBinaryReaderWriter.hpp:175
typename Type::Tape Tape
See TapeReaderInterface.
Definition primalBinaryReaderWriter.hpp:176
void restorePrimals()
Read and record the primal values from the sparse primal vector stored in "filename"Primals....
Definition primalBinaryReaderWriter.hpp:237
std::vector< typename Tape::EvalHandle > evalHandles
Definition primalBinaryReaderWriter.hpp:180
void readFile(std::string const &name)
This method uses the the fileName to reproduce a valid tape.
Definition primalBinaryReaderWriter.hpp:186
PrimalBinaryTapeReader(std::vector< typename Tape::EvalHandle > const &handles)
Constructor.
Definition primalBinaryReaderWriter.hpp:182
Writes a primal value tape in a binary format.
Definition primalBinaryReaderWriter.hpp:63
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 primalBinaryReaderWriter.hpp:95
PrimalBinaryTapeWriter(std::string const &name, std::vector< Identifier > const &in, std::vector< Identifier > const &out)
Constructor.
Definition primalBinaryReaderWriter.hpp:74
typename Type::Tape Tape
See TapeWriterInterface.
Definition primalBinaryReaderWriter.hpp:65
void printPrimals(Tape &tape)
Print the primal vector in sparse vector representation.
Definition primalBinaryReaderWriter.hpp:121
typename Type::Identifier Identifier
See TapeWriterInterface.
Definition primalBinaryReaderWriter.hpp:67
FILE * fileHandleBin
File handle.
Definition primalBinaryReaderWriter.hpp:71
T_Type Type
See TapeWriterInterface.
Definition primalBinaryReaderWriter.hpp:64
typename Type::Real Real
See TapeWriterInterface.
Definition primalBinaryReaderWriter.hpp:68
void start(Tape &tape)
Destructor.
Definition primalBinaryReaderWriter.hpp:79
void finish()
After all the statements have been written, the finish method finalizes the writing process.
Definition primalBinaryReaderWriter.hpp:143
typename Tape::EvalHandle EvalHandle
See TapeWriterInterface.
Definition primalBinaryReaderWriter.hpp:69
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