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 // $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_Fad_SimpleFad.hpp"
35 
36 #include "Fad/fad.h"
37 #include "TinyFadET/tfad.h"
38 
39 #include "Teuchos_Time.hpp"
41 
42 // A simple performance test that computes the derivative of a simple
43 // expression using many variants of Fad.
44 
45 template <>
47 
48 void FAD::error(const char *msg) {
49  std::cout << msg << std::endl;
50 }
51 
52 template <typename T>
53 inline void
54 func1(const T& x1, const T& x2, T& y) {
55  y = (x1*x2 + sin(x1)/x2);
56 }
57 
58 inline void
59 func1_and_deriv(int n, double x1, double x2, double* x1dot, double* x2dot,
60  double& y, double* ydot) {
61  double s = sin(x1);
62  double c = cos(x1);
63  double t = s/x2;
64  double t1 = x2 + c/x2;
65  double t2 = x1 - t/x2;
66  y = x1*x2 + t;
67  for (int i=0; i<10; i++)
68  ydot[i] = t1*x1dot[i] + t2*x2dot[i];
69 }
70 
71 template <typename FadType>
72 double
73 do_time(int nderiv, int nloop)
74 {
75  FadType x1, x2, y;
76  Sacado::Random<double> urand(0.0, 1.0);
77 
78  x1 = FadType(nderiv, urand.number());
79  x2 = FadType(nderiv, urand.number());
80  y = 0.0;
81  for (int j=0; j<nderiv; j++) {
82  x1.fastAccessDx(j) = urand.number();
83  x2.fastAccessDx(j) = urand.number();
84  }
85 
86  Teuchos::Time timer("mult", false);
87  timer.start(true);
88  for (int j=0; j<nloop; j++) {
89  func1(x1, x2, y);
90  }
91  timer.stop();
92 
93  return timer.totalElapsedTime() / nloop;
94 }
95 
96 double
97 do_time_analytic(int nderiv, int nloop)
98 {
99  double x1, x2, y;
100  double *x1dot, *x2dot, *ydot;
101  Sacado::Random<double> urand(0.0, 1.0);
102 
103  x1 = urand.number();
104  x2 = urand.number();
105  y = 0.0;
106  x1dot = new double[nderiv];
107  x2dot = new double[nderiv];
108  ydot = new double[nderiv];
109  for (int j=0; j<nderiv; j++) {
110  x1dot[j] = urand.number();
111  x2dot[j] = urand.number();
112  }
113 
114  Teuchos::Time timer("mult", false);
115  timer.start(true);
116  for (int j=0; j<nloop; j++) {
117  func1_and_deriv(nderiv, x1, x2, x1dot, x2dot, y, ydot);
118  }
119  timer.stop();
120 
121  return timer.totalElapsedTime() / nloop;
122 }
123 
124 int main(int argc, char* argv[]) {
125  int ierr = 0;
126 
127  try {
128  double t, ta;
129  int p = 2;
130  int w = p+7;
131 
132  // Set up command line options
134  clp.setDocString("This program tests the speed of various forward mode AD implementations for a single multiplication operation");
135  int nderiv = 10;
136  clp.setOption("nderiv", &nderiv, "Number of derivative components");
137  int nloop = 1000000;
138  clp.setOption("nloop", &nloop, "Number of loops");
139 
140  // Parse options
142  parseReturn= clp.parse(argc, argv);
144  return 1;
145 
146  // Memory pool & manager
147  Sacado::Fad::MemPoolManager<double> poolManager(10);
148  Sacado::Fad::MemPool* pool = poolManager.getMemoryPool(nderiv);
150 
151  std::cout.setf(std::ios::scientific);
152  std::cout.precision(p);
153  std::cout << "Times (sec) for nderiv = " << nderiv
154  << " nloop = " << nloop << ": " << std::endl;
155 
156  ta = do_time_analytic(nderiv, nloop);
157  std::cout << "Analytic: " << std::setw(w) << ta << std::endl;
158 
159  t = do_time< Sacado::Fad::SimpleFad<double> >(nderiv, nloop);
160  std::cout << "SimpleFad: " << std::setw(w) << t << "\t" << std::setw(w) << t/ta << std::endl;
161 
162  t = do_time< FAD::TFad<10,double> >(nderiv, nloop);
163  std::cout << "TFad: " << std::setw(w) << t << "\t" << std::setw(w) << t/ta << std::endl;
164 
165  t = do_time< FAD::Fad<double> >(nderiv, nloop);
166  std::cout << "Fad: " << std::setw(w) << t << "\t" << std::setw(w) << t/ta << std::endl;
167 
168  t = do_time< Sacado::Fad::SFad<double,10> >(nderiv, nloop);
169  std::cout << "SFad: " << std::setw(w) << t << "\t" << std::setw(w) << t/ta << std::endl;
170 
171  t = do_time< Sacado::Fad::SLFad<double,10> >(nderiv, nloop);
172  std::cout << "SLFad: " << std::setw(w) << t << "\t" << std::setw(w) << t/ta << std::endl;
173 
174  t = do_time< Sacado::Fad::DFad<double> >(nderiv, nloop);
175  std::cout << "DFad: " << std::setw(w) << t << "\t" << std::setw(w) << t/ta << std::endl;
176 
177  t = do_time< Sacado::Fad::DMFad<double> >(nderiv, nloop);
178  std::cout << "DMFad: " << std::setw(w) << t << "\t" << std::setw(w) << t/ta << std::endl;
179 
180  t = do_time< Sacado::ELRFad::SFad<double,10> >(nderiv, nloop);
181  std::cout << "ELRSFad: " << std::setw(w) << t << "\t" << std::setw(w) << t/ta << std::endl;
182 
183  t = do_time< Sacado::ELRFad::SLFad<double,10> >(nderiv, nloop);
184  std::cout << "ELRSLFad: " << std::setw(w) << t << "\t" << std::setw(w) << t/ta << std::endl;
185 
186  t = do_time< Sacado::ELRFad::DFad<double> >(nderiv, nloop);
187  std::cout << "ELRDFad: " << std::setw(w) << t << "\t" << std::setw(w) << t/ta << std::endl;
188 
189  t = do_time< Sacado::CacheFad::DFad<double> >(nderiv, nloop);
190  std::cout << "CacheFad: " << std::setw(w) << t << "\t" << std::setw(w) << t/ta << std::endl;
191 
192  t = do_time< Sacado::Fad::DVFad<double> >(nderiv, nloop);
193  std::cout << "DVFad: " << std::setw(w) << t << "\t" << std::setw(w) << t/ta << std::endl;
194 
195  }
196  catch (std::exception& e) {
197  std::cout << e.what() << std::endl;
198  ierr = 1;
199  }
200  catch (const char *s) {
201  std::cout << s << std::endl;
202  ierr = 1;
203  }
204  catch (...) {
205  std::cout << "Caught unknown exception!" << std::endl;
206  ierr = 1;
207  }
208 
209  return ierr;
210 }
MemPool * getMemoryPool(unsigned int dim)
Get memory pool for supplied dimension dim.
double do_time_analytic(int nderiv, int nloop)
Definition: fad_expr.cpp:97
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:59
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.
int main()
Definition: ad_example.cpp:191
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:73
void setDocString(const char doc_string[])
double totalElapsedTime(bool readCurrentTime=false) const
Forward-mode AD class using dynamic memory allocation and expression templates.
void func1(const T &x1, const T &x2, T &y)
Definition: fad_expr.cpp:54
cos(expr.val())