source: trunk/cppad/local/dependent.hpp @ 3607

Last change on this file since 3607 was 3607, checked in by bradbell, 5 years ago

merge to branch: trunk
from repository: https://github.com/coin-or/CppAD
start hash code: f4baab0ea7c2679ba9351b0439b6efebfa77ba04
end hash code: 894d554a00ceec8a3545f05eca62708a7b5cb43d

commit 894d554a00ceec8a3545f05eca62708a7b5cb43d
Author: Brad Bell <bradbell@…>
Date: Tue Jan 20 09:06:10 2015 -0700

Fix copyright end date.


whats_new_15.omh: Add comment about date of deprecation.

commit 611e982000168db91aba22b763c14bb78d57da47
Author: Brad Bell <bradbell@…>
Date: Tue Jan 20 08:53:00 2015 -0700

Squashed commit from old/compare_op to master:


In addition, fix copyright end date for some files, and add note about
deprecated date in whats_new_15.omh.


commit 6e46df5c850ecd58d7a886db4043bc3f2d4579d1
Author: Brad Bell <bradbell@…>
Date: Tue Jan 20 08:16:57 2015 -0700


Always return f.compare_change_op_index() == 0 after f.optimize().


checkpoint.hpp: ignore comparison operators.
fun_construct.hpp: remove double initilaization of values.
compare_change.cpp: demonstrate more features of new interface.
whats_new_15.omh: prepare for merging this branch into master.
wish_list.omh: update wish list item to new compare_change interface.


commit 45315907c70e5b383d984fb9498b54a474001af0
Author: Brad Bell <bradbell@…>
Date: Tue Jan 20 05:04:37 2015 -0700


Use the new f.compare_change_count(0) option in speed tests.


commit bb6e72befd6d01f1fb62c43b9b19471ffaa7cc2c
Author: Brad Bell <bradbell@…>
Date: Tue Jan 20 04:51:16 2015 -0700


Move CompareChange? -> deprecated and plan -> compare_change.


forward0sweep.hpp: skip comparison operator when count is zero.
forward1sweep.hpp: skip comparison operator when count is zero.
compare_change.cpp: demonstrate count == 0 case.


commit 622a13c568c612d9dfe9ccd1a01f4ac5f74ba824
Author: Brad Bell <bradbell@…>
Date: Mon Jan 19 23:17:42 2015 -0700


Add example and test of new compare change user API.


ad_fun.hpp: fix f.compare_change_op_index.
compare_change.cpp: Change example to use new API.
compare_change.cpp: Move old example here (just test now).


commit ec4c1613eae8df56fbf31e7b8711ce69cc41df83
Author: Brad Bell <bradbell@…>
Date: Mon Jan 19 21:12:11 2015 -0700


Implement the compare change plan, still needs an example and test.
Also have the change from 'plan' to just plain documentation.


commit a81a46f27011bee08ba072551044dc9f4a99a906
Author: Brad Bell <bradbell@…>
Date: Mon Jan 19 17:49:05 2015 -0700


Change name of compare_change functions and partially implement this new plan.


commit 146faad48a700a56362e74f9c3a3c39144a79738
Author: Brad Bell <bradbell@…>
Date: Mon Jan 19 14:22:40 2015 -0700


Branch: compare_op
plan.omh: change name of count function.


commit 35d91d126765d1a0ab4bfe9e2b006bbf535cd648
Author: Brad Bell <bradbell@…>
Date: Mon Jan 19 13:19:07 2015 -0700


Add deprecation date for some more cases.


commit 5bb65a8c48fae4263b66fcd04520e10e66febc11
Author: Brad Bell <bradbell@…>
Date: Mon Jan 19 13:13:51 2015 -0700


Add date of deprecation for some more cases.


commit e95ee6b209601cd9a075d2e37c602e73c32fb6ab
Author: Brad Bell <bradbell@…>
Date: Mon Jan 19 12:58:44 2015 -0700


Add date of deprecation for some more cases.


commit 0ea84ccd87383edc62a6ae1711da104b12e8c444
Author: Brad Bell <bradbell@…>
Date: Mon Jan 19 12:47:01 2015 -0700


Add date of deprecation for some cases.


commit 17755e609ea8e03472b08dcc2fb5ad347eb723cb
Author: Brad Bell <bradbell@…>
Date: Mon Jan 19 08:20:45 2015 -0700


plan.omh: changs some names.


commit 29f369c06d4d0ee284c4c668d52d8461613066dc
Author: Brad Bell <bradbell@…>
Date: Fri Jan 16 06:39:45 2015 -0700


Document the plan for compare_change user API.


compare_change.omh: fix minor typo.
plan.txt: change to the omhelp file plan.omh.


commit a3a2f4dedd202a722812b6eb2714851b40726e6e
Author: Brad Bell <bradbell@…>
Date: Thu Jan 15 21:03:44 2015 -0700


new_branch.sh: remove unused variable.
push_git2svn.py: move repository from github/bradbell to coin-or/bradbell.


