37 template <
typename ExecSpace>
39 static const bool value =
false;
42 #ifdef KOKKOS_ENABLE_CUDA
45 static const bool value =
true;
49 template <
typename scalar>
52 const size_t n2,
const size_t n3,
const int fad_size,
53 const size_t i0,
const size_t i1,
54 const size_t i2,
const size_t i3,
57 const scalar x0 = 10.0 + scalar(n0) / scalar(i0+1);
58 const scalar x1 = 100.0 + scalar(n1) / scalar(i1+1);
59 const scalar x2 = 1000.0 + scalar(n2) / scalar(i2+1);
60 const scalar x3 = 10000.0 + scalar(n3) / scalar(i3+1);
61 const scalar x = x0 + x1 + x2 + x3;
62 if (i_fad == fad_size)
64 const scalar x_fad = 1.0 + scalar(fad_size) / scalar(i_fad+1);
68 template <
typename V1,
typename V2,
typename V3,
typename V4,
typename V5>
69 void init_fad(
const V1& v1,
const V2& v2,
const V3& v3,
const V4& v4,
72 typedef typename V1::non_const_value_type::value_type scalar;
74 const int ncells = v1.extent(0);
75 const int num_basis = v1.extent(1);
76 const int num_points = v1.extent(2);
77 const int ndim = v1.extent(3);
78 const int N = Kokkos::dimension_scalar(v1)-1;
85 auto v1_h = Kokkos::create_mirror_view(v1);
86 auto v2_h = Kokkos::create_mirror_view(v2);
87 auto v3_h = Kokkos::create_mirror_view(v3);
88 auto v4_h = Kokkos::create_mirror_view(v4);
89 for (
int cell=0; cell<ncells; ++cell) {
90 for (
int basis=0; basis<num_basis; ++basis) {
91 for (
int qp=0; qp<num_points; ++qp) {
92 for (
int dim=0; dim<ndim; ++dim) {
93 for (
int i=0; i<
N; ++i)
94 v1_h(cell,basis,qp,dim).fastAccessDx(i) =
95 generate_fad<scalar>(ncells,num_basis,num_points,ndim,
N,cell,basis,qp,dim,i);
96 v1_h(cell,basis,qp,dim).val() =
97 generate_fad<scalar>(ncells,num_basis,num_points,ndim,
N,cell,basis,qp,dim,
N);
99 for (
int i=0; i<
N; ++i)
100 v2_h(cell,basis,qp).fastAccessDx(i) =
101 generate_fad<scalar>(ncells,num_basis,num_points,1,
N,cell,basis,qp,0,i);
102 v2_h(cell,basis,qp).val() =
103 generate_fad<scalar>(ncells,num_basis,num_points,1,
N,cell,basis,qp,0,
N);
106 for (
int qp=0; qp<num_points; ++qp) {
107 for (
int dim=0; dim<ndim; ++dim) {
108 for (
int i=0; i<
N; ++i)
109 v3_h(cell,qp,dim).fastAccessDx(i) =
110 generate_fad<scalar>(ncells,1,num_points,ndim,
N,cell,0,qp,dim,i);
111 v3_h(cell,qp,dim).val() =
112 generate_fad<scalar>(ncells,1,num_points,ndim,
N,cell,0,qp,dim,
N);
114 for (
int i=0; i<
N; ++i)
115 v4_h(cell,qp).fastAccessDx(i) =
116 generate_fad<scalar>(ncells,1,num_points,1,
N,cell,0,qp,0,i);
117 v4_h(cell,qp).val() =
118 generate_fad<scalar>(ncells,1,num_points,1,
N,cell,0,qp,0,
N);
122 Kokkos::deep_copy( v1, v1_h );
123 Kokkos::deep_copy( v2, v2_h );
124 Kokkos::deep_copy( v3, v3_h );
125 Kokkos::deep_copy( v4, v4_h );
127 Kokkos::deep_copy(
typename V5::array_type(v5), 0.0);
130 template <
typename V1,
typename V2,
typename V3,
typename V4,
typename V5>
131 void init_array(
const V1& v1,
const V2& v2,
const V3& v3,
const V4& v4,
134 typedef typename V1::non_const_value_type scalar;
136 const int ncells = v1.extent(0);
137 const int num_basis = v1.extent(1);
138 const int num_points = v1.extent(2);
139 const int ndim = v1.extent(3);
140 const int N = v1.extent(4)-1;
147 auto v1_h = Kokkos::create_mirror_view(v1);
148 auto v2_h = Kokkos::create_mirror_view(v2);
149 auto v3_h = Kokkos::create_mirror_view(v3);
150 auto v4_h = Kokkos::create_mirror_view(v4);
151 for (
int cell=0; cell<ncells; ++cell) {
152 for (
int basis=0; basis<num_basis; ++basis) {
153 for (
int qp=0; qp<num_points; ++qp) {
154 for (
int dim=0; dim<ndim; ++dim) {
155 for (
int i=0; i<
N; ++i)
156 v1_h(cell,basis,qp,dim,i) =
157 generate_fad<scalar>(ncells,num_basis,num_points,ndim,
N,cell,basis,qp,dim,i);
158 v1_h(cell,basis,qp,dim,N) =
159 generate_fad<scalar>(ncells,num_basis,num_points,ndim,
N,cell,basis,qp,dim,
N);
161 for (
int i=0; i<
N; ++i)
162 v2_h(cell,basis,qp,i) =
163 generate_fad<scalar>(ncells,num_basis,num_points,1,
N,cell,basis,qp,0,i);
164 v2_h(cell,basis,qp,N) =
165 generate_fad<scalar>(ncells,num_basis,num_points,1,
N,cell,basis,qp,0,
N);
168 for (
int qp=0; qp<num_points; ++qp) {
169 for (
int dim=0; dim<ndim; ++dim) {
170 for (
int i=0; i<
N; ++i)
171 v3_h(cell,qp,dim,i) =
172 generate_fad<scalar>(ncells,1,num_points,ndim,
N,cell,0,qp,dim,i);
173 v3_h(cell,qp,dim,N) =
174 generate_fad<scalar>(ncells,1,num_points,ndim,
N,cell,0,qp,dim,
N);
176 for (
int i=0; i<
N; ++i)
178 generate_fad<scalar>(ncells,1,num_points,1,
N,cell,0,qp,0,i);
180 generate_fad<scalar>(ncells,1,num_points,1,
N,cell,0,qp,0,
N);
184 Kokkos::deep_copy( v1, v1_h );
185 Kokkos::deep_copy( v2, v2_h );
186 Kokkos::deep_copy( v3, v3_h );
187 Kokkos::deep_copy( v4, v4_h );
189 Kokkos::deep_copy(
typename V5::array_type(v5), 0.0);
192 template <
typename View1,
typename View2>
193 typename std::enable_if< !Kokkos::is_view_fad<View2>::value,
bool>::type
194 check(
const View1& v_gold,
const View2& v,
const double tol)
197 typename View1::HostMirror v_gold_h = Kokkos::create_mirror_view(v_gold);
198 typename View2::HostMirror v_h = Kokkos::create_mirror_view(v);
199 Kokkos::deep_copy(v_gold_h, v_gold);
200 Kokkos::deep_copy(v_h, v);
202 typedef typename View1::value_type value_type;
204 const size_t n0 = v_gold_h.extent(0);
205 const size_t n1 = v_gold_h.extent(1);
206 const size_t n2 = v_gold_h.extent(2);
209 for (
size_t i0 = 0 ; i0 < n0 ; ++i0 ) {
210 for (
size_t i1 = 0 ; i1 < n1 ; ++i1 ) {
211 for (
size_t i2 = 0 ; i2 < n2 ; ++i2 ) {
212 value_type x_gold = v_gold_h(i0,i1,i2);
213 value_type x = v_h(i0,i1,i2);
215 std::cout <<
"Comparison failed! x_gold("
216 << i0 <<
"," << i1 <<
"," << i2 <<
") = "
217 << x_gold <<
" , x = " << x
228 template <
typename View1,
typename View2>
229 typename std::enable_if< Kokkos::is_view_fad<View2>::value,
bool>::type
230 check(
const View1& v_gold,
const View2& v,
const double tol)
233 typename View1::HostMirror v_gold_h = Kokkos::create_mirror_view(v_gold);
234 typename View2::HostMirror v_h = Kokkos::create_mirror_view(v);
235 Kokkos::deep_copy(v_gold_h, v_gold);
236 Kokkos::deep_copy(v_h, v);
238 typedef typename View1::value_type value_type;
240 const size_t n0 = v_gold_h.extent(0);
241 const size_t n1 = v_gold_h.extent(1);
242 const size_t n2 = v_gold_h.extent(2);
245 for (
size_t i0 = 0 ; i0 < n0 ; ++i0 ) {
246 for (
size_t i1 = 0 ; i1 < n1 ; ++i1 ) {
247 for (
size_t i2 = 0 ; i2 < n2 ; ++i2 ) {
248 value_type x_gold = v_gold_h(i0,i1,i2);
249 value_type x = (i2 == n2-1) ? v_h(i0,i1).val() : v_h(i0,i1).dx(i2);
251 std::cout <<
"Comparison failed! x_gold("
252 << i0 <<
"," << i1 <<
"," << i2 <<
") = "
253 << x_gold <<
" , x = " << x
264 template<
typename FluxView,
typename WgbView,
typename SrcView,
266 Kokkos::View<double***,typename FluxView::execution_space>
268 const FluxView& flux,
const WgbView& wgb,
const SrcView& src,
270 typename std::enable_if< Kokkos::is_view_fad<FluxView>::value>::type* = 0)
272 typedef typename FluxView::execution_space execution_space;
274 const size_t num_cells = wgb.extent(0);
275 const int num_basis = wgb.extent(1);
276 const int num_points = wgb.extent(2);
277 const int num_dim = wgb.extent(3);
278 const int N = Kokkos::dimension_scalar(wgb)-1;
280 Kokkos::View<double***,typename FluxView::execution_space> residual(
281 "",num_cells,num_basis,N+1);
283 Kokkos::parallel_for(Kokkos::RangePolicy<execution_space>( 0,num_cells ),
284 KOKKOS_LAMBDA (
const size_t cell)
286 double value, value2;
289 for (
int basis=0; basis<num_basis; ++basis) {
290 value = value2 = 0.0;
291 for (
int qp=0; qp<num_points; ++qp) {
292 for (
int dim=0; dim<num_dim; ++dim)
293 value += flux(cell,qp,dim).
val()*wgb(cell,basis,qp,dim).
val();
294 value2 += src(cell,qp).val()*wbs(cell,basis,qp).val();
296 residual(cell,basis,N) = value+value2;
300 for (
int k=0; k<
N; ++k) {
301 for (
int basis=0; basis<num_basis; ++basis) {
302 value = value2 = 0.0;
303 for (
int qp=0; qp<num_points; ++qp) {
304 for (
int dim=0; dim<num_dim; ++dim)
306 flux(cell,qp,dim).val()*wgb(cell,basis,qp,dim).dx(k) +
307 flux(cell,qp,dim).dx(k)*wgb(cell,basis,qp,dim).val();
309 src(cell,qp).val()*wbs(cell,basis,qp).dx(k) +
310 src(cell,qp).dx(k)*wbs(cell,basis,qp).val();
312 residual(cell,basis,k) = value+value2;
320 template<
typename FluxView,
typename WgbView,
typename SrcView,
322 Kokkos::View<double***,typename FluxView::execution_space>
324 const FluxView& flux,
const WgbView& wgb,
const SrcView& src,
326 typename std::enable_if< !Kokkos::is_view_fad<FluxView>::value>::type* = 0)
328 typedef typename FluxView::execution_space execution_space;
330 const size_t num_cells = wgb.extent(0);
331 const int num_basis = wgb.extent(1);
332 const int num_points = wgb.extent(2);
333 const int num_dim = wgb.extent(3);
334 const int N = wgb.extent(4)-1;
336 Kokkos::View<double***,typename FluxView::execution_space> residual(
337 "",num_cells,num_basis,N+1);
339 Kokkos::parallel_for(Kokkos::RangePolicy<execution_space>( 0,num_cells ),
340 KOKKOS_LAMBDA (
const size_t cell)
342 double value, value2;
345 for (
int basis=0; basis<num_basis; ++basis) {
346 value = value2 = 0.0;
347 for (
int qp=0; qp<num_points; ++qp) {
348 for (
int dim=0; dim<num_dim; ++dim)
349 value += flux(cell,qp,dim,N)*wgb(cell,basis,qp,dim,N);
350 value2 += src(cell,qp,N)*wbs(cell,basis,qp,N);
352 residual(cell,basis,N) = value+value2;
356 for (
int k=0; k<
N; ++k) {
357 for (
int basis=0; basis<num_basis; ++basis) {
358 value = value2 = 0.0;
359 for (
int qp=0; qp<num_points; ++qp) {
360 for (
int dim=0; dim<num_dim; ++dim)
362 flux(cell,qp,dim,N)*wgb(cell,basis,qp,dim,k) +
363 flux(cell,qp,dim,k)*wgb(cell,basis,qp,dim,N);
365 src(cell,qp,N)*wbs(cell,basis,qp,k) +
366 src(cell,qp,k)*wbs(cell,basis,qp,N);
368 residual(cell,basis,k) = value+value2;
376 template<
typename FluxView,
typename WgbView,
typename SrcView,
377 typename WbsView,
typename ResidualView>
379 const SrcView& src,
const WbsView& wbs,
380 const ResidualView& residual)
386 const double tol = 1.0e-14;
387 check(residual_gold, residual, tol);
std::enable_if< !Kokkos::is_view_fad< View2 >::value, bool >::type check(const View1 &v_gold, const View2 &v, const double tol)
scalar generate_fad(const size_t n0, const size_t n1, const size_t n2, const size_t n3, const int fad_size, const size_t i0, const size_t i1, const size_t i2, const size_t i3, const int i_fad)
Sacado::Fad::SFad< double, fad_dim > SFadType
Sacado::Fad::DFad< double > DFadType
Kokkos::View< double ***, typename FluxView::execution_space > compute_gold_residual(const FluxView &flux, const WgbView &wgb, const SrcView &src, const WbsView &wbs, typename std::enable_if< Kokkos::is_view_fad< FluxView >::value >::type *=0)
void init_array(const V1 &v1, const V2 &v2, const V3 &v3, const V4 &v4, const V5 &v5)
void init_fad(const V1 &v1, const V2 &v2, const V3 &v3, const V4 &v4, const V5 &v5)
Sacado::Fad::SLFad< double, fad_dim > SLFadType
void check_residual(const FluxView &flux, const WgbView &wgb, const SrcView &src, const WbsView &wbs, const ResidualView &residual)