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 template <class DataType, class... Properties>
339 KOKKOS_INLINE_FUNCTION auto
340 as_scalar_1d_view(const Kokkos::View<DataType, Properties...> &view) {
341 using view_t = Kokkos::View<DataType, Properties...>;
342#ifdef HAVE_INTREPID2_SACADO
343 if constexpr (Kokkos::is_view_fad<view_t>::value) {
344 return Sacado::as_scalar_view(view);
345 } else
346#endif
347 return Kokkos::View<typename view_t::value_type*, Properties...>(view.data(), view.mapping().required_span_size());
348 }
349
350 template <class... Args>
351 KOKKOS_INLINE_FUNCTION auto
352 as_scalar_1d_view(const Kokkos::DynRankView<Args...> &dyn) {
353 return as_scalar_1d_view(dyn.ConstDownCast());
354 }
355
356
357 namespace Impl
358 {
362 template <class... ViewPack>
364 {
370 template <class OutViewType, class CtorProp, class... Dims>
371 static OutViewType
372 create_view(const ViewPack &...views,
373 const CtorProp &prop,
374 const Dims... dims)
375 {
376 #ifdef HAVE_INTREPID2_SACADO
377 using view_factory = Kokkos::ViewFactory<ViewPack...>;
378 return view_factory::template create_view<OutViewType>(views..., prop, dims...);
379 #else
380 ((void)views, ...);
381 return OutViewType(prop, dims...);
382 #endif
383 }
384 };
385
392 template <typename OutViewType, typename InViewType, typename CtorProp, typename... Dims>
393 OutViewType
394 createMatchingView(const InViewType &view,
395 const CtorProp &prop,
396 const Dims... dims)
397 {
399 return cvf::template create_view<OutViewType>(view, prop, dims...);
400 }
401
407 template <typename InViewType, typename CtorProp, typename... Dims>
408 typename DeduceDynRankView<InViewType>::type
409 createMatchingDynRankView(const InViewType &view,
410 const CtorProp &prop,
411 const Dims... dims)
412 {
413 using OutViewType = typename DeduceDynRankView<InViewType>::type;
414 return createMatchingView<OutViewType>(view, prop, dims...);
415 }
416
423 template <typename OutViewType, typename InViewType, typename CtorProp, typename... Dims>
424 KOKKOS_INLINE_FUNCTION
425 typename std::enable_if<
426 std::is_pointer_v<CtorProp> && !std::is_convertible_v<CtorProp, const char*>,
427 OutViewType>::type
428 createMatchingUnmanagedView(const InViewType &view, const CtorProp &data, const Dims... dims)
429 {
430 #ifdef HAVE_INTREPID2_SACADO
431 #ifdef SACADO_HAS_NEW_KOKKOS_VIEW_IMPL
432 if constexpr (Sacado::is_view_fad<InViewType>::value)
433 #else
434 if constexpr (Kokkos::is_view_fad<InViewType>::value)
435 #endif
436 {
437 const int derivative_dimension = get_dimension_scalar(view);
438 return OutViewType(data, dims..., derivative_dimension);
439 }
440 else
441 return OutViewType(data, dims...);
442 #else
443 (void)view;
444 return OutViewType(data, dims...);
445 #endif
446 }
447
453 template <typename InViewType, typename CtorProp, typename ... Dims>
454 KOKKOS_INLINE_FUNCTION
455 typename std::enable_if<
456 std::is_pointer_v<CtorProp> && !std::is_convertible_v<CtorProp, const char*>,
457 typename DeduceDynRankView<InViewType>::type>::type
458 createMatchingUnmanagedDynRankView(const InViewType& view, const CtorProp& data, const Dims... dims){
459 using OutViewType = typename DeduceDynRankView<InViewType>::type;
460 return createMatchingUnmanagedView<OutViewType>(view, data, dims...);
461 }
462
463 } //Impl namespace
464
465
474 template<class ViewType, class ... DimArgs>
475 inline
476 Kokkos::DynRankView<typename ViewType::value_type, typename DeduceLayout< ViewType >::result_layout, typename ViewType::device_type >
477 getMatchingViewWithLabel(const ViewType &view, const std::string &label, DimArgs... dims)
478 {
479 return Impl::createMatchingDynRankView(view, label, dims...);
480 }
481
482 using std::enable_if_t;
483
487 template <typename T, typename = void>
488 struct has_rank_member : std::false_type{};
489
493 template <typename T>
494 struct has_rank_member<T, decltype((void)T::rank, void())> : std::true_type {};
495
496 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.");
497
501 template<class Functor, ordinal_type default_value>
502 constexpr
503 enable_if_t<has_rank_member<Functor>::value, ordinal_type>
505 {
506 return Functor::rank;
507 }
508
512 template<class Functor, ordinal_type default_value>
513 constexpr
514 enable_if_t<!has_rank_member<Functor>::value, ordinal_type>
516 {
517 return default_value;
518 }
519
523 template <typename T>
525 {
526 typedef char one;
527 struct two { char x[2]; };
528
529 template <typename C> static one test( typename std::remove_reference<decltype( std::declval<C>().operator()(0))>::type );
530 template <typename C> static two test(...);
531
532 public:
533 enum { value = sizeof(test<T>(0)) == sizeof(char) && (getFixedRank<T,1>() == 1) };
534 };
535
539 template <typename T>
541 {
542 typedef char one;
543 struct two { char x[2]; };
544
545 template <typename C> static one test( typename std::remove_reference<decltype( std::declval<C>().operator()(0,0))>::type ) ;
546 template <typename C> static two test(...);
547
548 public:
549 enum { value = sizeof(test<T>(0)) == sizeof(char) && (getFixedRank<T,2>() == 2) };
550 };
551
555 template <typename T>
557 {
558 typedef char one;
559 struct two { char x[2]; };
560
561 template <typename C> static one test( typename std::remove_reference<decltype( std::declval<C>().operator()(0,0,0))>::type ) ;
562 template <typename C> static two test(...);
563
564 public:
565 enum { value = (sizeof(test<T>(0)) == sizeof(char)) && (getFixedRank<T,3>() == 3) };
566 };
567
571 template <typename T>
573 {
574 typedef char one;
575 struct two { char x[2]; };
576
577 template <typename C> static one test( typename std::remove_reference<decltype( std::declval<C>().operator()(0,0,0,0))>::type ) ;
578 template <typename C> static two test(...);
579
580 public:
581 enum { value = sizeof(test<T>(0)) == sizeof(char) && (getFixedRank<T,4>() == 4) };
582 };
583
587 template <typename T>
589 {
590 typedef char one;
591 struct two { char x[2]; };
592
593 template <typename C> static one test( typename std::remove_reference<decltype( std::declval<C>().operator()(0,0,0,0,0))>::type ) ;
594 template <typename C> static two test(...);
595
596 public:
597 enum { value = sizeof(test<T>(0)) == sizeof(char) && (getFixedRank<T,5>() == 5) };
598 };
599
603 template <typename T>
605 {
606 typedef char one;
607 struct two { char x[2]; };
608
609 template <typename C> static one test( typename std::remove_reference<decltype( std::declval<C>().operator()(0,0,0,0,0,0))>::type ) ;
610 template <typename C> static two test(...);
611
612 public:
613 enum { value = sizeof(test<T>(0)) == sizeof(char) && (getFixedRank<T,6>() == 6) };
614 };
615
619 template <typename T>
621 {
622 typedef char one;
623 struct two { char x[2]; };
624
625 template <typename C> static one test( typename std::remove_reference<decltype( std::declval<C>().operator()(0,0,0,0,0,0,0))>::type ) ;
626 template <typename C> static two test(...);
627
628 public:
629 enum { value = sizeof(test<T>(0)) == sizeof(char) && (getFixedRank<T,7>() == 7) };
630 };
631
635 template <typename T, int rank>
637 {
638 public:
639 enum { value = false };
640 };
641
645 template <typename T>
646 class supports_rank<T,1>
647 {
648 public:
649 enum { value = supports_rank_1<T>::value };
650 };
651
653 template <typename T>
654 class supports_rank<T,2>
655 {
656 public:
657 enum { value = supports_rank_2<T>::value };
658 };
659
661 template <typename T>
662 class supports_rank<T,3>
663 {
664 public:
665 enum { value = supports_rank_3<T>::value };
666 };
667
669 template <typename T>
670 class supports_rank<T,4>
671 {
672 public:
673 enum { value = supports_rank_4<T>::value };
674 };
675
677 template <typename T>
678 class supports_rank<T,5>
679 {
680 public:
681 enum { value = supports_rank_5<T>::value };
682 };
683
685 template <typename T>
686 class supports_rank<T,6>
687 {
688 public:
689 enum { value = supports_rank_6<T>::value };
690 };
691
693 template <typename T>
694 class supports_rank<T,7>
695 {
696 public:
697 enum { value = supports_rank_7<T>::value };
698 };
699
700
701
705 template<typename Scalar, int rank>
707
708 };
709
713 template<typename Scalar>
714 struct RankExpander<Scalar,0>
715 {
716 using value_type = Scalar;
717 };
718
722 template<typename Scalar>
723 struct RankExpander<Scalar,1>
724 {
725 using value_type = Scalar*;
726 };
727
731 template<typename Scalar>
732 struct RankExpander<Scalar,2>
733 {
734 using value_type = Scalar**;
735 };
736
740 template<typename Scalar>
741 struct RankExpander<Scalar,3>
742 {
743 using value_type = Scalar***;
744 };
745
749 template<typename Scalar>
750 struct RankExpander<Scalar,4>
751 {
752 using value_type = Scalar****;
753 };
754
758 template<typename Scalar>
759 struct RankExpander<Scalar,5>
760 {
761 using value_type = Scalar*****;
762 };
763
767 template<typename Scalar>
768 struct RankExpander<Scalar,6>
769 {
770 using value_type = Scalar******;
771 };
772
776 template<typename Scalar>
777 struct RankExpander<Scalar,7>
778 {
779 using value_type = Scalar*******;
780 };
781
782 // positive checks of supports_rank for Kokkos::DynRankView:
783 static_assert(supports_rank<Kokkos::DynRankView<double>, 1>::value, "rank 1 check of supports_rank for DynRankView");
784 static_assert(supports_rank<Kokkos::DynRankView<double>, 2>::value, "rank 2 check of supports_rank for DynRankView");
785 static_assert(supports_rank<Kokkos::DynRankView<double>, 3>::value, "rank 3 check of supports_rank for DynRankView");
786 static_assert(supports_rank<Kokkos::DynRankView<double>, 4>::value, "rank 4 check of supports_rank for DynRankView");
787 static_assert(supports_rank<Kokkos::DynRankView<double>, 5>::value, "rank 5 check of supports_rank for DynRankView");
788 static_assert(supports_rank<Kokkos::DynRankView<double>, 6>::value, "rank 6 check of supports_rank for DynRankView");
789 static_assert(supports_rank<Kokkos::DynRankView<double>, 7>::value, "rank 7 check of supports_rank for DynRankView");
790
791 // positive checks of supports_rank for Kokkos::View:
792 static_assert(supports_rank<Kokkos::View<double*>, 1>::value, "rank 1 check of supports_rank");
793 static_assert(supports_rank<Kokkos::View<double**>, 2>::value, "rank 2 check of supports_rank");
794 static_assert(supports_rank<Kokkos::View<double***>, 3>::value, "rank 3 check of supports_rank");
795 static_assert(supports_rank<Kokkos::View<double****>, 4>::value, "rank 4 check of supports_rank");
796 static_assert(supports_rank<Kokkos::View<double*****>, 5>::value, "rank 5 check of supports_rank");
797 static_assert(supports_rank<Kokkos::View<double******>, 6>::value, "rank 6 check of supports_rank");
798 static_assert(supports_rank<Kokkos::View<double*******>, 7>::value, "rank 7 check of supports_rank");
799
800 // negative checks of supports_rank for Kokkos::View:
801 static_assert(!supports_rank<Kokkos::View<double*>, 2>::value, "rank 1 check of supports_rank");
802 static_assert(!supports_rank<Kokkos::View<double*>, 3>::value, "rank 1 check of supports_rank");
803 static_assert(!supports_rank<Kokkos::View<double*>, 4>::value, "rank 1 check of supports_rank");
804 static_assert(!supports_rank<Kokkos::View<double*>, 5>::value, "rank 1 check of supports_rank");
805 static_assert(!supports_rank<Kokkos::View<double*>, 6>::value, "rank 1 check of supports_rank");
806 static_assert(!supports_rank<Kokkos::View<double*>, 7>::value, "rank 1 check of supports_rank");
807 static_assert(!supports_rank<Kokkos::View<double**>, 1>::value, "rank 2 check of supports_rank");
808 static_assert(!supports_rank<Kokkos::View<double**>, 3>::value, "rank 2 check of supports_rank");
809 static_assert(!supports_rank<Kokkos::View<double**>, 4>::value, "rank 2 check of supports_rank");
810 static_assert(!supports_rank<Kokkos::View<double**>, 5>::value, "rank 2 check of supports_rank");
811 static_assert(!supports_rank<Kokkos::View<double**>, 6>::value, "rank 2 check of supports_rank");
812 static_assert(!supports_rank<Kokkos::View<double**>, 7>::value, "rank 2 check of supports_rank");
813 static_assert(!supports_rank<Kokkos::View<double***>, 1>::value, "rank 3 check of supports_rank");
814 static_assert(!supports_rank<Kokkos::View<double***>, 2>::value, "rank 3 check of supports_rank");
815 static_assert(!supports_rank<Kokkos::View<double***>, 4>::value, "rank 3 check of supports_rank");
816 static_assert(!supports_rank<Kokkos::View<double***>, 5>::value, "rank 3 check of supports_rank");
817 static_assert(!supports_rank<Kokkos::View<double***>, 6>::value, "rank 3 check of supports_rank");
818 static_assert(!supports_rank<Kokkos::View<double***>, 7>::value, "rank 3 check of supports_rank");
819 static_assert(!supports_rank<Kokkos::View<double****>, 1>::value, "rank 4 check of supports_rank");
820 static_assert(!supports_rank<Kokkos::View<double****>, 2>::value, "rank 4 check of supports_rank");
821 static_assert(!supports_rank<Kokkos::View<double****>, 3>::value, "rank 4 check of supports_rank");
822 static_assert(!supports_rank<Kokkos::View<double****>, 5>::value, "rank 4 check of supports_rank");
823 static_assert(!supports_rank<Kokkos::View<double****>, 6>::value, "rank 4 check of supports_rank");
824 static_assert(!supports_rank<Kokkos::View<double****>, 7>::value, "rank 4 check of supports_rank");
825 static_assert(!supports_rank<Kokkos::View<double*****>, 1>::value, "rank 5 check of supports_rank");
826 static_assert(!supports_rank<Kokkos::View<double*****>, 2>::value, "rank 5 check of supports_rank");
827 static_assert(!supports_rank<Kokkos::View<double*****>, 3>::value, "rank 5 check of supports_rank");
828 static_assert(!supports_rank<Kokkos::View<double*****>, 4>::value, "rank 5 check of supports_rank");
829 static_assert(!supports_rank<Kokkos::View<double*****>, 6>::value, "rank 5 check of supports_rank");
830 static_assert(!supports_rank<Kokkos::View<double*****>, 7>::value, "rank 5 check of supports_rank");
831 static_assert(!supports_rank<Kokkos::View<double******>, 1>::value, "rank 6 check of supports_rank");
832 static_assert(!supports_rank<Kokkos::View<double******>, 2>::value, "rank 6 check of supports_rank");
833 static_assert(!supports_rank<Kokkos::View<double******>, 3>::value, "rank 6 check of supports_rank");
834 static_assert(!supports_rank<Kokkos::View<double******>, 4>::value, "rank 6 check of supports_rank");
835 static_assert(!supports_rank<Kokkos::View<double******>, 5>::value, "rank 6 check of supports_rank");
836 static_assert(!supports_rank<Kokkos::View<double******>, 7>::value, "rank 6 check of supports_rank");
837 static_assert(!supports_rank<Kokkos::View<double*******>, 1>::value, "rank 7 check of supports_rank");
838 static_assert(!supports_rank<Kokkos::View<double*******>, 2>::value, "rank 7 check of supports_rank");
839 static_assert(!supports_rank<Kokkos::View<double*******>, 3>::value, "rank 7 check of supports_rank");
840 static_assert(!supports_rank<Kokkos::View<double*******>, 4>::value, "rank 7 check of supports_rank");
841 static_assert(!supports_rank<Kokkos::View<double*******>, 5>::value, "rank 7 check of supports_rank");
842 static_assert(!supports_rank<Kokkos::View<double*******>, 6>::value, "rank 7 check of supports_rank");
843
847 template <typename T>
849 {
850 typedef char one;
851 struct two { char x[2]; };
852
853 template <typename C> static one test( decltype( std::declval<C>().rank() ) ) ;
854 template <typename C> static two test(...);
855
856 public:
857 enum { value = sizeof(test<T>(0)) == sizeof(char) };
858 };
859
860 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.");
861
865 template<class Functor>
866 enable_if_t<has_rank_method<Functor>::value, unsigned>
867 KOKKOS_INLINE_FUNCTION
868 getFunctorRank(const Functor &functor)
869 {
870 return functor.rank();
871 }
872
876 template<class Functor>
877 enable_if_t<!has_rank_method<Functor>::value, unsigned>
878 KOKKOS_INLINE_FUNCTION
879 getFunctorRank(const Functor &functor)
880 {
881 return functor.rank;
882 }
883
887#if defined(HAVE_INTREPID2_SACADO) && !defined(SACADO_HAS_NEW_KOKKOS_VIEW_IMPL)
888 template <typename ValueType>
889 struct NaturalLayoutForType {
890 using layout =
891 typename std::conditional<(std::is_standard_layout<ValueType>::value && std::is_trivial<ValueType>::value),
892 Kokkos::LayoutLeft, // for POD types, use LayoutLeft
893 Kokkos::LayoutNatural<Kokkos::LayoutLeft> >::type; // For FAD types, use LayoutNatural
894 };
895#else
896 template <typename ValueType>
898 using layout = Kokkos::LayoutLeft;
899 };
900#endif
901
902 // define vector sizes for hierarchical parallelism
903 const int VECTOR_SIZE = 1;
904#if defined(SACADO_VIEW_CUDA_HIERARCHICAL_DFAD) && defined(INTREPID2_ENABLE_DEVICE)
905 const int FAD_VECTOR_SIZE = 32;
906#else
907 const int FAD_VECTOR_SIZE = 1;
908#endif
909
913 template<typename Scalar>
915 {
916 return (std::is_standard_layout<Scalar>::value && std::is_trivial<Scalar>::value) ? VECTOR_SIZE : FAD_VECTOR_SIZE;
917 }
918
924 template<typename ViewType>
925 KOKKOS_INLINE_FUNCTION
926 constexpr unsigned getScalarDimensionForView(const ViewType &view)
927 {
928 return (std::is_standard_layout<typename ViewType::value_type>::value && std::is_trivial<typename ViewType::value_type>::value) ? 0 : get_dimension_scalar(view);
929 }
930
932 template<typename Device>
934 template<typename T>
935 void operator()(T* ptr) {
936 Kokkos::parallel_for(Kokkos::RangePolicy<typename Device::execution_space>(0,1),
937 KOKKOS_LAMBDA (const int i) { ptr->~T(); });
938 typename Device::execution_space().fence();
939 Kokkos::kokkos_free<typename Device::memory_space>(ptr);
940 }
941 };
942
946 template<typename Device,typename Derived>
947 std::unique_ptr<Derived,DeviceDeleter<Device>>
948 copy_virtual_class_to_device(const Derived& host_source)
949 {
950 auto* p = static_cast<Derived*>(Kokkos::kokkos_malloc<typename Device::memory_space>(sizeof(Derived)));
951 Kokkos::parallel_for(Kokkos::RangePolicy<typename Device::execution_space>(0,1),
952 KOKKOS_LAMBDA (const int i) {new (p) Derived(host_source); });
953 typename Device::execution_space().fence();
954 return std::unique_ptr<Derived,DeviceDeleter<Device>>(p);
955 }
956
957
958
959
960
961
962
963
964} // end namespace Intrepid2
965
966#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...