Sacado Package Browser (Single Doxygen Collection)  Version of the Day
fad_expr.cpp
Go to the documentation of this file.
1 // $Id$
2 // $Source$
3 // @HEADER
4 // ***********************************************************************
5 //
6 // Sacado Package
7 // Copyright (2006) Sandia Corporation
8 //
9 // Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
10 // the U.S. Government retains certain rights in this software.
11 //
12 // This library is free software; you can redistribute it and/or modify
13 // it under the terms of the GNU Lesser General Public License as
14 // published by the Free Software Foundation; either version 2.1 of the
15 // License, or (at your option) any later version.
16 //
17 // This library is distributed in the hope that it will be useful, but
18 // WITHOUT ANY WARRANTY; without even the implied warranty of
19 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 // Lesser General Public License for more details.
21 //
22 // You should have received a copy of the GNU Lesser General Public
23 // License along with this library; if not, write to the Free Software
24 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
25 // USA
26 // Questions? Contact David M. Gay (dmgay@sandia.gov) or Eric T. Phipps
27 // (etphipp@sandia.gov).
28 //
29 // ***********************************************************************
30 // @HEADER
31 
32 #include "Sacado_No_Kokkos.hpp"
33 #include "Sacado_Random.hpp"
34 #include "Sacado_CacheFad_DFad.hpp"
35 #include "Sacado_Fad_SimpleFad.hpp"
36 
37 #include "Fad/fad.h"
38 #include "TinyFadET/tfad.h"
39 
40 #include "Teuchos_Time.hpp"
42 
43 // A simple performance test that computes the derivative of a simple
44 // expression using many variants of Fad.
45 
46 template <>
48 
49 void FAD::error(const char *msg) {
50  std::cout << msg << std::endl;
51 }
52 
53 template <typename T>
54 inline void
55 func1(const T& x1, const T& x2, T& y) {
56  y = (x1*x2 + sin(x1)/x2);
57 }
58 
59 inline void
60 func1_and_deriv(int n, double x1, double x2, double* x1dot, double* x2dot,
61  double& y, double* ydot) {
62  double s = sin(x1);
63  double c = cos(x1);
64  double t = s/x2;
65  double t1 = x2 + c/x2;
66  double t2 = x1 - t/x2;
67  y = x1*x2 + t;
68  for (int i=0; i<10; i++)
69  ydot[i] = t1*x1dot[i] + t2*x2dot[i];
70 }
71 
72 template <typename FadType>
73 double
74 do_time(int nderiv, int nloop)
75 {
76  FadType x1, x2, y;
77  Sacado::Random<double> urand(0.0, 1.0);
78 
79  x1 = FadType(nderiv, urand.number());
80  x2 = FadType(nderiv, urand.number());
81  y = 0.0;
82  for (int j=0; j<nderiv; j++) {
83  x1.fastAccessDx(j) = urand.number();
84  x2.fastAccessDx(j) = urand.number();
85  }
86 
87  Teuchos::Time timer("mult", false);
88  timer.start(true);
89  for (int j=0; j<nloop; j++) {
90  func1(x1, x2, y);
91  }
92  timer.stop();
93 
94  return timer.totalElapsedTime() / nloop;
95 }
96 
97 double
98 do_time_analytic(int nderiv, int nloop)
99 {
100  double x1, x2, y;
101  double *x1dot, *x2dot, *ydot;
102  Sacado::Random<double> urand(0.0, 1.0);
103 
104  x1 = urand.number();
105  x2 = urand.number();
106  y = 0.0;
107  x1dot = new double[nderiv];
108  x2dot = new double[nderiv];
109  ydot = new double[nderiv];
110  for (int j=0; j<nderiv; j++) {
111  x1dot[j] = urand.number();
112  x2dot[j] = urand.number();
113  }
114 
115  Teuchos::Time timer("mult", false);
116  timer.start(true);
117  for (int j=0; j<nloop; j++) {
118  func1_and_deriv(nderiv, x1, x2, x1dot, x2dot, y, ydot);
119  }
120  timer.stop();
121 
122  return timer.totalElapsedTime() / nloop;
123 }
124 
125 int main(int argc, char* argv[]) {
126  int ierr = 0;
127 
128  try {
129  double t, ta;
130  int p = 2;
131  int w = p+7;
132 
133  // Set up command line options
135  clp.setDocString("This program tests the speed of various forward mode AD implementations for a single multiplication operation");
136  int nderiv = 10;
137  clp.setOption("nderiv", &nderiv, "Number of derivative components");
138  int nloop = 1000000;
139  clp.setOption("nloop", &nloop, "Number of loops");
140 
141  // Parse options
143  parseReturn= clp.parse(argc, argv);
145  return 1;
146 
147  // Memory pool & manager
148  Sacado::Fad::MemPoolManager<double> poolManager(10);
149  Sacado::Fad::MemPool* pool = poolManager.getMemoryPool(nderiv);
151 
152  std::cout.setf(std::ios::scientific);
153  std::cout.precision(p);
154  std::cout << "Times (sec) for nderiv = " << nderiv
155  << " nloop = " << nloop << ": " << std::endl;
156 
157  ta = do_time_analytic(nderiv, nloop);
158  std::cout << "Analytic: " << std::setw(w) << ta << std::endl;
159 
160  t = do_time< Sacado::Fad::SimpleFad<double> >(nderiv, nloop);
161  std::cout << "SimpleFad: " << std::setw(w) << t << "\t" << std::setw(w) << t/ta << std::endl;
162 
163  t = do_time< FAD::TFad<10,double> >(nderiv, nloop);
164  std::cout << "TFad: " << std::setw(w) << t << "\t" << std::setw(w) << t/ta << std::endl;
165 
166  t = do_time< FAD::Fad<double> >(nderiv, nloop);
167  std::cout << "Fad: " << std::setw(w) << t << "\t" << std::setw(w) << t/ta << std::endl;
168 
169  t = do_time< Sacado::Fad::SFad<double,10> >(nderiv, nloop);
170  std::cout << "SFad: " << std::setw(w) << t << "\t" << std::setw(w) << t/ta << std::endl;
171 
172  t = do_time< Sacado::Fad::SLFad<double,10> >(nderiv, nloop);
173  std::cout << "SLFad: " << std::setw(w) << t << "\t" << std::setw(w) << t/ta << std::endl;
174 
175  t = do_time< Sacado::Fad::DFad<double> >(nderiv, nloop);
176  std::cout << "DFad: " << std::setw(w) << t << "\t" << std::setw(w) << t/ta << std::endl;
177 
178  t = do_time< Sacado::Fad::DMFad<double> >(nderiv, nloop);
179  std::cout << "DMFad: " << std::setw(w) << t << "\t" << std::setw(w) << t/ta << std::endl;
180 
181  t = do_time< Sacado::ELRFad::SFad<double,10> >(nderiv, nloop);
182  std::cout << "ELRSFad: " << std::setw(w) << t << "\t" << std::setw(w) << t/ta << std::endl;
183 
184  t = do_time< Sacado::ELRFad::SLFad<double,10> >(nderiv, nloop);
185  std::cout << "ELRSLFad: " << std::setw(w) << t << "\t" << std::setw(w) << t/ta << std::endl;
186 
187  t = do_time< Sacado::ELRFad::DFad<double> >(nderiv, nloop);
188  std::cout << "ELRDFad: " << std::setw(w) << t << "\t" << std::setw(w) << t/ta << std::endl;
189 
190  t = do_time< Sacado::CacheFad::DFad<double> >(nderiv, nloop);
191  std::cout << "CacheFad: " << std::setw(w) << t << "\t" << std::setw(w) << t/ta << std::endl;
192 
193  t = do_time< Sacado::Fad::DVFad<double> >(nderiv, nloop);
194  std::cout << "DVFad: " << std::setw(w) << t << "\t" << std::setw(w) << t/ta << std::endl;
195 
196  }
197  catch (std::exception& e) {
198  std::cout << e.what() << std::endl;
199  ierr = 1;
200  }
201  catch (const char *s) {
202  std::cout << s << std::endl;
203  ierr = 1;
204  }
205  catch (...) {
206  std::cout << "Caught unknown exception!" << std::endl;
207  ierr = 1;
208  }
209 
210  return ierr;
211 }
MemPool * getMemoryPool(unsigned int dim)
Get memory pool for supplied dimension dim.
double do_time_analytic(int nderiv, int nloop)
Definition: fad_expr.cpp:98
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:60
ScalarT number()
Get random number.
#define T
Definition: Sacado_rad.hpp:573
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)
Derivative array storage class using dynamic memory allocation.
EParseCommandLineReturn parse(int argc, char *argv[], std::ostream *errout=&std::cerr) const
SimpleFad< ValueT > sin(const SimpleFad< ValueT > &a)
double totalElapsedTime(bool readCurrentTime=false) const
double do_time(int nderiv, int nloop)
Definition: fad_expr.cpp:74
void setDocString(const char doc_string[])
SimpleFad< ValueT > cos(const SimpleFad< ValueT > &a)
Forward-mode AD class using dynamic memory allocation and expression templates.
int main(int argc, char *argv[])
Definition: fad_expr.cpp:125
void func1(const T &x1, const T &x2, T &y)
Definition: fad_expr.cpp:55