Sacado Package Browser (Single Doxygen Collection)  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
trad_sfc_example.cpp
Go to the documentation of this file.
1 // @HEADER
2 // *****************************************************************************
3 // Sacado Package
4 //
5 // Copyright 2006 NTESS and the Sacado contributors.
6 // SPDX-License-Identifier: LGPL-2.1-or-later
7 // *****************************************************************************
8 // @HEADER
9 
10 // dfad_sfc_example
11 //
12 // usage:
13 // dfad_sfc_example
14 //
15 // output:
16 // Uses the scalar flop counter to count the flops for a derivative
17 // of a simple function using Sacado::Rad::ADvar
18 
19 #include <iostream>
20 #include <iomanip>
21 
22 #include "Sacado_No_Kokkos.hpp"
23 
24 // The function to differentiate
25 template <typename ScalarT>
26 ScalarT func(const ScalarT& a, const ScalarT& b, const ScalarT& c) {
27  ScalarT r = c*std::log(b+1.)/std::sin(a);
28 
29  return r;
30 }
31 
32 // The analytic derivative of func(a,b,c) with respect to a and b
33 template <typename ScalarT>
34 void func_deriv(const ScalarT& a, const ScalarT& b, const ScalarT& c,
35  ScalarT& drda, ScalarT& drdb)
36 {
37  drda = -(c*std::log(b+1.)/std::pow(std::sin(a),2.))*std::cos(a);
38  drdb = c / ((b+1.)*std::sin(a));
39 }
40 
43 
44 int main(int argc, char **argv)
45 {
46  double pi = std::atan(1.0)*4.0;
47 
48  // Values of function arguments
49  double a = pi/4;
50  double b = 2.0;
51  double c = 3.0;
52 
53  // Compute function
54  SFC as(a);
55  SFC bs(b);
56  SFC cs(c);
58  SFC rs = func(as, bs, cs);
60 
61  std::cout << "Flop counts for function evaluation:";
62  SFC::printCounters(std::cout);
63 
64  // Compute derivative analytically
65  SFC drdas, drdbs;
67  func_deriv(as, bs, cs, drdas, drdbs);
69 
70  std::cout << "\nFlop counts for analytic derivative evaluation:";
71  SFC::printCounters(std::cout);
72 
73  // Compute function and derivative with AD
74  RAD_SFC arad(a);
75  RAD_SFC brad(b);
76  RAD_SFC crad(c);
78  RAD_SFC rrad = func(arad, brad, crad);
81 
82  std::cout << "\nFlop counts for AD function and derivative evaluation:";
83  SFC::printCounters(std::cout);
84 
85  // Extract value and derivatives
86  double r = rs.val(); // r
87  double drda = drdas.val(); // dr/da
88  double drdb = drdbs.val(); // dr/db
89 
90  double r_ad = rrad.val().val(); // r
91  double drda_ad = arad.adj().val(); // dr/da
92  double drdb_ad = brad.adj().val(); // dr/db
93 
94  // Print the results
95  int p = 4;
96  int w = p+7;
97  std::cout.setf(std::ios::scientific);
98  std::cout.precision(p);
99  std::cout << "\nValues/derivatives of computation" << std::endl
100  << " r = " << r << " (original) == " << std::setw(w) << r_ad
101  << " (AD) Error = " << std::setw(w) << r - r_ad << std::endl
102  << "dr/da = " << std::setw(w) << drda << " (analytic) == "
103  << std::setw(w) << drda_ad << " (AD) Error = " << std::setw(w)
104  << drda - drda_ad << std::endl
105  << "dr/db = " << std::setw(w) << drdb << " (analytic) == "
106  << std::setw(w) << drdb_ad << " (AD) Error = " << std::setw(w)
107  << drdb - drdb_ad << std::endl;
108 
109  double tol = 1.0e-14;
111  // The Solaris and Irix CC compilers get higher counts for operator+=
112  // and operator* than does g++.
113  // The test on fc.totalFlopCount allows for this variation.
114  if (std::fabs(r - r_ad) < tol &&
115  std::fabs(drda - drda_ad) < tol &&
116  std::fabs(drdb - drdb_ad) < tol&&
117  (fc.totalFlopCount == 27 || fc.totalFlopCount == 29)) {
118  std::cout << "\nExample passed!" << std::endl;
119  return 0;
120  }
121  else {
122  std::cout <<"\nSomething is wrong, example failed!" << std::endl;
123  return 1;
124  }
125 }
const char * p
static FlopCounts getCounters()
Get the flop counts after a block of computations.
static void resetCounters()
Reset static flop counters before starting a block of computations.
Sacado::Rad::ADvar< SFC > RAD_SFC
static void Gradcomp()
const T & val() const
Return the current value.
atan(expr.val())
expr expr1 expr1 expr1 c expr2 expr1 expr2 expr1 expr2 expr1 expr1 expr1 expr1 c expr2 expr1 expr2 expr1 expr2 expr1 expr1 expr1 expr1 c *expr2 expr1 expr2 expr1 expr2 expr1 expr1 expr1 expr1 c expr2 expr1 expr2 expr1 expr2 expr1 expr1 expr1 expr2 expr1 expr2 expr1 expr1 expr1 expr2 expr1 expr2 expr1 expr1 expr1 c
static std::ostream & printCounters(std::ostream &out)
Print the current static flop counts to out.
int main()
Definition: ad_example.cpp:171
Class storing flop counts and summary flop counts.
TypeTo as(const TypeFrom &t)
void func_deriv(double a, double b, double c, double &drda, double &drdb)
sin(expr.val())
log(expr.val())
const double tol
const T func(int n, T *x)
Definition: ad_example.cpp:29
SACADO_INLINE_FUNCTION mpl::enable_if_c< ExprLevel< Expr< T1 > >::value==ExprLevel< Expr< T2 > >::value, Expr< PowerOp< Expr< T1 >, Expr< T2 > > > >::type pow(const Expr< T1 > &expr1, const Expr< T2 > &expr2)
static void finalizeCounters()
Finalize total flop count after block of computations.
Sacado::FlopCounterPack::ScalarFlopCounter< double > SFC
fabs(expr.val())
cos(expr.val())