21 template <
typename T,
typename F>
28 for (
int j=0; j<nloop; j++)
34 template <
typename T,
template <
typename,
int>
class F>
50 double do_time_adolc(
double *
x,
double **seed,
int d,
int nloop,
56 adouble *x_ad =
new adouble[n];
57 for (
int i=0;
i<n;
i++)
66 double **jac =
new double*[1];
67 jac[0] =
new double[d];
69 for (
int j=0; j<nloop; j++)
70 forward(tag, 1, n, d, x, seed, &y, jac);
76 return timer.totalElapsedTime() / nloop;
79 template <
template <
typename,
int>
class F>
80 void do_times_adolc(
double *x,
double **seed,
int d,
int nloop,
84 times[i++] = do_time_adolc(x, seed, d, nloop, tag++,
F<adouble,1>());
85 times[i++] = do_time_adolc(x, seed, d, nloop, tag++,
F<adouble,2>());
86 times[i++] = do_time_adolc(x, seed, d, nloop, tag++,
F<adouble,3>());
87 times[i++] = do_time_adolc(x, seed, d, nloop, tag++,
F<adouble,4>());
88 times[i++] = do_time_adolc(x, seed, d, nloop, tag++,
F<adouble,5>());
89 times[i++] = do_time_adolc(x, seed, d, nloop, tag++,
F<adouble,10>());
90 times[i++] = do_time_adolc(x, seed, d, nloop, tag++,
F<adouble,15>());
91 times[i++] = do_time_adolc(x, seed, d, nloop, tag++,
F<adouble,20>());
95 template <
typename FadType>
96 void print_times(
const std::string& screen_name,
const std::string& file_name)
98 const int nderiv = 10;
99 int deriv_dim[nderiv] = { 0, 1, 3, 5, 10, 15, 20, 30, 40, 50 };
103 std::ofstream file(file_name.c_str(), std::ios::out);
106 std::cout.setf(std::ios::scientific);
107 std::cout.precision(p);
108 std::cout << screen_name <<
" Relative times (time/(func_time*nderiv)): "
110 std::cout << std::setw(5) <<
"deriv" <<
" ";
113 std::cout << std::endl;
114 std::cout <<
"===== ";
116 for (
int j=0; j<w; j++)
120 std::cout << std::endl;
126 int nloop_func = 10000000;
128 do_times<double,ExprFuncs::mult>(
x, nloop_func, times_func);
131 for (
int i=0; i<nderiv; i++) {
135 for (
int j=0; j<deriv_dim[
i]; j++) {
136 fx[k].fastAccessDx(j) = urand.
number();
141 int nloop =
static_cast<int>(100000.0);
143 do_times<FadType,ExprFuncs::mult>(fx, nloop, times);
146 int d = deriv_dim[
i];
149 std::cout << std::setw(5) << deriv_dim[
i] <<
" ";
150 file << deriv_dim[
i] <<
" ";
151 for (
int j=0; j<times.
size(); j++) {
152 double rel_time = times[j]/(times_func[j]*d);
153 std::cout << std::setw(w) << rel_time <<
" ";
154 file << rel_time <<
" ";
156 std::cout << std::endl;
162 std::cout.setf(std::ios::scientific);
163 std::cout.precision(p);
164 std::cout << screen_name <<
" Relative times (time/(func_time*nderiv)): "
166 std::cout << std::setw(5) <<
"deriv" <<
" ";
169 std::cout << std::endl;
170 std::cout <<
"===== ";
172 for (
int j=0; j<w; j++)
176 std::cout << std::endl;
182 int nloop_func = 10000000;
184 do_times<double,ExprFuncs::add>(
x, nloop_func, times_func);
187 for (
int i=0; i<nderiv; i++) {
191 for (
int j=0; j<deriv_dim[
i]; j++) {
192 fx[k].fastAccessDx(j) = urand.
number();
197 int nloop =
static_cast<int>(100000.0);
199 do_times<FadType,ExprFuncs::add>(fx, nloop, times);
202 int d = deriv_dim[
i];
205 std::cout << std::setw(5) << deriv_dim[
i] <<
" ";
206 file << deriv_dim[
i] <<
" ";
207 for (
int j=0; j<times.
size(); j++) {
208 double rel_time = times[j]/(times_func[j]*d);
209 std::cout << std::setw(w) << rel_time <<
" ";
210 file << rel_time <<
" ";
212 std::cout << std::endl;
218 std::cout.setf(std::ios::scientific);
219 std::cout.precision(p);
220 std::cout << screen_name <<
" Relative times (time/(func_time*nderiv)): "
222 std::cout << std::setw(5) <<
"deriv" <<
" ";
225 std::cout << std::endl;
226 std::cout <<
"===== ";
228 for (
int j=0; j<w; j++)
232 std::cout << std::endl;
238 int nloop_func = 10000000;
240 do_times<double,ExprFuncs::nest>(
x, nloop_func, times_func);
243 for (
int i=0; i<nderiv; i++) {
247 for (
int j=0; j<deriv_dim[
i]; j++) {
248 fx[k].fastAccessDx(j) = urand.
number();
253 int nloop =
static_cast<int>(100000.0);
255 do_times<FadType,ExprFuncs::nest>(fx, nloop, times);
258 int d = deriv_dim[
i];
261 std::cout << std::setw(5) << deriv_dim[
i] <<
" ";
262 file << deriv_dim[
i] <<
" ";
263 for (
int j=0; j<times.
size(); j++) {
264 double rel_time = times[j]/(times_func[j]*d);
265 std::cout << std::setw(w) << rel_time <<
" ";
266 file << rel_time <<
" ";
268 std::cout << std::endl;
275 void print_times_adolc(
const std::string& screen_name,
276 const std::string& file_name)
278 const int nderiv = 10;
279 int deriv_dim[nderiv] = { 0, 1, 3, 5, 10, 15, 20, 30, 40, 50 };
280 const int deriv_max = 50;
284 std::ofstream file(file_name.c_str(), std::ios::out);
289 seed[
i] =
new double[deriv_max];
290 for (
int j=0; j<deriv_max; j++)
291 seed[i][j] = urand.number();
295 std::cout.setf(std::ios::scientific);
296 std::cout.precision(p);
297 std::cout << screen_name <<
" Relative times (time/(func_time*nderiv)): "
299 std::cout << std::setw(5) <<
"deriv" <<
" ";
302 std::cout << std::endl;
303 std::cout <<
"===== ";
305 for (
int j=0; j<w; j++)
309 std::cout << std::endl;
314 x[i] = urand.number();
315 int nloop_func = 10000000;
317 do_times<double,ExprFuncs::mult>(
x, nloop_func, times_func);
320 for (
int i=0; i<nderiv; i++) {
322 int nloop =
static_cast<int>(10000.0);
324 do_times_adolc<ExprFuncs::mult>(
x, seed, deriv_dim[
i], nloop, tag, times);
327 int d = deriv_dim[
i];
330 std::cout << std::setw(5) << deriv_dim[
i] <<
" ";
331 file << deriv_dim[
i] <<
" ";
332 for (
int j=0; j<times.
size(); j++) {
333 double rel_time = times[j]/(times_func[j]*d);
334 std::cout << std::setw(w) << rel_time <<
" ";
335 file << rel_time <<
" ";
337 std::cout << std::endl;
343 std::cout.setf(std::ios::scientific);
344 std::cout.precision(p);
345 std::cout << screen_name <<
" Relative times (time/(func_time*nderiv)): "
347 std::cout << std::setw(5) <<
"deriv" <<
" ";
350 std::cout << std::endl;
351 std::cout <<
"===== ";
353 for (
int j=0; j<w; j++)
357 std::cout << std::endl;
362 x[i] = urand.number();
363 int nloop_func = 10000000;
365 do_times<double,ExprFuncs::add>(
x, nloop_func, times_func);
368 for (
int i=0; i<nderiv; i++) {
370 int nloop =
static_cast<int>(10000.0);
372 do_times_adolc<ExprFuncs::add>(
x, seed, deriv_dim[
i], nloop, tag, times);
375 int d = deriv_dim[
i];
378 std::cout << std::setw(5) << deriv_dim[
i] <<
" ";
379 file << deriv_dim[
i] <<
" ";
380 for (
int j=0; j<times.
size(); j++) {
381 double rel_time = times[j]/(times_func[j]*d);
382 std::cout << std::setw(w) << rel_time <<
" ";
383 file << rel_time <<
" ";
385 std::cout << std::endl;
391 std::cout.setf(std::ios::scientific);
392 std::cout.precision(p);
393 std::cout << screen_name <<
" Relative times (time/(func_time*nderiv)): "
395 std::cout << std::setw(5) <<
"deriv" <<
" ";
398 std::cout << std::endl;
399 std::cout <<
"===== ";
401 for (
int j=0; j<w; j++)
405 std::cout << std::endl;
410 x[i] = urand.number();
411 int nloop_func = 10000000;
413 do_times<double,ExprFuncs::nest>(
x, nloop_func, times_func);
416 for (
int i=0; i<nderiv; i++) {
418 int nloop =
static_cast<int>(1000.0);
420 do_times_adolc<ExprFuncs::nest>(
x, seed, deriv_dim[
i], nloop, tag, times);
423 int d = deriv_dim[
i];
426 std::cout << std::setw(5) << deriv_dim[
i] <<
" ";
427 file << deriv_dim[
i] <<
" ";
428 for (
int j=0; j<times.
size(); j++) {
429 double rel_time = times[j]/(times_func[j]*d);
430 std::cout << std::setw(w) << rel_time <<
" ";
431 file << rel_time <<
" ";
433 std::cout << std::endl;
443 print_times< Sacado::Fad::DFad<double> >(
444 "Sacado::Fad::DFad",
"fad_expr_depth_dfad.txt");
445 print_times< Sacado::ELRFad::DFad<double> >(
446 "Sacado::ELRFad::DFad",
"fad_expr_depth_elr_dfad.txt");
447 print_times< Sacado::CacheFad::DFad<double> >(
448 "Sacado::CacheFad::DFad",
"fad_expr_depth_cache_dfad.txt");
449 print_times< Sacado::ELRCacheFad::DFad<double> >(
450 "Sacado::ELRCacheFad::DFad",
"fad_expr_depth_elr_cache_dfad.txt");
451 print_times< Sacado::Fad::SimpleFad<double> >(
452 "Sacado::Fad::SimpleFad",
"fad_expr_depth_simple_fad.txt");
454 print_times_adolc(
"ADOL-C",
"fad_expr_depth_adolc.txt");
void do_times(const T x[], int nloop, Teuchos::Array< double > ×)
Sacado::Fad::DFad< double > FadType
ScalarT number()
Get random number.
void start(bool reset=false)
static const char * mult_names[nfunc]
static const char * add_names[nfunc]
void print_times(const std::string &screen_name, const std::string &file_name)
double do_time(int nderiv, int nloop)
double totalElapsedTime(bool readCurrentTime=false) const
static const char * nest_names[nfunc]