Sacado Package Browser (Single Doxygen Collection) Version of the Day
Loading...
Searching...
No Matches
Kokkos_ViewFactory.hpp
Go to the documentation of this file.
1// @HEADER
2// ***********************************************************************
3//
4// Sacado Package
5// Copyright (2006) Sandia Corporation
6//
7// Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
8// the U.S. Government retains certain rights in this software.
9//
10// This library is free software; you can redistribute it and/or modify
11// it under the terms of the GNU Lesser General Public License as
12// published by the Free Software Foundation; either version 2.1 of the
13// License, or (at your option) any later version.
14//
15// This library is distributed in the hope that it will be useful, but
16// WITHOUT ANY WARRANTY; without even the implied warranty of
17// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18// Lesser General Public License for more details.
19//
20// You should have received a copy of the GNU Lesser General Public
21// License along with this library; if not, write to the Free Software
22// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
23// USA
24// Questions? Contact David M. Gay (dmgay@sandia.gov) or Eric T. Phipps
25// (etphipp@sandia.gov).
26//
27// ***********************************************************************
28// @HEADER
29
30#ifndef KOKKOS_VIEW_FACTORY_HPP
31#define KOKKOS_VIEW_FACTORY_HPP
32
33#include <type_traits>
34
35#include "Sacado_Traits.hpp"
38
39namespace Kokkos {
40
41namespace Impl {
42
43// Class to determine the value_type for a view as a function of one or more
44// input views
45template <class ... ViewPack>
47
48template <class View>
50 typedef typename View::value_type type;
51};
52
53template <class View, class ... ViewPack>
54struct ViewFactoryType<View,ViewPack...> {
55 typedef typename Sacado::Promote<
56 typename View::value_type,
57 typename ViewFactoryType<ViewPack...>::type
58 >::type type;
59};
60
61}
62
63// Function to compute the scalar dimension (e.g., Fad dimesion) from one or
64// more views. It relies on the overload for a single view provided by Sacado
65
66// Traits class used to create a view for a given rank and dimension as a
67// function of one or more views. The value_type for the view is determined
68// by value_type, and the view is created through the create_view() function.
69// The calling code must determine the rank and dimensions of the view to create
70// however internal Sacado dimension will be determined automatically.
71template <class ... ViewPack>
73
74 typedef typename Impl::ViewFactoryType<ViewPack...>::type value_type;
75
76 template <class ResultView, class CtorProp, class ... Dims>
77 static ResultView
78 create_view(const ViewPack& ... views,
79 const CtorProp& prop,
80 const Dims ... dims) {
81
82 using nc_value_type = typename ResultView::non_const_value_type;
83 constexpr bool is_scalar = Sacado::IsScalarType<nc_value_type>::value;
84 constexpr bool is_dyn_rank = is_dyn_rank_view<ResultView>::value;
85
86 // rank == number of arguments
87 constexpr unsigned rank = sizeof...(Dims);
88
89 // Check rank is valid
90 static_assert( rank <= 7, "Invalid rank...too many dimension arguments" );
91
92 // Create layout from our dimension arguments
93 typename ResultView::array_layout layout(dims...);
94
95 // Set scalar dimension
96 layout.dimension[rank] = dimension_scalar(views...);
97
98 // Handle the case where all of the input view's are scalar's, but the
99 // result isn't (e.g., a Fad), in which case we have to specify a valid
100 // scalar dimension
101 if (!is_scalar && layout.dimension[rank] == 0)
102 layout.dimension[rank] = 1;
103
104 // Reconstruct layout for dynamic rank
105 if (is_dyn_rank) {
106 constexpr unsigned r = is_scalar ? rank : rank + 1;
107 layout = Impl::reconstructLayout(layout, r);
108 }
109
110 return ResultView(prop, layout);
111 }
112
113};
114
116template <typename ResultViewType, typename InputViewType, typename CtorProp,
117 typename ... Dims>
118typename std::enable_if<
119 is_view<InputViewType>::value || is_dyn_rank_view<InputViewType>::value,
120 ResultViewType>::type
121createDynRankViewWithType(const InputViewType& a,
122 const CtorProp& prop,
123 const Dims... dims)
124{
125 using view_factory = Kokkos::ViewFactory<InputViewType>;
126 return view_factory::template create_view<ResultViewType>(a,prop,dims...);
127}
128
129namespace Impl {
130 // Helper type trait to determine type of resulting DynRankView from
131 // createDynRankView below
132 template <typename InputView>
134 // Allow for use of LayoutStride in InputViewType. We don't want to create
135 // a new view with LayoutStride, so replace it with the default layout
136 // instead.
137 using input_value = typename InputView::non_const_value_type;
138 using input_layout = typename InputView::array_layout;
139 using input_device = typename InputView::device_type;
140 using default_layout = typename input_device::execution_space::array_layout;
142 typename std::conditional<
143 std::is_same< input_layout, Kokkos::LayoutStride >::value,
146 using type =
147 Kokkos::DynRankView<input_value, result_layout, input_device>;
148 };
149
150}
151
153template <typename InputViewType, typename CtorProp, typename ... Dims >
154typename std::enable_if<
155 is_view<InputViewType>::value || is_dyn_rank_view<InputViewType>::value,
157 >::type
158createDynRankView(const InputViewType& a,
159 const CtorProp& prop,
160 const Dims... dims)
161{
162 using ResultViewType = typename Impl::ResultDynRankView<InputViewType>::type;
163 return createDynRankViewWithType<ResultViewType>(a, prop, dims...);
164}
165
167template <typename ResultViewType, typename InputViewType, typename CtorProp,
168 typename ... Dims>
169typename std::enable_if<
170 is_view<InputViewType>::value || is_dyn_rank_view<InputViewType>::value,
171 ResultViewType>::type
172createViewWithType(const InputViewType& a,
173 const CtorProp& prop,
174 const Dims... dims)
175{
176 using view_factory = Kokkos::ViewFactory<InputViewType>;
177 return view_factory::template create_view<ResultViewType>(a,prop,dims...);
178}
179
180}
181
182#endif /* #ifndef KOKKOS_VIEW_FACTORY_HPP */
View
std::enable_if< is_view< InputViewType >::value||is_dyn_rank_view< InputViewType >::value, typenameImpl::ResultDynRankView< InputViewType >::type >::type createDynRankView(const InputViewType &a, const CtorProp &prop, const Dims... dims)
Wrapper to simplify use of Sacado ViewFactory.
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.
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.
typename InputView::non_const_value_type input_value
Kokkos::DynRankView< input_value, result_layout, input_device > type
typename std::conditional< std::is_same< input_layout, Kokkos::LayoutStride >::value, default_layout, input_layout >::type result_layout
typename InputView::device_type input_device
typename InputView::array_layout input_layout
typename input_device::execution_space::array_layout default_layout
Sacado::Promote< typenameView::value_type, typenameViewFactoryType< ViewPack... >::type >::type type
Impl::ViewFactoryType< ViewPack... >::type value_type
static ResultView create_view(const ViewPack &... views, const CtorProp &prop, const Dims ... dims)
Base template specification for IsScalarType.
Base template specification for Promote.