CoDiPack  2.3.0
A Code Differentiation Package
SciComp TU Kaiserslautern
Loading...
Searching...
No Matches
codi::TapeReaderInterface< T_Type > Struct Template Referenceabstract

The interface is used by all the tape readers. The tape readers are used to restore a tape from either a text file or a binary file that was created by the codi::TapeWriterInterface. The readers produce an identical tape in a new context, which can be evaluated and new statements can be added. More...

#include <tapeReaderWriterInterface.hpp>

Inheritance diagram for codi::TapeReaderInterface< T_Type >:

Public Types

using Identifier = typename Type::Identifier
 Identifier for the internal management, e.g. int.
 
using Real = typename Type::Real
 Primal computation type, e.g. double.
 
using Tape = typename Type::Tape
 The tape type that is to be restored.
 
using Type = T_Type
 The evaluation type.
 

Public Member Functions

virtual std::vector< Identifier > const & getInputs () const &=0
 Used to get the restored inputs of the tape.
 
virtual std::vector< Identifier > const & getOutputs () const &=0
 
virtual TapegetTape ()=0
 Used to get a reference to the restored tape.
 
virtual void readFile (std::string const &name)
 This method uses the the fileName to reproduce a valid tape.
 
virtual ~TapeReaderInterface ()
 Destructor.
 

Detailed Description

template<typename T_Type>
struct codi::TapeReaderInterface< T_Type >

The interface is used by all the tape readers. The tape readers are used to restore a tape from either a text file or a binary file that was created by the codi::TapeWriterInterface. The readers produce an identical tape in a new context, which can be evaluated and new statements can be added.

The readers are supported for both Jacobian and primal value tapes, however reading a primal value tape requires additional steps to restore the EvalHandles.

For reading and restoring a Jacobian tape file, the following steps are followed.

1) Use the codi::readTapeFile() with the name of the text or binary tape file that was previously created. Ensure that the file with the "IO.txt" or "IO.dat" suffix is also available in the same directory.

2) The restored tape can then be accessed using the codi::TapeReaderInterface::getTape() method.

3) Use the codi::TapeReaderInterface::getInputs() or codi::TapeReaderInterface::getOutputs() to set seed the gradients before evaluating the restored tape.

4) The same input and output vectors can be use to access the adjoint values after the evaluation.

// Restore the tape from the text or binary file;
std::string fileName{"Example.txt"}
auto reader = codi::readTapeFile<Real>(fileName);
// Use the reader to get the tape
Tape& tape = reader->getTape();
// Seed the desired IO using the reader.
tape.gradient(reader->getOutputs()[0]) = 1;
// Evaluate the tape
tape.evaluate();
// Get the resulting gradients
std::cout << tape.gradient(textRead->getInputs()[0]) << std::endl;

The procedure for restoring a primal value tape is similar, however there is a header file "filename".hpp that the codi::TapeWriterInterface produces, which needs to be added to reproduce the EvalHandles in the new context.

The example below shows the additional steps for a primal value tape

// Include the header file that was produced by the primal value writer
#include "Example.hpp"
// Call the createEvalHandles function that is contained in the header file
std::vector<typename Tape::EvalHandle> evalHandles = fileNameCreateEvalHandles<codi::Tape>();
// Restore the tape from the text or binary file. The evalHandles are added to the function arguments.
std::string fileName{"Example.txt"}
auto reader = codi::readTapeFile<Real>(fileName, evalHandles);
// Use the reader to get the tape
Tape& tape = reader->getTape();
// Seed the desired IO using the reader.
tape.gradient(reader->getOutputs()[0]) = 1;
// Evaluate the tape
tape.evaluate();
// Get the resulting gradients
std::cout << tape.gradient(textRead->getInputs()[0]) << std::endl;
Template Parameters
T_TypeThe CoDiPack type of the tape that is to be restored.

Member Function Documentation

◆ getInputs()

template<typename T_Type >
virtual std::vector< Identifier > const & codi::TapeReaderInterface< T_Type >::getInputs ( ) const &
pure virtual

Used to get the restored inputs of the tape.

Implemented in codi::CommonBaseTapeReader< T_Type >.

◆ getOutputs()

template<typename T_Type >
virtual std::vector< Identifier > const & codi::TapeReaderInterface< T_Type >::getOutputs ( ) const &
pure virtual

Used to get the restored outputs of the tape.

Implemented in codi::CommonBaseTapeReader< T_Type >.

◆ getTape()

template<typename T_Type >
virtual Tape & codi::TapeReaderInterface< T_Type >::getTape ( )
pure virtual

Used to get a reference to the restored tape.

Implemented in codi::CommonBaseTapeReader< T_Type >.

◆ readFile()

template<typename T_Type >
virtual void codi::TapeReaderInterface< T_Type >::readFile ( std::string const & name)
inlinevirtual

The documentation for this struct was generated from the following file: