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 "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 };
137 template<>
138 struct ScalarTraits<double> {
139 typedef double scalar_type;
140 };
144 template<>
145 struct ScalarTraits<int> {
146 typedef int scalar_type;
147 };
151 template<>
152 struct ScalarTraits<long int> {
153 typedef long int scalar_type;
154 };
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
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
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
482 template<class Functor, ordinal_type default_value>
483 constexpr
484 enable_if_t<has_rank_member<Functor>::value, ordinal_type>
486 {
487 return Functor::rank;
488 }
489
493 template<class Functor, ordinal_type default_value>
494 constexpr
495 enable_if_t<!has_rank_member<Functor>::value, ordinal_type>
497 {
498 return default_value;
499 }
500
504 template <typename T>
506 {
507 typedef char one;
508 struct two { char x[2]; };
509
510 template <typename C> static one test( typename std::remove_reference<decltype( std::declval<C>().operator()(0))>::type );
511 template <typename C> static two test(...);
512
513 public:
514 enum { value = sizeof(test<T>(0)) == sizeof(char) && (getFixedRank<T,1>() == 1) };
515 };
516
520 template <typename T>
522 {
523 typedef char one;
524 struct two { char x[2]; };
525
526 template <typename C> static one test( typename std::remove_reference<decltype( std::declval<C>().operator()(0,0))>::type ) ;
527 template <typename C> static two test(...);
528
529 public:
530 enum { value = sizeof(test<T>(0)) == sizeof(char) && (getFixedRank<T,2>() == 2) };
531 };
532
536 template <typename T>
538 {
539 typedef char one;
540 struct two { char x[2]; };
541
542 template <typename C> static one test( typename std::remove_reference<decltype( std::declval<C>().operator()(0,0,0))>::type ) ;
543 template <typename C> static two test(...);
544
545 public:
546 enum { value = (sizeof(test<T>(0)) == sizeof(char)) && (getFixedRank<T,3>() == 3) };
547 };
548
552 template <typename T>
554 {
555 typedef char one;
556 struct two { char x[2]; };
557
558 template <typename C> static one test( typename std::remove_reference<decltype( std::declval<C>().operator()(0,0,0,0))>::type ) ;
559 template <typename C> static two test(...);
560
561 public:
562 enum { value = sizeof(test<T>(0)) == sizeof(char) && (getFixedRank<T,4>() == 4) };
563 };
564
568 template <typename T>
570 {
571 typedef char one;
572 struct two { char x[2]; };
573
574 template <typename C> static one test( typename std::remove_reference<decltype( std::declval<C>().operator()(0,0,0,0,0))>::type ) ;
575 template <typename C> static two test(...);
576
577 public:
578 enum { value = sizeof(test<T>(0)) == sizeof(char) && (getFixedRank<T,5>() == 5) };
579 };
580
584 template <typename T>
586 {
587 typedef char one;
588 struct two { char x[2]; };
589
590 template <typename C> static one test( typename std::remove_reference<decltype( std::declval<C>().operator()(0,0,0,0,0,0))>::type ) ;
591 template <typename C> static two test(...);
592
593 public:
594 enum { value = sizeof(test<T>(0)) == sizeof(char) && (getFixedRank<T,6>() == 6) };
595 };
596
600 template <typename T>
602 {
603 typedef char one;
604 struct two { char x[2]; };
605
606 template <typename C> static one test( typename std::remove_reference<decltype( std::declval<C>().operator()(0,0,0,0,0,0,0))>::type ) ;
607 template <typename C> static two test(...);
608
609 public:
610 enum { value = sizeof(test<T>(0)) == sizeof(char) && (getFixedRank<T,7>() == 7) };
611 };
612
616 template <typename T, int rank>
618 {
619 public:
620 enum { value = false };
621 };
622
626 template <typename T>
627 class supports_rank<T,1>
628 {
629 public:
630 enum { value = supports_rank_1<T>::value };
631 };
632
634 template <typename T>
635 class supports_rank<T,2>
636 {
637 public:
638 enum { value = supports_rank_2<T>::value };
639 };
640
642 template <typename T>
643 class supports_rank<T,3>
644 {
645 public:
646 enum { value = supports_rank_3<T>::value };
647 };
648
650 template <typename T>
651 class supports_rank<T,4>
652 {
653 public:
654 enum { value = supports_rank_4<T>::value };
655 };
656
658 template <typename T>
659 class supports_rank<T,5>
660 {
661 public:
662 enum { value = supports_rank_5<T>::value };
663 };
664
666 template <typename T>
667 class supports_rank<T,6>
668 {
669 public:
670 enum { value = supports_rank_6<T>::value };
671 };
672
674 template <typename T>
675 class supports_rank<T,7>
676 {
677 public:
678 enum { value = supports_rank_7<T>::value };
679 };
680
681
682
686 template<typename Scalar, int rank>
688
689 };
690
694 template<typename Scalar>
695 struct RankExpander<Scalar,0>
696 {
697 using value_type = Scalar;
698 };
699
703 template<typename Scalar>
704 struct RankExpander<Scalar,1>
705 {
706 using value_type = Scalar*;
707 };
708
712 template<typename Scalar>
713 struct RankExpander<Scalar,2>
714 {
715 using value_type = Scalar**;
716 };
717
721 template<typename Scalar>
722 struct RankExpander<Scalar,3>
723 {
724 using value_type = Scalar***;
725 };
726
730 template<typename Scalar>
731 struct RankExpander<Scalar,4>
732 {
733 using value_type = Scalar****;
734 };
735
739 template<typename Scalar>
740 struct RankExpander<Scalar,5>
741 {
742 using value_type = Scalar*****;
743 };
744
748 template<typename Scalar>
749 struct RankExpander<Scalar,6>
750 {
751 using value_type = Scalar******;
752 };
753
757 template<typename Scalar>
758 struct RankExpander<Scalar,7>
759 {
760 using value_type = Scalar*******;
761 };
762
763 // positive checks of supports_rank for Kokkos::DynRankView:
764 static_assert(supports_rank<Kokkos::DynRankView<double>, 1>::value, "rank 1 check of supports_rank for DynRankView");
765 static_assert(supports_rank<Kokkos::DynRankView<double>, 2>::value, "rank 2 check of supports_rank for DynRankView");
766 static_assert(supports_rank<Kokkos::DynRankView<double>, 3>::value, "rank 3 check of supports_rank for DynRankView");
767 static_assert(supports_rank<Kokkos::DynRankView<double>, 4>::value, "rank 4 check of supports_rank for DynRankView");
768 static_assert(supports_rank<Kokkos::DynRankView<double>, 5>::value, "rank 5 check of supports_rank for DynRankView");
769 static_assert(supports_rank<Kokkos::DynRankView<double>, 6>::value, "rank 6 check of supports_rank for DynRankView");
770 static_assert(supports_rank<Kokkos::DynRankView<double>, 7>::value, "rank 7 check of supports_rank for DynRankView");
771
772 // positive checks of supports_rank for Kokkos::View:
773 static_assert(supports_rank<Kokkos::View<double*>, 1>::value, "rank 1 check of supports_rank");
774 static_assert(supports_rank<Kokkos::View<double**>, 2>::value, "rank 2 check of supports_rank");
775 static_assert(supports_rank<Kokkos::View<double***>, 3>::value, "rank 3 check of supports_rank");
776 static_assert(supports_rank<Kokkos::View<double****>, 4>::value, "rank 4 check of supports_rank");
777 static_assert(supports_rank<Kokkos::View<double*****>, 5>::value, "rank 5 check of supports_rank");
778 static_assert(supports_rank<Kokkos::View<double******>, 6>::value, "rank 6 check of supports_rank");
779 static_assert(supports_rank<Kokkos::View<double*******>, 7>::value, "rank 7 check of supports_rank");
780
781 // negative checks of supports_rank for Kokkos::View:
782 static_assert(!supports_rank<Kokkos::View<double*>, 2>::value, "rank 1 check of supports_rank");
783 static_assert(!supports_rank<Kokkos::View<double*>, 3>::value, "rank 1 check of supports_rank");
784 static_assert(!supports_rank<Kokkos::View<double*>, 4>::value, "rank 1 check of supports_rank");
785 static_assert(!supports_rank<Kokkos::View<double*>, 5>::value, "rank 1 check of supports_rank");
786 static_assert(!supports_rank<Kokkos::View<double*>, 6>::value, "rank 1 check of supports_rank");
787 static_assert(!supports_rank<Kokkos::View<double*>, 7>::value, "rank 1 check of supports_rank");
788 static_assert(!supports_rank<Kokkos::View<double**>, 1>::value, "rank 2 check of supports_rank");
789 static_assert(!supports_rank<Kokkos::View<double**>, 3>::value, "rank 2 check of supports_rank");
790 static_assert(!supports_rank<Kokkos::View<double**>, 4>::value, "rank 2 check of supports_rank");
791 static_assert(!supports_rank<Kokkos::View<double**>, 5>::value, "rank 2 check of supports_rank");
792 static_assert(!supports_rank<Kokkos::View<double**>, 6>::value, "rank 2 check of supports_rank");
793 static_assert(!supports_rank<Kokkos::View<double**>, 7>::value, "rank 2 check of supports_rank");
794 static_assert(!supports_rank<Kokkos::View<double***>, 1>::value, "rank 3 check of supports_rank");
795 static_assert(!supports_rank<Kokkos::View<double***>, 2>::value, "rank 3 check of supports_rank");
796 static_assert(!supports_rank<Kokkos::View<double***>, 4>::value, "rank 3 check of supports_rank");
797 static_assert(!supports_rank<Kokkos::View<double***>, 5>::value, "rank 3 check of supports_rank");
798 static_assert(!supports_rank<Kokkos::View<double***>, 6>::value, "rank 3 check of supports_rank");
799 static_assert(!supports_rank<Kokkos::View<double***>, 7>::value, "rank 3 check of supports_rank");
800 static_assert(!supports_rank<Kokkos::View<double****>, 1>::value, "rank 4 check of supports_rank");
801 static_assert(!supports_rank<Kokkos::View<double****>, 2>::value, "rank 4 check of supports_rank");
802 static_assert(!supports_rank<Kokkos::View<double****>, 3>::value, "rank 4 check of supports_rank");
803 static_assert(!supports_rank<Kokkos::View<double****>, 5>::value, "rank 4 check of supports_rank");
804 static_assert(!supports_rank<Kokkos::View<double****>, 6>::value, "rank 4 check of supports_rank");
805 static_assert(!supports_rank<Kokkos::View<double****>, 7>::value, "rank 4 check of supports_rank");
806 static_assert(!supports_rank<Kokkos::View<double*****>, 1>::value, "rank 5 check of supports_rank");
807 static_assert(!supports_rank<Kokkos::View<double*****>, 2>::value, "rank 5 check of supports_rank");
808 static_assert(!supports_rank<Kokkos::View<double*****>, 3>::value, "rank 5 check of supports_rank");
809 static_assert(!supports_rank<Kokkos::View<double*****>, 4>::value, "rank 5 check of supports_rank");
810 static_assert(!supports_rank<Kokkos::View<double*****>, 6>::value, "rank 5 check of supports_rank");
811 static_assert(!supports_rank<Kokkos::View<double*****>, 7>::value, "rank 5 check of supports_rank");
812 static_assert(!supports_rank<Kokkos::View<double******>, 1>::value, "rank 6 check of supports_rank");
813 static_assert(!supports_rank<Kokkos::View<double******>, 2>::value, "rank 6 check of supports_rank");
814 static_assert(!supports_rank<Kokkos::View<double******>, 3>::value, "rank 6 check of supports_rank");
815 static_assert(!supports_rank<Kokkos::View<double******>, 4>::value, "rank 6 check of supports_rank");
816 static_assert(!supports_rank<Kokkos::View<double******>, 5>::value, "rank 6 check of supports_rank");
817 static_assert(!supports_rank<Kokkos::View<double******>, 7>::value, "rank 6 check of supports_rank");
818 static_assert(!supports_rank<Kokkos::View<double*******>, 1>::value, "rank 7 check of supports_rank");
819 static_assert(!supports_rank<Kokkos::View<double*******>, 2>::value, "rank 7 check of supports_rank");
820 static_assert(!supports_rank<Kokkos::View<double*******>, 3>::value, "rank 7 check of supports_rank");
821 static_assert(!supports_rank<Kokkos::View<double*******>, 4>::value, "rank 7 check of supports_rank");
822 static_assert(!supports_rank<Kokkos::View<double*******>, 5>::value, "rank 7 check of supports_rank");
823 static_assert(!supports_rank<Kokkos::View<double*******>, 6>::value, "rank 7 check of supports_rank");
824
828 template <typename T>
830 {
831 typedef char one;
832 struct two { char x[2]; };
833
834 template <typename C> static one test( decltype( std::declval<C>().rank() ) ) ;
835 template <typename C> static two test(...);
836
837 public:
838 enum { value = sizeof(test<T>(0)) == sizeof(char) };
839 };
840
841 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.");
842
846 template<class Functor>
847 enable_if_t<has_rank_method<Functor>::value, unsigned>
848 KOKKOS_INLINE_FUNCTION
849 getFunctorRank(const Functor &functor)
850 {
851 return functor.rank();
852 }
853
857 template<class Functor>
858 enable_if_t<!has_rank_method<Functor>::value, unsigned>
859 KOKKOS_INLINE_FUNCTION
860 getFunctorRank(const Functor &functor)
861 {
862 return functor.rank;
863 }
864
868#if defined(HAVE_INTREPID2_SACADO) && !defined(SACADO_HAS_NEW_KOKKOS_VIEW_IMPL)
869 template <typename ValueType>
870 struct NaturalLayoutForType {
871 using layout =
872 typename std::conditional<(std::is_standard_layout<ValueType>::value && std::is_trivial<ValueType>::value),
873 Kokkos::LayoutLeft, // for POD types, use LayoutLeft
874 Kokkos::LayoutNatural<Kokkos::LayoutLeft> >::type; // For FAD types, use LayoutNatural
875 };
876#else
877 template <typename ValueType>
879 using layout = Kokkos::LayoutLeft;
880 };
881#endif
882
883 // define vector sizes for hierarchical parallelism
884 const int VECTOR_SIZE = 1;
885#if defined(SACADO_VIEW_CUDA_HIERARCHICAL_DFAD) && defined(INTREPID2_ENABLE_DEVICE)
886 const int FAD_VECTOR_SIZE = 32;
887#else
888 const int FAD_VECTOR_SIZE = 1;
889#endif
890
894 template<typename Scalar>
896 {
897 return (std::is_standard_layout<Scalar>::value && std::is_trivial<Scalar>::value) ? VECTOR_SIZE : FAD_VECTOR_SIZE;
898 }
899
905 template<typename ViewType>
906 KOKKOS_INLINE_FUNCTION
907 constexpr unsigned getScalarDimensionForView(const ViewType &view)
908 {
909 return (std::is_standard_layout<typename ViewType::value_type>::value && std::is_trivial<typename ViewType::value_type>::value) ? 0 : get_dimension_scalar(view);
910 }
911
913 template<typename Device>
915 template<typename T>
916 void operator()(T* ptr) {
917 Kokkos::parallel_for(Kokkos::RangePolicy<typename Device::execution_space>(0,1),
918 KOKKOS_LAMBDA (const int i) { ptr->~T(); });
919 typename Device::execution_space().fence();
920 Kokkos::kokkos_free<typename Device::memory_space>(ptr);
921 }
922 };
923
927 template<typename Device,typename Derived>
928 std::unique_ptr<Derived,DeviceDeleter<Device>>
929 copy_virtual_class_to_device(const Derived& host_source)
930 {
931 auto* p = static_cast<Derived*>(Kokkos::kokkos_malloc<typename Device::memory_space>(sizeof(Derived)));
932 Kokkos::parallel_for(Kokkos::RangePolicy<typename Device::execution_space>(0,1),
933 KOKKOS_LAMBDA (const int i) {new (p) Derived(host_source); });
934 typename Device::execution_space().fence();
935 return std::unique_ptr<Derived,DeviceDeleter<Device>>(p);
936 }
937
938
939
940
941
942
943
944
945} // end namespace Intrepid2
946
947#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...