Sacado Package Browser (Single Doxygen Collection)  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
fad_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_No_Kokkos.hpp"
11 #include "Sacado_Random.hpp"
12 #include "Sacado_Fad_SimpleFad.hpp"
13 
14 #include "Fad/fad.h"
15 #include "TinyFadET/tfad.h"
16 
17 #include "Teuchos_Time.hpp"
19 
20 // A simple performance test that computes the derivative of a simple
21 // expression using many variants of Fad.
22 
23 void FAD::error(const char *msg) {
24  std::cout << msg << std::endl;
25 }
26 
27 template <typename T>
28 inline void
29 func1(const T& x1, const T& x2, T& y) {
30  y = (x1*x2 + sin(x1)/x2);
31 }
32 
33 inline void
34 func1_and_deriv(int n, double x1, double x2, double* x1dot, double* x2dot,
35  double& y, double* ydot) {
36  double s = sin(x1);
37  double c = cos(x1);
38  double t = s/x2;
39  double t1 = x2 + c/x2;
40  double t2 = x1 - t/x2;
41  y = x1*x2 + t;
42  for (int i=0; i<10; i++)
43  ydot[i] = t1*x1dot[i] + t2*x2dot[i];
44 }
45 
46 template <typename FadType>
47 double
48 do_time(int nderiv, int nloop)
49 {
50  FadType x1, x2, y;
51  Sacado::Random<double> urand(0.0, 1.0);
52 
53  x1 = FadType(nderiv, urand.number());
54  x2 = FadType(nderiv, urand.number());
55  y = 0.0;
56  for (int j=0; j<nderiv; j++) {
57  x1.fastAccessDx(j) = urand.number();
58  x2.fastAccessDx(j) = urand.number();
59  }
60 
61  Teuchos::Time timer("mult", false);
62  timer.start(true);
63  for (int j=0; j<nloop; j++) {
64  func1(x1, x2, y);
65  }
66  timer.stop();
67 
68  return timer.totalElapsedTime() / nloop;
69 }
70 
71 double
72 do_time_analytic(int nderiv, int nloop)
73 {
74  double x1, x2, y;
75  double *x1dot, *x2dot, *ydot;
76  Sacado::Random<double> urand(0.0, 1.0);
77 
78  x1 = urand.number();
79  x2 = urand.number();
80  y = 0.0;
81  x1dot = new double[nderiv];
82  x2dot = new double[nderiv];
83  ydot = new double[nderiv];
84  for (int j=0; j<nderiv; j++) {
85  x1dot[j] = urand.number();
86  x2dot[j] = urand.number();
87  }
88 
89  Teuchos::Time timer("mult", false);
90  timer.start(true);
91  for (int j=0; j<nloop; j++) {
92  func1_and_deriv(nderiv, x1, x2, x1dot, x2dot, y, ydot);
93  }
94  timer.stop();
95 
96  return timer.totalElapsedTime() / nloop;
97 }
98 
99 int main(int argc, char* argv[]) {
100  int ierr = 0;
101 
102  try {
103  double t, ta;
104  int p = 2;
105  int w = p+7;
106 
107  // Set up command line options
109  clp.setDocString("This program tests the speed of various forward mode AD implementations for a single multiplication operation");
110  int nderiv = 10;
111  clp.setOption("nderiv", &nderiv, "Number of derivative components");
112  int nloop = 1000000;
113  clp.setOption("nloop", &nloop, "Number of loops");
114 
115  // Parse options
117  parseReturn= clp.parse(argc, argv);
119  return 1;
120 
121  std::cout.setf(std::ios::scientific);
122  std::cout.precision(p);
123  std::cout << "Times (sec) for nderiv = " << nderiv
124  << " nloop = " << nloop << ": " << std::endl;
125 
126  ta = do_time_analytic(nderiv, nloop);
127  std::cout << "Analytic: " << std::setw(w) << ta << std::endl;
128 
129  t = do_time< Sacado::Fad::SimpleFad<double> >(nderiv, nloop);
130  std::cout << "SimpleFad: " << std::setw(w) << t << "\t" << std::setw(w) << t/ta << std::endl;
131 
132  t = do_time< FAD::TFad<10,double> >(nderiv, nloop);
133  std::cout << "TFad: " << std::setw(w) << t << "\t" << std::setw(w) << t/ta << std::endl;
134 
135  t = do_time< FAD::Fad<double> >(nderiv, nloop);
136  std::cout << "Fad: " << std::setw(w) << t << "\t" << std::setw(w) << t/ta << std::endl;
137 
138  t = do_time< Sacado::Fad::SFad<double,10> >(nderiv, nloop);
139  std::cout << "SFad: " << std::setw(w) << t << "\t" << std::setw(w) << t/ta << std::endl;
140 
141  t = do_time< Sacado::Fad::SLFad<double,10> >(nderiv, nloop);
142  std::cout << "SLFad: " << std::setw(w) << t << "\t" << std::setw(w) << t/ta << std::endl;
143 
144  t = do_time< Sacado::Fad::DFad<double> >(nderiv, nloop);
145  std::cout << "DFad: " << std::setw(w) << t << "\t" << std::setw(w) << t/ta << std::endl;
146 
147  t = do_time< Sacado::ELRFad::SFad<double,10> >(nderiv, nloop);
148  std::cout << "ELRSFad: " << std::setw(w) << t << "\t" << std::setw(w) << t/ta << std::endl;
149 
150  t = do_time< Sacado::ELRFad::SLFad<double,10> >(nderiv, nloop);
151  std::cout << "ELRSLFad: " << std::setw(w) << t << "\t" << std::setw(w) << t/ta << std::endl;
152 
153  t = do_time< Sacado::ELRFad::DFad<double> >(nderiv, nloop);
154  std::cout << "ELRDFad: " << std::setw(w) << t << "\t" << std::setw(w) << t/ta << std::endl;
155 
156  t = do_time< Sacado::CacheFad::DFad<double> >(nderiv, nloop);
157  std::cout << "CacheFad: " << std::setw(w) << t << "\t" << std::setw(w) << t/ta << std::endl;
158 
159  t = do_time< Sacado::Fad::DVFad<double> >(nderiv, nloop);
160  std::cout << "DVFad: " << std::setw(w) << t << "\t" << std::setw(w) << t/ta << std::endl;
161 
162  }
163  catch (std::exception& e) {
164  std::cout << e.what() << std::endl;
165  ierr = 1;
166  }
167  catch (const char *s) {
168  std::cout << s << std::endl;
169  ierr = 1;
170  }
171  catch (...) {
172  std::cout << "Caught unknown exception!" << std::endl;
173  ierr = 1;
174  }
175 
176  return ierr;
177 }
const char * p
double do_time_analytic(int nderiv, int nloop)
Definition: fad_expr.cpp:72
Sacado::Fad::DFad< double > FadType
void func1_and_deriv(int n, double x1, double x2, double *x1dot, double *x2dot, double &y, double *ydot)
Definition: fad_expr.cpp:34
ScalarT number()
Get random number.
#define T
Definition: Sacado_rad.hpp:553
void start(bool reset=false)
expr expr1 expr1 expr1 c expr2 expr1 expr2 expr1 expr2 expr1 expr1 expr1 expr1 c expr2 expr1 expr2 expr1 expr2 expr1 expr1 expr1 expr1 c *expr2 expr1 expr2 expr1 expr2 expr1 expr1 expr1 expr1 c expr2 expr1 expr2 expr1 expr2 expr1 expr1 expr1 expr2 expr1 expr2 expr1 expr1 expr1 expr2 expr1 expr2 expr1 expr1 expr1 c
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
void func1(const T &x1, const T &x2, T &y)
Definition: fad_expr.cpp:29
cos(expr.val())
const double y