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

Public Types

using Node = OperationNode< Base >
 
using Arg = Argument< Base >
 

Public Member Functions

 LanguageC (std::string varTypeName, size_t spaces=3)
 
const std::string & getArgumentIn () const
 
void setArgumentIn (const std::string &inArgName)
 
const std::string & getArgumentOut () const
 
void setArgumentOut (const std::string &outArgName)
 
const std::string & getArgumentAtomic () const
 
void setArgumentAtomic (const std::string &atomicArgName)
 
const std::string & getDependentAssignOperation () const
 
void setDependentAssignOperation (const std::string &depAssignOperation)
 
bool isIgnoreZeroDepAssign () const
 
void setIgnoreZeroDepAssign (bool ignore)
 
virtual void setGenerateFunction (const std::string &functionName)
 
virtual void setFunctionIndexArgument (const Node &funcArgIndex)
 
virtual void setFunctionIndexArguments (const std::vector< const Node *> &funcArgIndexes)
 
virtual const std::vector< const Node * > & getFunctionIndexArguments () const
 
virtual size_t getParameterPrecision () const
 
virtual void setParameterPrecision (size_t p)
 
virtual void setMaxAssignmentsPerFunction (size_t maxAssignmentsPerFunction, std::map< std::string, std::string > *sources)
 
size_t getMaxOperationsPerAssignment () const
 
void setMaxOperationsPerAssignment (size_t maxOperationsPerAssignment)
 
std::string generateTemporaryVariableDeclaration (bool isWrapperFunction, bool zeroArrayDependents, const std::vector< int > &atomicMaxForward, const std::vector< int > &atomicMaxReverse)
 
virtual std::string generateTemporaryVariableDeclaration (bool isWrapperFunction=false, bool zeroArrayDependents=false, int maxForwardOrder=-1, int maxReverseOrder=-1)
 
void generateArrayContainersDeclaration (std::ostringstream &ss, const std::vector< int > &atomicMaxForward, const std::vector< int > &atomicMaxReverse)
 
virtual void generateArrayContainersDeclaration (std::ostringstream &ss, int maxForwardOrder=-1, int maxReverseOrder=-1)
 
virtual std::string generateDependentVariableDeclaration ()
 
virtual std::string generateIndependentVariableDeclaration ()
 
std::string generateArgumentAtomicDcl () const
 
virtual std::string generateFunctionArgumentsDcl () const
 
virtual std::vector< std::string > generateFunctionArgumentsDcl2 () const
 
virtual std::string generateDefaultFunctionArgumentsDcl () const
 
virtual std::vector< std::string > generateDefaultFunctionArgumentsDcl2 () const
 
virtual std::string generateFunctionIndexArgumentsDcl () const
 
virtual std::vector< std::string > generateFunctionIndexArgumentsDcl2 () const
 
virtual std::string generateDefaultFunctionArguments () const
 
virtual std::string generateFunctionIndexArguments () const
 
void createIndexDeclaration ()
 

Static Public Member Functions

static void printFunctionDeclaration (std::ostringstream &out, const std::string &returnType, const std::string &functionName, const std::vector< std::string > &arguments, const std::vector< std::string > &arguments2={})
 
static void printIndexCondExpr (std::ostringstream &out, const std::vector< size_t > &info, const std::string &index)
 
static void printStaticIndexArray (std::ostringstream &os, const std::string &name, const std::vector< size_t > &values)
 
static void printStaticIndexMatrix (std::ostringstream &os, const std::string &name, const std::map< size_t, std::map< size_t, size_t > > &values)
 
static void generateNames4RandomIndexPatterns (const std::set< RandomIndexPattern *> &randomPatterns)
 
static void printRandomIndexPatternDeclaration (std::ostringstream &os, const std::string &identation, const std::set< RandomIndexPattern *> &randomPatterns)
 
static std::string indexPattern2String (const IndexPattern &ip, const Node &index)
 
static std::string indexPattern2String (const IndexPattern &ip, const std::string &index)
 
