CoDiPack  2.2.0
A Code Differentiation Package
SciComp TU Kaiserslautern
Loading...
Searching...
No Matches
codi::LinearIndexManager< T_Index > Struct Template Reference

Identifiers are created in a linear fashion. Each assign creates a new index which is counted up. More...

#include <linearIndexManager.hpp>

Inheritance diagram for codi::LinearIndexManager< T_Index >:

Public Types

using Base = IndexManagerInterface<Index>
 Base class abbreviation.
 
using Index = T_Index
 See LinearIndexManager.
 
DataInterface: Type declaration
using Position = Index
 Positions coincide with indices.
 
using NestedData = void
 Terminator, no further nested data.
 
using InternalPosHandle = size_t
 Internal positions coincide with positions.
 
- Public Types inherited from codi::IndexManagerInterface< T_Index >
using Index = T_Index
 See IndexManagerInterface.
 
- Public Types inherited from codi::DataInterface< T_NestedData, T_InternalPosHandle >
using InternalPosHandle = T_InternalPosHandle
 See DataInterface.
 
using NestedData = T_NestedData
 See DataInterface.
 
using Position = EmptyPosition
 Contains position data for this DataInterface and all nested interfaces.
 

Public Member Functions

 LinearIndexManager (Index reservedIndices)
 Constructor.
 
IndexManagerInterface: Methods
void addToTapeValues (TapeValues &values) const
 Add storage and other information to the tape values.


 
template<typename Tape >
void freeIndex (Index &index) const
 Call on destruction of a primal value. Usually called from the destructor.


 
template<typename Tape >
bool assignIndex (Index &index)
 Call on assignment of a primal value, e.g. on w for w = a + b.
 
template<typename Tape >
bool assignUnusedIndex (Index &index)
 Call on registering input values.
 
template<typename Tape >
void copyIndex (Index &lhs, Index const &rhs)
 Call on copy of a primal value, e.g. w = a.
 
Index getLargestCreatedIndex () const
 Returns the largest created index.
 
DataInterface: Methods
template<typename TargetPosition >
TargetPosition extractPosition (Position const &pos) const
 Extract the position of a nested DataInterface from the global position object provide by this interface.
 
size_t getDataSize () const
 
Position getPosition () const
 
size_t getPushedDataCount (InternalPosHandle const &startPos)
 
Position getZeroPosition () const
 
void pushData ()
 Add data to the storage allocated by the implementation. The method can only be called after a call to reserveItems and only as often as the number of reserved items.
 
InternalPosHandle reserveItems (size_t const &items)
 Reserve this many items on the data stream. See pushData for details.
 
void resize (size_t const &totalSize)
 
void resetTo (Position const &pos)
 
void reset ()
 
void resetHard ()
 
void erase (Position const &start, Position const &end, bool recursive=true)
 
void setNested (NestedData *v)
 
void swap (LinearIndexManager< Index > &other)
 
template<int selectedDepth = -1, typename FunctionObject , typename... Args>
void evaluateForward (Position const &start, Position const &end, FunctionObject function, Args &&... args)
 Evaluates the function object with segments of continuous and valid data for all nested DataInterfaces.


 
template<int selectedDepth = -1, typename FunctionObject , typename... Args>
void evaluateReverse (Position const &start, Position const &end, FunctionObject function, Args &&... args)
 Evaluates the function object with segments of continuous and valid data for all nested DataInterfaces.




 
template<typename FunctionObject , typename... Args>
void forEachChunk (FunctionObject &function, bool recursive, Args &&... args)
 Calls the function object for each continuous segment of data.
 
template<typename FunctionObject , typename... Args>
void forEachForward (Position const &start, Position const &end, FunctionObject function, Args &&... args)
 Calls the function object for each item in the data stream. This call is not recursive.
 
template<typename FunctionObject , typename... Args>
void forEachReverse (Position const &start, Position const &end, FunctionObject function, Args &&... args)
 Calls the function object for each item in the data stream. This call is not recursive.
 
- Public Member Functions inherited from codi::IndexManagerInterface< T_Index >
void addToTapeValues (TapeValues &values) const
 Add storage and other information to the tape values.
 
Index getLargestCreatedIndex () const
 Returns the largest created index.
 
template<typename Tape >
bool assignIndex (Index &index)
 Call on assignment of a primal value, e.g. on w for w = a + b.
 
template<typename Tape >
bool assignUnusedIndex (Index &index)
 Call on registering input values.
 
template<typename Tape >
void copyIndex (Index &lhs, Index const &rhs)
 Call on copy of a primal value, e.g. w = a.
 
template<typename Tape >
void freeIndex (Index &index)
 Call on destruction of a primal value. Usually called from the destructor.
 
