CppADCodeGen  HEAD
A C++ Algorithmic Differentiation Package with Source Code Generation
CppAD::cg::CodeHandler< Base > Class Template Reference
Inheritance diagram for CppAD::cg::CodeHandler< Base >:
Inheritance graph
Collaboration diagram for CppAD::cg::CodeHandler< Base >:
Collaboration graph

Classes

struct  LoopData
 

Public Types

using PathNode = OperationPathNode< Base >
 
using SourceCodePath = std::vector< PathNode >
 
using ScopePath = std::vector< ScopePathElement< Base > >
 
using Node = OperationNode< Base >
 
using Arg = Argument< Base >
 
using CGB = CG< Base >
 
using ScopeIDType = unsigned short
 

Public Member Functions

 CodeHandler (size_t varCount=50)
 
 CodeHandler (const CodeHandler &)=delete
 
CodeHandleroperator= (const CodeHandler &)=delete
 
virtual ~CodeHandler ()
 
void setReuseVariableIDs (bool reuse)
 
bool isReuseVariableIDs () const
 
template<class VectorCG >
void makeVariables (VectorCG &variables)
 
void makeVariables (std::vector< AD< CGB > > &variables)
 
void makeVariable (AD< CGB > &variable)
 
void makeVariable (CGB &variable)
 
size_t getIndependentVariableSize () const
 
size_t getIndependentVariableIndex (const Node &var) const
 
const CodeHandlerVector< Base, size_t > & getVariablesIDs () const
 
size_t getMaximumVariableID () const
 
bool isVerbose () const
 
void setVerbose (bool verbose)
 
JobTimergetJobTimer () const
 
void setJobTimer (JobTimer *jobTimer)
 
bool isZeroDependents () const
 
void setZeroDependents (bool zeroDependents)
 
size_t getOperationTreeVisitId () const
 
void startNewOperationTreeVisit ()
 
bool isVisited (const Node &node) const
 
void markVisited (const Node &node)
 
std::string getAtomicFunctionName (size_t id) const
 
const std::map< size_t, CGAbstractAtomicFun< Base > *> & getAtomicFunctions () const
 
const std::vector< int > & getExternalFuncMaxForwardOrder () const
 
const std::vector< int > & getExternalFuncMaxReverseOrder () const
 
const std::string * getLoopName (size_t id) const
 
const std::vector< ScopePath > & getScopes () const
 
std::vector< SourceCodePath > findPaths (Node &root, Node &target, size_t max)
 
BidirGraph< Base > findPathGraph (Node &root, Node &target)
 
BidirGraph< Base > findPathGraph (Node &root, Node &target, size_t &bifurcations, size_t maxBifurcations=(std::numeric_limits< size_t >::max)())
 
virtual void generateCode (std::ostream &out, Language< Base > &lang, CppAD::vector< CGB > &dependent, VariableNameGenerator< Base > &nameGen, const std::string &jobName="source")
 
virtual void generateCode (std::ostream &out, Language< Base > &lang, std::vector< CGB > &dependent, VariableNameGenerator< Base > &nameGen, const std::string &jobName="source")
 
virtual void generateCode (std::ostream &out, Language< Base > &lang, ArrayView< CGB > &dependent, VariableNameGenerator< Base > &nameGen, const std::string &jobName="source")
 
virtual void generateCode (std::ostream &out, Language< Base > &lang, CppAD::vector< CGB > &dependent, VariableNameGenerator< Base > &nameGen, std::vector< std::string > &atomicFunctions, const std::string &jobName="source")
 
virtual void generateCode (std::ostream &out, Language< Base > &lang, std::vector< CGB > &dependent, VariableNameGenerator< Base > &nameGen, std::vector< std::string > &atomicFunctions, const std::string &jobName="source")
 
virtual void generateCode (std::ostream &out, Language< Base > &lang, ArrayView< CGB > &dependent, VariableNameGenerator< Base > &nameGen, std::vector< std::string > &atomicFunctions, const std::string &jobName="source")
 
size_t getTemporaryVariableCount () const
 
size_t getTemporaryArraySize () const
 
size_t getTemporarySparseArraySize () const
 
virtual void reset ()
 
void resetNodes ()
 
NodecloneNode (const Node &n)
 
NodemakeNode (CGOpCode op)
 
NodemakeNode (CGOpCode op, const Arg &arg)
 
NodemakeNode (CGOpCode op, std::vector< Arg > &&args)
 
NodemakeNode (CGOpCode op, std::vector< size_t > &&info, std::vector< Arg > &&args)
 
NodemakeNode (CGOpCode op, const std::vector< size_t > &info, const std::vector< Arg > &args)
 
LoopStartOperationNode< Base > * makeLoopStartNode (Node &indexDcl, size_t iterationCount)
 
LoopStartOperationNode< Base > * makeLoopStartNode (Node &indexDcl, IndexOperationNode< Base > &iterCount)
 
LoopEndOperationNode< Base > * makeLoopEndNode (LoopStartOperationNode< Base > &loopStart, const std::vector< Arg > &endArgs)
 
PrintOperationNode< Base > * makePrintNode (const std::string &before, const Arg &arg, const std::string &after)
 
IndexOperationNode< Base > * makeIndexNode (Node &indexDcl)
 
IndexOperationNode< Base > * makeIndexNode (LoopStartOperationNode< Base > &loopStart)
 
IndexOperationNode< Base > * makeIndexNode (IndexAssignOperationNode< Base > &indexAssign)
 
IndexAssignOperationNode< Base > * makeIndexAssignNode (Node &index, IndexPattern &indexPattern, IndexOperationNode< Base > &index1)
 
