Sacado Package Browser (Single Doxygen Collection)  Version of the Day
Kokkos_DynRankView_Fad.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_HPP
31 #define KOKKOS_DYN_RANK_VIEW_SACADO_FAD_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 Experimental {
48 namespace Impl {
49 
50 template <>
51 struct DynRankDimTraits<ViewSpecializeSacadoFad> {
52 
53  enum : size_t{unspecified = ~size_t(0)};
54 
55  // Compute the rank of the view from the nonzero dimension arguments.
56  // For views of Fad, the rank is one less than the rank determined by the nonzero dimension args
58  static size_t computeRank( const size_t N0
59  , const size_t N1
60  , const size_t N2
61  , const size_t N3
62  , const size_t N4
63  , const size_t N5
64  , const size_t N6
65  , const size_t N7 )
66  {
67  return
68  ( (N7 == unspecified && N6 == unspecified && N5 == unspecified && N4 == unspecified && N3 == unspecified && N2 == unspecified && N1 == unspecified && N0 == unspecified) ? 0
69  : ( (N7 == unspecified && N6 == unspecified && N5 == unspecified && N4 == unspecified && N3 == unspecified && N2 == unspecified && N1 == unspecified) ? 0
70  : ( (N7 == unspecified && N6 == unspecified && N5 == unspecified && N4 == unspecified && N3 == unspecified && N2 == unspecified) ? 1
71  : ( (N7 == unspecified && N6 == unspecified && N5 == unspecified && N4 == unspecified && N3 == unspecified) ? 2
72  : ( (N7 == unspecified && N6 == unspecified && N5 == unspecified && N4 == unspecified) ? 3
73  : ( (N7 == unspecified && N6 == unspecified && N5 == unspecified) ? 4
74  : ( (N7 == unspecified && N6 == unspecified) ? 5
75  : ( (N7 == unspecified) ? 6
76  : 7 ) ) ) ) ) ) ) );
77  }
78 
79  // Compute the rank of the view from the nonzero layout arguments.
80  template <typename Layout>
82  static size_t computeRank( const Layout& layout )
83  {
84  return computeRank( layout.dimension[0]
85  , layout.dimension[1]
86  , layout.dimension[2]
87  , layout.dimension[3]
88  , layout.dimension[4]
89  , layout.dimension[5]
90  , layout.dimension[6]
91  , layout.dimension[7] );
92  }
93 
94  // Create the layout for the rank-7 view.
95  // For Fad we have to move the fad dimension to the last (rank 8 since the DynRankView is rank-7)
96  // LayoutLeft or LayoutRight
97  template <typename Layout>
99  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 )
100  {
101  Layout l( layout.dimension[0] != unspecified ? layout.dimension[0] : 1
102  , layout.dimension[1] != unspecified ? layout.dimension[1] : 1
103  , layout.dimension[2] != unspecified ? layout.dimension[2] : 1
104  , layout.dimension[3] != unspecified ? layout.dimension[3] : 1
105  , layout.dimension[4] != unspecified ? layout.dimension[4] : 1
106  , layout.dimension[5] != unspecified ? layout.dimension[5] : 1
107  , layout.dimension[6] != unspecified ? layout.dimension[6] : 1
108  , layout.dimension[7] != unspecified ? layout.dimension[7] : 1 );
109  const unsigned fad_dim = computeRank(layout);
110  const size_t fad_size = layout.dimension[fad_dim];
111  l.dimension[fad_dim] = 1;
112  l.dimension[7] = fad_size;
113 
114  return l;
115  }
116 
117  //LayoutStride
118  template <typename Layout>
120  static typename std::enable_if< (std::is_same<Layout , Kokkos::LayoutStride>::value) , Layout>::type createLayout( const Layout& layout )
121  {
122  Layout l( layout.dimension[0] != unspecified ? layout.dimension[0] : 1
123  , layout.stride[0]
124  , layout.dimension[1] != unspecified ? layout.dimension[1] : 1
125  , layout.stride[1]
126  , layout.dimension[2] != unspecified ? layout.dimension[2] : 1
127  , layout.stride[2]
128  , layout.dimension[3] != unspecified ? layout.dimension[3] : 1
129  , layout.stride[3]
130  , layout.dimension[4] != unspecified ? layout.dimension[4] : 1
131  , layout.stride[4]
132  , layout.dimension[5] != unspecified ? layout.dimension[5] : 1
133  , layout.stride[5]
134  , layout.dimension[6] != unspecified ? layout.dimension[6] : 1
135  , layout.stride[6]
136  , layout.dimension[7] != unspecified ? layout.dimension[7] : 1
137  , layout.stride[7]
138  );
139  const unsigned fad_dim = computeRank(layout);
140  const size_t fad_size = layout.dimension[fad_dim];
141  l.dimension[fad_dim] = 1;
142  l.dimension[7] = fad_size;
143 
144  return l;
145  }
146 
147  // Create a view from the given dimension arguments.
148  // This is only necessary because the shmem constructor doesn't take a layout.
149  template <typename ViewType, typename ViewArg>
150  static ViewType createView( const ViewArg& arg
151  , const size_t N0
152  , const size_t N1
153  , const size_t N2
154  , const size_t N3
155  , const size_t N4
156  , const size_t N5
157  , const size_t N6
158  , const size_t N7 )
159  {
160  typename ViewType::array_layout l( N0, N1, N2, N3, N4, N5, N6, N7 );
161  typename ViewType::array_layout l_fad = createLayout(l);
162  return ViewType( arg
163  , l_fad.dimension[0]
164  , l_fad.dimension[1]
165  , l_fad.dimension[2]
166  , l_fad.dimension[3]
167  , l_fad.dimension[4]
168  , l_fad.dimension[5]
169  , l_fad.dimension[6]
170  , l_fad.dimension[7] );
171  }
172 
173 };
174 
175 // Utility class that handles calculation of the stride in Fad subview
176 template <unsigned> struct AssignFadStride {};
177 template <> struct AssignFadStride<0u> {
178  template <typename Src, typename Dst>
180  static void eval(Dst& dst, const Src& src) {
181  dst.m_stride.S0 = 0 ;
182  dst.m_stride.S1 = 0 ;
183  dst.m_stride.S2 = 0 ;
184  dst.m_stride.S3 = 0 ;
185  dst.m_stride.S4 = 0 ;
186  dst.m_stride.S5 = 0 ;
187  dst.m_stride.S6 = 0 ;
188  dst.m_stride.S7 = src.m_stride.S0 ;
189  }
190 };
191 template <> struct AssignFadStride<1u> {
192  template <typename Src, typename Dst>
194  static void eval(Dst& dst, const Src& src) {
195  dst.m_stride.S0 = src.m_stride.S0 ;
196  dst.m_stride.S1 = 0 ;
197  dst.m_stride.S2 = 0 ;
198  dst.m_stride.S3 = 0 ;
199  dst.m_stride.S4 = 0 ;
200  dst.m_stride.S5 = 0 ;
201  dst.m_stride.S6 = 0 ;
202  dst.m_stride.S7 = src.m_stride.S1 ;
203  }
204 };
205 template <> struct AssignFadStride<2u> {
206  template <typename Src, typename Dst>
208  static void eval(Dst& dst, const Src& src) {
209  dst.m_stride.S0 = src.m_stride.S0 ;
210  dst.m_stride.S1 = src.m_stride.S1 ;
211  dst.m_stride.S2 = 0 ;
212  dst.m_stride.S3 = 0 ;
213  dst.m_stride.S4 = 0 ;
214  dst.m_stride.S5 = 0 ;
215  dst.m_stride.S6 = 0 ;
216  dst.m_stride.S7 = src.m_stride.S2 ;
217  }
218 };
219 template <> struct AssignFadStride<3u> {
220  template <typename Src, typename Dst>
222  static void eval(Dst& dst, const Src& src) {
223  dst.m_stride.S0 = src.m_stride.S0 ;
224  dst.m_stride.S1 = src.m_stride.S1 ;
225  dst.m_stride.S2 = src.m_stride.S2 ;
226  dst.m_stride.S3 = 0 ;
227  dst.m_stride.S4 = 0 ;
228  dst.m_stride.S5 = 0 ;
229  dst.m_stride.S6 = 0 ;
230  dst.m_stride.S7 = src.m_stride.S3 ;
231  }
232 };
233 template <> struct AssignFadStride<4u> {
234  template <typename Src, typename Dst>
236  static void eval(Dst& dst, const Src& src) {
237  dst.m_stride.S0 = src.m_stride.S0 ;
238  dst.m_stride.S1 = src.m_stride.S1 ;
239  dst.m_stride.S2 = src.m_stride.S2 ;
240  dst.m_stride.S3 = src.m_stride.S3 ;
241  dst.m_stride.S4 = 0 ;
242  dst.m_stride.S5 = 0 ;
243  dst.m_stride.S6 = 0 ;
244  dst.m_stride.S7 = src.m_stride.S4 ;
245  }
246 };
247 template <> struct AssignFadStride<5u> {
248  template <typename Src, typename Dst>
250  static void eval(Dst& dst, const Src& src) {
251  dst.m_stride.S0 = src.m_stride.S0 ;
252  dst.m_stride.S1 = src.m_stride.S1 ;
253  dst.m_stride.S2 = src.m_stride.S2 ;
254  dst.m_stride.S3 = src.m_stride.S3 ;
255  dst.m_stride.S4 = src.m_stride.S4 ;
256  dst.m_stride.S5 = 0 ;
257  dst.m_stride.S6 = 0 ;
258  dst.m_stride.S7 = src.m_stride.S5 ;
259  }
260 };
261 template <> struct AssignFadStride<6u> {
262  template <typename Src, typename Dst>
264  static void eval(Dst& dst, const Src& src) {
265  dst.m_stride.S0 = src.m_stride.S0 ;
266  dst.m_stride.S1 = src.m_stride.S1 ;
267  dst.m_stride.S2 = src.m_stride.S2 ;
268  dst.m_stride.S3 = src.m_stride.S3 ;
269  dst.m_stride.S4 = src.m_stride.S4 ;
270  dst.m_stride.S5 = src.m_stride.S5 ;
271  dst.m_stride.S6 = 0 ;
272  dst.m_stride.S7 = src.m_stride.S6 ;
273  }
274 };
275 template <> struct AssignFadStride<7u> {
276  template <typename Src, typename Dst>
278  static void eval(Dst& dst, const Src& src) {
279  dst.m_stride.S0 = src.m_stride.S0 ;
280  dst.m_stride.S1 = src.m_stride.S1 ;
281  dst.m_stride.S2 = src.m_stride.S2 ;
282  dst.m_stride.S3 = src.m_stride.S3 ;
283  dst.m_stride.S4 = src.m_stride.S4 ;
284  dst.m_stride.S5 = src.m_stride.S5 ;
285  dst.m_stride.S6 = src.m_stride.S6 ;
286  dst.m_stride.S7 = src.m_stride.S7 ;
287  }
288 };
289 
290 template <unsigned> struct AssignDim7 {
291  template <typename Src, typename Dst>
293  static void eval(Dst& dst, const Src& src) {}
294 };
295 template <> struct AssignDim7<0u> {
296  template <typename Src, typename Dst>
298  static void eval(Dst& dst, const Src& src) {
299  dst.m_dim.N7 = src.m_dim.N7;
300  }
301 };
302 
303 // Specializations for subdynrankview
304 template< class SrcTraits , class ... Args >
305 struct ViewMapping
306  < typename std::enable_if<(
307  std::is_same< typename SrcTraits::specialize ,
308  ViewSpecializeSacadoFad >::value
309  &&
310  (
311  std::is_same< typename SrcTraits::array_layout
312  , Kokkos::LayoutLeft >::value ||
313  std::is_same< typename SrcTraits::array_layout
314  , Kokkos::LayoutRight >::value ||
315  std::is_same< typename SrcTraits::array_layout
316  , Kokkos::LayoutStride >::value
317  )
318  ), DynRankSubviewTag >::type
319  , SrcTraits
320  , Args ... >
321 {
322 private:
323 
324  enum
325  { RZ = false
326  , R0 = bool(is_integral_extent<0,Args...>::value)
327  , R1 = bool(is_integral_extent<1,Args...>::value)
328  , R2 = bool(is_integral_extent<2,Args...>::value)
329  , R3 = bool(is_integral_extent<3,Args...>::value)
330  , R4 = bool(is_integral_extent<4,Args...>::value)
331  , R5 = bool(is_integral_extent<5,Args...>::value)
332  , R6 = bool(is_integral_extent<6,Args...>::value)
333  };
334 
335  enum { rank = unsigned(R0) + unsigned(R1) + unsigned(R2) + unsigned(R3)
336  + unsigned(R4) + unsigned(R5) + unsigned(R6) };
337 
338  typedef Kokkos::LayoutStride array_layout ;
339 
340  typedef typename SrcTraits::value_type value_type ;
341 
342  typedef value_type******* data_type ;
343 
344 public:
345 
346  typedef Kokkos::Experimental::ViewTraits
347  < data_type
348  , array_layout
349  , typename SrcTraits::device_type
350  , typename SrcTraits::memory_traits > traits_type ;
351 
352  typedef Kokkos::Experimental::View
353  < data_type
354  , array_layout
355  , typename SrcTraits::device_type
356  , typename SrcTraits::memory_traits > type ;
357 
358 
359  template< class MemoryTraits >
360  struct apply {
361 
362  static_assert( Kokkos::Impl::is_memory_traits< MemoryTraits >::value , "" );
363 
364  typedef Kokkos::Experimental::ViewTraits
365  < data_type
366  , array_layout
367  , typename SrcTraits::device_type
368  , MemoryTraits > traits_type ;
369 
370  typedef Kokkos::Experimental::View
371  < data_type
372  , array_layout
373  , typename SrcTraits::device_type
374  , MemoryTraits > type ;
375  };
376 
377 
378  template < class Arg0 = int, class Arg1 = int, class Arg2 = int, class Arg3 = int, class Arg4 = int, class Arg5 = int, class Arg6 = int >
379  struct ExtentGenerator {
380  template <typename dimension>
382  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() )
383  {
384  return SubviewExtents< 8 , rank+1 >( dim , arg0 , arg1 , arg2 , arg3 , arg4 , arg5 , arg6 , Kokkos::ALL() );
385  }
386  };
387 
388  typedef DynRankView< value_type , array_layout , typename SrcTraits::device_type , typename SrcTraits::memory_traits > ret_type;
389 
390  template < typename T , class ... P >
392  static ret_type subview( const unsigned src_rank , Kokkos::Experimental::DynRankView< T , P...> const & src , Args ... args )
393  {
394 
395  typedef ViewMapping< traits_type, void > DstType ;
396  typedef ViewMapping< SrcTraits, void> SrcType;
397  enum { FadStaticDim = SrcType::FadStaticDimension };
398  typedef typename std::conditional< (rank==0) , ViewDimension<FadStaticDim>
399  , typename std::conditional< (rank==1) , ViewDimension<0,FadStaticDim>
400  , typename std::conditional< (rank==2) , ViewDimension<0,0,FadStaticDim>
401  , typename std::conditional< (rank==3) , ViewDimension<0,0,0,FadStaticDim>
402  , typename std::conditional< (rank==4) , ViewDimension<0,0,0,0,FadStaticDim>
403  , typename std::conditional< (rank==5) , ViewDimension<0,0,0,0,0,FadStaticDim>
404  , typename std::conditional< (rank==6) , ViewDimension<0,0,0,0,0,0,FadStaticDim>
405  , ViewDimension<0,0,0,0,0,0,0,FadStaticDim>
406  >::type >::type >::type >::type >::type >::type >::type DstDimType ;
407 
408  typedef ViewOffset< DstDimType , Kokkos::LayoutStride > dst_offset_type ;
409  typedef typename DstType::handle_type dst_handle_type ;
410 
411  ret_type dst ;
412 
413  const SubviewExtents< 8 , rank+1 > extents =
414  ExtentGenerator< Args ... >::generator( src.m_map.m_offset.m_dim , args... ) ;
415 
416  dst_offset_type tempdst( src.m_map.m_offset , extents ) ;
417 
418  dst.m_track = src.m_track ;
419 
420  dst.m_map.m_offset.m_dim.N0 = tempdst.m_dim.N0 ;
421  dst.m_map.m_offset.m_dim.N1 = tempdst.m_dim.N1 ;
422  dst.m_map.m_offset.m_dim.N2 = tempdst.m_dim.N2 ;
423  dst.m_map.m_offset.m_dim.N3 = tempdst.m_dim.N3 ;
424  dst.m_map.m_offset.m_dim.N4 = tempdst.m_dim.N4 ;
425  dst.m_map.m_offset.m_dim.N5 = tempdst.m_dim.N5 ;
426  dst.m_map.m_offset.m_dim.N6 = tempdst.m_dim.N6 ;
427 
428  // Do this except for when Fad dim is static
429  // dst.m_map.m_offset.m_dim.N7 = tempdst.m_dim.N7 ;
430  AssignDim7<FadStaticDim>::eval( dst.m_map.m_offset, tempdst );
431 
432  // Move last non-unit stride to S7
433  // dst.m_map.m_offset.m_stride.S* = tempdst.m_stride.S*;
434  // dst.m_map.m_offset.m_stride.S7 = tempdst.m_stride.S{rank}
435  AssignFadStride<rank>::eval( dst.m_map.m_offset, tempdst );
436 
437  dst.m_track = src.m_track ;
438 
439  dst.m_map.m_handle =
440  dst_handle_type( src.m_map.m_handle +
441  src.m_map.m_offset( extents.domain_offset(0)
442  , extents.domain_offset(1)
443  , extents.domain_offset(2)
444  , extents.domain_offset(3)
445  , extents.domain_offset(4)
446  , extents.domain_offset(5)
447  , extents.domain_offset(6)
448  , extents.domain_offset(7)
449  ) );
450 
451  dst.m_map.m_fad_size = src.m_map.m_fad_size;
452  dst.m_map.m_fad_stride = src.m_map.m_fad_stride.value;
453 
454  dst.m_rank = ( src_rank > 0 ? unsigned(R0) : 0 )
455  + ( src_rank > 1 ? unsigned(R1) : 0 )
456  + ( src_rank > 2 ? unsigned(R2) : 0 )
457  + ( src_rank > 3 ? unsigned(R3) : 0 )
458  + ( src_rank > 4 ? unsigned(R4) : 0 )
459  + ( src_rank > 5 ? unsigned(R5) : 0 )
460  + ( src_rank > 6 ? unsigned(R6) : 0 ) ;
461 
462  return dst ;
463  }
464 };
465 
466 // ViewMapping for copy and copy-assign from View to DynRankView
467 template <unsigned> struct AssignFadDim7 {
468  template <typename Src, typename Dst>
470  static void eval(Dst& dst, const Src& src , const unsigned dim ) {}
471 };
472 
473 template <> struct AssignFadDim7<0u> {
474  template <typename Src, typename Dst>
476  static void eval(Dst& dst, const Src& src , const unsigned dim ) {
477  dst.m_dim.N7 = src.m_dim.extent(dim);
478  }
479 };
480 
481 
482 template <unsigned , unsigned> struct AssignDim {};
483 
484 template <unsigned FadStaticDim> struct AssignDim<0u,FadStaticDim> {
485  template <typename Src, typename Dst>
487  static void eval(Dst& dst, const Src& src) {
488  dst.m_dim.N0 = 1;
489  dst.m_dim.N1 = 1;
490  dst.m_dim.N2 = 1;
491  dst.m_dim.N3 = 1;
492  dst.m_dim.N4 = 1;
493  dst.m_dim.N5 = 1;
494  dst.m_dim.N6 = 1;
495  AssignFadDim7<FadStaticDim>::eval( dst , src , 0 );
496  }
497 };
498 
499 template <unsigned FadStaticDim> struct AssignDim<1u,FadStaticDim> {
500  template <typename Src, typename Dst>
502  static void eval(Dst& dst, const Src& src) {
503  dst.m_dim.N0 = src.m_dim.N0;
504  dst.m_dim.N1 = 1;
505  dst.m_dim.N2 = 1;
506  dst.m_dim.N3 = 1;
507  dst.m_dim.N4 = 1;
508  dst.m_dim.N5 = 1;
509  dst.m_dim.N6 = 1;
510  AssignFadDim7<FadStaticDim>::eval( dst , src , 1 );
511  }
512 };
513 
514 template <unsigned FadStaticDim> struct AssignDim<2u,FadStaticDim> {
515  template <typename Src, typename Dst>
517  static void eval(Dst& dst, const Src& src) {
518  dst.m_dim.N0 = src.m_dim.N0;
519  dst.m_dim.N1 = src.m_dim.N1;
520  dst.m_dim.N2 = 1;
521  dst.m_dim.N3 = 1;
522  dst.m_dim.N4 = 1;
523  dst.m_dim.N5 = 1;
524  dst.m_dim.N6 = 1;
525  AssignFadDim7<FadStaticDim>::eval( dst , src , 2 );
526  }
527 };
528 
529 template <unsigned FadStaticDim> struct AssignDim<3u,FadStaticDim> {
530  template <typename Src, typename Dst>
532  static void eval(Dst& dst, const Src& src) {
533  dst.m_dim.N0 = src.m_dim.N0;
534  dst.m_dim.N1 = src.m_dim.N1;
535  dst.m_dim.N2 = src.m_dim.N2;
536  dst.m_dim.N3 = 1;
537  dst.m_dim.N4 = 1;
538  dst.m_dim.N5 = 1;
539  dst.m_dim.N6 = 1;
540  AssignFadDim7<FadStaticDim>::eval( dst , src , 3 );
541  }
542 };
543 
544 template <unsigned FadStaticDim> struct AssignDim<4u,FadStaticDim> {
545  template <typename Src, typename Dst>
547  static void eval(Dst& dst, const Src& src) {
548  dst.m_dim.N0 = src.m_dim.N0;
549  dst.m_dim.N1 = src.m_dim.N1;
550  dst.m_dim.N2 = src.m_dim.N2;
551  dst.m_dim.N3 = src.m_dim.N3;
552  dst.m_dim.N4 = 1;
553  dst.m_dim.N5 = 1;
554  dst.m_dim.N6 = 1;
555  AssignFadDim7<FadStaticDim>::eval( dst , src , 4 );
556  }
557 };
558 
559 template <unsigned FadStaticDim> struct AssignDim<5u,FadStaticDim> {
560  template <typename Src, typename Dst>
562  static void eval(Dst& dst, const Src& src) {
563  dst.m_dim.N0 = src.m_dim.N0;
564  dst.m_dim.N1 = src.m_dim.N1;
565  dst.m_dim.N2 = src.m_dim.N2;
566  dst.m_dim.N3 = src.m_dim.N3;
567  dst.m_dim.N4 = src.m_dim.N4;
568  dst.m_dim.N5 = 1;
569  dst.m_dim.N6 = 1;
570  AssignFadDim7<FadStaticDim>::eval( dst , src , 5 );
571  }
572 };
573 
574 template <unsigned FadStaticDim> struct AssignDim<6u,FadStaticDim> {
575  template <typename Src, typename Dst>
577  static void eval(Dst& dst, const Src& src) {
578  dst.m_dim.N0 = src.m_dim.N0;
579  dst.m_dim.N1 = src.m_dim.N1;
580  dst.m_dim.N2 = src.m_dim.N2;
581  dst.m_dim.N3 = src.m_dim.N3;
582  dst.m_dim.N4 = src.m_dim.N4;
583  dst.m_dim.N5 = src.m_dim.N5;
584  dst.m_dim.N6 = 1;
585  AssignFadDim7<FadStaticDim>::eval( dst , src , 6 );
586  }
587 };
588 
589 template <unsigned FadStaticDim> struct AssignDim<7u,FadStaticDim> {
590  template <typename Src, typename Dst>
592  static void eval(Dst& dst, const Src& src) {
593  dst.m_dim.N0 = src.m_dim.N0;
594  dst.m_dim.N1 = src.m_dim.N1;
595  dst.m_dim.N2 = src.m_dim.N2;
596  dst.m_dim.N3 = src.m_dim.N3;
597  dst.m_dim.N4 = src.m_dim.N4;
598  dst.m_dim.N5 = src.m_dim.N5;
599  dst.m_dim.N6 = src.m_dim.N6;
600  AssignFadDim7<FadStaticDim>::eval( dst , src , 7 );
601  }
602 };
603 
610 template< class DstTraits , class SrcTraits >
611 class ViewMapping< DstTraits , SrcTraits ,
612  typename std::enable_if<(
613  std::is_same< typename DstTraits::memory_space
614  , typename SrcTraits::memory_space >::value
615  &&
616  // Destination view has FAD or ordinary
617  ( std::is_same< typename DstTraits::specialize
618  , ViewSpecializeSacadoFad >::value
619  ||
620  std::is_same< typename DstTraits::specialize , void >::value
621  )
622  &&
623  // Source view has FAD only
624  std::is_same< typename SrcTraits::specialize
625  , ViewSpecializeSacadoFad >::value
626  ), ViewToDynRankViewTag >::type >
627 {
628 public:
629 
630  enum { is_assignable = true };
631 
632  typedef Kokkos::Experimental::Impl::SharedAllocationTracker TrackType ;
633  typedef ViewMapping< DstTraits , void > DstType ;
634  typedef ViewMapping< SrcTraits , void > SrcFadType ;
635 
636  template< class S , class D >
638  typename std::enable_if<(
639  std::is_same< S , ViewSpecializeSacadoFad >::value
640  )>::type
641  assign_fad_size( D & dst , const SrcFadType & src )
642  { dst.m_fad_size = src.m_fad_size.value ;
643  dst.m_fad_stride = src.m_fad_stride.value ;
644  }
645 
646  template< class S , class D >
648  typename std::enable_if<(
649  ! std::is_same< S , ViewSpecializeSacadoFad >::value
650  )>::type
651  assign_fad_size( D & , const SrcFadType & ) {}
652 
653 
654  template < typename DT , typename ... DP , typename ST , typename ... SP >
656  void assign( Kokkos::DynRankView< DT , DP... > & dst
657  , const Kokkos::View< ST , SP... >& src )
658  {
659  static_assert(
660  (
661  std::is_same< typename DstTraits::array_layout
662  , Kokkos::LayoutLeft >::value ||
663  std::is_same< typename DstTraits::array_layout
664  , Kokkos::LayoutRight >::value ||
665  std::is_same< typename DstTraits::array_layout
666  , Kokkos::LayoutStride >::value
667  )
668  &&
669  (
670  std::is_same< typename SrcTraits::array_layout
671  , Kokkos::LayoutLeft >::value ||
672  std::is_same< typename SrcTraits::array_layout
673  , Kokkos::LayoutRight >::value ||
674  std::is_same< typename SrcTraits::array_layout
675  , Kokkos::LayoutStride >::value
676  )
677  , "View of FAD requires LayoutLeft, LayoutRight, or LayoutStride" );
678 
679  static_assert(
680  std::is_same< typename DstTraits::value_type
681  , typename SrcTraits::value_type >::value ||
682  std::is_same< typename DstTraits::value_type
683  , typename SrcTraits::const_value_type >::value ,
684  "View assignment must have same value type or const = non-const" );
685 
686  typedef ViewMapping< SrcTraits, void> SrcType;
687  enum { FadStaticDim = SrcType::FadStaticDimension };
688 
689  AssignDim<SrcTraits::rank,FadStaticDim>::eval(dst.m_map.m_offset, src.m_map.m_offset);
690 
691  dst.m_map.m_handle = src.m_map.m_handle ;
692  dst.m_rank = src.Rank ;
693  dst.m_map.m_offset.m_stride = src.m_map.m_offset.m_stride ;
694 
695  ViewMapping::template assign_fad_size< typename DstTraits::specialize >( dst.m_map , src.m_map );
696  }
697 };
698 
699 } //end Impl
700 
701 } //end Experimental
702 
703 template <typename view_type>
704 struct is_dynrankview_fad { static const bool value = false; };
705 
706 template <typename T, typename ... P>
707 struct is_dynrankview_fad< DynRankView<T,P...> > {
708  typedef DynRankView<T,P...> view_type;
709  static const bool value =
710  std::is_same< typename view_type::specialize,
711  Experimental::Impl::ViewSpecializeSacadoFad >::value;
712 };
713 
714 template <typename T, typename ... P>
716 constexpr typename
717 std::enable_if< is_dynrankview_fad< DynRankView<T,P...> >::value, unsigned >::type
718 dimension_scalar(const DynRankView<T,P...>& view) {
719  return view.implementation_map().dimension_scalar();
720 }
721 
722 namespace Experimental {
723 // Overload of deep_copy for Fad views intializing to a constant scalar
724 
725 template< class DT, class ... DP >
726 void deep_copy(
727  const DynRankView<DT,DP...> & view ,
728  const typename Sacado::ScalarType< typename DynRankView<DT,DP...>::value_type >::type & value
729  , typename std::enable_if<(
730  std::is_same< typename ViewTraits<DT,DP...>::specialize
731  , Kokkos::Experimental::Impl::ViewSpecializeSacadoFad >::value
732  )>::type * = 0 )
733 {
734  static_assert(
735  std::is_same< typename ViewTraits<DT,DP...>::value_type ,
736  typename ViewTraits<DT,DP...>::non_const_value_type >::value
737  , "Can only deep copy into non-const type" );
738 
739  Kokkos::Experimental::Impl::DynRankViewFill< DynRankView<DT,DP...> >( view , value );
740 }
741 
742 // Overload of deep_copy for Fad views intializing to a constant Fad
743 template< class DT, class ... DP >
744 void deep_copy(
745  const DynRankView<DT,DP...> & view ,
746  const typename DynRankView<DT,DP...>::value_type & value
747  , typename std::enable_if<(
748  std::is_same< typename ViewTraits<DT,DP...>::specialize
749  , Kokkos::Experimental::Impl::ViewSpecializeSacadoFad >::value
750  )>::type * = 0 )
751 {
752  static_assert(
753  std::is_same< typename ViewTraits<DT,DP...>::value_type ,
754  typename ViewTraits<DT,DP...>::non_const_value_type >::value
755  , "Can only deep copy into non-const type" );
756 
757  Kokkos::Experimental::Impl::DynRankViewFill< DynRankView<DT,DP...> >( view , value );
758 }
759 
760 template< class DstType , class SrcType >
761 inline
762 void deep_copy
763  ( const DstType & dst
764  , const SrcType & src
765  , typename std::enable_if<(
766  std::is_same< typename DstType::traits::specialize
767  , Kokkos::Experimental::Impl::ViewSpecializeSacadoFad >::value
768  &&
769  std::is_same< typename SrcType::traits::specialize
770  , Kokkos::Experimental::Impl::ViewSpecializeSacadoFad >::value
771  &&
772  ( Kokkos::Experimental::is_dyn_rank_view<DstType>::value || Kokkos::Experimental::is_dyn_rank_view<SrcType
773 >::value )
774  )>::type * = 0 )
775 {
776  static_assert(
777  std::is_same< typename DstType::traits::value_type ,
778  typename DstType::traits::non_const_value_type >::value
779  , "deep_copy requires non-const destination type" );
780 
781  typedef DstType dst_type ;
782  typedef SrcType src_type ;;
783 
784  typedef typename dst_type::execution_space dst_execution_space ;
785  typedef typename src_type::execution_space src_execution_space ;
786  typedef typename dst_type::memory_space dst_memory_space ;
787  typedef typename src_type::memory_space src_memory_space ;
788 
789  enum { DstExecCanAccessSrc =
790  Kokkos::Impl::VerifyExecutionCanAccessMemorySpace< typename dst_execution_space::memory_space , src_memory_space >::value };
791 
792  enum { SrcExecCanAccessDst =
793  Kokkos::Impl::VerifyExecutionCanAccessMemorySpace< typename src_execution_space::memory_space , dst_memory_space >::value };
794 
795  if ( (void *) dst.data() != (void*) src.data() ) {
796 
797  // Concern: If overlapping views then a parallel copy will be erroneous.
798  // ...
799 
800  // If same type, equal layout, equal dimensions, equal span, and contiguous memory then can byte-wise copy
801  if ( rank(src) == 0 && rank(dst) == 0 )
802  {
803  typedef typename dst_type::value_type value_type ;
804  Kokkos::Impl::DeepCopy< dst_memory_space , src_memory_space >( dst.data() , src.data() , sizeof(value_type) );
805  }
806  else if ( std::is_same< typename DstType::traits::value_type ,
807  typename SrcType::traits::non_const_value_type >::value &&
808  (
809  ( std::is_same< typename DstType::traits::array_layout ,
810  typename SrcType::traits::array_layout >::value
811  &&
812  ( std::is_same< typename DstType::traits::array_layout ,
813  typename Kokkos::LayoutLeft>::value
814  ||
815  std::is_same< typename DstType::traits::array_layout ,
816  typename Kokkos::LayoutRight>::value
817  )
818  )
819  ||
820  (
821  rank(dst) == 1
822  &&
823  rank(src) == 1
824  )
825  ) &&
826 // dst.span_is_contiguous() && //will always fail - stride set to 0s
827 // src.span_is_contiguous() &&
828  dst.span() == src.span() &&
829  dst.dimension_0() == src.dimension_0() &&
830  dst.dimension_1() == src.dimension_1() &&
831  dst.dimension_2() == src.dimension_2() &&
832  dst.dimension_3() == src.dimension_3() &&
833  dst.dimension_4() == src.dimension_4() &&
834  dst.dimension_5() == src.dimension_5() &&
835  dst.dimension_6() == src.dimension_6() &&
836  dst.dimension_7() == src.dimension_7() ) {
837 
838 // const size_t nbytes = sizeof(typename dst_type::value_type) * dst.span() * dimension_scalar(dst) ;
839  //const size_t nbytes = sizeof(typename dst_type::scalar_array_type) * dst.span() ;
840  const size_t nbytes = sizeof(typename dst_type::value_type::value_type) * dst.span() ;
841  //dst_type::value_type is outer FAD type, dst_type::value_type::value_type is inner FAD type
842 
843  Kokkos::Impl::DeepCopy< dst_memory_space , src_memory_space >( dst.data() , src.data() , nbytes );
844  }
845  else if ( std::is_same< typename DstType::traits::value_type ,
846  typename SrcType::traits::non_const_value_type >::value &&
847  (
848  ( std::is_same< typename DstType::traits::array_layout ,
849  typename SrcType::traits::array_layout >::value
850  &&
851  std::is_same< typename DstType::traits::array_layout ,
852  typename Kokkos::LayoutStride>::value
853  )
854  ||
855  (
856  rank(dst) == 1
857  &&
858  rank(src) == 1
859  )
860  ) &&
861 // dst.span_is_contiguous() && //will always fail - stride set to 0s
862 // src.span_is_contiguous() &&
863  dst.span() == src.span() &&
864  dst.dimension_0() == src.dimension_0() &&
865  dst.dimension_1() == src.dimension_1() &&
866  dst.dimension_2() == src.dimension_2() &&
867  dst.dimension_3() == src.dimension_3() &&
868  dst.dimension_4() == src.dimension_4() &&
869  dst.dimension_5() == src.dimension_5() &&
870  dst.dimension_6() == src.dimension_6() &&
871  dst.dimension_7() == src.dimension_7() &&
872  dst.stride_0() == src.stride_0() &&
873  dst.stride_1() == src.stride_1() &&
874  dst.stride_2() == src.stride_2() &&
875  dst.stride_3() == src.stride_3() &&
876  dst.stride_4() == src.stride_4() &&
877  dst.stride_5() == src.stride_5() &&
878  dst.stride_6() == src.stride_6() &&
879  dst.stride_7() == src.stride_7()
880  ) {
881 
882  const size_t nbytes = sizeof(typename dst_type::value_type::value_type) * dst.span() ;
883 
884  Kokkos::Impl::DeepCopy< dst_memory_space , src_memory_space >( dst.data() , src.data() , nbytes );
885  }
886  else if ( DstExecCanAccessSrc ) {
887  // Copying data between views in accessible memory spaces and either non-contiguous or incompatible shape.
888  Kokkos::Experimental::Impl::DynRankViewRemap< dst_type , src_type >( dst , src );
889  }
890  else if ( SrcExecCanAccessDst ) {
891  // Copying data between views in accessible memory spaces and either non-contiguous or incompatible shape.
892  Kokkos::Experimental::Impl::DynRankViewRemap< dst_type , src_type , src_execution_space >( dst , src );
893  }
894  else {
895  Kokkos::Impl::throw_runtime_exception("deep_copy given views that would require a temporary allocation");
896  }
897  }
898 }
899 
900 } //end Experimental
901 
902 using Kokkos::Experimental::deep_copy;
903 
904 }
905 
906 #endif //defined(HAVE_SACADO_VIEW_SPEC) && !defined(SACADO_DISABLE_FAD_VIEW_SPEC)
907 
908 #endif // defined(HAVE_SACADO_KOKKOSCONTAINERS)
909 
910 #endif /* #ifndef KOKKOS_DYN_RANK_VIEW_SACADO_FAD_HPP */
Base template specification for ScalarType.
#define KOKKOS_INLINE_FUNCTION
#define T
Definition: Sacado_rad.hpp:573
#define D
Definition: Sacado_rad.hpp:577
expr expr expr bar false
unsigned dimension_scalar(const View &v, const ViewPack &... views)