source: stable/20170000/cppad/local/ad_tape.hpp @ 3866

Last change on this file since 3866 was 3866, checked in by bradbell, 3 years ago

merge to branch: stable/20170000
from repository: https://github.com/coin-or/CppAD
start hash code: be5942014af74c220dc1ed7ba397599cc48a7282
end hash code: 0201594a22c035232289215f8095c7ae308d194b

commit 0201594a22c035232289215f8095c7ae308d194b
Author: Brad Bell <bradbell@…>
Date: Thu Jan 19 04:59:07 2017 -0700

stable/20170000 branch:
package.sh: suppress runing doxygen; see d505fac90a987
ad_tape.hpp: fix problem with Visual C++2015; see issue 17.
check_copyright.sh: change date for changes from 2016 -> 2017.

  • Property svn:keywords set to Id
File size: 6.7 KB
Line 
1# ifndef CPPAD_LOCAL_AD_TAPE_HPP
2# define CPPAD_LOCAL_AD_TAPE_HPP
3
4/* --------------------------------------------------------------------------
5CppAD: C++ Algorithmic Differentiation: Copyright (C) 2003-17 Bradley M. Bell
6
7CppAD is distributed under multiple licenses. This distribution is under
8the terms of the
9                    Eclipse Public License Version 1.0.
10
11A copy of this license is included in the COPYING file of this distribution.
12Please visit http://www.coin-or.org/CppAD/ for information on other licenses.
13-------------------------------------------------------------------------- */
14# include <cppad/core/define.hpp>
15
16namespace CppAD { namespace local { // BEGIN_CPPAD_LOCAL__NAMESPACE
17
18/*!
19Class used to hold tape that records AD<Base> operations.
20
21\tparam Base
22An <tt>AD<Base></tt> object is used to recording <tt>AD<Base></tt> operations.
23*/
24
25template <class Base>
26class 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// --------------------------------------------------------------------------
92private:
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        */
101        tape_id_t                    id_;
102        /// Number of independent variables in this tapes reconding.
103        /// Set by Independent and effectively const
104        size_t         size_independent_;
105        /// This is where the information is recorded.
106        local::recorder<Base>              Rec_;
107        // ----------------------------------------------------------------------
108        // private functions
109        //
110        // add a parameter to the tape
111        size_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
129public:
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/*!
144Place a parameter in the tape.
145
146On rare occations it is necessary to place a parameter in the tape; e.g.,
147when it is one of the dependent variabes.
148
149\param z
150value of the parameter that we are placing in the tape.
151
152\return
153variable index (for this recording) correpsonding to the parameter.
154
155\par 2DO
156All these operates are preformed in \c Rec_, so we should
157move this routine from <tt>ADTape<Base></tt> to <tt>recorder<Base></tt>.
158*/
159template <class Base>
160size_t ADTape<Base>::RecordParOp(const Base &z)
161{       size_t z_taddr;
162        size_t ind;
163        CPPAD_ASSERT_UNKNOWN( NumRes(ParOp) == 1 );
164        CPPAD_ASSERT_UNKNOWN( NumArg(ParOp) == 1 );
165        z_taddr = Rec_.PutOp(ParOp);
166        ind     = Rec_.PutPar(z);
167        Rec_.PutArg(ind);
168
169        return z_taddr;
170}
171
172/*!
173Put initialization for a VecAD<Base> object in the tape.
174
175This routine should be called once for each VecAD object when just
176before it changes from a parameter to a variable.
177
178\param length
179size of the <tt>VecAD<Base></tt> object.
180
181\param data
182initial values for the <tt>VecAD<Base></tt> object
183(values before it becomes a variable).
184
185\return
186index of the start of this vector in the list of vector indices.
187The value for this vector index is the length of the vector.
188There are \c length indices following for this vector.
189The values for these vector indices are the corresponding
190parameter indices in the tape for the initial value of the corresponding
191vec_ad element.
192
193\par 2DO
194All these operates are preformed in \c Rec_, so we should
195move this routine from <tt>ADTape<Base></tt> to <tt>recorder<Base></tt>.
196*/
197template <class Base>
198size_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
Note: See TracBrowser for help on using the repository browser.