static std::string indexPattern2String (const IndexPattern &ip, const std::vector< const Node *> &indexes)
 
static std::string indexPattern2String (const IndexPattern &ip, const std::vector< const std::string *> &indexes)
 
static std::string linearIndexPattern2String (const LinearIndexPattern &lip, const Node &index)
 
static std::string linearIndexPattern2String (const LinearIndexPattern &lip, const std::string &index)
 
static bool isOffsetBy (const IndexPattern *ip, const IndexPattern *refIp, long offset)
 
static bool isOffsetBy (const LinearIndexPattern *lIp, const LinearIndexPattern *refLIp, long offset)
 
static bool isOffsetBy (const LinearIndexPattern &lIp, const LinearIndexPattern &refLIp, long offset)
 
static bool isOffsetBy (const SectionedIndexPattern *sIp, const SectionedIndexPattern *refSecp, long offset)
 
static bool isOffsetBy (const SectionedIndexPattern &lIp, const SectionedIndexPattern &refSecp, long offset)
 
static Plane2DIndexPatternencapsulateIndexPattern (const LinearIndexPattern &refLIp, size_t starti)
 
static Plane2DIndexPatternencapsulateIndexPattern (const SectionedIndexPattern &refSecp, size_t starti)
 

Static Public Attributes

static const std::string U_INDEX_TYPE = "unsigned long"
 
static const std::string ATOMICFUN_STRUCT_DEFINITION
 

Protected Member Functions

void generateSourceCode (std::ostream &out, std::unique_ptr< LanguageGenerationData< Base > > info) override
 
size_t getVariableID (const Node &node) const
 
unsigned printAssignment (Node &node)
 
unsigned pushAssignment (Node &nodeName, const Arg &nodeRhs)
 
unsigned pushAssignment (Node &nodeName, Node &nodeRhs)
 
virtual void pushAssignmentStart (Node &op)
 
virtual void pushAssignmentStart (Node &node, const std::string &varName, bool isDep)
 
virtual void pushAssignmentEnd (Node &op)
 
virtual std::string argumentDeclaration (const FuncArgument &funcArg) const
 
virtual void saveLocalFunction (std::vector< std::string > &localFuncNames, bool zeroDependentArray)
 
bool createsNewVariable (const Node &var, size_t totalUseCount, size_t opCount) const override
 
virtual bool requiresVariableName (const Node &var) const
 
virtual bool directlyAssignsVariable (const Node &var) const
 
bool requiresVariableArgument (enum CGOpCode op, size_t argIndex) const override
 
const std::string & createVariableName (Node &var)
 
bool requiresVariableDependencies () const override
 
virtual void pushIndependentVariableName (Node &op)
 
virtual unsigned push (const Arg &arg)
 
virtual unsigned pushExpression (Node &op)
 
virtual unsigned pushExpressionNoVarCheck2 (Node &node)
 
virtual unsigned pushExpressionNoVarCheck (Node &node)
 
virtual unsigned pushAssignOp (Node &node)
 
virtual void pushUnaryFunction (Node &op)
 
virtual void pushPowFunction (Node &op)
 
virtual void pushSignFunction (Node &op)
 
virtual unsigned pushOperationAlias (Node &op)
 
virtual void pushOperationAdd (Node &op)
 
virtual void pushOperationMinus (Node &op)
 
bool encloseInParenthesesDiv (const Node *node) const
 
virtual void pushOperationDiv (Node &op)
 
bool encloseInParenthesesMul (const Node *node) const
 
virtual void pushOperationMul (Node &op)
 
virtual void pushOperationUnaryMinus (Node &op)
 
virtual void pushPrintOperation (const Node &node)
 
virtual void pushConditionalAssignment (Node &node)
 
bool isSameArgument (const Arg &newArg, const Arg *oldArg)
 
virtual void pushArrayCreationOp (Node &op)
 
virtual void pushSparseArrayCreationOp (Node &op)
 
