Panzer  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Panzer_ExprEval_impl.hpp
Go to the documentation of this file.
1 // @HEADER
2 // *****************************************************************************
3 // Panzer: A partial differential equation assembly
4 // engine for strongly coupled complex multiphysics systems
5 //
6 // Copyright 2011 NTESS and the Panzer contributors.
7 // SPDX-License-Identifier: BSD-3-Clause
8 // *****************************************************************************
9 // @HEADER
10 
11 #ifndef PANZER_EXPR_EVAL_IMPL_HPP
12 #define PANZER_EXPR_EVAL_IMPL_HPP
13 
14 #include <Panzer_ExprEval.hpp>
15 
16 #include <algorithm>
17 #include <cmath>
18 
19 namespace panzer
20 {
21 namespace Expr
22 {
23 
24 struct ScalarTernary {
25  template <typename T>
26  static KOKKOS_FORCEINLINE_FUNCTION
27  T apply(bool cond, T const& left, T const& right) {
28  return cond ? left : right;
29  }
30 };
31 
32 struct ScalarOr {
33  static KOKKOS_FORCEINLINE_FUNCTION
34  bool apply(bool left, bool right) {
35  return left || right;
36  }
37 };
38 
39 struct ScalarAnd {
40  static KOKKOS_FORCEINLINE_FUNCTION
41  bool apply(bool left, bool right) {
42  return left && right;
43  }
44 };
45 
46 struct ScalarGT {
47  template <typename T>
48  static KOKKOS_FORCEINLINE_FUNCTION
49  bool apply(T const& left, T const& right) {
50  return left > right;
51  }
52 };
53 
54 struct ScalarLT {
55  template <typename T>
56  static KOKKOS_FORCEINLINE_FUNCTION
57  bool apply(T const& left, T const& right) {
58  return left < right;
59  }
60 };
61 
62 struct ScalarGEQ {
63  template <typename T>
64  static KOKKOS_FORCEINLINE_FUNCTION
65  bool apply(T const& left, T const& right) {
66  return left >= right;
67  }
68 };
69 
70 struct ScalarLEQ {
71  template <typename T>
72  static KOKKOS_FORCEINLINE_FUNCTION
73  bool apply(T const& left, T const& right) {
74  return left <= right;
75  }
76 };
77 
78 struct ScalarEQ {
79  template <typename T>
80  static KOKKOS_FORCEINLINE_FUNCTION
81  bool apply(T const& left, T const& right) {
82  return left == right;
83  }
84 };
85 
86 struct ScalarAdd {
87  template <typename T>
88  static KOKKOS_FORCEINLINE_FUNCTION
89  T apply(T const& left, T const& right) {
90  return left + right;
91  }
92 };
93 
94 struct ScalarSub {
95  template <typename T>
96  static KOKKOS_FORCEINLINE_FUNCTION
97  T apply(T const& left, T const& right) {
98  return left - right;
99  }
100 };
101 
102 struct ScalarMul {
103  template <typename T>
104  static KOKKOS_FORCEINLINE_FUNCTION
105  T apply(T const& left, T const& right) {
106  return left * right;
107  }
108 };
109 
110 struct ScalarDiv {
111  template <typename T>
112  static KOKKOS_FORCEINLINE_FUNCTION
113  T apply(T const& left, T const& right) {
114  return left / right;
115  }
116 };
117 
118 struct ScalarPow {
119  template <typename T>
120  static KOKKOS_FORCEINLINE_FUNCTION
121  T apply(T const& left, T const& right) {
122  using std::pow;
123  return pow(left, right);
124  }
125 };
126 
127 struct ScalarNeg {
128  template <typename T>
129  static KOKKOS_FORCEINLINE_FUNCTION
130  T apply(T const& right) {
131  return -right;
132  }
133 };
134 
135 // TODO: replace this with .access() after next Kokkos release
136 template <typename Indexed, size_t IterationRank, size_t IndexedRank = Indexed::rank>
137 struct Indexer;
138 
139 template <typename ViewType>
140 struct Indexer<ViewType, 1, 0> {
141  template <typename Integral>
142  static KOKKOS_FORCEINLINE_FUNCTION
143  typename ViewType::reference_type index(ViewType const& x, Integral) { return x(); }
144 };
145 
146 template <typename ViewType>
147 struct Indexer<ViewType, 1, 1> {
148  template <typename Integral>
149  static KOKKOS_FORCEINLINE_FUNCTION
150  typename ViewType::reference_type index(ViewType const& x, Integral i) { return x(i); }
151 };
152 
153 template <typename ViewType>
154 struct Indexer<ViewType, 2, 0> {
155  template <typename Integral>
156  static KOKKOS_FORCEINLINE_FUNCTION
157  typename ViewType::reference_type index(ViewType const& x, Integral, Integral) { return x(); }
158 };
159 
160 template <typename ViewType>
161 struct Indexer<ViewType, 2, 1> {
162  template <typename Integral>
163  static KOKKOS_FORCEINLINE_FUNCTION
164  typename ViewType::reference_type index(ViewType const& x, Integral i, Integral) { return x(i); }
165 };
166 
167 template <typename ViewType>
168 struct Indexer<ViewType, 2, 2> {
169  template <typename Integral>
170  static KOKKOS_FORCEINLINE_FUNCTION
171  typename ViewType::reference_type index(ViewType const& x, Integral i, Integral j) { return x(i, j); }
172 };
173 
174 //TODO: just use std::max once C++14 is the Trilinos standard (which makes std::max constexpr)
175 template <typename T, typename ... TS>
176 struct MaxRank;
177 
178 template <typename T>
179 struct MaxRank<T> {
180  static constexpr size_t value = T::rank;
181 };
182 
183 template <typename T, typename ... TS>
184 struct MaxRank {
185  static constexpr size_t left_value = T::rank;
186  static constexpr size_t right_value = MaxRank<TS ...>::value;
187  static constexpr size_t value = left_value > right_value ? left_value : right_value;
188 };
189 
190 template <typename A, typename B>
191 struct ResultType {
192  static constexpr size_t a_rank = A::rank;
193  static constexpr size_t b_rank = B::rank;
194  using biggest_type = typename std::conditional<(b_rank > a_rank), B, A>::type;
197 };
198 
199 template <typename C, typename A, typename B>
201  static constexpr size_t a_rank = A::rank;
202  static constexpr size_t b_rank = B::rank;
203  static constexpr size_t c_rank = C::rank;
204  using biggest_ab_type = typename std::conditional<(b_rank > a_rank), B, A>::type;
205  using biggest_type = typename std::conditional<(c_rank > biggest_ab_type::rank), C, biggest_ab_type>::type;
208 };
209 
210 template <typename Op, typename Result, typename Left, typename Right, size_t Rank = Result::rank>
212 
213 template <typename Op, typename Result, typename Left, typename Right>
214 struct BinaryFunctor<Op, Result, Left, Right, 0> {
216  using execution_space = typename Result::execution_space;
218  Left left_;
219  Right right_;
220  KOKKOS_INLINE_FUNCTION
221  void operator()(typename execution_space::size_type) const {
222  result_() = Op::apply(left_(), right_());
223  }
224  BinaryFunctor(std::string const& name, Teuchos::any& result, Teuchos::any& left, Teuchos::any& right) {
225  left_ = Teuchos::any_cast<Left>(left);
226  right_ = Teuchos::any_cast<Right>(right);
227  result_ = NonConstResult(Kokkos::ViewAllocateWithoutInitializing(name));
228  Kokkos::parallel_for(name, Kokkos::RangePolicy<execution_space>(0, 1), *this);
229  result = Result(result_);
230  }
231 };
232 
233 template <typename Op, typename Result, typename Left, typename Right>
234 struct BinaryFunctor<Op, Result, Left, Right, 1> {
236  using execution_space = typename Result::execution_space;
238  Left left_;
239  Right right_;
240  KOKKOS_INLINE_FUNCTION
241  void operator()(typename execution_space::size_type i) const {
242  result_(i) =
243  Op::apply(
244  Indexer<Left, 1>::index(left_, i),
245  Indexer<Right, 1>::index(right_, i));
246  }
247  BinaryFunctor(std::string const& name, Teuchos::any& result, Teuchos::any& left, Teuchos::any& right) {
248  left_ = Teuchos::any_cast<Left>(left);
249  right_ = Teuchos::any_cast<Right>(right);
250  auto extent_0 = std::max(left_.extent(0), right_.extent(0));
251  result_ = NonConstResult(Kokkos::ViewAllocateWithoutInitializing(name), extent_0);
252  Kokkos::parallel_for(name, Kokkos::RangePolicy<execution_space>(0, extent_0), *this);
253  result = Result{result_};
254  }
255 };
256 
257 template <typename Op, typename Result, typename Left, typename Right>
258 struct BinaryFunctor<Op, Result, Left, Right, 2> {
260  using execution_space = typename Result::execution_space;
262  Left left_;
263  Right right_;
264  KOKKOS_INLINE_FUNCTION
265  void operator()(typename execution_space::size_type i, typename execution_space::size_type j) const {
266  result_(i, j) =
267  Op::apply(
268  Indexer<Left, 2>::index(left_, i, j),
269  Indexer<Right, 2>::index(right_, i, j));
270  }
271  BinaryFunctor(std::string const& name, Teuchos::any& result, Teuchos::any& left, Teuchos::any& right) {
272  left_ = Teuchos::any_cast<Left>(left);
273  right_ = Teuchos::any_cast<Right>(right);
274  auto extent_0 = std::max(left_.extent(0), right_.extent(0));
275  auto extent_1 = std::max(left_.extent(1), right_.extent(1));
276  result_ = NonConstResult(Kokkos::ViewAllocateWithoutInitializing(name), extent_0, extent_1);
277  using policy_type = Kokkos::MDRangePolicy<execution_space, Kokkos::Rank<2>>;
278  Kokkos::parallel_for(name, policy_type({0, 0}, {extent_0, extent_1}), *this);
279  result = Result{result_};
280  }
281 };
282 
283 template <typename Cond, typename Left, typename Right, size_t Rank = MaxRank<Cond, Left, Right>::value>
285 
286 template <typename Cond, typename Left, typename Right>
287 struct TernaryFunctor<Cond, Left, Right, 1> {
290  using execution_space = typename Result::execution_space;
292  Cond cond_;
293  Left left_;
294  Right right_;
295  KOKKOS_INLINE_FUNCTION
296  void operator()(typename execution_space::size_type i) const {
297  result_(i) =
298  Indexer<Cond, 1>::index(cond_, i) ?
299  Indexer<Left, 1>::index(left_, i) :
300  Indexer<Right, 1>::index(right_, i);
301  }
302  TernaryFunctor(std::string const& name, Teuchos::any& result, Teuchos::any& cond, Teuchos::any& left, Teuchos::any& right) {
303  cond_ = Teuchos::any_cast<Cond>(cond);
304  left_ = Teuchos::any_cast<Left>(left);
305  right_ = Teuchos::any_cast<Right>(right);
306  auto extent_0 =
307  std::max(cond_.extent(0),
308  std::max(left_.extent(0), right_.extent(0)));
309  result_ = NonConstResult(Kokkos::ViewAllocateWithoutInitializing(name), extent_0);
310  Kokkos::parallel_for(name, Kokkos::RangePolicy<execution_space>(0, extent_0), *this);
311  result = Result{result_};
312  }
313 };
314 
315 template <typename Cond, typename Left, typename Right>
316 struct TernaryFunctor<Cond, Left, Right, 2> {
319  using execution_space = typename Result::execution_space;
321  Cond cond_;
322  Left left_;
323  Right right_;
324  KOKKOS_INLINE_FUNCTION
325  void operator()(typename execution_space::size_type i, typename execution_space::size_type j) const {
326  result_(i, j) =
327  Indexer<Cond, 2>::index(cond_, i, j) ?
328  Indexer<Left, 2>::index(left_, i, j) :
329  Indexer<Right, 2>::index(right_, i, j);
330  }
331  TernaryFunctor(std::string const& name, Teuchos::any& result, Teuchos::any& cond, Teuchos::any& left, Teuchos::any& right) {
332  cond_ = Teuchos::any_cast<Cond>(cond);
333  left_ = Teuchos::any_cast<Left>(left);
334  right_ = Teuchos::any_cast<Right>(right);
335  auto extent_0 =
336  std::max(cond_.extent(0),
337  std::max(left_.extent(0), right_.extent(0)));
338  auto extent_1 =
339  std::max(cond_.extent(1),
340  std::max(left_.extent(1), right_.extent(1)));
341  result_ = NonConstResult(Kokkos::ViewAllocateWithoutInitializing(name), extent_0, extent_1);
342  using policy_type = Kokkos::MDRangePolicy<execution_space, Kokkos::Rank<2>>;
343  Kokkos::parallel_for(name, policy_type({0, 0}, {extent_0, extent_1}), *this);
344  result = Result{result_};
345  }
346 };
347 
348 template <typename DT, typename ... VP>
350  : EvalBase()
351 {
352 }
353 
354 template <typename DT, typename ... VP>
355 void Eval<DT, VP ...>::set(std::string const& name, bool value) {
356  single_bool_view_type view{Kokkos::ViewAllocateWithoutInitializing{name}};
357  auto host_view = Kokkos::create_mirror_view(view);
358  host_view() = value;
359  Kokkos::deep_copy(view, host_view);
360  symbol_map[name] = const_single_bool_view_type{view};
361 }
362 
363 template <typename DT, typename ... VP>
364 void Eval<DT, VP ...>::set(std::string const& name, scalar_type const& value) {
365  single_view_type view{Kokkos::ViewAllocateWithoutInitializing{name}};
366  auto host_view = Kokkos::create_mirror_view(view);
367  host_view() = value;
368  Kokkos::deep_copy(view, host_view);
369  symbol_map[name] = const_single_view_type{view};
370  bool a, b;
371  this->inspect_arg(symbol_map[name], a, b);
372 }
373 
374 template <typename DT, typename ... VP>
375 void Eval<DT, VP ...>::set(std::string const& name, const_view_type const& value) {
376  symbol_map[name] = value;
377 }
378 
379 template <typename DT, typename ... VP>
381  single_view_type view{Kokkos::ViewAllocateWithoutInitializing{"constant"}};
382  auto host_view = Kokkos::create_mirror_view(view);
383  host_view() = value;
384  Kokkos::deep_copy(view, host_view);
385  result = const_single_view_type{view};
386  bool a, b;
387  this->inspect_arg(result, a, b);
388 }
389 
390 template <typename DT, typename ... VP>
391 void Eval<DT, VP ...>::inspect_arg(Teuchos::any const& arg, bool& is_many, bool& is_bool) {
392  if (arg.type() == typeid(const_single_bool_view_type)) {
393  is_many = false;
394  is_bool = true;
395  } else if (arg.type() == typeid(const_single_view_type)) {
396  is_many = false;
397  is_bool = false;
398  } else if (arg.type() == typeid(const_view_type)) {
399  is_many = true;
400  is_bool = false;
401  } else if (arg.type() == typeid(const_bool_view_type)) {
402  is_many = true;
403  is_bool = true;
404  } else {
406  "value is of illegal type " << arg.typeName() << ", view type is "
407  << typeid(const_view_type).name());
408  }
409 }
410 
411 template <typename DT, typename ... VP>
413  using ManyBool = const_bool_view_type;
414  using Single = const_single_view_type;
415  TernaryFunctor<ManyBool, Single, Single>("many ? single : single", result, cond, left, right);
416 }
417 
418 template <typename DT, typename ... VP>
420  using ManyBool = const_bool_view_type;
421  using Single = const_single_view_type;
422  using Many = const_view_type;
423  TernaryFunctor<ManyBool, Single, Many>("many ? single : many", result, cond, left, right);
424 }
425 
426 template <typename DT, typename ... VP>
428  using ManyBool = const_bool_view_type;
429  using Single = const_single_view_type;
430  using Many = const_view_type;
431  TernaryFunctor<ManyBool, Many, Single>("many ? many : single", result, cond, left, right);
432 }
433 
434 template <typename DT, typename ... VP>
436  using ManyBool = const_bool_view_type;
437  using Many = const_view_type;
438  TernaryFunctor<ManyBool, Many, Many>("many ? many : many", result, cond, left, right);
439 }
440 
441 template <typename DT, typename ... VP>
443  using SingleBool = const_single_bool_view_type;
444  using Single = const_single_view_type;
445  switch (code) {
446  case BinaryOpCode::OR: BinaryFunctor<ScalarOr , SingleBool, SingleBool, SingleBool>("single||single", result, left, right); break;
448  case BinaryOpCode::LT: BinaryFunctor<ScalarLT , SingleBool, Single, Single>("single< single", result, left, right); break;
449  case BinaryOpCode::GT: BinaryFunctor<ScalarGT , SingleBool, Single, Single>("single> single", result, left, right); break;
450  case BinaryOpCode::GEQ: BinaryFunctor<ScalarGEQ, SingleBool, Single, Single>("single>=single", result, left, right); break;
451  case BinaryOpCode::LEQ: BinaryFunctor<ScalarLEQ, SingleBool, Single, Single>("single<=single", result, left, right); break;
452  case BinaryOpCode::EQ: BinaryFunctor<ScalarEQ , SingleBool, Single, Single>("single==single", result, left, right); break;
453  case BinaryOpCode::MUL: BinaryFunctor<ScalarMul, Single, Single, Single>("single* single", result, left, right); break;
454  case BinaryOpCode::DIV: BinaryFunctor<ScalarDiv, Single, Single, Single>("single/ single", result, left, right); break;
455  case BinaryOpCode::ADD: BinaryFunctor<ScalarAdd, Single, Single, Single>("single+ single", result, left, right); break;
456  case BinaryOpCode::SUB: BinaryFunctor<ScalarSub, Single, Single, Single>("single- single", result, left, right); break;
457  case BinaryOpCode::POW: BinaryFunctor<ScalarPow, Single, Single, Single>("single^ single", result, left, right); break;
458  }
459 }
460 
461 template <typename DT, typename ... VP>
463  using Single = const_single_view_type;
464  using SingleBool = const_single_bool_view_type;
465  using Many = const_view_type;
466  using ManyBool = const_bool_view_type;
467  switch (code) {
468  case BinaryOpCode::OR: BinaryFunctor<ScalarOr , ManyBool, SingleBool, ManyBool>("single||many", result, left, right); break;
469  case BinaryOpCode::AND: BinaryFunctor<ScalarAnd, ManyBool, SingleBool, ManyBool>("single&&many", result, left, right); break;
470  case BinaryOpCode::LT: BinaryFunctor<ScalarLT , ManyBool, Single, Many>("single< many", result, left, right); break;
471  case BinaryOpCode::GT: BinaryFunctor<ScalarGT , ManyBool, Single, Many>("single> many", result, left, right); break;
472  case BinaryOpCode::GEQ: BinaryFunctor<ScalarGEQ, ManyBool, Single, Many>("single>=many", result, left, right); break;
473  case BinaryOpCode::LEQ: BinaryFunctor<ScalarLEQ, ManyBool, Single, Many>("single<=many", result, left, right); break;
474  case BinaryOpCode::EQ: BinaryFunctor<ScalarEQ , ManyBool, Single, Many>("single==many", result, left, right); break;
475  case BinaryOpCode::MUL: BinaryFunctor<ScalarMul, Many, Single, Many>("single* many", result, left, right); break;
476  case BinaryOpCode::DIV: BinaryFunctor<ScalarDiv, Many, Single, Many>("single/ many", result, left, right); break;
477  case BinaryOpCode::ADD: BinaryFunctor<ScalarAdd, Many, Single, Many>("single+ many", result, left, right); break;
478  case BinaryOpCode::SUB: BinaryFunctor<ScalarSub, Many, Single, Many>("single- many", result, left, right); break;
479  case BinaryOpCode::POW: BinaryFunctor<ScalarPow, Many, Single, Many>("single^ many", result, left, right); break;
480  }
481 }
482 
483 template <typename DT, typename ... VP>
485  using Single = const_single_view_type;
486  using SingleBool = const_single_bool_view_type;
487  using Many = const_view_type;
488  using ManyBool = const_bool_view_type;
489  switch (code) {
490  case BinaryOpCode::OR: BinaryFunctor<ScalarOr , ManyBool, ManyBool, SingleBool>("many||single", result, left, right); break;
491  case BinaryOpCode::AND: BinaryFunctor<ScalarAnd, ManyBool, ManyBool, SingleBool>("many&&single", result, left, right); break;
492  case BinaryOpCode::LT: BinaryFunctor<ScalarLT , ManyBool, Many, Single>("many< single", result, left, right); break;
493  case BinaryOpCode::GT: BinaryFunctor<ScalarGT , ManyBool, Many, Single>("many> single", result, left, right); break;
494  case BinaryOpCode::GEQ: BinaryFunctor<ScalarGEQ, ManyBool, Many, Single>("many>=single", result, left, right); break;
495  case BinaryOpCode::LEQ: BinaryFunctor<ScalarLEQ, ManyBool, Many, Single>("many<=single", result, left, right); break;
496  case BinaryOpCode::EQ: BinaryFunctor<ScalarEQ , ManyBool, Many, Single>("many==single", result, left, right); break;
497  case BinaryOpCode::MUL: BinaryFunctor<ScalarMul, Many, Many, Single>("many* single", result, left, right); break;
498  case BinaryOpCode::DIV: BinaryFunctor<ScalarDiv, Many, Many, Single>("many/ single", result, left, right); break;
499  case BinaryOpCode::ADD: BinaryFunctor<ScalarAdd, Many, Many, Single>("many+ single", result, left, right); break;
500  case BinaryOpCode::SUB: BinaryFunctor<ScalarSub, Many, Many, Single>("many- single", result, left, right); break;
501  case BinaryOpCode::POW: BinaryFunctor<ScalarPow, Many, Many, Single>("many^ single", result, left, right); break;
502  }
503 }
504 
505 template <typename DT, typename ... VP>
507  using Many = const_view_type;
508  using ManyBool = const_bool_view_type;
509  switch (code) {
510  case BinaryOpCode::OR: BinaryFunctor<ScalarOr , ManyBool, ManyBool, ManyBool>("many||many", result, left, right); break;
512  case BinaryOpCode::LT: BinaryFunctor<ScalarLT , ManyBool, Many, Many>("many< many", result, left, right); break;
513  case BinaryOpCode::GT: BinaryFunctor<ScalarGT , ManyBool, Many, Many>("many> many", result, left, right); break;
514  case BinaryOpCode::GEQ: BinaryFunctor<ScalarGEQ, ManyBool, Many, Many>("many>=many", result, left, right); break;
515  case BinaryOpCode::LEQ: BinaryFunctor<ScalarLEQ, ManyBool, Many, Many>("many<=many", result, left, right); break;
516  case BinaryOpCode::EQ: BinaryFunctor<ScalarEQ , ManyBool, Many, Many>("many==many", result, left, right); break;
517  case BinaryOpCode::MUL: BinaryFunctor<ScalarMul, Many, Many, Many>("many* many", result, left, right); break;
518  case BinaryOpCode::DIV: BinaryFunctor<ScalarDiv, Many, Many, Many>("many/ many", result, left, right); break;
519  case BinaryOpCode::ADD: BinaryFunctor<ScalarAdd, Many, Many, Many>("many+ many", result, left, right); break;
520  case BinaryOpCode::SUB: BinaryFunctor<ScalarSub, Many, Many, Many>("many- many", result, left, right); break;
521  case BinaryOpCode::POW: BinaryFunctor<ScalarPow, Many, Many, Many>("many^ many", result, left, right); break;
522  }
523 }
524 
525 template <typename Op, typename Result, size_t Rank = Result::rank>
527 
528 template <typename Op, typename Result>
529 struct UnaryFunctor<Op, Result, 0> {
531  using execution_space = typename Result::execution_space;
533  Result right_;
534  KOKKOS_INLINE_FUNCTION
535  void operator()(typename execution_space::size_type i) const {
536  result_() = Op::apply(right_());
537  }
538  UnaryFunctor(std::string const& name, Teuchos::any& result, Teuchos::any& right) {
539  right_ = Teuchos::any_cast<Result>(right);
540  result_ = NonConstResult(Kokkos::ViewAllocateWithoutInitializing(name));
541  Kokkos::parallel_for(name, Kokkos::RangePolicy<execution_space>(0, 1), *this);
542  result = Result(result_);
543  }
544 };
545 
546 template <typename Op, typename Result>
547 struct UnaryFunctor<Op, Result, 1> {
549  using execution_space = typename Result::execution_space;
551  Result right_;
552  KOKKOS_INLINE_FUNCTION
553  void operator()(typename execution_space::size_type i) const {
554  result_(i) = Op::apply(right_(i));
555  }
556  UnaryFunctor(std::string const& name, Teuchos::any& result, Teuchos::any& right) {
557  right_ = Teuchos::any_cast<Result>(right);
558  auto extent_0 = right_.extent(0);
559  result_ = NonConstResult(Kokkos::ViewAllocateWithoutInitializing(name), extent_0);
560  Kokkos::parallel_for(name, Kokkos::RangePolicy<execution_space>(0, extent_0), *this);
561  result = Result(result_);
562  }
563 };
564 
565 template <typename Op, typename Result>
566 struct UnaryFunctor<Op, Result, 2> {
568  using execution_space = typename Result::execution_space;
570  Result right_;
571  KOKKOS_INLINE_FUNCTION
572  void operator()(typename execution_space::size_type i, typename execution_space::size_type j) const {
573  result_(i, j) = Op::apply(right_(i, j));
574  }
575  UnaryFunctor(std::string const& name, Teuchos::any& result, Teuchos::any& right) {
576  right_ = Teuchos::any_cast<Result>(right);
577  auto extent_0 = right_.extent(0);
578  auto extent_1 = right_.extent(1);
579  result_ = NonConstResult(Kokkos::ViewAllocateWithoutInitializing(name), extent_0, extent_1);
580  using policy_type = Kokkos::MDRangePolicy<execution_space, Kokkos::Rank<2>>;
581  Kokkos::parallel_for(name, policy_type({0, 0}, {extent_0, extent_1}), *this);
582  result = Result(result_);
583  }
584 };
585 
586 template <typename DT, typename ... VP>
589 }
590 
591 template <typename DT, typename ... VP>
594 }
595 
596 struct ScalarAbs {
597  template <typename T>
598  static KOKKOS_FORCEINLINE_FUNCTION
599  T apply(T const& right) {
600  using std::abs;
601  return abs(right);
602  }
603 };
604 
605 struct ScalarExp {
606  template <typename T>
607  static KOKKOS_FORCEINLINE_FUNCTION
608  T apply(T const& right) {
609  using std::exp;
610  return exp(right);
611  }
612 };
613 
614 struct ScalarLog {
615  template <typename T>
616  static KOKKOS_FORCEINLINE_FUNCTION
617  T apply(T const& right) {
618  using std::log;
619  return log(right);
620  }
621 };
622 
623 struct ScalarSqrt {
624  template <typename T>
625  static KOKKOS_FORCEINLINE_FUNCTION
626  T apply(T const& right) {
627  using std::sqrt;
628  return sqrt(right);
629  }
630 };
631 
632 struct ScalarSin {
633  template <typename T>
634  static KOKKOS_FORCEINLINE_FUNCTION
635  T apply(T const& right) {
636  using std::sin;
637  return sin(right);
638  }
639 };
640 
641 struct ScalarCos {
642  template <typename T>
643  static KOKKOS_FORCEINLINE_FUNCTION
644  T apply(T const& right) {
645  using std::cos;
646  return cos(right);
647  }
648 };
649 
650 struct ScalarTan {
651  template <typename T>
652  static KOKKOS_FORCEINLINE_FUNCTION
653  T apply(T const& right) {
654  using std::tan;
655  return tan(right);
656  }
657 };
658 
659 template <typename Op, typename EvalType>
661  void operator()(std::string const& name, Teuchos::any& result, std::vector<Teuchos::any>& rhs) const {
662  auto& right = rhs.at(0);
663  using single_type = typename EvalType::const_single_view_type;
664  using many_type = typename EvalType::const_view_type;
665  if (right.type() == typeid(single_type)) {
667  } else if (right.type() == typeid(many_type)) {
668  UnaryFunctor<Op, many_type>(name, result, right);
669  } else {
671  "Unexpected type " << right.typeName() << " passed to UnaryFunction \"" << name << "\"\n");
672  }
673  }
674 };
675 
676 template <typename DT, typename ... VP>
678  using eval_type = Eval<DT, VP ...>;
679  EvalBase& eval_base = eval;
687 }
688 
689 }} // end namespace panzer::Expr
690 
691 #endif // PANZER_EXPR_EVAL_IMPL_HPP
typename RebindViewType< Result, typename Result::non_const_value_type >::type NonConstResult
void many_many_binary_op(BinaryOpCode code, Teuchos::any &result, Teuchos::any &left, Teuchos::any &right) override
Base class for panzer::Expr::Eval, does everything that is independent of the Kokkos::View template p...
void make_constant(Teuchos::any &result, double const &value) override
void set(std::string const &name, bool value)
Assign a boolean value to a variable symbol.
static KOKKOS_FORCEINLINE_FUNCTION bool apply(T const &left, T const &right)
typename RebindViewType< biggest_type, typename biggest_type::const_value_type >::type const_type
typename RebindViewType< Result, typename Result::non_const_value_type >::type NonConstResult
static KOKKOS_FORCEINLINE_FUNCTION T apply(T const &right)
BinaryOpCode
Denotes the native binary operators in the Teuchos::MathExpr language.
UnaryFunctor(std::string const &name, Teuchos::any &result, Teuchos::any &right)
typename Result::execution_space execution_space
void single_many_ternary_op(Teuchos::any &result, Teuchos::any &cond, Teuchos::any &left, Teuchos::any &right) override
static KOKKOS_FORCEINLINE_FUNCTION T apply(T const &left, T const &right)
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
typename RebindViewType< Result, typename Result::non_const_value_type >::type NonConstResult
BinaryFunctor(std::string const &name, Teuchos::any &result, Teuchos::any &left, Teuchos::any &right)
std::function< void(std::string const &name, Teuchos::any &, std::vector< Teuchos::any > &rhs)> Function
The type of user-defined functions which are callable in the math language.
static KOKKOS_FORCEINLINE_FUNCTION T apply(T const &right)
static KOKKOS_FORCEINLINE_FUNCTION T apply(T const &left, T const &right)
static KOKKOS_FORCEINLINE_FUNCTION T apply(T const &right)
typename RebindViewType< Result, typename Result::non_const_value_type >::type NonConstResult
static KOKKOS_FORCEINLINE_FUNCTION ViewType::reference_type index(ViewType const &x, Integral, Integral)
KOKKOS_INLINE_FUNCTION void operator()(typename execution_space::size_type i, typename execution_space::size_type j) const
typename std::conditional<(b_rank > a_rank), Right, Left >::type biggest_ab_type
typename TernaryResultType< Cond, Left, Right >::non_const_type NonConstResult
void single_neg_op(Teuchos::any &result, Teuchos::any &right) override
UnaryFunctor(std::string const &name, Teuchos::any &result, Teuchos::any &right)
typename Result::execution_space execution_space
KOKKOS_INLINE_FUNCTION void operator()(typename execution_space::size_type i) const
TernaryFunctor(std::string const &name, Teuchos::any &result, Teuchos::any &cond, Teuchos::any &left, Teuchos::any &right)
static KOKKOS_FORCEINLINE_FUNCTION bool apply(T const &left, T const &right)
static constexpr size_t b_rank
void set(std::string const &name, Function const &value)
Registers an EvalBase::Function, binding it to a name and making it callable.
Declares the panzer::Expr::Eval templated class.
PHX::MDField< ScalarT, panzer::Cell, panzer::IP > result
A field that will be used to build up the result of the integral we&#39;re performing.
static KOKKOS_FORCEINLINE_FUNCTION bool apply(T const &left, T const &right)
static KOKKOS_FORCEINLINE_FUNCTION ViewType::reference_type index(ViewType const &x, Integral i, Integral)
static KOKKOS_FORCEINLINE_FUNCTION bool apply(T const &left, T const &right)
static KOKKOS_FORCEINLINE_FUNCTION T apply(bool cond, T const &left, T const &right)
static constexpr size_t left_value
static KOKKOS_FORCEINLINE_FUNCTION T apply(T const &right)
typename std::conditional<(c_rank > biggest_ab_type::rank), Cond, biggest_ab_type >::type biggest_type
static KOKKOS_FORCEINLINE_FUNCTION T apply(T const &right)
static KOKKOS_FORCEINLINE_FUNCTION ViewType::reference_type index(ViewType const &x, Integral i, Integral j)
typename Result::execution_space execution_space
typename RebindViewType< Result, typename Result::non_const_value_type >::type NonConstResult
void single_many_binary_op(BinaryOpCode code, Teuchos::any &result, Teuchos::any &left, Teuchos::any &right) override
void operator()(std::string const &name, Teuchos::any &result, std::vector< Teuchos::any > &rhs) const
KOKKOS_INLINE_FUNCTION void operator()(typename execution_space::size_type i) const
void inspect_arg(Teuchos::any const &arg, bool &is_many, bool &is_bool) override
static KOKKOS_FORCEINLINE_FUNCTION T apply(T const &right)
typename RebindViewType< Result, typename Result::non_const_value_type >::type NonConstResult
typename original_view_type::non_const_value_type scalar_type
The scalar type.
void many_many_ternary_op(Teuchos::any &result, Teuchos::any &cond, Teuchos::any &left, Teuchos::any &right) override
static constexpr size_t a_rank
KOKKOS_INLINE_FUNCTION void operator()(typename execution_space::size_type i) const
typename TernaryResultType< Cond, Left, Right >::type Result
static KOKKOS_FORCEINLINE_FUNCTION T apply(T const &right)
static KOKKOS_FORCEINLINE_FUNCTION T apply(T const &right)
BinaryFunctor(std::string const &name, Teuchos::any &result, Teuchos::any &left, Teuchos::any &right)
static KOKKOS_FORCEINLINE_FUNCTION ViewType::reference_type index(ViewType const &x, Integral)
Builds on RebindDataType, but acts directly on a Kokkos::View type.
typename RebindViewType< biggest_type, typename biggest_type::non_const_value_type >::type type
KOKKOS_INLINE_FUNCTION void operator()(typename execution_space::size_type) const
UnaryFunctor(std::string const &name, Teuchos::any &result, Teuchos::any &right)
Interprets mathematical expressions in a string and evaluates them using Kokkos::View objects as valu...
void many_neg_op(Teuchos::any &result, Teuchos::any &right) override
static KOKKOS_FORCEINLINE_FUNCTION bool apply(bool left, bool right)
typename RebindViewType< biggest_type, typename Left::non_const_value_type >::type non_const_type
KOKKOS_INLINE_FUNCTION void operator()(typename execution_space::size_type i, typename execution_space::size_type j) const
KOKKOS_INLINE_FUNCTION void operator()(typename execution_space::size_type i) const
static constexpr size_t right_value
void many_single_ternary_op(Teuchos::any &result, Teuchos::any &cond, Teuchos::any &left, Teuchos::any &right) override
static KOKKOS_FORCEINLINE_FUNCTION ViewType::reference_type index(ViewType const &x, Integral i)
TernaryFunctor(std::string const &name, Teuchos::any &result, Teuchos::any &cond, Teuchos::any &left, Teuchos::any &right)
static constexpr size_t value
static KOKKOS_FORCEINLINE_FUNCTION bool apply(T const &left, T const &right)
std::string typeName() const
static KOKKOS_FORCEINLINE_FUNCTION T apply(T const &left, T const &right)
void single_single_ternary_op(Teuchos::any &result, Teuchos::any &cond, Teuchos::any &left, Teuchos::any &right) override
typename RebindViewType< biggest_type, typename Left::const_value_type >::type type
const std::type_info & type() const
static KOKKOS_FORCEINLINE_FUNCTION T apply(T const &left, T const &right)
typename std::conditional<(b_rank > a_rank), B, A >::type biggest_type
void single_single_binary_op(BinaryOpCode code, Teuchos::any &result, Teuchos::any &left, Teuchos::any &right) override
void set_cmath_functions(Eval< DT, VP...> &eval)
Add support for functions such as sqrt(), sin(), and cos()
typename TernaryResultType< Cond, Left, Right >::non_const_type NonConstResult
BinaryFunctor(std::string const &name, Teuchos::any &result, Teuchos::any &left, Teuchos::any &right)
KOKKOS_INLINE_FUNCTION void operator()(typename execution_space::size_type i, typename execution_space::size_type j) const
void many_single_binary_op(BinaryOpCode code, Teuchos::any &result, Teuchos::any &left, Teuchos::any &right) override
typename TernaryResultType< Cond, Left, Right >::type Result
static KOKKOS_FORCEINLINE_FUNCTION bool apply(bool left, bool right)
static KOKKOS_FORCEINLINE_FUNCTION T apply(T const &left, T const &right)