IndexAssignOperationNode< Base > * makeIndexAssignNode (Node &index, IndexPattern &indexPattern, IndexOperationNode< Base > *index1, IndexOperationNode< Base > *index2)
 
NodemakeIndexDclrNode (const std::string &name)
 
size_t getManagedNodesCount () const
 
const std::vector< Node * > & getManagedNodes () const
 
void deleteManagedNodes (size_t start, size_t end)
 
CGB createCG (const Arg &arg)
 
const std::map< size_t, LoopModel< Base > * > & getLoops () const
 
LoopModel< Base > * getLoop (size_t loopId) const
 
size_t addLoopDependentIndexPattern (IndexPattern &jacPattern)
 
void manageLoopDependentIndexPattern (const IndexPattern *pattern)
 
size_t addLoopIndependentIndexPattern (IndexPattern &pattern, size_t hint)
 
CGB solveFor (Node &expression, Node &var)
 
bool isSolvable (Node &expression, Node &var)
 
void substituteIndependent (const CGB &indep, const CGB &dep, bool removeFromIndeps=true)
 
void substituteIndependent (Node &indep, Node &dep, bool removeFromIndeps=true)
 
void undoSubstituteIndependent (Node &indep)
 
void removeIndependent (Node &indep)
 
bool manageOperationNodeMemory (Node *code)
 

Static Public Member Functions

static void findRandomIndexPatterns (IndexPattern *ip, std::set< RandomIndexPattern *> &found)
 

Protected Member Functions

virtual NodemanageOperationNode (Node *code)
 
void addVector (CodeHandlerVectorSync< Base > *v)
 
void removeVector (CodeHandlerVectorSync< Base > *v)
 
virtual void markCodeBlockUsed (Node &code)
 
bool handleTemporaryVarInDiffScopes (Node &code, size_t oldScope, size_t newScope)
 
void replaceWithConditionalTempVar (Node &tmp, IndexOperationNode< Base > &iterationIndexOp, const std::vector< size_t > &iterationRegions, ScopeIDType oldScope, ScopeIDType commonScopeColor)
 
void updateTemporaryVarInDiffScopes (Node &code)
 
void restoreTemporaryVar (Node &tmp)
 
void restoreTemporaryVar (Node *tmp, Node *opClone)
 
void updateVarScopeUsage (Node *node, ScopeIDType usageScope, ScopeIDType oldUsageScope)
 
void addScopeToVarOrder (size_t scope, size_t &e)
 
size_t findFirstDifferentScope (size_t color1, size_t color2)
 
void optimizeIfs ()
 
void replaceScope (Node *node, ScopeIDType oldScope, ScopeIDType newScope)
 
void breakCyclicDependency (Node *node, size_t scope, Node *endIf)
 
bool containedInScope (const Node &node, ScopeIDType scope)
 
virtual void registerAtomicFunction (CGAbstractAtomicFun< Base > &atomic)
 
virtual void checkVariableCreation (Node &code)
 
void addToEvaluationQueue (Node &arg)
 
void reduceTemporaryVariables (ArrayView< CGB > &dependent)
 
void reorderOperations (ArrayView< CGB > &dependent)
 
void reorderOperation (Node &node)
 
size_t findLastTemporaryLocation (Node &node)
 
void repositionEvaluationQueue (size_t fromPos, size_t toPos)
 
void determineLastTempVarUsage (Node &node)
 
void findVariableDependencies ()
 
void findVariableDependencies (size_t i, Node &node)
 
void dependentAdded2EvaluationQueue (Node &node)
 
void updateEvaluationQueueOrder (Node &node, size_t newEvalOrder)
 
bool isIndependent (const Node &arg) const
 
bool isTemporary (const Node &arg) const
 
size_t getEvaluationOrder (const Node &node) const
 
void setEvaluationOrder (Node &node, size_t order)
 
size_t getLastUsageEvaluationOrder (const Node &node) const
 
void setLastUsageEvaluationOrder (const Node &node, size_t last)
 
size_t getTotalUsageCount (const Node &node) const
 
void setTotalUsageCount (const Node &node, size_t cout)
 
void increaseTotalUsageCount (const Node &node)
 
void resetManagedNodes ()
 
void findPaths (SourceCodePath &path2node, Node &code, std::vector< SourceCodePath > &found, size_t max)
 
CGB solveFor (const SourceCodePath &path)
 
CGB collectVariable (Node &expression, const SourceCodePath &path1, const SourceCodePath &path2, size_t bifPos)
 
CGB collectVariableAddSub (const SourceCodePath &pathLeft, const SourceCodePath &pathRight)
 
bool isCollectableVariableAddSub (const SourceCodePath &pathLeft, const SourceCodePath &pathRight, bool throwEx)
 
bool isSolvable (const SourceCodePath &path) const
 

Static Protected Member Functions

static bool containsArgument (const Node &node, const Node &arg)
 
static bool isTemporaryArray (const Node &arg)
 
static bool isTemporarySparseArray (const Node &arg)
 
static NodegetOperationFromAlias (Node &alias)
 
static std::vector< SourceCodePath > findPathsFromNode (const std::vector< SourceCodePath > nodePaths, Node &node)
 

Protected Attributes

size_t _idVisit
 
size_t _idCount
 
size_t _idArrayCount
 
size_t _idSparseArrayCount
 
size_t _idAtomicCount
 
std::vector< Node * > _independentVariables
 
ArrayView< CGB > * _dependents
 
std::vector< Node * > _codeBlocks
 
std::set< CodeHandlerVectorSync< Base > * > _managedVectors
 
CodeHandlerVector< Base, size_t > _lastVisit
 
CodeHandlerVector< Base, ScopeIDType > _scope
 
CodeHandlerVector< Base, size_t > _evaluationOrder
 
CodeHandlerVector< Base, size_t > _lastUsageOrder
 
