38#include "primalBaseReaderWriter.hpp"
62 template<
typename T_Type>
65 using Tape =
typename Type::Tape;
68 using Real =
typename Type::Real;
78 std::vector<Identifier>
const& out,
bool ifIO,
bool ifColumnNames)
83 if (Tape::TapeTypes::IsStaticIndexHandler) {
93 if (printColumnNames) {
95 "| LHS Index | Primal Value | # of Passive Args | # of Active Args | RHS Indices | RHS "
96 "Primal Values | # of Constants | Constants | Statement Key |");
109 Identifier const*
const rhsIdentifiers,
size_t const& curPassiveValuePos,
110 Real const*
const passiveValues,
size_t& curConstantPos,
Real const*
const constantValues,
112 fprintf(
fileHandleTxt,
"\n%d %0.12e %hhu ", curLhsIdentifier, primalValue,
123 fprintf(
fileHandleTxt,
" %d ", rhsIdentifiers[curRhsIdentifiersPos + rhsCount]);
128 for (
size_t passiveCount = 0; passiveCount < nPassiveValues; passiveCount++) {
129 fprintf(
fileHandleTxt,
" %0.12e ", passiveValues[curPassiveValuePos + passiveCount]);
137 fprintf(
fileHandleTxt,
" %0.12e ", constantValues[curConstantPos + constantCount]);
151 FILE* filePrimalTxt =
nullptr;
154 std::string fileNamePrimals = this->
modifyFileName(
"Primals.txt");
156 this->
openFile(filePrimalTxt, fileNamePrimals,
"w");
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);
168 fclose(filePrimalTxt);
200 template<
typename T_Type>
203 using Tape =
typename Type::Tape;
205 using Real =
typename Type::Real;
214 FILE* fileHandleReadTxt =
nullptr;
232 if (Tape::TapeTypes::IsStaticIndexHandler) {
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 |");
242 while (fscanf(fileHandleReadTxt,
"\n%d %lf %hhu ", &lhsIdentifier, &primalValue, &nPassiveValues) == 3) {
248 fscanf(fileHandleReadTxt,
" %hhu [", &nActiveValues);
251 for (
size_t argumentCount = 0; argumentCount < nActiveValues; argumentCount++) {
252 fscanf(fileHandleReadTxt,
" %d ", &rhsIdentifiers[argumentCount]);
254 fscanf(fileHandleReadTxt,
"] [");
256 for (
size_t argumentCount = 0; argumentCount < nPassiveValues; argumentCount++) {
257 fscanf(fileHandleReadTxt,
" %lf ", &rhsPrimalValues[argumentCount]);
259 fscanf(fileHandleReadTxt,
"] %hhu [", &nConstants);
261 for (
size_t argumentCount = 0; argumentCount < nConstants; argumentCount++) {
262 fscanf(fileHandleReadTxt,
" %lf ", &constants[argumentCount]);
264 fscanf(fileHandleReadTxt,
"]");
267 fscanf(fileHandleReadTxt,
" [ %zu ]", &evalHandleKey);
268 this->
tape.createStatementManual(lhsIdentifier, primalValue, nActiveValues, rhsIdentifiers.data(),
269 nPassiveValues, rhsPrimalValues.data(), nConstants, constants.data(),
273 fclose(fileHandleReadTxt);
278 FILE* filePrimalTxt =
nullptr;
287 fscanf(filePrimalTxt,
"%zu\n", &nPrimals);
292 while (fscanf(filePrimalTxt,
"%d ", &curIdentifier) == 1) {
293 fscanf(filePrimalTxt,
"%lf\n", &curPrimal);
294 this->
tape.setPrimal(curIdentifier, curPrimal);
#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