void printArrayStructInit (const std::string &dataArrayName, size_t pos, const std::vector< Node *> &arrays, size_t k)
 
void printArrayStructInit (const std::string &dataArrayName, Node &array)
 
void markArrayChanged (Node &ty)
 
size_t printArrayCreationUsingLoop (size_t startPos, Node &array, size_t startj, std::vector< const Arg *> &tmpArrayValues)
 
std::string getTempArrayName (const Node &op)
 
virtual void pushArrayElementOp (Node &op)
 
virtual void pushAtomicForwardOp (Node &atomicFor)
 
virtual void pushAtomicReverseOp (Node &atomicRev)
 
virtual unsigned pushDependentMultiAssign (Node &node)
 
virtual void pushLoopStart (Node &node)
 
virtual void pushLoopEnd (Node &node)
 
virtual size_t printLoopIndexDeps (const std::vector< Node *> &variableOrder, size_t pos)
 
virtual size_t printLoopIndexedDepsUsingLoop (const std::vector< Node *> &variableOrder, size_t starti)
 
virtual void pushLoopIndexedDep (Node &node)
 
virtual void pushLoopIndexedIndep (Node &node)
 
virtual void pushLoopIndexedTmp (Node &node)
 
virtual void pushTmpVar (Node &node)
 
virtual void pushIndexAssign (Node &node)
 
virtual void pushIndexCondExprOp (Node &node)
 
virtual void pushStartIf (Node &node)
 
virtual void pushElseIf (Node &node)
 
virtual void pushElse (Node &node)
 
virtual void pushEndIf (Node &node)
 
virtual void pushCondResult (Node &node)
 
virtual void pushUserCustom (Node &node)
 
bool isDependent (const Node &arg) const
 
virtual void printParameter (const Base &value)
 
virtual void pushParameter (const Base &value)
 
template<class Output >
void writeParameter (const Base &value, Output &output)
 
virtual const std::string & getComparison (enum CGOpCode op) const
 
const std::string & getPrintfBaseFormat ()
 
template<>
const std::string & getPrintfBaseFormat ()
 
template<>
const std::string & getPrintfBaseFormat ()
 

Static Protected Member Functions

static bool isFunction (enum CGOpCode op)
 
static bool isUnaryFunction (enum CGOpCode op)
 
static bool isCondAssign (enum CGOpCode op)
 

Protected Attributes

const std::string _baseTypeName
 
const std::string _spaces
 
std::unique_ptr< LanguageGenerationData< Base > > _info
 
std::string _indentation
 
std::string _inArgName
 
std::string _outArgName
 
std::string _atomicArgName
 
std::ostringstream _code
 
VariableNameGenerator< Base > * _nameGen
 
std::ostringstream _ss
 
LangStreamStack< Base > _streamStack
 
size_t _independentSize
 
size_t _minTemporaryVarID
 
std::map< size_t, size_t > _dependentIDs
 
const ArrayView< CG< Base > > * _dependent
 
std::map< size_t, Node * > _temporary
 
std::string _depAssignOperation
 
bool _ignoreZeroDepAssign
 
std::string _functionName
 
size_t _maxAssignmentsPerFunction
 
size_t _maxOperationsPerAssignment
 
std::map< std::string, std::string > * _sources
 
std::vector< const Arg * > _tmpArrayValues
 
std::vector< const Arg * > _tmpSparseArrayValues
 
std::map< std::string, AtomicFuncArray > _atomicFuncArrays
 
std::vector< const Node * > _funcArgIndexes
 
std::vector< const LoopStartOperationNode< Base > * > _currentLoops
 
size_t _parameterPrecision
 

Static Protected Attributes

static const std::string _C_COMP_OP_LT = "<"
 
static const std::string _C_COMP_OP_LE = "<="
 
static const std::string _C_COMP_OP_EQ = "=="
 
static const std::string _C_COMP_OP_GE = ">="
 
static const std::string _C_COMP_OP_GT = ">"
 
static const std::string _C_COMP_OP_NE = "!="
 