void reset ()
 Reset for a new recording.
 
- Public Member Functions inherited from codi::DataInterface< T_NestedData, T_InternalPosHandle >
void addDataSize (size_t const &size)
 Add this many items to the data stream, after the data has been manipulated via pointers obtained from getDataPointers().
 
template<typename... Data>
void getDataPointers (Data *&... pointers)
 Get pointers to the data from the storage implementation. The method can only be called after a call to reserveItems() and data can only be accessed from 0 to the number given by reserveItems (excluding). Afterwards, addDataSize() needs to be called with the actual number of elements that have been written.
 
template<typename... Data>
void pushData (Data const &... data)
 Add data to the storage allocated by the implementation. The method can only be called after a call to reserveItems and only as often as the number of reserved items.
 
InternalPosHandle reserveItems (size_t const &items)
 Reserve this many items on the data stream. See pushData for details.
 
void resize (size_t const &totalSize)
 
void reset ()
 
void resetHard ()
 
void resetTo (Position const &pos)
 
void erase (Position const &start, Position const &end, bool recursive=true)
 
size_t getDataSize () const
 
Position getPosition () const
 
size_t getPushedDataCount (InternalPosHandle const &startPos)
 
Position getZeroPosition () const
 
void addToTapeValues (TapeValues &values) const
 Add amount of stored data to the TapeValues object. Not called on the nested vector.
 
template<typename TargetPosition >
TargetPosition extractPosition (Position const &pos) const
 Extract the position of a nested DataInterface from the global position object provide by this interface.
 
void setNested (NestedData *v)
 
void swap (DataInterface &other)
 
template<int selectedDepth = -1, typename FunctionObject , typename... Args>
void evaluateForward (Position const &start, Position const &end, FunctionObject function, Args &&... args)
 Evaluates the function object with segments of continuous and valid data for all nested DataInterfaces.
 
template<int selectedDepth = -1, typename FunctionObject , typename... Args>
void evaluateReverse (Position const &start, Position const &end, FunctionObject function, Args &&... args)
 Evaluates the function object with segments of continuous and valid data for all nested DataInterfaces.
 
template<typename FunctionObject , typename... Args>
void forEachChunk (FunctionObject &function, bool recursive, Args &&... args)
 Calls the function object for each continuous segment of data.
 
template<typename FunctionObject , typename... Args>
void forEachForward (Position const &start, Position const &end, FunctionObject function, Args &&... args)
 Calls the function object for each item in the data stream. This call is not recursive.
 
template<typename FunctionObject , typename... Args>
void forEachReverse (Position const &start, Position const &end, FunctionObject function, Args &&... args)
 Calls the function object for each item in the data stream. This call is not recursive.
 

Static Public Attributes

IndexManagerInterface: Constants
static bool constexpr CopyNeedsStatement = false
 Copy optimization is implemented.
 
static bool constexpr IsLinear = true
 Tightly coupled to statements.
 
static bool constexpr NeedsStaticStorage = false
 Linear indices are not meaningful across tape instances.
 
- Static Public Attributes inherited from codi::IndexManagerInterface< T_Index >
static Index constexpr InactiveIndex = Index(0)
 Default inactive index for all index managers.
 
static Index constexpr InvalidIndex
 Default invalid index for all index mangers (max value for unsigned types).
 
static bool constexpr CopyNeedsStatement
 True if no copy optimization is implemented. See IndexManagerInterface.
 
static bool constexpr IsLinear
 True if identifiers are coupled to the statements. See IndexManagerInterface.
 
static bool constexpr NeedsStaticStorage = false
 True if the index manager is specific to a tape type (and not a tape instance). See IndexManagerInterface.
 

Detailed Description

template<typename T_Index>
struct codi::LinearIndexManager< T_Index >

Identifiers are created in a linear fashion. Each assign creates a new index which is counted up.

A simple copy optimization is implemented here. Since each index is only bound to one primal value, the index can simply be copied.

Mathematical and implementational details are explained in SBG2021Index.

Since this index manager is tightly coupled to the statements, it is specific to a tape instance and requires non-static storage.

Since there is a one-to-one relation between tape and index manager for linear index managers, this index manager is thread-safe. Due to its linear nature, however, it can only be used to record tapes in parallel that do not depend on each other.

Template Parameters
T_IndexType for the identifier, usually an integer type.

Member Function Documentation

◆ addToTapeValues()

template<typename T_Index >
void codi::LinearIndexManager< T_Index >::addToTapeValues ( TapeValues & values) const
inline

Add storage and other information to the tape values.

Parameters
[in,out]valuesWill only create new data entries and no new section.

Implementation: Adds maximum live indices.

◆ assignIndex()

template<typename T_Index >
template<typename Tape >
bool codi::LinearIndexManager< T_Index >::assignIndex ( Index & index)
inline

