22 #if defined(HAVE_SACADO_VIEW_SPEC) && !defined(SACADO_DISABLE_FAD_VIEW_SPEC)
25 template <
typename ViewTypeA,
typename ViewTypeB,
typename ViewTypeC>
34 MatVec(
const ViewTypeA& A_,
const ViewTypeB& b_,
const ViewTypeC&
c_) :
36 m(
A.extent(0)),
n(
A.extent(1))
38 typedef typename ViewTypeC::execution_space execution_space;
39 Kokkos::parallel_for(Kokkos::RangePolicy<execution_space>(0,m), *
this);
42 KOKKOS_INLINE_FUNCTION
43 void operator() (
const size_t i)
const {
44 typedef typename ViewTypeC::value_type scalar_type;
47 for (
size_t j=0; j<
n; ++j) {
48 scalar_type bb = b(j);
56 template <
typename ViewTypeA,
typename ViewTypeB,
typename ViewTypeC>
57 void run_mat_vec(
const ViewTypeA&
A,
const ViewTypeB& b,
const ViewTypeC&
c)
59 MatVec<ViewTypeA,ViewTypeB,ViewTypeC>
f(A,b,c);
64 template <
typename ViewTypeA,
typename ViewTypeB,
typename ViewTypeC>
73 MatVecDeriv(
const ViewTypeA& A_,
const ViewTypeB& b_,
const ViewTypeC& c_) :
75 m(
A.extent(0)),
n(
A.extent(1)),
p(
A.extent(2)-1)
77 typedef typename ViewTypeC::execution_space execution_space;
78 Kokkos::parallel_for(Kokkos::RangePolicy<execution_space>(0,m), *
this);
81 KOKKOS_INLINE_FUNCTION
82 void operator() (
const size_t i)
const {
83 typedef typename ViewTypeC::value_type scalar_type;
86 for (
size_t k=0; k<
p; ++k) {
88 for (
size_t j=0; j<
n; ++j)
89 t +=
A(i,j,k)*b(j,p) +
A(i,j,p)*b(j,k);
95 for (
size_t j=0; j<n; ++j)
102 template <
typename ViewTypeA,
typename ViewTypeB,
typename ViewTypeC>
106 MatVecDeriv<ViewTypeA,ViewTypeB,ViewTypeC>
f(A,b,c);
111 int main(
int argc,
char* argv[]) {
114 #if defined(HAVE_SACADO_VIEW_SPEC) && !defined(SACADO_DISABLE_FAD_VIEW_SPEC)
116 Kokkos::initialize(argc, argv);
126 Kokkos::View<FadType**>
A(
"A",m,n,p+1);
127 Kokkos::View<FadType*> b(
"b",n,p+1);
128 Kokkos::View<FadType*>
c(
"c",m,p+1);
131 Kokkos::deep_copy( A,
FadType(p, 0, 2.0) );
132 Kokkos::deep_copy( b,
FadType(p, 0, 3.0) );
133 Kokkos::deep_copy( c, 0.0 );
139 std::cout <<
"\nc = A*b: Differentiated using Sacado:" << std::endl;
140 auto h_c = Kokkos::create_mirror_view(c);
141 Kokkos::deep_copy(h_c, c);
142 for (
size_t i=0; i<m; ++
i)
143 std::cout <<
"\tc(" << i <<
") = " << h_c(i) << std::endl;
148 Kokkos::View<FadType*> c2(
"c",m,p+1);
149 Kokkos::View<double***> A_flat =
A;
150 Kokkos::View<double**> b_flat = b;
151 Kokkos::View<double**> c_flat = c2;
155 std::cout <<
"\nc = A*b: Differentiated analytically:" << std::endl;
156 auto h_c2 = Kokkos::create_mirror_view(c2);
157 Kokkos::deep_copy(h_c2, c2);
158 for (
size_t i=0; i<m; ++
i)
159 std::cout <<
"\tc(" << i <<
") = " << h_c2(i) << std::endl;
163 for (
size_t i=0; i<m; ++
i) {
164 for (
size_t k=0; k<
p; ++k)
169 double tol = 1.0e-14;
172 std::cout <<
"\nExample passed!" << std::endl;
176 std::cout <<
"\nSomething is wrong, example failed!" << std::endl;
void run_mat_vec(const ViewTypeA &A, const ViewTypeB &b, const ViewTypeC &c)
Sacado::Fad::DFad< double > FadType
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
void run_mat_vec_deriv(const ViewTypeA &A, const ViewTypeB &b, const ViewTypeC &c)
expr expr expr fastAccessDx(i)) FAD_UNARYOP_MACRO(exp