36 #include "TinyFadET/tfad.h"
44 void FAD::error(
const char *msg) {
45 std::cout << msg << std::endl;
49 double xi[3], xj[3], pa[4],
f[3], delr[3];
55 T delr0 = xi[0]-xj[0];
56 T delr1 = xi[1]-xj[1];
57 T delr2 = xi[2]-xj[2];
58 return delr0*delr0 + delr1*delr1 + delr2*delr2;
64 delr[0] = xi[0]-xj[0];
65 delr[1] = xi[1]-xj[1];
66 delr[2] = xi[2]-xj[2];
67 return delr[0]*delr[0] + delr[1]*delr[1] + delr[2]*delr[2];
72 lj(
const T xi[],
const double xj[],
T& energy) {
75 T delr_6 = delr_2*delr_2*delr_2;
76 energy = (pa[1]*delr_6 - pa[2])*delr_6 - pa[3];
80 lj_and_grad(
const double xi[],
const double xj[],
double& energy,
83 double delr_2 = 1.0/delr2;
84 double delr_6 = delr_2*delr_2*delr_2;
85 energy = (pa[1]*delr_6 - pa[2])*delr_6 - pa[3];
86 double tmp = (-12.0*pa[1]*delr_6 - 6.0*pa[2])*delr_6*delr_2;
92 template <
typename FadType>
99 for (
int i=0; i<3; i++) {
100 xi_fad[i] =
FadType(3, i, xi[i]);
104 for (
int j=0; j<nloop; j++) {
106 lj(xi_fad, xj, energy);
108 for (
int i=0; i<3; i++)
109 f[i] += -energy.fastAccessDx(i);
120 double energy, ff[3];
123 for (
int j=0; j<nloop; j++) {
127 for (
int i=0; i<3; i++)
136 int main(
int argc,
char* argv[]) {
146 clp.
setDocString(
"This program tests the speed of various forward mode AD implementations for a single multiplication operation");
148 clp.
setOption(
"nloop", &nloop,
"Number of loops");
152 parseReturn= clp.
parse(argc, argv);
156 std::cout.setf(std::ios::scientific);
157 std::cout.precision(p);
158 std::cout <<
"Times (sec) nloop = " << nloop <<
": " << std::endl;
161 for (
int i=0; i<3; i++) {
169 std::cout <<
"Analytic: " << std::setw(w) << ta << std::endl;
171 t = do_time< FAD::TFad<3,double> >(nloop);
172 std::cout <<
"TFad: " << std::setw(w) << t <<
"\t" << std::setw(w) << t/ta << std::endl;
174 t = do_time< FAD::Fad<double> >(nloop);
175 std::cout <<
"Fad: " << std::setw(w) << t <<
"\t" << std::setw(w) << t/ta << std::endl;
177 t = do_time< Sacado::Fad::SFad<double,3> >(nloop);
178 std::cout <<
"SFad: " << std::setw(w) << t <<
"\t" << std::setw(w) << t/ta << std::endl;
180 t = do_time< Sacado::Fad::SLFad<double,3> >(nloop);
181 std::cout <<
"SLFad: " << std::setw(w) << t <<
"\t" << std::setw(w) << t/ta << std::endl;
183 t = do_time< Sacado::Fad::DFad<double> >(nloop);
184 std::cout <<
"DFad: " << std::setw(w) << t <<
"\t" << std::setw(w) << t/ta << std::endl;
186 t = do_time< Sacado::ELRFad::SFad<double,3> >(nloop);
187 std::cout <<
"ELRSFad: " << std::setw(w) << t <<
"\t" << std::setw(w) << t/ta << std::endl;
189 t = do_time< Sacado::ELRFad::SLFad<double,3> >(nloop);
190 std::cout <<
"ELRSLFad: " << std::setw(w) << t <<
"\t" << std::setw(w) << t/ta << std::endl;
192 t = do_time< Sacado::ELRFad::DFad<double> >(nloop);
193 std::cout <<
"ELRDFad: " << std::setw(w) << t <<
"\t" << std::setw(w) << t/ta << std::endl;
195 t = do_time< Sacado::CacheFad::DFad<double> >(nloop);
196 std::cout <<
"CacheFad: " << std::setw(w) << t <<
"\t" << std::setw(w) << t/ta << std::endl;
198 t = do_time< Sacado::Fad::DVFad<double> >(nloop);
199 std::cout <<
"DVFad: " << std::setw(w) << t <<
"\t" << std::setw(w) << t/ta << std::endl;
202 catch (std::exception& e) {
203 std::cout << e.what() << std::endl;
206 catch (
const char *s) {
207 std::cout << s << std::endl;
211 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[])