CppAD: A C++ Algorithmic Differentiation Package  20171217
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
ad_tape.hpp
Go to the documentation of this file.
1 # ifndef CPPAD_LOCAL_AD_TAPE_HPP
2 # define CPPAD_LOCAL_AD_TAPE_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 # include <cppad/core/define.hpp>
15 
16 namespace CppAD { namespace local { // BEGIN_CPPAD_LOCAL__NAMESPACE
17 
18 /*!
19 Class used to hold tape that records AD<Base> operations.
20 
21 \tparam Base
22 An <tt>AD<Base></tt> object is used to recording <tt>AD<Base></tt> operations.
23 */
24 
25 template <class Base>
26 class ADTape {
27  // Friends =============================================================
28 
29  // classes -------------------------------------------------------------
30  friend class AD<Base>;
31  friend class ADFun<Base>;
32  friend class atomic_base<Base>;
33  friend class discrete<Base>;
34  friend class VecAD<Base>;
35  friend class VecAD_reference<Base>;
36 
37  // functions -----------------------------------------------------------
38  // PrintFor
39  friend void CppAD::PrintFor <Base> (
40  const AD<Base>& flag ,
41  const char* before ,
42  const AD<Base>& var ,
43  const char* after
44  );
45  // CondExpOp
46  friend AD<Base> CppAD::CondExpOp <Base> (
47  enum CompareOp cop ,
48  const AD<Base> &left ,
49  const AD<Base> &right ,
50  const AD<Base> &trueCase ,
51  const AD<Base> &falseCase
52  );
53  // pow
54  friend AD<Base> CppAD::pow <Base>
55  (const AD<Base> &x, const AD<Base> &y);
56  // azmul
57  friend AD<Base> CppAD::azmul <Base>
58  (const AD<Base> &x, const AD<Base> &y);
59  // Parameter
60  friend bool CppAD::Parameter <Base>
61  (const AD<Base> &u);
62  // Variable
63  friend bool CppAD::Variable <Base>
64  (const AD<Base> &u);
65  // operators -----------------------------------------------------------
66  // arithematic binary operators
67  friend AD<Base> CppAD::operator + <Base>
68  (const AD<Base> &left, const AD<Base> &right);
69  friend AD<Base> CppAD::operator - <Base>
70  (const AD<Base> &left, const AD<Base> &right);
71  friend AD<Base> CppAD::operator * <Base>
72  (const AD<Base> &left, const AD<Base> &right);
73  friend AD<Base> CppAD::operator / <Base>
74  (const AD<Base> &left, const AD<Base> &right);
75 
76  // comparison operators
77  friend bool CppAD::operator < <Base>
78  (const AD<Base> &left, const AD<Base> &right);
79  friend bool CppAD::operator <= <Base>
80  (const AD<Base> &left, const AD<Base> &right);
81  friend bool CppAD::operator > <Base>
82  (const AD<Base> &left, const AD<Base> &right);
83  friend bool CppAD::operator >= <Base>
84  (const AD<Base> &left, const AD<Base> &right);
85  friend bool CppAD::operator == <Base>
86  (const AD<Base> &left, const AD<Base> &right);
87  friend bool CppAD::operator != <Base>
88  (const AD<Base> &left, const AD<Base> &right);
89  // ======================================================================
90 
91 // --------------------------------------------------------------------------
92 private:
93  // ----------------------------------------------------------------------
94  // private data
95  /*!
96  Unique identifier for this tape. It is always greater than
97  CPPAD_MAX_NUM_THREADS, and different for every tape (even ones that have
98  been deleted). In addition, id_ % CPPAD_MAX_NUM_THREADS is the thread
99  number for this tape. Set by Independent and effectively const
100  */
102  /// Number of independent variables in this tapes reconding.
103  /// Set by Independent and effectively const
105  /// This is where the information is recorded.
107  // ----------------------------------------------------------------------
108  // private functions
109  //
110  // add a parameter to the tape
111  addr_t RecordParOp(const Base &x);
112 
113  // see CondExp.h
114  void RecordCondExp(
115  enum CompareOp cop ,
116  AD<Base> &returnValue ,
117  const AD<Base> &left ,
118  const AD<Base> &right ,
119  const AD<Base> &trueCase ,
120  const AD<Base> &falseCase
121  );
122 
123  // place a VecAD object in the tape
124  size_t AddVec(
125  size_t length,
126  const pod_vector<Base>& data
127  );
128 
129 public:
130  // default constructor and destructor
131 
132  // public function only used by CppAD::Independent
133  template <typename VectorADBase>
134  void Independent(VectorADBase &u);
135  template <typename VectorADBase>
136  void Independent(VectorADBase &u, size_t abort_op_index);
137 
138 };
139 // ---------------------------------------------------------------------------
140 // Private functions
141 //
142 
143 /*!
144 Place a parameter in the tape.
145 
146 On rare occations it is necessary to place a parameter in the tape; e.g.,
147 when it is one of the dependent variabes.
148 
149 \param z
150 value of the parameter that we are placing in the tape.
151 
152 \return
153 variable index (for this recording) correpsonding to the parameter.
154 
155 \par 2DO
156 All these operates are preformed in \c Rec_, so we should
157 move this routine from <tt>ADTape<Base></tt> to <tt>recorder<Base></tt>.
158 */
159 template <class Base>
161 { addr_t z_taddr;
162  addr_t ind;
165  z_taddr = Rec_.PutOp(ParOp);
166  ind = Rec_.PutPar(z);
167  Rec_.PutArg(ind);
168 
169  return z_taddr;
170 }
171 
172 /*!
173 Put initialization for a VecAD<Base> object in the tape.
174 
175 This routine should be called once for each VecAD object when just
176 before it changes from a parameter to a variable.
177 
178 \param length
179 size of the <tt>VecAD<Base></tt> object.
180 
181 \param data
182 initial values for the <tt>VecAD<Base></tt> object
183 (values before it becomes a variable).
184 
185 \return
186 index of the start of this vector in the list of vector indices.
187 The value for this vector index is the length of the vector.
188 There are \c length indices following for this vector.
189 The values for these vector indices are the corresponding
190 parameter indices in the tape for the initial value of the corresponding
191 vec_ad element.
192 
193 \par 2DO
194 All these operates are preformed in \c Rec_, so we should
195 move this routine from <tt>ADTape<Base></tt> to <tt>recorder<Base></tt>.
196 */
197 template <class Base>
198 size_t ADTape<Base>::AddVec(size_t length, const pod_vector<Base>& data)
199 { CPPAD_ASSERT_UNKNOWN( length > 0 );
200  size_t i;
201  size_t value_index;
202 
203  // store the length in VecInd
204  size_t start = Rec_.PutVecInd(length);
205 
206  // store indices of the values in VecInd
207  for(i = 0; i < length; i++)
208  {
209  value_index = Rec_.PutPar( data[i] );
210  Rec_.PutVecInd( value_index );
211  }
212 
213  // return the taddr of the length (where the vector starts)
214  return start;
215 }
216 
217 } } // END_CPPAD_LOCAL_NAMESPACE
218 
219 # endif
addr_t RecordParOp(const Base &x)
Place a parameter in the tape.
Definition: ad_tape.hpp:160
void Independent(VectorADBase &u)
Define processor symbols and macros that are used by CppAD.
Class used to hold function objects.
Definition: ad_fun.hpp:69
CPPAD_TAPE_ADDR_TYPE addr_t
Definition: declare_ad.hpp:44
Definition: ad.hpp:34
size_t NumArg(OpCode op)
Number of arguments for a specified operator.
Definition: op_code.hpp:175
size_t size_independent_
Number of independent variables in this tapes reconding. Set by Independent and effectively const...
Definition: ad_tape.hpp:104
size_t NumRes(OpCode op)
Number of variables resulting from the specified operation.
Definition: op_code.hpp:281
friend bool CppAD::operator(const AD< Base > &left, const AD< Base > &right)
Class used to store an operation sequence while it is being recorded (the operation sequence is copie...
Definition: declare_ad.hpp:28
void RecordCondExp(enum CompareOp cop, AD< Base > &returnValue, const AD< Base > &left, const AD< Base > &right, const AD< Base > &trueCase, const AD< Base > &falseCase)
All these operations are done in Rec_, so we should move this routine to recorder&lt;Base&gt;.
Definition: cond_exp.hpp:254
#define CPPAD_ASSERT_UNKNOWN(exp)
Check that exp is true, if not terminate execution.
size_t AddVec(size_t length, const pod_vector< Base > &data)
Put initialization for a VecAD&lt;Base&gt; object in the tape.
Definition: ad_tape.hpp:198
local::recorder< Base > Rec_
This is where the information is recorded.
Definition: ad_tape.hpp:106
Class used to hold a reference to an element of a VecAD object.
Definition: vec_ad.hpp:352
Class used to hold tape that records AD&lt;Base&gt; operations.
Definition: ad_tape.hpp:26
Vector of AD objects that tracks indexing operations on the tape.
Definition: vec_ad.hpp:462
tape_id_t id_
Unique identifier for this tape.
Definition: ad_tape.hpp:101
CPPAD_TAPE_ID_TYPE tape_id_t
Definition: declare_ad.hpp:45