Call on assignment of a primal value, e.g. on w for w = a + b.

Returns
true if new indices have been generated internally.

◆ assignUnusedIndex()

template<typename T_Index >
template<typename Tape >
bool codi::LinearIndexManager< T_Index >::assignUnusedIndex ( Index & index)
inline

Call on registering input values.

Returns
true if new indices have been generated internally.

◆ copyIndex()

template<typename T_Index >
template<typename Tape >
void codi::LinearIndexManager< T_Index >::copyIndex ( Index & lhs,
Index const & rhs )
inline

Call on copy of a primal value, e.g. w = a.

◆ erase()

template<typename T_Index >
void codi::LinearIndexManager< T_Index >::erase ( Position const & start,
Position const & end,
bool recursive = true )
inline

Erase the given range of data. Implementations may choose to free allocated memory. The parameter recursive controls whether erase is also called on nested interfaces. Implementation: Empty. Linear index management depends on indices being handed out in a contiguous fashion, and deleting a range of indices is not meaningful.

◆ evaluateForward()

template<typename T_Index >
template<int selectedDepth = -1, typename FunctionObject , typename... Args>
void codi::LinearIndexManager< T_Index >::evaluateForward ( Position const & start,
Position const & end,
FunctionObject function,
Args &&... args )
inline

Evaluates the function object with segments of continuous and valid data for all nested DataInterfaces.

func is called for each region of continuous data that is valid for all nested DataInterfaces. func is then called as

function(args...,
start, end, dataEntry1*, dataEntry2*,
startNested, endNested, dataEntry1Nested*, dataEntry2Nested*,
startNestedNested, endNestedNested, dataEntry1NestedNested*,
...);

What kind of data is appended by each DataInterface is implementation dependent. The default is the set (start, end, dataEntry1, dataEntry2, etc.) which is appended.

It has to hold start <= end.

Positions count up in this call.

Parameters
[in]startStarting position.
[in]endEnding position.
[in]functionFunction object called.
[in,out]argsAdditional arguments for the function object.
Template Parameters
selectedDepthSelected depth for the recursion into nested data interfaces. 0 means that only the data from this interface is used. A value of 1 means that the data from the first nested interface is used, etc.. A value of -1 can be used to select the innermost interface, regardless of the number of nested interfaces.
FunctionObjectFunction object which is called.
ArgsArguments for the function object.

This is a terminating DataInterface. The function object is always called and selectedDepth can be ignored.

◆ evaluateReverse()

template<typename T_Index >
template<int selectedDepth = -1, typename FunctionObject , typename... Args>
void codi::LinearIndexManager< T_Index >::evaluateReverse ( Position const & start,
Position const & end,
FunctionObject function,
Args &&... args )
inline

Evaluates the function object with segments of continuous and valid data for all nested DataInterfaces.



Same as evaluateForward. It has to hold start >= end. Positions count down in this call.

Parameters
[in]startStarting position.
[in]endEnding position.
[in]functionFunction object called.
[in,out]argsAdditional arguments for the function object.
Template Parameters
selectedDepthSelected depth for the recursion into nested data interfaces. 0 means that only the data from this interface is used. A value of 1 means that the data from the first nested interface is used, etc.. A value of -1 can be used to select the innermost interface, regardless of the number of nested interfaces.
FunctionObjectFunction object which is called.
ArgsArguments for the function object.

This is a terminating DataInterface. The function object is always called and selectedDepth can be ignored.

◆ extractPosition()

template<typename T_Index >
template<typename TargetPosition >
TargetPosition codi::LinearIndexManager< T_Index >::extractPosition ( Position const & pos) const
inline

Extract the position of a nested DataInterface from the global position object provide by this interface.

Parameters
[in]posPosition of the DataInterface.
Template Parameters
TargetPositionPosition definition of a nested DataInterface.

◆ forEachChunk()

template<typename T_Index >
template<typename FunctionObject , typename... Args>
void codi::LinearIndexManager< T_Index >::forEachChunk ( FunctionObject & function,
bool recursive,
Args &&... args )
inline

Calls the function object for each continuous segment of data.

The call is

function(chunk, args...);

Chunk is of the type ChunkBase.

Parameters
[in]functionFunction object called.
[in]recursiveTrue if same call should be performed for all nested DataInterfaces.
[in,out]argsAdditional arguments for the function object.
Template Parameters
FunctionObjectFunction object which is called.
ArgsArguments for the function object.

◆ forEachForward()

template<typename T_Index >
template<typename FunctionObject , typename... Args>
void codi::LinearIndexManager< T_Index >::forEachForward ( Position const & start,
Position const & end,
FunctionObject function,
Args &&... args )
inline

