File: chkpoint_two.cpp

package info (click to toggle)
cppad 2026.00.00.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 11,584 kB
  • sloc: cpp: 112,960; sh: 6,146; ansic: 179; python: 71; sed: 12; makefile: 10
file content (387 lines) | stat: -rw-r--r-- 11,806 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later
// SPDX-FileCopyrightText: Bradley M. Bell <bradbell@seanet.com>
// SPDX-FileContributor: 2003-22 Bradley M. Bell
// ----------------------------------------------------------------------------

# include <cppad/cppad.hpp>

namespace {
   using CppAD::AD;
   using CppAD::vector;
   using CppAD::chkpoint_two;
   // -----------------------------------------------------------------------
   template <class Algo>
   chkpoint_two<double> checkpoint_two(
      std::string name ,
      Algo&      algo  ,
      vector< AD<double> >& ax ,
      vector< AD<double> >& ay )
   {  CppAD::Independent(ax);
      algo(ax, ay);
      CppAD::ADFun<double> g_fun(ax, ay);
      bool internal_bool = false;
      bool use_hes_sparsity = true;
      bool use_base2ad      = true;
      bool use_in_parallel  = false;
      return chkpoint_two<double>(g_fun, name,
         internal_bool, use_hes_sparsity, use_base2ad, use_in_parallel
      );
   }
   // ----------------------------------------------------------------
   // Test for bug where checkpoint function did not depend on
   // the operands in the logical comparison because of the CondExp
   // sparsity pattern.
   void j_algo(
      const CppAD::vector< CppAD::AD<double> >& ax ,
              CppAD::vector< CppAD::AD<double> >& ay )
   {  ay[0] = CondExpGt(ax[0], ax[1], ax[2], ax[3]); }

   bool test_one(void)
   {  bool ok = true;
      using CppAD::NearEqual;
      double eps10 = 10.0 * std::numeric_limits<double>::epsilon();

      // Create a checkpoint version of the function g
      vector< AD<double> > au(4), av(1);
      for(size_t i = 0; i < 4; i++)
         au[i] = AD<double>(i);
      chkpoint_two<double> j_check =
         checkpoint_two("j_check", j_algo, au, av);

      // independent variable vector
      vector< AD<double> > ax(2), ay(1);
      ax[0] = 1.;
      ax[1] = 1.;
      Independent(ax);

      // call atomic function that does not get used
      for(size_t i = 0; i < 4; i++)
         au[i] = ax[0] + AD<double>(i + 1) * ax[1];
      j_check(au, ay);

      // create function object f : ax -> ay
      CppAD::ADFun<double> f(ax, ay);


      // now optimize the operation sequence
      f.optimize();

      // check result where true case is used; i.e., au[0] > au[1]
      vector<double> x(2), y(1);
      x[0] = 1.;
      x[1] = -1;
      y    = f.Forward(0, x);
      ok &= NearEqual(y[0], x[0] + double(3) * x[1], eps10, eps10);


      // check result where false case is used; i.e., au[0] <= au[1]
      x[0] = 1.;
      x[1] = 1;
      y    = f.Forward(0, x);
      ok &= NearEqual(y[0], x[0] + double(4) * x[1], eps10, eps10);

      return ok;
   }
   // -------------------------------------------------------------------
   // Test conditional optimizing out call to an atomic function call
   void k_algo(
      const CppAD::vector< CppAD::AD<double> >& x ,
              CppAD::vector< CppAD::AD<double> >& y )
   {  y[0] = x[0] + x[1]; }

   void h_algo(
      const CppAD::vector< CppAD::AD<double> >& x ,
              CppAD::vector< CppAD::AD<double> >& y )
   {  y[0] = x[0] - x[1]; }