static const std::string _C_STATIC_INDEX_ARRAY = "index"
 
static const std::string _C_SPARSE_INDEX_ARRAY = "idx"
 
static const std::string _ATOMIC_TX = "atx"
 
static const std::string _ATOMIC_TY = "aty"
 
static const std::string _ATOMIC_PX = "apx"
 
static const std::string _ATOMIC_PY = "apy"
 

Detailed Description

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

Generates code for the C language

Author
Joao Leal

Definition at line 126 of file declare_cg.hpp.

Constructor & Destructor Documentation

◆ LanguageC()

template<class Base>
CppAD::cg::LanguageC< Base >::LanguageC ( std::string  varTypeName,
size_t  spaces = 3 
)
inlineexplicit

Creates a C language source code generator

Parameters
varTypeNamevariable data type (e.g. double)
spacesnumber of spaces for indentations

Definition at line 127 of file language_c.hpp.

Member Function Documentation

◆ createsNewVariable()

template<class Base>
bool CppAD::cg::LanguageC< Base >::createsNewVariable ( const Node op,
size_t  totalUseCount,
size_t  opCount 
) const
inlineoverrideprotectedvirtual

Whether or not a new variable is created as a result of this operation

Parameters
opOperation
totalUseCountthe number of times this node has been used as an argument in other operations
Returns
true if a new variable is created

Implements CppAD::cg::Language< Base >.

Definition at line 1091 of file language_c.hpp.

◆ directlyAssignsVariable()

template<class Base>
virtual bool CppAD::cg::LanguageC< Base >::directlyAssignsVariable ( const Node var) const
inlineprotectedvirtual

Whether or not this operation assign its expression to a variable by itself.

Parameters
varthe operation node
Returns

Definition at line 1146 of file language_c.hpp.

◆ generateSourceCode()

template<class Base>
void CppAD::cg::LanguageC< Base >::generateSourceCode ( std::ostream &  out,
std::unique_ptr< LanguageGenerationData< Base > >  info 
)
inlineoverrideprotectedvirtual

generate index array names (might be used for variable names)

generate variable names

function variable declaration

Determine the dependent variables that result from the same operations

non-constant variables

Source code generation magic!

Create the wrapper function which calls the other functions

encapsulate the code in a function

Implements CppAD::cg::Language< Base >.

Definition at line 663 of file language_c.hpp.

◆ generateTemporaryVariableDeclaration()

template<class Base>
virtual std::string CppAD::cg::LanguageC< Base >::generateTemporaryVariableDeclaration ( bool  isWrapperFunction = false,
bool  zeroArrayDependents = false,
int  maxForwardOrder = -1,
int  maxReverseOrder = -1 
)
inlinevirtual

Declares temporary variables used by a function.

Parameters
isWrapperFunctiontrue if the declarations are for a wrapper function which calls other functions where the actual work is performed
zeroArrayDependentswhether or not the dependent variables should be set to zero before executing the operation graph
maxForwardOrderthe maximum order of forward mode calls to atomic functions
maxReverseOrderthe maximum order of reverse mode calls to atomic functions
Returns
the string with the declarations for the temporary variables

temporary variables

temporary array variables

temporary sparse array variables

Definition at line 308 of file language_c.hpp.

◆ getMaxOperationsPerAssignment()

template<class Base>
size_t CppAD::cg::LanguageC< Base >::getMaxOperationsPerAssignment ( ) const
inline

The maximum number of operations per variable assignment.

Returns
The maximum number of operations per variable assignment

Definition at line 263 of file language_c.hpp.

◆ getParameterPrecision()

template<class Base>
virtual size_t CppAD::cg::LanguageC< Base >::getParameterPrecision ( ) const
inlinevirtual

Provides the maximum precision used to print constant values in the generated source code

Returns
the maximum number of digits

Definition at line 227 of file language_c.hpp.

◆ isIgnoreZeroDepAssign()