CodeHandlerVector< Base, size_t > _totalUseCount
 
CodeHandlerVector< Base, size_t > _operationCount
 
CodeHandlerVector< Base, size_t > _varId
 
std::vector< Node * > _variableOrder
 
std::vector< std::set< Node * > > _variableDependencies
 
std::vector< std::vector< Node * > > _scopedVariableOrder
 
LoopData _loops
 
std::map< size_t, CGAbstractAtomicFun< Base > * > _atomicFunctions
 
std::map< std::string, size_t > _atomicFunctionName2Index
 
std::vector< std::string > * _atomicFunctionsOrder
 
std::vector< int > _atomicFunctionsMaxForward
 
std::vector< int > _atomicFunctionsMaxReverse
 
bool _used
 
bool _reuseIDs
 
ScopeIDType _scopeColorCount
 
ScopeIDType _currentScopeColor
 
std::vector< ScopePath > _scopes
 
std::list< std::pair< Node *, Node *> > _alteredNodes
 
Language< Base > * _lang
 
size_t _minTemporaryVarID
 
bool _zeroDependents
 
bool _verbose
 
JobTimer_jobTimer
 
Node_auxIndexI
 
IndexOperationNode< Base > * _auxIterationIndexOp
 

Friends

class CodeHandlerVectorSync< Base >
 
class CG< Base >
 
class CGAbstractAtomicFun< Base >
 
class BaseAbstractAtomicFun< Base >
 
class LoopModel< Base >
 

Detailed Description

template<class Base>
class CppAD::cg::CodeHandler< Base >

Helper class to analyze the operation graph and generate source code for several languages

Author
Joao Leal

Definition at line 28 of file code_handler.hpp.

Constructor & Destructor Documentation

◆ ~CodeHandler()

template<class Base >
CppAD::cg::CodeHandler< Base >::~CodeHandler ( )
inlinevirtual

Destructor

Definition at line 57 of file code_handler_impl.hpp.

Member Function Documentation

◆ breakCyclicDependency()

template<class Base >
void CppAD::cg::CodeHandler< Base >::breakCyclicDependency ( Node node,
size_t  scope,
Node endIf 
)
inlineprotected

Removes cyclic dependencies when 'ifs' are merged together. Relative variable order must have already been defined.

Parameters
nodethe node being visited
scopethe scope where the cyclic dependency could appear (or scopes inside it)
endIfthe dependency to remove

Definition at line 1370 of file code_handler_impl.hpp.

Referenced by CppAD::cg::CodeHandler< ScalarOut >::makeVariables().

◆ checkVariableCreation()

template<class Base >
void CppAD::cg::CodeHandler< Base >::checkVariableCreation ( Node code)
protectedvirtual

Save atomic function related information

make sure new temporary variables are NOT created for the independent variables and that a dependency did not use it first

Operation that mark a change in variable scope are always added

Definition at line 1444 of file code_handler_impl.hpp.

Referenced by CppAD::cg::CodeHandler< ScalarOut >::generateCode(), and CppAD::cg::CodeHandler< ScalarOut >::makeVariables().

◆ cloneNode()

template<class Base >
OperationNode< Base > * CppAD::cg::CodeHandler< Base >::cloneNode ( const Node n)
inline

Creates a shallow clone of an operation node

Definition at line 524 of file code_handler_impl.hpp.

Referenced by CppAD::cg::CodeHandler< ScalarOut >::makeVariables(), and CppAD::cg::CodeHandler< ScalarOut >::replaceWithConditionalTempVar().

◆ collectVariable()

template<class Base >
CG< Base > CppAD::cg::CodeHandler< Base >::collectVariable ( Node expression,
const SourceCodePath &  path1,
const SourceCodePath &  path2,
size_t  bifPos 
)
inlineprotected

Reduces the number of occurrences of a variable in an equation. For instance: f(x,y) = x + y + x could become f(x,y) = 2 * x + y

Parameters
expressionThe original expression (f(x, y))
path1A path from the equation residual to where the variable is used.
path2A different path from the equation residual to where the variable is used.
Returns
The new expression for the equation
Exceptions
CGExceptionif it is not possible to combine the multiple occurrences of the variable

Check common path

Replace the new expression

Definition at line 25 of file collect_variable.hpp.

Referenced by CppAD::cg::CodeHandler< ScalarOut >::makeVariables().

◆ collectVariableAddSub()

template<class Base >
CG< Base > CppAD::cg::CodeHandler< Base >::collectVariableAddSub ( const SourceCodePath &  pathLeft,
const SourceCodePath &  pathRight 
)
inlineprotected

Argument validation

Check operations after the divergence

find the coefficient which will multiply the variable

Clone operations in expression without var

add term for variable

Definition at line 100 of file collect_variable.hpp.

Referenced by CppAD::cg::CodeHandler< ScalarOut >::makeVariables().

◆ deleteManagedNodes()

template<class Base >
void CppAD::cg::CodeHandler< Base >::deleteManagedNodes ( size_t  start,
size_t  end 
)
inline

Allows to delete OperationNodes that are managed internally.

Warning
: This is a dangerous method, make sure these nodes are not used anywhere else!
Parameters
startThe index of the first OperationNode to be deleted
endThe index after the last OperationNode to be deleted

Definition at line 651 of file code_handler_impl.hpp.

Referenced by CppAD::cg::CodeHandler< ScalarOut >::makeVariables().

◆ dependentAdded2EvaluationQueue()

template<class Base >
void CppAD::cg::CodeHandler< Base >::dependentAdded2EvaluationQueue ( Node node)
inlineprotected

Defines the evaluation order for the code fragments that do not create variables (right hand side variables)

Parameters
codeThe operation just added to the evaluation order