commit 3751a197ab2897e76616f9d9b0915148bd855356
Author: Brad Bell <bradbell@…>
Date: Thu Jan 15 20:56:17 2015 -0700


plan.txt: plan for this branches API will go here.


commit 76013ec2ad7baacdeab5e761812d542867910174
Author: Brad Bell <bradbell@…>
Date: Thu Jan 15 18:04:33 2015 -0700


Store the operator index for the first comparision change in the ADFun object.


commit 9caf25014079a60df5de17bcac76775daf8ee201
Author: Brad Bell <bradbell@…>
Date: Thu Jan 15 12:45:56 2015 -0700


Make compare_change a parameter (so will be easy to add compare_first).


commit 2246d22fe82b8909d432f82ab0d783ce3351a02f
Author: Brad Bell <bradbell@…>
Date: Thu Jan 15 09:12:40 2015 -0700


speed_branch.sh: fix directory (before cd).


commit b3910de86558a97749741bfb728e45c5a86d1c73
Author: Brad Bell <bradbell@…>
Date: Thu Jan 15 05:14:01 2015 -0700


search.sh: use git to get list of source files.
ad_fun.hpp: imporve doxygen doc for compare_change_.
ad_tape.hpp: remove RecordCompare? (no longer used).
atomic_base.hpp: minor edit to user documentation.


commit dd74f331386cadc9cc272c264296e575691aa3f8
Author: Brad Bell <bradbell@…>
Date: Thu Jan 15 04:12:34 2015 -0700


Change Leq..Op -> Le..Op and leq.._op -> le.._op.


commit ae729296323eb7f4f4a7c0e90a303a8d7f4ed42a
Author: Brad Bell <bradbell@…>
Date: Wed Jan 14 21:19:55 2015 -0700


comp_op.hpp: Add doxygen documentaiton for compare operators.
compare.hpp: avoid an extra branch.


commit b064d59a5ad01dff5c708cc8c02f628f58c863ec
Author: Brad Bell <bradbell@…>
Date: Wed Jan 14 16:11:25 2015 -0700


Remove ComOp?, replaced by specific cases Eq..Op, Ne..Op, Leq..Op, Lt..Op,
which should run faster.


forward0sweep.hpp: Remove out-dated comment about CompOp?.
forward1sweep.hpp: Remove out-dated comment about CompOp?.


commit 5bb0a70d1151e9086b88024050cea6cf11e83aa7
Author: Brad Bell <bradbell@…>
Date: Wed Jan 14 09:02:17 2015 -0700


Use Eq..Op, Ne..Op to implement == and !=.


commit 0ebeec61bc040a00c50db41ca5da31fb87194f93
Author: Brad Bell <bradbell@…>
Date: Wed Jan 14 07:19:26 2015 -0700


compare.hpp: Finish folding < <= > >= into Lt..Op and Leq..Op.


commit c949e5b72158b98cbab61c8aea98e76008e9c2f4
Author: Brad Bell <bradbell@…>
Date: Wed Jan 14 06:28:41 2015 -0700


  1. Change plan to fold all compare operations into Leq..Op and Lt..Op cases.
  2. Fix alphabetical order between Ld and Leq.


commit 6ffee88b68b682359d62bc75a8c2ba3e28d012ac
Author: Brad Bell <bradbell@…>
Date: Tue Jan 13 22:40:18 2015 -0700


Splite parameter <= variable and parameter > variable as separate compare
operators.


commit 0841014db4ead690d1c2358f5e09494030ae1e5f
Author: Brad Bell <bradbell@…>
Date: Tue Jan 13 21:12:57 2015 -0700


Attempting to recording and playback of compare operators faster:
Split variable <= variable and variable > variable out as separate cases.


compare.hpp: remove white space at end of lines.


commit cfd3afceebd4b3383b3042cbca98caf82ff77670
Author: Brad Bell <bradbell@…>
Date: Tue Jan 13 08:39:12 2015 -0700


speed_branch.sh: compare speed_cppad between two git branches.
speed_new.sh: correct list of options, remove unused variable.
wish_list.omh: correct discussion of effect of keeping compare operators.


commit 9ed03e1ee2c258ca38561ad083067e235d032e14
Author: Brad Bell <bradbell@…>
Date: Tue Jan 13 05:54:30 2015 -0700


Change test so it corresponds to optimization keeping compare operators.


commit 5c418d477d58984b094bba027ebb0794e759e557
Author: Brad Bell <bradbell@…>
Date: Tue Jan 13 05:12:50 2015 -0700


Include example and test of using CompareChange? with optimized tape.


commit c1b48edfa56ca096ce8c2db1dbadb2658cb13fe3
Author: Brad Bell <bradbell@…>
Date: Tue Jan 13 04:24:54 2015 -0700


Fix optimization so variables used in compare opertions are alwasy available.


commit 6fe607ca30db07b356fd3a9fe9779fa2dfd382d8
Author: Brad Bell <bradbell@…>
Date: Mon Jan 12 07:56:41 2015 -0700


