Intrepid2
Intrepid2_Utils.hpp
Go to the documentation of this file.
1// @HEADER
2// *****************************************************************************
3// Intrepid2 Package
4//
5// Copyright 2007 NTESS and the Intrepid2 contributors.
6// SPDX-License-Identifier: BSD-3-Clause
7// *****************************************************************************
8// @HEADER
9
16#ifndef __INTREPID2_UTILS_HPP__
17#define __INTREPID2_UTILS_HPP__
18
19#include "Intrepid2_ConfigDefs.hpp"
21#include "Intrepid2_Types.hpp"
22
23#include "Kokkos_Core.hpp"
24#include "Kokkos_Macros.hpp" // provides some preprocessor values used in definitions of INTREPID2_DEPRECATED, etc.
25#include "Kokkos_Random.hpp"
26
27#ifdef HAVE_INTREPID2_SACADO
28#include "Sacado_Fad_Kokkos_Fwd.hpp"
29#include "Sacado_Fad_Kokkos_ViewFactory.hpp"
30#endif
31
32namespace Intrepid2 {
33
34#if defined(__CUDA_ARCH__) || defined(__HIP_DEVICE_COMPILE__) || defined(__SYCL_DEVICE_ONLY__)
35#define INTREPID2_COMPILE_DEVICE_CODE
36#endif
37
38#if defined(KOKKOS_ENABLE_CUDA) || defined(KOKKOS_ENABLE_HIP) || defined(KOKKOS_ENABLE_SYCL)
39#define INTREPID2_ENABLE_DEVICE
40#endif
41
42#if defined(KOKKOS_OPT_RANGE_AGGRESSIVE_VECTORIZATION) \
43 && defined(KOKKOS_ENABLE_PRAGMA_IVDEP) \
44 && !defined(INTREPID2_COMPILE_DEVICE_CODE)
45#define INTREPID2_USE_IVDEP
46#endif
47
48 //
49 // test macros
50 //
51
52#define INTREPID2_TEST_FOR_WARNING(test, msg) \
53 if (test) { \
54 Kokkos::printf("[Intrepid2] Warning in file %s, line %d\n",__FILE__,__LINE__); \
55 Kokkos::printf(" Test that evaluated to true: %s\n", #test); \
56 Kokkos::printf(" %s \n", msg); \
57 }
58
59#define INTREPID2_TEST_FOR_EXCEPTION(test, x, msg) \
60 if (test) { \
61 Kokkos::printf("[Intrepid2] Error in file %s, line %d\n",__FILE__,__LINE__); \
62 Kokkos::printf(" Test that evaluated to true: %s\n", #test); \
63 Kokkos::printf(" %s \n", msg); \
64 throw x(msg); \
65 }
66
69#ifndef INTREPID2_ENABLE_DEVICE
70#define INTREPID2_TEST_FOR_EXCEPTION_DEVICE_SAFE(test, x, msg) \
71 if (test) { \
72 std::cout << "[Intrepid2] Error in file " << __FILE__ << ", line " << __LINE__ << "\n"; \
73 std::cout << " Test that evaluated to true: " << #test << "\n"; \
74 std::cout << " " << msg << " \n"; \
75 throw x(msg); \
76 }
77#else
78#define INTREPID2_TEST_FOR_EXCEPTION_DEVICE_SAFE(test, x, msg) \
79 if (test) { \
80 Kokkos::printf("[Intrepid2] Error in file %s, line %d\n",__FILE__,__LINE__); \
81 Kokkos::printf(" Test that evaluated to true: %s\n", #test); \
82 Kokkos::printf(" %s \n", msg); \
83 Kokkos::abort( "[Intrepid2] Abort\n"); \
84 }
85#endif
86#if defined(INTREPID2_ENABLE_DEBUG) || defined(NDEBUG) || 1
87#define INTREPID2_TEST_FOR_ABORT(test, msg) \
88 if (test) { \
89 Kokkos::printf("[Intrepid2] Error in file %s, line %d\n",__FILE__,__LINE__); \
90 Kokkos::printf(" Test that evaluated to true: %s\n", #test); \
91 Kokkos::printf(" %s \n", msg); \
92 Kokkos::abort( "[Intrepid2] Abort\n"); \
93 }
94#else
95#define INTREPID2_TEST_FOR_ABORT(test, msg) ((void)0)
96#endif
97 // check the first error only
98#ifdef INTREPID2_TEST_FOR_DEBUG_ABORT_OVERRIDE_TO_CONTINUE
99#define INTREPID2_TEST_FOR_DEBUG_ABORT(test, info, msg) \
100 if (!(info) && (test)) { \
101 Kokkos::printf("[Intrepid2] Error in file %s, line %d\n",__FILE__,__LINE__); \
102 Kokkos::printf(" Test that evaluated to true: %s\n", #test); \
103 Kokkos::printf(" %s \n", msg); \
104 info = true; \
105 }
106#else
107#define INTREPID2_TEST_FOR_DEBUG_ABORT(test, info, msg) \
108 if (!(info) && (test)) { \
109 Kokkos::printf("[Intrepid2] Error in file %s, line %d\n",__FILE__,__LINE__); \
110 Kokkos::printf(" Test that evaluated to true: %s\n", #test); \
111 Kokkos::printf(" %s \n", msg); \
112 info = true ; \
113 Kokkos::abort( "[Intrepid2] Abort\n"); \
114 }
115#endif
116
120 template<typename T>
122 typedef typename T::scalar_type scalar_type;
123 };
124
125 // this is built in types to support
129 template<>
130 struct ScalarTraits<float> {
131 typedef float scalar_type;
132 };
136 template<>
137 struct ScalarTraits<double> {
138 typedef double scalar_type;
139 };
143 template<>
144 struct ScalarTraits<int> {
145 typedef int scalar_type;
146 };
150 template<>
151 struct ScalarTraits<long int> {
152 typedef long int scalar_type;
153 };
157 template<>
158 struct ScalarTraits<long long> {
159 typedef long long scalar_type;
160 };
161
162
163
167 template<typename ViewSpaceType, typename UserSpaceType>
168 struct ExecSpace {
169 typedef UserSpaceType ExecSpaceType;
170 };
171
175 template<typename ViewSpaceType>
176 struct ExecSpace<ViewSpaceType,void> {
177 typedef ViewSpaceType ExecSpaceType;
178 };
179
180
184 template <typename ViewType>
186 using input_layout = typename ViewType::array_layout;
187 using default_layout = typename ViewType::device_type::execution_space::array_layout;
188 using result_layout =
189 typename std::conditional<
190 std::is_same< input_layout, Kokkos::LayoutStride >::value,
191 default_layout,
192 input_layout >::type;
193 };
194
195
196 //
197 // utilities device comparible
198 //
199
200 // this will be gone
201 template<typename IdxType, typename DimType, typename IterType>
202 KOKKOS_FORCEINLINE_FUNCTION
203 static void
204 unrollIndex(IdxType &i, IdxType &j,
205 const DimType /* dim0 */,
206 const DimType dim1,
207 const IterType iter) {
208 // left index
209 //j = iter/dim0;
210 //i = iter%dim0;
211
212 // right index
213 i = iter/dim1;
214 j = iter%dim1;
215 }
216
217 template<typename IdxType, typename DimType, typename IterType>
218 KOKKOS_FORCEINLINE_FUNCTION
219 static void
220 unrollIndex(IdxType &i, IdxType &j, IdxType &k,
221 const DimType dim0,
222 const DimType dim1,
223 const DimType dim2,
224 const IterType iter) {
225 IdxType tmp;
226
227 //unrollIndex(tmp, k, dim0*dim1, dim2, iter);
228 //unrollIndex( i, j, dim0, dim1, tmp);
229
230 unrollIndex( i, tmp, dim0, dim1*dim2, iter);
231 unrollIndex( j, k, dim1, dim2, tmp);
232 }
233
237 template<typename T>
238 class Util {
239 public:
240 KOKKOS_FORCEINLINE_FUNCTION
241 static T min(const T a, const T b) {
242 return (a < b ? a : b);
243 }
244
245 KOKKOS_FORCEINLINE_FUNCTION
246 static T max(const T a, const T b) {
247 return (a > b ? a : b);
248 }
249
250 template <typename... Args>
251 requires (std::is_same_v<T, Args> && ...)
252 KOKKOS_FORCEINLINE_FUNCTION
253 static T max(const T a, const T b, Args... args) {
254 // Recursively find the max of everything after the first argument
255 T tmp_max = max(b, args...);
256 return (a > tmp_max) ? a : tmp_max;
257 }
258
259 KOKKOS_FORCEINLINE_FUNCTION
260 static T abs(const T a) {
261 return (a > 0 ? a : T(-a));
262 }
263
264 };
265
266 template<typename T>
267 KOKKOS_FORCEINLINE_FUNCTION
268 static T min(const T &a, const T &b) {
269 return (a < b ? a : b);
270 }
271
272 template<typename T>
273 KOKKOS_FORCEINLINE_FUNCTION
274 static T max(const T &a, const T &b) {
275 return (a > b ? a : b);
276 }
277
278 template<typename T>
279 KOKKOS_FORCEINLINE_FUNCTION
280 static T abs(const T &a) {
281 return (a > 0 ? a : T(-a));
282 }
283
291 template<typename T>
292 KOKKOS_FORCEINLINE_FUNCTION
293 constexpr typename
294 std::enable_if< !(std::is_standard_layout<T>::value && std::is_trivial<T>::value), typename ScalarTraits<T>::scalar_type >::type
295 get_scalar_value(const T& obj) {return obj.val();}
296
297 template<typename T>
298 KOKKOS_FORCEINLINE_FUNCTION
299 constexpr typename
300 std::enable_if< std::is_standard_layout<T>::value && std::is_trivial<T>::value, typename ScalarTraits<T>::scalar_type >::type
301 get_scalar_value(const T& obj){return obj;}
302
303
310 // These should not be needed anymore as the Sacado implementation of dimension_scalar should handle this, so commenting out
311
312 // template<typename T, typename ...P>
313 // KOKKOS_INLINE_FUNCTION
314 // constexpr typename
315 // std::enable_if< std::is_standard_layout<T>::value && std::is_trivial<T>::value, unsigned >::type
316 // dimension_scalar(const Kokkos::DynRankView<T, P...> /* view */) {return 1;}
317
318 // template<typename T, typename ...P>
319 // KOKKOS_INLINE_FUNCTION
320 // constexpr typename
321 // std::enable_if< std::is_standard_layout<typename Kokkos::View<T, P...>::value_type>::value && std::is_trivial<typename Kokkos::View<T, P...>::value_type>::value, unsigned >::type
322 // dimension_scalar(const Kokkos::View<T, P...> /*view*/) {return 1;}
323
324 template<typename T, typename ...P>
325 KOKKOS_FORCEINLINE_FUNCTION
326 static ordinal_type get_dimension_scalar(const Kokkos::DynRankView<T, P...> &view) {
327#ifdef HAVE_INTREPID2_SACADO
328 return Sacado::dimension_scalar(view);
329#else
330 return 1;
331#endif
332 }
333
334 template<typename T, typename ...P>
335 KOKKOS_FORCEINLINE_FUNCTION
336 static ordinal_type get_dimension_scalar(const Kokkos::View<T, P...> &view) {
337#ifdef HAVE_INTREPID2_SACADO
338 return Sacado::dimension_scalar(view);
339#else
340 return 1;
341#endif
342 }
343
345 // such that the ouptut view type has the default layout when the input has LayoutStride
346 template <typename InputView>
348 {
349 using value = typename InputView::non_const_value_type;
350 using layout = typename DeduceLayout<InputView>::result_layout;
351 using device = typename InputView::device_type;
352 using type = Kokkos::DynRankView<value, layout, device>;
353 };
354
355
356 template <class DataType, class... Properties>
357 KOKKOS_INLINE_FUNCTION auto
358 as_scalar_1d_view(const Kokkos::View<DataType, Properties...> &view) {
359 using view_t = Kokkos::View<DataType, Properties...>;
360#ifdef HAVE_INTREPID2_SACADO
361 if constexpr (Sacado::is_view_fad<view_t>::value) {
362 return Sacado::as_scalar_view(view);
363 } else
364#endif
365 return Kokkos::View<typename view_t::value_type*, Properties...>(view.data(), view.mapping().required_span_size());
366 }
367
368 template <class... Args>
369 KOKKOS_INLINE_FUNCTION auto
370 as_scalar_1d_view(const Kokkos::DynRankView<Args...> &dyn) {
371 return as_scalar_1d_view(dyn.ConstDownCast());
372 }
373
374
375 namespace Impl
376 {
380 template <class... ViewPack>
382 {
388 template <class OutViewType, class CtorProp, class... Dims>
389 static OutViewType
390 create_view(const ViewPack &...views,
391 const CtorProp &prop,
392 const Dims... dims)
393 {
394 #ifdef HAVE_INTREPID2_SACADO
395 using view_factory = Sacado::ViewFactory<ViewPack...>;
396 return view_factory::template create_view<OutViewType>(views..., prop, dims...);
397 #else
398 ((void)views, ...);
399 return OutViewType(prop, dims...);
400 #endif
401 }
402 };
403
410 template <typename OutViewType, typename InViewType, typename CtorProp, typename... Dims>
411 OutViewType
412 createMatchingView(const InViewType &view,
413 const CtorProp &prop,
414 const Dims... dims)
415 {
417 return cvf::template create_view<OutViewType>(view, prop, dims...);
418 }
419
425 template <typename InViewType, typename CtorProp, typename... Dims>
426 typename DeduceDynRankView<InViewType>::type
427 createMatchingDynRankView(const InViewType &view,
428 const CtorProp &prop,
429 const Dims... dims)
430 {
431 using OutViewType = typename DeduceDynRankView<InViewType>::type;
432 return createMatchingView<OutViewType>(view, prop, dims...);
433 }
434
441 template <typename OutViewType, typename InViewType, typename CtorProp, typename... Dims>
442 KOKKOS_INLINE_FUNCTION
443 typename std::enable_if<
444 std::is_pointer_v<CtorProp> && !std::is_convertible_v<CtorProp, const char*>,
445 OutViewType>::type
446 createMatchingUnmanagedView(const InViewType &view, const CtorProp &data, const Dims... dims)
447 {
448 #ifdef HAVE_INTREPID2_SACADO
449 if constexpr (Sacado::is_view_fad<InViewType>::value)
450 {
451 const int derivative_dimension = get_dimension_scalar(view);
452 return OutViewType(data, dims..., derivative_dimension);
453 }
454 else
455 return OutViewType(data, dims...);
456 #else
457 (void)view;
458 return OutViewType(data, dims...);
459 #endif
460 }
461
467 template <typename InViewType, typename CtorProp, typename ... Dims>
468 KOKKOS_INLINE_FUNCTION
469 typename std::enable_if<
470 std::is_pointer_v<CtorProp> && !std::is_convertible_v<CtorProp, const char*>,
471 typename DeduceDynRankView<InViewType>::type>::type
472 createMatchingUnmanagedDynRankView(const InViewType& view, const CtorProp& data, const Dims... dims){
473 using OutViewType = typename DeduceDynRankView<InViewType>::type;
474 return createMatchingUnmanagedView<OutViewType>(view, data, dims...);
475 }
476
477 } //Impl namespace
478
479
488 template<class ViewType, class ... DimArgs>
489 inline
490 Kokkos::DynRankView<typename ViewType::value_type, typename DeduceLayout< ViewType >::result_layout, typename ViewType::device_type >
491 getMatchingViewWithLabel(const ViewType &view, const std::string &label, DimArgs... dims)
492 {
493 return Impl::createMatchingDynRankView(view, label, dims...);
494 }
495
496 using std::enable_if_t;
497
501 template <typename T, typename = void>
502 struct has_rank_member : std::false_type{};
503
507 template <typename T>
508 struct has_rank_member<T, decltype((void)T::rank, void())> : std::true_type {};
509
510 static_assert(! has_rank_member<Kokkos::DynRankView<double> >::value, "DynRankView does not have a member rank, so this assert should pass -- if not, something may be wrong with has_rank_member.");
511
515 template<class Functor, ordinal_type default_value>
516 constexpr
517 enable_if_t<has_rank_member<Functor>::value, ordinal_type>
519 {
520 return Functor::rank;
521 }
522
526 template<class Functor, ordinal_type default_value>
527 constexpr
528 enable_if_t<!has_rank_member<Functor>::value, ordinal_type>
530 {
531 return default_value;
532 }
533
537 template <typename T>
539 {
540 typedef char one;
541 struct two { char x[2]; };
542
543 template <typename C> static one test( typename std::remove_reference<decltype( std::declval<C>().operator()(0))>::type );
544 template <typename C> static two test(...);
545
546 public:
547 enum { value = sizeof(test<T>(0)) == sizeof(char) && (getFixedRank<T,1>() == 1) };
548 };
549
553 template <typename T>
555 {
556 typedef char one;
557 struct two { char x[2]; };
558
559 template <typename C> static one test( typename std::remove_reference<decltype( std::declval<C>().operator()(0,0))>::type ) ;
560 template <typename C> static two test(...);
561
562 public:
563 enum { value = sizeof(test<T>(0)) == sizeof(char) && (getFixedRank<T,2>() == 2) };
564 };
565
569 template <typename T>
571 {
572 typedef char one;
573 struct two { char x[2]; };
574
575 template <typename C> static one test( typename std::remove_reference<decltype( std::declval<C>().operator()(0,0,0))>::type ) ;
576 template <typename C> static two test(...);
577
578 public:
579 enum { value = (sizeof(test<T>(0)) == sizeof(char)) && (getFixedRank<T,3>() == 3) };
580 };
581
585 template <typename T>
587 {
588 typedef char one;
589 struct two { char x[2]; };
590
591 template <typename C> static one test( typename std::remove_reference<decltype( std::declval<C>().operator()(0,0,0,0))>::type ) ;
592 template <typename C> static two test(...);
593
594 public:
595 enum { value = sizeof(test<T>(0)) == sizeof(char) && (getFixedRank<T,4>() == 4) };
596 };
597
601 template <typename T>
603 {
604 typedef char one;
605 struct two { char x[2]; };
606
607 template <typename C> static one test( typename std::remove_reference<decltype( std::declval<C>().operator()(0,0,0,0,0))>::type ) ;
608 template <typename C> static two test(...);
609
610 public:
611 enum { value = sizeof(test<T>(0)) == sizeof(char) && (getFixedRank<T,5>() == 5) };
612 };
613
617 template <typename T>
619 {
620 typedef char one;
621 struct two { char x[2]; };
622
623 template <typename C> static one test( typename std::remove_reference<decltype( std::declval<C>().operator()(0,0,0,0,0,0))>::type ) ;
624 template <typename C> static two test(...);
625
626 public:
627 enum { value = sizeof(test<T>(0)) == sizeof(char) && (getFixedRank<T,6>() == 6) };
628 };
629
633 template <typename T>
635 {
636 typedef char one;
637 struct two { char x[2]; };
638
639 template <typename C> static one test( typename std::remove_reference<decltype( std::declval<C>().operator()(0,0,0,0,0,0,0))>::type ) ;
640 template <typename C> static two test(...);
641
642 public:
643 enum { value = sizeof(test<T>(0)) == sizeof(char) && (getFixedRank<T,7>() == 7) };
644 };
645
649 template <typename T, int rank>
651 {
652 public:
653 enum { value = false };
654 };
655
659 template <typename T>
660 class supports_rank<T,1>
661 {
662 public:
663 enum { value = supports_rank_1<T>::value };
664 };
665
667 template <typename T>
668 class supports_rank<T,2>
669 {
670 public:
671 enum { value = supports_rank_2<T>::value };
672 };
673
675 template <typename T>
676 class supports_rank<T,3>
677 {
678 public:
679 enum { value = supports_rank_3<T>::value };
680 };
681
683 template <typename T>
684 class supports_rank<T,4>
685 {
686 public:
687 enum { value = supports_rank_4<T>::value };
688 };
689
691 template <typename T>
692 class supports_rank<T,5>
693 {
694 public:
695 enum { value = supports_rank_5<T>::value };
696 };
697
699 template <typename T>
700 class supports_rank<T,6>
701 {
702 public:
703 enum { value = supports_rank_6<T>::value };
704 };
705
707 template <typename T>
708 class supports_rank<T,7>
709 {
710 public:
711 enum { value = supports_rank_7<T>::value };
712 };
713
714
715
719 template<typename Scalar, int rank>
721
722 };
723
727 template<typename Scalar>
728 struct RankExpander<Scalar,0>
729 {
730 using value_type = Scalar;
731 };
732
736 template<typename Scalar>
737 struct RankExpander<Scalar,1>
738 {
739 using value_type = Scalar*;
740 };
741
745 template<typename Scalar>
746 struct RankExpander<Scalar,2>
747 {
748 using value_type = Scalar**;
749 };
750
754 template<typename Scalar>
755 struct RankExpander<Scalar,3>
756 {
757 using value_type = Scalar***;
758 };
759
763 template<typename Scalar>
764 struct RankExpander<Scalar,4>
765 {
766 using value_type = Scalar****;
767 };
768
772 template<typename Scalar>
773 struct RankExpander<Scalar,5>
774 {
775 using value_type = Scalar*****;
776 };
777
781 template<typename Scalar>
782 struct RankExpander<Scalar,6>
783 {
784 using value_type = Scalar******;
785 };
786
790 template<typename Scalar>
791 struct RankExpander<Scalar,7>
792 {
793 using value_type = Scalar*******;
794 };
795
796 // positive checks of supports_rank for Kokkos::DynRankView:
797 static_assert(supports_rank<Kokkos::DynRankView<double>, 1>::value, "rank 1 check of supports_rank for DynRankView");
798 static_assert(supports_rank<Kokkos::DynRankView<double>, 2>::value, "rank 2 check of supports_rank for DynRankView");
799 static_assert(supports_rank<Kokkos::DynRankView<double>, 3>::value, "rank 3 check of supports_rank for DynRankView");
800 static_assert(supports_rank<Kokkos::DynRankView<double>, 4>::value, "rank 4 check of supports_rank for DynRankView");
801 static_assert(supports_rank<Kokkos::DynRankView<double>, 5>::value, "rank 5 check of supports_rank for DynRankView");
802 static_assert(supports_rank<Kokkos::DynRankView<double>, 6>::value, "rank 6 check of supports_rank for DynRankView");
803 static_assert(supports_rank<Kokkos::DynRankView<double>, 7>::value, "rank 7 check of supports_rank for DynRankView");
804
805 // positive checks of supports_rank for Kokkos::View:
806 static_assert(supports_rank<Kokkos::View<double*>, 1>::value, "rank 1 check of supports_rank");
807 static_assert(supports_rank<Kokkos::View<double**>, 2>::value, "rank 2 check of supports_rank");
808 static_assert(supports_rank<Kokkos::View<double***>, 3>::value, "rank 3 check of supports_rank");
809 static_assert(supports_rank<Kokkos::View<double****>, 4>::value, "rank 4 check of supports_rank");
810 static_assert(supports_rank<Kokkos::View<double*****>, 5>::value, "rank 5 check of supports_rank");
811 static_assert(supports_rank<Kokkos::View<double******>, 6>::value, "rank 6 check of supports_rank");
812 static_assert(supports_rank<Kokkos::View<double*******>, 7>::value, "rank 7 check of supports_rank");
813
814 // negative checks of supports_rank for Kokkos::View:
815 static_assert(!supports_rank<Kokkos::View<double*>, 2>::value, "rank 1 check of supports_rank");
816 static_assert(!supports_rank<Kokkos::View<double*>, 3>::value, "rank 1 check of supports_rank");
817 static_assert(!supports_rank<Kokkos::View<double*>, 4>::value, "rank 1 check of supports_rank");
818 static_assert(!supports_rank<Kokkos::View<double*>, 5>::value, "rank 1 check of supports_rank");
819 static_assert(!supports_rank<Kokkos::View<double*>, 6>::value, "rank 1 check of supports_rank");
820 static_assert(!supports_rank<Kokkos::View<double*>, 7>::value, "rank 1 check of supports_rank");
821 static_assert(!supports_rank<Kokkos::View<double**>, 1>::value, "rank 2 check of supports_rank");
822 static_assert(!supports_rank<Kokkos::View<double**>, 3>::value, "rank 2 check of supports_rank");
823 static_assert(!supports_rank<Kokkos::View<double**>, 4>::value, "rank 2 check of supports_rank");
824 static_assert(!supports_rank<Kokkos::View<double**>, 5>::value, "rank 2 check of supports_rank");
825 static_assert(!supports_rank<Kokkos::View<double**>, 6>::value, "rank 2 check of supports_rank");
826 static_assert(!supports_rank<Kokkos::View<double**>, 7>::value, "rank 2 check of supports_rank");
827 static_assert(!supports_rank<Kokkos::View<double***>, 1>::value, "rank 3 check of supports_rank");
828 static_assert(!supports_rank<Kokkos::View<double***>, 2>::value, "rank 3 check of supports_rank");
829 static_assert(!supports_rank<Kokkos::View<double***>, 4>::value, "rank 3 check of supports_rank");
830 static_assert(!supports_rank<Kokkos::View<double***>, 5>::value, "rank 3 check of supports_rank");
831 static_assert(!supports_rank<Kokkos::View<double***>, 6>::value, "rank 3 check of supports_rank");
832 static_assert(!supports_rank<Kokkos::View<double***>, 7>::value, "rank 3 check of supports_rank");
833 static_assert(!supports_rank<Kokkos::View<double****>, 1>::value, "rank 4 check of supports_rank");
834 static_assert(!supports_rank<Kokkos::View<double****>, 2>::value, "rank 4 check of supports_rank");
835 static_assert(!supports_rank<Kokkos::View<double****>, 3>::value, "rank 4 check of supports_rank");
836 static_assert(!supports_rank<Kokkos::View<double****>, 5>::value, "rank 4 check of supports_rank");
837 static_assert(!supports_rank<Kokkos::View<double****>, 6>::value, "rank 4 check of supports_rank");
838 static_assert(!supports_rank<Kokkos::View<double****>, 7>::value, "rank 4 check of supports_rank");
839 static_assert(!supports_rank<Kokkos::View<double*****>, 1>::value, "rank 5 check of supports_rank");
840 static_assert(!supports_rank<Kokkos::View<double*****>, 2>::value, "rank 5 check of supports_rank");
841 static_assert(!supports_rank<Kokkos::View<double*****>, 3>::value, "rank 5 check of supports_rank");
842 static_assert(!supports_rank<Kokkos::View<double*****>, 4>::value, "rank 5 check of supports_rank");
843 static_assert(!supports_rank<Kokkos::View<double*****>, 6>::value, "rank 5 check of supports_rank");
844 static_assert(!supports_rank<Kokkos::View<double*****>, 7>::value, "rank 5 check of supports_rank");
845 static_assert(!supports_rank<Kokkos::View<double******>, 1>::value, "rank 6 check of supports_rank");
846 static_assert(!supports_rank<Kokkos::View<double******>, 2>::value, "rank 6 check of supports_rank");
847 static_assert(!supports_rank<Kokkos::View<double******>, 3>::value, "rank 6 check of supports_rank");
848 static_assert(!supports_rank<Kokkos::View<double******>, 4>::value, "rank 6 check of supports_rank");
849 static_assert(!supports_rank<Kokkos::View<double******>, 5>::value, "rank 6 check of supports_rank");
850 static_assert(!supports_rank<Kokkos::View<double******>, 7>::value, "rank 6 check of supports_rank");
851 static_assert(!supports_rank<Kokkos::View<double*******>, 1>::value, "rank 7 check of supports_rank");
852 static_assert(!supports_rank<Kokkos::View<double*******>, 2>::value, "rank 7 check of supports_rank");
853 static_assert(!supports_rank<Kokkos::View<double*******>, 3>::value, "rank 7 check of supports_rank");
854 static_assert(!supports_rank<Kokkos::View<double*******>, 4>::value, "rank 7 check of supports_rank");
855 static_assert(!supports_rank<Kokkos::View<double*******>, 5>::value, "rank 7 check of supports_rank");
856 static_assert(!supports_rank<Kokkos::View<double*******>, 6>::value, "rank 7 check of supports_rank");
857
861 template <typename T>
863 {
864 typedef char one;
865 struct two { char x[2]; };
866
867 template <typename C> static one test( decltype( std::declval<C>().rank() ) ) ;
868 template <typename C> static two test(...);
869
870 public:
871 enum { value = sizeof(test<T>(0)) == sizeof(char) };
872 };
873
874 static_assert( has_rank_method<Kokkos::DynRankView<double> >::value, "DynRankView implements rank(), so this assert should pass -- if not, something may be wrong with has_rank_method.");
875
879 template<class Functor>
880 enable_if_t<has_rank_method<Functor>::value, unsigned>
881 KOKKOS_INLINE_FUNCTION
882 getFunctorRank(const Functor &functor)
883 {
884 return functor.rank();
885 }
886
890 template<class Functor>
891 enable_if_t<!has_rank_method<Functor>::value, unsigned>
892 KOKKOS_INLINE_FUNCTION
893 getFunctorRank(const Functor &functor)
894 {
895 return functor.rank;
896 }
897
901 template <typename ValueType>
903 using layout = Kokkos::LayoutLeft;
904 };
905
906 // define vector sizes for hierarchical parallelism
907 const int VECTOR_SIZE = 1;
908#if defined(SACADO_VIEW_CUDA_HIERARCHICAL_DFAD) && defined(INTREPID2_ENABLE_DEVICE)
909 const int FAD_VECTOR_SIZE = 32;
910#else
911 const int FAD_VECTOR_SIZE = 1;
912#endif
913
917 template<typename Scalar>
919 {
920 return (std::is_standard_layout<Scalar>::value && std::is_trivial<Scalar>::value) ? VECTOR_SIZE : FAD_VECTOR_SIZE;
921 }
922
928 template<typename ViewType>
929 KOKKOS_INLINE_FUNCTION
930 constexpr unsigned getScalarDimensionForView(const ViewType &view)
931 {
932 return (std::is_standard_layout<typename ViewType::value_type>::value && std::is_trivial<typename ViewType::value_type>::value) ? 0 : get_dimension_scalar(view);
933 }
934
936 template<typename Device>
938 template<typename T>
939 void operator()(T* ptr) {
940 Kokkos::parallel_for(Kokkos::RangePolicy<typename Device::execution_space>(0,1),
941 KOKKOS_LAMBDA (const int i) { ptr->~T(); });
942 typename Device::execution_space().fence();
943 Kokkos::kokkos_free<typename Device::memory_space>(ptr);
944 }
945 };
946
950 template<typename Device,typename Derived>
951 std::unique_ptr<Derived,DeviceDeleter<Device>>
952 copy_virtual_class_to_device(const Derived& host_source)
953 {
954 auto* p = static_cast<Derived*>(Kokkos::kokkos_malloc<typename Device::memory_space>(sizeof(Derived)));
955 Kokkos::parallel_for(Kokkos::RangePolicy<typename Device::execution_space>(0,1),
956 KOKKOS_LAMBDA (const int i) {new (p) Derived(host_source); });
957 typename Device::execution_space().fence();
958 return std::unique_ptr<Derived,DeviceDeleter<Device>>(p);
959 }
960
961
962
963
964
965
966
967
968} // end namespace Intrepid2
969
970#endif
Implementation of an assert that can safely be called from device code.
Contains definitions of custom data types in Intrepid2.
enable_if_t< has_rank_method< Functor >::value, unsigned > KOKKOS_INLINE_FUNCTION getFunctorRank(const Functor &functor)
KOKKOS_INLINE_FUNCTION std::enable_if< std::is_pointer_v< CtorProp > &&!std::is_convertible_v< CtorProp, constchar * >, OutViewType >::type createMatchingUnmanagedView(const InViewType &view, const CtorProp &data, const Dims... dims)
Creates an unmanaged view that matches the value_type of the provided view The type of the output vie...
DeduceDynRankView< InViewType >::type createMatchingDynRankView(const InViewType &view, const CtorProp &prop, const Dims... dims)
Creates and returns a view that matches the value_type of the provided view The output view type is d...
KOKKOS_INLINE_FUNCTION std::enable_if< std::is_pointer_v< CtorProp > &&!std::is_convertible_v< CtorProp, constchar * >, typenameDeduceDynRankView< InViewType >::type >::type createMatchingUnmanagedDynRankView(const InViewType &view, const CtorProp &data, const Dims... dims)
Creates an unmanaged view that matches the value_type of the provided view The output view type is de...
OutViewType createMatchingView(const InViewType &view, const CtorProp &prop, const Dims... dims)
Creates and returns a view that matches the value_type of the provided view The type of the output vi...
constexpr int getVectorSizeForHierarchicalParallelism()
Returns a vector size to be used for the provided Scalar type in the context of hierarchically-parall...
static KOKKOS_FORCEINLINE_FUNCTION ordinal_type get_dimension_scalar(const Kokkos::DynRankView< T, P... > &view)
specialization of functions for pod types, returning the scalar dimension (1 for pod types) of a view...
KOKKOS_FORCEINLINE_FUNCTION constexpr std::enable_if<!(std::is_standard_layout< T >::value &&std::is_trivial< T >::value), typenameScalarTraits< T >::scalar_type >::type get_scalar_value(const T &obj)
functions returning the scalar value. for pod types, they return the input object itself....
constexpr enable_if_t< has_rank_member< Functor >::value, ordinal_type > getFixedRank()
std::unique_ptr< Derived, DeviceDeleter< Device > > copy_virtual_class_to_device(const Derived &host_source)
KOKKOS_INLINE_FUNCTION constexpr unsigned getScalarDimensionForView(const ViewType &view)
Returns the size of the Scalar dimension for the View. This is 0 for non-AD types....
Kokkos::DynRankView< typename ViewType::value_type, typename DeduceLayout< ViewType >::result_layout, typename ViewType::device_type > getMatchingViewWithLabel(const ViewType &view, const std::string &label, DimArgs... dims)
Creates and returns a view that matches the provided view in Kokkos Layout. DEPRECATED,...
small utility functions
Tests whether a class implements rank(). Used in getFunctorRank() method below; allows us to do one t...
SFINAE helper to detect whether a type supports a 1-integral-argument operator().
SFINAE helper to detect whether a type supports a 2-integral-argument operator().
SFINAE helper to detect whether a type supports a 3-integral-argument operator().
SFINAE helper to detect whether a type supports a 4-integral-argument operator().
SFINAE helper to detect whether a type supports a 5-integral-argument operator().
SFINAE helper to detect whether a type supports a 6-integral-argument operator().
SFINAE helper to detect whether a type supports a 7-integral-argument operator().
SFINAE helper to detect whether a type supports a rank-integral-argument operator().
Used to obtain the dynRankView type from an input View,.
layout deduction (temporary meta-function)
Struct for deleting device instantiation.
Factory to create a view based on the properties of input views The class is useful when the view can...
static OutViewType create_view(const ViewPack &...views, const CtorProp &prop, const Dims... dims)
Creates and returns a view that matches the value_type of the provided view When Sacado is enabled we...
Define layout that will allow us to wrap Sacado Scalar objects in Views without copying.
Helper to get Scalar[*+] where the number of *'s matches the given rank.
Tests whether a class has a member rank. Used in getFixedRank() method below, which in turn is used i...