1 /* --------------------------------------------------------------------------
2 CppAD: C++ Algorithmic Differentiation: Copyright (C) 2003-20 Bradley M. Bell
3 
4 CppAD is distributed under the terms of the
5              Eclipse Public License Version 2.0.
6 
7 This Source Code may also be made available under the following
8 Secondary License when the conditions for such availability set forth
9 in the Eclipse Public License, Version 2.0 are satisfied:
10       GNU General Public License, Version 2.0 or later.
11 ---------------------------------------------------------------------------- */
12 
13 /*
14 $begin capacity_order.cpp$$
15 $spell
16     Taylor
17 $$
18 
19 $section Controlling Taylor Coefficient Memory Allocation: Example and Test$$
20 
21 
22 $srcthisfile%0%// BEGIN C++%// END C++%1%$$
23 
24 $end
25 */
26 // BEGIN C++
27 # include <cppad/cppad.hpp>
28 
29 namespace {
test(void)30     bool test(void)
31     {   bool ok = true;
32         using CppAD::AD;
33         using CppAD::NearEqual;
34         using CppAD::thread_alloc;
35 
36         // domain space vector
37         size_t n(1), m(1);
38         CPPAD_TESTVECTOR(AD<double>) ax(n), ay(n);
39 
40         // declare independent variables and start tape recording
41         ax[0]  = 1.0;
42         CppAD::Independent(ax);
43 
44         // Set y = x^3, use enough variables so more that the minimal amount
45         // of memory is allocated for Taylor coefficients
46         ay[0] = 0.;
47         for( size_t i = 0; i < 10; i++)
48             ay[0] += ax[0] * ax[0] * ax[0];
49         ay[0] = ay[0] / 10.;
50 
51         // create f: x -> y and stop tape recording
52         // (without running zero order forward mode).
53         CppAD::ADFun<double> f;
54         f.Dependent(ax, ay);
55 
56         // check that this is master thread
57         size_t thread = thread_alloc::thread_num();
58         ok           &= thread == 0; // this should be master thread
59 
60         // The highest order forward mode calculation below is first order.
61         // This corresponds to two Taylor coefficient per variable,direction
62         // (orders zero and one). Preallocate memory for speed.
63         size_t inuse  = thread_alloc::inuse(thread);
64         f.capacity_order(2);
65         ok &= thread_alloc::inuse(thread) > inuse;
66 
67         // zero order forward mode
68         CPPAD_TESTVECTOR(double) x(n), y(m);
69         x[0] = 0.5;
70         y    = f.Forward(0, x);
71         double eps = 10. * CppAD::numeric_limits<double>::epsilon();
72         ok  &= NearEqual(y[0], x[0] * x[0] * x[0], eps, eps);
73 
74         // forward computation of partials w.r.t. x
75         CPPAD_TESTVECTOR(double) dx(n), dy(m);
76         dx[0] = 1.;
77         dy    = f.Forward(1, dx);
78         ok   &= NearEqual(dy[0], 3. * x[0] * x[0], eps, eps);
79 
80         // Suppose we no longer need the first order Taylor coefficients.
81         inuse = thread_alloc::inuse(thread);
82         f.capacity_order(1); // just keep zero order coefficients
83         ok   &= thread_alloc::inuse(thread) < inuse;
84 
85         // Suppose we no longer need the zero order Taylor coefficients
86         // (could have done this first and not used f.capacity_order(1)).
87         inuse = thread_alloc::inuse(thread);
88         f.capacity_order(0);
89         ok   &= thread_alloc::inuse(thread) < inuse;
90 
91         // turn off memory holding
92         thread_alloc::hold_memory(false);
93 
94         return ok;
95     }
96 }
capacity_order(void)97 bool capacity_order(void)
98 {   bool ok = true;
99     using CppAD::thread_alloc;
100 
101     // original amount of memory inuse
102     size_t thread = thread_alloc::thread_num();
103     ok           &= thread == 0; // this should be master thread
104     size_t inuse  = thread_alloc::inuse(thread);
105 
106     // do test in separate routine so all objects are destroyed
107     ok &= test();
108 
109     // check that the amount of memroy inuse has not changed
110     ok &= thread_alloc::inuse(thread) == inuse;
111 
112     // Test above uses hold_memory, so return available memory
113     thread_alloc::free_available(thread);
114 
115     return ok;
116 }
117 
118 // END C++
119