CppAD: A C++ Algorithmic Differentiation Package  20171217
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
num_skip.hpp
Go to the documentation of this file.
1 # ifndef CPPAD_CORE_NUM_SKIP_HPP
2 # define CPPAD_CORE_NUM_SKIP_HPP
3 
4 /* --------------------------------------------------------------------------
5 CppAD: C++ Algorithmic Differentiation: Copyright (C) 2003-17 Bradley M. Bell
6 
7 CppAD is distributed under multiple licenses. This distribution is under
8 the terms of the
9  Eclipse Public License Version 1.0.
10 
11 A copy of this license is included in the COPYING file of this distribution.
12 Please visit http://www.coin-or.org/CppAD/ for information on other licenses.
13 -------------------------------------------------------------------------- */
14 
15 /*
16 $begin number_skip$$
17 $spell
18  optimizer
19  var
20  taylor_
21 $$
22 
23 
24 $section Number of Variables that Can be Skipped$$
25 $mindex number_skip$$
26 
27 $head Syntax$$
28 $icode%n% = %f%.number_skip()%$$
29 
30 $subhead See Also$$
31 $cref seq_property$$
32 
33 $head Purpose$$
34 The $cref/conditional expressions/CondExp/$$ use either the
35 $cref/if_true/CondExp/$$ or $cref/if_false/CondExp/$$.
36 Hence, some terms only need to be evaluated
37 depending on the value of the comparison in the conditional expression.
38 The $cref optimize$$ option is capable of detecting some of these
39 case and determining variables that can be skipped.
40 This routine returns the number such variables.
41 
42 $head n$$
43 The return value $icode n$$ has type $code size_t$$
44 is the number of variables that the optimizer has determined can be skipped
45 (given the independent variable values specified by the previous call to
46 $cref/f.Forward/Forward/$$ for order zero).
47 
48 $head f$$
49 The object $icode f$$ has prototype
50 $codei%
51  ADFun<%Base%> %f%
52 %$$
53 
54 $children%
55  example/general/number_skip.cpp
56 %$$
57 $head Example$$
58 The file $cref number_skip.cpp$$
59 contains an example and test of this function.
60 It returns true if it succeeds and false otherwise.
61 
62 $end
63 -----------------------------------------------------------------------------
64 */
65 
66 // BEGIN CppAD namespace
67 namespace CppAD {
68 
69 // This routine is not const because it runs through the operations sequence
70 // 2DO: compute this value during zero order forward operations.
71 template <typename Base>
73 { // must pass through operation sequence to map operations to variables
74  local::OpCode op;
75  size_t i_op;
76  size_t i_var;
77  const addr_t* arg;
78 
79  // information defined by forward_user
80  size_t user_old=0, user_m=0, user_n=0;
81 
82  // number of variables skipped
83  size_t num_var_skip = 0;
84 
85  // start playback
86  i_op = 0;
87  play_.get_op_info(i_op, op, arg, i_var);
89  while(op != local::EndOp)
90  { // next op
91  play_.get_op_info(++i_op, op, arg, i_var);
92  //
93  if( op == local::UserOp )
94  { // skip only appears at front or back UserOp of user atomic call
95  bool skip_call = cskip_op_[i_op];
96  play_.get_user_info(op, arg, user_old, user_m, user_n);
97  CPPAD_ASSERT_UNKNOWN( NumRes(op) == 0 );
98  size_t num_op = user_m + user_n + 1;
99  for(size_t i = 0; i < num_op; i++)
100  { play_.get_op_info(++i_op, op, arg, i_var);
101  if( skip_call )
102  num_var_skip += NumRes(op);
103  }
105  }
106  else
107  { if( cskip_op_[i_op] )
108  num_var_skip += NumRes(op);
109  }
110  }
111  return num_var_skip;
112 }
113 
114 } // END CppAD namespace
115 
116 
117 # endif
CPPAD_TAPE_ADDR_TYPE addr_t
Definition: declare_ad.hpp:44
size_t NumRes(OpCode op)
Number of variables resulting from the specified operation.
Definition: op_code.hpp:281
OpCode
Type used to distinguish different AD&lt; Base &gt; atomic operations.
Definition: op_code.hpp:49
#define CPPAD_ASSERT_UNKNOWN(exp)
Check that exp is true, if not terminate execution.
size_t number_skip(void)
number of variables in conditional expressions that can be skipped
Definition: num_skip.hpp:72