Debian Codegraph @

packages/stokhos/src/sacado/kokkos/pce/KokkosExp_View_UQ_PCE_Contiguous.hpp

raw
1 // @HEADER
2 // *****************************************************************************
3 // Stokhos Package
4 //
5 // Copyright 2009 NTESS and the Stokhos contributors.
6 // SPDX-License-Identifier: BSD-3-Clause
7 // *****************************************************************************
8 // @HEADER
9
10 #ifndef KOKKOS_EXPERIMENTAL_VIEW_UQ_PCE_CONTIGUOUS_HPP
11 #define KOKKOS_EXPERIMENTAL_VIEW_UQ_PCE_CONTIGUOUS_HPP
12
13 // Only include forward declarations so any overloads appear before they
14 // might be used inside Kokkos
15 #include "Kokkos_View_UQ_PCE_Fwd.hpp"
16
17 // We are hooking into Kokkos Core internals here
18 // Need to define this macro since we include non-public headers
19 #ifndef KOKKOS_IMPL_PUBLIC_INCLUDE
20 #define KOKKOS_IMPL_PUBLIC_INCLUDE
21 #define KOKKOS_IMPL_PUBLIC_INCLUDE_NOTDEFINED_CORE
22 #endif
23 #include "Kokkos_Layout.hpp"
24 #ifdef KOKKOS_IMPL_PUBLIC_INCLUDE_NOTDEFINED_CORE
25 #undef KOKKOS_IMPL_PUBLIC_INCLUDE
26 #undef KOKKOS_IMPL_PUBLIC_INCLUDE_NOTDEFINED_CORE
27 #endif
28
29 #include "Kokkos_AnalyzeStokhosShape.hpp"
30 #include "Kokkos_View_Utils.hpp"
31 #include "Kokkos_View_UQ_PCE_Utils.hpp"
32
33
34 //----------------------------------------------------------------------------
35
36 namespace Kokkos {
37 namespace Experimental {
38 namespace Impl {
39
40 struct ViewPCEContiguous {};
41
42 template< class ... Args >
43 struct is_ViewPCEContiguous { enum { value = false }; };
44
45 template< class D , class ... P , class ... Args >
46 struct is_ViewPCEContiguous< Kokkos::View<D,P...> , Args... > {
47 enum { value =
48 std::is_same< typename Kokkos::ViewTraits<D,P...>::specialize
49 , ViewPCEContiguous >::value
50 &&
51 ( ( sizeof...(Args) == 0 ) ||
52 is_ViewPCEContiguous< Args... >::value ) };
53 };
54
55 } // namespace Impl
56 } // namespace Experimental
57 } // namespace Kokkos
58
59 namespace Kokkos {
60
61 template <typename T, typename ... P>
62 struct is_view_uq_pce< View<T,P...> > {
63 typedef View<T,P...> view_type;
64 static const bool value =
65 std::is_same< typename view_type::specialize,
66 Experimental::Impl::ViewPCEContiguous >::value;
67 };
68
69 template <typename ViewType>
70 struct CijkType< ViewType,
71 typename std::enable_if< is_view_uq_pce< ViewType >::value >::type > {
72 typedef typename ViewType::non_const_value_type::cijk_type type;
73 };
74
75 template <typename T, typename ... P>
76 KOKKOS_INLINE_FUNCTION
77 constexpr typename
78 std::enable_if< is_view_uq_pce< View<T,P...> >::value, unsigned >::type
79 dimension_scalar(const View<T,P...>& view) {
80 return view.impl_map().dimension_scalar();
81 }
82
83 template <typename view_type>
84 KOKKOS_INLINE_FUNCTION
85 constexpr typename
86 std::enable_if< is_view_uq_pce<view_type>::value,
87 typename CijkType<view_type>::type >::type
88 cijk(const view_type& view) {
89 return view.impl_map().cijk();
90 }
91
92 template <typename view_type>
93 KOKKOS_INLINE_FUNCTION
94 constexpr typename
95 std::enable_if< is_view_uq_pce<view_type>::value, bool >::type
96 is_allocation_contiguous(const view_type& view) {
97 return view.impl_map().is_allocation_contiguous();
98 }
99
100 template <typename ViewType>
101 ViewType
102 make_view(const std::string& label,
103 const typename CijkType<ViewType>::type& cijk,
104 size_t N0 = 0, size_t N1 = 0, size_t N2 = 0, size_t N3 = 0,
105 size_t N4 = 0, size_t N5 = 0, size_t N6 = 0, size_t N7 = 0)
106 {
107 return ViewType(view_alloc(label,cijk),
108 N0, N1, N2, N3, N4, N5, N6, N7);
109 }
110
111 template <typename ViewType>
112 ViewType
113 make_view(const std::string& label,
114 const Impl::WithoutInitializing_t& init,
115 const typename CijkType<ViewType>::type& cijk,
116 size_t N0 = 0, size_t N1 = 0, size_t N2 = 0, size_t N3 = 0,
117 size_t N4 = 0, size_t N5 = 0, size_t N6 = 0, size_t N7 = 0)
118 {
119 return ViewType(view_alloc(label,init,cijk),
120 N0, N1, N2, N3, N4, N5, N6, N7);
121 }
122
123 template <typename ViewType>
124 ViewType
125 make_view(const ViewAllocateWithoutInitializing& init,
126 const typename CijkType<ViewType>::type& cijk,
127 size_t N0 = 0, size_t N1 = 0, size_t N2 = 0, size_t N3 = 0,
128 size_t N4 = 0, size_t N5 = 0, size_t N6 = 0, size_t N7 = 0)
129 {
130 return ViewType(view_alloc(((Kokkos::Impl::ViewCtorProp<void, std::string>)init).value,
131 WithoutInitializing,
132 cijk),
133 N0, N1, N2, N3, N4, N5, N6, N7);
134 }
135
136 template <typename ViewType>
137 typename std::enable_if< is_view_uq_pce<ViewType>::value, ViewType>::type
138 make_view(typename ViewType::pointer_type ptr,
139 const typename CijkType<ViewType>::type& cijk,
140 size_t N0 = 0, size_t N1 = 0, size_t N2 = 0, size_t N3 = 0,
141 size_t N4 = 0, size_t N5 = 0, size_t N6 = 0, size_t N7 = 0)
142 {
143 size_t N[8] = { N0, N1, N2, N3, N4, N5, N6, N7 };
144 N[ViewType::rank] = cijk.dimension();
145 ViewType v(view_wrap(ptr, cijk),
146 N[0], N[1], N[2], N[3], N[4], N[5], N[6], N[7]);
147 return v;
148 }
149
150 } // namespace Kokkos
151
152 #include "Sacado_Traits.hpp"
153 #include "Sacado_UQ_PCE.hpp"
154 #include "Sacado_UQ_PCE_Traits.hpp"
155 #include "Kokkos_Core.hpp"
156
157 namespace Kokkos {
158
159 template <typename D, typename ... P>
160 struct FlatArrayType< View<D,P...>,
161 typename std::enable_if< is_view_uq_pce< View<D,P...> >::value >::type > {
162 typedef View<D,P...> view_type;
163 typedef typename view_type::traits::dimension dimension;
164 typedef typename view_type::array_type::value_type flat_value_type;
165 typedef typename Kokkos::Impl::ViewDataType< flat_value_type , dimension >::type flat_data_type;
166 typedef View<flat_data_type,P...> type;
167 };
168
169 template< class T , class ... P >
170 inline
171 typename std::enable_if<
172 std::is_same< typename ViewTraits<T,P...>::specialize ,
173 Kokkos::Experimental::Impl::ViewPCEContiguous >::value &&
174 !std::is_same< typename Kokkos::ViewTraits<T,P...>::array_layout,
175 Kokkos::LayoutStride >::value,
176 typename Kokkos::View<T,P...>::HostMirror>::type
177 create_mirror(const Kokkos::View<T,P...> & src)
178 {
179 typedef View<T,P...> src_type ;
180 typedef typename src_type::HostMirror dst_type ;
181
182 typename src_type::array_layout layout = src.layout();
183 layout.dimension[src_type::rank] = Kokkos::dimension_scalar(src);
184
185 return dst_type(view_alloc(std::string(src.label()).append("_mirror"),
186 src.impl_map().cijk()), layout);
187 }
188
189 template< class T , class ... P >
190 inline
191 typename std::enable_if<
192 std::is_same< typename ViewTraits<T,P...>::specialize ,
193 Kokkos::Experimental::Impl::ViewPCEContiguous >::value &&
194 std::is_same< typename Kokkos::ViewTraits<T,P...>::array_layout,
195 Kokkos::LayoutStride >::value,
196 typename Kokkos::View<T,P...>::HostMirror>::type
197 create_mirror(const Kokkos::View<T,P...> & src)
198 {
199 typedef View<T,P...> src_type ;
200 typedef typename src_type::HostMirror dst_type ;
201
202 Kokkos::LayoutStride layout ;
203
204 layout.dimension[0] = src.extent(0);
205 layout.dimension[1] = src.extent(1);
206 layout.dimension[2] = src.extent(2);
207 layout.dimension[3] = src.extent(3);
208 layout.dimension[4] = src.extent(4);
209 layout.dimension[5] = src.extent(5);
210 layout.dimension[6] = src.extent(6);
211 layout.dimension[7] = src.extent(7);
212
213 layout.stride[0] = src.stride_0();
214 layout.stride[1] = src.stride_1();
215 layout.stride[2] = src.stride_2();
216 layout.stride[3] = src.stride_3();
217 layout.stride[4] = src.stride_4();
218 layout.stride[5] = src.stride_5();
219 layout.stride[6] = src.stride_6();
220 layout.stride[7] = src.stride_7();
221
222 layout.dimension[src_type::rank] = Kokkos::dimension_scalar(src);
223
224 return dst_type(view_alloc(std::string(src.label()).append("_mirror"),
225 src.impl_map().cijk()), layout);
226 }
227
228 template<class Space, class T, class ... P, typename Enabled>
229 typename std::enable_if<
230 std::is_same< typename ViewTraits<T,P...>::specialize ,
231 Kokkos::Experimental::Impl::ViewPCEContiguous >::value,
232 typename Impl::MirrorViewType<Space,T,P ...>::dest_view_type>::type
233 create_mirror(const Space& , const Kokkos::View<T,P...> & src)
234 {
235 typedef View<T,P...> src_type ;
236 typename src_type::array_layout layout = src.layout();
237 layout.dimension[src_type::rank] = Kokkos::dimension_scalar(src);
238 return typename Impl::MirrorViewType<Space,T,P ...>::dest_view_type(
239 view_alloc(src.label(), src.impl_map().cijk()),layout);
240 }
241
242 template< class T , class ... P >
243 inline
244 typename std::enable_if<
245 std::is_same< typename ViewTraits<T,P...>::specialize ,
246 Kokkos::Experimental::Impl::ViewPCEContiguous >::value &&
247 !std::is_same< typename Kokkos::ViewTraits<T,P...>::array_layout,
248 Kokkos::LayoutStride >::value,
249 typename Kokkos::View<T,P...>::HostMirror>::type
250 create_mirror(Kokkos::Impl::WithoutInitializing_t wi,
251 const Kokkos::View<T,P...> & src)
252 {
253 typedef View<T,P...> src_type ;
254 typedef typename src_type::HostMirror dst_type ;
255
256 typename src_type::array_layout layout = src.layout();
257 layout.dimension[src_type::rank] = Kokkos::dimension_scalar(src);
258
259 return dst_type(view_alloc(std::string(src.label()).append("_mirror"), wi,
260 src.impl_map().cijk()), layout);
261 }
262
263 template< class T , class ... P >
264 inline
265 typename std::enable_if<
266 std::is_same< typename ViewTraits<T,P...>::specialize ,
267 Kokkos::Experimental::Impl::ViewPCEContiguous >::value &&
268 std::is_same< typename Kokkos::ViewTraits<T,P...>::array_layout,
269 Kokkos::LayoutStride >::value,
270 typename Kokkos::View<T,P...>::HostMirror>::type
271 create_mirror(Kokkos::Impl::WithoutInitializing_t wi,
272 const Kokkos::View<T,P...> & src)
273 {
274 typedef View<T,P...> src_type ;
275 typedef typename src_type::HostMirror dst_type ;
276
277 Kokkos::LayoutStride layout ;
278
279 layout.dimension[0] = src.extent(0);
280 layout.dimension[1] = src.extent(1);
281 layout.dimension[2] = src.extent(2);
282 layout.dimension[3] = src.extent(3);
283 layout.dimension[4] = src.extent(4);
284 layout.dimension[5] = src.extent(5);
285 layout.dimension[6] = src.extent(6);
286 layout.dimension[7] = src.extent(7);
287
288 layout.stride[0] = src.stride_0();
289 layout.stride[1] = src.stride_1();
290 layout.stride[2] = src.stride_2();
291 layout.stride[3] = src.stride_3();
292 layout.stride[4] = src.stride_4();
293 layout.stride[5] = src.stride_5();
294 layout.stride[6] = src.stride_6();
295 layout.stride[7] = src.stride_7();
296
297 layout.dimension[src_type::rank] = Kokkos::dimension_scalar(src);
298
299 return dst_type(view_alloc(std::string(src.label()).append("_mirror"), wi,
300 src.impl_map().cijk()), layout);
301 }
302
303 template<class Space, class T, class ... P, typename Enable>
304 typename std::enable_if<
305 std::is_same< typename ViewTraits<T,P...>::specialize ,
306 Kokkos::Experimental::Impl::ViewPCEContiguous >::value,
307 typename Impl::MirrorViewType<Space,T,P ...>::dest_view_type>::type
308 create_mirror(Kokkos::Impl::WithoutInitializing_t wi,
309 const Space& , const Kokkos::View<T,P...> & src)
310 {
311 typedef View<T,P...> src_type ;
312 typename src_type::array_layout layout = src.layout();
313 layout.dimension[src_type::rank] = Kokkos::dimension_scalar(src);
314 return typename Impl::MirrorViewType<Space,T,P ...>::dest_view_type(
315 view_alloc(src.label(), wi, src.impl_map().cijk()), layout);
316 }
317
318 template <class Space, class T, class... P>
319 typename Impl::MirrorViewType<Space, T, P...>::view_type
320 create_mirror_view_and_copy(
321 const Space&, const Kokkos::View<T, P...>& src,
322 std::string const& name,
323 typename std::enable_if<
324 std::is_same<typename ViewTraits<T, P...>::specialize,
325 Kokkos::Experimental::Impl::ViewPCEContiguous>::value &&
326 Impl::MirrorViewType<Space, T, P...>::is_same_memspace>::type*)
327 {
328 (void)name;
329 fence(
330 "Kokkos::create_mirror_view_and_copy: fence before returning src view"); // same behavior as deep_copy(src, src)
331 return src;
332 }
333
334 template <class Space, class T, class... P>
335 typename Impl::MirrorViewType<Space, T, P...>::view_type
336 create_mirror_view_and_copy(
337 const Space&, const Kokkos::View<T, P...>& src,
338 std::string const& name,
339 typename std::enable_if<
340 std::is_same<typename ViewTraits<T, P...>::specialize,
341 Kokkos::Experimental::Impl::ViewPCEContiguous>::value &&
342 !Impl::MirrorViewType<Space, T, P...>::is_same_memspace>::type*)
343 {
344 using src_type = View<T,P...>;
345 using Mirror = typename Impl::MirrorViewType<Space, T, P...>::view_type;
346 std::string label = name.empty() ? src.label() : name;
347 typename src_type::array_layout layout = src.layout();
348 layout.dimension[src_type::rank] = Kokkos::dimension_scalar(src);
349 auto mirror = typename Mirror::non_const_type{
350 view_alloc(WithoutInitializing, label, src.impl_map().cijk()), layout};
351 deep_copy(mirror, src);
352 return mirror;
353 }
354
355 // Overload of deep_copy for UQ::PCE views intializing to a constant scalar
356 template< class DT, class ... DP >
357 void deep_copy(
358 const View<DT,DP...> & view ,
359 const typename View<DT,DP...>::array_type::value_type & value
360 , typename std::enable_if<(
361 std::is_same< typename ViewTraits<DT,DP...>::specialize
362 , Kokkos::Experimental::Impl::ViewPCEContiguous >::value
363 )>::type * )
364 {
365 static_assert(
366 std::is_same< typename ViewTraits<DT,DP...>::value_type ,
367 typename ViewTraits<DT,DP...>::non_const_value_type >::value
368 , "Can only deep copy into non-const type" );
369
370 typedef View<DT,DP...> view_type;
371 typedef typename view_type::array_type::value_type scalar_type;
372 typedef typename FlatArrayType<view_type>::type flat_array_type;
373 if (value == scalar_type(0))
374 Kokkos::Impl::StokhosViewFill< flat_array_type >( view , value );
375 else
376 Kokkos::Impl::StokhosViewFill< view_type>( view , value );
377 }
378
379 // Overload of deep_copy for UQ::PCE views intializing to a constant UQ::PCE
380 template< class DT, class ... DP >
381 void deep_copy(
382 const View<DT,DP...> & view ,
383 const typename View<DT,DP...>::value_type & value
384 , typename std::enable_if<(
385 std::is_same< typename ViewTraits<DT,DP...>::specialize
386 , Kokkos::Experimental::Impl::ViewPCEContiguous >::value
387 )>::type * )
388 {
389 static_assert(
390 std::is_same< typename ViewTraits<DT,DP...>::value_type ,
391 typename ViewTraits<DT,DP...>::non_const_value_type >::value
392 , "Can only deep copy into non-const type" );
393
394 Kokkos::Impl::StokhosViewFill< View<DT,DP...> >( view , value );
395 }
396
397 // Overload of deep_copy for UQ::PCE views intializing to a constant scalar
398 template< class ExecSpace , class DT, class ... DP >
399 void deep_copy(
400 const ExecSpace &,
401 const View<DT,DP...> & view ,
402 const typename View<DT,DP...>::array_type::value_type & value
403 , typename std::enable_if<(
404 Kokkos::is_execution_space< ExecSpace >::value &&
405 std::is_same< typename ViewTraits<DT,DP...>::specialize
406 , Kokkos::Experimental::Impl::ViewPCEContiguous >::value
407 )>::type * )
408 {
409 static_assert(
410 std::is_same< typename ViewTraits<DT,DP...>::value_type ,
411 typename ViewTraits<DT,DP...>::non_const_value_type >::value
412 , "Can only deep copy into non-const type" );
413
414 typedef View<DT,DP...> view_type;
415 typedef typename view_type::array_type::value_type scalar_type;
416 typedef typename FlatArrayType<view_type>::type flat_array_type;
417 if (value == scalar_type(0))
418 Kokkos::Impl::StokhosViewFill< flat_array_type >( view , value );
419 else
420 Kokkos::Impl::StokhosViewFill< view_type>( view , value );
421 }
422
423 // Overload of deep_copy for UQ::PCE views intializing to a constant UQ::PCE
424 template< class ExecSpace , class DT, class ... DP >
425 void deep_copy(
426 const ExecSpace &,
427 const View<DT,DP...> & view ,
428 const typename View<DT,DP...>::value_type & value
429 , typename std::enable_if<(
430 Kokkos::is_execution_space< ExecSpace >::value &&
431 std::is_same< typename ViewTraits<DT,DP...>::specialize
432 , Kokkos::Experimental::Impl::ViewPCEContiguous >::value
433 )>::type * )
434 {
435 static_assert(
436 std::is_same< typename ViewTraits<DT,DP...>::value_type ,
437 typename ViewTraits<DT,DP...>::non_const_value_type >::value
438 , "Can only deep copy into non-const type" );
439
440 Kokkos::Impl::StokhosViewFill< View<DT,DP...> >( view , value );
441 }
442
443 namespace Experimental {
444 namespace Impl {
445
446 // Deep copy between views not assuming contiguous storage of arrays
447 // Need to use team interface for Cuda
448 template< class OutputView , class InputView >
449 struct DeepCopyNonContiguous
450 {
451 typedef typename OutputView::execution_space execution_space ;
452 typedef typename execution_space::size_type size_type ;
453
454 const OutputView output ;
455 const InputView input ;
456
457 DeepCopyNonContiguous( const OutputView & arg_out ,
458 const InputView & arg_in ) :
459 output( arg_out ), input( arg_in )
460 {
461 parallel_for( output.extent(0) , *this );
462 execution_space().fence();
463 }
464
465 KOKKOS_INLINE_FUNCTION
466 void operator()( const size_type i0 ) const
467 {
468 for ( size_type i1 = 0 ; i1 < output.extent(1) ; ++i1 ) {
469 for ( size_type i2 = 0 ; i2 < output.extent(2) ; ++i2 ) {
470 for ( size_type i3 = 0 ; i3 < output.extent(3) ; ++i3 ) {
471 for ( size_type i4 = 0 ; i4 < output.extent(4) ; ++i4 ) {
472 for ( size_type i5 = 0 ; i5 < output.extent(5) ; ++i5 ) {
473 for ( size_type i6 = 0 ; i6 < output.extent(6) ; ++i6 ) {
474 for ( size_type i7 = 0 ; i7 < output.extent(7) ; ++i7 ) {
475 output.access(i0,i1,i2,i3,i4,i5,i6,i7) = input.access(i0,i1,i2,i3,i4,i5,i6,i7) ;
476 }}}}}}}
477 }
478 };
479
480 } // namespace Impl
481 } // namespace Experimental
482
483 /* Specialize for deep copy of UQ::PCE */
484 template< class ExecSpace, class DT , class ... DP , class ST , class ... SP >
485 inline
486 void deep_copy( const ExecSpace &,
487 const View<DT,DP...> & dst ,
488 const View<ST,SP...> & src
489 , typename std::enable_if<(
490 std::is_same< typename ViewTraits<DT,DP...>::specialize
491 , Kokkos::Experimental::Impl::ViewPCEContiguous >::value
492 &&
493 std::is_same< typename ViewTraits<ST,SP...>::specialize
494 , Kokkos::Experimental::Impl::ViewPCEContiguous >::value
495 )>::type * )
496 {
497 static_assert(
498 std::is_same< typename ViewTraits<DT,DP...>::value_type ,
499 typename ViewTraits<DT,DP...>::non_const_value_type >::value
500 , "Deep copy destination must be non-const" );
501
502 static_assert(
503 ( unsigned(ViewTraits<DT,DP...>::rank) ==
504 unsigned(ViewTraits<ST,SP...>::rank) )
505 , "Deep copy destination and source must have same rank" );
506
507 typedef View<DT,DP...> dst_type ;
508 typedef View<ST,SP...> src_type ;
509 typedef typename dst_type::array_type dst_array_type ;
510 typedef typename src_type::array_type src_array_type ;
511
512 if ( is_allocation_contiguous(dst) && is_allocation_contiguous(src) ) {
513 dst_array_type dst_array = dst ;
514 src_array_type src_array = src ;
515 deep_copy( ExecSpace(), dst_array , src_array );
516 }
517
518 // otherwise, use a custom kernel
519 else {
520
521 // If views are in the same memory space, copy component-wise
522 if ( std::is_same< typename dst_type::memory_space ,
523 typename src_type::memory_space >::value ) {
524 Experimental::Impl::DeepCopyNonContiguous< dst_type , src_type >( dst , src );
525 }
526
527 else {
528
529 typedef View< typename src_type::non_const_data_type ,
530 std::conditional_t<std::is_same<typename src_type::array_layout,
531 Kokkos::LayoutStride>::value,
532 Kokkos::LayoutRight,
533 typename src_type::array_layout>,
534 typename src_type::execution_space > tmp_src_type;
535 typedef typename tmp_src_type::array_type tmp_src_array_type;
536 typedef View< typename dst_type::non_const_data_type ,
537 std::conditional_t<std::is_same<typename dst_type::array_layout,
538 Kokkos::LayoutStride>::value,
539 Kokkos::LayoutRight,
540 typename dst_type::array_layout>,
541 typename dst_type::execution_space > tmp_dst_type;
542 typedef typename tmp_dst_type::array_type tmp_dst_array_type;
543
544 // Copy src into a contiguous view in src's memory space,
545 // then copy to dst
546 if ( is_allocation_contiguous(dst) &&
547 !is_allocation_contiguous(src) ) {
548 size_t src_dims[8];
549 //src.dimensions(src_dims);
550 src_dims[0] = src.extent(0);
551 src_dims[1] = src.extent(1);
552 src_dims[2] = src.extent(2);
553 src_dims[3] = src.extent(3);
554 src_dims[4] = src.extent(4);
555 src_dims[5] = src.extent(5);
556 src_dims[6] = src.extent(6);
557 src_dims[7] = src.extent(7);
558 src_dims[src_type::rank] = dimension_scalar(src);
559 tmp_src_type src_tmp(
560 view_alloc("src_tmp" , WithoutInitializing, cijk(src) ) ,
561 src_dims[0], src_dims[1], src_dims[2], src_dims[3],
562 src_dims[4], src_dims[5], src_dims[6], src_dims[7] );
563 Experimental::Impl::DeepCopyNonContiguous< tmp_src_type , src_type >( src_tmp , src );
564 dst_array_type dst_array = dst ;
565 tmp_src_array_type src_array = src_tmp ;
566 deep_copy( ExecSpace(), dst_array , src_array );
567 }
568
569 // Copy src into a contiguous view in dst's memory space,
570 // then copy to dst
571 else if ( !is_allocation_contiguous(dst) &&
572 is_allocation_contiguous(src) ) {
573 size_t dst_dims[8];
574 //dst.dimensions(dst_dims);
575 dst_dims[0] = dst.extent(0);
576 dst_dims[1] = dst.extent(1);
577 dst_dims[2] = dst.extent(2);
578 dst_dims[3] = dst.extent(3);
579 dst_dims[4] = dst.extent(4);
580 dst_dims[5] = dst.extent(5);
581 dst_dims[6] = dst.extent(6);
582 dst_dims[7] = dst.extent(7);
583 dst_dims[dst_type::rank] = dimension_scalar(dst);
584 tmp_dst_type dst_tmp(
585 view_alloc("dst_tmp" , WithoutInitializing, cijk(dst) ) ,
586 dst_dims[0], dst_dims[1], dst_dims[2], dst_dims[3],
587 dst_dims[4], dst_dims[5], dst_dims[6], dst_dims[7] );
588 tmp_dst_array_type dst_array = dst_tmp ;
589 src_array_type src_array = src ;
590 deep_copy( ExecSpace(), dst_array , src_array );
591 Experimental::Impl::DeepCopyNonContiguous< dst_type , tmp_dst_type >( dst , dst_tmp );
592 }
593
594 // Copy src into a contiguous view in src's memory space,
595 // copy to a continugous view in dst's memory space, then copy to dst
596 else {
597 size_t src_dims[8];
598 //src.dimensions(src_dims);
599 src_dims[0] = src.extent(0);
600 src_dims[1] = src.extent(1);
601 src_dims[2] = src.extent(2);
602 src_dims[3] = src.extent(3);
603 src_dims[4] = src.extent(4);
604 src_dims[5] = src.extent(5);
605 src_dims[6] = src.extent(6);
606 src_dims[7] = src.extent(7);
607 src_dims[src_type::rank] = dimension_scalar(src);
608 tmp_src_type src_tmp(
609 view_alloc("src_tmp" , WithoutInitializing, cijk(src) ) ,
610 src_dims[0], src_dims[1], src_dims[2], src_dims[3],
611 src_dims[4], src_dims[5], src_dims[6], src_dims[7] );
612 Experimental::Impl::DeepCopyNonContiguous< tmp_src_type , src_type >( src_tmp , src );
613 size_t dst_dims[8];
614 //dst.dimensions(dst_dims);
615 dst_dims[0] = dst.extent(0);
616 dst_dims[1] = dst.extent(1);
617 dst_dims[2] = dst.extent(2);
618 dst_dims[3] = dst.extent(3);
619 dst_dims[4] = dst.extent(4);
620 dst_dims[5] = dst.extent(5);
621 dst_dims[6] = dst.extent(6);
622 dst_dims[7] = dst.extent(7);
623 dst_dims[dst_type::rank] = dimension_scalar(dst);
624 tmp_dst_type dst_tmp(
625 view_alloc("dst_tmp" , WithoutInitializing, cijk(dst) ) ,
626 dst_dims[0], dst_dims[1], dst_dims[2], dst_dims[3],
627 dst_dims[4], dst_dims[5], dst_dims[6], dst_dims[7] );
628 tmp_dst_array_type dst_array = dst_tmp ;
629 tmp_src_array_type src_array = src_tmp ;
630 deep_copy( ExecSpace(), dst_array , src_array );
631 Experimental::Impl::DeepCopyNonContiguous< dst_type , tmp_dst_type >( dst , dst_tmp );
632 }
633 }
634 }
635 }
636
637 /* Specialize for deep copy of UQ::PCE */
638 template< class DT , class ... DP , class ST , class ... SP >
639 inline
640 void deep_copy( const View<DT,DP...> & dst ,
641 const View<ST,SP...> & src
642 , typename std::enable_if<(
643 std::is_same< typename ViewTraits<DT,DP...>::specialize
644 , Kokkos::Experimental::Impl::ViewPCEContiguous >::value
645 &&
646 std::is_same< typename ViewTraits<ST,SP...>::specialize
647 , Kokkos::Experimental::Impl::ViewPCEContiguous >::value
648 )>::type * )
649 {
650 using exec_space = typename View<DT,DP...>::execution_space;
651 Kokkos::fence();
652 Kokkos::deep_copy(exec_space(), dst, src);
653 Kokkos::fence();
654 }
655
656 namespace Impl {
657
658 template <unsigned N, typename... Args>
659 KOKKOS_FUNCTION std::enable_if_t<
660 N == View<Args...>::rank &&
661 std::is_same<typename ViewTraits<Args...>::specialize,
662 Kokkos::Experimental::Impl::ViewPCEContiguous>::value,
663 View<Args...>>
664 as_view_of_rank_n(View<Args...> v) {
665 return v;
666 }
667
668 // Placeholder implementation to compile generic code for DynRankView; should
669 // never be called
670 template <unsigned N, typename T, typename... Args>
671 std::enable_if_t<
672 N != View<T, Args...>::rank &&
673 std::is_same<typename ViewTraits<T, Args...>::specialize,
674 Kokkos::Experimental::Impl::ViewPCEContiguous>::value,
675 View<typename RankDataType<typename View<T, Args...>::value_type, N>::type,
676 Args...>>
677 as_view_of_rank_n(View<T, Args...>) {
678 Kokkos::Impl::throw_runtime_exception(
679 "Trying to get at a View of the wrong rank");
680 return {};
681 }
682
683 }
684
685 }
686
687 //----------------------------------------------------------------------------
688 //----------------------------------------------------------------------------
689 //----------------------------------------------------------------------------
690
691 namespace Kokkos {
692 //namespace Experimental {
693 namespace Impl {
694
695 // Allow passing of Cijk tensor through ViewCtorProp
696 template< typename Value, typename Execution, typename Memory >
697 struct ViewCtorProp< void , Stokhos::CrsProductTensor<Value, Execution, Memory> >
698 {
699 ViewCtorProp() = default ;
700 ViewCtorProp( const ViewCtorProp & ) = default ;
701 ViewCtorProp & operator = ( const ViewCtorProp & ) = default ;
702
703 typedef Stokhos::CrsProductTensor<Value, Execution, Memory> type ;
704
705 ViewCtorProp( const type & arg ) : value( arg ) {}
706 ViewCtorProp( type && arg ) : value( arg ) {}
707
708 type value ;
709 };
710
711 template <typename AllocProp>
712 struct ctor_prop_has_cijk
713 {
714 static const bool value = false;
715 };
716
717 template< typename T >
718 struct ctor_prop_has_cijk< ViewCtorProp<T> >
719 {
720 static const bool value = false;
721 };
722
723 template< typename Value, typename Execution, typename Memory >
724 struct ctor_prop_has_cijk<
725 ViewCtorProp< Stokhos::CrsProductTensor<Value, Execution, Memory> >
726 >
727 {
728 static const bool value = true;
729 };
730
731 template< typename T, typename ... P >
732 struct ctor_prop_has_cijk< ViewCtorProp<T,P...> >
733 {
734 static const bool value =
735 ctor_prop_has_cijk< ViewCtorProp<T> >::value ||
736 ctor_prop_has_cijk< ViewCtorProp<P...> >::value;
737 };
738
739 } /* namespace Impl */
740 //} /* namespace Experimental */
741
742 template <typename CijkType, typename AllocProp>
743 KOKKOS_INLINE_FUNCTION
744 typename std::enable_if< !Impl::ctor_prop_has_cijk<AllocProp>::value,
745 CijkType >::type
746 extract_cijk(const AllocProp& prop)
747 {
748 return CijkType();
749 }
750
751 template <typename CijkType, typename AllocProp>
752 KOKKOS_INLINE_FUNCTION
753 typename std::enable_if< Impl::ctor_prop_has_cijk<AllocProp>::value,
754 CijkType >::type
755 extract_cijk(const AllocProp& prop)
756 {
757 return ( (const Impl::ViewCtorProp<void,CijkType>&) prop ).value;
758 }
759
760 } /* namespace Kokkos */
761
762 //----------------------------------------------------------------------------
763 //----------------------------------------------------------------------------
764 //----------------------------------------------------------------------------
765
766 namespace Kokkos {
767 namespace Impl {
768
769 template< class DataType , class ArrayLayout , typename StorageType >
770 struct ViewDataAnalysis< DataType /* Original view data type */
771 , ArrayLayout
772 , Sacado::UQ::PCE< StorageType > >
773 {
774 private:
775
776 typedef typename StorageType::value_type ScalarType;
777 typedef ViewArrayAnalysis< DataType > array_analysis ;
778
779 public:
780
781 // Specialized view data mapping:
782 typedef Kokkos::Experimental::Impl::ViewPCEContiguous specialize ;
783
784 typedef typename array_analysis::dimension dimension ;
785 typedef typename array_analysis::value_type value_type ;
786 typedef typename array_analysis::const_value_type const_value_type ;
787 typedef typename array_analysis::non_const_value_type non_const_value_type ;
788
789 // Generate analogous multidimensional array specification type.
790 typedef typename
791 ViewDataType< value_type , dimension >::type type ;
792 typedef typename
793 ViewDataType< const_value_type , dimension >::type const_type ;
794 typedef typename
795 ViewDataType< non_const_value_type , dimension >::type non_const_type ;
796
797 private:
798
799 // A const ?
800 enum { is_const = std::is_same< value_type , const_value_type >::value };
801
802 // The unwrapped scalar types:
803 typedef typename
804 std::conditional< is_const , const ScalarType , ScalarType >::type
805 scalar_type ;
806
807 typedef ScalarType non_const_scalar_type ;
808 typedef const ScalarType const_scalar_type ;
809
810 // Prepend or append the pce dimension based on ArrayLayout
811 typedef typename array_analysis::dimension::
812 template prepend<0>::type
813 prepend_scalar_dimension ;
814 typedef typename array_analysis::dimension::
815 template append<0>::type
816 append_scalar_dimension ;
817 typedef typename std::conditional<
818 std::is_same< ArrayLayout, Kokkos::LayoutLeft>::value,
819 prepend_scalar_dimension,
820 append_scalar_dimension >::type scalar_dimension;
821
822 public:
823
824 // Generate "flattened" multidimensional array specification type.
825 typedef typename
826 ViewDataType< scalar_type , scalar_dimension >::type scalar_array_type ;
827
828 typedef typename
829 ViewDataType< const_scalar_type , scalar_dimension >::type
830 const_scalar_array_type ;
831
832 typedef typename
833 ViewDataType< non_const_scalar_type , scalar_dimension >::type
834 non_const_scalar_array_type ;
835 };
836
837 } // namespace Impl
838 } // namespace Kokkos
839
840 //----------------------------------------------------------------------------
841
842 namespace Kokkos {
843 namespace Experimental {
844 namespace Impl {
845
846 // UQ::PCE allocation for dynamically-sized UQ::PCE types.
847 // In this case we allocate two chunks of data, the first for the the
848 // UQ::PCE<Storage> itself and then for the underlying scalar type
849 // (UQ::PCE<Storage>::value_type). The memory is laid out with the
850 // former followed by the latter.
851 template <class ValueType>
852 struct PCEAllocation {
853 typedef ValueType value_type;
854 typedef typename Sacado::ValueType<value_type>::type scalar_type;
855 typedef typename value_type::cijk_type cijk_type;
856
857 value_type * value_ptr;
858 scalar_type * scalar_ptr;
859
860 KOKKOS_INLINE_FUNCTION
861 static constexpr size_t
862 memory_span(const size_t span, const unsigned pce_size) {
863 return span * ( pce_size * sizeof(scalar_type) + sizeof(value_type) );
864 }
865
866 KOKKOS_INLINE_FUNCTION
867 PCEAllocation() : value_ptr(0), scalar_ptr(0) {}
868
869 template <typename T>
870 KOKKOS_INLINE_FUNCTION
871 PCEAllocation& operator=(const PCEAllocation<T>& a) {
872 value_ptr = a.value_ptr;
873 scalar_ptr = a.scalar_ptr;
874 return *this;
875 }
876
877 // We are making an assumption the data is laid out as described above,
878 // which in general may not be true if the view is created from memory
879 // allocated elsewhere. We should check for that.
880 KOKKOS_INLINE_FUNCTION
881 void set(value_type* ptr, const size_t span, const unsigned pce_size) {
882 value_ptr = ptr;
883 scalar_ptr = reinterpret_cast<scalar_type*>(ptr+span);
884 }
885
886 template <class ExecSpace>
887 struct PCEConstruct {
888 ExecSpace m_space;
889 value_type* m_p;
890 scalar_type* m_sp;
891 size_t m_span;
892 unsigned m_pce_size;
893 cijk_type m_cijk;
894
895 PCEConstruct() = default;
896 PCEConstruct(const PCEConstruct&) = default;
897 PCEConstruct& operator=(const PCEConstruct&) = default;
898
899 inline
900 PCEConstruct(const ExecSpace& space,
901 value_type* p,
902 scalar_type* sp,
903 const size_t span,
904 const unsigned pce_size,
905 const cijk_type& cijk) :
906 m_space(space), m_p(p), m_sp(sp), m_span(span), m_pce_size(pce_size),
907 m_cijk(cijk) {}
908
909 inline void execute() {
910 typedef Kokkos::RangePolicy< ExecSpace > PolicyType ;
911 const Kokkos::Impl::ParallelFor< PCEConstruct , PolicyType >
912 closure( *this , PolicyType( 0 , m_span ) );
913 closure.execute();
914 m_space.fence();
915 }
916
917 KOKKOS_INLINE_FUNCTION
918 void operator() (const size_t i) const {
919 new (m_p+i) value_type(m_cijk, m_pce_size, m_sp+i*m_pce_size, false);
920 }
921 };
922
923 template <class ExecSpace>
924 struct ConstructDestructFunctor {
925 typedef Kokkos::Impl::ViewValueFunctor< ExecSpace, scalar_type > ScalarFunctorType ;
926 typedef PCEConstruct< ExecSpace > PCEFunctorType ;
927 ScalarFunctorType m_scalar_functor;
928 PCEFunctorType m_pce_functor;
929 bool m_initialize;
930
931 ConstructDestructFunctor() = default;
932 ConstructDestructFunctor(const ConstructDestructFunctor&) = default;
933 ConstructDestructFunctor& operator=(const ConstructDestructFunctor&) = default;
934
935 ConstructDestructFunctor(const ExecSpace & space,
936 const bool initialize,
937 const size_t span,
938 const unsigned pce_size,
939 const cijk_type& cijk,
940 scalar_type* scalar_ptr,
941 value_type* value_ptr) :
942 m_scalar_functor( space , scalar_ptr , span*pce_size , "Stokhos_UQ_PCE_Contig_ConstructDestructFunctor" ),
943 m_pce_functor( space , value_ptr , scalar_ptr , span , pce_size , cijk ),
944 m_initialize(initialize) {}
945
946 inline void construct_shared_allocation() {
947 // First initialize the scalar_type array
948 if (m_initialize)
949 m_scalar_functor.construct_shared_allocation();
950
951 // Construct each UQ::PCE using memory in scalar_ptr array,
952 // setting pointer to UQ::PCE values from values array
953 // Equivalent to:
954 // value_type* p = value_ptr;
955 // scalar_type* sp = scalar_ptr;
956 // for (size_t i=0; i<span; ++i) {
957 // new (p++) value_type(cijk, pce_size, sp, false);
958 // sp += pce_size;
959 // }
960 // (we always need to do this, regardless of initialization)
961 m_pce_functor.execute();
962 }
963
964 inline void destroy_shared_allocation() {
965 // We only need to (possibly) call the destructor on values in the
966 // scalar_type array, since the value_type array is a view into it
967 if (m_initialize)
968 m_scalar_functor.destroy_shared_allocation();
969 }
970
971 };
972
973 template <class ExecSpace>
974 inline ConstructDestructFunctor<ExecSpace>
975 create_functor(const ExecSpace & space,
976 const bool initialize,
977 const size_t span,
978 const unsigned pce_size,
979 const cijk_type& cijk) const {
980 return ConstructDestructFunctor<ExecSpace>(space, initialize, span,
981 pce_size, cijk, scalar_ptr,
982 value_ptr);
983 }
984
985 // Assign scalar_type pointer to give ptr
986 // This makes BIG assumption on how the data was allocated
987 template <typename T>
988 void assign(T * ptr) {
989 value_ptr = reinterpret_cast<value_type*>(ptr);
990 if (ptr != 0)
991 scalar_ptr = value_ptr->coeff();
992 else
993 scalar_ptr = 0;
994 }
995 };
996
997 }}} // namespace Kokkos::Experimental::Impl
998
999 namespace Kokkos {
1000 namespace Impl {
1001
1002 template< class Traits >
1003 class ViewMapping< Traits , /* View internal mapping */
1004 typename std::enable_if<
1005 ( std::is_same< typename Traits::specialize
1006 , Kokkos::Experimental::Impl::ViewPCEContiguous >::value
1007 &&
1008 ( std::is_same< typename Traits::array_layout
1009 , Kokkos::LayoutLeft >::value
1010 ||
1011 std::is_same< typename Traits::array_layout
1012 , Kokkos::LayoutRight >::value
1013 ||
1014 std::is_same< typename Traits::array_layout
1015 , Kokkos::LayoutStride >::value
1016 )
1017 )
1018 , typename Traits::specialize
1019 >::type >
1020 {
1021 private:
1022
1023 template< class , class ... > friend class ViewMapping ;
1024 template< class , class ... > friend class Kokkos::View ;
1025
1026 public:
1027 typedef typename Traits::value_type sacado_uq_pce_type ;
1028 typedef typename sacado_uq_pce_type::storage_type stokhos_storage_type ;
1029 typedef typename stokhos_storage_type::value_type intrinsic_scalar_type ;
1030 typedef typename
1031 std::add_const< intrinsic_scalar_type >::type const_intrinsic_scalar_type ;
1032 typedef typename sacado_uq_pce_type::cijk_type cijk_type ;
1033 private:
1034
1035 typedef Kokkos::Experimental::Impl::PCEAllocation<sacado_uq_pce_type> handle_type;
1036
1037 typedef ViewOffset< typename Traits::dimension
1038 , typename Traits::array_layout
1039 , void
1040 > offset_type ;
1041
1042 // Prepend or append the pce dimension based on array_layout
1043 typedef ViewArrayAnalysis< typename Traits::data_type > array_analysis ;
1044 typedef typename array_analysis::dimension array_dimension;
1045 typedef ViewOffset< typename array_dimension::
1046 template append<0>::type,
1047 typename Traits::array_layout,
1048 void
1049 > append_offset_type ;
1050 typedef ViewOffset< typename array_dimension::
1051 template prepend<0>::type,
1052 typename Traits::array_layout,
1053 void
1054 > prepend_offset_type ;
1055 typedef typename std::conditional<
1056 std::is_same< typename Traits::array_layout, Kokkos::LayoutLeft>::value,
1057 prepend_offset_type,
1058 append_offset_type >::type array_offset_type;
1059
1060 handle_type m_impl_handle ;
1061 offset_type m_impl_offset ;
1062 unsigned m_sacado_size ; // Size of sacado dimension
1063 cijk_type m_cijk ; // Sparse 3 tensor
1064 bool m_is_contiguous ; // Is data allocated contiguously
1065
1066 // Check whether data allocation is contiguous
1067 // Since View() takes an arbitrary pointer, we can't necessarily assume
1068 // the data was allocated contiguously
1069 KOKKOS_INLINE_FUNCTION
1070 bool is_data_contiguous() const {
1071 const size_t sz = this->span();
1072 if (sz == 0)
1073 return true;
1074 const intrinsic_scalar_type* last_coeff =
1075 m_impl_handle.value_ptr[sz-1].coeff();
1076 const intrinsic_scalar_type* last_coeff_expected =
1077 m_impl_handle.scalar_ptr + (sz-1)*m_sacado_size;
1078 return last_coeff == last_coeff_expected;
1079 }
1080
1081 public:
1082
1083 //----------------------------------------
1084 // Domain dimensions
1085
1086 enum { Rank = Traits::dimension::rank };
1087
1088 // Rank corresponding to the sacado dimension
1089 enum { Sacado_Rank = std::is_same< typename Traits::array_layout, Kokkos::LayoutLeft >::value ? 0 : Rank+1 };
1090
1091 // Using the internal offset mapping so limit to public rank:
1092 template< typename iType >
1093 KOKKOS_INLINE_FUNCTION constexpr size_t extent( const iType & r ) const
1094 { return m_impl_offset.m_dim.extent(r); }
1095
1096 KOKKOS_INLINE_FUNCTION constexpr
1097 typename Traits::array_layout layout() const
1098 { return m_impl_offset.layout(); }
1099
1100 KOKKOS_INLINE_FUNCTION constexpr size_t dimension_0() const
1101 { return m_impl_offset.dimension_0(); }
1102 KOKKOS_INLINE_FUNCTION constexpr size_t dimension_1() const
1103 { return m_impl_offset.dimension_1(); }
1104 KOKKOS_INLINE_FUNCTION constexpr size_t dimension_2() const
1105 { return m_impl_offset.dimension_2(); }
1106 KOKKOS_INLINE_FUNCTION constexpr size_t dimension_3() const
1107 { return m_impl_offset.dimension_3(); }
1108 KOKKOS_INLINE_FUNCTION constexpr size_t dimension_4() const
1109 { return m_impl_offset.dimension_4(); }
1110 KOKKOS_INLINE_FUNCTION constexpr size_t dimension_5() const
1111 { return m_impl_offset.dimension_5(); }
1112 KOKKOS_INLINE_FUNCTION constexpr size_t dimension_6() const
1113 { return m_impl_offset.dimension_6(); }
1114 KOKKOS_INLINE_FUNCTION constexpr size_t dimension_7() const
1115 { return m_impl_offset.dimension_7(); }
1116
1117 // Is a regular layout with uniform striding for each index.
1118 // Since we all for striding within the data type, we can't guarantee
1119 // regular striding
1120 using is_regular = std::false_type ;
1121
1122 KOKKOS_INLINE_FUNCTION constexpr size_t stride_0() const
1123 { return m_impl_offset.stride_0(); }
1124 KOKKOS_INLINE_FUNCTION constexpr size_t stride_1() const
1125 { return m_impl_offset.stride_1(); }
1126 KOKKOS_INLINE_FUNCTION constexpr size_t stride_2() const
1127 { return m_impl_offset.stride_2(); }
1128 KOKKOS_INLINE_FUNCTION constexpr size_t stride_3() const
1129 { return m_impl_offset.stride_3(); }
1130 KOKKOS_INLINE_FUNCTION constexpr size_t stride_4() const
1131 { return m_impl_offset.stride_4(); }
1132 KOKKOS_INLINE_FUNCTION constexpr size_t stride_5() const
1133 { return m_impl_offset.stride_5(); }
1134 KOKKOS_INLINE_FUNCTION constexpr size_t stride_6() const
1135 { return m_impl_offset.stride_6(); }
1136 KOKKOS_INLINE_FUNCTION constexpr size_t stride_7() const
1137 { return m_impl_offset.stride_7(); }
1138
1139 template< typename iType >
1140 KOKKOS_INLINE_FUNCTION void stride( iType * const s ) const
1141 { m_impl_offset.stride(s); }
1142
1143 // Size of sacado scalar dimension
1144 KOKKOS_FORCEINLINE_FUNCTION constexpr unsigned dimension_scalar() const
1145 { return m_sacado_size; }
1146
1147 // Sparse tensor
1148 KOKKOS_FORCEINLINE_FUNCTION
1149 cijk_type cijk() const
1150 { return m_cijk; }
1151
1152 // Sparse tensor
1153 KOKKOS_FORCEINLINE_FUNCTION
1154 void set_cijk(const cijk_type& cijk)
1155 { m_cijk = cijk; }
1156
1157 // Is allocation contiguous
1158 KOKKOS_INLINE_FUNCTION
1159 bool is_allocation_contiguous() const
1160 { return m_is_contiguous; }
1161
1162 // Whether the storage type is statically sized
1163 static const bool is_static = false ;
1164
1165 // Whether sacado dimension is contiguous
1166 static const bool is_contiguous = true;
1167
1168 //----------------------------------------
1169 // Range of mapping
1170
1171 // Return type of reference operators
1172 typedef sacado_uq_pce_type & reference_type ;
1173
1174 /** \brief Pointer to underlying memory type */
1175 typedef sacado_uq_pce_type * pointer_type ;
1176
1177 /** \brief Span of the mapped range : [ data() .. data() + span() ) */
1178 KOKKOS_INLINE_FUNCTION constexpr size_t span() const
1179 { return m_impl_offset.span(); }
1180
1181 /** \brief Is the mapped range span contiguous */
1182 KOKKOS_INLINE_FUNCTION constexpr bool span_is_contiguous() const
1183 { return m_impl_offset.span_is_contiguous() ; }
1184
1185 /** \brief Raw data access */
1186 KOKKOS_INLINE_FUNCTION constexpr pointer_type data() const
1187 { return m_impl_handle.value_ptr ; }
1188
1189 //----------------------------------------
1190
1191 KOKKOS_FORCEINLINE_FUNCTION
1192 reference_type reference() const
1193 { return *m_impl_handle.value_ptr; }
1194
1195 // FIXME: Check this
1196 template< typename I0 >
1197 KOKKOS_FORCEINLINE_FUNCTION
1198 typename
1199 std::enable_if< std::is_integral<I0>::value &&
1200 ! std::is_same< typename Traits::array_layout , Kokkos::LayoutStride >::value
1201 , reference_type >::type
1202 reference( const I0 & i0 ) const
1203 { return m_impl_handle.value_ptr[i0]; }
1204
1205 // FIXME: Check this
1206 template< typename I0 >
1207 KOKKOS_FORCEINLINE_FUNCTION
1208 typename
1209 std::enable_if< std::is_integral<I0>::value &&
1210 std::is_same< typename Traits::array_layout , Kokkos::LayoutStride >::value
1211 , reference_type >::type
1212 reference( const I0 & i0 ) const
1213 { return m_impl_handle.value_ptr[ m_impl_offset(i0) ]; }
1214
1215 template< typename I0 , typename I1 >
1216 KOKKOS_FORCEINLINE_FUNCTION
1217 reference_type reference( const I0 & i0 , const I1 & i1 ) const
1218 { return m_impl_handle.value_ptr[ m_impl_offset(i0,i1) ]; }
1219
1220 template< typename I0 , typename I1 , typename I2 >
1221 KOKKOS_FORCEINLINE_FUNCTION
1222 reference_type reference( const I0 & i0 , const I1 & i1 , const I2 & i2 ) const
1223 { return m_impl_handle.value_ptr[ m_impl_offset(i0,i1,i2) ]; }
1224
1225 template< typename I0 , typename I1 , typename I2 , typename I3 >
1226 KOKKOS_FORCEINLINE_FUNCTION
1227 reference_type reference( const I0 & i0 , const I1 & i1 , const I2 & i2 , const I3 & i3 ) const
1228 { return m_impl_handle.value_ptr[ m_impl_offset(i0,i1,i2,i3) ]; }
1229
1230 template< typename I0 , typename I1 , typename I2 , typename I3
1231 , typename I4 >
1232 KOKKOS_FORCEINLINE_FUNCTION
1233 reference_type reference( const I0 & i0 , const I1 & i1 , const I2 & i2 , const I3 & i3
1234 , const I4 & i4 ) const
1235 { return m_impl_handle.value_ptr[ m_impl_offset(i0,i1,i2,i3,i4) ]; }
1236
1237 template< typename I0 , typename I1 , typename I2 , typename I3
1238 , typename I4 , typename I5 >
1239 KOKKOS_FORCEINLINE_FUNCTION
1240 reference_type reference( const I0 & i0 , const I1 & i1 , const I2 & i2 , const I3 & i3
1241 , const I4 & i4 , const I5 & i5 ) const
1242 { return m_impl_handle.value_ptr[ m_impl_offset(i0,i1,i2,i3,i4,i5) ]; }
1243
1244 template< typename I0 , typename I1 , typename I2 , typename I3
1245 , typename I4 , typename I5 , typename I6 >
1246 KOKKOS_FORCEINLINE_FUNCTION
1247 reference_type reference( const I0 & i0 , const I1 & i1 , const I2 & i2 , const I3 & i3
1248 , const I4 & i4 , const I5 & i5 , const I6 & i6 ) const
1249 { return m_impl_handle.value_ptr[ m_impl_offset(i0,i1,i2,i3,i4,i5,i6) ]; }
1250
1251 template< typename I0 , typename I1 , typename I2 , typename I3
1252 , typename I4 , typename I5 , typename I6 , typename I7 >
1253 KOKKOS_FORCEINLINE_FUNCTION
1254 reference_type reference( const I0 & i0 , const I1 & i1 , const I2 & i2 , const I3 & i3
1255 , const I4 & i4 , const I5 & i5 , const I6 & i6 , const I7 & i7 ) const
1256 { return m_impl_handle.value_ptr[ m_impl_offset(i0,i1,i2,i3,i4,i5,i6,i7) ]; }
1257
1258 //----------------------------------------
1259
1260 /** \brief Span, in bytes, of the required memory */
1261 KOKKOS_INLINE_FUNCTION
1262 static size_t memory_span( typename Traits::array_layout const & layout )
1263 {
1264 // Do not introduce padding...
1265 typedef std::integral_constant< unsigned , 0 > padding ;
1266 offset_type offset( padding(), layout );
1267 unsigned sacado_size =
1268 Kokkos::Impl::GetSacadoSize<unsigned(Rank)>::eval(layout);
1269 return handle_type::memory_span( offset.span(), sacado_size );
1270 }
1271
1272 //----------------------------------------
1273
1274 KOKKOS_DEFAULTED_FUNCTION ~ViewMapping() = default ;
1275 KOKKOS_INLINE_FUNCTION ViewMapping() :
1276 m_impl_handle(),
1277 m_impl_offset(),
1278 m_sacado_size(0),
1279 m_cijk(),
1280 m_is_contiguous(true)
1281 {}
1282
1283 KOKKOS_DEFAULTED_FUNCTION ViewMapping( const ViewMapping & ) = default ;
1284 KOKKOS_DEFAULTED_FUNCTION ViewMapping & operator = ( const ViewMapping & ) = default ;
1285
1286 KOKKOS_DEFAULTED_FUNCTION ViewMapping( ViewMapping && ) = default ;
1287 KOKKOS_DEFAULTED_FUNCTION ViewMapping & operator = ( ViewMapping && ) = default ;
1288
1289 template< class ... P >
1290 KOKKOS_INLINE_FUNCTION
1291 ViewMapping
1292 ( ViewCtorProp< P ... > const & prop
1293 , typename Traits::array_layout const & layout
1294 )
1295 : m_impl_handle()
1296 , m_impl_offset( std::integral_constant< unsigned , 0 >() , layout )
1297 , m_sacado_size( Kokkos::Impl::GetSacadoSize<unsigned(Rank)>::eval(layout) )
1298 {
1299 m_impl_handle.set( ( (ViewCtorProp<void,pointer_type> const &) prop ).value
1300 , m_impl_offset.span(), m_sacado_size );
1301 m_cijk = extract_cijk<cijk_type>(prop);
1302 #ifndef __CUDA_ARCH__
1303 if (m_cijk.dimension() == 0)
1304 m_cijk = getGlobalCijkTensor<cijk_type>();
1305 // Use 0 or KOKKOS_IMPL_CTOR_DEFAULT_ARG to signal the size wasn't
1306 // specified in the constructor
1307 if (m_sacado_size == 0 ||
1308 m_sacado_size == unsigned(KOKKOS_IMPL_CTOR_DEFAULT_ARG))
1309 m_sacado_size = m_cijk.dimension();
1310 #endif
1311 m_is_contiguous = this->is_data_contiguous();
1312 }
1313
1314 /**\brief Assign data */
1315 KOKKOS_INLINE_FUNCTION
1316 void assign_data( pointer_type arg_ptr )
1317 { m_impl_handle.set( arg_ptr, m_impl_offset.span(), m_sacado_size ); }
1318
1319 //----------------------------------------
1320 /* Allocate and construct mapped array.
1321 * Allocate via shared allocation record and
1322 * return that record for allocation tracking.
1323 */
1324 template< class ... P >
1325 SharedAllocationRecord<> *
1326 allocate_shared( ViewCtorProp< P... > const & prop
1327 , typename Traits::array_layout const & layout )
1328 {
1329 typedef ViewCtorProp< P... > ctor_prop ;
1330
1331 typedef typename ctor_prop::execution_space execution_space ;
1332 typedef typename Traits::memory_space memory_space ;
1333 typedef typename handle_type::template ConstructDestructFunctor<execution_space> functor_type ;
1334 typedef SharedAllocationRecord< memory_space , functor_type > record_type ;
1335
1336 // Disallow padding
1337 typedef std::integral_constant< unsigned , 0 > padding ;
1338
1339 m_impl_offset = offset_type( padding(), layout );
1340 m_sacado_size = Kokkos::Impl::GetSacadoSize<unsigned(Rank)>::eval(layout);
1341 m_cijk = extract_cijk<cijk_type>(prop);
1342 if (m_cijk.dimension() == 0)
1343 m_cijk = getGlobalCijkTensor<cijk_type>();
1344 // Use 0 or KOKKOS_IMPL_CTOR_DEFAULT_ARG to signal the size wasn't
1345 // specified in the constructor
1346 if (m_sacado_size == 0 ||
1347 m_sacado_size == unsigned(KOKKOS_IMPL_CTOR_DEFAULT_ARG))
1348 m_sacado_size = m_cijk.dimension();
1349 m_is_contiguous = true;
1350
1351 const size_t alloc_size =
1352 handle_type::memory_span( m_impl_offset.span(), m_sacado_size );
1353
1354 // Create shared memory tracking record with allocate memory from the memory space
1355 record_type * const record =
1356 record_type::allocate( ( (ViewCtorProp<void,memory_space> const &) prop ).value
1357 , ( (ViewCtorProp<void,std::string> const &) prop ).value
1358 , alloc_size );
1359
1360 // Only set the the pointer and initialize if the allocation is non-zero.
1361 // May be zero if one of the dimensions is zero.
1362 if ( alloc_size ) {
1363 auto space = ((ViewCtorProp<void,execution_space> const &) prop).value;
1364
1365 m_impl_handle.set( reinterpret_cast< pointer_type >( record->data() ),
1366 m_impl_offset.span(), m_sacado_size );
1367
1368 // Assume destruction is only required when construction is requested.
1369 // The ViewValueFunctor has both value construction and destruction operators.
1370 record->m_destroy = m_impl_handle.create_functor(
1371 space
1372 , ctor_prop::initialize
1373 , m_impl_offset.span()
1374 , m_sacado_size
1375 , m_cijk );
1376
1377 // Construct values
1378 record->m_destroy.construct_shared_allocation();
1379 space.fence();
1380 }
1381
1382 return record ;
1383 }
1384
1385 template< class ... P >
1386 SharedAllocationRecord<> *
1387 allocate_shared( ViewCtorProp< P... > const & prop
1388 , typename Traits::array_layout const & layout
1389 , bool /*execution_space_specified*/)
1390 {
1391 return allocate_shared(prop, layout);
1392 }
1393
1394 };
1395
1396 } // namespace Impl
1397 } // namespace Kokkos
1398
1399 //----------------------------------------------------------------------------
1400
1401 namespace Kokkos {
1402 namespace Impl {
1403
1404 /**\brief Assign compatible Sacado::UQ::PCE view mappings.
1405 *
1406 * View<UQ::PCE> = View<UQ::PCE>
1407 */
1408 template< class DstTraits , class SrcTraits >
1409 class ViewMapping< DstTraits , SrcTraits ,
1410 typename std::enable_if<(
1411 Kokkos::Impl::MemorySpaceAccess< typename DstTraits::memory_space
1412 , typename SrcTraits::memory_space >::assignable
1413 &&
1414 // Destination view has UQ::PCE
1415 std::is_same< typename DstTraits::specialize
1416 , Kokkos::Experimental::Impl::ViewPCEContiguous >::value
1417 &&
1418 // Source view has UQ::PCE only
1419 std::is_same< typename SrcTraits::specialize
1420 , Kokkos::Experimental::Impl::ViewPCEContiguous >::value
1421 )
1422 , typename DstTraits::specialize
1423 >::type >
1424 {
1425 public:
1426
1427 enum { is_assignable = true };
1428 enum { is_assignable_data_type = true };
1429
1430 typedef Kokkos::Impl::SharedAllocationTracker TrackType ;
1431 typedef ViewMapping< DstTraits , typename DstTraits::specialize > DstType ;
1432 typedef ViewMapping< SrcTraits , typename SrcTraits::specialize > SrcType ;
1433
1434 KOKKOS_INLINE_FUNCTION static
1435 void assign( DstType & dst
1436 , const SrcType & src
1437 , const TrackType & )
1438 {
1439 static_assert(
1440 (
1441 std::is_same< typename DstTraits::array_layout
1442 , Kokkos::LayoutLeft >::value ||
1443 std::is_same< typename DstTraits::array_layout
1444 , Kokkos::LayoutRight >::value ||
1445 std::is_same< typename DstTraits::array_layout
1446 , Kokkos::LayoutStride >::value
1447 )
1448 &&
1449 (
1450 std::is_same< typename SrcTraits::array_layout
1451 , Kokkos::LayoutLeft >::value ||
1452 std::is_same< typename SrcTraits::array_layout
1453 , Kokkos::LayoutRight >::value ||
1454 std::is_same< typename SrcTraits::array_layout
1455 , Kokkos::LayoutStride >::value
1456 )
1457 , "View of UQ::PCE requires LayoutLeft, LayoutRight, or LayoutStride" );
1458
1459 static_assert(
1460 std::is_same< typename DstTraits::array_layout
1461 , typename SrcTraits::array_layout >::value ||
1462 std::is_same< typename DstTraits::array_layout
1463 , Kokkos::LayoutStride >::value ||
1464 ( unsigned(DstTraits::rank) == 1 && unsigned(SrcTraits::rank) == 1 ) ,
1465 "View assignment must have compatible layout" );
1466
1467 static_assert(
1468 std::is_same< typename DstTraits::value_type
1469 , typename SrcTraits::value_type >::value ||
1470 std::is_same< typename DstTraits::value_type
1471 , typename SrcTraits::const_value_type >::value ,
1472 "View assignment must have same value type or const = non-const" );
1473
1474 static_assert(
1475 ViewDimensionAssignable
1476 < typename DstType::offset_type::dimension_type
1477 , typename SrcType::offset_type::dimension_type >::value ,
1478 "View assignment must have compatible dimensions" );
1479
1480 dst.m_impl_handle = src.m_impl_handle ;
1481 dst.m_impl_offset = src.m_impl_offset ;
1482 dst.m_sacado_size = src.m_sacado_size ;
1483 dst.m_cijk = src.m_cijk ;
1484 dst.m_is_contiguous = src.m_is_contiguous ;
1485 }
1486 };
1487
1488 /**\brief Assign compatible Sacado::UQ::PCE view mappings.
1489 *
1490 * View<ordinary> = View<UQ::PCE>
1491 * where View<ordinay>::Rank = View<UQ::PCE>::Rank+1
1492 */
1493 template< class DstTraits , class SrcTraits >
1494 class ViewMapping< DstTraits , SrcTraits ,
1495 typename std::enable_if<(
1496 Kokkos::Impl::MemorySpaceAccess< typename DstTraits::memory_space
1497 , typename SrcTraits::memory_space >::assignable
1498 &&
1499 // Destination view has ordinary
1500 std::is_same< typename DstTraits::specialize , void >::value
1501 &&
1502 // Source view has UQ::PCE only
1503 std::is_same< typename SrcTraits::specialize
1504 , Kokkos::Experimental::Impl::ViewPCEContiguous >::value
1505 &&
1506 // Ranks match
1507 unsigned(DstTraits::dimension::rank) == unsigned(SrcTraits::dimension::rank)+1
1508 )
1509 , typename DstTraits::specialize
1510 >::type >
1511 {
1512 public:
1513
1514 enum { is_assignable = true };
1515 enum { is_assignable_data_type = true };
1516
1517 typedef Kokkos::Impl::SharedAllocationTracker TrackType ;
1518 typedef ViewMapping< DstTraits , typename DstTraits::specialize > DstType ;
1519 typedef ViewMapping< SrcTraits , typename SrcTraits::specialize > SrcType ;
1520
1521 KOKKOS_INLINE_FUNCTION static
1522 void assign( DstType & dst
1523 , const SrcType & src
1524 , const TrackType & )
1525 {
1526 static_assert(
1527 (
1528 std::is_same< typename DstTraits::array_layout
1529 , Kokkos::LayoutLeft >::value ||
1530 std::is_same< typename DstTraits::array_layout
1531 , Kokkos::LayoutRight >::value ||
1532 std::is_same< typename DstTraits::array_layout
1533 , Kokkos::LayoutStride >::value
1534 )
1535 &&
1536 (
1537 std::is_same< typename SrcTraits::array_layout
1538 , Kokkos::LayoutLeft >::value ||
1539 std::is_same< typename SrcTraits::array_layout
1540 , Kokkos::LayoutRight >::value ||
1541 std::is_same< typename SrcTraits::array_layout
1542 , Kokkos::LayoutStride >::value
1543 )
1544 , "View of UQ::PCE requires LayoutLeft, LayoutRight, or LayoutStride" );
1545
1546 static_assert(
1547 std::is_same< typename DstTraits::array_layout
1548 , typename SrcTraits::array_layout >::value ||
1549 std::is_same< typename DstTraits::array_layout
1550 , Kokkos::LayoutStride >::value ,
1551 "View assignment must have compatible layout" );
1552
1553 static_assert(
1554 std::is_same< typename DstTraits::scalar_array_type
1555 , typename SrcTraits::scalar_array_type >::value ||
1556 std::is_same< typename DstTraits::scalar_array_type
1557 , typename SrcTraits::const_scalar_array_type >::value ,
1558 "View assignment must have same value type or const = non-const" );
1559
1560 static_assert(
1561 ViewDimensionAssignable<
1562 typename DstType::offset_type::dimension_type,
1563 typename SrcType::array_offset_type::dimension_type >::value,
1564 "View assignment must have compatible dimensions" );
1565
1566 if ( !src.m_is_contiguous )
1567 Kokkos::abort("\n\n ****** Kokkos::View< Sacado::UQ::PCE ... >: can't assign non-contiguous view ******\n\n");
1568
1569 unsigned dims[8];
1570 dims[0] = src.m_impl_offset.dimension_0();
1571 dims[1] = src.m_impl_offset.dimension_1();
1572 dims[2] = src.m_impl_offset.dimension_2();
1573 dims[3] = src.m_impl_offset.dimension_3();
1574 dims[4] = src.m_impl_offset.dimension_4();
1575 dims[5] = src.m_impl_offset.dimension_5();
1576 dims[6] = src.m_impl_offset.dimension_6();
1577 dims[7] = src.m_impl_offset.dimension_7();
1578 unsigned rank = SrcTraits::dimension::rank;
1579 unsigned sacado_size = src.m_sacado_size;
1580 if (std::is_same<typename SrcTraits::array_layout, LayoutLeft>::value) {
1581 // Move sacado_size to the first dimension, shift all others up one
1582 for (unsigned i=rank; i>0; --i)
1583 dims[i] = dims[i-1];
1584 dims[0] = sacado_size;
1585 }
1586 else {
1587 dims[rank] = sacado_size;
1588 }
1589 typedef typename DstType::offset_type dst_offset_type;
1590 dst.m_impl_offset = dst_offset_type( std::integral_constant< unsigned , 0 >(),
1591 typename DstTraits::array_layout(
1592 dims[0] , dims[1] , dims[2] , dims[3] ,
1593 dims[4] , dims[5] , dims[6] , dims[7] ) );
1594
1595 // For CudaLDGFetch, which doesn't define operator=() for pointer RHS
1596 // but does define a constructor
1597 //dst.m_impl_handle = src.m_impl_handle.scalar_ptr ;
1598 dst.m_impl_handle = typename DstType::handle_type(src.m_impl_handle.scalar_ptr);
1599 }
1600 };
1601
1602 /**\brief Assign compatible Sacado::UQ::PCE view mappings.
1603 *
1604 * View<ordinary> = View<UQ::PCE>
1605 * where View<ordinay>::Rank = View<UQ::PCE>::Rank, i.e., assigning
1606 * to the "flattened" view type
1607 */
1608 template< class DstTraits , class SrcTraits >
1609 class ViewMapping< DstTraits , SrcTraits ,
1610 typename std::enable_if<(
1611 Kokkos::Impl::MemorySpaceAccess< typename DstTraits::memory_space
1612 , typename SrcTraits::memory_space >::assignable
1613 &&
1614 // Destination view has ordinary
1615 std::is_same< typename DstTraits::specialize , void >::value
1616 &&
1617 // Source view has UQ::PCE only
1618 std::is_same< typename SrcTraits::specialize
1619 , Kokkos::Experimental::Impl::ViewPCEContiguous >::value
1620 &&
1621 // Ranks match
1622 unsigned(DstTraits::dimension::rank) == unsigned(SrcTraits::dimension::rank)
1623 )
1624 , typename DstTraits::specialize
1625 >::type >
1626 {
1627 public:
1628
1629 enum { is_assignable = true };
1630 enum { is_assignable_data_type = true };
1631
1632 typedef Kokkos::Impl::SharedAllocationTracker TrackType ;
1633 typedef ViewMapping< DstTraits , typename DstTraits::specialize > DstType ;
1634 typedef ViewMapping< SrcTraits , typename SrcTraits::specialize > SrcType ;
1635
1636 KOKKOS_INLINE_FUNCTION static
1637 void assign( DstType & dst
1638 , const SrcType & src
1639 , const TrackType & )
1640 {
1641 static_assert(
1642 (
1643 std::is_same< typename DstTraits::array_layout
1644 , Kokkos::LayoutLeft >::value ||
1645 std::is_same< typename DstTraits::array_layout
1646 , Kokkos::LayoutRight >::value ||
1647 std::is_same< typename DstTraits::array_layout
1648 , Kokkos::LayoutStride >::value
1649 )
1650 &&
1651 (
1652 std::is_same< typename SrcTraits::array_layout
1653 , Kokkos::LayoutLeft >::value ||
1654 std::is_same< typename SrcTraits::array_layout
1655 , Kokkos::LayoutRight >::value ||
1656 std::is_same< typename SrcTraits::array_layout
1657 , Kokkos::LayoutStride >::value
1658 )
1659 , "View of UQ::PCE requires LayoutLeft, LayoutRight, or LayoutStride" );
1660
1661 static_assert(
1662 std::is_same< typename DstTraits::array_layout
1663 , typename SrcTraits::array_layout >::value ||
1664 std::is_same< typename DstTraits::array_layout
1665 , Kokkos::LayoutStride >::value ,
1666 "View assignment must have compatible layout" );
1667
1668 static_assert(
1669 std::is_same< typename DstTraits::value_type
1670 , typename SrcTraits::non_const_value_type::value_type >::value ||
1671 std::is_same< typename DstTraits::value_type
1672 , const typename SrcTraits::non_const_value_type::value_type >::value ,
1673 "View assignment must have same value type or const = non-const" );
1674
1675 static_assert(
1676 ViewDimensionAssignable<
1677 typename DstType::offset_type::dimension_type,
1678 typename SrcType::offset_type::dimension_type >::value,
1679 "View assignment must have compatible dimensions" );
1680
1681 if ( !src.m_is_contiguous )
1682 Kokkos::abort("\n\n ****** Kokkos::View< Sacado::UQ::PCE ... >: can't assign non-contiguous view ******\n\n");
1683
1684 unsigned dims[8];
1685 dims[0] = src.m_impl_offset.dimension_0();
1686 dims[1] = src.m_impl_offset.dimension_1();
1687 dims[2] = src.m_impl_offset.dimension_2();
1688 dims[3] = src.m_impl_offset.dimension_3();
1689 dims[4] = src.m_impl_offset.dimension_4();
1690 dims[5] = src.m_impl_offset.dimension_5();
1691 dims[6] = src.m_impl_offset.dimension_6();
1692 dims[7] = src.m_impl_offset.dimension_7();
1693 unsigned rank = SrcTraits::dimension::rank;
1694 unsigned sacado_size = src.m_sacado_size;
1695 if (std::is_same<typename DstTraits::array_layout, LayoutLeft>::value) {
1696 dims[0] = dims[0]*sacado_size;
1697 dims[rank] = 0;
1698 }
1699 else {
1700 dims[rank-1] = dims[rank-1]*sacado_size;
1701 dims[rank] = 0;
1702 }
1703 typedef typename DstType::offset_type dst_offset_type;
1704 dst.m_impl_offset = dst_offset_type( std::integral_constant< unsigned , 0 >(),
1705 typename DstTraits::array_layout(
1706 dims[0] , dims[1] , dims[2] , dims[3] ,
1707 dims[4] , dims[5] , dims[6] , dims[7] ) );
1708 dst.m_impl_handle = src.m_impl_handle.scalar_ptr ;
1709 }
1710 };
1711
1712 } // namespace Impl
1713 } // namespace Kokkos
1714
1715 //----------------------------------------------------------------------------
1716
1717 namespace Kokkos {
1718 namespace Impl {
1719
1720 // Subview mapping
1721
1722 template< class DataType, class ... P , class Arg0, class ... Args >
1723 struct ViewMapping
1724 < typename std::enable_if<(
1725 // Source view has UQ::PCE only
1726 std::is_same< typename Kokkos::ViewTraits<DataType,P...>::specialize
1727 , Kokkos::Experimental::Impl::ViewPCEContiguous >::value
1728 &&
1729 (
1730 std::is_same< typename Kokkos::ViewTraits<DataType,P...>::array_layout
1731 , Kokkos::LayoutLeft >::value ||
1732 std::is_same< typename Kokkos::ViewTraits<DataType,P...>::array_layout
1733 , Kokkos::LayoutRight >::value ||
1734 std::is_same< typename Kokkos::ViewTraits<DataType,P...>::array_layout
1735 , Kokkos::LayoutStride >::value
1736 )
1737 )>::type
1738 , Kokkos::ViewTraits<DataType,P...>
1739 , Arg0, Args ... >
1740 {
1741 private:
1742
1743 typedef Kokkos::ViewTraits<DataType,P...> SrcTraits;
1744
1745 //static_assert( SrcTraits::rank == sizeof...(Args) , "" );
1746
1747 enum
1748 { RZ = false
1749 , R0 = bool(is_integral_extent<0,Arg0,Args...>::value)
1750 , R1 = bool(is_integral_extent<1,Arg0,Args...>::value)
1751 , R2 = bool(is_integral_extent<2,Arg0,Args...>::value)
1752 , R3 = bool(is_integral_extent<3,Arg0,Args...>::value)
1753 , R4 = bool(is_integral_extent<4,Arg0,Args...>::value)
1754 , R5 = bool(is_integral_extent<5,Arg0,Args...>::value)
1755 , R6 = bool(is_integral_extent<6,Arg0,Args...>::value)
1756 };
1757
1758 // Public rank
1759 enum { rank = unsigned(R0) + unsigned(R1) + unsigned(R2) + unsigned(R3)
1760 + unsigned(R4) + unsigned(R5) + unsigned(R6) };
1761
1762 // Whether right-most non-UQ::PCE rank is a range.
1763 enum { R0_rev = ( 0 == SrcTraits::rank ? RZ : (
1764 1 == SrcTraits::rank ? R0 : (
1765 2 == SrcTraits::rank ? R1 : (
1766 3 == SrcTraits::rank ? R2 : (
1767 4 == SrcTraits::rank ? R3 : (
1768 5 == SrcTraits::rank ? R4 : (
1769 6 == SrcTraits::rank ? R5 : R6 ))))))) };
1770
1771 // Subview's layout
1772 typedef typename std::conditional<
1773 ( /* Same array layout IF */
1774 ( rank == 0 ) /* output rank zero */
1775 ||
1776 // OutputRank 1 or 2, InputLayout Left, Interval 0
1777 // because single stride one or second index has a stride.
1778 ( rank <= 2 && R0 && std::is_same< typename SrcTraits::array_layout , Kokkos::LayoutLeft >::value )
1779 ||
1780 // OutputRank 1 or 2, InputLayout Right, Interval [InputRank-1]
1781 // because single stride one or second index has a stride.
1782 ( rank <= 2 && R0_rev && std::is_same< typename SrcTraits::array_layout , Kokkos::LayoutRight >::value )
1783 ), typename SrcTraits::array_layout , Kokkos::LayoutStride
1784 >::type array_layout ;
1785
1786 typedef typename SrcTraits::value_type sacado_uq_pce_type ;
1787
1788 typedef typename std::conditional< rank == 0 , sacado_uq_pce_type ,
1789 typename std::conditional< rank == 1 , sacado_uq_pce_type * ,
1790 typename std::conditional< rank == 2 , sacado_uq_pce_type ** ,
1791 typename std::conditional< rank == 3 , sacado_uq_pce_type *** ,
1792 typename std::conditional< rank == 4 , sacado_uq_pce_type **** ,
1793 typename std::conditional< rank == 5 , sacado_uq_pce_type ***** ,
1794 typename std::conditional< rank == 6 , sacado_uq_pce_type ****** ,
1795 sacado_uq_pce_type *******
1796 >::type >::type >::type >::type >::type >::type >::type
1797 data_type ;
1798
1799 public:
1800
1801 typedef Kokkos::ViewTraits
1802 < data_type
1803 , array_layout
1804 , typename SrcTraits::device_type
1805 , typename SrcTraits::memory_traits > traits_type ;
1806
1807 typedef Kokkos::View
1808 < data_type
1809 , array_layout
1810 , typename SrcTraits::device_type
1811 , typename SrcTraits::memory_traits > type ;
1812
1813
1814 // The presumed type is 'ViewMapping< traits_type , void >'
1815 // However, a compatible ViewMapping is acceptable.
1816 template< class DstTraits >
1817 KOKKOS_INLINE_FUNCTION
1818 static void assign( ViewMapping< DstTraits , typename DstTraits::specialize > & dst
1819 , ViewMapping< SrcTraits , typename SrcTraits::specialize > const & src
1820 , Arg0 arg0, Args ... args )
1821 {
1822 static_assert(
1823 ViewMapping< DstTraits , traits_type , typename DstTraits::specialize >::is_assignable ,
1824 "Subview destination type must be compatible with subview derived type" );
1825
1826 typedef ViewMapping< DstTraits , typename DstTraits::specialize > DstType ;
1827 typedef typename DstType::offset_type dst_offset_type ;
1828
1829 const SubviewExtents< SrcTraits::rank , rank >
1830 extents( src.m_impl_offset.m_dim , arg0 , args... );
1831
1832 const size_t offset = src.m_impl_offset( extents.domain_offset(0)
1833 , extents.domain_offset(1)
1834 , extents.domain_offset(2)
1835 , extents.domain_offset(3)
1836 , extents.domain_offset(4)
1837 , extents.domain_offset(5)
1838 , extents.domain_offset(6)
1839 , extents.domain_offset(7) );
1840
1841 dst.m_impl_offset = dst_offset_type( src.m_impl_offset , extents );
1842 dst.m_impl_handle.value_ptr = src.m_impl_handle.value_ptr + offset;
1843 dst.m_impl_handle.scalar_ptr =
1844 src.m_impl_handle.scalar_ptr + offset * src.m_sacado_size;
1845 dst.m_sacado_size = src.m_sacado_size;
1846 dst.m_cijk = src.m_cijk;
1847 dst.m_is_contiguous = src.m_is_contiguous;
1848 }
1849
1850 };
1851
1852 } // namespace Impl
1853 } // namespace Kokkos
1854
1855 //----------------------------------------------------------------------------
1856 //----------------------------------------------------------------------------
1857 //----------------------------------------------------------------------------
1858
1859 namespace Kokkos {
1860 namespace Impl {
1861
1862 // Specialization for deep_copy( view, view::value_type ) for Cuda
1863 #if defined( KOKKOS_ENABLE_CUDA )
1864 template< class OutputView >
1865 struct StokhosViewFill< OutputView ,
1866 typename std::enable_if< std::is_same< typename OutputView::specialize,
1867 Kokkos::Experimental::Impl::ViewPCEContiguous >::value &&
1868 std::is_same< typename OutputView::execution_space,
1869 Cuda >::value >::type >
1870 {
1871 typedef typename OutputView::const_value_type const_value_type ;
1872 typedef typename Sacado::ScalarType<const_value_type>::type scalar_type ;
1873 typedef typename OutputView::execution_space execution_space ;
1874 typedef typename OutputView::size_type size_type ;
1875
1876 template <unsigned VectorLength>
1877 struct PCEKernel {
1878 typedef typename OutputView::execution_space execution_space ;
1879 const OutputView output;
1880 const_value_type input;
1881
1882 PCEKernel( const OutputView & arg_out , const_value_type & arg_in ) :
1883 output(arg_out), input(arg_in) {}
1884
1885 typedef typename Kokkos::TeamPolicy< execution_space >::member_type team_member ;
1886
1887 KOKKOS_INLINE_FUNCTION
1888 void operator()( const team_member & dev ) const
1889 {
1890 const size_type tidx = dev.team_rank() % VectorLength;
1891 const size_type tidy = dev.team_rank() / VectorLength;
1892 const size_type nrow = dev.team_size() / VectorLength;
1893 const size_type nvec = dimension_scalar(output);
1894
1895 const size_type i0 = dev.league_rank() * nrow + tidy;
1896 if ( i0 >= output.extent(0) ) return;
1897
1898 for ( size_type i1 = 0 ; i1 < output.extent(1) ; ++i1 ) {
1899 for ( size_type i2 = 0 ; i2 < output.extent(2) ; ++i2 ) {
1900 for ( size_type i3 = 0 ; i3 < output.extent(3) ; ++i3 ) {
1901 for ( size_type i4 = 0 ; i4 < output.extent(4) ; ++i4 ) {
1902 for ( size_type i5 = 0 ; i5 < output.extent(5) ; ++i5 ) {
1903 for ( size_type i6 = 0 ; i6 < output.extent(6) ; ++i6 ) {
1904 for ( size_type i7 = 0 ; i7 < output.extent(7) ; ++i7 ) {
1905 for ( size_type is = tidx ; is < nvec ; is+=VectorLength ) {
1906 output.access(i0,i1,i2,i3,i4,i5,i6,i7).fastAccessCoeff(is) =
1907 input.fastAccessCoeff(is) ;
1908 }}}}}}}}
1909 }
1910 };
1911
1912 template <unsigned VectorLength>
1913 struct ScalarKernel {
1914 typedef typename OutputView::execution_space execution_space ;
1915 const OutputView output;
1916 const scalar_type input;
1917
1918 ScalarKernel( const OutputView & arg_out , const scalar_type & arg_in ) :
1919 output(arg_out), input(arg_in) {}
1920
1921 typedef typename Kokkos::TeamPolicy< execution_space >::member_type team_member ;
1922 KOKKOS_INLINE_FUNCTION
1923 void operator()( const team_member & dev ) const
1924 {
1925 const size_type tidx = dev.team_rank() % VectorLength;
1926 const size_type tidy = dev.team_rank() / VectorLength;
1927 const size_type nrow = dev.team_size() / VectorLength;
1928 const size_type npce = dimension_scalar(output);
1929
1930 const size_type i0 = dev.league_rank() * nrow + tidy;
1931 if ( i0 >= output.extent(0) ) return;
1932
1933 for ( size_type i1 = 0 ; i1 < output.extent(1) ; ++i1 ) {
1934 for ( size_type i2 = 0 ; i2 < output.extent(2) ; ++i2 ) {
1935 for ( size_type i3 = 0 ; i3 < output.extent(3) ; ++i3 ) {
1936 for ( size_type i4 = 0 ; i4 < output.extent(4) ; ++i4 ) {
1937 for ( size_type i5 = 0 ; i5 < output.extent(5) ; ++i5 ) {
1938 for ( size_type i6 = 0 ; i6 < output.extent(6) ; ++i6 ) {
1939 for ( size_type i7 = 0 ; i7 < output.extent(7) ; ++i7 ) {
1940 for ( size_type is = tidx ; is < npce ; is+=VectorLength ) {
1941 output.access(i0,i1,i2,i3,i4,i5,i6,i7).fastAccessCoeff(is) =
1942 is == 0 ? input : scalar_type(0) ;
1943 }}}}}}}}
1944 }
1945 };
1946
1947 StokhosViewFill( const OutputView & output , const_value_type & input )
1948 {
1949 // Coalesced accesses are 128 bytes in size
1950 typedef typename OutputView::array_type::value_type scalar_type;
1951 const unsigned vector_length =
1952 ( 128 + sizeof(scalar_type)-1 ) / sizeof(scalar_type);
1953
1954 // 8 warps per block should give good occupancy
1955 const size_type block_size = 256;
1956
1957 const size_type rows_per_block = block_size / vector_length;
1958 const size_type n = output.extent(0);
1959 const size_type league_size = ( n + rows_per_block-1 ) / rows_per_block;
1960 const size_type team_size = rows_per_block * vector_length;
1961 Kokkos::TeamPolicy< execution_space > config( league_size, team_size );
1962
1963 if (static_cast<unsigned>(input.size()) != dimension_scalar(output) &&
1964 input.size() != 1)
1965 Kokkos::abort("StokhosViewFill: Invalid input value size");
1966
1967 if (input.size() == 1)
1968 parallel_for(
1969 config, ScalarKernel<vector_length>(output, input.fastAccessCoeff(0)) );
1970 else
1971 parallel_for( config, PCEKernel<vector_length>(output, input) );
1972 execution_space().fence();
1973 }
1974
1975 StokhosViewFill( const OutputView & output , const scalar_type & input )
1976 {
1977 // Coalesced accesses are 128 bytes in size
1978 typedef typename OutputView::array_type::value_type scalar_type;
1979 const unsigned vector_length =
1980 ( 128 + sizeof(scalar_type)-1 ) / sizeof(scalar_type);
1981
1982 // 8 warps per block should give good occupancy
1983 const size_type block_size = 256;
1984
1985 const size_type rows_per_block = block_size / vector_length;
1986 const size_type n = output.extent(0);
1987 const size_type league_size = ( n + rows_per_block-1 ) / rows_per_block;
1988 const size_type team_size = rows_per_block * vector_length;
1989 Kokkos::TeamPolicy< execution_space > config( league_size, team_size );
1990
1991 parallel_for( config, ScalarKernel<vector_length>(output, input) );
1992 execution_space().fence();
1993 }
1994
1995 };
1996 #endif /* #if defined( KOKKOS_ENABLE_CUDA ) */
1997
1998 } // namespace Impl
1999 } // namespace Kokkos
2000
2001 #include "Kokkos_View_Utils_Def.hpp"
2002
2003 //----------------------------------------------------------------------------
2004 //----------------------------------------------------------------------------
2005 //----------------------------------------------------------------------------
2006
2007 #endif /* #ifndef KOKKOS_EXPERIMENTAL_VIEW_UQ_PCE_CONTIGUOUS_HPP */