16#ifndef __INTREPID2_UTILS_HPP__
17#define __INTREPID2_UTILS_HPP__
19#include "Intrepid2_ConfigDefs.hpp"
23#include "Kokkos_Core.hpp"
24#include "Kokkos_Macros.hpp"
25#include "Kokkos_Random.hpp"
27#ifdef HAVE_INTREPID2_SACADO
28#include "Sacado_Fad_Kokkos_Fwd.hpp"
29#include "Sacado_Fad_Kokkos_ViewFactory.hpp"
34#if defined(__CUDA_ARCH__) || defined(__HIP_DEVICE_COMPILE__) || defined(__SYCL_DEVICE_ONLY__)
35#define INTREPID2_COMPILE_DEVICE_CODE
38#if defined(KOKKOS_ENABLE_CUDA) || defined(KOKKOS_ENABLE_HIP) || defined(KOKKOS_ENABLE_SYCL)
39#define INTREPID2_ENABLE_DEVICE
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
52#define INTREPID2_TEST_FOR_WARNING(test, msg) \
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); \
59#define INTREPID2_TEST_FOR_EXCEPTION(test, x, msg) \
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); \
69#ifndef INTREPID2_ENABLE_DEVICE
70#define INTREPID2_TEST_FOR_EXCEPTION_DEVICE_SAFE(test, x, msg) \
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"; \
78#define INTREPID2_TEST_FOR_EXCEPTION_DEVICE_SAFE(test, x, msg) \
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"); \
86#if defined(INTREPID2_ENABLE_DEBUG) || defined(NDEBUG) || 1
87#define INTREPID2_TEST_FOR_ABORT(test, msg) \
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"); \
95#define INTREPID2_TEST_FOR_ABORT(test, msg) ((void)0)
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); \
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); \
113 Kokkos::abort( "[Intrepid2] Abort\n"); \
122 typedef typename T::scalar_type scalar_type;
131 typedef float scalar_type;
138 typedef double scalar_type;
145 typedef int scalar_type;
152 typedef long int scalar_type;
159 typedef long long scalar_type;
167 template<
typename ViewSpaceType,
typename UserSpaceType>
169 typedef UserSpaceType ExecSpaceType;
175 template<
typename ViewSpaceType>
177 typedef ViewSpaceType ExecSpaceType;
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,
192 input_layout >::type;
201 template<
typename IdxType,
typename DimType,
typename IterType>
202 KOKKOS_FORCEINLINE_FUNCTION
204 unrollIndex(IdxType &i, IdxType &j,
207 const IterType iter) {
217 template<
typename IdxType,
typename DimType,
typename IterType>
218 KOKKOS_FORCEINLINE_FUNCTION
220 unrollIndex(IdxType &i, IdxType &j, IdxType &k,
224 const IterType iter) {
230 unrollIndex( i, tmp, dim0, dim1*dim2, iter);
231 unrollIndex( j, k, dim1, dim2, tmp);
240 KOKKOS_FORCEINLINE_FUNCTION
241 static T min(
const T a,
const T b) {
242 return (a < b ? a : b);
245 KOKKOS_FORCEINLINE_FUNCTION
246 static T max(
const T a,
const T b) {
247 return (a > b ? a : b);
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) {
255 T tmp_max = max(b, args...);
256 return (a > tmp_max) ? a : tmp_max;
259 KOKKOS_FORCEINLINE_FUNCTION
260 static T abs(
const T a) {
261 return (a > 0 ? a : T(-a));
267 KOKKOS_FORCEINLINE_FUNCTION
268 static T min(
const T &a,
const T &b) {
269 return (a < b ? a : b);
273 KOKKOS_FORCEINLINE_FUNCTION
274 static T max(
const T &a,
const T &b) {
275 return (a > b ? a : b);
279 KOKKOS_FORCEINLINE_FUNCTION
280 static T abs(
const T &a) {
281 return (a > 0 ? a : T(-a));
292 KOKKOS_FORCEINLINE_FUNCTION
294 std::enable_if< !(std::is_standard_layout<T>::value && std::is_trivial<T>::value),
typename ScalarTraits<T>::scalar_type >::type
298 KOKKOS_FORCEINLINE_FUNCTION
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;}
324 template<
typename T,
typename ...P>
325 KOKKOS_FORCEINLINE_FUNCTION
327#ifdef HAVE_INTREPID2_SACADO
328 return Sacado::dimension_scalar(view);
334 template<
typename T,
typename ...P>
335 KOKKOS_FORCEINLINE_FUNCTION
336 static ordinal_type get_dimension_scalar(
const Kokkos::View<T, P...> &view) {
337#ifdef HAVE_INTREPID2_SACADO
338 return Sacado::dimension_scalar(view);
346 template <
typename InputView>
349 using value =
typename InputView::non_const_value_type;
350 using layout =
typename DeduceLayout<InputView>::result_layout;
351 using device =
typename InputView::device_type;
352 using type = Kokkos::DynRankView<value, layout, device>;
356 template <
class DataType,
class... Properties>
357 KOKKOS_INLINE_FUNCTION
auto
358 as_scalar_1d_view(
const Kokkos::View<DataType, Properties...> &view) {
359 using view_t = Kokkos::View<DataType, Properties...>;
360#ifdef HAVE_INTREPID2_SACADO
361 if constexpr (Sacado::is_view_fad<view_t>::value) {
362 return Sacado::as_scalar_view(view);
365 return Kokkos::View<
typename view_t::value_type*, Properties...>(view.data(), view.mapping().required_span_size());
368 template <
class... Args>
369 KOKKOS_INLINE_FUNCTION
auto
370 as_scalar_1d_view(
const Kokkos::DynRankView<Args...> &dyn) {
371 return as_scalar_1d_view(dyn.ConstDownCast());
380 template <
class... ViewPack>
388 template <
class OutViewType,
class CtorProp,
class... Dims>
391 const CtorProp &prop,
394 #ifdef HAVE_INTREPID2_SACADO
395 using view_factory = Sacado::ViewFactory<ViewPack...>;
396 return view_factory::template create_view<OutViewType>(views..., prop, dims...);
399 return OutViewType(prop, dims...);
410 template <
typename OutViewType,
typename InViewType,
typename CtorProp,
typename... Dims>
413 const CtorProp &prop,
417 return cvf::template create_view<OutViewType>(view, prop, dims...);
425 template <
typename InViewType,
typename CtorProp,
typename... Dims>
426 typename DeduceDynRankView<InViewType>::type
428 const CtorProp &prop,
431 using OutViewType =
typename DeduceDynRankView<InViewType>::type;
432 return createMatchingView<OutViewType>(view, prop, dims...);
441 template <
typename OutViewType,
typename InViewType,
typename CtorProp,
typename... Dims>
442 KOKKOS_INLINE_FUNCTION
443 typename std::enable_if<
444 std::is_pointer_v<CtorProp> && !std::is_convertible_v<CtorProp, const char*>,
448 #ifdef HAVE_INTREPID2_SACADO
449 if constexpr (Sacado::is_view_fad<InViewType>::value)
452 return OutViewType(data, dims..., derivative_dimension);
455 return OutViewType(data, dims...);
458 return OutViewType(data, dims...);
467 template <
typename InViewType,
typename CtorProp,
typename ... Dims>
468 KOKKOS_INLINE_FUNCTION
469 typename std::enable_if<
470 std::is_pointer_v<CtorProp> && !std::is_convertible_v<CtorProp, const char*>,
471 typename DeduceDynRankView<InViewType>::type>::type
473 using OutViewType =
typename DeduceDynRankView<InViewType>::type;
474 return createMatchingUnmanagedView<OutViewType>(view, data, dims...);
488 template<
class ViewType,
class ... DimArgs>
490 Kokkos::DynRankView<typename ViewType::value_type, typename DeduceLayout< ViewType >::result_layout,
typename ViewType::device_type >
493 return Impl::createMatchingDynRankView(view, label, dims...);
496 using std::enable_if_t;
501 template <
typename T,
typename =
void>
507 template <
typename T>
515 template<
class Functor, ordinal_type default_value>
517 enable_if_t<has_rank_member<Functor>::value, ordinal_type>
520 return Functor::rank;
526 template<
class Functor, ordinal_type default_value>
528 enable_if_t<!has_rank_member<Functor>::value, ordinal_type>
531 return default_value;
537 template <
typename T>
541 struct two {
char x[2]; };
543 template <
typename C>
static one test(
typename std::remove_reference<
decltype( std::declval<C>().
operator()(0))>::type );
544 template <
typename C>
static two test(...);
547 enum { value =
sizeof(test<T>(0)) ==
sizeof(
char) && (getFixedRank<T,1>() == 1) };
553 template <
typename T>
557 struct two {
char x[2]; };
559 template <
typename C>
static one test(
typename std::remove_reference<
decltype( std::declval<C>().
operator()(0,0))>::type ) ;
560 template <
typename C>
static two test(...);
563 enum { value =
sizeof(test<T>(0)) ==
sizeof(
char) && (getFixedRank<T,2>() == 2) };
569 template <
typename T>
573 struct two {
char x[2]; };
575 template <
typename C>
static one test(
typename std::remove_reference<
decltype( std::declval<C>().
operator()(0,0,0))>::type ) ;
576 template <
typename C>
static two test(...);
579 enum { value = (
sizeof(test<T>(0)) ==
sizeof(
char)) && (getFixedRank<T,3>() == 3) };
585 template <
typename T>
589 struct two {
char x[2]; };
591 template <
typename C>
static one test(
typename std::remove_reference<
decltype( std::declval<C>().
operator()(0,0,0,0))>::type ) ;
592 template <
typename C>
static two test(...);
595 enum { value =
sizeof(test<T>(0)) ==
sizeof(
char) && (getFixedRank<T,4>() == 4) };
601 template <
typename T>
605 struct two {
char x[2]; };
607 template <
typename C>
static one test(
typename std::remove_reference<
decltype( std::declval<C>().
operator()(0,0,0,0,0))>::type ) ;
608 template <
typename C>
static two test(...);
611 enum { value =
sizeof(test<T>(0)) ==
sizeof(
char) && (getFixedRank<T,5>() == 5) };
617 template <
typename T>
621 struct two {
char x[2]; };
623 template <
typename C>
static one test(
typename std::remove_reference<
decltype( std::declval<C>().
operator()(0,0,0,0,0,0))>::type ) ;
624 template <
typename C>
static two test(...);
627 enum { value =
sizeof(test<T>(0)) ==
sizeof(
char) && (getFixedRank<T,6>() == 6) };
633 template <
typename T>
637 struct two {
char x[2]; };
639 template <
typename C>
static one test(
typename std::remove_reference<
decltype( std::declval<C>().
operator()(0,0,0,0,0,0,0))>::type ) ;
640 template <
typename C>
static two test(...);
643 enum { value =
sizeof(test<T>(0)) ==
sizeof(
char) && (getFixedRank<T,7>() == 7) };
649 template <
typename T,
int rank>
653 enum { value =
false };
659 template <
typename T>
667 template <
typename T>
675 template <
typename T>
683 template <
typename T>
691 template <
typename T>
699 template <
typename T>
707 template <
typename T>
719 template<
typename Scalar,
int rank>
727 template<
typename Scalar>
730 using value_type = Scalar;
736 template<
typename Scalar>
739 using value_type = Scalar*;
745 template<
typename Scalar>
748 using value_type = Scalar**;
754 template<
typename Scalar>
757 using value_type = Scalar***;
763 template<
typename Scalar>
766 using value_type = Scalar****;
772 template<
typename Scalar>
775 using value_type = Scalar*****;
781 template<
typename Scalar>
784 using value_type = Scalar******;
790 template<
typename Scalar>
793 using value_type = Scalar*******;
861 template <
typename T>
865 struct two {
char x[2]; };
867 template <
typename C>
static one test(
decltype( std::declval<C>().rank() ) ) ;
868 template <
typename C>
static two test(...);
871 enum { value =
sizeof(test<T>(0)) ==
sizeof(
char) };
874 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.");
879 template<
class Functor>
880 enable_if_t<has_rank_method<Functor>::value,
unsigned>
881 KOKKOS_INLINE_FUNCTION
884 return functor.rank();
890 template<
class Functor>
891 enable_if_t<!has_rank_method<Functor>::value,
unsigned>
892 KOKKOS_INLINE_FUNCTION
901 template <
typename ValueType>
903 using layout = Kokkos::LayoutLeft;
907 const int VECTOR_SIZE = 1;
908#if defined(SACADO_VIEW_CUDA_HIERARCHICAL_DFAD) && defined(INTREPID2_ENABLE_DEVICE)
909 const int FAD_VECTOR_SIZE = 32;
911 const int FAD_VECTOR_SIZE = 1;
917 template<
typename Scalar>
920 return (std::is_standard_layout<Scalar>::value && std::is_trivial<Scalar>::value) ? VECTOR_SIZE : FAD_VECTOR_SIZE;
928 template<
typename ViewType>
929 KOKKOS_INLINE_FUNCTION
932 return (std::is_standard_layout<typename ViewType::value_type>::value && std::is_trivial<typename ViewType::value_type>::value) ? 0 :
get_dimension_scalar(view);
936 template<
typename Device>
939 void operator()(T* ptr) {
940 Kokkos::parallel_for(Kokkos::RangePolicy<typename Device::execution_space>(0,1),
941 KOKKOS_LAMBDA (
const int i) { ptr->~T(); });
942 typename Device::execution_space().fence();
943 Kokkos::kokkos_free<typename Device::memory_space>(ptr);
950 template<
typename Device,
typename Derived>
951 std::unique_ptr<Derived,DeviceDeleter<Device>>
954 auto* p =
static_cast<Derived*
>(Kokkos::kokkos_malloc<typename Device::memory_space>(
sizeof(Derived)));
955 Kokkos::parallel_for(Kokkos::RangePolicy<typename Device::execution_space>(0,1),
956 KOKKOS_LAMBDA (
const int i) {
new (p) Derived(host_source); });
957 typename Device::execution_space().fence();
958 return std::unique_ptr<Derived,DeviceDeleter<Device>>(p);
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,...
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...