Keep CompareChange? in optimized versions of tape and when NDEBUG is defined.

commit b4c0e51489cc0878499a331b4af4875b2781d8d8
Author: Brad Bell <bradbell@…>
Date: Sun Jan 11 17:01:59 2015 -0700

new_branch.sh: final procedure (only hand tested).

  • Property svn:keywords set to Id
File size: 9.2 KB
Line 
1/* $Id: dependent.hpp 3607 2015-01-20 16:20:41Z bradbell $ */
2# ifndef CPPAD_DEPENDENT_INCLUDED
3# define CPPAD_DEPENDENT_INCLUDED
4
5/* --------------------------------------------------------------------------
6CppAD: C++ Algorithmic Differentiation: Copyright (C) 2003-15 Bradley M. Bell
7
8CppAD is distributed under multiple licenses. This distribution is under
9the terms of the
10                    Eclipse Public License Version 1.0.
11
12A copy of this license is included in the COPYING file of this distribution.
13Please 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$$
41Stop recording and the AD of $icode Base$$
42$cref/operation sequence/glossary/Operation/Sequence/$$
43that started with the call
44$codei%
45        Independent(%x%)
46%$$
47and store the operation sequence in $icode f$$.
48The operation sequence defines an
49$cref/AD function/glossary/AD Function/$$
50$latex \[
51        F : B^n \rightarrow B^m
52\] $$
53where $latex B$$ is the space corresponding to objects of type $icode Base$$.
54The value $latex n$$ is the dimension of the
55$cref/domain/seq_property/Domain/$$ space for the operation sequence.
56The 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$$
61The object $icode f$$ has prototype
62$codei%
63        ADFun<%Base%> %f%
64%$$
65The AD of $icode Base$$ operation sequence is stored in $icode f$$; i.e.,
66it becomes the operation sequence corresponding to $icode f$$.
67If a previous operation sequence was stored in $icode f$$,
68it is deleted.
69
70$head x$$
71The argument $icode x$$
72must be the vector argument in a previous call to
73$cref Independent$$.
74Neither its size, or any of its values, are allowed to change
75between calling
76$codei%
77        Independent(%x%)
78%$$
79and
80$codei%
81        %f%.Dependent(%x%, %y%)
82%$$.
83
84$head y$$
85The vector $icode y$$ has prototype
86$codei%
87        const %ADvector% &%y%
88%$$
89(see $cref/ADvector/FunConstruct/$$ below).
90The length of $icode y$$ must be greater than zero
91and is the dimension of the range space for $icode f$$.
92
93$head ADvector$$
94The type $icode ADvector$$ must be a $cref SimpleVector$$ class with
95$cref/elements of type/SimpleVector/Elements of Specified Type/$$
96$codei%AD<%Base%>%$$.
97The routine $cref CheckSimpleVector$$ will generate an error message
98if this is not the case.
99
100$head Taping$$
101The tape,
102that was created when $codei%Independent(%x%)%$$ was called,
103will stop recording.
104The AD operation sequence will be transferred from
105the tape to the object $icode f$$ and the tape will then be deleted.
106
107$head Forward$$
108No $cref Forward$$ calculation is preformed during this operation.
109Thus, directly after this operation,
110$codei%
111        %f%.size_order()
112%$$
113is zero (see $cref size_order$$).
114
115$head Parallel Mode$$
116$index parallel, Dependent$$
117$index Dependent, parallel$$
118The call to $code Independent$$,
119and the corresponding call to
120$codei%
121        ADFun<%Base%> %f%( %x%, %y%)
122%$$
123or
124$codei%
125        %f%.Dependent( %x%, %y%)
126%$$
127or $cref abort_recording$$,
128must 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$$
132The file
133$cref fun_check.cpp$$
134contains an example and test of this operation.
135It returns true if it succeeds and false otherwise.
136
137$end
138----------------------------------------------------------------------------
139*/
140
141
142// BEGIN CppAD namespace
143namespace CppAD {
144
145/*!
146\file dependent.hpp
147Different versions of Dependent function.
148*/
149
150/*!
151Determine 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]
155The dependent variable vector for the corresponding function.
156*/
157template <typename Base>
158template <typename ADvector>
159void 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/*!
173Determine 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]
177The independent variable vector for this tape. This informaiton is
178also 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]
182The dependent variable vector for the corresponding function.
183*/
184template <typename Base>
185template <typename ADvector>
186void 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/*!
227Replace the floationg point operations sequence for this function object.
228
229\param tape
230is a tape that contains the new floating point operation sequence
231for this function.
232After this operation, all memory allocated for this tape is deleted.
233
234\param y
235The dependent variable vector for the function being stored in this object.
236
237\par
238All of the private member data in ad_fun.hpp is set to correspond to the
239new tape except for check_for_nan_.
240*/
241
242template <typename Base>
243template <typename ADvector>
244void 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
Note: See TracBrowser for help on using the repository browser.