TMB Documentation
v1.9.11
|
Struct defining the main AD context. More...
#include <global.hpp>
Classes | |
struct | ad_aug |
Augmented AD type. More... | |
struct | ad_segment |
Contiguous set of variables on the current tape. More... | |
struct | AddDependencies |
Add default implementation of mandatory member: dependencies More... | |
struct | AddForwardFromEval |
Add default implementation of mandatory member: forward from optional member eval More... | |
struct | AddForwardFromEval< OperatorBase, 1 > |
Unary case. More... | |
struct | AddForwardFromEval< OperatorBase, 2 > |
Binary case. More... | |
struct | AddForwardIncrReverseDecr |
Add default implementation of mandatory members: forward_incr and reverse_decr from forward and reverse More... | |
struct | AddForwardMarkReverseMark |
Add default implementation of mandatory members: forward_mark and reverse_mark More... | |
struct | AddForwardReverse |
Add default implementation of mandatory members: forward and reverse from forward_incr and reverse_decr More... | |
struct | AddIncrementDecrement |
Add default implementation of mandatory members: increment and decrement More... | |
struct | AddInputSizeOutputSize |
Add default implementation of mandatory members: input_size ans output_size More... | |
struct | Complete |
Operator auto-completion. More... | |
struct | CPL |
Generate all mandatory members. More... | |
struct | DynamicOperator |
Operator that requires dynamic allocation. Compile time known input/output size. More... | |
struct | DynamicOutputOperator |
Operator that requires dynamic allocation. Compile time known input size. More... | |
struct | Fused |
Fuse two operators. More... | |
struct | hash_config |
Configuration of hash_sweep. More... | |
struct | if_else |
Utility for member completion. More... | |
struct | no_filter |
Substitute of std::vector<bool> with all elements true More... | |
struct | NullOp |
Empty operator without inputs or outputs. More... | |
struct | NullOp2 |
Empty operator with inputs and outputs. More... | |
struct | operation_stack |
Operation stack. More... | |
struct | Operator |
Operator with input/output dimension known at compile time. More... | |
struct | OperatorPure |
The abstract operator for the operation stack global::opstack More... | |
struct | print_config |
Configuration of print method. More... | |
struct | ReferenceCounter |
Reference counting. More... | |
struct | RefOp |
Reference a variable on another tape. More... | |
struct | Rep |
Replicate an operator. More... | |
struct | RepCompress |
Replicate an operator and apply input compression. More... | |
struct | ZeroOp |
Add zero allocated workspace to the tape. More... | |
Public Member Functions | |
void | ad_start () |
Enable ad calulations to be piped to this glob. More... | |
void | ad_stop () |
Stop ad calculations from being piped to this glob. | |
void | add_to_opstack (OperatorPure *pOp) |
Add OperatorPure to stack and trigger operator fusion if enabled. | |
template<class OperatorBase > | |
ad_plain | add_to_stack (Scalar result=0) |
Add nullary operator to the stack based on its result | |
template<class OperatorBase > | |
ad_plain | add_to_stack (const ad_plain &x) |
Add unary operator to the stack based on its argument | |
template<class OperatorBase > | |
ad_plain | add_to_stack (const ad_plain &x, const ad_plain &y) |
Add binary operator to the stack based on its two arguments | |
template<class OperatorBase > | |
std::vector< ad_plain > | add_to_stack (OperatorPure *pOp, const std::vector< ad_plain > &x) |
Add vector operator to the stack based on its vector argument | |
Position | begin () |
The three pointers defining the begining of the tape. | |
graph | build_graph (bool transpose, const std::vector< bool > &keep_var) |
Build a graph-representation of the operation stack. More... | |
void | clear () |
Clear all workspace without actually freeing the workspace. More... | |
template<class Vector > | |
void | clear_array_subgraph (Vector &array, typename Vector::value_type value=typename Vector::value_type(0)) const |
Generic clear array along global::subgraph. More... | |
void | clear_deriv (Position start=Position(0, 0, 0)) |
Set derivatives to zero. More... | |
void | clear_deriv_sub () |
Clear derivative array along a subgraph. More... | |
std::vector< bool > | dep_marks () |
Boolean representation of dependent variable positions. | |
Scalar & | deriv_dep (Index i) |
Reference to i'th 'range direction' used to seed the derivative. | |
Scalar & | deriv_inv (Index i) |
Reference to i'th component of the gradient. | |
void | eliminate () |
Very simple tape optimizer. More... | |
Position | end () |
The three pointers defining the end of the tape. | |
global | extract_sub (std::vector< Index > &var_remap, global new_glob=global()) |
Extract a subgraph as a new global object. Fast when called many times. More... | |
global | extract_sub () |
Extract a subgraph as a new global object. Slow when called many times. More... | |
void | extract_sub_inplace (std::vector< bool > marks) |
In-place subgraph extractor. More... | |
void | forward (Position start=Position(0, 0, 0)) |
Full or partial forward sweep through the operation stack. Updates global::values . More... | |
void | forward (std::vector< bool > &marks) |
Full forward dependency sweep through the operation stack. | |
void | forward_dense (std::vector< bool > &marks) |
Full forward dependency sweep through the operation stack. More... | |
graph | forward_graph (std::vector< bool > keep_var=std::vector< bool >(0)) |
Construct operator graph with forward connections. More... | |
template<class ForwardArgs , class NodeFilter > | |
void | forward_loop (ForwardArgs &args, size_t begin, const NodeFilter &node_filter) const |
Generic forward sweep. More... | |
template<class ForwardArgs > | |
void | forward_loop (ForwardArgs &args, size_t begin=0) const |
Generic forward sweep. More... | |
template<class ForwardArgs > | |
void | forward_loop_subgraph (ForwardArgs &args) const |
Generic forward sweep along global::subgraph_seq. | |
void | forward_replay (bool inv_tags=true, bool dep_tags=true) |
Replay this operation sequence to itself. More... | |
void | forward_sub () |
Forward sweep along a subgraph. | |
void | forward_sub (std::vector< bool > &marks, const std::vector< bool > &node_filter=std::vector< bool >()) |
Forward sweep along a subgraph. Subgraph can be specified through a sequence subgraph_seq (default) or through a boolean node_filter . | |
template<class T > | |
void | hash (hash_t &h, T x) const |
Simple hash code of scalar. | |
hash_t | hash () const |
Simple hash code of tape. More... | |
std::vector< hash_t > | hash_sweep (hash_config cfg) const |
Calculate hash codes of each dependent variable using a single forward sweep. More... | |
std::vector< hash_t > | hash_sweep (bool weak=true) const |
Handle two common cases. | |
bool | identical (const global &other) const |
Test if two tapes are identical. More... | |
std::vector< bool > | inv_marks () |
Boolean representation of independent variable positions. | |
std::vector< bool > | mark_space (size_t n, const std::vector< Index > ind) |
General boolean table generator. | |
void | mark_subgraph (std::vector< bool > &marks) |
Mark all outputs of a subgraph. | |
std::vector< Index > | op2idx (const std::vector< Index > &var_subset, Index NA=(Index) -1) |
General operator -> variable table generator. More... | |
std::vector< Index > | op2var (const std::vector< Index > &seq) |
Get variables produces by a node seqence. | |
std::vector< bool > | op2var (const std::vector< bool > &seq_mark) |
Get variables produced by a node seqence. | |
void | print (print_config cfg) |
Print workspace. | |
void | print () |
Print workspace with default configuration (works from debugger) | |
void | reverse (Position start=Position(0, 0, 0)) |
Full or partial reverse sweep through the operation stack. Updates global::derivs . More... | |
void | reverse (std::vector< bool > &marks) |
Full reverse dependency sweep through the operation stack. | |
graph | reverse_graph (std::vector< bool > keep_var=std::vector< bool >(0)) |
Construct operator graph with reverse connections. More... | |
template<class ReverseArgs , class NodeFilter > | |
void | reverse_loop (ReverseArgs &args, size_t begin, const NodeFilter &node_filter) const |
Generic reverse sweep. More... | |
template<class ReverseArgs > | |
void | reverse_loop (ReverseArgs &args, size_t begin=0) const |
Generic reverse sweep. More... | |
template<class ReverseArgs > | |
void | reverse_loop_subgraph (ReverseArgs &args) const |
Generic reverse sweep along global::subgraph_seq. | |
void | reverse_sub () |
Reverse sweep along a subgraph. | |
void | reverse_sub (std::vector< bool > &marks, const std::vector< bool > &node_filter=std::vector< bool >()) |
Reverse sweep along a subgraph. Subgraph can be specified through a sequence subgraph_seq (default) or through a boolean node_filter . | |
void | set_fuse (bool flag) |
Enable/disable operator fusion. More... | |
void | set_subgraph (const std::vector< bool > &marks, bool append=false) |
Convert selected variables to a subgraph sequence. More... | |
void | shrink_to_fit (double tol=.9) |
Release unnecessary workspace to the system. More... | |
void | subgraph_cache_ptr () const |
Cache array pointers required by all subgraph routines. More... | |
std::vector< bool > | subgraph_marks () |
Boolean representation of sub graph positions. | |
void | subgraph_trivial () |
Create a full subgraph (for testing only) | |
void | unmark_subgraph (std::vector< bool > &marks) |
Unmark all outputs of a subgraph. | |
Scalar & | value_dep (Index i) |
Reference to i'th component of the function value. | |
Scalar & | value_inv (Index i) |
Reference to i'th input value (parameter) | |
std::vector< Index > | var2op () |
Build variable -> operator node lookup table using a single forward pass. The resulting sequence is monotonically increasing. More... | |
std::vector< bool > | var2op (const std::vector< bool > &values) |
Select operators from a subset of variables An operator is selected if it generates any of the selected variables. This is the fast equivalent of ans[ var2op() [ which(values) ] ] <- true | |
Public Attributes | |
std::vector< Index > | dep_index |
Pointers into global::values determining dependent variables. | |
std::vector< Scalar > | derivs |
Contiguous workspace for derivatives (same length as global::values ) | |
void(* | forward_compiled )(Scalar *) |
Optional pointer to compiled code. More... | |
bool | in_use |
Is this glob present in the context stack? | |
IndexVector | inputs |
Pointers into global::values determining operator inputs. | |
std::vector< Index > | inv_index |
Pointers into global::values determining independent variables. | |
operation_stack | opstack |
Operation stack. | |
global * | parent_glob |
Previous ad context to be restored then this context ends. More... | |
void(* | reverse_compiled )(Scalar *, Scalar *) |
Optional pointer to compiled code. More... | |
std::vector< Scalar > | values |
Contiguous workspace for taped variables (same length as global::derivs ) | |
Struct defining the main AD context.
opstack
, inputs
and values
.ad_start()
or inactivated using ad_stop()
.get_glob()
gives a pointer to the current active AD context.get_glob()
(top) to NULL
(bottom). Definition at line 797 of file global.hpp.
void TMBad::global::ad_start | ( | ) |
Enable ad calulations to be piped to this glob.
Definition at line 2065 of file TMBad.cpp.
Referenced by TMBad::accumulation_tree_split(), TMBad::ADFun<>::ADFun(), and TMBad::ADFun<>::decompose().
graph TMBad::global::build_graph | ( | bool | transpose, |
const std::vector< bool > & | keep_var | ||
) |
Build a graph-representation of the operation stack.
The graph is defined as follows:
opstack.size()
transpose | Flag to reverse all edges. |
keep_var | Boolean mask of variables to be considered as potential edges. |
void TMBad::global::clear | ( | ) |
|
inline |
Generic clear array along global::subgraph.
Applicable for
subgraph_cache_ptr()
. It follows that complexity is proportional to the full graph for the first call. Following calls have complexity proportional to the subgraph. Definition at line 1076 of file global.hpp.
void TMBad::global::clear_deriv | ( | Position | start = Position(0, 0, 0) | ) |
Set derivatives to zero.
start | Specify 'tail sweep' starting from this position |
Definition at line 984 of file TMBad.cpp.
Referenced by TMBad::accumulation_tree_split(), and TMBad::ADFun<>::Jacobian().
void TMBad::global::clear_deriv_sub | ( | ) |
Clear derivative array along a subgraph.
Clear all relevant input variable derivatives by noting that each such input variable must also be an output of some operator node.
Definition at line 1269 of file TMBad.cpp.
Referenced by TMBad::ADFun<>::Jacobian().
void TMBad::global::eliminate | ( | ) |
Very simple tape optimizer.
The tape optimizer attempts to remove variables unless they are
global::extract_sub
). Formally this is correct but in practice it could result in nan function evaluations (Example: Vectorized log function with negative values of x[0]
). Definition at line 1747 of file TMBad.cpp.
Referenced by TMBad::accumulation_tree_split(), TMBad::ADFun<>::decompose(), and TMBad::ADFun<>::eliminate().
Extract a subgraph as a new global object. Fast when called many times.
Sub-graph can either be sorted (common case) or just topologically sorted* (e.g. for graph permutations). In both cases the subgraph may or may not include all independent/dependent variables.
It's important to note that this routine permutes inv_index
and dep_index
to reflect the original parameter order (which is only relevant in the un-sorted case). FIXME: Correct the code!
var_remap | Workspace containing garbage on output. Can be used to avoid re-allocating. |
new_glob | Target of extraction. Empty by default. |
global::subgraph_seq
must be calculated in advance. new_glob
may be useful for function slicing. var_remap
) prior to calling this function. Note that by default boundary variables are remapped to an arbitrary variable (the first variable values[0]
). Definition at line 1271 of file TMBad.cpp.
Referenced by TMBad::autopar::extract(), TMBad::reorder_depth_first(), and TMBad::reorder_temporaries().
global TMBad::global::extract_sub | ( | ) |
void TMBad::global::extract_sub_inplace | ( | std::vector< bool > | marks | ) |
void TMBad::global::forward | ( | Position | start = Position(0, 0, 0) | ) |
Full or partial forward sweep through the operation stack. Updates global::values
.
start | Specify 'tail sweep' starting from this position |
Definition at line 1005 of file TMBad.cpp.
Referenced by TMBad::accumulation_tree_split(), TMBad::ADFun<>::activeRange(), TMBad::ADFun<>::Jacobian(), TMBad::ADFun<>::operator()(), and TMBad::sequential_reduction::sequential_reduction().
void TMBad::global::forward_dense | ( | std::vector< bool > & | marks | ) |
Full forward dependency sweep through the operation stack.
For each operator mark all outputs if any inputs are marked. The intended use of this function is to determine a valid (complete topoligically sorted) subgraph.
graph TMBad::global::forward_graph | ( | std::vector< bool > | keep_var = std::vector<bool>(0) | ) |
Construct operator graph with forward connections.
See build_graph()
Definition at line 1576 of file TMBad.cpp.
Referenced by TMBad::sequential_reduction::sequential_reduction().
|
inline |
Generic forward sweep.
begin | Skip operators up to this point. |
node_filter | Skip operators not in this mask. |
args.ptr
when using begin
. Definition at line 1010 of file global.hpp.
|
inline |
Generic forward sweep.
begin | Skip operators up to this point. |
node_filter | Skip operators not in this mask. |
args.ptr
when using begin
. Definition at line 1021 of file global.hpp.
void TMBad::global::forward_replay | ( | bool | inv_tags = true , |
bool | dep_tags = true |
||
) |
Replay this operation sequence to itself.
inv_tags | Keep independent variable tags? |
dep_tags | Keep dependent variable tags? |
Definition at line 1221 of file TMBad.cpp.
Referenced by TMBad::ADFun<>::replay().
hash_t TMBad::global::hash | ( | ) | const |
Simple hash code of tape.
The hash function has the following properties:
identical()
.identical()
. This situation must always be followed up by a collision test. std::vector< hash_t > TMBad::global::hash_sweep | ( | hash_config | cfg | ) | const |
Calculate hash codes of each dependent variable using a single forward sweep.
The hash function has the following properties:
identical()
.If x and y have equal hash codes then x and y are likely identical()
. This situation must always be followed up by a collision test.
In the weak case the hash codes attempt to discriminate between mappings. In the strong case the hash codes discriminate between sub-expressions. For example consider the computational graph of
(x1+x2) * (x3+x4)
In the weak case (x1+x2) and (x3+x4) are considered identical because the mappings f(x1,x2)=x1+x2 and f(x3,x4)=x3+x4 are identical. If, in addition, the mappings are evaluated at the exact same arguments then they are identical in the strong sense (equal sub-expressions).
The hash function works as follows. Starting out with a scalar hash update function hash(x,h)
we generalize to vectors using the recursion
hash(x_1,...,x_n) := hash(x_1, hash(x_2,...,x_n) )
Hash codes of the operation
(y_1,...,y_m) = f(x_1,...,x_n)
are defined as
hash(y_k) = hash(f, x_1, ..., x_n) + k - 1
assuming f
is represented by its OperatorPure::identifier()
.
Note, that dynamic operators by default have a unique identifier (their memory address). It follows that the hash code of output variables are (very likely) unique as well.
For the reason above, it may seem tempting to skip input hashing completely in the dynamic operator case. However, that won't work because we allow dynamic operators to have a static identifier, see Operator::add_static_identifier
.
weak | If true calculate weak hash codes. Otherwise strong. |
values
). Definition at line 1659 of file TMBad.cpp.
Referenced by TMBad::autopar::output_size(), and TMBad::reorder_sub_expressions().
bool TMBad::global::identical | ( | const global & | other | ) | const |
std::vector< Index > TMBad::global::op2idx | ( | const std::vector< Index > & | var_subset, |
Index | NA = (Index)-1 |
||
) |
General operator -> variable table generator.
For each operator match its output variable in a given subset of variables. If a match exists return the index of the match, otherwise return a missing value code. If several matches exist, return the index corresponding to the first match from the left.
opstack.size()
. Definition at line 1462 of file TMBad.cpp.
Referenced by TMBad::autopar::extract(), and TMBad::sequential_reduction::sequential_reduction().
void TMBad::global::reverse | ( | Position | start = Position(0, 0, 0) | ) |
Full or partial reverse sweep through the operation stack. Updates global::derivs
.
start | Specify 'tail sweep' starting from this position |
opstack[start:end]
will in general affect the derivative array outside the desired range, i.e. the derivative array should be considered as invalidated for indices smaller than start.ptr.second
. Definition at line 1015 of file TMBad.cpp.
Referenced by TMBad::accumulation_tree_split(), TMBad::ADFun<>::activeDomain(), TMBad::get_accumulation_tree(), and TMBad::ADFun<>::Jacobian().
graph TMBad::global::reverse_graph | ( | std::vector< bool > | keep_var = std::vector<bool>(0) | ) |
Construct operator graph with reverse connections.
See build_graph()
Definition at line 1584 of file TMBad.cpp.
Referenced by TMBad::ADFun<>::Jacobian(), and TMBad::sequential_reduction::sequential_reduction().
|
inline |
Generic reverse sweep.
node_filter | Skip operators not in this mask. |
begin | Skip operators up to this point. |
Definition at line 1029 of file global.hpp.
|
inline |
Generic reverse sweep.
node_filter | Skip operators not in this mask. |
begin | Skip operators up to this point. |
Definition at line 1041 of file global.hpp.
void TMBad::global::set_fuse | ( | bool | flag | ) |
void TMBad::global::set_subgraph | ( | const std::vector< bool > & | marks, |
bool | append = false |
||
) |
Convert selected variables to a subgraph sequence.
For each variable check if the variable is marked. If yes add the (unique) operator, that generated the variable as its result, to the subgraph sequence. Although an operator can generate multiple variables it is guarantied that an operator is only added once.
void TMBad::global::shrink_to_fit | ( | double | tol = .9 | ) |
Release unnecessary workspace to the system.
In contrast to clear
this method does not destroy the object. Its intended use is to be called before and/or after a memory intensive routine, e.g. global::optimize
or global::build_graph
. The function attempts to shrink the workspace of the most critical containers:
derivs
is freed completely.subgraph_ptr
is freed completely.values
is reduced if the potential relative reduction is 'significant'.inputs
is reduced if the potential relative reduction is 'significant'. tol | Number between zero and one. A reduction is considered 'significant' if it is at least 1-tol . |
Definition at line 973 of file TMBad.cpp.
Referenced by TMBad::reorder_depth_first().
void TMBad::global::subgraph_cache_ptr | ( | ) | const |
Cache array pointers required by all subgraph routines.
sizeof(IndexPair) * opstack.size()
is allocated. Definition at line 1230 of file TMBad.cpp.
Referenced by TMBad::remap_identical_sub_expressions(), TMBad::reorder_depth_first(), TMBad::sequential_reduction::sequential_reduction(), and TMBad::split_period().
std::vector< Index > TMBad::global::var2op | ( | ) |
Build variable -> operator node lookup table using a single forward pass. The resulting sequence is monotonically increasing.
For each variable (i.e. pointer into global::values) find operator (i.e. pointer into global::opstack) that generated the variable as its results.
values.size()
. Definition at line 1409 of file TMBad.cpp.
Referenced by TMBad::ADFun<>::decompose_refs(), TMBad::get_accumulation_tree(), TMBad::remap_identical_sub_expressions(), TMBad::reorder_depth_first(), TMBad::reorder_temporaries(), and TMBad::reverse_boundary().
void(* TMBad::global::forward_compiled) (Scalar *) |
global* TMBad::global::parent_glob |
Previous ad context to be restored then this context ends.
The value NULL
signifies that either
Definition at line 2763 of file global.hpp.
Referenced by TMBad::global::ad_aug::in_context_stack().
void(* TMBad::global::reverse_compiled) (Scalar *, Scalar *) |