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
15
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 "Kokkos_View_Fad_Fwd.hpp"
29#include "Kokkos_LayoutNatural.hpp"
30#include "Kokkos_ViewFactory.hpp"
31#endif
32
33namespace Intrepid2 {
34
35#if defined(__CUDA_ARCH__) || defined(__HIP_DEVICE_COMPILE__) || defined(__SYCL_DEVICE_ONLY__)
36#define INTREPID2_COMPILE_DEVICE_CODE
37#endif
38
39#if defined(KOKKOS_ENABLE_CUDA) || defined(KOKKOS_ENABLE_HIP) || defined(KOKKOS_ENABLE_SYCL)
40#define INTREPID2_ENABLE_DEVICE
41#endif
42
43#if defined(KOKKOS_OPT_RANGE_AGGRESSIVE_VECTORIZATION) \
44 && defined(KOKKOS_ENABLE_PRAGMA_IVDEP) \
45 && !defined(INTREPID2_COMPILE_DEVICE_CODE)
46#define INTREPID2_USE_IVDEP
47#endif
48
49 //
50 // test macros
51 //
52
53#define INTREPID2_TEST_FOR_WARNING(test, msg) \
54 if (test) { \
55 Kokkos::printf("[Intrepid2] Warning in file %s, line %d\n",__FILE__,__LINE__); \
56 Kokkos::printf(" Test that evaluated to true: %s\n", #test); \
57 Kokkos::printf(" %s \n", msg); \
58 }
59
60#define INTREPID2_TEST_FOR_EXCEPTION(test, x, msg) \
61 if (test) { \
62 Kokkos::printf("[Intrepid2] Error in file %s, line %d\n",__FILE__,__LINE__); \
63 Kokkos::printf(" Test that evaluated to true: %s\n", #test); \
64 Kokkos::printf(" %s \n", msg); \
65 throw x(msg); \
66 }
67
70#ifndef INTREPID2_ENABLE_DEVICE
71#define INTREPID2_TEST_FOR_EXCEPTION_DEVICE_SAFE(test, x, msg) \
72 if (test) { \
73 std::cout << "[Intrepid2] Error in file " << __FILE__ << ", line " << __LINE__ << "\n"; \
74 std::cout << " Test that evaluated to true: " << #test << "\n"; \
75 std::cout << " " << msg << " \n"; \
76 throw x(msg); \
77 }
78#else
79#define INTREPID2_TEST_FOR_EXCEPTION_DEVICE_SAFE(test, x, msg) \
80 if (test) { \
81 Kokkos::printf("[Intrepid2] Error in file %s, line %d\n",__FILE__,__LINE__); \
82 Kokkos::printf(" Test that evaluated to true: %s\n", #test); \
83 Kokkos::printf(" %s \n", msg); \
84 Kokkos::abort( "[Intrepid2] Abort\n"); \
85 }
86#endif
87#if defined(INTREPID2_ENABLE_DEBUG) || defined(NDEBUG) || 1
88#define INTREPID2_TEST_FOR_ABORT(test, msg) \
89 if (test) { \
90 Kokkos::printf("[Intrepid2] Error in file %s, line %d\n",__FILE__,__LINE__); \
91 Kokkos::printf(" Test that evaluated to true: %s\n", #test); \
92 Kokkos::printf(" %s \n", msg); \
93 Kokkos::abort( "[Intrepid2] Abort\n"); \
94 }
95#else
96#define INTREPID2_TEST_FOR_ABORT(test, msg) ((void)0)
97#endif
98 // check the first error only
99#ifdef INTREPID2_TEST_FOR_DEBUG_ABORT_OVERRIDE_TO_CONTINUE
100#define INTREPID2_TEST_FOR_DEBUG_ABORT(test, info, msg) \
101 if (!(info) && (test)) { \
102 Kokkos::printf("[Intrepid2] Error in file %s, line %d\n",__FILE__,__LINE__); \
103 Kokkos::printf(" Test that evaluated to true: %s\n", #test); \
104 Kokkos::printf(" %s \n", msg); \
105 info = true; \
106 }
107#else
108#define INTREPID2_TEST_FOR_DEBUG_ABORT(test, info, msg) \
109 if (!(info) && (test)) { \
110 Kokkos::printf("[Intrepid2] Error in file %s, line %d\n",__FILE__,__LINE__); \
111 Kokkos::printf(" Test that evaluated to true: %s\n", #test); \
112 Kokkos::printf(" %s \n", msg); \
113 info = true ; \
114 Kokkos::abort( "[Intrepid2] Abort\n"); \
115 }
116#endif
117
121 template<typename T>
123 typedef typename T::scalar_type scalar_type;
124 };
125
126 // this is built in types to support
130 template<>
131 struct ScalarTraits<float> {
132 typedef float scalar_type;
133 };
134
137 template<>
138 struct ScalarTraits<double> {
139 typedef double scalar_type;
140 };
141
144 template<>
145 struct ScalarTraits<int> {
146 typedef int scalar_type;
147 };
148
151 template<>
152 struct ScalarTraits<long int> {
153 typedef long int scalar_type;
154 };
155
158 template<>
159 struct ScalarTraits<long long> {
160 typedef long long scalar_type;
161 };
162
163
164
168 template<typename ViewSpaceType, typename UserSpaceType>
169 struct ExecSpace {
170 typedef UserSpaceType ExecSpaceType;
171 };
172
176 template<typename ViewSpaceType>
177 struct ExecSpace<ViewSpaceType,void> {
178 typedef ViewSpaceType ExecSpaceType;
179 };
180
181
185 template <typename ViewType>
187 using input_layout = typename ViewType::array_layout;
188 using default_layout = typename ViewType::device_type::execution_space::array_layout;
189 using result_layout =
190 typename std::conditional<
191 std::is_same< input_layout, Kokkos::LayoutStride >::value,
192 default_layout,
193 input_layout >::type;
194 };
195
196
197 //
198 // utilities device comparible
199 //
200
201 // this will be gone
202 template<typename IdxType, typename DimType, typename IterType>
203 KOKKOS_FORCEINLINE_FUNCTION
204 static void
205 unrollIndex(IdxType &i, IdxType &j,
206 const DimType /* dim0 */,
207 const DimType dim1,
208 const IterType iter) {
209 // left index
210 //j = iter/dim0;
211 //i = iter%dim0;
212
213 // right index
214 i = iter/dim1;
215 j = iter%dim1;
216 }
217
218 template<typename IdxType, typename DimType, typename IterType>
219 KOKKOS_FORCEINLINE_FUNCTION
220 static void
221 unrollIndex(IdxType &i, IdxType &j, IdxType &k,
222 const DimType dim0,
223 const DimType dim1,
224 const DimType dim2,
225 const IterType iter) {
226 IdxType tmp;
227
228 //unrollIndex(tmp, k, dim0*dim1, dim2, iter);
229 //unrollIndex( i, j, dim0, dim1, tmp);
230
231 unrollIndex( i, tmp, dim0, dim1*dim2, iter);
232 unrollIndex( j, k, dim1, dim2, tmp);
233 }
234
238 template<typename T>
239 class Util {
240 public:
241 KOKKOS_FORCEINLINE_FUNCTION
242 static T min(const T a, const T b) {
243 return (a < b ? a : b);
244 }
245
246 KOKKOS_FORCEINLINE_FUNCTION
247 static T max(const T a, const T b) {
248 return (a > b ? a : b);
249 }
250
251 KOKKOS_FORCEINLINE_FUNCTION
252 static T abs(const T a) {
253 return (a > 0 ? a : T(-a));
254 }
255
256 };
257
258 template<typename T>
259 KOKKOS_FORCEINLINE_FUNCTION
260 static T min(const T &a, const T &b) {
261 return (a < b ? a : b);
262 }
263
264 template<typename T>
265 KOKKOS_FORCEINLINE_FUNCTION
266 static T max(const T &a, const T &b) {
267 return (a > b ? a : b);
268 }
269
270 template<typename T>
271 KOKKOS_FORCEINLINE_FUNCTION
272 static T abs(const T &a) {
273 return (a > 0 ? a : T(-a));
274 }
275
282
283 template<typename T>
284 KOKKOS_FORCEINLINE_FUNCTION
285 constexpr typename
286 std::enable_if< !(std::is_standard_layout<T>::value && std::is_trivial<T>::value), typename ScalarTraits<T>::scalar_type >::type
287 get_scalar_value(const T& obj) {return obj.val();}
288
289 template<typename T>
290 KOKKOS_FORCEINLINE_FUNCTION
291 constexpr typename
292 std::enable_if< std::is_standard_layout<T>::value && std::is_trivial<T>::value, typename ScalarTraits<T>::scalar_type >::type
293 get_scalar_value(const T& obj){return obj;}
294
295
301
302 template<typename T, typename ...P>
303 KOKKOS_INLINE_FUNCTION
304 constexpr typename
305 std::enable_if< std::is_standard_layout<T>::value && std::is_trivial<T>::value, unsigned >::type
306 dimension_scalar(const Kokkos::DynRankView<T, P...> /* view */) {return 1;}
307
308 template<typename T, typename ...P>
309 KOKKOS_INLINE_FUNCTION
310 constexpr typename
311 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
312 dimension_scalar(const Kokkos::View<T, P...> /*view*/) {return 1;}
313
314 template<typename T, typename ...P>
315 KOKKOS_FORCEINLINE_FUNCTION
316 static ordinal_type get_dimension_scalar(const Kokkos::DynRankView<T, P...> &view) {
317 return dimension_scalar(view);
318 }
319
320 template<typename T, typename ...P>
321 KOKKOS_FORCEINLINE_FUNCTION
322 static ordinal_type get_dimension_scalar(const Kokkos::View<T, P...> &view) {
323 return dimension_scalar(view);
324 }
325
327 // such that the ouptut view type has the default layout when the input has LayoutStride
328 template <typename InputView>
330 {
331 using value = typename InputView::non_const_value_type;
332 using layout = typename DeduceLayout<InputView>::result_layout;
333 using device = typename InputView::device_type;
334 using type = Kokkos::DynRankView<value, layout, device>;
335 };
336
337
338 namespace Impl
339 {
343 template <class... ViewPack>
345 {
351 template <class OutViewType, class CtorProp, class... Dims>
352 static OutViewType
353 create_view(const ViewPack &...views,
354 const CtorProp &prop,
355 const Dims... dims)
356 {
357 #ifdef HAVE_INTREPID2_SACADO
358 using view_factory = Kokkos::ViewFactory<ViewPack...>;
359 return view_factory::template create_view<OutViewType>(views..., prop, dims...);
360 #else
361 ((void)views, ...);
362 return OutViewType(prop, dims...);
363 #endif
364 }
365 };
366
373 template <typename OutViewType, typename InViewType, typename CtorProp, typename... Dims>
374 OutViewType
375 createMatchingView(const InViewType &view,
376 const CtorProp &prop,
377 const Dims... dims)
378 {
380 return cvf::template create_view<OutViewType>(view, prop, dims...);
381 }
382
388 template <typename InViewType, typename CtorProp, typename... Dims>
389 typename DeduceDynRankView<InViewType>::type
390 createMatchingDynRankView(const InViewType &view,
391 const CtorProp &prop,
392 const Dims... dims)
393 {
394 using OutViewType = typename DeduceDynRankView<InViewType>::type;
395 return createMatchingView<OutViewType>(view, prop, dims...);
396 }
397
404 template <typename OutViewType, typename InViewType, typename CtorProp, typename... Dims>
405 KOKKOS_INLINE_FUNCTION
406 typename std::enable_if<
407 std::is_pointer_v<CtorProp> && !std::is_convertible_v<CtorProp, const char*>,
408 OutViewType>::type
409 createMatchingUnmanagedView(const InViewType &view, const CtorProp &data, const Dims... dims)
410 {
411 #ifdef HAVE_INTREPID2_SACADO
412 #ifdef SACADO_HAS_NEW_KOKKOS_VIEW_IMPL
413 if constexpr (Sacado::is_view_fad<InViewType>::value)
414 #else
415 if constexpr (Kokkos::is_view_fad<InViewType>::value)
416 #endif
417 {
418 const int derivative_dimension = get_dimension_scalar(view);
419 return OutViewType(data, dims..., derivative_dimension);
420 }
421 else
422 return OutViewType(data, dims...);
423 #else
424 (void)view;
425 return OutViewType(data, dims...);
426 #endif
427 }
428
434 template <typename InViewType, typename CtorProp, typename ... Dims>
435 KOKKOS_INLINE_FUNCTION
436 typename std::enable_if<
437 std::is_pointer_v<CtorProp> && !std::is_convertible_v<CtorProp, const char*>,
438 typename DeduceDynRankView<InViewType>::type>::type
439 createMatchingUnmanagedDynRankView(const InViewType& view, const CtorProp& data, const Dims... dims){
440 using OutViewType = typename DeduceDynRankView<InViewType>::type;
441 return createMatchingUnmanagedView<OutViewType>(view, data, dims...);
442 }
443
444 } //Impl namespace
445
446
455 template<class ViewType, class ... DimArgs>
456 inline
457 Kokkos::DynRankView<typename ViewType::value_type, typename DeduceLayout< ViewType >::result_layout, typename ViewType::device_type >
458 getMatchingViewWithLabel(const ViewType &view, const std::string &label, DimArgs... dims)
459 {
460 return Impl::createMatchingDynRankView(view, label, dims...);
461 }
462
463 using std::enable_if_t;
464
468 template <typename T, typename = void>
469 struct has_rank_member : std::false_type{};
470
474 template <typename T>
475 struct has_rank_member<T, decltype((void)T::rank, void())> : std::true_type {};
476
477 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.");
478#if KOKKOS_VERSION < 40099
479 static_assert( has_rank_member<Kokkos::View<double*> >::value, "View has a member rank -- if this assert fails, something may be wrong with has_rank_member.");
480#endif
481
485 template<class Functor, ordinal_type default_value>
486 constexpr
487 enable_if_t<has_rank_member<Functor>::value, ordinal_type>
489 {
490 return Functor::rank;
491 }
492
496 template<class Functor, ordinal_type default_value>
497 constexpr
498 enable_if_t<!has_rank_member<Functor>::value, ordinal_type>
500 {
501 return default_value;
502 }
503
507 template <typename T>
509 {
510 typedef char one;
511 struct two { char x[2]; };
512
513 template <typename C> static one test( typename std::remove_reference<decltype( std::declval<C>().operator()(0))>::type );
514 template <typename C> static two test(...);
515
516 public:
517 enum { value = sizeof(test<T>(0)) == sizeof(char) && (getFixedRank<T,1>() == 1) };
518 };
519
523 template <typename T>
525 {
526 typedef char one;
527 struct two { char x[2]; };
528
529 template <typename C> static one test( typename std::remove_reference<decltype( std::declval<C>().operator()(0,0))>::type ) ;
530 template <typename C> static two test(...);
531
532 public:
533 enum { value = sizeof(test<T>(0)) == sizeof(char) && (getFixedRank<T,2>() == 2) };
534 };
535
539 template <typename T>
541 {
542 typedef char one;
543 struct two { char x[2]; };
544
545 template <typename C> static one test( typename std::remove_reference<decltype( std::declval<C>().operator()(0,0,0))>::type ) ;
546 template <typename C> static two test(...);
547
548 public:
549 enum { value = (sizeof(test<T>(0)) == sizeof(char)) && (getFixedRank<T,3>() == 3) };
550 };
551
555 template <typename T>
557 {
558 typedef char one;
559 struct two { char x[2]; };
560
561 template <typename C> static one test( typename std::remove_reference<decltype( std::declval<C>().operator()(0,0,0,0))>::type ) ;
562 template <typename C> static two test(...);
563
564 public:
565 enum { value = sizeof(test<T>(0)) == sizeof(char) && (getFixedRank<T,4>() == 4) };
566 };
567
571 template <typename T>
573 {
574 typedef char one;
575 struct two { char x[2]; };
576
577 template <typename C> static one test( typename std::remove_reference<decltype( std::declval<C>().operator()(0,0,0,0,0))>::type ) ;
578 template <typename C> static two test(...);
579
580 public:
581 enum { value = sizeof(test<T>(0)) == sizeof(char) && (getFixedRank<T,5>() == 5) };
582 };
583
587 template <typename T>
589 {
590 typedef char one;
591 struct two { char x[2]; };
592
593 template <typename C> static one test( typename std::remove_reference<decltype( std::declval<C>().operator()(0,0,0,0,0,0))>::type ) ;
594 template <typename C> static two test(...);
595
596 public:
597 enum { value = sizeof(test<T>(0)) == sizeof(char) && (getFixedRank<T,6>() == 6) };
598 };
599
603 template <typename T>
605 {
606 typedef char one;
607 struct two { char x[2]; };
608
609 template <typename C> static one test( typename std::remove_reference<decltype( std::declval<C>().operator()(0,0,0,0,0,0,0))>::type ) ;
610 template <typename C> static two test(...);
611
612 public:
613 enum { value = sizeof(test<T>(0)) == sizeof(char) && (getFixedRank<T,7>() == 7) };
614 };
615
619 template <typename T, int rank>
621 {
622 public:
623 enum { value = false };
624 };
625
629 template <typename T>
630 class supports_rank<T,1>
631 {
632 public:
633 enum { value = supports_rank_1<T>::value };
634 };
635
637 template <typename T>
638 class supports_rank<T,2>
639 {
640 public:
641 enum { value = supports_rank_2<T>::value };
642 };
643
645 template <typename T>
646 class supports_rank<T,3>
647 {
648 public:
649 enum { value = supports_rank_3<T>::value };
650 };
651
653 template <typename T>
654 class supports_rank<T,4>
655 {
656 public:
657 enum { value = supports_rank_4<T>::value };
658 };
659
661 template <typename T>
662 class supports_rank<T,5>
663 {
664 public:
665 enum { value = supports_rank_5<T>::value };
666 };
667
669 template <typename T>
670 class supports_rank<T,6>
671 {
672 public:
673 enum { value = supports_rank_6<T>::value };
674 };
675
677 template <typename T>
678 class supports_rank<T,7>
679 {
680 public:
681 enum { value = supports_rank_7<T>::value };
682 };
683
684
685
689 template<typename Scalar, int rank>
691
692 };
693
697 template<typename Scalar>
698 struct RankExpander<Scalar,0>
699 {
700 using value_type = Scalar;
701 };
702
706 template<typename Scalar>
707 struct RankExpander<Scalar,1>
708 {
709 using value_type = Scalar*;
710 };
711
715 template<typename Scalar>
716 struct RankExpander<Scalar,2>
717 {
718 using value_type = Scalar**;
719 };
720
724 template<typename Scalar>
725 struct RankExpander<Scalar,3>
726 {
727 using value_type = Scalar***;
728 };
729
733 template<typename Scalar>
734 struct RankExpander<Scalar,4>
735 {
736 using value_type = Scalar****;
737 };
738
742 template<typename Scalar>
743 struct RankExpander<Scalar,5>
744 {
745 using value_type = Scalar*****;
746 };
747
751 template<typename Scalar>
752 struct RankExpander<Scalar,6>
753 {
754 using value_type = Scalar******;
755 };
756
760 template<typename Scalar>
761 struct RankExpander<Scalar,7>
762 {
763 using value_type = Scalar*******;
764 };
765
766 // positive checks of supports_rank for Kokkos::DynRankView:
767 static_assert(supports_rank<Kokkos::DynRankView<double>, 1>::value, "rank 1 check of supports_rank for DynRankView");
768 static_assert(supports_rank<Kokkos::DynRankView<double>, 2>::value, "rank 2 check of supports_rank for DynRankView");
769 static_assert(supports_rank<Kokkos::DynRankView<double>, 3>::value, "rank 3 check of supports_rank for DynRankView");
770 static_assert(supports_rank<Kokkos::DynRankView<double>, 4>::value, "rank 4 check of supports_rank for DynRankView");
771 static_assert(supports_rank<Kokkos::DynRankView<double>, 5>::value, "rank 5 check of supports_rank for DynRankView");
772 static_assert(supports_rank<Kokkos::DynRankView<double>, 6>::value, "rank 6 check of supports_rank for DynRankView");
773 static_assert(supports_rank<Kokkos::DynRankView<double>, 7>::value, "rank 7 check of supports_rank for DynRankView");
774
775 // positive checks of supports_rank for Kokkos::View:
776 static_assert(supports_rank<Kokkos::View<double*>, 1>::value, "rank 1 check of supports_rank");
777 static_assert(supports_rank<Kokkos::View<double**>, 2>::value, "rank 2 check of supports_rank");
778 static_assert(supports_rank<Kokkos::View<double***>, 3>::value, "rank 3 check of supports_rank");
779 static_assert(supports_rank<Kokkos::View<double****>, 4>::value, "rank 4 check of supports_rank");
780 static_assert(supports_rank<Kokkos::View<double*****>, 5>::value, "rank 5 check of supports_rank");
781 static_assert(supports_rank<Kokkos::View<double******>, 6>::value, "rank 6 check of supports_rank");
782 static_assert(supports_rank<Kokkos::View<double*******>, 7>::value, "rank 7 check of supports_rank");
783
784 // negative checks of supports_rank for Kokkos::View:
785 static_assert(!supports_rank<Kokkos::View<double*>, 2>::value, "rank 1 check of supports_rank");
786 static_assert(!supports_rank<Kokkos::View<double*>, 3>::value, "rank 1 check of supports_rank");
787 static_assert(!supports_rank<Kokkos::View<double*>, 4>::value, "rank 1 check of supports_rank");
788 static_assert(!supports_rank<Kokkos::View<double*>, 5>::value, "rank 1 check of supports_rank");
789 static_assert(!supports_rank<Kokkos::View<double*>, 6>::value, "rank 1 check of supports_rank");
790 static_assert(!supports_rank<Kokkos::View<double*>, 7>::value, "rank 1 check of supports_rank");
791 static_assert(!supports_rank<Kokkos::View<double**>, 1>::value, "rank 2 check of supports_rank");
792 static_assert(!supports_rank<Kokkos::View<double**>, 3>::value, "rank 2 check of supports_rank");
793 static_assert(!supports_rank<Kokkos::View<double**>, 4>::value, "rank 2 check of supports_rank");
794 static_assert(!supports_rank<Kokkos::View<double**>, 5>::value, "rank 2 check of supports_rank");
795 static_assert(!supports_rank<Kokkos::View<double**>, 6>::value, "rank 2 check of supports_rank");
796 static_assert(!supports_rank<Kokkos::View<double**>, 7>::value, "rank 2 check of supports_rank");
797 static_assert(!supports_rank<Kokkos::View<double***>, 1>::value, "rank 3 check of supports_rank");
798 static_assert(!supports_rank<Kokkos::View<double***>, 2>::value, "rank 3 check of supports_rank");
799 static_assert(!supports_rank<Kokkos::View<double***>, 4>::value, "rank 3 check of supports_rank");
800 static_assert(!supports_rank<Kokkos::View<double***>, 5>::value, "rank 3 check of supports_rank");
801 static_assert(!supports_rank<Kokkos::View<double***>, 6>::value, "rank 3 check of supports_rank");
802 static_assert(!supports_rank<Kokkos::View<double***>, 7>::value, "rank 3 check of supports_rank");
803 static_assert(!supports_rank<Kokkos::View<double****>, 1>::value, "rank 4 check of supports_rank");
804 static_assert(!supports_rank<Kokkos::View<double****>, 2>::value, "rank 4 check of supports_rank");
805 static_assert(!supports_rank<Kokkos::View<double****>, 3>::value, "rank 4 check of supports_rank");
806 static_assert(!supports_rank<Kokkos::View<double****>, 5>::value, "rank 4 check of supports_rank");
807 static_assert(!supports_rank<Kokkos::View<double****>, 6>::value, "rank 4 check of supports_rank");
808 static_assert(!supports_rank<Kokkos::View<double****>, 7>::value, "rank 4 check of supports_rank");
809 static_assert(!supports_rank<Kokkos::View<double*****>, 1>::value, "rank 5 check of supports_rank");
810 static_assert(!supports_rank<Kokkos::View<double*****>, 2>::value, "rank 5 check of supports_rank");
811 static_assert(!supports_rank<Kokkos::View<double*****>, 3>::value, "rank 5 check of supports_rank");
812 static_assert(!supports_rank<Kokkos::View<double*****>, 4>::value, "rank 5 check of supports_rank");
813 static_assert(!supports_rank<Kokkos::View<double*****>, 6>::value, "rank 5 check of supports_rank");
814 static_assert(!supports_rank<Kokkos::View<double*****>, 7>::value, "rank 5 check of supports_rank");
815 static_assert(!supports_rank<Kokkos::View<double******>, 1>::value, "rank 6 check of supports_rank");
816 static_assert(!supports_rank<Kokkos::View<double******>, 2>::value, "rank 6 check of supports_rank");
817 static_assert(!supports_rank<Kokkos::View<double******>, 3>::value, "rank 6 check of supports_rank");
818 static_assert(!supports_rank<Kokkos::View<double******>, 4>::value, "rank 6 check of supports_rank");
819 static_assert(!supports_rank<Kokkos::View<double******>, 5>::value, "rank 6 check of supports_rank");
820 static_assert(!supports_rank<Kokkos::View<double******>, 7>::value, "rank 6 check of supports_rank");
821 static_assert(!supports_rank<Kokkos::View<double*******>, 1>::value, "rank 7 check of supports_rank");
822 static_assert(!supports_rank<Kokkos::View<double*******>, 2>::value, "rank 7 check of supports_rank");
823 static_assert(!supports_rank<Kokkos::View<double*******>, 3>::value, "rank 7 check of supports_rank");
824 static_assert(!supports_rank<Kokkos::View<double*******>, 4>::value, "rank 7 check of supports_rank");
825 static_assert(!supports_rank<Kokkos::View<double*******>, 5>::value, "rank 7 check of supports_rank");
826 static_assert(!supports_rank<Kokkos::View<double*******>, 6>::value, "rank 7 check of supports_rank");
827
831 template <typename T>
833 {
834 typedef char one;
835 struct two { char x[2]; };
836
837 template <typename C> static one test( decltype( std::declval<C>().rank() ) ) ;
838 template <typename C> static two test(...);
839
840 public:
841 enum { value = sizeof(test<T>(0)) == sizeof(char) };
842 };
843
844 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.");
845#if KOKKOS_VERSION < 40099
846 static_assert( has_rank_member<Kokkos::View<double*> >::value, "View has a member rank -- if this assert fails, something may be wrong with has_rank_member.");
847#endif
848
852 template<class Functor>
853 enable_if_t<has_rank_method<Functor>::value, unsigned>
854 KOKKOS_INLINE_FUNCTION
855 getFunctorRank(const Functor &functor)
856 {
857 return functor.rank();
858 }
859
863 template<class Functor>
864 enable_if_t<!has_rank_method<Functor>::value, unsigned>
865 KOKKOS_INLINE_FUNCTION
866 getFunctorRank(const Functor &functor)
867 {
868 return functor.rank;
869 }
870
874#if defined(HAVE_INTREPID2_SACADO) && !defined(SACADO_HAS_NEW_KOKKOS_VIEW_IMPL)
875 template <typename ValueType>
876 struct NaturalLayoutForType {
877 using layout =
878 typename std::conditional<(std::is_standard_layout<ValueType>::value && std::is_trivial<ValueType>::value),
879 Kokkos::LayoutLeft, // for POD types, use LayoutLeft
880 Kokkos::LayoutNatural<Kokkos::LayoutLeft> >::type; // For FAD types, use LayoutNatural
881 };
882#else
883 template <typename ValueType>
885 using layout = Kokkos::LayoutLeft;
886 };
887#endif
888
889 // define vector sizes for hierarchical parallelism
890 const int VECTOR_SIZE = 1;
891#if defined(SACADO_VIEW_CUDA_HIERARCHICAL_DFAD) && defined(INTREPID2_ENABLE_DEVICE)
892 const int FAD_VECTOR_SIZE = 32;
893#else
894 const int FAD_VECTOR_SIZE = 1;
895#endif
896
900 template<typename Scalar>
902 {
903 return (std::is_standard_layout<Scalar>::value && std::is_trivial<Scalar>::value) ? VECTOR_SIZE : FAD_VECTOR_SIZE;
904 }
905
911 template<typename ViewType>
912 KOKKOS_INLINE_FUNCTION
913 constexpr unsigned getScalarDimensionForView(const ViewType &view)
914 {
915 return (std::is_standard_layout<typename ViewType::value_type>::value && std::is_trivial<typename ViewType::value_type>::value) ? 0 : get_dimension_scalar(view);
916 }
917
919 template<typename Device>
921 template<typename T>
922 void operator()(T* ptr) {
923 Kokkos::parallel_for(Kokkos::RangePolicy<typename Device::execution_space>(0,1),
924 KOKKOS_LAMBDA (const int i) { ptr->~T(); });
925 typename Device::execution_space().fence();
926 Kokkos::kokkos_free<typename Device::memory_space>(ptr);
927 }
928 };
929
933 template<typename Device,typename Derived>
934 std::unique_ptr<Derived,DeviceDeleter<Device>>
935 copy_virtual_class_to_device(const Derived& host_source)
936 {
937 auto* p = static_cast<Derived*>(Kokkos::kokkos_malloc<typename Device::memory_space>(sizeof(Derived)));
938 Kokkos::parallel_for(Kokkos::RangePolicy<typename Device::execution_space>(0,1),
939 KOKKOS_LAMBDA (const int i) {new (p) Derived(host_source); });
940 typename Device::execution_space().fence();
941 return std::unique_ptr<Derived,DeviceDeleter<Device>>(p);
942 }
943
944
945
946
947
948
949
950
951} // end namespace Intrepid2
952
953#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...
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()
KOKKOS_INLINE_FUNCTION constexpr std::enable_if< std::is_standard_layout< T >::value &&std::is_trivial< T >::value, unsigned >::type dimension_scalar(const Kokkos::DynRankView< T, P... >)
specialization of functions for pod types, returning the scalar dimension (1 for pod types) of a view...
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...