43#include "tapeReaderWriterInterface.hpp"
60 std::string::size_type sepPosition =
fileName.rfind(
'.');
61 return fileName.substr(0, sepPosition) + suffix;
67 void openFile(FILE*& fileHandle, std::string
const& name, std::string
const& mode) {
68 fileHandle = fopen(name.c_str(), mode.c_str());
69 if (
nullptr == fileHandle) {
70 CODI_EXCEPTION(
"Could not open file %s", name.c_str());
83 template<
typename T_Type>
87 using Tape =
typename Type::Tape;
94 std::vector<Identifier>
const& out)
101 FILE* ioFileHandleTxt =
nullptr;
105 for (
size_t inputCount = 0; inputCount <
inputVariables.size(); inputCount++) {
109 for (
size_t outputCount = 0; outputCount <
outputVariables.size(); outputCount++) {
112 fprintf(ioFileHandleTxt,
"\nLargest Index = %d", tape.getIndexManager().getLargestCreatedIndex());
113 fclose(ioFileHandleTxt);
118 FILE* ioFileHandleBinary =
nullptr;
125 fwrite(&nInputs,
sizeof(nInputs), 1, ioFileHandleBinary);
127 fwrite(&nOutputs,
sizeof(nOutputs), 1, ioFileHandleBinary);
129 Identifier largestIndex = tape.getIndexManager().getLargestCreatedIndex();
130 fwrite(&largestIndex,
sizeof(
Identifier), 1, ioFileHandleBinary);
132 fclose(ioFileHandleBinary);
142 template<
typename T_Type>
145 using Tape =
typename Type::Tape;
162 FILE* ioFileHandleTxt;
165 fscanf(ioFileHandleTxt,
"%zu Inputs = ", &
nInputs);
167 for (
size_t inputCount = 0; inputCount <
nInputs; inputCount++) {
170 fscanf(ioFileHandleTxt,
"\n%zu Outputs = ", &
nOutputs);
172 for (
size_t outputCount = 0; outputCount <
nOutputs; outputCount++) {
175 fscanf(ioFileHandleTxt,
"\nLargest Index = %d", &
largestIndex);
176 fclose(ioFileHandleTxt);
181 FILE* fileIoHandleBin;
212 for (
size_t inputCount = 0; inputCount <
inputVariables.size(); inputCount++) {
216 for (
size_t outputCount = 0; outputCount <
outputVariables.size(); outputCount++) {
230 template<
typename T_Type>
235 using Tape =
typename Type::Tape;
237 using Real =
typename Type::Real;
252 std::vector<Identifier>
const& out)
253 :
Base(name, in, out),
265 identifierPrefix.resize(tape.getIndexManager().getLargestCreatedIndex() + 1, 0);
267 identifierType.resize(tape.getIndexManager().getLargestCreatedIndex() + 1, IdentifierType::Temp);
271 for (
size_t inputCount = 0; inputCount < this->
inputVariables.size(); inputCount++) {
275 for (
size_t outputCount = 0; outputCount < this->
outputVariables.size(); outputCount++) {
284 "digraph Net {\nInputs [label = \"Inputs\", color=\"blue\"];\nOutputs [label = \"Outputs\", "
285 "color=\"red\"];\nInter [label = \"Inter\"];\n");
292 fprintf(this->fileHandleGraph,
"}");
294 fclose(this->fileHandleGraph);
301 return "Aggregate" + std::to_string(aggregate);
306 std::string prefix =
"";
315 return "A" + std::to_string(identifier) +
"_" +
328 }
else if (
identifierType[identifier] == IdentifierType::Output) {
337 std::string name =
"";
338 std::string finalLabel =
"";
339 std::string shape =
"";
340 if (1 == nIdentifiers) {
343 if (0 != label.size()) {
344 finalLabel +=
" = " + label;
350 finalLabel =
"{ " + label +
" | {";
353 for (
size_t i = 0; i < nIdentifiers; i += 1) {
361 shape =
", shape=\"record\"";
364 std::string node = name +
" [label = \"" + finalLabel +
"\", color=\"" +
nodeColorProperties(identifiers[0]) +
365 "\"" + shape +
"];\n";
366 fprintf(this->fileHandleGraph,
"%s", node.c_str());
375 if (this->identifierType[identifier] == IdentifierType::Input) {
377 }
else if (this->identifierType[identifier] == IdentifierType::Output) {
383 result += std::to_string(identifier);
389 void replaceAll(std::string& str,
const std::string& search,
const std::string& replace) {
390 size_t start_pos = 0;
391 while ((start_pos = str.find(search, start_pos)) != std::string::npos) {
392 str.replace(start_pos, search.length(), replace);
393 start_pos += replace.length();
402 size_t const& nActiveValues) {
403 std::string result = mathRep;
406 for (
size_t curArg = 0; curArg < nActiveValues; curArg++) {
407 std::string searchString =
"x" + std::to_string(rhsIdentifiers[curArg]);
410 replaceAll(result, searchString, replaceString);
420 for (
size_t argCount = 0; argCount < nArguments; argCount++) {
421 if (this->identifierExtensions[rhsIdentifiers[argCount]] == 0) {
432 if (0 != label.size()) {
433 edge +=
" [label=\"" + label +
"\"];";
436 fprintf(this->fileHandleGraph,
"%s", edge.c_str());
441 for (
size_t i = 0; i < nIdentifiers; i += 1) {
442 this->identifierExtensions[identifiers[i]] += 1;
#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
CoDiPack - Code Differentiation Package.
Definition codi.hpp:94
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
typename Type::Tape Tape
See TapeReaderInterface.
Definition commonReaderWriterBase.hpp:145
T_Type Type
See TapeReaderInterface.
Definition commonReaderWriterBase.hpp:144
Tape & getTape()
Used to get a reference to the restored tape.
Definition commonReaderWriterBase.hpp:194
size_t nOutputs
Size of the output vector.
Definition commonReaderWriterBase.hpp:151
std::vector< Identifier > const & getInputs() const &
Used to get the restored inputs of the tape.
Definition commonReaderWriterBase.hpp:199
std::vector< Identifier > outputVariables
Stores the restored output variables from the tape file.
Definition commonReaderWriterBase.hpp:149
Tape tape
The newly resorted tape.
Definition commonReaderWriterBase.hpp:153
std::vector< Identifier > const & getOutputs() const &
Definition commonReaderWriterBase.hpp:204
Identifier largestIndex
The largest index on the stored tape.
Definition commonReaderWriterBase.hpp:155
typename Type::Identifier Identifier
See TapeReaderInterface.
Definition commonReaderWriterBase.hpp:146
size_t nInputs
Size of the input vector.
Definition commonReaderWriterBase.hpp:150
std::vector< Identifier > inputVariables
Stores the restored input variables from the tape file.
Definition commonReaderWriterBase.hpp:148
void restoreIoBinary()
Restore the IO for the binary readers.
Definition commonReaderWriterBase.hpp:180
void printIoText(Tape &tape)
Generate the IO file in a text format.
Definition commonReaderWriterBase.hpp:100
std::vector< Identifier > outputVariables
The identifiers which have been registered as outputs.
Definition commonReaderWriterBase.hpp:90
CommonBaseTapeWriter(std::string const &name, std::vector< Identifier > const &in, std::vector< Identifier > const &out)
Constructor.
Definition commonReaderWriterBase.hpp:93
std::vector< Identifier > inputVariables
The identifiers which have been registered as inputs.
Definition commonReaderWriterBase.hpp:89
T_Type Type
See TapeWriterInterface.
Definition commonReaderWriterBase.hpp:85
typename Type::Tape Tape
See TapeWriterInterface.
Definition commonReaderWriterBase.hpp:87
typename Type::Identifier Identifier
See TapeWriterInterface.
Definition commonReaderWriterBase.hpp:86
void printIoBinary(Tape &tape)
Generate the IO file in a binary format.
Definition commonReaderWriterBase.hpp:117
CommonReaderWriterMethods(std::string const &name)
Constructor.
Definition commonReaderWriterBase.hpp:54
CommonReaderWriterMethods()
Constructor.
Definition commonReaderWriterBase.hpp:56
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
std::vector< Identifier > identifierExtensions
Definition commonReaderWriterBase.hpp:246
void replaceAll(std::string &str, const std::string &search, const std::string &replace)
Replaces all substrings of search with replace in str.
Definition commonReaderWriterBase.hpp:389
std::string formatNodePrefix(Identifier const &identifier)
Create the prefix for a node name.
Definition commonReaderWriterBase.hpp:305
std::string formatNodeName(Identifier const &identifier, int extensionOffset=0)
Add the identifier extension of the identifier to the node name.
Definition commonReaderWriterBase.hpp:314
std::vector< IdentifierType > identifierType
Definition commonReaderWriterBase.hpp:248
FILE * fileHandleGraph
The handle for the writer.
Definition commonReaderWriterBase.hpp:241
std::vector< Identifier > identifierPrefix
Used to prefix nodes of aggregated types. Zero means no prefix.
Definition commonReaderWriterBase.hpp:245
void start(Tape &tape)
Destructor.
Definition commonReaderWriterBase.hpp:263
typename Type::Identifier Identifier
See TapeWriterInterface.
Definition commonReaderWriterBase.hpp:236
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 formatNodeNameForLink(Identifier const &identifier)
Prefixes the node name with the prefix for the identifier.
Definition commonReaderWriterBase.hpp:320
bool writeDotHeaderFooter
This flag sets toggles the heading and color index.
Definition commonReaderWriterBase.hpp:239
std::string formatNodeLabel(Identifier const &identifier)
Return a string with the current identifier type and the identifier value.
Definition commonReaderWriterBase.hpp:372
std::string nodeColorProperties(Identifier const &identifier)
Returns the color for a given identifier.
Definition commonReaderWriterBase.hpp:325
CommonTextTapeWriter(bool writeDotHeaderFooter, std::string const &name, std::vector< Identifier > const &in, std::vector< Identifier > const &out)
Constructor.
Definition commonReaderWriterBase.hpp:251
std::string formatAggregateName(Identifier const &aggregate)
Create the prefix for a node name.
Definition commonReaderWriterBase.hpp:300
std::string createNode(size_t nIdentifiers, Identifier const *identifiers, std::string const &label)
Creates a new node for a given Identifier and label.
Definition commonReaderWriterBase.hpp:336
typename Type::Tape Tape
See TapeWriterInterface.
Definition commonReaderWriterBase.hpp:235
T_Type Type
See TapeWriterInterface.
Definition commonReaderWriterBase.hpp:232
void createEdge(Identifier const &from, std::string const &to, std::string const &label="")
Return a string that creates an edge between two nodes in the .dot language.
Definition commonReaderWriterBase.hpp:429
typename Type::Real Real
See TapeWriterInterface.
Definition commonReaderWriterBase.hpp:237
Identifier aggregateCounter
Counts up or types of aggregate nodes.
Definition commonReaderWriterBase.hpp:243
CommonBaseTapeWriter< T_Type > Base
See CommonBaseTapeWriter.
Definition commonReaderWriterBase.hpp:233
bool lastNodeWasAggregate
Indicates if the lats created node was for an aggreate.
Definition commonReaderWriterBase.hpp:244
void updateLhsIdentifiers(size_t nIdentifiers, Identifier const *identifiers)
Update the identifiers extension and the prefix.
Definition commonReaderWriterBase.hpp:440
void placeUnusedRhsNodes(Identifier const *const rhsIdentifiers, Config::ArgumentSize const &nArguments)
Ensure that all the nodes on the rhs have been placed in the .dot file before creating edges to them.
Definition commonReaderWriterBase.hpp:417
void finish()
After all the statements have been written, the finish method finalizes the writing process.
Definition commonReaderWriterBase.hpp:290
The interface is used by all the tape readers. The tape readers are used to restore a tape from eithe...
Definition tapeReaderWriterInterface.hpp:252
If the tape inherits from PrimalValueBaseTape.
Definition tapeTraits.hpp:95
The interface used by all the tape writers. The tape writers are used to generate text,...
Definition tapeReaderWriterInterface.hpp:129