14 #include "TinyFadET/tfad.h"
22 void FAD::error(
const char *msg) {
23 std::cout << msg << std::endl;
27 double xi[3], xj[3], pa[4],
f[3], delr[3];
33 T delr0 = xi[0]-xj[0];
34 T delr1 = xi[1]-xj[1];
35 T delr2 = xi[2]-xj[2];
36 return delr0*delr0 + delr1*delr1 + delr2*delr2;
42 delr[0] = xi[0]-xj[0];
43 delr[1] = xi[1]-xj[1];
44 delr[2] = xi[2]-xj[2];
45 return delr[0]*delr[0] + delr[1]*delr[1] + delr[2]*delr[2];
50 lj(
const T xi[],
const double xj[],
T& energy) {
53 T delr_6 = delr_2*delr_2*delr_2;
54 energy = (pa[1]*delr_6 - pa[2])*delr_6 - pa[3];
58 lj_and_grad(
const double xi[],
const double xj[],
double& energy,
61 double delr_2 = 1.0/delr2;
62 double delr_6 = delr_2*delr_2*delr_2;
63 energy = (pa[1]*delr_6 - pa[2])*delr_6 - pa[3];
64 double tmp = (-12.0*pa[1]*delr_6 - 6.0*pa[2])*delr_6*delr_2;
70 template <
typename FadType>
77 for (
int i=0;
i<3;
i++) {
82 for (
int j=0; j<nloop; j++) {
84 lj(xi_fad, xj, energy);
86 for (
int i=0;
i<3;
i++)
87 f[
i] += -energy.fastAccessDx(
i);
101 for (
int j=0; j<nloop; j++) {
105 for (
int i=0;
i<3;
i++)
114 int main(
int argc,
char* argv[]) {
124 clp.
setDocString(
"This program tests the speed of various forward mode AD implementations for a single multiplication operation");
126 clp.
setOption(
"nloop", &nloop,
"Number of loops");
130 parseReturn= clp.
parse(argc, argv);
134 std::cout.setf(std::ios::scientific);
135 std::cout.precision(p);
136 std::cout <<
"Times (sec) nloop = " << nloop <<
": " << std::endl;
139 for (
int i=0;
i<3;
i++) {
147 std::cout <<
"Analytic: " << std::setw(w) << ta << std::endl;
149 t = do_time< FAD::TFad<3,double> >(nloop);
150 std::cout <<
"TFad: " << std::setw(w) << t <<
"\t" << std::setw(w) << t/ta << std::endl;
152 t = do_time< FAD::Fad<double> >(nloop);
153 std::cout <<
"Fad: " << std::setw(w) << t <<
"\t" << std::setw(w) << t/ta << std::endl;
155 t = do_time< Sacado::Fad::SFad<double,3> >(nloop);
156 std::cout <<
"SFad: " << std::setw(w) << t <<
"\t" << std::setw(w) << t/ta << std::endl;
158 t = do_time< Sacado::Fad::SLFad<double,3> >(nloop);
159 std::cout <<
"SLFad: " << std::setw(w) << t <<
"\t" << std::setw(w) << t/ta << std::endl;
161 t = do_time< Sacado::Fad::DFad<double> >(nloop);
162 std::cout <<
"DFad: " << std::setw(w) << t <<
"\t" << std::setw(w) << t/ta << std::endl;
164 t = do_time< Sacado::ELRFad::SFad<double,3> >(nloop);
165 std::cout <<
"ELRSFad: " << std::setw(w) << t <<
"\t" << std::setw(w) << t/ta << std::endl;
167 t = do_time< Sacado::ELRFad::SLFad<double,3> >(nloop);
168 std::cout <<
"ELRSLFad: " << std::setw(w) << t <<
"\t" << std::setw(w) << t/ta << std::endl;
170 t = do_time< Sacado::ELRFad::DFad<double> >(nloop);
171 std::cout <<
"ELRDFad: " << std::setw(w) << t <<
"\t" << std::setw(w) << t/ta << std::endl;
173 t = do_time< Sacado::CacheFad::DFad<double> >(nloop);
174 std::cout <<
"CacheFad: " << std::setw(w) << t <<
"\t" << std::setw(w) << t/ta << std::endl;
176 t = do_time< Sacado::Fad::DVFad<double> >(nloop);
177 std::cout <<
"DVFad: " << std::setw(w) << t <<
"\t" << std::setw(w) << t/ta << std::endl;
180 catch (std::exception& e) {
181 std::cout << e.what() << std::endl;
184 catch (
const char *s) {
185 std::cout << s << std::endl;
189 std::cout <<
"Caught unknown exception!" << std::endl;
double do_time_analytic(int nderiv, int nloop)
Sacado::Fad::DFad< double > FadType
ScalarT number()
Get random number.
void start(bool reset=false)
void setOption(const char option_true[], const char option_false[], bool *option_val, const char documentation[]=NULL)
EParseCommandLineReturn parse(int argc, char *argv[], std::ostream *errout=&std::cerr) const
void lj_and_grad(const double xi[], const double xj[], double &energy, double f[])
double do_time(int nderiv, int nloop)
void setDocString(const char doc_string[])
double totalElapsedTime(bool readCurrentTime=false) const
void lj(const T xi[], const double xj[], T &energy)
T vec3_distsq(const T xi[], const double xj[])