Sacado Package Browser (Single Doxygen Collection)  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
taylor_expr.cpp
Go to the documentation of this file.
1 // @HEADER
2 // *****************************************************************************
3 // Sacado Package
4 //
5 // Copyright 2006 NTESS and the Sacado contributors.
6 // SPDX-License-Identifier: LGPL-2.1-or-later
7 // *****************************************************************************
8 // @HEADER
9 
10 #include "Sacado_Random.hpp"
11 #include "Sacado_No_Kokkos.hpp"
13 
14 #include "Teuchos_Time.hpp"
16 
17 // ADOL-C includes
18 #ifdef HAVE_ADOLC
19 #ifdef PACKAGE
20 #undef PACKAGE
21 #endif
22 #ifdef PACKAGE_NAME
23 #undef PACKAGE_NAME
24 #endif
25 #ifdef PACKAGE_BUGREPORT
26 #undef PACKAGE_BUGREPORT
27 #endif
28 #ifdef PACKAGE_STRING
29 #undef PACKAGE_STRING
30 #endif
31 #ifdef PACKAGE_TARNAME
32 #undef PACKAGE_TARNAME
33 #endif
34 #ifdef PACKAGE_VERSION
35 #undef PACKAGE_VERSION
36 #endif
37 #ifdef VERSION
38 #undef VERSION
39 #endif
40 #include "adolc/adouble.h"
41 #include "adolc/interfaces.h"
42 #include "adolc/taping.h"
43 #endif
44 
45 using std::cout;
46 using std::endl;
47 
48 // A simple performance test that computes a Taylor expansion of a simple
49 // expression using many variants of Taylor polynomial classes.
50 
51 template <typename T>
52 inline void
53 func(const T& x1, const T& x2, T& y) {
54  y = x1*x2 + sin(x1)/x2;
55 }
56 
57 template <typename TaylorType>
58 double
59 do_time(int degree, int nloop)
60 {
61  TaylorType x1, x2, y;
62  Sacado::Random<double> urand(0.0, 1.0);
63 
64  x1 = TaylorType(degree, urand.number());
65  x2 = TaylorType(degree, urand.number());
66  y = 0.0;
67  for (int j=0; j<=degree; j++) {
68  x1.fastAccessCoeff(j) = urand.number();
69  x2.fastAccessCoeff(j) = urand.number();
70  }
71 
72  Teuchos::Time timer("mult", false);
73  timer.start(true);
74  for (int j=0; j<nloop; j++) {
75  func(x1, x2, y);
76  }
77  timer.stop();
78 
79  return timer.totalElapsedTime() / nloop;
80 }
81 
82 #ifdef HAVE_ADOLC
83 double
84 do_time_adolc(int degree, int nloop)
85 {
86  Sacado::Random<double> urand(0.0, 1.0);
87  double **X, **Y;
88 
89  X = new double*[2];
90  X[0] = new double[degree+1];
91  X[1] = new double[degree+1];
92  Y = new double*[1];
93  Y[0] = new double[degree+1];
94  for (int j=0; j<=degree; j++) {
95  X[0][j] = urand.number();
96  X[1][j] = urand.number();
97  Y[0][j] = 0.0;
98  }
99 
100  trace_on(0);
101  adouble x1, x2, y;
102  x1 <<= X[0][0];
103  x2 <<= X[1][0];
104  func(x1, x2, y);
105  y >>= Y[0][0];
106  trace_off();
107 
108  Teuchos::Time timer("mult", false);
109  timer.start(true);
110  for (int j=0; j<nloop; j++) {
111  forward(0,1,2,degree,0,X,Y);
112  }
113  timer.stop();
114 
115  delete [] X[1];
116  delete [] X[0];
117  delete [] X;
118 
119  delete [] Y[0];
120  delete [] Y;
121 
122  return timer.totalElapsedTime() / nloop;
123 }
124 
125 double
126 do_time_adolc_retape(int degree, int nloop)
127 {
128  Sacado::Random<double> urand(0.0, 1.0);
129  double **X, **Y;
130 
131  X = new double*[2];
132  X[0] = new double[degree+1];
133  X[1] = new double[degree+1];
134  Y = new double*[1];
135  Y[0] = new double[degree+1];
136  for (int j=0; j<=degree; j++) {
137  X[0][j] = urand.number();
138  X[1][j] = urand.number();
139  Y[0][j] = 0.0;
140  }
141  adouble x1, x2, y;
142 
143  Teuchos::Time timer("mult", false);
144  timer.start(true);
145  for (int j=0; j<nloop; j++) {
146  trace_on(0);
147  x1 <<= X[0][0];
148  x2 <<= X[1][0];
149  func(x1, x2, y);
150  y >>= Y[0][0];
151  trace_off();
152  forward(0,1,2,degree,0,X,Y);
153  }
154  timer.stop();
155 
156  delete [] X[1];
157  delete [] X[0];
158  delete [] X;
159 
160  delete [] Y[0];
161  delete [] Y;
162 
163  return timer.totalElapsedTime() / nloop;
164 }
165 #endif
166 
167 int main(int argc, char* argv[]) {
168  int ierr = 0;
169 
170  try {
171  double t;
172  int p = 2;
173  int w = p+7;
174 
175  // Set up command line options
177  clp.setDocString("This program tests the speed of various forward mode AD implementations for a single multiplication operation");
178  int degree = 10;
179  clp.setOption("degree", &degree, "Polynomial degree");
180  int nloop = 1000000;
181  clp.setOption("nloop", &nloop, "Number of loops");
182 
183  // Parse options
185  parseReturn= clp.parse(argc, argv);
187  return 1;
188 
189  std::cout.setf(std::ios::scientific);
190  std::cout.precision(p);
191  std::cout << "Times (sec) for degree = " << degree
192  << " nloop = " << nloop << ": " << std::endl;
193 
194  t = do_time< Sacado::Tay::Taylor<double> >(degree, nloop);
195  std::cout << "Taylor: " << std::setw(w) << t << std::endl;
196 
197  t = do_time< Sacado::Tay::CacheTaylor<double> >(degree, nloop);
198  std::cout << "CacheTaylor: " << std::setw(w) << t << std::endl;
199 
200 #ifdef HAVE_ADOLC
201  t = do_time_adolc(degree, nloop);
202  std::cout << "ADOL-C: " << std::setw(w) << t << std::endl;
203  t = do_time_adolc_retape(degree, nloop);
204  std::cout << "ADOL-C (retape): " << std::setw(w) << t << std::endl;
205 #endif
206 
207  }
208  catch (std::exception& e) {
209  cout << e.what() << endl;
210  ierr = 1;
211  }
212  catch (const char *s) {
213  cout << s << endl;
214  ierr = 1;
215  }
216  catch (...) {
217  cout << "Caught unknown exception!" << endl;
218  ierr = 1;
219  }
220 
221  return ierr;
222 }
const char * p
ScalarT number()
Get random number.
Sacado::Tay::Taylor< double > TaylorType
#define T
Definition: Sacado_rad.hpp:553
void start(bool reset=false)
T & fastAccessCoeff(int i)
Returns degree i term without bounds checking.
double stop()
void setOption(const char option_true[], const char option_false[], bool *option_val, const char documentation[]=NULL)
int main()
Definition: ad_example.cpp:171
EParseCommandLineReturn parse(int argc, char *argv[], std::ostream *errout=&std::cerr) const
sin(expr.val())
double do_time(int nderiv, int nloop)
Definition: fad_expr.cpp:48
void setDocString(const char doc_string[])
double totalElapsedTime(bool readCurrentTime=false) const
const T func(int n, T *x)
Definition: ad_example.cpp:29
const double y