36 #include "TinyFadET/tfad.h"
47 void FAD::error(
const char *msg) {
48 std::cout << msg << std::endl;
52 double xi[3], xj[3], pa[4],
f[3], delr[3];
58 T delr0 = xi[0]-xj[0];
59 T delr1 = xi[1]-xj[1];
60 T delr2 = xi[2]-xj[2];
61 return delr0*delr0 + delr1*delr1 + delr2*delr2;
67 delr[0] = xi[0]-xj[0];
68 delr[1] = xi[1]-xj[1];
69 delr[2] = xi[2]-xj[2];
70 return delr[0]*delr[0] + delr[1]*delr[1] + delr[2]*delr[2];
75 lj(
const T xi[],
const double xj[],
T& energy) {
78 T delr_6 = delr_2*delr_2*delr_2;
79 energy = (pa[1]*delr_6 - pa[2])*delr_6 - pa[3];
83 lj_and_grad(
const double xi[],
const double xj[],
double& energy,
86 double delr_2 = 1.0/delr2;
87 double delr_6 = delr_2*delr_2*delr_2;
88 energy = (pa[1]*delr_6 - pa[2])*delr_6 - pa[3];
89 double tmp = (-12.0*pa[1]*delr_6 - 6.0*pa[2])*delr_6*delr_2;
95 template <
typename FadType>
102 for (
int i=0; i<3; i++) {
103 xi_fad[i] =
FadType(3, i, xi[i]);
107 for (
int j=0; j<nloop; j++) {
109 lj(xi_fad, xj, energy);
111 for (
int i=0; i<3; i++)
112 f[i] += -energy.fastAccessDx(i);
123 double energy, ff[3];
126 for (
int j=0; j<nloop; j++) {
130 for (
int i=0; i<3; i++)
139 int main(
int argc,
char* argv[]) {
149 clp.
setDocString(
"This program tests the speed of various forward mode AD implementations for a single multiplication operation");
151 clp.
setOption(
"nloop", &nloop,
"Number of loops");
155 parseReturn= clp.
parse(argc, argv);
164 std::cout.setf(std::ios::scientific);
165 std::cout.precision(p);
166 std::cout <<
"Times (sec) nloop = " << nloop <<
": " << std::endl;
169 for (
int i=0; i<3; i++) {
177 std::cout <<
"Analytic: " << std::setw(w) << ta << std::endl;
179 t = do_time< FAD::TFad<3,double> >(nloop);
180 std::cout <<
"TFad: " << std::setw(w) << t <<
"\t" << std::setw(w) << t/ta << std::endl;
182 t = do_time< FAD::Fad<double> >(nloop);
183 std::cout <<
"Fad: " << std::setw(w) << t <<
"\t" << std::setw(w) << t/ta << std::endl;
185 t = do_time< Sacado::Fad::SFad<double,3> >(nloop);
186 std::cout <<
"SFad: " << std::setw(w) << t <<
"\t" << std::setw(w) << t/ta << std::endl;
188 t = do_time< Sacado::Fad::SLFad<double,3> >(nloop);
189 std::cout <<
"SLFad: " << std::setw(w) << t <<
"\t" << std::setw(w) << t/ta << std::endl;
191 t = do_time< Sacado::Fad::DFad<double> >(nloop);
192 std::cout <<
"DFad: " << std::setw(w) << t <<
"\t" << std::setw(w) << t/ta << std::endl;
194 t = do_time< Sacado::Fad::DMFad<double> >(nloop);
195 std::cout <<
"DMFad: " << std::setw(w) << t <<
"\t" << std::setw(w) << t/ta << std::endl;
197 t = do_time< Sacado::ELRFad::SFad<double,3> >(nloop);
198 std::cout <<
"ELRSFad: " << std::setw(w) << t <<
"\t" << std::setw(w) << t/ta << std::endl;
200 t = do_time< Sacado::ELRFad::SLFad<double,3> >(nloop);
201 std::cout <<
"ELRSLFad: " << std::setw(w) << t <<
"\t" << std::setw(w) << t/ta << std::endl;
203 t = do_time< Sacado::ELRFad::DFad<double> >(nloop);
204 std::cout <<
"ELRDFad: " << std::setw(w) << t <<
"\t" << std::setw(w) << t/ta << std::endl;
206 t = do_time< Sacado::CacheFad::DFad<double> >(nloop);
207 std::cout <<
"CacheFad: " << std::setw(w) << t <<
"\t" << std::setw(w) << t/ta << std::endl;
209 t = do_time< Sacado::Fad::DVFad<double> >(nloop);
210 std::cout <<
"DVFad: " << std::setw(w) << t <<
"\t" << std::setw(w) << t/ta << std::endl;
213 catch (std::exception& e) {
214 std::cout << e.what() << std::endl;
217 catch (
const char *s) {
218 std::cout << s << std::endl;
222 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
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)
Derivative array storage class using dynamic memory allocation.
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
Forward-mode AD class using dynamic memory allocation and expression templates.
void lj(const T xi[], const double xj[], T &energy)
T vec3_distsq(const T xi[], const double xj[])