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 template <typename... Args>
252 requires (std::is_same_v<T, Args> && ...)
253 KOKKOS_FORCEINLINE_FUNCTION
254 static T max(const T a, const T b, Args... args) {
255 // Recursively find the max of everything after the first argument
256 T tmp_max = max(b, args...);
257 return (a > tmp_max) ? a : tmp_max;
258 }
259
260 KOKKOS_FORCEINLINE_FUNCTION
261 static T abs(const T a) {
262 return (a > 0 ? a : T(-a));
263 }
264
265 };
266
267 template<typename T>
268 KOKKOS_FORCEINLINE_FUNCTION
269 static T min(const T &a, const T &b) {
270 return (a < b ? a : b);
271 }
272
273 template<typename T>
274 KOKKOS_FORCEINLINE_FUNCTION
275 static T max(const T &a, const T &b) {
276 return (a > b ? a : b);
277 }
278
279 template<typename T>
280 KOKKOS_FORCEINLINE_FUNCTION
281 static T abs(const T &a) {
282 return (a > 0 ? a : T(-a));
283 }
284
292 template<typename T>
293 KOKKOS_FORCEINLINE_FUNCTION
294 constexpr typename
295 std::enable_if< !(std::is_standard_layout<T>::value && std::is_trivial<T>::value), typename ScalarTraits<T>::scalar_type >::type
296 get_scalar_value(const T& obj) {return obj.val();}
297
298 template<typename T>
299 KOKKOS_FORCEINLINE_FUNCTION
300 constexpr typename
301 std::enable_if< std::is_standard_layout<T>::value && std::is_trivial<T>::value, typename ScalarTraits<T>::scalar_type >::type
302 get_scalar_value(const T& obj){return obj;}
303
304
311 template<typename T, typename ...P>
312 KOKKOS_INLINE_FUNCTION
313 constexpr typename
314 std::enable_if< std::is_standard_layout<T>::value && std::is_trivial<T>::value, unsigned >::type
315 dimension_scalar(const Kokkos::DynRankView<T, P...> /* view */) {return 1;}
316
317 template<typename T, typename ...P>
318 KOKKOS_INLINE_FUNCTION
319 constexpr typename
320 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
321 dimension_scalar(const Kokkos::View<T, P...> /*view*/) {return 1;}
322
323 template<typename T, typename ...P>
324 KOKKOS_FORCEINLINE_FUNCTION
325 static ordinal_type get_dimension_scalar(const Kokkos::DynRankView<T, P...> &view) {
326 return dimension_scalar(view);
327 }
328
329 template<typename T, typename ...P>
330 KOKKOS_FORCEINLINE_FUNCTION
331 static ordinal_type get_dimension_scalar(const Kokkos::View<T, P...> &view) {
332 return dimension_scalar(view);
333 }
334
336 // such that the ouptut view type has the default layout when the input has LayoutStride
337 template <typename InputView>
339 {
340 using value = typename InputView::non_const_value_type;
341 using layout = typename DeduceLayout<InputView>::result_layout;
342 using device = typename InputView::device_type;
343 using type = Kokkos::DynRankView<value, layout, device>;
344 };
345
346
347 template <class DataType, class... Properties>
348 KOKKOS_INLINE_FUNCTION auto
349 as_scalar_1d_view(const Kokkos::View<DataType, Properties...> &view) {
350 using view_t = Kokkos::View<DataType, Properties...>;
351#ifdef HAVE_INTREPID2_SACADO
352 if constexpr (Kokkos::is_view_fad<view_t>::value) {
353 return Sacado::as_scalar_view(view);
354 } else
355#endif
356 return Kokkos::View<typename view_t::value_type*, Properties...>(view.data(), view.mapping().required_span_size());
357 }
358
359 template <class... Args>
360 KOKKOS_INLINE_FUNCTION auto
361 as_scalar_1d_view(const Kokkos::DynRankView<Args...> &dyn) {
362 return as_scalar_1d_view(dyn.ConstDownCast());
363 }
364
365
366 namespace Impl
367 {
371 template <class... ViewPack>
373 {
379 template <class OutViewType, class CtorProp, class... Dims>
380 static OutViewType
381 create_view(const ViewPack &...views,
382 const CtorProp &prop,
383 const Dims... dims)
384 {
385 #ifdef HAVE_INTREPID2_SACADO
386 using view_factory = Kokkos::ViewFactory<ViewPack...>;
387 return view_factory::template create_view<OutViewType>(views..., prop, dims...);
388 #else
389 ((void)views, ...);
390 return OutViewType(prop, dims...);
391 #endif
392 }
393 };
394
401 template <typename OutViewType, typename InViewType, typename CtorProp, typename... Dims>
402 OutViewType
403 createMatchingView(const InViewType &view,
404 const CtorProp &prop,
405 const Dims... dims)
406 {
408 return cvf::template create_view<OutViewType>(view, prop, dims...);
409 }
410
416 template <typename InViewType, typename CtorProp, typename... Dims>
417 typename DeduceDynRankView<InViewType>::type
418 createMatchingDynRankView(const InViewType &view,
419 const CtorProp &prop,
420 const Dims... dims)
421 {
422 using OutViewType = typename DeduceDynRankView<InViewType>::type;
423 return createMatchingView<OutViewType>(view, prop, dims...);
424 }
425
432 template <typename OutViewType, typename InViewType, typename CtorProp, typename... Dims>
433 KOKKOS_INLINE_FUNCTION
434 typename std::enable_if<
435 std::is_pointer_v<CtorProp> && !std::is_convertible_v<CtorProp, const char*>,
436 OutViewType>::type
437 createMatchingUnmanagedView(const InViewType &view, const CtorProp &data, const Dims... dims)
438 {
439 #ifdef HAVE_INTREPID2_SACADO
440 #ifdef SACADO_HAS_NEW_KOKKOS_VIEW_IMPL
441 if constexpr (Sacado::is_view_fad<InViewType>::value)
442 #else
443 if constexpr (Kokkos::is_view_fad<InViewType>::value)
444 #endif
445 {
446 const int derivative_dimension = get_dimension_scalar(view);
447 return OutViewType(data, dims..., derivative_dimension);
448 }
449 else
450 return OutViewType(data, dims...);
451 #else
452 (void)view;
453 return OutViewType(data, dims...);
454 #endif
455 }
456
462 template <typename InViewType, typename CtorProp, typename ... Dims>
463 KOKKOS_INLINE_FUNCTION
464 typename std::enable_if<
465 std::is_pointer_v<CtorProp> && !std::is_convertible_v<CtorProp, const char*>,
466 typename DeduceDynRankView<InViewType>::type>::type
467 createMatchingUnmanagedDynRankView(const InViewType& view, const CtorProp& data, const Dims... dims){
468 using OutViewType = typename DeduceDynRankView<InViewType>::type;
469 return createMatchingUnmanagedView<OutViewType>(view, data, dims...);
470 }
471
472 } //Impl namespace
473
474
483 template<class ViewType, class ... DimArgs>
484 inline
485 Kokkos::DynRankView<typename ViewType::value_type, typename DeduceLayout< ViewType >::result_layout, typename ViewType::device_type >
486 getMatchingViewWithLabel(const ViewType &view, const std::string &label, DimArgs... dims)
487 {
488 return Impl::createMatchingDynRankView(view, label, dims...);
489 }
490
491 using std::enable_if_t;
492
496 template <typename T, typename = void>
497 struct has_rank_member : std::false_type{};
498
502 template <typename T>
503 struct has_rank_member<T, decltype((void)T::rank, void())> : std::true_type {};
504
505 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.");
506
510 template<class Functor, ordinal_type default_value>
511 constexpr
512 enable_if_t<has_rank_member<Functor>::value, ordinal_type>
514 {
515 return Functor::rank;
516 }
517
521 template<class Functor, ordinal_type default_value>
522 constexpr
523 enable_if_t<!has_rank_member<Functor>::value, ordinal_type>
525 {
526 return default_value;
527 }
528
532 template <typename T>
534 {
535 typedef char one;
536 struct two { char x[2]; };
537
538 template <typename C> static one test( typename std::remove_reference<decltype( std::declval<C>().operator()(0))>::type );
539 template <typename C> static two test(...);
540
541 public:
542 enum { value = sizeof(test<T>(0)) == sizeof(char) && (getFixedRank<T,1>() == 1) };
543 };
544
548 template <typename T>
550 {
551 typedef char one;
552 struct two { char x[2]; };
553
554 template <typename C> static one test( typename std::remove_reference<decltype( std::declval<C>().operator()(0,0))>::type ) ;
555 template <typename C> static two test(...);
556
557 public:
558 enum { value = sizeof(test<T>(0)) == sizeof(char) && (getFixedRank<T,2>() == 2) };
559 };
560
564 template <typename T>
566 {
567 typedef char one;
568 struct two { char x[2]; };
569
570 template <typename C> static one test( typename std::remove_reference<decltype( std::declval<C>().operator()(0,0,0))>::type ) ;
571 template <typename C> static two test(...);
572
573 public:
574 enum { value = (sizeof(test<T>(0)) == sizeof(char)) && (getFixedRank<T,3>() == 3) };
575 };
576
580 template <typename T>
582 {
583 typedef char one;
584 struct two { char x[2]; };
585
586 template <typename C> static one test( typename std::remove_reference<decltype( std::declval<C>().operator()(0,0,0,0))>::type ) ;
587 template <typename C> static two test(...);
588
589 public:
590 enum { value = sizeof(test<T>(0)) == sizeof(char) && (getFixedRank<T,4>() == 4) };
591 };
592
596 template <typename T>
598 {
599 typedef char one;
600 struct two { char x[2]; };
601
602 template <typename C> static one test( typename std::remove_reference<decltype( std::declval<C>().operator()(0,0,0,0,0))>::type ) ;
603 template <typename C> static two test(...);
604
605 public:
606 enum { value = sizeof(test<T>(0)) == sizeof(char) && (getFixedRank<T,5>() == 5) };
607 };
608
612 template <typename T>
614 {
615 typedef char one;
616 struct two { char x[2]; };
617
618 template <typename C> static one test( typename std::remove_reference<decltype( std::declval<C>().operator()(0,0,0,0,0,0))>::type ) ;
619 template <typename C> static two test(...);
620
621 public:
622 enum { value = sizeof(test<T>(0)) == sizeof(char) && (getFixedRank<T,6>() == 6) };
623 };
624
628 template <typename T>
630 {
631 typedef char one;
632 struct two { char x[2]; };
633
634 template <typename C> static one test( typename std::remove_reference<decltype( std::declval<C>().operator()(0,0,0,0,0,0,0))>::type ) ;
635 template <typename C> static two test(...);
636
637 public:
638 enum { value = sizeof(test<T>(0)) == sizeof(char) && (getFixedRank<T,7>() == 7) };
639 };
640
644 template <typename T, int rank>
646 {
647 public:
648 enum { value = false };
649 };
650
654 template <typename T>
655 class supports_rank<T,1>
656 {
657 public:
658 enum { value = supports_rank_1<T>::value };
659 };
660
662 template <typename T>
663 class supports_rank<T,2>
664 {
665 public:
666 enum { value = supports_rank_2<T>::value };
667 };
668
670 template <typename T>
671 class supports_rank<T,3>
672 {
673 public:
674 enum { value = supports_rank_3<T>::value };
675 };
676
678 template <typename T>
679 class supports_rank<T,4>
680 {
681 public:
682 enum { value = supports_rank_4<T>::value };
683 };
684
686 template <typename T>
687 class supports_rank<T,5>
688 {
689 public:
690 enum { value = supports_rank_5<T>::value };
691 };
692
694 template <typename T>
695 class supports_rank<T,6>
696 {
697 public:
698 enum { value = supports_rank_6<T>::value };
699 };
700
702 template <typename T>
703 class supports_rank<T,7>
704 {
705 public:
706 enum { value = supports_rank_7<T>::value };
707 };
708
709
710
714 template<typename Scalar, int rank>
716
717 };
718
722 template<typename Scalar>
723 struct RankExpander<Scalar,0>
724 {
725 using value_type = Scalar;
726 };
727
731 template<typename Scalar>
732 struct RankExpander<Scalar,1>
733 {
734 using value_type = Scalar*;
735 };
736
740 template<typename Scalar>
741 struct RankExpander<Scalar,2>
742 {
743 using value_type = Scalar**;
744 };
745
749 template<typename Scalar>
750 struct RankExpander<Scalar,3>
751 {
752 using value_type = Scalar***;
753 };
754
758 template<typename Scalar>
759 struct RankExpander<Scalar,4>
760 {
761 using value_type = Scalar****;
762 };
763
767 template<typename Scalar>
768 struct RankExpander<Scalar,5>
769 {
770 using value_type = Scalar*****;
771 };
772
776 template<typename Scalar>
777 struct RankExpander<Scalar,6>
778 {
779 using value_type = Scalar******;
780 };
781
785 template<typename Scalar>
786 struct RankExpander<Scalar,7>
787 {
788 using value_type = Scalar*******;
789 };
790
791 // positive checks of supports_rank for Kokkos::DynRankView:
792 static_assert(supports_rank<Kokkos::DynRankView<double>, 1>::value, "rank 1 check of supports_rank for DynRankView");
793 static_assert(supports_rank<Kokkos::DynRankView<double>, 2>::value, "rank 2 check of supports_rank for DynRankView");
794 static_assert(supports_rank<Kokkos::DynRankView<double>, 3>::value, "rank 3 check of supports_rank for DynRankView");
795 static_assert(supports_rank<Kokkos::DynRankView<double>, 4>::value, "rank 4 check of supports_rank for DynRankView");
796 static_assert(supports_rank<Kokkos::DynRankView<double>, 5>::value, "rank 5 check of supports_rank for DynRankView");
797 static_assert(supports_rank<Kokkos::DynRankView<double>, 6>::value, "rank 6 check of supports_rank for DynRankView");
798 static_assert(supports_rank<Kokkos::DynRankView<double>, 7>::value, "rank 7 check of supports_rank for DynRankView");
799
800 // positive checks of supports_rank for Kokkos::View:
801 static_assert(supports_rank<Kokkos::View<double*>, 1>::value, "rank 1 check of supports_rank");
802 static_assert(supports_rank<Kokkos::View<double**>, 2>::value, "rank 2 check of supports_rank");
803 static_assert(supports_rank<Kokkos::View<double***>, 3>::value, "rank 3 check of supports_rank");
804 static_assert(supports_rank<Kokkos::View<double****>, 4>::value, "rank 4 check of supports_rank");
805 static_assert(supports_rank<Kokkos::View<double*****>, 5>::value, "rank 5 check of supports_rank");
806 static_assert(supports_rank<Kokkos::View<double******>, 6>::value, "rank 6 check of supports_rank");
807 static_assert(supports_rank<Kokkos::View<double*******>, 7>::value, "rank 7 check of supports_rank");
808
809 // negative checks of supports_rank for Kokkos::View:
810 static_assert(!supports_rank<Kokkos::View<double*>, 2>::value, "rank 1 check of supports_rank");
811 static_assert(!supports_rank<Kokkos::View<double*>, 3>::value, "rank 1 check of supports_rank");
812 static_assert(!supports_rank<Kokkos::View<double*>, 4>::value, "rank 1 check of supports_rank");
813 static_assert(!supports_rank<Kokkos::View<double*>, 5>::value, "rank 1 check of supports_rank");
814 static_assert(!supports_rank<Kokkos::View<double*>, 6>::value, "rank 1 check of supports_rank");
815 static_assert(!supports_rank<Kokkos::View<double*>, 7>::value, "rank 1 check of supports_rank");
816 static_assert(!supports_rank<Kokkos::View<double**>, 1>::value, "rank 2 check of supports_rank");
817 static_assert(!supports_rank<Kokkos::View<double**>, 3>::value, "rank 2 check of supports_rank");
818 static_assert(!supports_rank<Kokkos::View<double**>, 4>::value, "rank 2 check of supports_rank");
819 static_assert(!supports_rank<Kokkos::View<double**>, 5>::value, "rank 2 check of supports_rank");
820 static_assert(!supports_rank<Kokkos::View<double**>, 6>::value, "rank 2 check of supports_rank");
821 static_assert(!supports_rank<Kokkos::View<double**>, 7>::value, "rank 2 check of supports_rank");
822 static_assert(!supports_rank<Kokkos::View<double***>, 1>::value, "rank 3 check of supports_rank");
823 static_assert(!supports_rank<Kokkos::View<double***>, 2>::value, "rank 3 check of supports_rank");
824 static_assert(!supports_rank<Kokkos::View<double***>, 4>::value, "rank 3 check of supports_rank");
825 static_assert(!supports_rank<Kokkos::View<double***>, 5>::value, "rank 3 check of supports_rank");
826 static_assert(!supports_rank<Kokkos::View<double***>, 6>::value, "rank 3 check of supports_rank");
827 static_assert(!supports_rank<Kokkos::View<double***>, 7>::value, "rank 3 check of supports_rank");
828 static_assert(!supports_rank<Kokkos::View<double****>, 1>::value, "rank 4 check of supports_rank");
829 static_assert(!supports_rank<Kokkos::View<double****>, 2>::value, "rank 4 check of supports_rank");
830 static_assert(!supports_rank<Kokkos::View<double****>, 3>::value, "rank 4 check of supports_rank");
831 static_assert(!supports_rank<Kokkos::View<double****>, 5>::value, "rank 4 check of supports_rank");
832 static_assert(!supports_rank<Kokkos::View<double****>, 6>::value, "rank 4 check of supports_rank");
833 static_assert(!supports_rank<Kokkos::View<double****>, 7>::value, "rank 4 check of supports_rank");
834 static_assert(!supports_rank<Kokkos::View<double*****>, 1>::value, "rank 5 check of supports_rank");
835 static_assert(!supports_rank<Kokkos::View<double*****>, 2>::value, "rank 5 check of supports_rank");
836 static_assert(!supports_rank<Kokkos::View<double*****>, 3>::value, "rank 5 check of supports_rank");
837 static_assert(!supports_rank<Kokkos::View<double*****>, 4>::value, "rank 5 check of supports_rank");
838 static_assert(!supports_rank<Kokkos::View<double*****>, 6>::value, "rank 5 check of supports_rank");
839 static_assert(!supports_rank<Kokkos::View<double*****>, 7>::value, "rank 5 check of supports_rank");
840 static_assert(!supports_rank<Kokkos::View<double******>, 1>::value, "rank 6 check of supports_rank");
841 static_assert(!supports_rank<Kokkos::View<double******>, 2>::value, "rank 6 check of supports_rank");
842 static_assert(!supports_rank<Kokkos::View<double******>, 3>::value, "rank 6 check of supports_rank");
843 static_assert(!supports_rank<Kokkos::View<double******>, 4>::value, "rank 6 check of supports_rank");
844 static_assert(!supports_rank<Kokkos::View<double******>, 5>::value, "rank 6 check of supports_rank");
845 static_assert(!supports_rank<Kokkos::View<double******>, 7>::value, "rank 6 check of supports_rank");
846 static_assert(!supports_rank<Kokkos::View<double*******>, 1>::value, "rank 7 check of supports_rank");
847 static_assert(!supports_rank<Kokkos::View<double*******>, 2>::value, "rank 7 check of supports_rank");
848 static_assert(!supports_rank<Kokkos::View<double*******>, 3>::value, "rank 7 check of supports_rank");
849 static_assert(!supports_rank<Kokkos::View<double*******>, 4>::value, "rank 7 check of supports_rank");
850 static_assert(!supports_rank<Kokkos::View<double*******>, 5>::value, "rank 7 check of supports_rank");
851 static_assert(!supports_rank<Kokkos::View<double*******>, 6>::value, "rank 7 check of supports_rank");
852
856 template <typename T>
858 {
859 typedef char one;
860 struct two { char x[2]; };
861
862 template <typename C> static one test( decltype( std::declval<C>().rank() ) ) ;
863 template <typename C> static two test(...);
864
865 public:
866 enum { value = sizeof(test<T>(0)) == sizeof(char) };
867 };
868
869 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.");
870
874 template<class Functor>
875 enable_if_t<has_rank_method<Functor>::value, unsigned>
876 KOKKOS_INLINE_FUNCTION
877 getFunctorRank(const Functor &functor)
878 {
879 return functor.rank();
880 }
881
885 template<class Functor>
886 enable_if_t<!has_rank_method<Functor>::value, unsigned>
887 KOKKOS_INLINE_FUNCTION
888 getFunctorRank(const Functor &functor)
889 {
890 return functor.rank;
891 }
892
896#if defined(HAVE_INTREPID2_SACADO) && !defined(SACADO_HAS_NEW_KOKKOS_VIEW_IMPL)
897 template <typename ValueType>
898 struct NaturalLayoutForType {
899 using layout =
900 typename std::conditional<(std::is_standard_layout<ValueType>::value && std::is_trivial<ValueType>::value),
901 Kokkos::LayoutLeft, // for POD types, use LayoutLeft
902 Kokkos::LayoutNatural<Kokkos::LayoutLeft> >::type; // For FAD types, use LayoutNatural
903 };
904#else
905 template <typename ValueType>
907 using layout = Kokkos::LayoutLeft;
908 };
909#endif
910
911 // define vector sizes for hierarchical parallelism
912 const int VECTOR_SIZE = 1;
913#if defined(SACADO_VIEW_CUDA_HIERARCHICAL_DFAD) && defined(INTREPID2_ENABLE_DEVICE)
914 const int FAD_VECTOR_SIZE = 32;
915#else
916 const int FAD_VECTOR_SIZE = 1;
917#endif
918
922 template<typename Scalar>
924 {
925 return (std::is_standard_layout<Scalar>::value && std::is_trivial<Scalar>::value) ? VECTOR_SIZE : FAD_VECTOR_SIZE;
926 }
927
933 template<typename ViewType>
934 KOKKOS_INLINE_FUNCTION
935 constexpr unsigned getScalarDimensionForView(const ViewType &view)
936 {
937 return (std::is_standard_layout<typename ViewType::value_type>::value && std::is_trivial<typename ViewType::value_type>::value) ? 0 : get_dimension_scalar(view);
938 }
939
941 template<typename Device>
943 template<typename T>
944 void operator()(T* ptr) {
945 Kokkos::parallel_for(Kokkos::RangePolicy<typename Device::execution_space>(0,1),
946 KOKKOS_LAMBDA (const int i) { ptr->~T(); });
947 typename Device::execution_space().fence();
948 Kokkos::kokkos_free<typename Device::memory_space>(ptr);
949 }
950 };
951
955 template<typename Device,typename Derived>
956 std::unique_ptr<Derived,DeviceDeleter<Device>>
957 copy_virtual_class_to_device(const Derived& host_source)
958 {
959 auto* p = static_cast<Derived*>(Kokkos::kokkos_malloc<typename Device::memory_space>(sizeof(Derived)));
960 Kokkos::parallel_for(Kokkos::RangePolicy<typename Device::execution_space>(0,1),
961 KOKKOS_LAMBDA (const int i) {new (p) Derived(host_source); });
962 typename Device::execution_space().fence();
963 return std::unique_ptr<Derived,DeviceDeleter<Device>>(p);
964 }
965
966
967
968
969
970
971
972
973} // end namespace Intrepid2
974
975#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...