fad_expr.cpp

Go to the documentation of this file.
00001 // $Id: fad_expr.cpp,v 1.1.2.1 2007/08/14 00:19:09 etphipp Exp $ 
00002 // $Source: /space/CVS/Trilinos/packages/sacado/test/performance/fad_expr.cpp,v $ 
00003 // @HEADER
00004 // ***********************************************************************
00005 // 
00006 //                           Sacado Package
00007 //                 Copyright (2006) Sandia Corporation
00008 // 
00009 // Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
00010 // the U.S. Government retains certain rights in this software.
00011 // 
00012 // This library is free software; you can redistribute it and/or modify
00013 // it under the terms of the GNU Lesser General Public License as
00014 // published by the Free Software Foundation; either version 2.1 of the
00015 // License, or (at your option) any later version.
00016 //  
00017 // This library is distributed in the hope that it will be useful, but
00018 // WITHOUT ANY WARRANTY; without even the implied warranty of
00019 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00020 // Lesser General Public License for more details.
00021 //  
00022 // You should have received a copy of the GNU Lesser General Public
00023 // License along with this library; if not, write to the Free Software
00024 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
00025 // USA
00026 // Questions? Contact David M. Gay (dmgay@sandia.gov) or Eric T. Phipps
00027 // (etphipp@sandia.gov).
00028 // 
00029 // ***********************************************************************
00030 // @HEADER
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 // A simple performance test that computes the derivative of a simple
00043 // expression using many variants of Fad.
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     // Set up command line options
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     // Parse options
00101     Teuchos::CommandLineProcessor::EParseCommandLineReturn
00102       parseReturn= clp.parse(argc, argv);
00103     if(parseReturn != Teuchos::CommandLineProcessor::PARSE_SUCCESSFUL)
00104       return 1;
00105 
00106     // Memory pool & manager
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 }

Generated on Tue Oct 20 12:55:02 2009 for Sacado Package Browser (Single Doxygen Collection) by doxygen 1.4.7