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 return Sacado::dimension_scalar(view);
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);
338 template <
typename InputView>
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>;
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);
357 return Kokkos::View<
typename view_t::value_type*, Properties...>(view.data(), view.mapping().required_span_size());
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());
372 template <
class... ViewPack>
380 template <
class OutViewType,
class CtorProp,
class... Dims>
383 const CtorProp &prop,
386 #ifdef HAVE_INTREPID2_SACADO
387 using view_factory = Sacado::ViewFactory<ViewPack...>;
388 return view_factory::template create_view<OutViewType>(views..., prop, dims...);
391 return OutViewType(prop, dims...);
402 template <
typename OutViewType,
typename InViewType,
typename CtorProp,
typename... Dims>
405 const CtorProp &prop,
409 return cvf::template create_view<OutViewType>(view, prop, dims...);
417 template <
typename InViewType,
typename CtorProp,
typename... Dims>
418 typename DeduceDynRankView<InViewType>::type
420 const CtorProp &prop,
423 using OutViewType =
typename DeduceDynRankView<InViewType>::type;
424 return createMatchingView<OutViewType>(view, prop, dims...);
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*>,
440 #ifdef HAVE_INTREPID2_SACADO
441 if constexpr (Sacado::is_view_fad<InViewType>::value)
444 return OutViewType(data, dims..., derivative_dimension);
447 return OutViewType(data, dims...);
450 return OutViewType(data, dims...);
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
465 using OutViewType =
typename DeduceDynRankView<InViewType>::type;
466 return createMatchingUnmanagedView<OutViewType>(view, data, dims...);
480 template<
class ViewType,
class ... DimArgs>
482 Kokkos::DynRankView<typename ViewType::value_type, typename DeduceLayout< ViewType >::result_layout,
typename ViewType::device_type >
485 return Impl::createMatchingDynRankView(view, label, dims...);
488 using std::enable_if_t;
493 template <
typename T,
typename =
void>
499 template <
typename T>
507 template<
class Functor, ordinal_type default_value>
509 enable_if_t<has_rank_member<Functor>::value, ordinal_type>
512 return Functor::rank;
518 template<
class Functor, ordinal_type default_value>
520 enable_if_t<!has_rank_member<Functor>::value, ordinal_type>
523 return default_value;
529 template <
typename T>
533 struct two {
char x[2]; };
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(...);
539 enum { value =
sizeof(test<T>(0)) ==
sizeof(
char) && (getFixedRank<T,1>() == 1) };
545 template <
typename T>
549 struct two {
char x[2]; };
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(...);
555 enum { value =
sizeof(test<T>(0)) ==
sizeof(
char) && (getFixedRank<T,2>() == 2) };
561 template <
typename T>
565 struct two {
char x[2]; };
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(...);
571 enum { value = (
sizeof(test<T>(0)) ==
sizeof(
char)) && (getFixedRank<T,3>() == 3) };
577 template <
typename T>
581 struct two {
char x[2]; };
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(...);
587 enum { value =
sizeof(test<T>(0)) ==
sizeof(
char) && (getFixedRank<T,4>() == 4) };
593 template <
typename T>
597 struct two {
char x[2]; };
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(...);
603 enum { value =
sizeof(test<T>(0)) ==
sizeof(
char) && (getFixedRank<T,5>() == 5) };
609 template <
typename T>
613 struct two {
char x[2]; };
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(...);
619 enum { value =
sizeof(test<T>(0)) ==
sizeof(
char) && (getFixedRank<T,6>() == 6) };
625 template <
typename T>
629 struct two {
char x[2]; };
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(...);
635 enum { value =
sizeof(test<T>(0)) ==
sizeof(
char) && (getFixedRank<T,7>() == 7) };
641 template <
typename T,
int rank>
645 enum { value =
false };
651 template <
typename T>
659 template <
typename T>
667 template <
typename T>
675 template <
typename T>
683 template <
typename T>
691 template <
typename T>
699 template <
typename T>
711 template<
typename Scalar,
int rank>
719 template<
typename Scalar>
722 using value_type = Scalar;
728 template<
typename Scalar>
731 using value_type = Scalar*;
737 template<
typename Scalar>
740 using value_type = Scalar**;
746 template<
typename Scalar>
749 using value_type = Scalar***;
755 template<
typename Scalar>
758 using value_type = Scalar****;
764 template<
typename Scalar>
767 using value_type = Scalar*****;
773 template<
typename Scalar>
776 using value_type = Scalar******;
782 template<
typename Scalar>
785 using value_type = Scalar*******;
853 template <
typename T>
857 struct two {
char x[2]; };
859 template <
typename C>
static one test(
decltype( std::declval<C>().rank() ) ) ;
860 template <
typename C>
static two test(...);
863 enum { value =
sizeof(test<T>(0)) ==
sizeof(
char) };
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.");
871 template<
class Functor>
872 enable_if_t<has_rank_method<Functor>::value,
unsigned>
873 KOKKOS_INLINE_FUNCTION
876 return functor.rank();
882 template<
class Functor>
883 enable_if_t<!has_rank_method<Functor>::value,
unsigned>
884 KOKKOS_INLINE_FUNCTION
893 template <
typename ValueType>
895 using layout = Kokkos::LayoutLeft;
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;
903 const int FAD_VECTOR_SIZE = 1;
909 template<
typename Scalar>
912 return (std::is_standard_layout<Scalar>::value && std::is_trivial<Scalar>::value) ? VECTOR_SIZE : FAD_VECTOR_SIZE;
920 template<
typename ViewType>
921 KOKKOS_INLINE_FUNCTION
924 return (std::is_standard_layout<typename ViewType::value_type>::value && std::is_trivial<typename ViewType::value_type>::value) ? 0 :
get_dimension_scalar(view);
928 template<
typename Device>
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);
942 template<
typename Device,
typename Derived>
943 std::unique_ptr<Derived,DeviceDeleter<Device>>
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);
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...