Sacado Package Browser (Single Doxygen Collection)  Version of the Day
Kokkos_DynRankView_Fad_Contiguous.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_DYN_RANK_VIEW_SACADO_FAD_CONTIGUOUS_HPP
31 #define KOKKOS_DYN_RANK_VIEW_SACADO_FAD_CONTIGUOUS_HPP
32 
33 #include "Sacado_ConfigDefs.h"
34 
35 // This file is setup to always work even when KokkosContainers (which contains
36 // Kokkos::DynRankView) isn't enabled.
37 
38 #if defined(HAVE_SACADO_KOKKOSCONTAINERS)
39 
40 #include "Kokkos_DynRankView.hpp"
41 
42 #if defined(HAVE_SACADO_VIEW_SPEC) && !defined(SACADO_DISABLE_FAD_VIEW_SPEC)
43 
44 #include "Kokkos_View_Fad.hpp"
45 
46 namespace Kokkos {
47 namespace Impl {
48 
49 template <>
50 struct DynRankDimTraits<Kokkos::Impl::ViewSpecializeSacadoFadContiguous> {
51 
52  enum : size_t{unspecified = ~size_t(0)};
53 
54  // Compute the rank of the view from the nonzero dimension arguments.
55  // For views of Fad, the rank is one less than the rank determined by the nonzero dimension args
57  static size_t computeRank( const size_t N0
58  , const size_t N1
59  , const size_t N2
60  , const size_t N3
61  , const size_t N4
62  , const size_t N5
63  , const size_t N6
64  , const size_t N7 )
65  {
66  return
67  ( (N7 == unspecified && N6 == unspecified && N5 == unspecified && N4 == unspecified && N3 == unspecified && N2 == unspecified && N1 == unspecified && N0 == unspecified) ? 0
68  : ( (N7 == unspecified && N6 == unspecified && N5 == unspecified && N4 == unspecified && N3 == unspecified && N2 == unspecified && N1 == unspecified) ? 0
69  : ( (N7 == unspecified && N6 == unspecified && N5 == unspecified && N4 == unspecified && N3 == unspecified && N2 == unspecified) ? 1
70  : ( (N7 == unspecified && N6 == unspecified && N5 == unspecified && N4 == unspecified && N3 == unspecified) ? 2
71  : ( (N7 == unspecified && N6 == unspecified && N5 == unspecified && N4 == unspecified) ? 3
72  : ( (N7 == unspecified && N6 == unspecified && N5 == unspecified) ? 4
73  : ( (N7 == unspecified && N6 == unspecified) ? 5
74  : ( (N7 == unspecified) ? 6
75  : 7 ) ) ) ) ) ) ) );
76  }
77 
78  // Compute the rank of the view from the nonzero layout arguments.
79  template <typename Layout>
81  static size_t computeRank( const Layout& layout )
82  {
83  return computeRank( layout.dimension[0]
84  , layout.dimension[1]
85  , layout.dimension[2]
86  , layout.dimension[3]
87  , layout.dimension[4]
88  , layout.dimension[5]
89  , layout.dimension[6]
90  , layout.dimension[7] );
91  }
92 
93  // Compute the rank of the view from the nonzero layout arguments and possible hidden dim
94  template <typename Layout, typename ... P>
96  static size_t computeRank( const ViewCtorProp<P...>& prop, const Layout& layout )
97  {
98  size_t rank = computeRank( layout.dimension[0]
99  , layout.dimension[1]
100  , layout.dimension[2]
101  , layout.dimension[3]
102  , layout.dimension[4]
103  , layout.dimension[5]
104  , layout.dimension[6]
105  , layout.dimension[7] );
106 
107  // Check if has_common_view_alloc_prop; if so, return rank+1, else rank
108  enum { test_traits_check = Kokkos::Impl::check_has_common_view_alloc_prop< P... >::value };
109  return (test_traits_check == true) ? rank+1 : rank;
110  }
111 
112  // Create the layout for the rank-7 view.
113  // For Fad we have to move the fad dimension to the last (rank 8 since the DynRankView is rank-7)
114  // LayoutLeft or LayoutRight
115  template <typename Layout>
117  static typename std::enable_if< (std::is_same<Layout , Kokkos::LayoutRight>::value || std::is_same<Layout , Kokkos::LayoutLeft>::value) , Layout >::type createLayout( const Layout& layout )
118  {
119  Layout l( layout.dimension[0] != unspecified ? layout.dimension[0] : 1
120  , layout.dimension[1] != unspecified ? layout.dimension[1] : 1
121  , layout.dimension[2] != unspecified ? layout.dimension[2] : 1
122  , layout.dimension[3] != unspecified ? layout.dimension[3] : 1
123  , layout.dimension[4] != unspecified ? layout.dimension[4] : 1
124  , layout.dimension[5] != unspecified ? layout.dimension[5] : 1
125  , layout.dimension[6] != unspecified ? layout.dimension[6] : 1
126  , layout.dimension[7] != unspecified ? layout.dimension[7] : 1 );
127  const unsigned fad_dim = computeRank(layout);
128  const size_t fad_size = layout.dimension[fad_dim];
129  l.dimension[fad_dim] = 1;
130  l.dimension[7] = fad_size;
131 
132  return l;
133  }
134 
135  //LayoutStride
136  template <typename Layout>
138  static typename std::enable_if< (std::is_same<Layout , Kokkos::LayoutStride>::value) , Layout>::type createLayout( const Layout& layout )
139  {
140  Layout l( layout.dimension[0] != unspecified ? layout.dimension[0] : 1
141  , layout.stride[0]
142  , layout.dimension[1] != unspecified ? layout.dimension[1] : 1
143  , layout.stride[1]
144  , layout.dimension[2] != unspecified ? layout.dimension[2] : 1
145  , layout.stride[2]
146  , layout.dimension[3] != unspecified ? layout.dimension[3] : 1
147  , layout.stride[3]
148  , layout.dimension[4] != unspecified ? layout.dimension[4] : 1
149  , layout.stride[4]
150  , layout.dimension[5] != unspecified ? layout.dimension[5] : 1
151  , layout.stride[5]
152  , layout.dimension[6] != unspecified ? layout.dimension[6] : 1
153  , layout.stride[6]
154  , layout.dimension[7] != unspecified ? layout.dimension[7] : 1
155  , layout.stride[7]
156  );
157  const unsigned fad_dim = computeRank(layout);
158  const size_t fad_size = layout.dimension[fad_dim];
159  l.dimension[fad_dim] = 1;
160  l.dimension[7] = fad_size;
161 
162  return l;
163  }
164 
165  // If fad_dim is stored in ViewCtorProp
166  // LayoutLeft or LayoutRight
167  template <typename Traits, typename ... P>
169  static typename std::enable_if< (std::is_same<typename Traits::array_layout , Kokkos::LayoutRight>::value || std::is_same<typename Traits::array_layout , Kokkos::LayoutLeft>::value) , typename Traits::array_layout >::type createLayout( const ViewCtorProp<P...> & arg_prop, const typename Traits::array_layout& layout )
170  {
171  using Layout = typename Traits::array_layout;
172 
173  Layout l( layout.dimension[0] != unspecified ? layout.dimension[0] : 1
174  , layout.dimension[1] != unspecified ? layout.dimension[1] : 1
175  , layout.dimension[2] != unspecified ? layout.dimension[2] : 1
176  , layout.dimension[3] != unspecified ? layout.dimension[3] : 1
177  , layout.dimension[4] != unspecified ? layout.dimension[4] : 1
178  , layout.dimension[5] != unspecified ? layout.dimension[5] : 1
179  , layout.dimension[6] != unspecified ? layout.dimension[6] : 1
180  , layout.dimension[7] != unspecified ? layout.dimension[7] : 1 );
181 
182  enum { test_traits_check = Kokkos::Impl::check_has_common_view_alloc_prop< P... >::value };
183  if (test_traits_check == true) {
184  using CVTR_type = typename Kokkos::Impl::CommonViewAllocProp< typename Kokkos::Impl::ViewSpecializeSacadoFadContiguous, typename Traits::value_type >;
185  auto cast_prop = ((Kokkos::Impl::ViewCtorProp<void, CVTR_type> const &)arg_prop).value;
186  l.dimension[7] = cast_prop.fad_dim;
187  }
188  else {
189  const unsigned fad_dim = computeRank(layout);
190  const size_t fad_size = layout.dimension[fad_dim];
191  l.dimension[fad_dim] = 1;
192  l.dimension[7] = fad_size;
193  }
194 
195  return l;
196  }
197 
198  // If fad_dim is stored in ViewCtorProp
199  //LayoutStride
200  template <typename Traits, typename ... P>
202  static typename std::enable_if< (std::is_same<typename Traits::array_layout , Kokkos::LayoutStride>::value) , typename Traits::array_layout>::type createLayout( const ViewCtorProp<P...> & arg_prop, const typename Traits::array_layout& layout )
203  {
204  using Layout = typename Traits::array_layout;
205 
206  Layout l( layout.dimension[0] != unspecified ? layout.dimension[0] : 1
207  , layout.stride[0]
208  , layout.dimension[1] != unspecified ? layout.dimension[1] : 1
209  , layout.stride[1]
210  , layout.dimension[2] != unspecified ? layout.dimension[2] : 1
211  , layout.stride[2]
212  , layout.dimension[3] != unspecified ? layout.dimension[3] : 1
213  , layout.stride[3]
214  , layout.dimension[4] != unspecified ? layout.dimension[4] : 1
215  , layout.stride[4]
216  , layout.dimension[5] != unspecified ? layout.dimension[5] : 1
217  , layout.stride[5]
218  , layout.dimension[6] != unspecified ? layout.dimension[6] : 1
219  , layout.stride[6]
220  , layout.dimension[7] != unspecified ? layout.dimension[7] : 1
221  , layout.stride[7]
222  );
223 
224  enum { test_traits_check = Kokkos::Impl::check_has_common_view_alloc_prop< P... >::value };
225  if (test_traits_check == true) {
226  using CVTR_type = typename Kokkos::Impl::CommonViewAllocProp< typename Kokkos::Impl::ViewSpecializeSacadoFadContiguous, typename Traits::value_type >;
227  auto cast_prop = ((Kokkos::Impl::ViewCtorProp<void, CVTR_type> const &)arg_prop).value;
228  l.dimension[7] = cast_prop.fad_dim;
229  }
230  else {
231  const unsigned fad_dim = computeRank(layout);
232  const size_t fad_size = layout.dimension[fad_dim];
233  l.dimension[fad_dim] = 1;
234  l.dimension[7] = fad_size;
235  }
236 
237  return l;
238  }
239 
240 
241  // Create a view from the given dimension arguments.
242  // This is only necessary because the shmem constructor doesn't take a layout.
243  template <typename ViewType, typename ViewArg>
244  static ViewType createView( const ViewArg& arg
245  , const size_t N0
246  , const size_t N1
247  , const size_t N2
248  , const size_t N3
249  , const size_t N4
250  , const size_t N5
251  , const size_t N6
252  , const size_t N7 )
253  {
254  typename ViewType::array_layout l( N0, N1, N2, N3, N4, N5, N6, N7 );
255  typename ViewType::array_layout l_fad = createLayout(l);
256  return ViewType( arg
257  , l_fad.dimension[0]
258  , l_fad.dimension[1]
259  , l_fad.dimension[2]
260  , l_fad.dimension[3]
261  , l_fad.dimension[4]
262  , l_fad.dimension[5]
263  , l_fad.dimension[6]
264  , l_fad.dimension[7] );
265  }
266 
267 };
268 
269 }} // end Kokkos::Impl
270 
271 namespace Kokkos {
272 namespace Impl {
273 
274 // Specializations for subdynrankview
275 
276 template< class SrcTraits , class ... Args >
277 struct ViewMapping
278  < typename std::enable_if<(
279  std::is_same< typename SrcTraits::specialize ,
280  Kokkos::Impl::ViewSpecializeSacadoFadContiguous >::value
281  &&
282  (
283  std::is_same< typename SrcTraits::array_layout
284  , Kokkos::LayoutRight >::value ||
285  std::is_same< typename SrcTraits::array_layout
286  , Kokkos::LayoutStride >::value
287  )
288  ), Kokkos::Impl::DynRankSubviewTag >::type
289  , SrcTraits
290  , Args ... >
291 {
292 private:
293 
294  enum
295  { RZ = false
296  , R0 = bool(is_integral_extent<0,Args...>::value)
297  , R1 = bool(is_integral_extent<1,Args...>::value)
298  , R2 = bool(is_integral_extent<2,Args...>::value)
299  , R3 = bool(is_integral_extent<3,Args...>::value)
300  , R4 = bool(is_integral_extent<4,Args...>::value)
301  , R5 = bool(is_integral_extent<5,Args...>::value)
302  , R6 = bool(is_integral_extent<6,Args...>::value)
303  };
304 
305  enum { rank = unsigned(R0) + unsigned(R1) + unsigned(R2) + unsigned(R3)
306  + unsigned(R4) + unsigned(R5) + unsigned(R6) };
307 
309 
310  typedef typename SrcTraits::value_type value_type ;
311 
312  typedef value_type******* data_type ;
313 
314 public:
315 
316  typedef Kokkos::ViewTraits
317  < data_type
318  , array_layout
319  , typename SrcTraits::device_type
320  , typename SrcTraits::memory_traits > traits_type ;
321 
322  typedef Kokkos::View
323  < data_type
324  , array_layout
325  , typename SrcTraits::device_type
326  , typename SrcTraits::memory_traits > type ;
327 
328 
329  template< class MemoryTraits >
330  struct apply {
331 
332  static_assert( Kokkos::Impl::is_memory_traits< MemoryTraits >::value , "" );
333 
334  typedef Kokkos::ViewTraits
335  < data_type
336  , array_layout
337  , typename SrcTraits::device_type
338  , MemoryTraits > traits_type ;
339 
340  typedef Kokkos::View
341  < data_type
342  , array_layout
343  , typename SrcTraits::device_type
344  , MemoryTraits > type ;
345  };
346 
347  template < class Arg0 = int, class Arg1 = int, class Arg2 = int, class Arg3 = int, class Arg4 = int, class Arg5 = int, class Arg6 = int >
348  struct ExtentGenerator {
349  template <typename dimension>
351  static SubviewExtents< 7 , rank > generator ( const dimension & dim , Arg0 arg0 = Arg0(), Arg1 arg1 = Arg1(), Arg2 arg2 = Arg2(), Arg3 arg3 = Arg3(), Arg4 arg4 = Arg4(), Arg5 arg5 = Arg5(), Arg6 arg6 = Arg6() )
352  {
353  return SubviewExtents< 7 , rank >( dim , arg0 , arg1 , arg2 , arg3 ,
354  arg4 , arg5 , arg6 );
355  }
356  };
357 
358  template < class Arg0 = int, class Arg1 = int, class Arg2 = int, class Arg3 = int, class Arg4 = int, class Arg5 = int, class Arg6 = int >
359  struct ArrayExtentGenerator {
360  template <typename dimension>
362  static SubviewExtents< 8 , rank+1 > generator ( const dimension & dim , Arg0 arg0 = Arg0(), Arg1 arg1 = Arg1(), Arg2 arg2 = Arg2(), Arg3 arg3 = Arg3(), Arg4 arg4 = Arg4(), Arg5 arg5 = Arg5(), Arg6 arg6 = Arg6() )
363  {
364  return SubviewExtents< 8 , rank+1 >( dim , arg0 , arg1 , arg2 , arg3 ,
365  arg4 , arg5 , arg6 , Kokkos::ALL() );
366  }
367  };
368 
369  typedef DynRankView< value_type , array_layout , typename SrcTraits::device_type , typename SrcTraits::memory_traits > ret_type;
370 
371  template < typename T , class ... P >
373  static ret_type subview( const unsigned src_rank , Kokkos::DynRankView< T , P...> const & src , Args ... args )
374  {
375 
376  typedef ViewMapping< traits_type, void > DstType ;
377  typedef typename std::conditional< (rank==0) , ViewDimension<>
378  , typename std::conditional< (rank==1) , ViewDimension<0>
379  , typename std::conditional< (rank==2) , ViewDimension<0,0>
380  , typename std::conditional< (rank==3) , ViewDimension<0,0,0>
381  , typename std::conditional< (rank==4) , ViewDimension<0,0,0,0>
382  , typename std::conditional< (rank==5) , ViewDimension<0,0,0,0,0>
383  , typename std::conditional< (rank==6) , ViewDimension<0,0,0,0,0,0>
384  , ViewDimension<0,0,0,0,0,0,0>
385  >::type >::type >::type >::type >::type >::type >::type DstDimType ;
386  typedef typename std::conditional< (rank==0) , ViewDimension<0>
387  , typename std::conditional< (rank==1) , ViewDimension<0,0>
388  , typename std::conditional< (rank==2) , ViewDimension<0,0,0>
389  , typename std::conditional< (rank==3) , ViewDimension<0,0,0,0>
390  , typename std::conditional< (rank==4) , ViewDimension<0,0,0,0,0>
391  , typename std::conditional< (rank==5) , ViewDimension<0,0,0,0,0,0>
392  , typename std::conditional< (rank==6) , ViewDimension<0,0,0,0,0,0,0>
393  , ViewDimension<0,0,0,0,0,0,0,0>
394  >::type >::type >::type >::type >::type >::type >::type DstArrayDimType ;
395 
396  typedef ViewOffset< DstDimType , Kokkos::LayoutStride > dst_offset_type ;
397  typedef ViewOffset< DstArrayDimType , Kokkos::LayoutStride > dst_array_offset_type ;
398  typedef typename DstType::handle_type dst_handle_type ;
399 
400  ret_type dst ;
401 
402  const SubviewExtents< 7 , rank > extents =
403  ExtentGenerator< Args ... >::generator(
404  src.m_map.m_offset.m_dim , args... ) ;
405  const SubviewExtents< 8 , rank+1 > array_extents =
406  ArrayExtentGenerator< Args ... >::generator(
407  src.m_map.m_array_offset.m_dim , args... ) ;
408 
409  dst_offset_type tempdst( src.m_map.m_offset , extents ) ;
410  dst_array_offset_type temparraydst(
411  src.m_map.m_array_offset , array_extents ) ;
412 
413  dst.m_track = src.m_track ;
414 
415  dst.m_map.m_offset.m_dim.N0 = tempdst.m_dim.N0 ;
416  dst.m_map.m_offset.m_dim.N1 = tempdst.m_dim.N1 ;
417  dst.m_map.m_offset.m_dim.N2 = tempdst.m_dim.N2 ;
418  dst.m_map.m_offset.m_dim.N3 = tempdst.m_dim.N3 ;
419  dst.m_map.m_offset.m_dim.N4 = tempdst.m_dim.N4 ;
420  dst.m_map.m_offset.m_dim.N5 = tempdst.m_dim.N5 ;
421  dst.m_map.m_offset.m_dim.N6 = tempdst.m_dim.N6 ;
422 
423  dst.m_map.m_offset.m_stride.S0 = tempdst.m_stride.S0;
424  dst.m_map.m_offset.m_stride.S1 = tempdst.m_stride.S1;
425  dst.m_map.m_offset.m_stride.S2 = tempdst.m_stride.S2;
426  dst.m_map.m_offset.m_stride.S3 = tempdst.m_stride.S3;
427  dst.m_map.m_offset.m_stride.S4 = tempdst.m_stride.S4;
428  dst.m_map.m_offset.m_stride.S5 = tempdst.m_stride.S5;
429  dst.m_map.m_offset.m_stride.S6 = tempdst.m_stride.S6;
430 
431  // Move last non-unit dim and stride to N7/S7 since subview collapses
432  // out all singleton dimensions between the last rank and the fad
433  // dimension. Equivalent to:
434  // dst.m_map.m_array_offset.m_dim.N* = temparraydst.m_dim.N*
435  // dst.m_map.m_array_offset.m_dim.N7 = temparraydst.m_dim.N{rank}
436  // dst.m_map.m_array_offset.m_stride.S* = temparraydst.m_stride.S*
437  // dst.m_map.m_array_offset.m_stride.S7 = temparraydst.m_stride.S{rank}
438  AssignFadDimStride<rank,0>::eval( dst.m_map.m_array_offset, temparraydst );
439 
440  dst.m_track = src.m_track ;
441 
442  dst.m_map.m_handle =
443  dst_handle_type(
444  src.m_map.m_handle +
445  src.m_map.m_array_offset( array_extents.domain_offset(0)
446  , array_extents.domain_offset(1)
447  , array_extents.domain_offset(2)
448  , array_extents.domain_offset(3)
449  , array_extents.domain_offset(4)
450  , array_extents.domain_offset(5)
451  , array_extents.domain_offset(6)
452  , array_extents.domain_offset(7)
453  ) );
454 
455  dst.m_map.m_fad_size = src.m_map.m_fad_size;
456  dst.m_map.m_original_fad_size = src.m_map.m_original_fad_size;
457  dst.m_map.m_fad_stride = src.m_map.m_fad_stride;
458  dst.m_map.m_fad_index = src.m_map.m_fad_index;
459 
460  dst.m_rank = ( src_rank > 0 ? unsigned(R0) : 0 )
461  + ( src_rank > 1 ? unsigned(R1) : 0 )
462  + ( src_rank > 2 ? unsigned(R2) : 0 )
463  + ( src_rank > 3 ? unsigned(R3) : 0 )
464  + ( src_rank > 4 ? unsigned(R4) : 0 )
465  + ( src_rank > 5 ? unsigned(R5) : 0 )
466  + ( src_rank > 6 ? unsigned(R6) : 0 ) ;
467 
468  return dst ;
469  }
470 };
471 
472 template< class SrcTraits , class ... Args >
473 struct ViewMapping
474  < typename std::enable_if<(
475  std::is_same< typename SrcTraits::specialize ,
476  Kokkos::Impl::ViewSpecializeSacadoFadContiguous >::value
477  &&
478  std::is_same< typename SrcTraits::array_layout
479  , Kokkos::LayoutLeft >::value
480  ), Kokkos::Impl::DynRankSubviewTag >::type
481  , SrcTraits
482  , Args ... >
483 {
484 private:
485 
486  enum
487  { RZ = false
488  , R0 = bool(is_integral_extent<0,Args...>::value)
489  , R1 = bool(is_integral_extent<1,Args...>::value)
490  , R2 = bool(is_integral_extent<2,Args...>::value)
491  , R3 = bool(is_integral_extent<3,Args...>::value)
492  , R4 = bool(is_integral_extent<4,Args...>::value)
493  , R5 = bool(is_integral_extent<5,Args...>::value)
494  , R6 = bool(is_integral_extent<6,Args...>::value)
495  };
496 
497  enum { rank = unsigned(R0) + unsigned(R1) + unsigned(R2) + unsigned(R3)
498  + unsigned(R4) + unsigned(R5) + unsigned(R6) };
499 
501 
502  typedef typename SrcTraits::value_type value_type ;
503 
504  typedef value_type******* data_type ;
505 
506 public:
507 
508  typedef Kokkos::ViewTraits
509  < data_type
510  , array_layout
511  , typename SrcTraits::device_type
512  , typename SrcTraits::memory_traits > traits_type ;
513 
514  typedef Kokkos::View
515  < data_type
516  , array_layout
517  , typename SrcTraits::device_type
518  , typename SrcTraits::memory_traits > type ;
519 
520 
521  template< class MemoryTraits >
522  struct apply {
523 
524  static_assert( Kokkos::Impl::is_memory_traits< MemoryTraits >::value , "" );
525 
526  typedef Kokkos::ViewTraits
527  < data_type
528  , array_layout
529  , typename SrcTraits::device_type
530  , MemoryTraits > traits_type ;
531 
532  typedef Kokkos::View
533  < data_type
534  , array_layout
535  , typename SrcTraits::device_type
536  , MemoryTraits > type ;
537  };
538 
539  template < class Arg0 = int, class Arg1 = int, class Arg2 = int, class Arg3 = int, class Arg4 = int, class Arg5 = int, class Arg6 = int >
540  struct ExtentGenerator {
541  template <typename dimension>
543  static SubviewExtents< 7 , rank > generator ( const dimension & dim , Arg0 arg0 = Arg0(), Arg1 arg1 = Arg1(), Arg2 arg2 = Arg2(), Arg3 arg3 = Arg3(), Arg4 arg4 = Arg4(), Arg5 arg5 = Arg5(), Arg6 arg6 = Arg6() )
544  {
545  return SubviewExtents< 7 , rank >( dim , arg0 , arg1 , arg2 , arg3 ,
546  arg4 , arg5 , arg6 );
547  }
548  };
549 
550  template < class Arg0 = int, class Arg1 = int, class Arg2 = int, class Arg3 = int, class Arg4 = int, class Arg5 = int, class Arg6 = int >
551  struct ArrayExtentGenerator {
552  template <typename dimension>
554  static SubviewExtents< 8 , rank+1 > generator ( const dimension & dim , Arg0 arg0 = Arg0(), Arg1 arg1 = Arg1(), Arg2 arg2 = Arg2(), Arg3 arg3 = Arg3(), Arg4 arg4 = Arg4(), Arg5 arg5 = Arg5(), Arg6 arg6 = Arg6() )
555  {
556  return SubviewExtents< 8 , rank+1 >( dim , Kokkos::ALL() , arg0 , arg1 ,
557  arg2 , arg3 , arg4 , arg5 , arg6 );
558  }
559  };
560 
561  typedef DynRankView< value_type , array_layout , typename SrcTraits::device_type , typename SrcTraits::memory_traits > ret_type;
562 
563  template < typename T , class ... P >
565  static ret_type subview( const unsigned src_rank , Kokkos::DynRankView< T , P...> const & src , Args ... args )
566  {
567 
568  typedef ViewMapping< traits_type, void > DstType ;
569  typedef typename std::conditional< (rank==0) , ViewDimension<>
570  , typename std::conditional< (rank==1) , ViewDimension<0>
571  , typename std::conditional< (rank==2) , ViewDimension<0,0>
572  , typename std::conditional< (rank==3) , ViewDimension<0,0,0>
573  , typename std::conditional< (rank==4) , ViewDimension<0,0,0,0>
574  , typename std::conditional< (rank==5) , ViewDimension<0,0,0,0,0>
575  , typename std::conditional< (rank==6) , ViewDimension<0,0,0,0,0,0>
576  , ViewDimension<0,0,0,0,0,0,0>
577  >::type >::type >::type >::type >::type >::type >::type DstDimType ;
578  typedef typename std::conditional< (rank==0) , ViewDimension<0>
579  , typename std::conditional< (rank==1) , ViewDimension<0,0>
580  , typename std::conditional< (rank==2) , ViewDimension<0,0,0>
581  , typename std::conditional< (rank==3) , ViewDimension<0,0,0,0>
582  , typename std::conditional< (rank==4) , ViewDimension<0,0,0,0,0>
583  , typename std::conditional< (rank==5) , ViewDimension<0,0,0,0,0,0>
584  , typename std::conditional< (rank==6) , ViewDimension<0,0,0,0,0,0,0>
585  , ViewDimension<0,0,0,0,0,0,0,0>
586  >::type >::type >::type >::type >::type >::type >::type DstArrayDimType ;
587 
588  typedef ViewOffset< DstDimType , Kokkos::LayoutStride > dst_offset_type ;
589  typedef ViewOffset< DstArrayDimType , Kokkos::LayoutStride > dst_array_offset_type ;
590  typedef typename DstType::handle_type dst_handle_type ;
591 
592  ret_type dst ;
593 
594  const SubviewExtents< 7 , rank > extents =
595  ExtentGenerator< Args ... >::generator(
596  src.m_map.m_offset.m_dim , args... ) ;
597  const SubviewExtents< 8 , rank+1 > array_extents =
598  ArrayExtentGenerator< Args ... >::generator(
599  src.m_map.m_array_offset.m_dim , args... ) ;
600 
601  dst_offset_type tempdst( src.m_map.m_offset , extents ) ;
602  dst_array_offset_type temparraydst(
603  src.m_map.m_array_offset , array_extents ) ;
604 
605  dst.m_track = src.m_track ;
606 
607  dst.m_map.m_offset.m_dim.N0 = tempdst.m_dim.N0 ;
608  dst.m_map.m_offset.m_dim.N1 = tempdst.m_dim.N1 ;
609  dst.m_map.m_offset.m_dim.N2 = tempdst.m_dim.N2 ;
610  dst.m_map.m_offset.m_dim.N3 = tempdst.m_dim.N3 ;
611  dst.m_map.m_offset.m_dim.N4 = tempdst.m_dim.N4 ;
612  dst.m_map.m_offset.m_dim.N5 = tempdst.m_dim.N5 ;
613  dst.m_map.m_offset.m_dim.N6 = tempdst.m_dim.N6 ;
614 
615  dst.m_map.m_offset.m_stride.S0 = tempdst.m_stride.S0;
616  dst.m_map.m_offset.m_stride.S1 = tempdst.m_stride.S1;
617  dst.m_map.m_offset.m_stride.S2 = tempdst.m_stride.S2;
618  dst.m_map.m_offset.m_stride.S3 = tempdst.m_stride.S3;
619  dst.m_map.m_offset.m_stride.S4 = tempdst.m_stride.S4;
620  dst.m_map.m_offset.m_stride.S5 = tempdst.m_stride.S5;
621  dst.m_map.m_offset.m_stride.S6 = tempdst.m_stride.S6;
622 
623  // dst is always LayoutStride, which uses the last (rank-8) index for
624  // the fad dimension, thus we need to move its stride/dimension from the
625  // first to the last
626 
627  dst.m_map.m_array_offset.m_dim.N0 = temparraydst.m_dim.N1 ;
628  dst.m_map.m_array_offset.m_dim.N1 = temparraydst.m_dim.N2 ;
629  dst.m_map.m_array_offset.m_dim.N2 = temparraydst.m_dim.N3 ;
630  dst.m_map.m_array_offset.m_dim.N3 = temparraydst.m_dim.N4 ;
631  dst.m_map.m_array_offset.m_dim.N4 = temparraydst.m_dim.N5 ;
632  dst.m_map.m_array_offset.m_dim.N5 = temparraydst.m_dim.N6 ;
633  dst.m_map.m_array_offset.m_dim.N6 = temparraydst.m_dim.N7 ;
634  dst.m_map.m_array_offset.m_dim.N7 = temparraydst.m_dim.N0 ;
635 
636  dst.m_map.m_array_offset.m_stride.S0 = temparraydst.m_stride.S1;
637  dst.m_map.m_array_offset.m_stride.S1 = temparraydst.m_stride.S2;
638  dst.m_map.m_array_offset.m_stride.S2 = temparraydst.m_stride.S3;
639  dst.m_map.m_array_offset.m_stride.S3 = temparraydst.m_stride.S4;
640  dst.m_map.m_array_offset.m_stride.S4 = temparraydst.m_stride.S5;
641  dst.m_map.m_array_offset.m_stride.S5 = temparraydst.m_stride.S6;
642  dst.m_map.m_array_offset.m_stride.S6 = temparraydst.m_stride.S7;
643  dst.m_map.m_array_offset.m_stride.S7 = temparraydst.m_stride.S0;
644 
645  dst.m_track = src.m_track ;
646 
647  dst.m_map.m_handle =
648  dst_handle_type(
649  src.m_map.m_handle +
650  src.m_map.m_array_offset( array_extents.domain_offset(0)
651  , array_extents.domain_offset(1)
652  , array_extents.domain_offset(2)
653  , array_extents.domain_offset(3)
654  , array_extents.domain_offset(4)
655  , array_extents.domain_offset(5)
656  , array_extents.domain_offset(6)
657  , array_extents.domain_offset(7)
658  ) );
659 
660  dst.m_map.m_fad_size = src.m_map.m_fad_size;
661  dst.m_map.m_original_fad_size = src.m_map.m_original_fad_size;
662  dst.m_map.m_fad_stride = src.m_map.m_fad_stride;
663  dst.m_map.m_fad_index = src.m_map.m_fad_index;
664 
665  dst.m_rank = ( src_rank > 0 ? unsigned(R0) : 0 )
666  + ( src_rank > 1 ? unsigned(R1) : 0 )
667  + ( src_rank > 2 ? unsigned(R2) : 0 )
668  + ( src_rank > 3 ? unsigned(R3) : 0 )
669  + ( src_rank > 4 ? unsigned(R4) : 0 )
670  + ( src_rank > 5 ? unsigned(R5) : 0 )
671  + ( src_rank > 6 ? unsigned(R6) : 0 ) ;
672 
673  return dst ;
674  }
675 };
676 
681 template< class DstTraits , class SrcTraits >
682 class ViewMapping< DstTraits , SrcTraits ,
683  typename std::enable_if<(
684  Kokkos::Impl::MemorySpaceAccess
685  < typename DstTraits::memory_space
686  , typename SrcTraits::memory_space >::assignable
687  &&
688  // Destination view has FAD only
689  std::is_same< typename DstTraits::specialize
690  , Kokkos::Impl::ViewSpecializeSacadoFadContiguous >::value
691  &&
692  // Source view has FAD only
693  std::is_same< typename SrcTraits::specialize
694  , Kokkos::Impl::ViewSpecializeSacadoFadContiguous >::value
695  ), Kokkos::Impl::ViewToDynRankViewTag >::type >
696 {
697 public:
698 
699  enum { is_assignable = true };
700 
701  typedef Kokkos::Impl::SharedAllocationTracker TrackType ;
702  typedef ViewMapping< DstTraits , void > DstType ;
703  typedef ViewMapping< SrcTraits , void > SrcFadType ;
704 
705  template < typename DT , typename ... DP , typename ST , typename ... SP >
707  void assign( Kokkos::DynRankView< DT , DP... > & dst
708  , const Kokkos::View< ST , SP... >& src )
709  {
710  static_assert(
711  (
712  std::is_same< typename DstTraits::array_layout
713  , Kokkos::LayoutLeft >::value ||
714  std::is_same< typename DstTraits::array_layout
715  , Kokkos::LayoutRight >::value ||
716  std::is_same< typename DstTraits::array_layout
717  , Kokkos::LayoutStride >::value
718  )
719  &&
720  (
721  std::is_same< typename SrcTraits::array_layout
722  , Kokkos::LayoutLeft >::value ||
723  std::is_same< typename SrcTraits::array_layout
724  , Kokkos::LayoutRight >::value ||
725  std::is_same< typename SrcTraits::array_layout
726  , Kokkos::LayoutStride >::value
727  )
728  , "View of FAD requires LayoutLeft, LayoutRight, or LayoutStride" );
729 
730  static_assert(
731  std::is_same< typename DstTraits::value_type
732  , typename SrcTraits::value_type >::value ||
733  std::is_same< typename DstTraits::value_type
734  , typename SrcTraits::const_value_type >::value ,
735  "View assignment must have same value type or const = non-const" );
736 
737  const bool is_left =
738  std::is_same<typename DstTraits::array_layout,Kokkos::LayoutLeft>::value;
739  typedef typename DstType::offset_type dst_offset_type;
740  typedef typename DstType::array_offset_type dst_array_offset_type;
741  if (is_left) {
742  dst.m_map.m_array_offset =
743  dst_array_offset_type(std::integral_constant<unsigned,0>(),
744  src.m_map.m_array_offset.layout() );
745  }
746  else {
747  dst.m_map.m_array_offset =
748  dst_array_offset_type(std::integral_constant<unsigned,0>(),
749  permute_fad_layout(src.m_map.m_array_offset.layout(),
750  SrcTraits::rank) );
751  }
752  dst.m_map.m_offset =
753  dst_offset_type(std::integral_constant<unsigned,0>(),
754  src.m_map.m_offset.layout() );
755 
756  dst.m_map.m_handle = src.m_map.m_handle ;
757  dst.m_rank = src.Rank ;
758 
759  dst.m_map.m_fad_size = src.m_map.m_fad_size ;
760  dst.m_map.m_original_fad_size = src.m_map.m_original_fad_size;
761  dst.m_map.m_fad_stride = src.m_map.m_fad_stride ;
762  dst.m_map.m_fad_index = src.m_map.m_fad_index;
763  }
764 };
765 
770 template< class DstTraits , class SrcTraits >
771 class ViewMapping< DstTraits , SrcTraits ,
772  typename std::enable_if<(
773  Kokkos::Impl::MemorySpaceAccess
774  < typename DstTraits::memory_space
775  , typename SrcTraits::memory_space >::assignable
776  &&
777  // Destination view has ordinary
778  std::is_same< typename DstTraits::specialize , void >::value
779  &&
780  // Source view has FAD only
781  std::is_same< typename SrcTraits::specialize
782  , Kokkos::Impl::ViewSpecializeSacadoFadContiguous >::value
783  ), Kokkos::Impl::ViewToDynRankViewTag >::type >
784 {
785 public:
786 
787  enum { is_assignable = true };
788 
789  typedef Kokkos::Impl::SharedAllocationTracker TrackType ;
790  typedef ViewMapping< DstTraits , void > DstType ;
791  typedef ViewMapping< SrcTraits , void > SrcFadType ;
792 
793  template < typename DT , typename ... DP , typename ST , typename ... SP >
795  void assign( Kokkos::DynRankView< DT , DP... > & dst
796  , const Kokkos::View< ST , SP... >& src )
797  {
798  static_assert(
799  (
800  std::is_same< typename DstTraits::array_layout
801  , Kokkos::LayoutLeft >::value ||
802  std::is_same< typename DstTraits::array_layout
803  , Kokkos::LayoutRight >::value ||
804  std::is_same< typename DstTraits::array_layout
805  , Kokkos::LayoutStride >::value
806  )
807  &&
808  (
809  std::is_same< typename SrcTraits::array_layout
810  , Kokkos::LayoutLeft >::value ||
811  std::is_same< typename SrcTraits::array_layout
812  , Kokkos::LayoutRight >::value ||
813  std::is_same< typename SrcTraits::array_layout
814  , Kokkos::LayoutStride >::value
815  )
816  , "View of FAD requires LayoutLeft, LayoutRight, or LayoutStride" );
817 
818  static_assert(
819  std::is_same< typename DstTraits::value_type
820  , typename SrcTraits::value_type >::value ||
821  std::is_same< typename DstTraits::value_type
822  , typename SrcTraits::const_value_type >::value ,
823  "View assignment must have same value type or const = non-const" );
824 
825  dst.m_map.m_offset.m_dim = src.m_map.m_array_offset.m_dim;
826  dst.m_map.m_offset.m_stride = src.m_map.m_array_offset.m_stride ;
827 
828  dst.m_map.m_handle = src.m_map.m_handle ;
829  dst.m_rank = src.Rank ;
830  }
831 };
832 
833 }} //end Kokkos::Impl
834 
835 #endif //defined(HAVE_SACADO_VIEW_SPEC) && !defined(SACADO_DISABLE_FAD_VIEW_SPEC)
836 
837 #endif // defined(HAVE_SACADO_KOKKOSCONTAINERS)
838 
839 #endif /* #ifndef KOKKOS_DYN_RANK_VIEW_SACADO_FAD_HPP */
#define KOKKOS_INLINE_FUNCTION
#define T
Definition: Sacado_rad.hpp:573
expr expr expr bar false