Panzer Version of the Day
Loading...
Searching...
No Matches
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
19namespace panzer
20{
21namespace Expr
22{
23
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
32struct ScalarOr {
33 static KOKKOS_FORCEINLINE_FUNCTION
34 bool apply(bool left, bool right) {
35 return left || right;
36 }
37};
38
39struct ScalarAnd {
40 static KOKKOS_FORCEINLINE_FUNCTION
41 bool apply(bool left, bool right) {
42 return left && right;
43 }
44};
45
46struct 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
54struct 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
62struct 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
70struct 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
78struct 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
86struct 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
94struct 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
102struct 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
110struct 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
118struct 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
127struct 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
136template <typename Indexed, size_t IterationRank, size_t IndexedRank = Indexed::rank>
137struct Indexer;
138
139template <typename ViewType>
140struct 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
146template <typename ViewType>
147struct 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
153template <typename ViewType>
154struct 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
160template <typename ViewType>
161struct 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
167template <typename ViewType>
168struct 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)
175template <typename T, typename ... TS>
176struct MaxRank;
177
178template <typename T>
179struct MaxRank<T> {
180 static constexpr size_t value = T::rank;
181};
182
183template <typename T, typename ... TS>
184struct 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
190template <typename A, typename B>
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
199template <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
210template <typename Op, typename Result, typename Left, typename Right, size_t Rank = Result::rank>
212
213template <typename Op, typename Result, typename Left, typename Right>
214struct 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
233template <typename Op, typename Result, typename Left, typename Right>
234struct 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
257template <typename Op, typename Result, typename Left, typename Right>
258struct 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
283template <typename Cond, typename Left, typename Right, size_t Rank = MaxRank<Cond, Left, Right>::value>
285
286template <typename Cond, typename Left, typename Right>
287struct 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
315template <typename Cond, typename Left, typename Right>
316struct 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
348template <typename DT, typename ... VP>
353
354template <typename DT, typename ... VP>
355void 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
363template <typename DT, typename ... VP>
364void 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
374template <typename DT, typename ... VP>
375void Eval<DT, VP ...>::set(std::string const& name, const_view_type const& value) {
376 symbol_map[name] = value;
377}
378
379template <typename DT, typename ... VP>
380void Eval<DT, VP ...>::make_constant(Teuchos::any& result, double const& value) {
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);
386 bool a, b;
387 this->inspect_arg(result, a, b);
388}
389
390template <typename DT, typename ... VP>
391void 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 {
405 TEUCHOS_TEST_FOR_EXCEPTION(true, Teuchos::ParserFail,
406 "value is of illegal type " << arg.typeName() << ", view type is "
407 << typeid(const_view_type).name());
408 }
409}
410
411template <typename DT, typename ... VP>
412void Eval<DT, VP ...>::single_single_ternary_op(Teuchos::any& result, Teuchos::any& cond, Teuchos::any& left, Teuchos::any& right) {
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
418template <typename DT, typename ... VP>
419void Eval<DT, VP ...>::single_many_ternary_op(Teuchos::any& result, Teuchos::any& cond, Teuchos::any& left, Teuchos::any& right) {
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
426template <typename DT, typename ... VP>
427void Eval<DT, VP ...>::many_single_ternary_op(Teuchos::any& result, Teuchos::any& cond, Teuchos::any& left, Teuchos::any& right) {
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
434template <typename DT, typename ... VP>
435void Eval<DT, VP ...>::many_many_ternary_op(Teuchos::any& result, Teuchos::any& cond, Teuchos::any& left, Teuchos::any& right) {
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
441template <typename DT, typename ... VP>
442void Eval<DT, VP ...>::single_single_binary_op(BinaryOpCode code, Teuchos::any& result, Teuchos::any& left, Teuchos::any& right) {
443 using SingleBool = const_single_bool_view_type;
444 using Single = const_single_view_type;
445 switch (code) {
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
461template <typename DT, typename ... VP>
462void Eval<DT, VP ...>::single_many_binary_op(BinaryOpCode code, Teuchos::any& result, Teuchos::any& left, Teuchos::any& right) {
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) {
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
483template <typename DT, typename ... VP>
484void Eval<DT, VP ...>::many_single_binary_op(BinaryOpCode code, Teuchos::any& result, Teuchos::any& left, Teuchos::any& right) {
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) {
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
505template <typename DT, typename ... VP>
506void Eval<DT, VP ...>::many_many_binary_op(BinaryOpCode code, Teuchos::any& result, Teuchos::any& left, Teuchos::any& right) {
507 using Many = const_view_type;
508 using ManyBool = const_bool_view_type;
509 switch (code) {
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
525template <typename Op, typename Result, size_t Rank = Result::rank>
527
528template <typename Op, typename Result>
529struct 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
546template <typename Op, typename Result>
547struct 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
565template <typename Op, typename Result>
566struct 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
586template <typename DT, typename ... VP>
587void Eval<DT, VP ...>::many_neg_op(Teuchos::any& result, Teuchos::any& right) {
589}
590
591template <typename DT, typename ... VP>
592void Eval<DT, VP ...>::single_neg_op(Teuchos::any& result, Teuchos::any& right) {
594}
595
596struct 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
605struct 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
614struct 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
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
632struct 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
641struct 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
650struct 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
659template <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)) {
669 } else {
670 TEUCHOS_TEST_FOR_EXCEPTION(true, Teuchos::ParserFail,
671 "Unexpected type " << right.typeName() << " passed to UnaryFunction \"" << name << "\"\n");
672 }
673 }
674};
675
676template <typename DT, typename ... VP>
688
689}} // end namespace panzer::Expr
690
691#endif // PANZER_EXPR_EVAL_IMPL_HPP
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're performing.
Base class for panzer::Expr::Eval, does everything that is independent of the Kokkos::View template p...
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.
void set(std::string const &name, Function const &value)
Registers an EvalBase::Function, binding it to a name and making it callable.
Interprets mathematical expressions in a string and evaluates them using Kokkos::View objects as valu...
Kokkos::View< scalar_type, VP ... > single_view_type
One scalar (same for all evaluation points)
Kokkos::View< typename RebindDataType< view_data_type, bool const >::type, VP ... > const_bool_view_type
One boolean for each evaluation point, read-only.
typename original_view_type::non_const_value_type scalar_type
The scalar type.
Kokkos::View< bool const, VP ... > const_single_bool_view_type
One boolean (same for all evaluation points), read-only.
void many_many_binary_op(BinaryOpCode code, Teuchos::any &result, Teuchos::any &left, Teuchos::any &right) override
void single_single_ternary_op(Teuchos::any &result, Teuchos::any &cond, Teuchos::any &left, Teuchos::any &right) override
void many_neg_op(Teuchos::any &result, Teuchos::any &right) override
void make_constant(Teuchos::any &result, double const &value) override
void single_single_binary_op(BinaryOpCode code, Teuchos::any &result, Teuchos::any &left, Teuchos::any &right) override
void single_many_binary_op(BinaryOpCode code, Teuchos::any &result, Teuchos::any &left, Teuchos::any &right) override
void many_many_ternary_op(Teuchos::any &result, Teuchos::any &cond, Teuchos::any &left, Teuchos::any &right) override
void set(std::string const &name, bool value)
Assign a boolean value to a variable symbol.
void inspect_arg(Teuchos::any const &arg, bool &is_many, bool &is_bool) override
void many_single_ternary_op(Teuchos::any &result, Teuchos::any &cond, Teuchos::any &left, Teuchos::any &right) override
Kokkos::View< typename RebindDataType< view_data_type, scalar_type const >::type, VP ... > const_view_type
One scalar for each evaluation point, read-only.
void single_neg_op(Teuchos::any &result, Teuchos::any &right) override
Kokkos::View< scalar_type const, VP ... > const_single_view_type
One scalar (same for all evaluation points), read-only.
void single_many_ternary_op(Teuchos::any &result, Teuchos::any &cond, Teuchos::any &left, Teuchos::any &right) override
Kokkos::View< bool, VP ... > single_bool_view_type
One boolean (same for all evaluation points)
void many_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()
BinaryOpCode
Denotes the native binary operators in the Teuchos::MathExpr language.
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)
KOKKOS_INLINE_FUNCTION void operator()(typename execution_space::size_type) const
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)
KOKKOS_INLINE_FUNCTION void operator()(typename execution_space::size_type i) const
BinaryFunctor(std::string const &name, Teuchos::any &result, Teuchos::any &left, Teuchos::any &right)
typename RebindViewType< Result, typename Result::non_const_value_type >::type NonConstResult
KOKKOS_INLINE_FUNCTION void operator()(typename execution_space::size_type i, typename execution_space::size_type j) const
static KOKKOS_FORCEINLINE_FUNCTION ViewType::reference_type index(ViewType const &x, Integral)
static KOKKOS_FORCEINLINE_FUNCTION ViewType::reference_type index(ViewType const &x, Integral i)
static KOKKOS_FORCEINLINE_FUNCTION ViewType::reference_type index(ViewType const &x, Integral, Integral)
static KOKKOS_FORCEINLINE_FUNCTION ViewType::reference_type index(ViewType const &x, Integral i, Integral)
static KOKKOS_FORCEINLINE_FUNCTION ViewType::reference_type index(ViewType const &x, Integral i, Integral j)
static constexpr size_t right_value
static constexpr size_t value
static constexpr size_t left_value
Builds on RebindDataType, but acts directly on a Kokkos::View type.
typename RebindViewType< biggest_type, typename biggest_type::non_const_value_type >::type type
static constexpr size_t a_rank
typename std::conditional<(b_rank > a_rank), B, A >::type biggest_type
static constexpr size_t b_rank
typename RebindViewType< biggest_type, typename biggest_type::const_value_type >::type const_type
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 bool apply(bool left, bool right)
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 bool apply(T const &left, T const &right)
static KOKKOS_FORCEINLINE_FUNCTION T apply(T const &right)
static KOKKOS_FORCEINLINE_FUNCTION bool apply(T const &left, T const &right)
static KOKKOS_FORCEINLINE_FUNCTION bool apply(T const &left, T const &right)
static KOKKOS_FORCEINLINE_FUNCTION bool apply(T const &left, T const &right)
static KOKKOS_FORCEINLINE_FUNCTION bool apply(T const &left, T const &right)
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)
static KOKKOS_FORCEINLINE_FUNCTION bool apply(bool left, bool right)
static KOKKOS_FORCEINLINE_FUNCTION T apply(T const &left, T const &right)
static KOKKOS_FORCEINLINE_FUNCTION T apply(T const &right)
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)
static KOKKOS_FORCEINLINE_FUNCTION T apply(bool cond, T const &left, T const &right)
KOKKOS_INLINE_FUNCTION void operator()(typename execution_space::size_type i) const
typename TernaryResultType< Cond, Left, Right >::type Result
TernaryFunctor(std::string const &name, Teuchos::any &result, Teuchos::any &cond, Teuchos::any &left, Teuchos::any &right)
typename TernaryResultType< Cond, Left, Right >::non_const_type NonConstResult
TernaryFunctor(std::string const &name, Teuchos::any &result, Teuchos::any &cond, Teuchos::any &left, Teuchos::any &right)
typename TernaryResultType< Cond, Left, Right >::type Result
typename TernaryResultType< Cond, Left, Right >::non_const_type NonConstResult
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), B, A >::type biggest_ab_type
typename RebindViewType< biggest_type, typename A::const_value_type >::type type
typename std::conditional<(c_rank > biggest_ab_type::rank), C, biggest_ab_type >::type biggest_type
typename RebindViewType< biggest_type, typename A::non_const_value_type >::type non_const_type
void operator()(std::string const &name, Teuchos::any &result, std::vector< Teuchos::any > &rhs) const
typename RebindViewType< Result, typename Result::non_const_value_type >::type NonConstResult
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
typename RebindViewType< Result, typename Result::non_const_value_type >::type NonConstResult
typename Result::execution_space execution_space
UnaryFunctor(std::string const &name, Teuchos::any &result, Teuchos::any &right)
KOKKOS_INLINE_FUNCTION void operator()(typename execution_space::size_type i) const
KOKKOS_INLINE_FUNCTION void operator()(typename execution_space::size_type i, typename execution_space::size_type j) const
typename RebindViewType< Result, typename Result::non_const_value_type >::type NonConstResult
typename Result::execution_space execution_space
UnaryFunctor(std::string const &name, Teuchos::any &result, Teuchos::any &right)