Definition at line 1970 of file code_handler_impl.hpp.

Referenced by CppAD::cg::CodeHandler< ScalarOut >::generateCode(), and CppAD::cg::CodeHandler< ScalarOut >::makeVariables().

◆ determineLastTempVarUsage()

template<class Base >
void CppAD::cg::CodeHandler< Base >::determineLastTempVarUsage ( Node node)
inlineprotected

Determines when each temporary variable is last used in the evaluation order

Parameters
nodeThe current node for which the number of usages is to be to determined

count variable usage

temporary variables from outside the loop which are used within the loop cannot be overwritten inside that loop

Definition at line 1873 of file code_handler_impl.hpp.

Referenced by CppAD::cg::CodeHandler< ScalarOut >::makeVariables(), and CppAD::cg::CodeHandler< ScalarOut >::reduceTemporaryVariables().

◆ findFirstDifferentScope()

template<class Base >
size_t CppAD::cg::CodeHandler< Base >::findFirstDifferentScope ( size_t  color1,
size_t  color2 
)
inlineprotected

Determines the depth of the first different scope from scope paths of two scopes

Parameters
color1scope color 1
color2scope color 2
Returns
the depth of the first different scope

Definition at line 1249 of file code_handler_impl.hpp.

Referenced by CppAD::cg::CodeHandler< ScalarOut >::makeVariables(), and CppAD::cg::CodeHandler< ScalarOut >::updateTemporaryVarInDiffScopes().

◆ findLastTemporaryLocation()

template<class Base >
size_t CppAD::cg::CodeHandler< Base >::findLastTemporaryLocation ( Node node)
inlineprotected

Determine the highest location in the evaluation queue of temporary variables used by an operation node in the same scope.

Returns
the highest location of the temporary variables or the location of node itself if it doesn't use any temporary variable (in the same scope)

Definition at line 1809 of file code_handler_impl.hpp.

Referenced by CppAD::cg::CodeHandler< ScalarOut >::makeVariables(), and CppAD::cg::CodeHandler< ScalarOut >::reorderOperation().

◆ findPaths()

template<class Base >
std::vector< std::vector< OperationPathNode< Base > > > CppAD::cg::CodeHandler< Base >::findPaths ( Node root,
Node target,
size_t  max 
)
inline

Finds occurrences of a source code fragment in an operation graph.

Parameters
rootthe operation graph where to search
targetthe source code fragment to find in root
maxthe maximum number of occurrences of code to find in root
Returns
the paths from root to code

Definition at line 117 of file operation_path.hpp.

Referenced by CppAD::cg::CodeHandler< ScalarOut >::findPaths(), and CppAD::cg::CodeHandler< ScalarOut >::makeVariables().

◆ findVariableDependencies()

template<class Base >
void CppAD::cg::CodeHandler< Base >::findVariableDependencies ( )
inlineprotected

◆ generateCode() [1/3]

template<class Base>
void CppAD::cg::CodeHandler< Base >::generateCode ( std::ostream &  out,
Language< Base > &  lang,
CppAD::vector< CGB > &  dependent,
VariableNameGenerator< Base > &  nameGen,
const std::string &  jobName = "source" 
)
virtual

◆ generateCode() [2/3]

template<class Base>
void CppAD::cg::CodeHandler< Base >::generateCode ( std::ostream &  out,
Language< Base > &  lang,
CppAD::vector< CGB > &  dependent,
VariableNameGenerator< Base > &  nameGen,
std::vector< std::string > &  atomicFunctions,
const std::string &  jobName = "source" 
)
virtual

Creates the source code from the operations registered so far.

Parameters
outThe output stream where the source code is to be printed.
langThe targeted language.
dependentThe dependent variables for which the source code should be generated. By defining this vector the number of operations in the source code can be reduced and thus providing a more optimized code.
nameGenProvides the rules for variable name creation.
atomicFunctionsThe order of the atomic functions.

Definition at line 244 of file code_handler_impl.hpp.

◆ generateCode() [3/3]

template<class Base>
void CppAD::cg::CodeHandler< Base >::generateCode ( std::ostream &  out,
Language< Base > &  lang,
ArrayView< CGB > &  dependent,
VariableNameGenerator< Base > &  nameGen,
std::vector< std::string > &  atomicFunctions,
const std::string &  jobName = "source" 
)
virtual

the first variable IDs are for the independent variables

determine the number of times each variable is used

determine the variable creation order

Generate flat variable order (without scopes)

Reuse temporary variables

Creates the source code for a specific language

clean-up

Definition at line 266 of file code_handler_impl.hpp.

◆ getAtomicFunctionName()

template<class Base >
std::string CppAD::cg::CodeHandler< Base >::getAtomicFunctionName ( size_t  id) const
inline

Provides the name used by an atomic function with a given ID.

Parameters
idthe atomic function ID.
Returns
the atomic function name if it was registered or an empty string otherwise.

Definition at line 178 of file code_handler_impl.hpp.

Referenced by CppAD::cg::CodeHandler< ScalarOut >::makeVariables().

◆ getAtomicFunctions()

template<class Base >
const std::map< size_t, CGAbstractAtomicFun< Base > *> & CppAD::cg::CodeHandler< Base >::getAtomicFunctions ( ) const
inline

Provides a map with all the currently registered atomic functions.

Returns
a map with the atomic function ID as key and the atomic function as value

Definition at line 188 of file code_handler_impl.hpp.

Referenced by CppAD::cg::CodeHandler< ScalarOut >::makeVariables().

◆ getExternalFuncMaxForwardOrder()

template<class Base >
const std::vector< int > & CppAD::cg::CodeHandler< Base >::getExternalFuncMaxForwardOrder ( ) const

