NeoPZ
benchadolc.cc
Go to the documentation of this file.
1 // Emacs will be in -*- Mode: c++ -*-
2 //
3 // ********** DO NOT REMOVE THIS BANNER **********
4 //
5 // Nicolas Di Cesare <Nicolas.Dicesare@ann.jussieu.fr>
6 // http://www.ann.jussieu.fr/~dicesare
7 //
8 // Benchmark between ADOL-C and Fad
9 // in forward mode
10 //
11 //********************************************************
12 //
13 // KCC 3.3f (kai C++) doesn't compile ADOL-C
14 // egcs 1.1.2 (cygnus) is OK.
15 //
16 //********************************************************
17 
18 // C++ includes
19 #include <iomanip>
20 
21 // C include
22 #include <cmath>
23 
24 // ADOL-C includes
25 #include <adouble.h> // use of active double and taping
26 #include <DRIVERS/drivers.h> // use of "Easy To Use" drivers
27  // gradient(.) and hessian(.)
28 // Fad include
29 #include <Fad/fad.h>
30 
31 // vector and timer includes
32 #include <utils/timer.h>
33 #include <utils/vectors.h>
34 
35 
36 
37 double testADOLC(Timer& timer, const int n, const int nloop, double & yp, double& errg);
38 double testFAD(Timer& timer, const int n, const int nloop, double & yp, double& errg);
39 
40 /****************************************************************************/
41 /* MAIN PROGRAM */
42 int main()
43 {
44 
45  //int n,k;
46  const int nloop = 10000;
47  const int nbench = 3;
48 
49  double errg1 = 0., errg2 = 0.;
50  double yp1 = 0.0, yp2 = 0.;
51  double time1 = 0., time2 = 0.;
52 
53 
54  Timer timer(123123000.12);
55 
56  for (int n=1; n<100; ++n) {
57 
58  //clear cache
59  testFAD(timer, n, nloop, yp1, errg1);
60  testADOLC(timer, n, nloop, yp2, errg2);
61 
62  time1 = time2 = 0.;
63  for (int j=0; j<nbench; ++j)
64  {
65  errg1 = errg2 = 0.;
66  yp1 = yp2 = 0.;
67  time1 += testFAD(timer, n, nloop, yp1, errg1);
68  time2 += testADOLC(timer, n, nloop, yp2, errg2);
69 
70  }
71 
72  cout.setf(ios::fixed,ios::floatfield);
73  cout << setw(4) << n << setw(12) << time1/(double)nbench << setw(12) << time2/(double)nbench << endl;
74 
75  }
76 
77 
78  return 1;
79 } // end main
80 
81 
82 double testADOLC(Timer& timer, const int n, const int nloop, double & yp, double & errg)
83 {
84  double mtime;
85  int i,k;
86  double *xp = new double[n];
87  adouble *x = new adouble[n]; // or: adoublev x(n);
88  adouble y = 1., tmp;
89  double* g = new double[n];
90 
91  for(i=0; i<n; i++)
92  xp[i] = (i+1.0)/(2.0+i); // some initialization
93 
94  timer.start();
95  // repeat nloop time the loop to obtain
96  // a mesurable time
97  for (k=0; k<nloop; ++k) {
98 
99  trace_on(1); // tag = 1, keep = 0 by default
100 
101  y = 1.;
102  for(i=0; i<n; i++){
103  tmp <<= xp[i];
104  y = y+tmp+tmp+tmp+tmp;// y = y*x[i]*x[i]+x[i]+x[i];
105  }
106 
107  y >>= yp;
108  trace_off();
109 
110  gradient(1,n,xp,g); // gradient evaluation
111  }
112 
113  timer.stop();
114  mtime = timer.elapsedSeconds();
115 
116  for(i=0; i<n; i++)
117  errg += g[i]; // vanishes analytically.
118 
119  delete[] x; // not needed if x adoublev
120  delete [] g;
121  delete [] xp;
122 
123  return mtime;
124 }
125 
126 
127 double testFAD(Timer& timer, const int n, const int nloop, double & yp, double& errg)
128 {
129  int i,k;
130  double mtime;
131  double *xp = new double[n];
132  Fad<double> *x = new Fad<double>[n]; // or: adoublev x(n);
133  Fad<double> y(n,1.), tmp;
134 
135  for(i=0; i<n; i++)
136  xp[i] = (i+1.0)/(2.0+i); // some initialization
137 
138  timer.start();
139  // repeat nloop time the loop to obtain
140  // a mesurable time
141  for (k=0; k<nloop; ++k) {
142 
143  y = 1.;
144  for(i=0; i<n; i++)
145  {
146  tmp = Fad<double>(n, i, xp[i]);
147  y = y+tmp+tmp+tmp+tmp;// y = y*x[i]*x[i]+x[i]+x[i];
148  } // end for
149 
150  yp = y.val();
151  }
152  timer.stop();
153  mtime = timer.elapsedSeconds();
154 
155 
156  for(i=0; i<n; i++)
157  errg += y.d(i); // vanishes analytically.
158 
159  delete [] x; // not needed if x adoublev
160  delete [] xp;
161 
162  return mtime;
163 }
Definition: timer.h:10
void stop()
Definition: timer.h:23
void start()
Definition: timer.h:17
Definition: fad.h:54
AutoPointerMutexArrayInit tmp
double elapsedSeconds()
Definition: timer.h:29
clarg::argInt nloop("-l", "Number of loop iterations of the Subst_Backward/Subst_Forward", 1)
double testADOLC(Timer &timer, const int n, const int nloop, double &yp, double &errg)
Definition: benchadolc.cc:82
int main()
Definition: benchadolc.cc:42
double testFAD(Timer &timer, const int n, const int nloop, double &yp, double &errg)
Definition: benchadolc.cc:127