38 #include "TinyFadET/tfad.h" 40 #include "Teuchos_Time.hpp" 41 #include "Teuchos_CommandLineProcessor.hpp" 49 void FAD::error(
const char *msg) {
50 std::cout << msg << std::endl;
56 y = (x1*x2 +
sin(x1)/x2);
61 double& y,
double* ydot) {
65 double t1 = x2 +
c/x2;
66 double t2 = x1 - t/x2;
68 for (
int i=0; i<10; i++)
69 ydot[i] = t1*x1dot[i] + t2*x2dot[i];
72 template <
typename FadType>
82 for (
int j=0; j<nderiv; j++) {
83 x1.fastAccessDx(j) = urand.
number();
84 x2.fastAccessDx(j) = urand.
number();
87 Teuchos::Time timer(
"mult",
false);
89 for (
int j=0; j<nloop; j++) {
94 return timer.totalElapsedTime() / nloop;
101 double *x1dot, *x2dot, *ydot;
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();
115 Teuchos::Time timer(
"mult",
false);
117 for (
int j=0; j<nloop; j++) {
122 return timer.totalElapsedTime() / nloop;
125 int main(
int argc,
char* argv[]) {
134 Teuchos::CommandLineProcessor clp;
135 clp.setDocString(
"This program tests the speed of various forward mode AD implementations for a single multiplication operation");
137 clp.setOption(
"nderiv", &nderiv,
"Number of derivative components");
139 clp.setOption(
"nloop", &nloop,
"Number of loops");
142 Teuchos::CommandLineProcessor::EParseCommandLineReturn
143 parseReturn= clp.parse(argc, argv);
144 if(parseReturn != Teuchos::CommandLineProcessor::PARSE_SUCCESSFUL)
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;
158 std::cout <<
"Analytic: " << std::setw(w) << ta << std::endl;
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;
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;
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;
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;
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;
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;
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;
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;
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;
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;
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;
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;
197 catch (std::exception& e) {
198 std::cout << e.what() << std::endl;
201 catch (
const char *s) {
202 std::cout << s << std::endl;
206 std::cout <<
"Caught unknown exception!" << std::endl;
MemPool * getMemoryPool(unsigned int dim)
Get memory pool for supplied dimension dim.
double do_time_analytic(int nderiv, int nloop)
Sacado::Fad::DFad< double > FadType
void func1_and_deriv(int n, double x1, double x2, double *x1dot, double *x2dot, double &y, double *ydot)
ScalarT number()
Get random number.
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
Derivative array storage class using dynamic memory allocation.
SimpleFad< ValueT > sin(const SimpleFad< ValueT > &a)
double do_time(int nderiv, int nloop)
SimpleFad< ValueT > cos(const SimpleFad< ValueT > &a)
Forward-mode AD class using dynamic memory allocation and expression templates.
int main(int argc, char *argv[])
void func1(const T &x1, const T &x2, T &y)