CppAD: A C++ Algorithmic Differentiation Package  20171217
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
core/independent.hpp
Go to the documentation of this file.
1 # ifndef CPPAD_CORE_INDEPENDENT_HPP
2 # define CPPAD_CORE_INDEPENDENT_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 
17 $begin Independent$$
18 $spell
19  op
20  alloc
21  num
22  Cpp
23  bool
24  const
25  var
26  typename
27 $$
28 
29 $section Declare Independent Variables and Start Recording$$
30 
31 $head Syntax$$
32 $codei%Independent(%x%)
33 %$$
34 $codei%Independent(%x%, %abort_op_index%)
35 %$$
36 
37 $head Purpose$$
38 Start recording
39 $cref/AD of Base/glossary/AD of Base/$$ operations
40 with $icode x$$ as the independent variable vector.
41 Once the
42 $cref/operation sequence/glossary/Operation/Sequence/$$ is completed,
43 it must be transferred to a function object; see below.
44 
45 $head Start Recording$$
46 An operation sequence recording is started by the commands
47 $codei%
48  Independent(%x%)
49  Independent(%x%, %abort_op_index%)
50 %$$
51 
52 $head Stop Recording$$
53 The recording is stopped,
54 and the operation sequence is transferred to the AD function object $icode f$$,
55 using either the $cref/function constructor/FunConstruct/$$
56 $codei%
57  ADFun<%Base%> %f%(%x%, %y%)
58 %$$
59 or the $cref/dependent variable specifier/Dependent/$$
60 $codei%
61  %f%.Dependent(%x%, %y%)
62 %$$
63 The only other way to stop a recording is using
64 $cref abort_recording$$.
65 Between when the recording is started and when it stopped,
66 we refer to the elements of $icode x$$,
67 and the values that depend on the elements of $icode x$$,
68 as $codei%AD<%Base%>%$$ variables.
69 
70 $head x$$
71 The vector $icode x$$ has prototype
72 $codei%
73  %VectorAD% &%x%
74 %$$
75 (see $icode VectorAD$$ below).
76 The size of the vector $icode x$$, must be greater than zero,
77 and is the number of independent variables for this
78 AD operation sequence.
79 
80 $head abort_op_index$$
81 It specifies the operator index at which the execution is be aborted
82 by calling the CppAD $cref/error handler/ErrorHandler/$$.
83 When this error handler leads to an assert, the user
84 can inspect the call stack to see the source code corresponding to
85 this operator index; see
86 $cref/purpose/compare_change/op_index/Purpose/$$.
87 No abort will occur if $icode abort_op_index$$ is zero,
88 of if $cref/NDEBUG/Faq/Speed/NDEBUG/$$ is defined.
89 
90 $head VectorAD$$
91 The type $icode VectorAD$$ must be a $cref SimpleVector$$ class with
92 $cref/elements of type/SimpleVector/Elements of Specified Type/$$
93 $codei%AD<%Base%>%$$.
94 The routine $cref CheckSimpleVector$$ will generate an error message
95 if this is not the case.
96 
97 $head Parallel Mode$$
98 Each thread can have one, and only one, active recording.
99 A call to $code Independent$$ starts the recording for the current thread.
100 The recording must be stopped by a corresponding call to
101 $codei%
102  ADFun<%Base%> %f%( %x%, %y%)
103 %$$
104 or
105 $codei%
106  %f%.Dependent( %x%, %y%)
107 %$$
108 or $cref abort_recording$$
109 preformed by the same thread; i.e.,
110 $cref/thread_alloc::thread_num/ta_thread_num/$$ must be the same.
111 
112 $head Example$$
113 $children%
114  example/general/independent.cpp
115 %$$
116 The file
117 $cref independent.cpp$$
118 contains an example and test of this operation.
119 It returns true if it succeeds and false otherwise.
120 
121 $end
122 -----------------------------------------------------------------------------
123 */
124 # include <cppad/local/independent.hpp>
125 /*!
126 \file core/independent.hpp
127 Declare the independent variables
128 */
129 
130 namespace CppAD { // BEGIN_CPPAD_NAMESPACE
131 
132 /*!
133 Declaration of independent variables.
134 
135 \tparam VectorAD
136 This is simple vector type with elements of type AD<Base>.
137 
138 \param x
139 Vector of the independent variablerd.
140 
141 \param abort_op_index
142 operator index at which execution will be aborted (during the recording
143 of operations). The value zero corresponds to not aborting (will not match).
144 */
145 template <typename VectorAD>
146 inline void Independent(VectorAD &x, size_t abort_op_index)
147 { typedef typename VectorAD::value_type ADBase;
148  typedef typename ADBase::value_type Base;
150  ADBase::tape_ptr() == CPPAD_NULL,
151  "Independent: cannot create a new tape because\n"
152  "a previous tape is still active (for this thread).\n"
153  "AD<Base>::abort_recording() would abort this previous recording."
154  );
155  local::ADTape<Base>* tape = ADBase::tape_manage(tape_manage_new);
156  tape->Independent(x, abort_op_index);
157 }
158 /*!
159 Declaration of independent variables without abort option.
160 
161 \tparam VectorAD
162 This is simple vector type with elements of type AD<Base>.
163 
164 \param x
165 Vector of the independent variablerd.
166 */
167 template <typename VectorAD>
168 inline void Independent(VectorAD &x)
169 { size_t abort_op_index = 0;
170  Independent(x, abort_op_index);
171 }
172 
173 } // END_CPPAD_NAMESPACE
174 
175 # endif
#define CPPAD_ASSERT_KNOWN(exp, msg)
Check that exp is true, if not print msg and terminate execution.
void Independent(VectorADBase &u)
void Independent(VectorAD &x, size_t abort_op_index)
Declaration of independent variables.
Class used to hold tape that records AD&lt;Base&gt; operations.
Definition: ad_tape.hpp:26
Scalar value_type