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 // $Id$
2 // $Source$
3 // @HEADER
4 // ***********************************************************************
5 //
6 // Sacado Package
7 // Copyright (2006) Sandia Corporation
8 //
9 // Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
10 // the U.S. Government retains certain rights in this software.
11 //
12 // This library is free software; you can redistribute it and/or modify
13 // it under the terms of the GNU Lesser General Public License as
14 // published by the Free Software Foundation; either version 2.1 of the
15 // License, or (at your option) any later version.
16 //
17 // This library is distributed in the hope that it will be useful, but
18 // WITHOUT ANY WARRANTY; without even the implied warranty of
19 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 // Lesser General Public License for more details.
21 //
22 // You should have received a copy of the GNU Lesser General Public
23 // License along with this library; if not, write to the Free Software
24 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
25 // USA
26 // Questions? Contact David M. Gay (dmgay@sandia.gov) or Eric T. Phipps
27 // (etphipp@sandia.gov).
28 //
29 // ***********************************************************************
30 // @HEADER
31 
32 // dfad_sfc_example
33 //
34 // usage:
35 // dfad_sfc_example
36 //
37 // output:
38 // Uses the scalar flop counter to count the flops for a derivative
39 // of a simple function using Sacado::Rad::ADvar
40 
41 #include <iostream>
42 #include <iomanip>
43 
44 #include "Sacado_No_Kokkos.hpp"
45 
46 // The function to differentiate
47 template <typename ScalarT>
48 ScalarT func(const ScalarT& a, const ScalarT& b, const ScalarT& c) {
49  ScalarT r = c*std::log(b+1.)/std::sin(a);
50 
51  return r;
52 }
53 
54 // The analytic derivative of func(a,b,c) with respect to a and b
55 template <typename ScalarT>
56 void func_deriv(const ScalarT& a, const ScalarT& b, const ScalarT& c,
57  ScalarT& drda, ScalarT& drdb)
58 {
59  drda = -(c*std::log(b+1.)/std::pow(std::sin(a),2.))*std::cos(a);
60  drdb = c / ((b+1.)*std::sin(a));
61 }
62 
65 
66 int main(int argc, char **argv)
67 {
68  double pi = std::atan(1.0)*4.0;
69 
70  // Values of function arguments
71  double a = pi/4;
72  double b = 2.0;
73  double c = 3.0;
74 
75  // Compute function
76  SFC as(a);
77  SFC bs(b);
78  SFC cs(c);
80  SFC rs = func(as, bs, cs);
82 
83  std::cout << "Flop counts for function evaluation:";
84  SFC::printCounters(std::cout);
85 
86  // Compute derivative analytically
87  SFC drdas, drdbs;
89  func_deriv(as, bs, cs, drdas, drdbs);
91 
92  std::cout << "\nFlop counts for analytic derivative evaluation:";
93  SFC::printCounters(std::cout);
94 
95  // Compute function and derivative with AD
96  RAD_SFC arad(a);
97  RAD_SFC brad(b);
98  RAD_SFC crad(c);
100  RAD_SFC rrad = func(arad, brad, crad);
103 
104  std::cout << "\nFlop counts for AD function and derivative evaluation:";
105  SFC::printCounters(std::cout);
106 
107  // Extract value and derivatives
108  double r = rs.val(); // r
109  double drda = drdas.val(); // dr/da
110  double drdb = drdbs.val(); // dr/db
111 
112  double r_ad = rrad.val().val(); // r
113  double drda_ad = arad.adj().val(); // dr/da
114  double drdb_ad = brad.adj().val(); // dr/db
115 
116  // Print the results
117  int p = 4;
118  int w = p+7;
119  std::cout.setf(std::ios::scientific);
120  std::cout.precision(p);
121  std::cout << "\nValues/derivatives of computation" << std::endl
122  << " r = " << r << " (original) == " << std::setw(w) << r_ad
123  << " (AD) Error = " << std::setw(w) << r - r_ad << std::endl
124  << "dr/da = " << std::setw(w) << drda << " (analytic) == "
125  << std::setw(w) << drda_ad << " (AD) Error = " << std::setw(w)
126  << drda - drda_ad << std::endl
127  << "dr/db = " << std::setw(w) << drdb << " (analytic) == "
128  << std::setw(w) << drdb_ad << " (AD) Error = " << std::setw(w)
129  << drdb - drdb_ad << std::endl;
130 
131  double tol = 1.0e-14;
133  // The Solaris and Irix CC compilers get higher counts for operator+=
134  // and operator* than does g++.
135  // The test on fc.totalFlopCount allows for this variation.
136  if (std::fabs(r - r_ad) < tol &&
137  std::fabs(drda - drda_ad) < tol &&
138  std::fabs(drdb - drdb_ad) < tol&&
139  (fc.totalFlopCount == 27 || fc.totalFlopCount == 29)) {
140  std::cout << "\nExample passed!" << std::endl;
141  return 0;
142  }
143  else {
144  std::cout <<"\nSomething is wrong, example failed!" << std::endl;
145  return 1;
146  }
147 }
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())
KOKKOS_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)
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:191
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:49
static void finalizeCounters()
Finalize total flop count after block of computations.
Sacado::FlopCounterPack::ScalarFlopCounter< double > SFC
fabs(expr.val())
cos(expr.val())