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 return Sacado::dimension_scalar(view);
328 }
329
330 template<typename T, typename ...P>
331 KOKKOS_FORCEINLINE_FUNCTION
332 static ordinal_type get_dimension_scalar(const Kokkos::View<T, P...> &view) {
333 return Sacado::dimension_scalar(view);
334 }
335
337 // such that the ouptut view type has the default layout when the input has LayoutStride
338 template <typename InputView>
340 {
341 using value = typename InputView::non_const_value_type;
342 using layout = typename DeduceLayout<InputView>::result_layout;
343 using device = typename InputView::device_type;
344 using type = Kokkos::DynRankView<value, layout, device>;
345 };
346
347
348 template <class DataType, class... Properties>
349 KOKKOS_INLINE_FUNCTION auto
350 as_scalar_1d_view(const Kokkos::View<DataType, Properties...> &view) {
351 using view_t = Kokkos::View<DataType, Properties...>;
352#ifdef HAVE_INTREPID2_SACADO
353 if constexpr (Sacado::is_view_fad<view_t>::value) {
354 return Sacado::as_scalar_view(view);
355 } else
356#endif
357 return Kokkos::View<typename view_t::value_type*, Properties...>(view.data(), view.mapping().required_span_size());
358 }
359
360 template <class... Args>
361 KOKKOS_INLINE_FUNCTION auto
362 as_scalar_1d_view(const Kokkos::DynRankView<Args...> &dyn) {
363 return as_scalar_1d_view(dyn.ConstDownCast());
364 }
365
366
367 namespace Impl
368 {
372 template <class... ViewPack>
374 {
380 template <class OutViewType, class CtorProp, class... Dims>
381 static OutViewType
382 create_view(const ViewPack &...views,
383 const CtorProp &prop,
384 const Dims... dims)
385 {
386 #ifdef HAVE_INTREPID2_SACADO
387 using view_factory = Sacado::ViewFactory<ViewPack...>;
388 return view_factory::template create_view<OutViewType>(views..., prop, dims...);
389 #else
390 ((void)views, ...);
391 return OutViewType(prop, dims...);
392 #endif
393 }
394 };
395
402 template <typename OutViewType, typename InViewType, typename CtorProp, typename... Dims>
403 OutViewType
404 createMatchingView(const InViewType &view,
405 const CtorProp &prop,
406 const Dims... dims)
407 {
409 return cvf::template create_view<OutViewType>(view, prop, dims...);
410 }
411
417 template <typename InViewType, typename CtorProp, typename... Dims>
418 typename DeduceDynRankView<InViewType>::type
419 createMatchingDynRankView(const InViewType &view,
420 const CtorProp &prop,
421 const Dims... dims)
422 {
423 using OutViewType = typename DeduceDynRankView<InViewType>::type;
424 return createMatchingView<OutViewType>(view, prop, dims...);
425 }
426
433 template <typename OutViewType, typename InViewType, typename CtorProp, typename... Dims>
434 KOKKOS_INLINE_FUNCTION
435 typename std::enable_if<
436 std::is_pointer_v<CtorProp> && !std::is_convertible_v<CtorProp, const char*>,
437 OutViewType>::type
438 createMatchingUnmanagedView(const InViewType &view, const CtorProp &data, const Dims... dims)
439 {
440 #ifdef HAVE_INTREPID2_SACADO
441 if constexpr (Sacado::is_view_fad<InViewType>::value)
442 {
443 const int derivative_dimension = get_dimension_scalar(view);
444 return OutViewType(data, dims..., derivative_dimension);
445 }
446 else
447 return OutViewType(data, dims...);
448 #else
449 (void)view;
450 return OutViewType(data, dims...);
451 #endif
452 }
453
459 template <typename InViewType, typename CtorProp, typename ... Dims>
460 KOKKOS_INLINE_FUNCTION
461 typename std::enable_if<
462 std::is_pointer_v<CtorProp> && !std::is_convertible_v<CtorProp, const char*>,
463 typename DeduceDynRankView<InViewType>::type>::type
464 createMatchingUnmanagedDynRankView(const InViewType& view, const CtorProp& data, const Dims... dims){
465 using OutViewType = typename DeduceDynRankView<InViewType>::type;
466 return createMatchingUnmanagedView<OutViewType>(view, data, dims...);
467 }
468
469 } //Impl namespace
470
471
480 template<class ViewType, class ... DimArgs>
481 inline
482 Kokkos::DynRankView<typename ViewType::value_type, typename DeduceLayout< ViewType >::result_layout, typename ViewType::device_type >
483 getMatchingViewWithLabel(const ViewType &view, const std::string &label, DimArgs... dims)
484 {
485 return Impl::createMatchingDynRankView(view, label, dims...);
486 }
487
488 using std::enable_if_t;
489
493 template <typename T, typename = void>
494 struct has_rank_member : std::false_type{};
495
499 template <typename T>
500 struct has_rank_member<T, decltype((void)T::rank, void())> : std::true_type {};
501
502 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.");
503
507 template<class Functor, ordinal_type default_value>
508 constexpr
509 enable_if_t<has_rank_member<Functor>::value, ordinal_type>
511 {
512 return Functor::rank;
513 }
514
518 template<class Functor, ordinal_type default_value>
519 constexpr
520 enable_if_t<!has_rank_member<Functor>::value, ordinal_type>
522 {
523 return default_value;
524 }
525
529 template <typename T>
531 {
532 typedef char one;
533 struct two { char x[2]; };
534
535 template <typename C> static one test( typename std::remove_reference<decltype( std::declval<C>().operator()(0))>::type );
536 template <typename C> static two test(...);
537
538 public:
539 enum { value = sizeof(test<T>(0)) == sizeof(char) && (getFixedRank<T,1>() == 1) };
540 };
541
545 template <typename T>
547 {
548 typedef char one;
549 struct two { char x[2]; };
550
551 template <typename C> static one test( typename std::remove_reference<decltype( std::declval<C>().operator()(0,0))>::type ) ;
552 template <typename C> static two test(...);
553
554 public:
555 enum { value = sizeof(test<T>(0)) == sizeof(char) && (getFixedRank<T,2>() == 2) };
556 };
557
561 template <typename T>
563 {
564 typedef char one;
565 struct two { char x[2]; };
566
567 template <typename C> static one test( typename std::remove_reference<decltype( std::declval<C>().operator()(0,0,0))>::type ) ;
568 template <typename C> static two test(...);
569
570 public:
571 enum { value = (sizeof(test<T>(0)) == sizeof(char)) && (getFixedRank<T,3>() == 3) };
572 };
573
577 template <typename T>
579 {
580 typedef char one;
581 struct two { char x[2]; };
582
583 template <typename C> static one test( typename std::remove_reference<decltype( std::declval<C>().operator()(0,0,0,0))>::type ) ;
584 template <typename C> static two test(...);
585
586 public:
587 enum { value = sizeof(test<T>(0)) == sizeof(char) && (getFixedRank<T,4>() == 4) };
588 };
589
593 template <typename T>
595 {
596 typedef char one;
597 struct two { char x[2]; };
598
599 template <typename C> static one test( typename std::remove_reference<decltype( std::declval<C>().operator()(0,0,0,0,0))>::type ) ;
600 template <typename C> static two test(...);
601
602 public:
603 enum { value = sizeof(test<T>(0)) == sizeof(char) && (getFixedRank<T,5>() == 5) };
604 };
605
609 template <typename T>
611 {
612 typedef char one;
613 struct two { char x[2]; };
614
615 template <typename C> static one test( typename std::remove_reference<decltype( std::declval<C>().operator()(0,0,0,0,0,0))>::type ) ;
616 template <typename C> static two test(...);
617
618 public:
619 enum { value = sizeof(test<T>(0)) == sizeof(char) && (getFixedRank<T,6>() == 6) };
620 };
621
625 template <typename T>
627 {
628 typedef char one;
629 struct two { char x[2]; };
630
631 template <typename C> static one test( typename std::remove_reference<decltype( std::declval<C>().operator()(0,0,0,0,0,0,0))>::type ) ;
632 template <typename C> static two test(...);
633
634 public:
635 enum { value = sizeof(test<T>(0)) == sizeof(char) && (getFixedRank<T,7>() == 7) };
636 };
637
641 template <typename T, int rank>
643 {
644 public:
645 enum { value = false };
646 };
647
651 template <typename T>
652 class supports_rank<T,1>
653 {
654 public:
655 enum { value = supports_rank_1<T>::value };
656 };
657
659 template <typename T>
660 class supports_rank<T,2>
661 {
662 public:
663 enum { value = supports_rank_2<T>::value };
664 };
665
667 template <typename T>
668 class supports_rank<T,3>
669 {
670 public:
671 enum { value = supports_rank_3<T>::value };
672 };
673
675 template <typename T>
676 class supports_rank<T,4>
677 {
678 public:
679 enum { value = supports_rank_4<T>::value };
680 };
681
683 template <typename T>
684 class supports_rank<T,5>
685 {
686 public:
687 enum { value = supports_rank_5<T>::value };
688 };
689
691 template <typename T>
692 class supports_rank<T,6>
693 {
694 public:
695 enum { value = supports_rank_6<T>::value };
696 };
697
699 template <typename T>
700 class supports_rank<T,7>
701 {
702 public:
703 enum { value = supports_rank_7<T>::value };
704 };
705
706
707
711 template<typename Scalar, int rank>
713
714 };
715
719 template<typename Scalar>
720 struct RankExpander<Scalar,0>
721 {
722 using value_type = Scalar;
723 };
724
728 template<typename Scalar>
729 struct RankExpander<Scalar,1>
730 {
731 using value_type = Scalar*;
732 };
733
737 template<typename Scalar>
738 struct RankExpander<Scalar,2>
739 {
740 using value_type = Scalar**;
741 };
742
746 template<typename Scalar>
747 struct RankExpander<Scalar,3>
748 {
749 using value_type = Scalar***;
750 };
751
755 template<typename Scalar>
756 struct RankExpander<Scalar,4>
757 {
758 using value_type = Scalar****;
759 };
760
764 template<typename Scalar>
765 struct RankExpander<Scalar,5>
766 {
767 using value_type = Scalar*****;
768 };
769
773 template<typename Scalar>
774 struct RankExpander<Scalar,6>
775 {
776 using value_type = Scalar******;
777 };
778
782 template<typename Scalar>
783 struct RankExpander<Scalar,7>
784 {
785 using value_type = Scalar*******;
786 };
787
788 // positive checks of supports_rank for Kokkos::DynRankView:
789 static_assert(supports_rank<Kokkos::DynRankView<double>, 1>::value, "rank 1 check of supports_rank for DynRankView");
790 static_assert(supports_rank<Kokkos::DynRankView<double>, 2>::value, "rank 2 check of supports_rank for DynRankView");
791 static_assert(supports_rank<Kokkos::DynRankView<double>, 3>::value, "rank 3 check of supports_rank for DynRankView");
792 static_assert(supports_rank<Kokkos::DynRankView<double>, 4>::value, "rank 4 check of supports_rank for DynRankView");
793 static_assert(supports_rank<Kokkos::DynRankView<double>, 5>::value, "rank 5 check of supports_rank for DynRankView");
794 static_assert(supports_rank<Kokkos::DynRankView<double>, 6>::value, "rank 6 check of supports_rank for DynRankView");
795 static_assert(supports_rank<Kokkos::DynRankView<double>, 7>::value, "rank 7 check of supports_rank for DynRankView");
796
797 // positive checks of supports_rank for Kokkos::View:
798 static_assert(supports_rank<Kokkos::View<double*>, 1>::value, "rank 1 check of supports_rank");
799 static_assert(supports_rank<Kokkos::View<double**>, 2>::value, "rank 2 check of supports_rank");
800 static_assert(supports_rank<Kokkos::View<double***>, 3>::value, "rank 3 check of supports_rank");
801 static_assert(supports_rank<Kokkos::View<double****>, 4>::value, "rank 4 check of supports_rank");
802 static_assert(supports_rank<Kokkos::View<double*****>, 5>::value, "rank 5 check of supports_rank");
803 static_assert(supports_rank<Kokkos::View<double******>, 6>::value, "rank 6 check of supports_rank");
804 static_assert(supports_rank<Kokkos::View<double*******>, 7>::value, "rank 7 check of supports_rank");
805
806 // negative checks of supports_rank for Kokkos::View:
807 static_assert(!supports_rank<Kokkos::View<double*>, 2>::value, "rank 1 check of supports_rank");
808 static_assert(!supports_rank<Kokkos::View<double*>, 3>::value, "rank 1 check of supports_rank");
809 static_assert(!supports_rank<Kokkos::View<double*>, 4>::value, "rank 1 check of supports_rank");
810 static_assert(!supports_rank<Kokkos::View<double*>, 5>::value, "rank 1 check of supports_rank");
811 static_assert(!supports_rank<Kokkos::View<double*>, 6>::value, "rank 1 check of supports_rank");
812 static_assert(!supports_rank<Kokkos::View<double*>, 7>::value, "rank 1 check of supports_rank");
813 static_assert(!supports_rank<Kokkos::View<double**>, 1>::value, "rank 2 check of supports_rank");
814 static_assert(!supports_rank<Kokkos::View<double**>, 3>::value, "rank 2 check of supports_rank");
815 static_assert(!supports_rank<Kokkos::View<double**>, 4>::value, "rank 2 check of supports_rank");
816 static_assert(!supports_rank<Kokkos::View<double**>, 5>::value, "rank 2 check of supports_rank");
817 static_assert(!supports_rank<Kokkos::View<double**>, 6>::value, "rank 2 check of supports_rank");
818 static_assert(!supports_rank<Kokkos::View<double**>, 7>::value, "rank 2 check of supports_rank");
819 static_assert(!supports_rank<Kokkos::View<double***>, 1>::value, "rank 3 check of supports_rank");
820 static_assert(!supports_rank<Kokkos::View<double***>, 2>::value, "rank 3 check of supports_rank");
821 static_assert(!supports_rank<Kokkos::View<double***>, 4>::value, "rank 3 check of supports_rank");
822 static_assert(!supports_rank<Kokkos::View<double***>, 5>::value, "rank 3 check of supports_rank");
823 static_assert(!supports_rank<Kokkos::View<double***>, 6>::value, "rank 3 check of supports_rank");
824 static_assert(!supports_rank<Kokkos::View<double***>, 7>::value, "rank 3 check of supports_rank");
825 static_assert(!supports_rank<Kokkos::View<double****>, 1>::value, "rank 4 check of supports_rank");
826 static_assert(!supports_rank<Kokkos::View<double****>, 2>::value, "rank 4 check of supports_rank");
827 static_assert(!supports_rank<Kokkos::View<double****>, 3>::value, "rank 4 check of supports_rank");
828 static_assert(!supports_rank<Kokkos::View<double****>, 5>::value, "rank 4 check of supports_rank");
829 static_assert(!supports_rank<Kokkos::View<double****>, 6>::value, "rank 4 check of supports_rank");
830 static_assert(!supports_rank<Kokkos::View<double****>, 7>::value, "rank 4 check of supports_rank");
831 static_assert(!supports_rank<Kokkos::View<double*****>, 1>::value, "rank 5 check of supports_rank");
832 static_assert(!supports_rank<Kokkos::View<double*****>, 2>::value, "rank 5 check of supports_rank");
833 static_assert(!supports_rank<Kokkos::View<double*****>, 3>::value, "rank 5 check of supports_rank");
834 static_assert(!supports_rank<Kokkos::View<double*****>, 4>::value, "rank 5 check of supports_rank");
835 static_assert(!supports_rank<Kokkos::View<double*****>, 6>::value, "rank 5 check of supports_rank");
836 static_assert(!supports_rank<Kokkos::View<double*****>, 7>::value, "rank 5 check of supports_rank");
837 static_assert(!supports_rank<Kokkos::View<double******>, 1>::value, "rank 6 check of supports_rank");
838 static_assert(!supports_rank<Kokkos::View<double******>, 2>::value, "rank 6 check of supports_rank");
839 static_assert(!supports_rank<Kokkos::View<double******>, 3>::value, "rank 6 check of supports_rank");
840 static_assert(!supports_rank<Kokkos::View<double******>, 4>::value, "rank 6 check of supports_rank");
841 static_assert(!supports_rank<Kokkos::View<double******>, 5>::value, "rank 6 check of supports_rank");
842 static_assert(!supports_rank<Kokkos::View<double******>, 7>::value, "rank 6 check of supports_rank");
843 static_assert(!supports_rank<Kokkos::View<double*******>, 1>::value, "rank 7 check of supports_rank");
844 static_assert(!supports_rank<Kokkos::View<double*******>, 2>::value, "rank 7 check of supports_rank");
845 static_assert(!supports_rank<Kokkos::View<double*******>, 3>::value, "rank 7 check of supports_rank");
846 static_assert(!supports_rank<Kokkos::View<double*******>, 4>::value, "rank 7 check of supports_rank");
847 static_assert(!supports_rank<Kokkos::View<double*******>, 5>::value, "rank 7 check of supports_rank");
848 static_assert(!supports_rank<Kokkos::View<double*******>, 6>::value, "rank 7 check of supports_rank");
849
853 template <typename T>
855 {
856 typedef char one;
857 struct two { char x[2]; };
858
859 template <typename C> static one test( decltype( std::declval<C>().rank() ) ) ;
860 template <typename C> static two test(...);
861
862 public:
863 enum { value = sizeof(test<T>(0)) == sizeof(char) };
864 };
865
866 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.");
867
871 template<class Functor>
872 enable_if_t<has_rank_method<Functor>::value, unsigned>
873 KOKKOS_INLINE_FUNCTION
874 getFunctorRank(const Functor &functor)
875 {
876 return functor.rank();
877 }
878
882 template<class Functor>
883 enable_if_t<!has_rank_method<Functor>::value, unsigned>
884 KOKKOS_INLINE_FUNCTION
885 getFunctorRank(const Functor &functor)
886 {
887 return functor.rank;
888 }
889
893 template <typename ValueType>
895 using layout = Kokkos::LayoutLeft;
896 };
897
898 // define vector sizes for hierarchical parallelism
899 const int VECTOR_SIZE = 1;
900#if defined(SACADO_VIEW_CUDA_HIERARCHICAL_DFAD) && defined(INTREPID2_ENABLE_DEVICE)
901 const int FAD_VECTOR_SIZE = 32;
902#else
903 const int FAD_VECTOR_SIZE = 1;
904#endif
905
909 template<typename Scalar>
911 {
912 return (std::is_standard_layout<Scalar>::value && std::is_trivial<Scalar>::value) ? VECTOR_SIZE : FAD_VECTOR_SIZE;
913 }
914
920 template<typename ViewType>
921 KOKKOS_INLINE_FUNCTION
922 constexpr unsigned getScalarDimensionForView(const ViewType &view)
923 {
924 return (std::is_standard_layout<typename ViewType::value_type>::value && std::is_trivial<typename ViewType::value_type>::value) ? 0 : get_dimension_scalar(view);
925 }
926
928 template<typename Device>
930 template<typename T>
931 void operator()(T* ptr) {
932 Kokkos::parallel_for(Kokkos::RangePolicy<typename Device::execution_space>(0,1),
933 KOKKOS_LAMBDA (const int i) { ptr->~T(); });
934 typename Device::execution_space().fence();
935 Kokkos::kokkos_free<typename Device::memory_space>(ptr);
936 }
937 };
938
942 template<typename Device,typename Derived>
943 std::unique_ptr<Derived,DeviceDeleter<Device>>
944 copy_virtual_class_to_device(const Derived& host_source)
945 {
946 auto* p = static_cast<Derived*>(Kokkos::kokkos_malloc<typename Device::memory_space>(sizeof(Derived)));
947 Kokkos::parallel_for(Kokkos::RangePolicy<typename Device::execution_space>(0,1),
948 KOKKOS_LAMBDA (const int i) {new (p) Derived(host_source); });
949 typename Device::execution_space().fence();
950 return std::unique_ptr<Derived,DeviceDeleter<Device>>(p);
951 }
952
953
954
955
956
957
958
959
960} // end namespace Intrepid2
961
962#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...