Stokhos Package Browser (Single Doxygen Collection)  Version of the Day
Kokkos_View_Utils.hpp
Go to the documentation of this file.
1 // @HEADER
2 // ***********************************************************************
3 //
4 // Stokhos Package
5 // Copyright (2009) Sandia Corporation
6 //
7 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
8 // license for use of this work by or on behalf of the U.S. Government.
9 //
10 // Redistribution and use in source and binary forms, with or without
11 // modification, are permitted provided that the following conditions are
12 // met:
13 //
14 // 1. Redistributions of source code must retain the above copyright
15 // notice, this list of conditions and the following disclaimer.
16 //
17 // 2. Redistributions in binary form must reproduce the above copyright
18 // notice, this list of conditions and the following disclaimer in the
19 // documentation and/or other materials provided with the distribution.
20 //
21 // 3. Neither the name of the Corporation nor the names of the
22 // contributors may be used to endorse or promote products derived from
23 // this software without specific prior written permission.
24 //
25 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
26 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
29 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 //
37 // Questions? Contact Eric T. Phipps (etphipp@sandia.gov).
38 //
39 // ***********************************************************************
40 // @HEADER
41 
42 #ifndef KOKKOS_VIEW_UTILS_HPP
43 #define KOKKOS_VIEW_UTILS_HPP
44 
45 #include <stdexcept>
46 
47 // We are hooking into Kokkos Core internals here
48 // Need to define this macro since we include non-public headers
49 #ifndef KOKKOS_IMPL_PUBLIC_INCLUDE
50 #define KOKKOS_IMPL_PUBLIC_INCLUDE
51 #define KOKKOS_IMPL_PUBLIC_INCLUDE_NOTDEFINED_CORE
52 #endif
53 #include "Kokkos_View.hpp"
54 #ifdef KOKKOS_IMPL_PUBLIC_INCLUDE_NOTDEFINED_CORE
55 #undef KOKKOS_IMPL_PUBLIC_INCLUDE
56 #undef KOKKOS_IMPL_PUBLIC_INCLUDE_NOTDEFINED_CORE
57 #endif
58 
59 namespace Kokkos {
60 
61 namespace Impl {
62 
63 KOKKOS_INLINE_FUNCTION
64 void raise_error(const char *msg)
65 {
66  KOKKOS_IF_ON_HOST(throw std::runtime_error(msg);)
67 
68  KOKKOS_IF_ON_DEVICE(Kokkos::abort(msg);)
69 }
70 
71 template< class T , class Device > struct RebindStokhosStorageDevice ;
72 
73 template< class T , class Device >
74 struct RebindStokhosStorageDevice< T * , Device >
75 {
77 };
78 
79 template< class T , class Device >
80 struct RebindStokhosStorageDevice< T [] , Device >
81 {
83 };
84 
85 template< class T , unsigned N , class Device >
86 struct RebindStokhosStorageDevice< T[N] , Device >
87 {
89 };
90 
91 // Get Sacado size from a list of dimensions
92 template <unsigned Rank> struct GetSacadoSize {};
93 template <> struct GetSacadoSize<0> {
94  KOKKOS_INLINE_FUNCTION
95  static size_t eval( const size_t n0 ,
96  const size_t n1 = 0 ,
97  const size_t n2 = 0 ,
98  const size_t n3 = 0 ,
99  const size_t n4 = 0 ,
100  const size_t n5 = 0 ,
101  const size_t n6 = 0 ,
102  const size_t n7 = 0 ) {
103  return n0;
104  }
105 
106  template <typename Layout>
107  KOKKOS_INLINE_FUNCTION
108  static size_t eval( const Layout& layout ) {
109  return layout.dimension[0];
110  }
111 };
112 template <> struct GetSacadoSize<1> {
113  KOKKOS_INLINE_FUNCTION
114  static size_t eval( const size_t n0 ,
115  const size_t n1 ,
116  const size_t n2 = 0 ,
117  const size_t n3 = 0 ,
118  const size_t n4 = 0 ,
119  const size_t n5 = 0 ,
120  const size_t n6 = 0 ,
121  const size_t n7 = 0 ) {
122  return n1;
123  }
124 
125  template <typename Layout>
126  KOKKOS_INLINE_FUNCTION
127  static size_t eval( const Layout& layout ) {
128  return layout.dimension[1];
129  }
130 };
131 template <> struct GetSacadoSize<2> {
132  KOKKOS_INLINE_FUNCTION
133  static size_t eval( const size_t n0 ,
134  const size_t n1 ,
135  const size_t n2 ,
136  const size_t n3 = 0 ,
137  const size_t n4 = 0 ,
138  const size_t n5 = 0 ,
139  const size_t n6 = 0 ,
140  const size_t n7 = 0 ) {
141  return n2;
142  }
143 
144  template <typename Layout>
145  KOKKOS_INLINE_FUNCTION
146  static size_t eval( const Layout& layout ) {
147  return layout.dimension[2];
148  }
149 };
150 template <> struct GetSacadoSize<3> {
151  KOKKOS_INLINE_FUNCTION
152  static size_t eval( const size_t n0 ,
153  const size_t n1 ,
154  const size_t n2 ,
155  const size_t n3 ,
156  const size_t n4 = 0 ,
157  const size_t n5 = 0 ,
158  const size_t n6 = 0 ,
159  const size_t n7 = 0 ) {
160  return n3;
161  }
162 
163  template <typename Layout>
164  KOKKOS_INLINE_FUNCTION
165  static size_t eval( const Layout& layout ) {
166  return layout.dimension[3];
167  }
168 };
169 template <> struct GetSacadoSize<4> {
170  KOKKOS_INLINE_FUNCTION
171  static size_t eval( const size_t n0 ,
172  const size_t n1 ,
173  const size_t n2 ,
174  const size_t n3 ,
175  const size_t n4 ,
176  const size_t n5 = 0 ,
177  const size_t n6 = 0 ,
178  const size_t n7 = 0 ) {
179  return n4;
180  }
181 
182  template <typename Layout>
183  KOKKOS_INLINE_FUNCTION
184  static size_t eval( const Layout& layout ) {
185  return layout.dimension[4];
186  }
187 };
188 template <> struct GetSacadoSize<5> {
189  KOKKOS_INLINE_FUNCTION
190  static size_t eval( const size_t n0 ,
191  const size_t n1 ,
192  const size_t n2 ,
193  const size_t n3 ,
194  const size_t n4 ,
195  const size_t n5 ,
196  const size_t n6 = 0 ,
197  const size_t n7 = 0 ) {
198  return n5;
199  }
200 
201  template <typename Layout>
202  KOKKOS_INLINE_FUNCTION
203  static size_t eval( const Layout& layout ) {
204  return layout.dimension[5];
205  }
206 };
207 template <> struct GetSacadoSize<6> {
208  KOKKOS_INLINE_FUNCTION
209  static size_t eval( const size_t n0 ,
210  const size_t n1 ,
211  const size_t n2 ,
212  const size_t n3 ,
213  const size_t n4 ,
214  const size_t n5 ,
215  const size_t n6 ,
216  const size_t n7 = 0 ) {
217  return n6;
218  }
219 
220  template <typename Layout>
221  KOKKOS_INLINE_FUNCTION
222  static size_t eval( const Layout& layout ) {
223  return layout.dimension[6];
224  }
225 };
226 template <> struct GetSacadoSize<7> {
227  KOKKOS_INLINE_FUNCTION
228  static size_t eval( const size_t n0 ,
229  const size_t n1 ,
230  const size_t n2 ,
231  const size_t n3 ,
232  const size_t n4 ,
233  const size_t n5 ,
234  const size_t n6 ,
235  const size_t n7 ) {
236  return n7;
237  }
238 
239  template <typename Layout>
240  KOKKOS_INLINE_FUNCTION
241  static size_t eval( const Layout& layout ) {
242  return layout.dimension[7];
243  }
244 };
245 
246 } // namespace Impl
247 
248 // Typename of flat array where sacado dimension is folded into neighbor
249 template <typename view_type, typename Enabled = void>
251  typedef view_type type;
252 };
253 
254 // Typename of the intrinsic scalar type in a view
255 template <typename view_type, typename Enabled = void>
257  typedef typename view_type::array_type::non_const_value_type type;
258 };
259 
260 template <typename ViewType>
261 ViewType
262 make_view(const std::string& label,
263  size_t N0 = 0, size_t N1 = 0, size_t N2 = 0, size_t N3 = 0,
264  size_t N4 = 0, size_t N5 = 0, size_t N6 = 0, size_t N7 = 0)
265 {
266  return ViewType(label, N0, N1, N2, N3, N4, N5, N6, N7);
267 }
268 
269 template <typename ViewType>
270 ViewType
271 make_view(const ViewAllocateWithoutInitializing& init,
272  size_t N0 = 0, size_t N1 = 0, size_t N2 = 0, size_t N3 = 0,
273  size_t N4 = 0, size_t N5 = 0, size_t N6 = 0, size_t N7 = 0)
274 {
275  return ViewType(init, N0, N1, N2, N3, N4, N5, N6, N7);
276 }
277 
278 template <typename ViewType>
279 ViewType
280 make_view(typename ViewType::pointer_type ptr,
281  size_t N0 = 0, size_t N1 = 0, size_t N2 = 0, size_t N3 = 0,
282  size_t N4 = 0, size_t N5 = 0, size_t N6 = 0, size_t N7 = 0)
283 {
284  return ViewType(ptr, N0, N1, N2, N3, N4, N5, N6, N7);
285 }
286 
287 template <typename ViewType>
288 ViewType
289 make_view(const std::string& label,
290  const Impl::WithoutInitializing_t& init,
291  size_t N0 = 0, size_t N1 = 0, size_t N2 = 0, size_t N3 = 0,
292  size_t N4 = 0, size_t N5 = 0, size_t N6 = 0, size_t N7 = 0)
293 {
294  return ViewType(view_alloc(label,init),
295  N0, N1, N2, N3, N4, N5, N6, N7);
296 }
297 
298 namespace Impl {
299 
300 // Specialization for deep_copy( view, view::value_type ) for Cuda
301 
302 template <class OutputView, typename Enabled = void>
304 
305 } // namespace Impl
306 
307 } // namespace Kokkos
308 
309 #endif // KOKKOS_VIEW_UTILS_HPP
static KOKKOS_INLINE_FUNCTION size_t eval(const size_t n0, const size_t n1, const size_t n2, const size_t n3, const size_t n4, const size_t n5, const size_t n6, const size_t n7)
RebindStokhosStorageDevice< T, Device >::type * type
static KOKKOS_INLINE_FUNCTION size_t eval(const size_t n0, const size_t n1, const size_t n2, const size_t n3=0, const size_t n4=0, const size_t n5=0, const size_t n6=0, const size_t n7=0)
static KOKKOS_INLINE_FUNCTION size_t eval(const size_t n0, const size_t n1, const size_t n2, const size_t n3, const size_t n4, const size_t n5, const size_t n6, const size_t n7=0)
RebindStokhosStorageDevice< T, Device >::type * type
static KOKKOS_INLINE_FUNCTION size_t eval(const size_t n0, const size_t n1, const size_t n2, const size_t n3, const size_t n4, const size_t n5=0, const size_t n6=0, const size_t n7=0)
static KOKKOS_INLINE_FUNCTION size_t eval(const size_t n0, const size_t n1=0, const size_t n2=0, const size_t n3=0, const size_t n4=0, const size_t n5=0, const size_t n6=0, const size_t n7=0)
static KOKKOS_INLINE_FUNCTION size_t eval(const Layout &layout)
KOKKOS_INLINE_FUNCTION void raise_error(const char *msg)
static KOKKOS_INLINE_FUNCTION size_t eval(const size_t n0, const size_t n1, const size_t n2, const size_t n3, const size_t n4, const size_t n5, const size_t n6=0, const size_t n7=0)
static KOKKOS_INLINE_FUNCTION size_t eval(const Layout &layout)
static KOKKOS_INLINE_FUNCTION size_t eval(const Layout &layout)
ViewType make_view(const std::string &label, size_t N0=0, size_t N1=0, size_t N2=0, size_t N3=0, size_t N4=0, size_t N5=0, size_t N6=0, size_t N7=0)
static KOKKOS_INLINE_FUNCTION size_t eval(const size_t n0, const size_t n1, const size_t n2, const size_t n3, const size_t n4=0, const size_t n5=0, const size_t n6=0, const size_t n7=0)
static KOKKOS_INLINE_FUNCTION size_t eval(const Layout &layout)
view_type::array_type::non_const_value_type type
static KOKKOS_INLINE_FUNCTION size_t eval(const size_t n0, const size_t n1, const size_t n2=0, const size_t n3=0, const size_t n4=0, const size_t n5=0, const size_t n6=0, const size_t n7=0)
static KOKKOS_INLINE_FUNCTION size_t eval(const Layout &layout)
static KOKKOS_INLINE_FUNCTION size_t eval(const Layout &layout)
static KOKKOS_INLINE_FUNCTION size_t eval(const Layout &layout)
static KOKKOS_INLINE_FUNCTION size_t eval(const Layout &layout)