1 | /* $Id: dependent.hpp 3607 2015-01-20 16:20:41Z bradbell $ */ |
---|
2 | # ifndef CPPAD_DEPENDENT_INCLUDED |
---|
3 | # define CPPAD_DEPENDENT_INCLUDED |
---|
4 | |
---|
5 | /* -------------------------------------------------------------------------- |
---|
6 | CppAD: C++ Algorithmic Differentiation: Copyright (C) 2003-15 Bradley M. Bell |
---|
7 | |
---|
8 | CppAD is distributed under multiple licenses. This distribution is under |
---|
9 | the terms of the |
---|
10 | Eclipse Public License Version 1.0. |
---|
11 | |
---|
12 | A copy of this license is included in the COPYING file of this distribution. |
---|
13 | Please visit http://www.coin-or.org/CppAD/ for information on other licenses. |
---|
14 | -------------------------------------------------------------------------- */ |
---|
15 | /* |
---|
16 | $begin Dependent$$ |
---|
17 | $spell |
---|
18 | alloc |
---|
19 | num |
---|
20 | taylor_ |
---|
21 | ADvector |
---|
22 | const |
---|
23 | $$ |
---|
24 | |
---|
25 | $spell |
---|
26 | $$ |
---|
27 | |
---|
28 | $section Stop Recording and Store Operation Sequence$$ |
---|
29 | |
---|
30 | $index ADFun, operation sequence$$ |
---|
31 | $index operation, sequence store$$ |
---|
32 | $index sequence, operation store$$ |
---|
33 | $index recording, stop$$ |
---|
34 | $index tape, stop recording$$ |
---|
35 | $index Dependent$$ |
---|
36 | |
---|
37 | $head Syntax$$ |
---|
38 | $icode%f%.Dependent(%x%, %y%)%$$ |
---|
39 | |
---|
40 | $head Purpose$$ |
---|
41 | Stop recording and the AD of $icode Base$$ |
---|
42 | $cref/operation sequence/glossary/Operation/Sequence/$$ |
---|
43 | that started with the call |
---|
44 | $codei% |
---|
45 | Independent(%x%) |
---|
46 | %$$ |
---|
47 | and store the operation sequence in $icode f$$. |
---|
48 | The operation sequence defines an |
---|
49 | $cref/AD function/glossary/AD Function/$$ |
---|
50 | $latex \[ |
---|
51 | F : B^n \rightarrow B^m |
---|
52 | \] $$ |
---|
53 | where $latex B$$ is the space corresponding to objects of type $icode Base$$. |
---|
54 | The value $latex n$$ is the dimension of the |
---|
55 | $cref/domain/seq_property/Domain/$$ space for the operation sequence. |
---|
56 | The value $latex m$$ is the dimension of the |
---|
57 | $cref/range/seq_property/Range/$$ space for the operation sequence |
---|
58 | (which is determined by the size of $icode y$$). |
---|
59 | |
---|
60 | $head f$$ |
---|
61 | The object $icode f$$ has prototype |
---|
62 | $codei% |
---|
63 | ADFun<%Base%> %f% |
---|
64 | %$$ |
---|
65 | The AD of $icode Base$$ operation sequence is stored in $icode f$$; i.e., |
---|
66 | it becomes the operation sequence corresponding to $icode f$$. |
---|
67 | If a previous operation sequence was stored in $icode f$$, |
---|
68 | it is deleted. |
---|
69 | |
---|
70 | $head x$$ |
---|
71 | The argument $icode x$$ |
---|
72 | must be the vector argument in a previous call to |
---|
73 | $cref Independent$$. |
---|
74 | Neither its size, or any of its values, are allowed to change |
---|
75 | between calling |
---|
76 | $codei% |
---|
77 | Independent(%x%) |
---|
78 | %$$ |
---|
79 | and |
---|
80 | $codei% |
---|
81 | %f%.Dependent(%x%, %y%) |
---|
82 | %$$. |
---|
83 | |
---|
84 | $head y$$ |
---|
85 | The vector $icode y$$ has prototype |
---|
86 | $codei% |
---|
87 | const %ADvector% &%y% |
---|
88 | %$$ |
---|
89 | (see $cref/ADvector/FunConstruct/$$ below). |
---|
90 | The length of $icode y$$ must be greater than zero |
---|
91 | and is the dimension of the range space for $icode f$$. |
---|
92 | |
---|
93 | $head ADvector$$ |
---|
94 | The type $icode ADvector$$ must be a $cref SimpleVector$$ class with |
---|
95 | $cref/elements of type/SimpleVector/Elements of Specified Type/$$ |
---|
96 | $codei%AD<%Base%>%$$. |
---|
97 | The routine $cref CheckSimpleVector$$ will generate an error message |
---|
98 | if this is not the case. |
---|
99 | |
---|
100 | $head Taping$$ |
---|
101 | The tape, |
---|
102 | that was created when $codei%Independent(%x%)%$$ was called, |
---|
103 | will stop recording. |
---|
104 | The AD operation sequence will be transferred from |
---|
105 | the tape to the object $icode f$$ and the tape will then be deleted. |
---|
106 | |
---|
107 | $head Forward$$ |
---|
108 | No $cref Forward$$ calculation is preformed during this operation. |
---|
109 | Thus, directly after this operation, |
---|
110 | $codei% |
---|
111 | %f%.size_order() |
---|
112 | %$$ |
---|
113 | is zero (see $cref size_order$$). |
---|
114 | |
---|
115 | $head Parallel Mode$$ |
---|
116 | $index parallel, Dependent$$ |
---|
117 | $index Dependent, parallel$$ |
---|
118 | The call to $code Independent$$, |
---|
119 | and the corresponding call to |
---|
120 | $codei% |
---|
121 | ADFun<%Base%> %f%( %x%, %y%) |
---|
122 | %$$ |
---|
123 | or |
---|
124 | $codei% |
---|
125 | %f%.Dependent( %x%, %y%) |
---|
126 | %$$ |
---|
127 | or $cref abort_recording$$, |
---|
128 | must be preformed by the same thread; i.e., |
---|
129 | $cref/thread_alloc::thread_num/ta_thread_num/$$ must be the same. |
---|
130 | |
---|
131 | $head Example$$ |
---|
132 | The file |
---|
133 | $cref fun_check.cpp$$ |
---|
134 | contains an example and test of this operation. |
---|
135 | It returns true if it succeeds and false otherwise. |
---|
136 | |
---|
137 | $end |
---|
138 | ---------------------------------------------------------------------------- |
---|
139 | */ |
---|
140 | |
---|
141 | |
---|
142 | // BEGIN CppAD namespace |
---|
143 | namespace CppAD { |
---|
144 | |
---|
145 | /*! |
---|
146 | \file dependent.hpp |
---|
147 | Different versions of Dependent function. |
---|
148 | */ |
---|
149 | |
---|
150 | /*! |
---|
151 | Determine the \c tape corresponding to this exeuction thread and then use |
---|
152 | <code>Dependent(tape, y)</code> to store this tapes recording in a function. |
---|
153 | |
---|
154 | \param y [in] |
---|
155 | The dependent variable vector for the corresponding function. |
---|
156 | */ |
---|
157 | template <typename Base> |
---|
158 | template <typename ADvector> |
---|
159 | void ADFun<Base>::Dependent(const ADvector &y) |
---|
160 | { ADTape<Base>* tape = AD<Base>::tape_ptr(); |
---|
161 | CPPAD_ASSERT_KNOWN( |
---|
162 | tape != CPPAD_NULL, |
---|
163 | "Can't store current operation sequence in this ADFun object" |
---|
164 | "\nbecause there is no active tape (for this thread)." |
---|
165 | ); |
---|
166 | |
---|
167 | // code above just determines the tape and checks for errors |
---|
168 | Dependent(tape, y); |
---|
169 | } |
---|
170 | |
---|
171 | |
---|
172 | /*! |
---|
173 | Determine the \c tape corresponding to this exeuction thread and then use |
---|
174 | <code>Dependent(tape, y)</code> to store this tapes recording in a function. |
---|
175 | |
---|
176 | \param x [in] |
---|
177 | The independent variable vector for this tape. This informaiton is |
---|
178 | also stored in the tape so a check is done to make sure it is correct |
---|
179 | (if NDEBUG is not defined). |
---|
180 | |
---|
181 | \param y [in] |
---|
182 | The dependent variable vector for the corresponding function. |
---|
183 | */ |
---|
184 | template <typename Base> |
---|
185 | template <typename ADvector> |
---|
186 | void ADFun<Base>::Dependent(const ADvector &x, const ADvector &y) |
---|
187 | { |
---|
188 | CPPAD_ASSERT_KNOWN( |
---|
189 | x.size() > 0, |
---|
190 | "Dependent: independent variable vector has size zero." |
---|
191 | ); |
---|
192 | CPPAD_ASSERT_KNOWN( |
---|
193 | Variable(x[0]), |
---|
194 | "Dependent: independent variable vector has been changed." |
---|
195 | ); |
---|
196 | ADTape<Base> *tape = AD<Base>::tape_ptr(x[0].tape_id_); |
---|
197 | CPPAD_ASSERT_KNOWN( |
---|
198 | tape->size_independent_ == size_t( x.size() ), |
---|
199 | "Dependent: independent variable vector has been changed." |
---|
200 | ); |
---|
201 | # ifndef NDEBUG |
---|
202 | size_t i, j; |
---|
203 | for(j = 0; j < size_t(x.size()); j++) |
---|
204 | { CPPAD_ASSERT_KNOWN( |
---|
205 | size_t(x[j].taddr_) == (j+1), |
---|
206 | "ADFun<Base>: independent variable vector has been changed." |
---|
207 | ); |
---|
208 | CPPAD_ASSERT_KNOWN( |
---|
209 | x[j].tape_id_ == x[0].tape_id_, |
---|
210 | "ADFun<Base>: independent variable vector has been changed." |
---|
211 | ); |
---|
212 | } |
---|
213 | for(i = 0; i < size_t(y.size()); i++) |
---|
214 | { CPPAD_ASSERT_KNOWN( |
---|
215 | CppAD::Parameter( y[i] ) | (y[i].tape_id_ == x[0].tape_id_) , |
---|
216 | "ADFun<Base>: dependent vector contains a variable for" |
---|
217 | "\na different tape (thread) than the independent variables." |
---|
218 | ); |
---|
219 | } |
---|
220 | # endif |
---|
221 | |
---|
222 | // code above just determines the tape and checks for errors |
---|
223 | Dependent(tape, y); |
---|
224 | } |
---|
225 | |
---|
226 | /*! |
---|
227 | Replace the floationg point operations sequence for this function object. |
---|
228 | |
---|
229 | \param tape |
---|
230 | is a tape that contains the new floating point operation sequence |
---|
231 | for this function. |
---|
232 | After this operation, all memory allocated for this tape is deleted. |
---|
233 | |
---|
234 | \param y |
---|
235 | The dependent variable vector for the function being stored in this object. |
---|
236 | |
---|
237 | \par |
---|
238 | All of the private member data in ad_fun.hpp is set to correspond to the |
---|
239 | new tape except for check_for_nan_. |
---|
240 | */ |
---|
241 | |
---|
242 | template <typename Base> |
---|
243 | template <typename ADvector> |
---|
244 | void ADFun<Base>::Dependent(ADTape<Base> *tape, const ADvector &y) |
---|
245 | { |
---|
246 | size_t m = y.size(); |
---|
247 | size_t n = tape->size_independent_; |
---|
248 | size_t i, j; |
---|
249 | size_t y_taddr; |
---|
250 | |
---|
251 | // check ADvector is Simple Vector class with AD<Base> elements |
---|
252 | CheckSimpleVector< AD<Base>, ADvector>(); |
---|
253 | |
---|
254 | CPPAD_ASSERT_KNOWN( |
---|
255 | y.size() > 0, |
---|
256 | "ADFun operation sequence dependent variable size is zero size" |
---|
257 | ); |
---|
258 | // --------------------------------------------------------------------- |
---|
259 | // Begin setting ad_fun.hpp private member data |
---|
260 | // --------------------------------------------------------------------- |
---|
261 | // dep_parameter_, dep_taddr_ |
---|
262 | CPPAD_ASSERT_UNKNOWN( NumRes(ParOp) == 1 ); |
---|
263 | dep_parameter_.resize(m); |
---|
264 | dep_taddr_.resize(m); |
---|
265 | for(i = 0; i < m; i++) |
---|
266 | { dep_parameter_[i] = CppAD::Parameter(y[i]); |
---|
267 | if( dep_parameter_[i] ) |
---|
268 | { // make a tape copy of dependent variables that are parameters, |
---|
269 | y_taddr = tape->RecordParOp( y[i].value_ ); |
---|
270 | } |
---|
271 | else y_taddr = y[i].taddr_; |
---|
272 | |
---|
273 | CPPAD_ASSERT_UNKNOWN( y_taddr > 0 ); |
---|
274 | dep_taddr_[i] = y_taddr; |
---|
275 | } |
---|
276 | |
---|
277 | // put an EndOp at the end of the tape |
---|
278 | tape->Rec_.PutOp(EndOp); |
---|
279 | |
---|
280 | // some size_t values in ad_fun.hpp |
---|
281 | has_been_optimized_ = false; |
---|
282 | compare_change_count_ = 1; |
---|
283 | compare_change_number_ = 0; |
---|
284 | compare_change_op_index_ = 0; |
---|
285 | num_order_taylor_ = 0; |
---|
286 | num_direction_taylor_ = 0; |
---|
287 | cap_order_taylor_ = 0; |
---|
288 | |
---|
289 | // num_var_tape_ |
---|
290 | // Now that all the variables are in the tape, we can set this value. |
---|
291 | num_var_tape_ = tape->Rec_.num_var_rec(); |
---|
292 | |
---|
293 | // taylor_ |
---|
294 | taylor_.erase(); |
---|
295 | |
---|
296 | // cskip_op_ |
---|
297 | cskip_op_.erase(); |
---|
298 | cskip_op_.extend( tape->Rec_.num_op_rec() ); |
---|
299 | |
---|
300 | // load_op_ |
---|
301 | load_op_.erase(); |
---|
302 | load_op_.extend( tape->Rec_.num_load_op_rec() ); |
---|
303 | |
---|
304 | // play_ |
---|
305 | // Now that each dependent variable has a place in the tape, |
---|
306 | // and there is a EndOp at the end of the tape, we can transfer the |
---|
307 | // recording to the player and and erase the tape. |
---|
308 | play_.get(tape->Rec_); |
---|
309 | |
---|
310 | // ind_taddr_ |
---|
311 | // Note that play_ has been set, we can use it to check operators |
---|
312 | ind_taddr_.resize(n); |
---|
313 | CPPAD_ASSERT_UNKNOWN( n < num_var_tape_); |
---|
314 | for(j = 0; j < n; j++) |
---|
315 | { CPPAD_ASSERT_UNKNOWN( play_.GetOp(j+1) == InvOp ); |
---|
316 | ind_taddr_[j] = j+1; |
---|
317 | } |
---|
318 | |
---|
319 | // for_jac_sparse_pack_, for_jac_sparse_set_ |
---|
320 | for_jac_sparse_pack_.resize(0, 0); |
---|
321 | for_jac_sparse_set_.resize(0,0); |
---|
322 | // --------------------------------------------------------------------- |
---|
323 | // End set ad_fun.hpp private member data |
---|
324 | // --------------------------------------------------------------------- |
---|
325 | |
---|
326 | // now we can delete the tape |
---|
327 | AD<Base>::tape_manage(tape_manage_delete); |
---|
328 | |
---|
329 | // total number of varables in this recording |
---|
330 | CPPAD_ASSERT_UNKNOWN( num_var_tape_ == play_.num_var_rec() ); |
---|
331 | |
---|
332 | // used to determine if there is an operation sequence in *this |
---|
333 | CPPAD_ASSERT_UNKNOWN( num_var_tape_ > 0 ); |
---|
334 | |
---|
335 | } |
---|
336 | |
---|
337 | } // END CppAD namespace |
---|
338 | |
---|
339 | # endif |
---|