Sacado Package Browser (Single Doxygen Collection)  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
dfad_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 DFad
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  // Number of independent variables
54  int num_deriv = 2;
55 
56  // Compute function
57  SFC as(a);
58  SFC bs(b);
59  SFC cs(c);
61  SFC rs = func(as, bs, cs);
63 
64  std::cout << "Flop counts for function evaluation:";
65  SFC::printCounters(std::cout);
66 
67  // Compute derivative analytically
68  SFC drdas, drdbs;
70  func_deriv(as, bs, cs, drdas, drdbs);
72 
73  std::cout << "\nFlop counts for analytic derivative evaluation:";
74  SFC::printCounters(std::cout);
75 
76  // Compute function and derivative with AD
77  FAD_SFC afad(num_deriv, 0, a);
78  FAD_SFC bfad(num_deriv, 1, b);
79  FAD_SFC cfad(c);
81  FAD_SFC rfad = func(afad, bfad, cfad);
83 
84  std::cout << "\nFlop counts for AD function and derivative evaluation:";
85  SFC::printCounters(std::cout);
86 
87  // Extract value and derivatives
88  double r = rs.val(); // r
89  double drda = drdas.val(); // dr/da
90  double drdb = drdbs.val(); // dr/db
91 
92  double r_ad = rfad.val().val(); // r
93  double drda_ad = rfad.dx(0).val(); // dr/da
94  double drdb_ad = rfad.dx(1).val(); // dr/db
95 
96  // Print the results
97  int p = 4;
98  int w = p+7;
99  std::cout.setf(std::ios::scientific);
100  std::cout.precision(p);
101  std::cout << "\nValues/derivatives of computation" << std::endl
102  << " r = " << r << " (original) == " << std::setw(w) << r_ad
103  << " (AD) Error = " << std::setw(w) << r - r_ad << std::endl
104  << "dr/da = " << std::setw(w) << drda << " (analytic) == "
105  << std::setw(w) << drda_ad << " (AD) Error = " << std::setw(w)
106  << drda - drda_ad << std::endl
107  << "dr/db = " << std::setw(w) << drdb << " (analytic) == "
108  << std::setw(w) << drdb_ad << " (AD) Error = " << std::setw(w)
109  << drdb - drdb_ad << std::endl;
110 
111  double tol = 1.0e-14;
113  // The Solaris and Irix CC compilers get higher counts for operator=
114  // than does g++, which avoids an extra copy when returning a function value.
115  // The test on fc.totalFlopCount allows for this variation.
116  if (std::fabs(r - r_ad) < tol &&
117  std::fabs(drda - drda_ad) < tol &&
118  std::fabs(drdb - drdb_ad) < tol &&
119  (fc.totalFlopCount == 40)) {
120  std::cout << "\nExample passed!" << std::endl;
121  return 0;
122  }
123  else {
124  std::cout <<"\nSomething is wrong, example failed!" << std::endl;
125  return 1;
126  }
127 }
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.
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())
Sacado::Fad::DFad< SFC > FAD_SFC
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())