CoDiPack  2.2.0
A Code Differentiation Package
SciComp TU Kaiserslautern
Loading...
Searching...
No Matches
codi::AggregatedTypeVectorAccessWrapper< T_Type, typename > Struct Template Reference

Generalized wrapper of the VectorAccessInterface for aggregated data types, e.g. std::complex<codi::RealReverse>. More...

#include <aggregatedTypeVectorAccessWrapper.hpp>

Inheritance diagram for codi::AggregatedTypeVectorAccessWrapper< T_Type, typename >:

Public Types

using Type = T_Type
 See AggregatedTypeVectorAccessWrapperBase.
 
- Public Types inherited from codi::VectorAccessInterface< int, int >
using Identifier
 See VectorAccessInterface.
 
using Real
 See VectorAccessInterface.
 

Additional Inherited Members

- Public Member Functions inherited from codi::VectorAccessInterface< int, int >
virtual ~VectorAccessInterface ()
 Destructor.
 
virtual size_t getVectorSize () const=0
 Vector size in the current tape evaluation.
 
virtual bool isLhsZero ()=0
 True if the adjoint set with setLhsAdjoint is zero.
 
virtual VectorAccessInterfaceclone () const=0
 
virtual void setLhsAdjoint (Identifier const &index)=0
 
virtual void updateAdjointWithLhs (Identifier const &index, Real const &jacobian)=0
 
virtual void setLhsTangent (Identifier const &index)=0
 
virtual void updateTangentWithLhs (Identifier const &index, Real const &jacobian)=0
 
virtual void resetAdjoint (Identifier const &index, size_t dim)=0
 Set the adjoint component to zero.
 
virtual void resetAdjointVec (Identifier const &index)=0
 Set the adjoint entry to zero.
 
virtual Real getAdjoint (Identifier const &index, size_t dim)=0
 Get the adjoint component.
 
virtual void getAdjointVec (Identifier const &index, Real *const vec)=0
 Get the adjoint entry.
 
virtual Real const * getAdjointVec (Identifier const &index)=0
 Get the adjoint entry.
 
virtual void updateAdjoint (Identifier const &index, size_t dim, Real const &adjoint)=0
 Update the adjoint component.
 
virtual void updateAdjointVec (Identifier const &index, Real const *const vec)=0
 Update the adjoint entry.
 
virtual void setPrimal (Identifier const &index, Real const &primal)=0
 Set the primal value.
 
virtual Real getPrimal (Identifier const &index)=0
 Get the primal value.
 
virtual bool hasPrimals ()=0
 True if the tape/vector interface has primal values.
 

Detailed Description

template<typename T_Type, typename = void>
struct codi::AggregatedTypeVectorAccessWrapper< T_Type, typename >

Generalized wrapper of the VectorAccessInterface for aggregated data types, e.g. std::complex<codi::RealReverse>.

This wrapper is instantiated by AggregatedTypeVectorAccessWrapperFactory. It can be specialized for arbitrary types that consist of CoDiPack types.

This class helps to write generalized external function, that handles aggregated data types. E.g. for std::complex<codi::RealReverse> the primal value as well as the adjoint value are defined as std::complex<double> and the identifier type is defined as std::complex<int>. Since this can be different for other types like vectors or matrices, this wrapper can be used to write generalized code, that works for arbitrary aggregated types. See RealTraits::DataExtraction for a generalized access to the primal and identifier data of aggregated types.

Here is an example for a generalized external function routine (documentation/examples/Example_20_Aggregated_active_type_handling.cpp):

template<typename Type>
void extFunc_rev(Tape* t, void* d, codi::VectorAccessInterface<RealBase, Identifier>* va) {
// Step 3: Create a wrapped vector access interface.
using VectorWrapper = typename Factory::RType;
VectorWrapper* vaType = Factory::create(va);
using TypeIdentifier = typename VectorWrapper::Identifier;
using TypeReal = typename VectorWrapper::Real;
// Step 4: Get the external function data
TypeReal x_v = data->getData<TypeReal>();
TypeIdentifier x_i = data->getData<TypeIdentifier>();
TypeIdentifier w_i = data->getData<TypeIdentifier>();
// Step 5: Use the wrapped vector access interface and perform the adjoint operation
TypeReal w_b = vaType->getAdjoint(w_i, 0);
TypeReal t_b = 2.0 * codi::ComputationTraits::transpose(x_v) * w_b;
vaType->updateAdjoint(x_i, 0, t_b);
vaType->resetAdjoint(w_i, 0);
// Step 6: Delete the created wrapper.
Factory::destroy(vaType);
}
void extFunc_del(Tape* t, void* d) {
delete data;
std::cout << " Reset: data is deleted." << std::endl;
}
template<typename Type>
Type addExternalFunc(Type const& x) {
Tape& tape = Real::getTape();
// Step 1: Perform the passive function evaluation.
tape.setPassive();
Type w = func(x);
tape.setActive();
// Step 2: Use the general access routines on the values to extract the primal and identifier data.
tape.pushExternalFunction(codi::ExternalFunction<Tape>::create(extFunc_rev<Type>, data, extFunc_del));
return w;
}

In general all implementations of the wrapper will forward all functions calls to the VectorAccessInterface of the underlying tape. For each active type in the structure the corresponding operation should be performed.

Implementations can use AggregatedTypeVectorAccessWrapperBase which implements most of the functions from VectorAccessInterface.

Template Parameters
T_TypeAn arbitrary type which consists of multiple CoDiPack types.

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