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>;
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);
347 return Kokkos::View<
typename view_t::value_type*, Properties...>(view.data(), view.mapping().required_span_size());
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());
362 template <
class... ViewPack>
370 template <
class OutViewType,
class CtorProp,
class... Dims>
373 const CtorProp &prop,
376 #ifdef HAVE_INTREPID2_SACADO
377 using view_factory = Kokkos::ViewFactory<ViewPack...>;
378 return view_factory::template create_view<OutViewType>(views..., prop, dims...);
381 return OutViewType(prop, dims...);
392 template <
typename OutViewType,
typename InViewType,
typename CtorProp,
typename... Dims>
395 const CtorProp &prop,
399 return cvf::template create_view<OutViewType>(view, prop, dims...);
407 template <
typename InViewType,
typename CtorProp,
typename... Dims>
408 typename DeduceDynRankView<InViewType>::type
410 const CtorProp &prop,
413 using OutViewType =
typename DeduceDynRankView<InViewType>::type;
414 return createMatchingView<OutViewType>(view, prop, dims...);
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*>,
430 #ifdef HAVE_INTREPID2_SACADO
431 #ifdef SACADO_HAS_NEW_KOKKOS_VIEW_IMPL
432 if constexpr (Sacado::is_view_fad<InViewType>::value)
434 if constexpr (Kokkos::is_view_fad<InViewType>::value)
437 const int derivative_dimension = get_dimension_scalar(view);
438 return OutViewType(data, dims..., derivative_dimension);
441 return OutViewType(data, dims...);
444 return OutViewType(data, dims...);
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
459 using OutViewType =
typename DeduceDynRankView<InViewType>::type;
460 return createMatchingUnmanagedView<OutViewType>(view, data, dims...);
474 template<
class ViewType,
class ... DimArgs>
476 Kokkos::DynRankView<typename ViewType::value_type, typename DeduceLayout< ViewType >::result_layout,
typename ViewType::device_type >
479 return Impl::createMatchingDynRankView(view, label, dims...);
482 using std::enable_if_t;
487 template <
typename T,
typename =
void>
493 template <
typename T>
501 template<
class Functor, ordinal_type default_value>
503 enable_if_t<has_rank_member<Functor>::value, ordinal_type>
506 return Functor::rank;
512 template<
class Functor, ordinal_type default_value>
514 enable_if_t<!has_rank_member<Functor>::value, ordinal_type>
517 return default_value;
523 template <
typename T>
527 struct two {
char x[2]; };
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(...);
533 enum { value =
sizeof(test<T>(0)) ==
sizeof(
char) && (getFixedRank<T,1>() == 1) };
539 template <
typename T>
543 struct two {
char x[2]; };
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(...);
549 enum { value =
sizeof(test<T>(0)) ==
sizeof(
char) && (getFixedRank<T,2>() == 2) };
555 template <
typename T>
559 struct two {
char x[2]; };
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(...);
565 enum { value = (
sizeof(test<T>(0)) ==
sizeof(
char)) && (getFixedRank<T,3>() == 3) };
571 template <
typename T>
575 struct two {
char x[2]; };
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(...);
581 enum { value =
sizeof(test<T>(0)) ==
sizeof(
char) && (getFixedRank<T,4>() == 4) };
587 template <
typename T>
591 struct two {
char x[2]; };
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(...);
597 enum { value =
sizeof(test<T>(0)) ==
sizeof(
char) && (getFixedRank<T,5>() == 5) };
603 template <
typename T>
607 struct two {
char x[2]; };
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(...);
613 enum { value =
sizeof(test<T>(0)) ==
sizeof(
char) && (getFixedRank<T,6>() == 6) };
619 template <
typename T>
623 struct two {
char x[2]; };
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(...);
629 enum { value =
sizeof(test<T>(0)) ==
sizeof(
char) && (getFixedRank<T,7>() == 7) };
635 template <
typename T,
int rank>
639 enum { value =
false };
645 template <
typename T>
653 template <
typename T>
661 template <
typename T>
669 template <
typename T>
677 template <
typename T>
685 template <
typename T>
693 template <
typename T>
705 template<
typename Scalar,
int rank>
713 template<
typename Scalar>
716 using value_type = Scalar;
722 template<
typename Scalar>
725 using value_type = Scalar*;
731 template<
typename Scalar>
734 using value_type = Scalar**;
740 template<
typename Scalar>
743 using value_type = Scalar***;
749 template<
typename Scalar>
752 using value_type = Scalar****;
758 template<
typename Scalar>
761 using value_type = Scalar*****;
767 template<
typename Scalar>
770 using value_type = Scalar******;
776 template<
typename Scalar>
779 using value_type = Scalar*******;
847 template <
typename T>
851 struct two {
char x[2]; };
853 template <
typename C>
static one test(
decltype( std::declval<C>().rank() ) ) ;
854 template <
typename C>
static two test(...);
857 enum { value =
sizeof(test<T>(0)) ==
sizeof(
char) };
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.");
865 template<
class Functor>
866 enable_if_t<has_rank_method<Functor>::value,
unsigned>
867 KOKKOS_INLINE_FUNCTION
870 return functor.rank();
876 template<
class Functor>
877 enable_if_t<!has_rank_method<Functor>::value,
unsigned>
878 KOKKOS_INLINE_FUNCTION
887#if defined(HAVE_INTREPID2_SACADO) && !defined(SACADO_HAS_NEW_KOKKOS_VIEW_IMPL)
888 template <
typename ValueType>
889 struct NaturalLayoutForType {
891 typename std::conditional<(std::is_standard_layout<ValueType>::value && std::is_trivial<ValueType>::value),
893 Kokkos::LayoutNatural<Kokkos::LayoutLeft> >::type;
896 template <
typename ValueType>
898 using layout = Kokkos::LayoutLeft;
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;
907 const int FAD_VECTOR_SIZE = 1;
913 template<
typename Scalar>
916 return (std::is_standard_layout<Scalar>::value && std::is_trivial<Scalar>::value) ? VECTOR_SIZE : FAD_VECTOR_SIZE;
924 template<
typename ViewType>
925 KOKKOS_INLINE_FUNCTION
928 return (std::is_standard_layout<typename ViewType::value_type>::value && std::is_trivial<typename ViewType::value_type>::value) ? 0 : get_dimension_scalar(view);
932 template<
typename Device>
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);
946 template<
typename Device,
typename Derived>
947 std::unique_ptr<Derived,DeviceDeleter<Device>>
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);
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...