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_Fad_DFad.hpp"
00034 #include "Sacado_ELRFad_DFad.hpp"
00035 #include "Sacado_CacheFad_DFad.hpp"
00036 #include "Fad/fad.h"
00037 #include "Teuchos_Time.hpp"
00038
00039
00040
00041
00042 void FAD::error(const char *msg) {
00043 std::cout << msg << std::endl;
00044 }
00045
00046 template <typename T>
00047 inline void
00048 mult1(const T& x1, const T& x2, T& y) {
00049 y = x1*x2;
00050 }
00051
00052 template <typename T>
00053 inline void
00054 mult2(const T& x1, const T& x2, T& y) {
00055 y = x1*x2*x1;
00056 }
00057
00058 template <typename T>
00059 inline void
00060 mult3(const T& x1, const T& x2, T& y) {
00061 y = x1*x2*x1*x2;
00062 }
00063
00064 template <typename T>
00065 inline void
00066 mult4(const T& x1, const T& x2, T& y) {
00067 y = x1*x2*x1*x2*x1;
00068 }
00069
00070 template <typename T>
00071 inline void
00072 mult5(const T& x1, const T& x2, T& y) {
00073 y = x1*x2*x1*x2*x1*x2;
00074 }
00075
00076 template <typename T>
00077 inline void
00078 mult10(const T& x1, const T& x2, T& y) {
00079 y = x1*x2*x1*x2*x1*x2*x1*x2*x1*x2*x1;
00080 }
00081
00082 template <typename T>
00083 inline void
00084 mult15(const T& x1, const T& x2, T& y) {
00085 y = x1*x2*x1*x2*x1*x2*x1*x2*x1*x2*x1*x2*x1*x2*x1*x2;
00086 }
00087
00088 template <typename T>
00089 inline void
00090 mult20(const T& x1, const T& x2, T& y) {
00091 y = x1*x2*x1*x2*x1*x2*x1*x2*x1*x2*x1*x2*x1*x2*x1*x2*x1*x2*x1*x2*x1;
00092 }
00093
00094 template <typename FadType>
00095 void
00096 do_times(const std::string& name)
00097 {
00098 const int nfunc = 8;
00099 const int nderiv = 10;
00100 int deriv_dim[nderiv];
00101 int nloop[nderiv];
00102 double times[nfunc][nderiv];
00103 int p = 1;
00104 int w = p+7;
00105
00106 std::cout.setf(std::ios::scientific);
00107 std::cout.precision(p);
00108 std::cout << name << " Times (sec): " << std::endl;
00109 std::cout << std::setw(5) << "deriv" << " "
00110 << std::setw(w) << "mult1" << " "
00111 << std::setw(w) << "mult2" << " "
00112 << std::setw(w) << "mult3" << " "
00113 << std::setw(w) << "mult4" << " "
00114 << std::setw(w) << "mult5" << " "
00115 << std::setw(w) << "mult10" << " "
00116 << std::setw(w) << "mult15" << " "
00117 << std::setw(w) << "mult20" << std::endl;
00118 std::cout << "===== ";
00119 for (int i=0; i<nfunc; i++) {
00120 for (int j=0; j<w; j++)
00121 std::cout << '=';
00122 std::cout << " ";
00123 }
00124 std::cout << std::endl;
00125
00126 for (int i=0; i<5; i++)
00127 deriv_dim[i] = i;
00128 for (int i=5; i<nderiv; i++)
00129 deriv_dim[i] = 5*(i-4);
00130 for (int i=0; i<nderiv; i++)
00131 nloop[i] = static_cast<int>(1000000.0/(deriv_dim[i]+1));
00132
00133 FadType x1, x2, y;
00134 Sacado::Random urand(0.0, 1.0);
00135 for (int i=0; i<nderiv; i++) {
00136 std::cout << std::setw(5) << deriv_dim[i] << " ";
00137
00138 x1 = FadType(deriv_dim[i], urand.number());
00139 x2 = FadType(deriv_dim[i], urand.number());
00140 y = 0.0;
00141 for (int j=0; j<deriv_dim[i]; j++) {
00142 x1.fastAccessDx(j) = urand.number();
00143 x2.fastAccessDx(j) = urand.number();
00144 }
00145
00146 Teuchos::Time timer("mult", false);
00147
00148 timer.start(true);
00149 for (int j=0; j<nloop[i]; j++)
00150 mult1(x1, x2, y);
00151 timer.stop();
00152 times[0][i] = timer.totalElapsedTime() / nloop[i];
00153 y = 0.0;
00154 std::cout << std::setw(w) << times[0][i] << " ";
00155
00156 timer.start(true);
00157 for (int j=0; j<nloop[i]; j++)
00158 mult2(x1, x2, y);
00159 timer.stop();
00160 times[1][i] = timer.totalElapsedTime() / nloop[i];
00161 y = 0.0;
00162 std::cout << std::setw(w) << times[1][i] << " ";
00163
00164 timer.start(true);
00165 for (int j=0; j<nloop[i]; j++)
00166 mult3(x1, x2, y);
00167 timer.stop();
00168 times[2][i] = timer.totalElapsedTime() / nloop[i];
00169 y = 0.0;
00170 std::cout << std::setw(w) << times[2][i] << " ";
00171
00172 timer.start(true);
00173 for (int j=0; j<nloop[i]; j++)
00174 mult4(x1, x2, y);
00175 timer.stop();
00176 times[3][i] = timer.totalElapsedTime() / nloop[i];
00177 y = 0.0;
00178 std::cout << std::setw(w) << times[3][i] << " ";
00179
00180 timer.start(true);
00181 for (int j=0; j<nloop[i]; j++)
00182 mult5(x1, x2, y);
00183 timer.stop();
00184 times[4][i] = timer.totalElapsedTime() / nloop[i];
00185 y = 0.0;
00186 std::cout << std::setw(w) << times[4][i] << " ";
00187
00188 timer.start(true);
00189 for (int j=0; j<nloop[i]; j++)
00190 mult10(x1, x2, y);
00191 timer.stop();
00192 times[5][i] = timer.totalElapsedTime() / nloop[i];
00193 y = 0.0;
00194 std::cout << std::setw(w) << times[5][i] << " ";
00195
00196 timer.start(true);
00197 for (int j=0; j<nloop[i]; j++)
00198 mult15(x1, x2, y);
00199 timer.stop();
00200 times[6][i] = timer.totalElapsedTime() / nloop[i];
00201 y = 0.0;
00202 std::cout << std::setw(w) << times[6][i] << " ";
00203
00204 timer.start(true);
00205 for (int j=0; j<nloop[i]; j++)
00206 mult20(x1, x2, y);
00207 timer.stop();
00208 times[7][i] = timer.totalElapsedTime() / nloop[i];
00209 y = 0.0;
00210 std::cout << std::setw(w) << times[7][i] << " ";
00211
00212 std::cout << endl;
00213 }
00214 }
00215
00216 int main() {
00217 do_times< FAD::Fad<double> >("FAD::Fad");
00218 do_times< Sacado::Fad::DFad<double> >("Sacado::Fad::DFad");
00219 do_times< Sacado::ELRFad::DFad<double> >("Sacado::ELRFad::DFad");
00220 do_times< Sacado::CacheFad::DFad<double> >("Sacado::CacheFad::DFad");
00221
00222 return 0;
00223 }