Provides the maximum forward mode order used by all atomic functions in the last call to ::generateCode (-1 means forward mode not used).

Definition at line 193 of file code_handler_impl.hpp.

Referenced by CppAD::cg::CodeHandler< ScalarOut >::makeVariables(), and CppAD::cg::ModelCSourceGen< Base >::prepareSparseReverseOneWithLoops().

◆ getExternalFuncMaxReverseOrder()

template<class Base >
const std::vector< int > & CppAD::cg::CodeHandler< Base >::getExternalFuncMaxReverseOrder ( ) const

Provides the maximum reverse mode order used by all atomic functions in the last call to ::generateCode (-1 means forward mode not used).

Definition at line 198 of file code_handler_impl.hpp.

Referenced by CppAD::cg::CodeHandler< ScalarOut >::makeVariables(), and CppAD::cg::ModelCSourceGen< Base >::prepareSparseReverseOneWithLoops().

◆ getIndependentVariableIndex()

template<class Base >
size_t CppAD::cg::CodeHandler< Base >::getIndependentVariableIndex ( const Node var) const
Exceptions
CGExceptionif a variable is not found in the independent vector

Definition at line 141 of file code_handler_impl.hpp.

Referenced by CppAD::cg::CodeHandler< ScalarOut >::makeVariables().

◆ getIndependentVariableSize()

◆ getLoopName()

template<class Base >
const std::string * CppAD::cg::CodeHandler< Base >::getLoopName ( size_t  id) const
inline

Provides the name used by a loop atomic function with a given ID.

Parameters
idthe atomic function ID.
Returns
a pointer to the atomic loop function name if it was registered or nullptr otherwise

Definition at line 203 of file code_handler_impl.hpp.

Referenced by CppAD::cg::CodeHandler< ScalarOut >::makeVariables().

◆ getManagedNodes()

template<class Base >
const std::vector< OperationNode< Base > * > & CppAD::cg::CodeHandler< Base >::getManagedNodes ( ) const
inline

Provides the OperationNodes created by the model.

Definition at line 646 of file code_handler_impl.hpp.

Referenced by CppAD::cg::CodeHandler< ScalarOut >::makeVariables().

◆ getManagedNodesCount()

template<class Base >
size_t CppAD::cg::CodeHandler< Base >::getManagedNodesCount ( ) const
inline

Provides the current number of OperationNodes created by the model. This number is not the total number of operations in the final model since it also contains Operations nodes marking independent variables and there could be unused operations by the model (dead-code).

Returns
The number of OperationNodes created by the model.

Definition at line 641 of file code_handler_impl.hpp.

Referenced by CppAD::cg::EvaluatorBase< ScalarIn, double, adouble, Evaluator< ScalarIn, double, adouble > >::clear(), and CppAD::cg::CodeHandler< ScalarOut >::makeVariables().

◆ getTotalUsageCount()

template<class Base >
size_t CppAD::cg::CodeHandler< Base >::getTotalUsageCount ( const Node node) const
inlineprotected

Provides the total number of times the result of an operation node is being used as an argument for another operation.

Returns
the total usage count

Definition at line 2142 of file code_handler_impl.hpp.

Referenced by CppAD::cg::CodeHandler< ScalarOut >::makeVariables().

◆ getVariablesIDs()

template<class Base >
const CodeHandlerVector< Base, size_t > & CppAD::cg::CodeHandler< Base >::getVariablesIDs ( ) const
inline

Provides variable IDs that were assigned to operation nodes. Zero means that no variable is assigned. The first IDs are reserved for the independent variables. It can be an empty vector if IDs have not yet been assigned.

Definition at line 101 of file code_handler_impl.hpp.

Referenced by CppAD::cg::CodeHandler< ScalarOut >::makeVariables().

◆ handleTemporaryVarInDiffScopes()

template<class Base >
bool CppAD::cg::CodeHandler< Base >::handleTemporaryVarInDiffScopes ( Node code,
size_t  oldScope,
size_t  newScope 
)
inlineprotected

allow Array elements to use a CGTmp instead of a CGArrayCreationOp

does this variable require a condition based on indexes?

determine the iterations which use this temporary variable

Definition at line 927 of file code_handler_impl.hpp.

Referenced by CppAD::cg::CodeHandler< ScalarOut >::makeVariables().

◆ isCollectableVariableAddSub()

template<class Base >
bool CppAD::cg::CodeHandler< Base >::isCollectableVariableAddSub ( const SourceCodePath &  pathLeft,
const SourceCodePath &  pathRight,
bool  throwEx 
)
inlineprotected

Check operations after the divergence

Definition at line 240 of file collect_variable.hpp.

Referenced by CppAD::cg::CodeHandler< ScalarOut >::makeVariables().

◆ isReuseVariableIDs()

template<class Base >
bool CppAD::cg::CodeHandler< Base >::isReuseVariableIDs ( ) const
inline

Whether or not node IDs are reused once they are not need by a node anymore.

Definition at line 71 of file code_handler_impl.hpp.

◆ isZeroDependents()

template<class Base >
bool CppAD::cg::CodeHandler< Base >::isZeroDependents ( ) const
inline

Determines whether or not the dependent variables will be set to zero before executing the operation graph

Returns
true if the dependents will be zeroed

Definition at line 131 of file code_handler_impl.hpp.

Referenced by CppAD::cg::CodeHandler< ScalarOut >::makeVariables().

◆ makeVariable() [1/2]

◆ makeVariable() [2/2]

template<class Base >
void CppAD::cg::CodeHandler< Base >::makeVariable ( CGB variable)
inline

Marks the provided variable as being an independent variable.

Parameters
variablesthe variable that will become an independent variables.

Definition at line 90 of file code_handler_impl.hpp.

