00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032 #include "Sacado_Random.hpp"
00033 #include "Sacado.hpp"
00034 #include "Sacado_ELRFad_DFad.hpp"
00035
00036 #include "Fad/fad.h"
00037 #include "TinyFadET/tfad.h"
00038
00039 #include "Teuchos_Time.hpp"
00040 #include "Teuchos_CommandLineProcessor.hpp"
00041
00042
00043
00044
00045 template <>
00046 Sacado::Fad::MemPool* Sacado::Fad::MemPoolStorage<double>::defaultPool_ = NULL;
00047
00048 void FAD::error(const char *msg) {
00049 std::cout << msg << std::endl;
00050 }
00051
00052 template <typename T>
00053 inline void
00054 func1(const T& x1, const T& x2, T& y) {
00055 T t = x1*x2 + sin(x1)/x2;
00056 y = t;
00057 }
00058
00059 template <typename FadType>
00060 double
00061 do_time(int nderiv, int nloop)
00062 {
00063 FadType x1, x2, y;
00064 Sacado::Random urand(0.0, 1.0);
00065
00066 x1 = FadType(nderiv, urand.number());
00067 x2 = FadType(nderiv, urand.number());
00068 y = 0.0;
00069 for (int j=0; j<nderiv; j++) {
00070 x1.fastAccessDx(j) = urand.number();
00071 x2.fastAccessDx(j) = urand.number();
00072 }
00073
00074 Teuchos::Time timer("mult", false);
00075 timer.start(true);
00076 for (int j=0; j<nloop; j++) {
00077 func1(x1, x2, y);
00078 }
00079 timer.stop();
00080
00081 return timer.totalElapsedTime() / nloop;
00082 }
00083
00084 int main(int argc, char* argv[]) {
00085 int ierr = 0;
00086
00087 try {
00088 double t;
00089 int p = 2;
00090 int w = p+7;
00091
00092
00093 Teuchos::CommandLineProcessor clp;
00094 clp.setDocString("This program tests the speed of various forward mode AD implementations for a single multiplication operation");
00095 int nderiv = 10;
00096 clp.setOption("nderiv", &nderiv, "Number of derivative components");
00097 int nloop = 1000000;
00098 clp.setOption("nloop", &nloop, "Number of loops");
00099
00100
00101 Teuchos::CommandLineProcessor::EParseCommandLineReturn
00102 parseReturn= clp.parse(argc, argv);
00103 if(parseReturn != Teuchos::CommandLineProcessor::PARSE_SUCCESSFUL)
00104 return 1;
00105
00106
00107 Sacado::Fad::MemPoolManager<double> poolManager(10);
00108 Sacado::Fad::MemPool* pool = poolManager.getMemoryPool(nderiv);
00109 Sacado::Fad::DMFad<double>::setDefaultPool(pool);
00110
00111 std::cout.setf(std::ios::scientific);
00112 std::cout.precision(p);
00113 std::cout << "Times (sec) for nderiv = " << nderiv
00114 << " nloop = " << nloop << ": " << std::endl;
00115
00116 t = do_time< FAD::Fad<double> >(nderiv, nloop);
00117 std::cout << "Fad: " << std::setw(w) << t << std::endl;
00118
00119 t = do_time< FAD::TFad<10,double> >(nderiv, nloop);
00120 std::cout << "TFad: " << std::setw(w) << t << std::endl;
00121
00122 t = do_time< Sacado::Fad::DFad<double> >(nderiv, nloop);
00123 std::cout << "DFad: " << std::setw(w) << t << std::endl;
00124
00125 t = do_time< Sacado::ELRFad::DFad<double> >(nderiv, nloop);
00126 std::cout << "ELRDFad: " << std::setw(w) << t << std::endl;
00127
00128 t = do_time< Sacado::Fad::DMFad<double> >(nderiv, nloop);
00129 std::cout << "DMFad: " << std::setw(w) << t << std::endl;
00130
00131 t = do_time< Sacado::Fad::SFad<double,10> >(nderiv, nloop);
00132 std::cout << "SFad: " << std::setw(w) << t << std::endl;
00133
00134 t = do_time< Sacado::Fad::SLFad<double,10> >(nderiv, nloop);
00135 std::cout << "SLFad: " << std::setw(w) << t << std::endl;
00136
00137 t = do_time< Sacado::CacheFad::DFad<double> >(nderiv, nloop);
00138 std::cout << "CacheFad: " << std::setw(w) << t << std::endl;
00139
00140 }
00141 catch (std::exception& e) {
00142 cout << e.what() << endl;
00143 ierr = 1;
00144 }
00145 catch (const char *s) {
00146 cout << s << endl;
00147 ierr = 1;
00148 }
00149 catch (...) {
00150 cout << "Caught unknown exception!" << endl;
00151 ierr = 1;
00152 }
00153
00154 return ierr;
00155 }