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 "Kokkos_View_Fad_Fwd.hpp"
29#include "Kokkos_LayoutNatural.hpp"
30#include "Kokkos_ViewFactory.hpp"
35#if defined(__CUDA_ARCH__) || defined(__HIP_DEVICE_COMPILE__) || defined(__SYCL_DEVICE_ONLY__)
36#define INTREPID2_COMPILE_DEVICE_CODE
39#if defined(KOKKOS_ENABLE_CUDA) || defined(KOKKOS_ENABLE_HIP) || defined(KOKKOS_ENABLE_SYCL)
40#define INTREPID2_ENABLE_DEVICE
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
53#define INTREPID2_TEST_FOR_WARNING(test, msg) \
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); \
60#define INTREPID2_TEST_FOR_EXCEPTION(test, x, msg) \
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); \
70#ifndef INTREPID2_ENABLE_DEVICE
71#define INTREPID2_TEST_FOR_EXCEPTION_DEVICE_SAFE(test, x, msg) \
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"; \
79#define INTREPID2_TEST_FOR_EXCEPTION_DEVICE_SAFE(test, x, msg) \
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"); \
87#if defined(INTREPID2_ENABLE_DEBUG) || defined(NDEBUG) || 1
88#define INTREPID2_TEST_FOR_ABORT(test, msg) \
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"); \
96#define INTREPID2_TEST_FOR_ABORT(test, msg) ((void)0)
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); \
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); \
114 Kokkos::abort( "[Intrepid2] Abort\n"); \
123 typedef typename T::scalar_type scalar_type;
132 typedef float scalar_type;
139 typedef double scalar_type;
146 typedef int scalar_type;
153 typedef long int scalar_type;
160 typedef long long scalar_type;
168 template<
typename ViewSpaceType,
typename UserSpaceType>
170 typedef UserSpaceType ExecSpaceType;
176 template<
typename ViewSpaceType>
178 typedef ViewSpaceType ExecSpaceType;
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,
193 input_layout >::type;
202 template<
typename IdxType,
typename DimType,
typename IterType>
203 KOKKOS_FORCEINLINE_FUNCTION
205 unrollIndex(IdxType &i, IdxType &j,
208 const IterType iter) {
218 template<
typename IdxType,
typename DimType,
typename IterType>
219 KOKKOS_FORCEINLINE_FUNCTION
221 unrollIndex(IdxType &i, IdxType &j, IdxType &k,
225 const IterType iter) {
231 unrollIndex( i, tmp, dim0, dim1*dim2, iter);
232 unrollIndex( j, k, dim1, dim2, tmp);
241 KOKKOS_FORCEINLINE_FUNCTION
242 static T min(
const T a,
const T b) {
243 return (a < b ? a : b);
246 KOKKOS_FORCEINLINE_FUNCTION
247 static T max(
const T a,
const T b) {
248 return (a > b ? a : b);
251 KOKKOS_FORCEINLINE_FUNCTION
252 static T abs(
const T a) {
253 return (a > 0 ? a : T(-a));
259 KOKKOS_FORCEINLINE_FUNCTION
260 static T min(
const T &a,
const T &b) {
261 return (a < b ? a : b);
265 KOKKOS_FORCEINLINE_FUNCTION
266 static T max(
const T &a,
const T &b) {
267 return (a > b ? a : b);
271 KOKKOS_FORCEINLINE_FUNCTION
272 static T abs(
const T &a) {
273 return (a > 0 ? a : T(-a));
284 KOKKOS_FORCEINLINE_FUNCTION
286 std::enable_if< !(std::is_standard_layout<T>::value && std::is_trivial<T>::value),
typename ScalarTraits<T>::scalar_type >::type
290 KOKKOS_FORCEINLINE_FUNCTION
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;}
302 template<
typename T,
typename ...P>
303 KOKKOS_INLINE_FUNCTION
305 std::enable_if< std::is_standard_layout<T>::value && std::is_trivial<T>::value,
unsigned >::type
308 template<
typename T,
typename ...P>
309 KOKKOS_INLINE_FUNCTION
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...> ) {
return 1;}
314 template<
typename T,
typename ...P>
315 KOKKOS_FORCEINLINE_FUNCTION
316 static ordinal_type get_dimension_scalar(
const Kokkos::DynRankView<T, P...> &view) {
320 template<
typename T,
typename ...P>
321 KOKKOS_FORCEINLINE_FUNCTION
322 static ordinal_type get_dimension_scalar(
const Kokkos::View<T, P...> &view) {
328 template <
typename InputView>
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>;
343 template <
class... ViewPack>
351 template <
class OutViewType,
class CtorProp,
class... Dims>
354 const CtorProp &prop,
357 #ifdef HAVE_INTREPID2_SACADO
358 using view_factory = Kokkos::ViewFactory<ViewPack...>;
359 return view_factory::template create_view<OutViewType>(views..., prop, dims...);
362 return OutViewType(prop, dims...);
373 template <
typename OutViewType,
typename InViewType,
typename CtorProp,
typename... Dims>
376 const CtorProp &prop,
380 return cvf::template create_view<OutViewType>(view, prop, dims...);
388 template <
typename InViewType,
typename CtorProp,
typename... Dims>
389 typename DeduceDynRankView<InViewType>::type
391 const CtorProp &prop,
394 using OutViewType =
typename DeduceDynRankView<InViewType>::type;
395 return createMatchingView<OutViewType>(view, prop, dims...);
404 template <
typename OutViewType,
typename InViewType,
typename CtorProp,
typename... Dims>
405 KOKKOS_INLINE_FUNCTION
406 typename std::enable_if<
407 std::is_pointer_v<CtorProp> && !std::is_convertible_v<CtorProp, const char*>,
411 #ifdef HAVE_INTREPID2_SACADO
412 #ifdef SACADO_HAS_NEW_KOKKOS_VIEW_IMPL
413 if constexpr (Sacado::is_view_fad<InViewType>::value)
415 if constexpr (Kokkos::is_view_fad<InViewType>::value)
418 const int derivative_dimension = get_dimension_scalar(view);
419 return OutViewType(data, dims..., derivative_dimension);
422 return OutViewType(data, dims...);
425 return OutViewType(data, dims...);
434 template <
typename InViewType,
typename CtorProp,
typename ... Dims>
435 KOKKOS_INLINE_FUNCTION
436 typename std::enable_if<
437 std::is_pointer_v<CtorProp> && !std::is_convertible_v<CtorProp, const char*>,
438 typename DeduceDynRankView<InViewType>::type>::type
440 using OutViewType =
typename DeduceDynRankView<InViewType>::type;
441 return createMatchingUnmanagedView<OutViewType>(view, data, dims...);
455 template<
class ViewType,
class ... DimArgs>
457 Kokkos::DynRankView<typename ViewType::value_type, typename DeduceLayout< ViewType >::result_layout,
typename ViewType::device_type >
460 return Impl::createMatchingDynRankView(view, label, dims...);
463 using std::enable_if_t;
468 template <
typename T,
typename =
void>
474 template <
typename T>
482 template<
class Functor, ordinal_type default_value>
484 enable_if_t<has_rank_member<Functor>::value, ordinal_type>
487 return Functor::rank;
493 template<
class Functor, ordinal_type default_value>
495 enable_if_t<!has_rank_member<Functor>::value, ordinal_type>
498 return default_value;
504 template <
typename T>
508 struct two {
char x[2]; };
510 template <
typename C>
static one test(
typename std::remove_reference<
decltype( std::declval<C>().
operator()(0))>::type );
511 template <
typename C>
static two test(...);
514 enum { value =
sizeof(test<T>(0)) ==
sizeof(
char) && (getFixedRank<T,1>() == 1) };
520 template <
typename T>
524 struct two {
char x[2]; };
526 template <
typename C>
static one test(
typename std::remove_reference<
decltype( std::declval<C>().
operator()(0,0))>::type ) ;
527 template <
typename C>
static two test(...);
530 enum { value =
sizeof(test<T>(0)) ==
sizeof(
char) && (getFixedRank<T,2>() == 2) };
536 template <
typename T>
540 struct two {
char x[2]; };
542 template <
typename C>
static one test(
typename std::remove_reference<
decltype( std::declval<C>().
operator()(0,0,0))>::type ) ;
543 template <
typename C>
static two test(...);
546 enum { value = (
sizeof(test<T>(0)) ==
sizeof(
char)) && (getFixedRank<T,3>() == 3) };
552 template <
typename T>
556 struct two {
char x[2]; };
558 template <
typename C>
static one test(
typename std::remove_reference<
decltype( std::declval<C>().
operator()(0,0,0,0))>::type ) ;
559 template <
typename C>
static two test(...);
562 enum { value =
sizeof(test<T>(0)) ==
sizeof(
char) && (getFixedRank<T,4>() == 4) };
568 template <
typename T>
572 struct two {
char x[2]; };
574 template <
typename C>
static one test(
typename std::remove_reference<
decltype( std::declval<C>().
operator()(0,0,0,0,0))>::type ) ;
575 template <
typename C>
static two test(...);
578 enum { value =
sizeof(test<T>(0)) ==
sizeof(
char) && (getFixedRank<T,5>() == 5) };
584 template <
typename T>
588 struct two {
char x[2]; };
590 template <
typename C>
static one test(
typename std::remove_reference<
decltype( std::declval<C>().
operator()(0,0,0,0,0,0))>::type ) ;
591 template <
typename C>
static two test(...);
594 enum { value =
sizeof(test<T>(0)) ==
sizeof(
char) && (getFixedRank<T,6>() == 6) };
600 template <
typename T>
604 struct two {
char x[2]; };
606 template <
typename C>
static one test(
typename std::remove_reference<
decltype( std::declval<C>().
operator()(0,0,0,0,0,0,0))>::type ) ;
607 template <
typename C>
static two test(...);
610 enum { value =
sizeof(test<T>(0)) ==
sizeof(
char) && (getFixedRank<T,7>() == 7) };
616 template <
typename T,
int rank>
620 enum { value =
false };
626 template <
typename T>
634 template <
typename T>
642 template <
typename T>
650 template <
typename T>
658 template <
typename T>
666 template <
typename T>
674 template <
typename T>
686 template<
typename Scalar,
int rank>
694 template<
typename Scalar>
697 using value_type = Scalar;
703 template<
typename Scalar>
706 using value_type = Scalar*;
712 template<
typename Scalar>
715 using value_type = Scalar**;
721 template<
typename Scalar>
724 using value_type = Scalar***;
730 template<
typename Scalar>
733 using value_type = Scalar****;
739 template<
typename Scalar>
742 using value_type = Scalar*****;
748 template<
typename Scalar>
751 using value_type = Scalar******;
757 template<
typename Scalar>
760 using value_type = Scalar*******;
828 template <
typename T>
832 struct two {
char x[2]; };
834 template <
typename C>
static one test(
decltype( std::declval<C>().rank() ) ) ;
835 template <
typename C>
static two test(...);
838 enum { value =
sizeof(test<T>(0)) ==
sizeof(
char) };
841 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.");
846 template<
class Functor>
847 enable_if_t<has_rank_method<Functor>::value,
unsigned>
848 KOKKOS_INLINE_FUNCTION
851 return functor.rank();
857 template<
class Functor>
858 enable_if_t<!has_rank_method<Functor>::value,
unsigned>
859 KOKKOS_INLINE_FUNCTION
868#if defined(HAVE_INTREPID2_SACADO) && !defined(SACADO_HAS_NEW_KOKKOS_VIEW_IMPL)
869 template <
typename ValueType>
870 struct NaturalLayoutForType {
872 typename std::conditional<(std::is_standard_layout<ValueType>::value && std::is_trivial<ValueType>::value),
874 Kokkos::LayoutNatural<Kokkos::LayoutLeft> >::type;
877 template <
typename ValueType>
879 using layout = Kokkos::LayoutLeft;
884 const int VECTOR_SIZE = 1;
885#if defined(SACADO_VIEW_CUDA_HIERARCHICAL_DFAD) && defined(INTREPID2_ENABLE_DEVICE)
886 const int FAD_VECTOR_SIZE = 32;
888 const int FAD_VECTOR_SIZE = 1;
894 template<
typename Scalar>
897 return (std::is_standard_layout<Scalar>::value && std::is_trivial<Scalar>::value) ? VECTOR_SIZE : FAD_VECTOR_SIZE;
905 template<
typename ViewType>
906 KOKKOS_INLINE_FUNCTION
909 return (std::is_standard_layout<typename ViewType::value_type>::value && std::is_trivial<typename ViewType::value_type>::value) ? 0 : get_dimension_scalar(view);
913 template<
typename Device>
916 void operator()(T* ptr) {
917 Kokkos::parallel_for(Kokkos::RangePolicy<typename Device::execution_space>(0,1),
918 KOKKOS_LAMBDA (
const int i) { ptr->~T(); });
919 typename Device::execution_space().fence();
920 Kokkos::kokkos_free<typename Device::memory_space>(ptr);
927 template<
typename Device,
typename Derived>
928 std::unique_ptr<Derived,DeviceDeleter<Device>>
931 auto* p =
static_cast<Derived*
>(Kokkos::kokkos_malloc<typename Device::memory_space>(
sizeof(Derived)));
932 Kokkos::parallel_for(Kokkos::RangePolicy<typename Device::execution_space>(0,1),
933 KOKKOS_LAMBDA (
const int i) {
new (p) Derived(host_source); });
934 typename Device::execution_space().fence();
935 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...
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,...
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...