template<class Base>
bool CppAD::cg::LanguageC< Base >::isIgnoreZeroDepAssign ( ) const
inline

Whether or not source code to set dependent variables to zero will be generated. It may be used not to set the dependent variables to zero when it is known they are already set to zero before the source code generation.

Returns
true if source code to explicitly set dependent variables to zero will NOT be created.

Definition at line 189 of file language_c.hpp.

◆ printArrayCreationUsingLoop()

template<class Base >
size_t CppAD::cg::LanguageC< Base >::printArrayCreationUsingLoop ( size_t  startPos,
Node array,
size_t  startj,
std::vector< const Arg *> &  tmpArrayValues 
)
inlineprotected

from independents array

from independents array in a loop

from temporary variable array

constant value?

print the loop

update values in the global temporary array

Definition at line 125 of file language_c_arrays.hpp.

◆ printFunctionDeclaration()

template<class Base>
static void CppAD::cg::LanguageC< Base >::printFunctionDeclaration ( std::ostringstream &  out,
const std::string &  returnType,
const std::string &  functionName,
const std::vector< std::string > &  arguments,
const std::vector< std::string > &  arguments2 = {} 
)
inlinestatic

Prints a function declaration where each argument is in a different line.

Parameters
outthe stream where the declaration is printed
returnTypethe function return type
functionNamethe function name
argumentsfunction arguments
arguments2additional function arguments

Definition at line 538 of file language_c.hpp.

Referenced by CppAD::cg::ModelCSourceGen< Base >::generateGlobalDirectionalFunctionSource(), CppAD::cg::ModelCSourceGen< Base >::generateSparseForwardOneSourcesNoAtomics(), CppAD::cg::ModelCSourceGen< Base >::generateSparseJacobianForRevMultiThreadSource(), CppAD::cg::ModelCSourceGen< Base >::generateSparseReverseOneSourcesNoAtomics(), and CppAD::cg::ModelCSourceGen< Base >::generateSparseReverseTwoSourcesNoAtomics().

◆ printLoopIndexedDepsUsingLoop()

template<class Base >
size_t CppAD::cg::LanguageC< Base >::printLoopIndexedDepsUsingLoop ( const std::vector< Node *> &  variableOrder,
size_t  starti 
)
inlineprotectedvirtual

Check that the assigned value is from an array

check that the type of index pattern can be used within a loop

Find last compatible variable in variableOrder

Create the dependent variable name with the new index pattern

print the loop

Definition at line 58 of file language_c_loops.hpp.

◆ printRandomIndexPatternDeclaration()

template<class Base >
void CppAD::cg::LanguageC< Base >::printRandomIndexPatternDeclaration ( std::ostringstream &  os,
const std::string &  identation,
const std::set< RandomIndexPattern *> &  randomPatterns 
)
inlinestatic

◆ pushAtomicForwardOp()

template<class Base>
virtual void CppAD::cg::LanguageC< Base >::pushAtomicForwardOp ( Node atomicFor)
inlineprotectedvirtual

the values of ty are now changed

Definition at line 1791 of file language_c.hpp.

◆ pushAtomicReverseOp()

template<class Base>
virtual void CppAD::cg::LanguageC< Base >::pushAtomicReverseOp ( Node atomicRev)
inlineprotectedvirtual

the values of px are now changed

Definition at line 1833 of file language_c.hpp.

◆ pushElse()

template<class Base>
virtual void CppAD::cg::LanguageC< Base >::pushElse ( Node node)
inlineprotectedvirtual

the first argument is the if start node, the following arguments are assignments in the previous if branch

Definition at line 2035 of file language_c.hpp.

◆ pushElseIf()

template<class Base>
virtual void CppAD::cg::LanguageC< Base >::pushElseIf ( Node node)
inlineprotectedvirtual

the first argument is the condition, the second argument is the if start node, the following arguments are assignments in the previous if branch

Definition at line 2015 of file language_c.hpp.

◆ pushStartIf()