◆ makeVariables() [1/2]

◆ makeVariables() [2/2]

template<class Base >
void CppAD::cg::CodeHandler< Base >::makeVariables ( std::vector< AD< CGB > > &  variables)
inline

Marks the provided variables as being independent variables.

Parameters
variablesthe vector of variables that will become independent variables.

Definition at line 76 of file code_handler_impl.hpp.

◆ manageOperationNodeMemory()

template<class Base >
bool CppAD::cg::CodeHandler< Base >::manageOperationNodeMemory ( Node code)
inline

Adds an operation node to the list of nodes to be deleted when this handler is destroyed.

Parameters
codeThe operation node to be managed.
Returns
true if the node was successfully added to the list or false if it had already been previously added.

Definition at line 706 of file code_handler_impl.hpp.

Referenced by CppAD::cg::CodeHandler< ScalarOut >::makeVariables().

◆ markCodeBlockUsed()

template<class Base >
void CppAD::cg::CodeHandler< Base >::markCodeBlockUsed ( Node code)
protectedvirtual

Alias operations are always followed so that there is a correct usage count at the operation that it points to

Iterate over all arguments with operation nodes

this node was previously altered to ensure that the evaluation of the expression is only performed for the required iterations

node previously used in a different scope must make sure it is defined before being used in both scopes

does this variable require a condition based on indexes?

Must also update the scope of the arguments used by this operation

reset scope

Definition at line 738 of file code_handler_impl.hpp.

Referenced by CppAD::cg::CodeHandler< ScalarOut >::generateCode(), and CppAD::cg::CodeHandler< ScalarOut >::makeVariables().

◆ optimizeIfs()

template<class Base >
void CppAD::cg::CodeHandler< Base >::optimizeIfs ( )
inlineprotected

Attempt to reduce the number of ifs when there are consecutive ifs with the same condition

same condition -> combine the contents into a single if

Definition at line 1270 of file code_handler_impl.hpp.

Referenced by CppAD::cg::CodeHandler< ScalarOut >::generateCode(), and CppAD::cg::CodeHandler< ScalarOut >::makeVariables().

◆ reduceTemporaryVariables()

template<class Base >
void CppAD::cg::CodeHandler< Base >::reduceTemporaryVariables ( ArrayView< CGB > &  dependent)
inlineprotected

determine the last line where each temporary variable is used

Redefine temporary variable IDs

Definition at line 1636 of file code_handler_impl.hpp.

Referenced by CppAD::cg::CodeHandler< ScalarOut >::generateCode(), and CppAD::cg::CodeHandler< ScalarOut >::makeVariables().

◆ removeIndependent()

template<class Base >
void CppAD::cg::CodeHandler< Base >::removeIndependent ( Node indep)
inline

Finalizes the substitution of an independent variable by eliminating it from the list of independents. After this operation the variable substitution cannot be undone.

Parameters
indepThe independent variable
Exceptions
CGExceptionif the dependent variable is not an not an alias or it does not belong to this handler

Definition at line 76 of file graph_mod.hpp.

Referenced by CppAD::cg::CodeHandler< ScalarOut >::makeVariables().

◆ reorderOperation()

template<class Base >
void CppAD::cg::CodeHandler< Base >::reorderOperation ( Node node)
inlineprotected

determine the location of the last temporary variable

move dependent if beneficial

Must be in same scope

Definition at line 1742 of file code_handler_impl.hpp.

Referenced by CppAD::cg::CodeHandler< ScalarOut >::makeVariables(), and CppAD::cg::CodeHandler< ScalarOut >::reorderOperations().

◆ reorderOperations()

template<class Base >
void CppAD::cg::CodeHandler< Base >::reorderOperations ( ArrayView< CGB > &  dependent)
inlineprotected

Change operation order so that the total number of temporary variables is reduced.

Parameters
dependentThe vector of dependent variable values

Definition at line 1716 of file code_handler_impl.hpp.

Referenced by CppAD::cg::CodeHandler< ScalarOut >::makeVariables(), and CppAD::cg::CodeHandler< ScalarOut >::reduceTemporaryVariables().

◆ replaceWithConditionalTempVar()

template<class Base>
void CppAD::cg::CodeHandler< Base >::replaceWithConditionalTempVar ( Node tmp,
IndexOperationNode< Base > &  iterationIndexOp,
const std::vector< size_t > &  iterationRegions,
ScopeIDType  oldScope,
ScopeIDType  commonScopeColor 
)
inlineprotected

clone

Create condition

Change original variable

add the new scope

Must also update the scope of the arguments used by this operation

Definition at line 983 of file code_handler_impl.hpp.

Referenced by CppAD::cg::CodeHandler< ScalarOut >::makeVariables().

◆ reset()

template<class Base >
void CppAD::cg::CodeHandler< Base >::reset ( )
virtual

Resets this handler for a usage with completely different nodes.

Warning
all managed memory will be deleted

Definition at line 493 of file code_handler_impl.hpp.

Referenced by CppAD::cg::CodeHandler< ScalarOut >::makeVariables().

◆ resetNodes()

template<class Base >
void CppAD::cg::CodeHandler< Base >::resetNodes ( )
inline

Resets the previously used dependents and their children so that they can be reused again by this handler.

Definition at line 510 of file code_handler_impl.hpp.

Referenced by CppAD::cg::CodeHandler< ScalarOut >::makeVariables(), and CppAD::cg::ModelCSourceGen< Base >::prepareSparseReverseOneWithLoops().

◆ restoreTemporaryVar() [1/2]

template<class Base >
void CppAD::cg::CodeHandler< Base >::restoreTemporaryVar ( Node tmp)
inlineprotected

Must also update the scope of the arguments used by this operation

