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;
249 std::vector<Identifier>
const& out)
250 :
Base(name, in, out),
260 identifierType.resize(tape.getIndexManager().getLargestCreatedIndex() + 1, IdentifierType::Temp);
264 for (
size_t inputCount = 0; inputCount < this->
inputVariables.size(); inputCount++) {
268 for (
size_t outputCount = 0; outputCount < this->
outputVariables.size(); outputCount++) {
274 if (writeDotHeaderFooter) {
277 "digraph Net {\nInputs [label = \"Inputs\", color=\"blue\"];\nOutputs [label = \"Outputs\", "
278 "color=\"red\"];\nInter [label = \"Inter\"];\n");
285 fprintf(this->fileHandleGraph,
"}");
287 fclose(this->fileHandleGraph);
293 return "A" + std::to_string(identifier) +
"_" +
301 }
else if (
identifierType[identifier] == IdentifierType::Output) {
310 std::string node =
formatNodeName(identifier, 1) +
" [label = \"" + label +
"\", color=\"" +
312 fprintf(this->fileHandleGraph,
"%s", node.c_str());
318 if (this->identifierType[identifier] == IdentifierType::Input) {
320 }
else if (this->identifierType[identifier] == IdentifierType::Output) {
326 result += std::to_string(identifier);
332 void replaceAll(std::string& str,
const std::string& search,
const std::string& replace) {
333 size_t start_pos = 0;
334 while ((start_pos = str.find(search, start_pos)) != std::string::npos) {
335 str.replace(start_pos, search.length(), replace);
336 start_pos += replace.length();
345 Identifier const*
const rhsIdentifiers,
size_t const& nActiveValues) {
348 for (
size_t curArg = 0; curArg < nActiveValues; curArg++) {
349 std::string searchString =
"x" + std::to_string(rhsIdentifiers[curArg]);
352 replaceAll(result, searchString, replaceString);
362 for (
size_t argCount = 0; argCount < nArguments; argCount++) {
363 if (this->identifierExtensions[rhsIdentifiers[argCount]] == 0) {
366 this->identifierExtensions[rhsIdentifiers[argCount]] += 1;
375 if (0 != label.size()) {
376 edge +=
" [label=\"" + label +
"\"];";
379 fprintf(this->fileHandleGraph,
"%s", edge.c_str());
#define CODI_DD(Type, Default)
Abbreviation for CODI_DECLARE_DEFAULT.
Definition macros.hpp:94
uint8_t ArgumentSize
Type for the number of arguments in statements.
Definition config.h:117
CoDiPack - Code Differentiation Package.
Definition codi.hpp:91
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
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
This class is a common base for all the writers and produces a IO file that contains the input and ou...
Definition commonReaderWriterBase.hpp:84
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
Used to implement methods common to both the tape readers and the tape writers.
Definition commonReaderWriterBase.hpp:50
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
This base class is used to modify the math representation of a statement.
Definition commonReaderWriterBase.hpp:231
std::vector< Identifier > identifierExtensions
Definition commonReaderWriterBase.hpp:243
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:332
std::string formatNodeName(Identifier const &identifier, int extensionOffset=0)
Add the identifier extension of the identifier to the node name.
Definition commonReaderWriterBase.hpp:292
std::vector< IdentifierType > identifierType
Definition commonReaderWriterBase.hpp:245
FILE * fileHandleGraph
The handle for the writer.
Definition commonReaderWriterBase.hpp:241
void start(Tape &tape)
Destructor.
Definition commonReaderWriterBase.hpp:257
typename Type::Identifier Identifier
See TapeWriterInterface.
Definition commonReaderWriterBase.hpp:236
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:316
std::string nodeColorProperties(Identifier const &identifier)
Returns the color for a given identifier.
Definition commonReaderWriterBase.hpp:298
CommonTextTapeWriter(bool writeDotHeaderFooter, std::string const &name, std::vector< Identifier > const &in, std::vector< Identifier > const &out)
Constructor.
Definition commonReaderWriterBase.hpp:248
typename Type::Tape Tape
See TapeWriterInterface.
Definition commonReaderWriterBase.hpp:235
T_Type Type
See TapeWriterInterface.
Definition commonReaderWriterBase.hpp:232
typename Type::Real Real
See TapeWriterInterface.
Definition commonReaderWriterBase.hpp:237
void createNode(Identifier const &identifier, std::string const &label)
Creates a new node for a given Identifier and label.
Definition commonReaderWriterBase.hpp:309
void createEdge(Identifier const &from, Identifier const &to, std::string const &label="")
Return a string that creates an edge between two nodes in the .dot language.
Definition commonReaderWriterBase.hpp:372
std::string modifyMathRep(std::string const &mathRep, Identifier const &lhsIdentifier, 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:344
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:359
void finish()
After all the statements have been written, the finish method finalizes the writing process.
Definition commonReaderWriterBase.hpp:283
The interface is used by all the tape readers. The tape readers are used to restore a tape from eithe...
Definition tapeReaderWriterInterface.hpp:250
If the tape inherits from PrimalValueBaseTape.
Definition tapeTraits.hpp:97
The interface used by all the tape writers. The tape writers are used to generate text,...
Definition tapeReaderWriterInterface.hpp:128