   bool test_two(void)
   {  bool ok = true;
      typedef vector< AD<double> > ADVector;
      using CppAD::NearEqual;
      double eps10 = 10.0 * std::numeric_limits<double>::epsilon();

      // Create a checkpoint version of the function g
      ADVector ax(2), ag(1), ah(1), ay(1);
      ax[0] = 0.;
      ax[1] = 1.;
      chkpoint_two<double> k_check =
         checkpoint_two("k_check", k_algo, ax, ag);
      chkpoint_two<double> h_check =
         checkpoint_two("h_check", h_algo, ax, ah);

      // independent variable vector
      Independent(ax);

      // atomic function calls that get conditionally used
      k_check(ax, ag);
      h_check(ax, ah);

      // conditional expression
      ay[0] = CondExpLt(ax[0], ax[1], ag[0], ah[0]);

      // create function object f : ax -> ay
      CppAD::ADFun<double> f;
      f.Dependent(ax, ay);

      // use zero order to evaluate when condition is true
      CppAD::vector<double>  x(2), dx(2);
      CppAD::vector<double>  y(1), dy(1), w(1);
      x[0] = 3.;
      x[1] = 4.;
      y    = f.Forward(0, x);
      ok &= NearEqual(y[0], x[0] + x[1], eps10, eps10);

      // before optimize
      ok  &= f.number_skip() == 0;

      // now optimize the operation sequence
      f.optimize();

      // optimized zero order forward when condition is false
      x[0] = 4.;
      x[1] = 3.;
      y    = f.Forward(0, x);
      ok &= NearEqual(y[0], x[0] - x[1], eps10, eps10);

      // after optimize can skip either call to g or call to h
      ok  &= f.number_skip() == 1;

      // optimized first order forward
      dx[0] = 2.;
      dx[1] = 1.;
      dy    = f.Forward(1, dx);
      ok &= NearEqual(dy[0], dx[0] - dx[1], eps10, eps10);

      // optimized first order reverse
      w[0]  = 1.;
      dx    = f.Reverse(1, w);
      ok &= NearEqual(dx[0], 1., eps10, eps10);
      ok &= NearEqual(dx[1], -1., eps10, eps10);

      return ok;
   }
   // -------------------------------------------------------------------
   // Test of optimizing out arguments to an atomic function
   void g_algo(
      const CppAD::vector< CppAD::AD<double> >& ax ,
              CppAD::vector< CppAD::AD<double> >& ay )
   {  ay = ax; }

