41 template <
typename T,
typename F>
48 for (
int j=0; j<nloop; j++)
54 template <
typename T,
template <
typename,
int>
class F>
70 double do_time_adolc(
double *x,
double **seed,
int d,
int nloop,
76 adouble *x_ad =
new adouble[n];
77 for (
int i=0; i<n; i++)
86 double **jac =
new double*[1];
87 jac[0] =
new double[d];
89 for (
int j=0; j<nloop; j++)
90 forward(tag, 1, n, d, x, seed, &y, jac);
96 return timer.totalElapsedTime() / nloop;
99 template <
template <
typename,
int>
class F>
100 void do_times_adolc(
double *x,
double **seed,
int d,
int nloop,
104 times[i++] = do_time_adolc(x, seed, d, nloop, tag++,
F<adouble,1>());
105 times[i++] = do_time_adolc(x, seed, d, nloop, tag++,
F<adouble,2>());
106 times[i++] = do_time_adolc(x, seed, d, nloop, tag++,
F<adouble,3>());
107 times[i++] = do_time_adolc(x, seed, d, nloop, tag++,
F<adouble,4>());
108 times[i++] = do_time_adolc(x, seed, d, nloop, tag++,
F<adouble,5>());
109 times[i++] = do_time_adolc(x, seed, d, nloop, tag++,
F<adouble,10>());
110 times[i++] = do_time_adolc(x, seed, d, nloop, tag++,
F<adouble,15>());
111 times[i++] = do_time_adolc(x, seed, d, nloop, tag++,
F<adouble,20>());
115 template <
typename FadType>
116 void print_times(
const std::string& screen_name,
const std::string& file_name)
118 const int nderiv = 10;
119 int deriv_dim[nderiv] = { 0, 1, 3, 5, 10, 15, 20, 30, 40, 50 };
123 std::ofstream file(file_name.c_str(), std::ios::out);
126 std::cout.setf(std::ios::scientific);
127 std::cout.precision(p);
128 std::cout << screen_name <<
" Relative times (time/(func_time*nderiv)): "
130 std::cout << std::setw(5) <<
"deriv" <<
" ";
133 std::cout << std::endl;
134 std::cout <<
"===== ";
136 for (
int j=0; j<w; j++)
140 std::cout << std::endl;
146 int nloop_func = 10000000;
148 do_times<double,ExprFuncs::mult>(x, nloop_func, times_func);
151 for (
int i=0; i<nderiv; i++) {
155 for (
int j=0; j<deriv_dim[i]; j++) {
156 fx[k].fastAccessDx(j) = urand.
number();
161 int nloop =
static_cast<int>(100000.0);
163 do_times<FadType,ExprFuncs::mult>(fx, nloop, times);
166 int d = deriv_dim[i];
169 std::cout << std::setw(5) << deriv_dim[i] <<
" ";
170 file << deriv_dim[i] <<
" ";
171 for (
int j=0; j<times.
size(); j++) {
172 double rel_time = times[j]/(times_func[j]*d);
173 std::cout << std::setw(w) << rel_time <<
" ";
174 file << rel_time <<
" ";
176 std::cout << std::endl;
182 std::cout.setf(std::ios::scientific);
183 std::cout.precision(p);
184 std::cout << screen_name <<
" Relative times (time/(func_time*nderiv)): "
186 std::cout << std::setw(5) <<
"deriv" <<
" ";
189 std::cout << std::endl;
190 std::cout <<
"===== ";
192 for (
int j=0; j<w; j++)
196 std::cout << std::endl;
202 int nloop_func = 10000000;
204 do_times<double,ExprFuncs::add>(x, nloop_func, times_func);
207 for (
int i=0; i<nderiv; i++) {
211 for (
int j=0; j<deriv_dim[i]; j++) {
212 fx[k].fastAccessDx(j) = urand.
number();
217 int nloop =
static_cast<int>(100000.0);
219 do_times<FadType,ExprFuncs::add>(fx, nloop, times);
222 int d = deriv_dim[i];
225 std::cout << std::setw(5) << deriv_dim[i] <<
" ";
226 file << deriv_dim[i] <<
" ";
227 for (
int j=0; j<times.
size(); j++) {
228 double rel_time = times[j]/(times_func[j]*d);
229 std::cout << std::setw(w) << rel_time <<
" ";
230 file << rel_time <<
" ";
232 std::cout << std::endl;
238 std::cout.setf(std::ios::scientific);
239 std::cout.precision(p);
240 std::cout << screen_name <<
" Relative times (time/(func_time*nderiv)): "
242 std::cout << std::setw(5) <<
"deriv" <<
" ";
245 std::cout << std::endl;
246 std::cout <<
"===== ";
248 for (
int j=0; j<w; j++)
252 std::cout << std::endl;
258 int nloop_func = 10000000;
260 do_times<double,ExprFuncs::nest>(x, nloop_func, times_func);
263 for (
int i=0; i<nderiv; i++) {
267 for (
int j=0; j<deriv_dim[i]; j++) {
268 fx[k].fastAccessDx(j) = urand.
number();
273 int nloop =
static_cast<int>(100000.0);
275 do_times<FadType,ExprFuncs::nest>(fx, nloop, times);
278 int d = deriv_dim[i];
281 std::cout << std::setw(5) << deriv_dim[i] <<
" ";
282 file << deriv_dim[i] <<
" ";
283 for (
int j=0; j<times.
size(); j++) {
284 double rel_time = times[j]/(times_func[j]*d);
285 std::cout << std::setw(w) << rel_time <<
" ";
286 file << rel_time <<
" ";
288 std::cout << std::endl;
295 void print_times_adolc(
const std::string& screen_name,
296 const std::string& file_name)
298 const int nderiv = 10;
299 int deriv_dim[nderiv] = { 0, 1, 3, 5, 10, 15, 20, 30, 40, 50 };
300 const int deriv_max = 50;
304 std::ofstream file(file_name.c_str(), std::ios::out);
309 seed[i] =
new double[deriv_max];
310 for (
int j=0; j<deriv_max; j++)
311 seed[i][j] = urand.number();
315 std::cout.setf(std::ios::scientific);
316 std::cout.precision(p);
317 std::cout << screen_name <<
" Relative times (time/(func_time*nderiv)): "
319 std::cout << std::setw(5) <<
"deriv" <<
" ";
322 std::cout << std::endl;
323 std::cout <<
"===== ";
325 for (
int j=0; j<w; j++)
329 std::cout << std::endl;
334 x[i] = urand.number();
335 int nloop_func = 10000000;
337 do_times<double,ExprFuncs::mult>(x, nloop_func, times_func);
340 for (
int i=0; i<nderiv; i++) {
342 int nloop =
static_cast<int>(10000.0);
344 do_times_adolc<ExprFuncs::mult>(x, seed, deriv_dim[i], nloop, tag, times);
347 int d = deriv_dim[i];
350 std::cout << std::setw(5) << deriv_dim[i] <<
" ";
351 file << deriv_dim[i] <<
" ";
352 for (
int j=0; j<times.
size(); j++) {
353 double rel_time = times[j]/(times_func[j]*d);
354 std::cout << std::setw(w) << rel_time <<
" ";
355 file << rel_time <<
" ";
357 std::cout << std::endl;
363 std::cout.setf(std::ios::scientific);
364 std::cout.precision(p);
365 std::cout << screen_name <<
" Relative times (time/(func_time*nderiv)): "
367 std::cout << std::setw(5) <<
"deriv" <<
" ";
370 std::cout << std::endl;
371 std::cout <<
"===== ";
373 for (
int j=0; j<w; j++)
377 std::cout << std::endl;
382 x[i] = urand.number();
383 int nloop_func = 10000000;
385 do_times<double,ExprFuncs::add>(x, nloop_func, times_func);
388 for (
int i=0; i<nderiv; i++) {
390 int nloop =
static_cast<int>(10000.0);
392 do_times_adolc<ExprFuncs::add>(x, seed, deriv_dim[i], nloop, tag, times);
395 int d = deriv_dim[i];
398 std::cout << std::setw(5) << deriv_dim[i] <<
" ";
399 file << deriv_dim[i] <<
" ";
400 for (
int j=0; j<times.
size(); j++) {
401 double rel_time = times[j]/(times_func[j]*d);
402 std::cout << std::setw(w) << rel_time <<
" ";
403 file << rel_time <<
" ";
405 std::cout << std::endl;
411 std::cout.setf(std::ios::scientific);
412 std::cout.precision(p);
413 std::cout << screen_name <<
" Relative times (time/(func_time*nderiv)): "
415 std::cout << std::setw(5) <<
"deriv" <<
" ";
418 std::cout << std::endl;
419 std::cout <<
"===== ";
421 for (
int j=0; j<w; j++)
425 std::cout << std::endl;
430 x[i] = urand.number();
431 int nloop_func = 10000000;
433 do_times<double,ExprFuncs::nest>(x, nloop_func, times_func);
436 for (
int i=0; i<nderiv; i++) {
438 int nloop =
static_cast<int>(1000.0);
440 do_times_adolc<ExprFuncs::nest>(x, seed, deriv_dim[i], nloop, tag, times);
443 int d = deriv_dim[i];
446 std::cout << std::setw(5) << deriv_dim[i] <<
" ";
447 file << deriv_dim[i] <<
" ";
448 for (
int j=0; j<times.
size(); j++) {
449 double rel_time = times[j]/(times_func[j]*d);
450 std::cout << std::setw(w) << rel_time <<
" ";
451 file << rel_time <<
" ";
453 std::cout << std::endl;
463 print_times< Sacado::Fad::DFad<double> >(
464 "Sacado::Fad::DFad",
"fad_expr_depth_dfad.txt");
465 print_times< Sacado::ELRFad::DFad<double> >(
466 "Sacado::ELRFad::DFad",
"fad_expr_depth_elr_dfad.txt");
467 print_times< Sacado::CacheFad::DFad<double> >(
468 "Sacado::CacheFad::DFad",
"fad_expr_depth_cache_dfad.txt");
469 print_times< Sacado::ELRCacheFad::DFad<double> >(
470 "Sacado::ELRCacheFad::DFad",
"fad_expr_depth_elr_cache_dfad.txt");
471 print_times< Sacado::Fad::SimpleFad<double> >(
472 "Sacado::Fad::SimpleFad",
"fad_expr_depth_simple_fad.txt");
474 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]