Sacado Package Browser (Single Doxygen Collection)
Version of the Day
Toggle main menu visibility
Loading...
Searching...
No Matches
example
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
63
typedef
Sacado::FlopCounterPack::ScalarFlopCounter<double>
SFC
;
64
typedef
Sacado::Rad::ADvar<SFC>
RAD_SFC
;
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
);
79
SFC::resetCounters
();
80
SFC
rs =
func
(as, bs, cs);
81
SFC::finalizeCounters
();
82
83
std::cout <<
"Flop counts for function evaluation:"
;
84
SFC::printCounters
(std::cout);
85
86
// Compute derivative analytically
87
SFC
drdas, drdbs;
88
SFC::resetCounters
();
89
func_deriv
(as, bs, cs, drdas, drdbs);
90
SFC::finalizeCounters
();
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
);
99
SFC::resetCounters
();
100
RAD_SFC
rrad =
func
(arad, brad, crad);
101
RAD_SFC::Gradcomp
();
102
SFC::finalizeCounters
();
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;
132
Sacado::FlopCounterPack::FlopCounts
fc =
SFC::getCounters
();
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
}
a
a
Definition
Sacado_CacheFad_Ops.hpp:426
c
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
Definition
Sacado_LFad_LogicalSparseOps.hpp:450
Sacado_No_Kokkos.hpp
main
int main()
Definition
ad_example.cpp:191
Sacado::FlopCounterPack::FlopCounts
Class storing flop counts and summary flop counts.
Definition
Sacado_ScalarFlopCounter.hpp:46
Sacado::FlopCounterPack::FlopCounts::totalFlopCount
double totalFlopCount
Total flop count.
Definition
Sacado_ScalarFlopCounter.hpp:128
Sacado::FlopCounterPack::ScalarFlopCounter
Templated flop counter class.
Definition
Sacado_ScalarFlopCounter.hpp:194
Sacado::FlopCounterPack::ScalarFlopCounter< double >::printCounters
static std::ostream & printCounters(std::ostream &out)
Definition
Sacado_ScalarFlopCounter.hpp:225
Sacado::FlopCounterPack::ScalarFlopCounter< double >::getCounters
static FlopCounts getCounters()
Definition
Sacado_ScalarFlopCounter.hpp:219
Sacado::FlopCounterPack::ScalarFlopCounter< double >::resetCounters
static void resetCounters()
Definition
Sacado_ScalarFlopCounter.hpp:213
Sacado::FlopCounterPack::ScalarFlopCounter::val
const T & val() const
Return the current value.
Definition
Sacado_ScalarFlopCounter.hpp:246
Sacado::FlopCounterPack::ScalarFlopCounter< double >::finalizeCounters
static void finalizeCounters()
Definition
Sacado_ScalarFlopCounter.hpp:216
Sacado::Rad::ADvar
Definition
Sacado_trad.hpp:851
Sacado::Rad::ADvar< SFC >::Gradcomp
static void Gradcomp()
Definition
Sacado_trad.hpp:978
Sacado::Rad::IndepADvar::val
Double val() const
Definition
Sacado_trad.hpp:755
Sacado::Rad::IndepADvar::adj
Double adj() const
Definition
Sacado_trad.hpp:762
SFC
Sacado::FlopCounterPack::ScalarFlopCounter< double > SFC
Definition
dfad_sfc_example.cpp:61
std::pow
PowExprType< Expr< T1 >, Expr< T2 > >::expr_type pow(const Expr< T1 > &expr1, const Expr< T2 > &expr2)
Definition
Sacado_Tay_CacheTaylorOps.hpp:1730
std::atan
ATanExprType< T >::expr_type atan(const Expr< T > &expr)
Definition
Sacado_Tay_CacheTaylorOps.hpp:1838
func_deriv
void func_deriv(const ScalarT &a, const ScalarT &b, const ScalarT &c, ScalarT &drda, ScalarT &drdb)
Definition
trad_sfc_example.cpp:56
func
ScalarT func(const ScalarT &a, const ScalarT &b, const ScalarT &c)
Definition
trad_sfc_example.cpp:48
RAD_SFC
Sacado::Rad::ADvar< SFC > RAD_SFC
Definition
trad_sfc_example.cpp:64
tol
const double tol
Definition
tradoptest_01.cpp:61
Generated by
1.17.0