template<class Base>
virtual void CppAD::cg::LanguageC< Base >::pushStartIf ( Node node)
inlineprotectedvirtual

the first argument is the condition, following arguments are just extra dependencies that must be defined outside the if

Definition at line 1999 of file language_c.hpp.

◆ requiresVariableDependencies()

template<class Base>
bool CppAD::cg::LanguageC< Base >::requiresVariableDependencies ( ) const
inlineoverrideprotectedvirtual

Whether or not this language can use information regarding the dependencies between different equations/variables.

Implements CppAD::cg::Language< Base >.

Definition at line 1231 of file language_c.hpp.

◆ setIgnoreZeroDepAssign()

template<class Base>
void CppAD::cg::LanguageC< Base >::setIgnoreZeroDepAssign ( bool  ignore)
inline

Whether or not to generate expressions to set dependent variables to zero. It may be used not to set the dependent variables to zero when it is known they are already set to zero before the source code generation.

Parameters
ignoretrue if source code to explicitly set dependent variables to zero will NOT be created.

Definition at line 200 of file language_c.hpp.

◆ setMaxAssignmentsPerFunction()

template<class Base>
virtual void CppAD::cg::LanguageC< Base >::setMaxAssignmentsPerFunction ( size_t  maxAssignmentsPerFunction,
std::map< std::string, std::string > *  sources 
)
inlinevirtual

Defines the maximum number of assignment per generated function. Zero means it is disabled (no limit). By setting a limit, it is possible to reduce the compiler workload by having multiple file/function instead of a very large one. Note that it is not possible to split some function (e.g., containing loops) and, therefore, this limit can be violated.

Parameters
maxAssignmentsPerFunctionthe maximum number of assignments per file/function
sourcesA map where the file names are associated with their contents.

Definition at line 252 of file language_c.hpp.

Referenced by CppAD::cg::ModelCSourceGen< Base >::generateSparseForwardOneSourcesNoAtomics(), CppAD::cg::ModelCSourceGen< Base >::generateSparseForwardOneSourcesWithAtomics(), CppAD::cg::ModelCSourceGen< Base >::generateSparseHessianSourceDirectly(), CppAD::cg::ModelCSourceGen< Base >::generateSparseReverseOneSourcesNoAtomics(), CppAD::cg::ModelCSourceGen< Base >::generateSparseReverseOneSourcesWithAtomics(), CppAD::cg::ModelCSourceGen< Base >::generateSparseReverseTwoSources(), CppAD::cg::ModelCSourceGen< Base >::generateSparseReverseTwoSourcesNoAtomics(), CppAD::cg::ModelCSourceGen< Base >::generateZeroSource(), and CppAD::cg::ModelCSourceGen< Base >::prepareSparseReverseOneWithLoops().

◆ setMaxOperationsPerAssignment()

template<class Base>
void CppAD::cg::LanguageC< Base >::setMaxOperationsPerAssignment ( size_t  maxOperationsPerAssignment)
inline

◆ setParameterPrecision()

Member Data Documentation

◆ ATOMICFUN_STRUCT_DEFINITION

template<class Base>
const std::string CppAD::cg::LanguageC< Base >::ATOMICFUN_STRUCT_DEFINITION
static
Initial value:
=
"typedef struct Array {\n"
" void* data;\n"
" " + U_INDEX_TYPE + " size;\n"
" int sparse;\n"
" const " + U_INDEX_TYPE + "* idx;\n"
" " + U_INDEX_TYPE + " nnz;\n"
"} Array;\n"
"\n"
"struct LangCAtomicFun {\n"
" void* libModel;\n"
" int (*forward)(void* libModel,\n"
" int atomicIndex,\n"
" int q,\n"
" int p,\n"
" const Array tx[],\n"
" Array* ty);\n"
" int (*reverse)(void* libModel,\n"
" int atomicIndex,\n"
" int p,\n"
" const Array tx[],\n"
" Array* px,\n"
" const Array py[]);\n"
"};"

Definition at line 40 of file language_c.hpp.


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