Definition at line 1151 of file code_handler_impl.hpp.

Referenced by CppAD::cg::CodeHandler< ScalarOut >::makeVariables(), and CppAD::cg::CodeHandler< ScalarOut >::updateTemporaryVarInDiffScopes().

◆ restoreTemporaryVar() [2/2]

template<class Base >
void CppAD::cg::CodeHandler< Base >::restoreTemporaryVar ( Node tmp,
Node opClone 
)
inlineprotected

Must also update the scope of the arguments used by this operation

Definition at line 1174 of file code_handler_impl.hpp.

◆ setReuseVariableIDs()

template<class Base >
void CppAD::cg::CodeHandler< Base >::setReuseVariableIDs ( bool  reuse)
inline

Defines whether or not to reuse the node IDs once they are not need by a node anymore.

Definition at line 66 of file code_handler_impl.hpp.

◆ setZeroDependents()

template<class Base >
void CppAD::cg::CodeHandler< Base >::setZeroDependents ( bool  zeroDependents)
inline

◆ solveFor() [1/2]

template<class Base >
CG< Base > CppAD::cg::CodeHandler< Base >::solveFor ( Node expression,
Node var 
)
inline

Solves an expression (e.g. f(x, y) == 0) for a given variable (e.g. x).

Parameters
expressionThe original expression (f(x, y))
varThe variable to solve for
Returns
The expression for the variable
Exceptions
CGExceptionif it is not possible to solve the expression

Definition at line 25 of file solver.hpp.

Referenced by CppAD::cg::CodeHandler< ScalarOut >::makeVariables().

◆ solveFor() [2/2]

template<class Base >
CG< Base > CppAD::cg::CodeHandler< Base >::solveFor ( const SourceCodePath &  path)
inlineprotected

Solves an expression (e.g. f(x, y) == 0) for a given variable (e.g. x) The variable can appear only once in the expression. This is also known as isolation.

Parameters
pathThe path from the equation residual to the variable
Returns
The expression for the variable
Exceptions
CGExceptionif it is not possible to solve the expression

Definition at line 83 of file solver.hpp.

◆ substituteIndependent()

template<class Base >
void CppAD::cg::CodeHandler< Base >::substituteIndependent ( const CGB indep,
const CGB dep,
bool  removeFromIndeps = true 
)
inline

Eliminates an independent variable by substitution using the provided dependent variable which is assumed to be a residual of an equation. If successful the model will contain one less independent variable.

Parameters
indepThe independent variable to eliminate.
depThe dependent variable representing a residual
removeFromIndepsWhether or not to immediately remove the independent variable from the list of independents in the model. The substitution operation can only be reversed if the variable is not removed.
Exceptions
CGExceptionif the dependent variable does not belong to this handler

Definition at line 22 of file graph_mod.hpp.

Referenced by CppAD::cg::DummyDerivatives< Base >::assignVar2Equation(), CppAD::cg::DummyDerivatives< Base >::generateSemiExplicitDAE(), CppAD::cg::CodeHandler< ScalarOut >::makeVariables(), and CppAD::cg::CodeHandler< ScalarOut >::substituteIndependent().

◆ undoSubstituteIndependent()

template<class Base >
void CppAD::cg::CodeHandler< Base >::undoSubstituteIndependent ( Node indep)
inline

Reverts a substitution of an independent variable that has not been removed from the list of independents yet. Warning: it does not recover any custom name assigned to the variable.

Parameters
indepThe independent variable
Exceptions
CGExceptionif the dependent variable does not belong to this handler

Definition at line 65 of file graph_mod.hpp.

Referenced by CppAD::cg::CodeHandler< ScalarOut >::makeVariables().

◆ updateTemporaryVarInDiffScopes()

template<class Base >
void CppAD::cg::CodeHandler< Base >::updateTemporaryVarInDiffScopes ( Node code)
inlineprotected

does this variable require a condition based on indexes?

Determine if it should be moved into a different scope so that it is defined before being used in both scopes

does this variable require a condition based on indexes?

determine the iterations which use this temporary variable

Must also update the scope of the arguments used by this operation

Definition at line 1068 of file code_handler_impl.hpp.

Referenced by CppAD::cg::CodeHandler< ScalarOut >::makeVariables().

Member Data Documentation

◆ _atomicFunctionName2Index

template<class Base>
std::map<std::string, size_t> CppAD::cg::CodeHandler< Base >::_atomicFunctionName2Index
protected

already used atomic function names (may contain names which were used by previous calls to this/other CondeHandlers)

Definition at line 124 of file code_handler.hpp.

Referenced by CppAD::cg::CodeHandler< ScalarOut >::generateCode().

◆ _atomicFunctions

template<class Base>
std::map<size_t, CGAbstractAtomicFun<Base>*> CppAD::cg::CodeHandler< Base >::_atomicFunctions
protected

◆ _atomicFunctionsMaxForward

template<class Base>
std::vector<int> CppAD::cg::CodeHandler< Base >::_atomicFunctionsMaxForward
protected

the maximum forward mode order each atomic function is called (-1 means forward mode not used)

Definition at line 134 of file code_handler.hpp.

Referenced by CppAD::cg::CodeHandler< ScalarOut >::generateCode(), and CppAD::cg::CodeHandler< ScalarOut >::getExternalFuncMaxForwardOrder().

◆ _atomicFunctionsMaxReverse

template<class Base>
std::vector<int> CppAD::cg::CodeHandler< Base >::_atomicFunctionsMaxReverse
protected

the maximum reverse mode order each atomic function is called (-1 means reverse mode not used)

Definition at line 139 of file code_handler.hpp.

Referenced by CppAD::cg::CodeHandler< ScalarOut >::generateCode(), and CppAD::cg::CodeHandler< ScalarOut >::getExternalFuncMaxReverseOrder().