   bool test_three(void)
   {  bool ok = true;
      using CppAD::NearEqual;
      double eps10 = 10.0 * std::numeric_limits<double>::epsilon();

      // Create a checkpoint version of the function g
      vector< AD<double> > ax(2), ay(2), az(1);
      ax[0] = 0.;
      ax[1] = 1.;
      chkpoint_two<double> g_check =
         checkpoint_two("g_check", g_algo, ax, ay);

      // independent variable vector
      Independent(ax);

      // call atomic function that does not get used
      g_check(ax, ay);

      // conditional expression
      az[0] = CondExpLt(ax[0], ax[1], ax[0] + ax[1], ax[0] - ax[1]);

      // create function object f : ax -> az
      CppAD::ADFun<double> f(ax, az);

      // number of variables before optimization
      // (include ay[0] and ay[1])
      size_t n_before = f.size_var();

      // now optimize the operation sequence
      f.optimize();

      // number of variables after optimization
      // (does not include ay[0] and ay[1])
      size_t n_after = f.size_var();
      ok            &= n_after + 2 == n_before;

      // check optimization works ok
      vector<double> x(2), z(1);
      x[0] = 4.;
      x[1] = 3.;
      z    = f.Forward(0, x);
      ok &= NearEqual(z[0], x[0] - x[1], eps10, eps10);

      return ok;
   }
   bool test_four(void)
   {  bool ok = true;
      using CppAD::NearEqual;
      double eps10 = 10.0 * std::numeric_limits<double>::epsilon();
      vector< AD<double> > au(2), aw(2), ax(2), ay(1);

      // create atomic function corresponding to g_algo
      au[0] = 1.0;
      au[1] = 2.0;
      chkpoint_two<double> g_check =
         checkpoint_two("g_algo", g_algo, au, ax);

      // start recording a new function
      CppAD::Independent(ax);

      // now use g_check during the recording
      au[0] = ax[0] + ax[1]; // this argument requires a new variable
      au[1] = ax[0] - ax[1]; // this argument also requires a new variable
      g_check(au, aw);

      // now create f(x) = x_0 + x_1
      ay[0] = aw[0];
      CppAD::ADFun<double> f(ax, ay);

      // number of variables before optimization
      // ax[0], ax[1], ax[0] + ax[1], ax[0] - ax[1], g[0], g[1]
      // and phantom variable at index 0
      size_t n_before = f.size_var();
      ok &= n_before == 7;

      // now optimize f so that the calculation of au[1] is removed
      f.optimize();

      // number of variables after optimization
      // ax[0], ax[1], ax[0] + ax[1], g[0]
      // and phantom variable at index 0
      size_t n_after = f.size_var();
      ok &= n_after == 5;

      // now compute and check a forward mode calculation
      vector<double> x(2), y(1);
      x[0] = 5.0;
      x[1] = 6.0;
      y    = f.Forward(0, x);
      ok &= NearEqual(y[0], x[0] + x[1], eps10, eps10);

      return ok;
   }
   // -----------------------------------------------------------------------
   void i_algo(
      const CppAD::vector< CppAD::AD<double> >& ax ,
              CppAD::vector< CppAD::AD<double> >& ay )
   {  ay[0] = 1.0 / ax[0]; }
   //
   // Test bug where atomic functions were not properly conditionally skipped.
   bool test_five(bool conditional_skip)
   {  bool ok = true;
      using CppAD::AD;
      using CppAD::NearEqual;
      double eps10 = 10.0 * std::numeric_limits<double>::epsilon();
      using CppAD::vector;

      // Create a checkpoint version of the function i_algo
      vector< AD<double> > au(1), av(1), aw(1);
      au[0] = 1.0;
      chkpoint_two<double> i_check =
         checkpoint_two("i_check", i_algo, au, av);

      // independent variable vector
      vector< AD<double> > ax(2), ay(1);
      ax[0] = 1.0;
      ax[1] = 2.0;
      Independent(ax);

      // call atomic function that does not get used
      au[0] = ax[0];
      i_check(au, av);
      au[0] = ax[1];
      i_check(au, aw);
      AD<double> zero = 0.0;
      ay[0] = CondExpGt(av[0], zero, av[0], aw[0]);

      // create function object f : ax -> ay
      CppAD::ADFun<double> f(ax, ay);

      // run case that skips the second call to afun
      // (can use trace in sweep/forward_0.hpp to see this).
      vector<double> x(2), y_before(1), y_after(1);
      x[0]      = 1.0;
      x[1]      = 2.0;
      y_before  = f.Forward(0, x);
      if( conditional_skip )
         f.optimize();
      else
         f.optimize("no_conditional_skip");
      y_after   = f.Forward(0, x);

      ok &= NearEqual(y_before[0], y_after[0], eps10, eps10);

      return ok;
   }
   // -----------------------------------------------------------------------
   void m_algo(
      const CppAD::vector< CppAD::AD<double> >& ax ,
              CppAD::vector< CppAD::AD<double> >& ay )
   {  ay[0] = 0.0;
      for(size_t j = 0; j < ax.size(); ++j)
         ay[0] += ax[j] * ax[j];
   }
   //
   // Test bug where select_y[i] should be select_x[i]
   bool test_six(void)
   {  bool ok = true;
      using CppAD::AD;
      using CppAD::NearEqual;
      using CppAD::vector;

      // Create a checkpoint version of the function m_algo
      size_t n = 3, m = 1;
      vector< AD<double> > ax(n), ay(m);
      for(size_t j = 0; j < n; ++j)
         ax[j] = 1.0;
      chkpoint_two<double> m_check =
         checkpoint_two("m_check", m_algo, ax, ay);

      // independent variable vector
      Independent(ax);

      // call atomic function that does not get used
      m_check(ax, ay);

      // create function object f : ax -> ay
      CppAD::ADFun<double> f(ax, ay);

      // Evaluate Hessian sparsity
      vector<bool> select_domain(n), select_range(m);
      select_range[0] = true;
      for(size_t j = 0; j < n; ++j)
         select_domain[j] = true;
      bool internal_bool = true;
      CppAD::sparse_rc< vector<size_t> > pattern_out;
      //
      f.for_hes_sparsity(
         select_domain, select_range, internal_bool, pattern_out
      );
      size_t nnz = pattern_out.nnz();
      const vector<size_t>& row( pattern_out.row() );
      const vector<size_t>& col( pattern_out.col() );
      vector<size_t> row_major = pattern_out.row_major();
      //
      ok &= nnz == n;
      for(size_t k = 0; k < nnz; ++k)
      {  ok &= row[ row_major[k] ] == k;
         ok &= col[ row_major[k] ] == k;
      }
      return ok;
   }

}
bool chkpoint_two(void)
{  bool ok = true;
   //
   ok  &= test_one();
   ok  &= test_two();
   ok  &= test_three();
   ok  &= test_four();
   ok  &= test_five(true);
   ok  &= test_five(false);
   ok  &= test_six();
   //
   return ok;
}
// END C++