Sacado Package Browser (Single Doxygen Collection)  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Kokkos_ViewFactory.hpp
Go to the documentation of this file.
1 // @HEADER
2 // *****************************************************************************
3 // Sacado Package
4 //
5 // Copyright 2006 NTESS and the Sacado contributors.
6 // SPDX-License-Identifier: LGPL-2.1-or-later
7 // *****************************************************************************
8 // @HEADER
9 
10 #ifndef KOKKOS_VIEW_FACTORY_HPP
11 #define KOKKOS_VIEW_FACTORY_HPP
12 
13 #include <type_traits>
14 
15 #include "Sacado_Traits.hpp"
16 #include "KokkosExp_View_Fad.hpp"
18 
19 namespace Kokkos {
20 
21 namespace Impl {
22 
23 // Class to determine the value_type for a view as a function of one or more
24 // input views
25 template <class ... ViewPack>
26 struct ViewFactoryType {};
27 
28 template <class View>
30  typedef typename View::value_type type;
31 };
32 
33 template <class View, class ... ViewPack>
34 struct ViewFactoryType<View,ViewPack...> {
35  typedef typename Sacado::Promote<
36  typename View::value_type,
37  typename ViewFactoryType<ViewPack...>::type
39 };
40 
41 }
42 
43 // Function to compute the scalar dimension (e.g., Fad dimesion) from one or
44 // more views. It relies on the overload for a single view provided by Sacado
45 
46 // Traits class used to create a view for a given rank and dimension as a
47 // function of one or more views. The value_type for the view is determined
48 // by value_type, and the view is created through the create_view() function.
49 // The calling code must determine the rank and dimensions of the view to create
50 // however internal Sacado dimension will be determined automatically.
51 template <class ... ViewPack>
52 struct ViewFactory {
53 
54  typedef typename Impl::ViewFactoryType<ViewPack...>::type value_type;
55 
56  template <class ResultView, class CtorProp, class ... Dims>
57  static ResultView
58  create_view(const ViewPack& ... views,
59  const CtorProp& prop,
60  const Dims ... dims) {
61 
62  using nc_value_type = typename ResultView::non_const_value_type;
63  constexpr bool is_scalar = Sacado::IsScalarType<nc_value_type>::value;
64  constexpr bool is_dyn_rank = is_dyn_rank_view<ResultView>::value;
65 
66  // rank == number of arguments
67  constexpr unsigned rank = sizeof...(Dims);
68 
69  // Check rank is valid
70  static_assert( rank <= 7, "Invalid rank...too many dimension arguments" );
71 
72  // Create layout from our dimension arguments
73  typename ResultView::array_layout layout(dims...);
74 
75  // Set scalar dimension
76  layout.dimension[rank] = dimension_scalar(views...);
77 
78  // Handle the case where all of the input view's are scalar's, but the
79  // result isn't (e.g., a Fad), in which case we have to specify a valid
80  // scalar dimension
81  if (!is_scalar && layout.dimension[rank] == 0)
82  layout.dimension[rank] = 1;
83 
84  // Reconstruct layout for dynamic rank
85  if (is_dyn_rank) {
86  constexpr unsigned r = is_scalar ? rank : rank + 1;
87  layout = Impl::reconstructLayout(layout, r);
88  }
89 
90  return ResultView(prop, layout);
91  }
92 
93 };
94 
96 template <typename ResultViewType, typename InputViewType, typename CtorProp,
97  typename ... Dims>
98 typename std::enable_if<
100  ResultViewType>::type
101 createDynRankViewWithType(const InputViewType& a,
102  const CtorProp& prop,
103  const Dims... dims)
104 {
105  using view_factory = Kokkos::ViewFactory<InputViewType>;
106  return view_factory::template create_view<ResultViewType>(a,prop,dims...);
107 }
108 
109 namespace Impl {
110  // Helper type trait to determine type of resulting DynRankView from
111  // createDynRankView below
112  template <typename InputView>
114  // Allow for use of LayoutStride in InputViewType. We don't want to create
115  // a new view with LayoutStride, so replace it with the default layout
116  // instead.
117  using input_value = typename InputView::non_const_value_type;
118  using input_layout = typename InputView::array_layout;
119  using input_device = typename InputView::device_type;
120  using default_layout = typename input_device::execution_space::array_layout;
121  using result_layout =
122  typename std::conditional<
126  using type =
127  Kokkos::DynRankView<input_value, result_layout, input_device>;
128  };
129 
130 }
131 
133 template <typename InputViewType, typename CtorProp, typename ... Dims >
134 typename std::enable_if<
137  >::type
138 createDynRankView(const InputViewType& a,
139  const CtorProp& prop,
140  const Dims... dims)
141 {
142  using ResultViewType = typename Impl::ResultDynRankView<InputViewType>::type;
143  return createDynRankViewWithType<ResultViewType>(a, prop, dims...);
144 }
145 
147 template <typename ResultViewType, typename InputViewType, typename CtorProp,
148  typename ... Dims>
149 typename std::enable_if<
151  ResultViewType>::type
152 createViewWithType(const InputViewType& a,
153  const CtorProp& prop,
154  const Dims... dims)
155 {
156  using view_factory = Kokkos::ViewFactory<InputViewType>;
157  return view_factory::template create_view<ResultViewType>(a,prop,dims...);
158 }
159 
160 }
161 
162 #endif /* #ifndef KOKKOS_VIEW_FACTORY_HPP */
typename InputView::device_type input_device
std::enable_if< is_view< InputViewType >::value||is_dyn_rank_view< InputViewType >::value, typename Impl::ResultDynRankView< InputViewType >::type >::type createDynRankView(const InputViewType &a, const CtorProp &prop, const Dims...dims)
Wrapper to simplify use of Sacado ViewFactory.
typename std::conditional< std::is_same< input_layout, Kokkos::LayoutStride >::value, default_layout, input_layout >::type result_layout
Impl::ViewFactoryType< ViewPack...>::type value_type
typename input_device::execution_space::array_layout default_layout
Sacado::Promote< typename View::value_type, typename ViewFactoryType< ViewPack...>::type >::type type
std::enable_if< is_view< InputViewType >::value||is_dyn_rank_view< InputViewType >::value, ResultViewType >::type createDynRankViewWithType(const InputViewType &a, const CtorProp &prop, const Dims...dims)
Wrapper to simplify use of Sacado ViewFactory.
Kokkos::DynRankView< input_value, result_layout, input_device > type
typename InputView::array_layout input_layout
std::enable_if< is_view< InputViewType >::value||is_dyn_rank_view< InputViewType >::value, ResultViewType >::type createViewWithType(const InputViewType &a, const CtorProp &prop, const Dims...dims)
Wrapper to simplify use of Sacado ViewFactory.
int value
static ResultView create_view(const ViewPack &...views, const CtorProp &prop, const Dims...dims)
typename InputView::non_const_value_type input_value
Base template specification for IsScalarType.
Base template specification for Promote.