Calls the function object for each item in the data stream. This call is not recursive.

The call to function is

function(args..., entry1*, entry2*, ...);

It has to hold start <= end.

Parameters
[in]startStarting position.
[in]endEnding position
[in]functionFunction object called.
[in,out]argsAdditional arguments for the function object.
Template Parameters
FunctionObjectFunction object which is called.
ArgsArguments for the function object.

◆ forEachReverse()

template<typename T_Index >
template<typename FunctionObject , typename... Args>
void codi::LinearIndexManager< T_Index >::forEachReverse ( Position const & start,
Position const & end,
FunctionObject function,
Args &&... args )
inline

Calls the function object for each item in the data stream. This call is not recursive.

See forEachForward.

It has to hold start >= end.

Parameters
[in]startStarting position.
[in]endEnding position
[in]functionFunction object called.
[in,out]argsAdditional arguments for the function object.
Template Parameters
FunctionObjectFunction object which is called.
ArgsArguments for the function object.

◆ freeIndex()

template<typename T_Index >
template<typename Tape >
void codi::LinearIndexManager< T_Index >::freeIndex ( Index & index) const
inline

Call on destruction of a primal value. Usually called from the destructor.



Implementation: Freed indices are ignored.

◆ getDataSize()

template<typename T_Index >
size_t codi::LinearIndexManager< T_Index >::getDataSize ( ) const
inline

Returns
Total number of data items stored.

◆ getLargestCreatedIndex()

template<typename T_Index >
Index codi::LinearIndexManager< T_Index >::getLargestCreatedIndex ( ) const
inline

Returns the largest created index.

This is the largest entry in the adjoint vectors created by the tapes. The following properties are specific to the LinearIndexManager:

  1. tape resets reset the largest created index to zero,
  2. the largest created index coincides with the largest assigned index.

◆ getPosition()

template<typename T_Index >
Position codi::LinearIndexManager< T_Index >::getPosition ( ) const
inline

Returns
The current global position of this DataInterface and all nested interfaces. The current position coincides with the largest created index.

◆ getPushedDataCount()

template<typename T_Index >
size_t codi::LinearIndexManager< T_Index >::getPushedDataCount ( InternalPosHandle const & startPos)
inline

Compute the number of data items stored after a call to reserveItems.

◆ getZeroPosition()

template<typename T_Index >
Position codi::LinearIndexManager< T_Index >::getZeroPosition ( ) const
inline

Returns
The start position of the DataInterface and all nested interfaces. The zero position coincides with the smallest index that may be assigned to active AD variables.

◆ pushData()

template<typename T_Index >
void codi::LinearIndexManager< T_Index >::pushData ( )
inline

Add data to the storage allocated by the implementation. The method can only be called after a call to reserveItems and only as often as the number of reserved items.

pushData() can be called less often than indicated with reserveItems(). The call to reserveItems only represents the maximum number of data items that can be pushed safely.

After a new call to reserveItems(), only this many number of data items can be pushed, leftovers will not accumulate.

For an example of how to use pushData, please see the DataInterface documentation.

Parameters
[in]dataThe number of arguments has to match the number of data stores of the implementation.
Template Parameters
DataTypes of the pushed data.

◆ reserveItems()

template<typename T_Index >
InternalPosHandle codi::LinearIndexManager< T_Index >::reserveItems ( size_t const & items)
inline

Reserve this many items on the data stream. See pushData for details.

Parameters
[in]itemsNumber of data items to reserve.
Returns
Can be used in getPushedDataCount(). Only the newest handle is valid.

◆ reset()

template<typename T_Index >
void codi::LinearIndexManager< T_Index >::reset ( )
inline

Reset to the zero position. Data is not deallocated. Also called on nested interfaces.

◆ resetHard()

template<typename T_Index >
void codi::LinearIndexManager< T_Index >::resetHard ( )
inline

Reset to the zero position. Data is deallocated and the default size is allocated again. Also called on nested interfaces.

◆ resetTo()

template<typename T_Index >
void codi::LinearIndexManager< T_Index >::resetTo ( Position const & pos)
inline

Reset to the given position. Data is not deallocated. Also called on the nested interfaces.

◆ resize()

template<typename T_Index >
void codi::LinearIndexManager< T_Index >::resize ( size_t const & totalSize)
inline

Allocate the requested number of data items.

◆ setNested()

template<typename T_Index >
void codi::LinearIndexManager< T_Index >::setNested ( NestedData * v)
inline

Set the pointer to the nested vector. Needs to be done before any other action and only once.

◆ swap()

template<typename T_Index >
void codi::LinearIndexManager< T_Index >::swap ( LinearIndexManager< Index > & other)
inline

Swap with other DataInterface of the same type.


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