◆ _atomicFunctionsOrder

template<class Base>
std::vector<std::string>* CppAD::cg::CodeHandler< Base >::_atomicFunctionsOrder
protected

the order of the atomic functions (may contain names which were used by previous calls to this/other CondeHandlers)

Definition at line 129 of file code_handler.hpp.

Referenced by CppAD::cg::CodeHandler< ScalarOut >::generateCode().

◆ _auxIndexI

template<class Base>
Node* CppAD::cg::CodeHandler< Base >::_auxIndexI
protected

Auxiliary index declaration (might not be used)

Definition at line 170 of file code_handler.hpp.

◆ _auxIterationIndexOp

template<class Base>
IndexOperationNode<Base>* CppAD::cg::CodeHandler< Base >::_auxIterationIndexOp
protected

Auxiliary index (might not be used)

Definition at line 174 of file code_handler.hpp.

Referenced by CppAD::cg::CodeHandler< ScalarOut >::generateCode().

◆ _codeBlocks

template<class Base>
std::vector<Node*> CppAD::cg::CodeHandler< Base >::_codeBlocks
protected

◆ _evaluationOrder

template<class Base>
CodeHandlerVector<Base, size_t> CppAD::cg::CodeHandler< Base >::_evaluationOrder
protected

evaluation order of each managed node (zero means that an evaluation position was never assigned)

Definition at line 77 of file code_handler.hpp.

Referenced by CppAD::cg::CodeHandler< ScalarOut >::generateCode(), CppAD::cg::CodeHandler< ScalarOut >::getTotalUsageCount(), and CppAD::cg::CodeHandler< ScalarOut >::resetNodes().

◆ _jobTimer

template<class Base>
JobTimer* CppAD::cg::CodeHandler< Base >::_jobTimer
protected

used to track evaluation times and print out messages

Definition at line 166 of file code_handler.hpp.

Referenced by CppAD::cg::CodeHandler< ScalarOut >::generateCode(), and CppAD::cg::CodeHandler< ScalarOut >::getVariablesIDs().

◆ _lastUsageOrder

template<class Base>
CodeHandlerVector<Base, size_t> CppAD::cg::CodeHandler< Base >::_lastUsageOrder
protected

the last index in the evaluation order for which an operation node is taken as an argument of another operation node. (zero means that the node was never used)

Definition at line 83 of file code_handler.hpp.

Referenced by CppAD::cg::CodeHandler< ScalarOut >::generateCode(), CppAD::cg::CodeHandler< ScalarOut >::getTotalUsageCount(), and CppAD::cg::CodeHandler< ScalarOut >::resetNodes().

◆ _lastVisit

template<class Base>
CodeHandlerVector<Base, size_t> CppAD::cg::CodeHandler< Base >::_lastVisit
protected

the ID of the last visit to each managed node

Definition at line 68 of file code_handler.hpp.

◆ _managedVectors

template<class Base>
std::set<CodeHandlerVectorSync<Base>*> CppAD::cg::CodeHandler< Base >::_managedVectors
protected

◆ _operationCount

template<class Base>
CodeHandlerVector<Base, size_t> CppAD::cg::CodeHandler< Base >::_operationCount
protected

The number of operations used in the expression that directly assign the value to a (temporary/dependent) variable. Operations used to compute other temporary variables are not considered.

Definition at line 93 of file code_handler.hpp.

Referenced by CppAD::cg::CodeHandler< ScalarOut >::generateCode(), CppAD::cg::CodeHandler< ScalarOut >::getTotalUsageCount(), and CppAD::cg::CodeHandler< ScalarOut >::resetNodes().

◆ _scope

◆ _scopedVariableOrder

template<class Base>
std::vector<std::vector<Node*> > CppAD::cg::CodeHandler< Base >::_scopedVariableOrder
protected

the order for the variable creation in the source code (each level represents a different variable scope)

Definition at line 111 of file code_handler.hpp.

Referenced by CppAD::cg::CodeHandler< ScalarOut >::generateCode(), CppAD::cg::CodeHandler< ScalarOut >::getTotalUsageCount(), and CppAD::cg::CodeHandler< ScalarOut >::optimizeIfs().

◆ _totalUseCount

template<class Base>
CodeHandlerVector<Base, size_t> CppAD::cg::CodeHandler< Base >::_totalUseCount
protected

the total number of times the result of an operation node is used

Definition at line 87 of file code_handler.hpp.

Referenced by CppAD::cg::CodeHandler< ScalarOut >::generateCode(), CppAD::cg::CodeHandler< ScalarOut >::getTotalUsageCount(), and CppAD::cg::CodeHandler< ScalarOut >::resetNodes().

◆ _variableDependencies

template<class Base>
std::vector<std::set<Node*> > CppAD::cg::CodeHandler< Base >::_variableDependencies
protected

maps dependencies between variables in _variableOrder

Definition at line 106 of file code_handler.hpp.

Referenced by CppAD::cg::CodeHandler< ScalarOut >::findVariableDependencies(), and CppAD::cg::CodeHandler< ScalarOut >::generateCode().

◆ _variableOrder

◆ _varId

◆ _zeroDependents

template<class Base>
bool CppAD::cg::CodeHandler< Base >::_zeroDependents
protected

whether or not the dependent variables should be zeroed before executing the operation graph

Definition at line 160 of file code_handler.hpp.

Referenced by CppAD::cg::CodeHandler< ScalarOut >::generateCode(), CppAD::cg::CodeHandler< ScalarOut >::isZeroDependents(), and CppAD::cg::CodeHandler< ScalarOut >::setZeroDependents().


The documentation for this class was generated from the following files: