Sacado Package Browser (Single Doxygen Collection)  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
KokkosExp_View_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_EXPERIMENTAL_VIEW_SACADO_FAD_HPP
31 #define KOKKOS_EXPERIMENTAL_VIEW_SACADO_FAD_HPP
32 
33 #include "Sacado_ConfigDefs.h"
34 #if defined(HAVE_SACADO_KOKKOSCORE)
35 
36 // Only include forward declarations so any overloads appear before they
37 // might be used inside Kokkos
38 #include "Kokkos_Core_fwd.hpp"
39 #include "Kokkos_Layout.hpp"
40 #include "Kokkos_View.hpp"
41 
42 // Some definition that should exist whether the specializations exist or not
43 
44 namespace Kokkos {
45 
46 // Whether a given type is a view with Sacado FAD scalar type
47 template <typename view_type>
48 struct is_view_fad { static const bool value = false; };
49 
50 // Whether a given type is a view with Sacado FAD scalar type with contiguous
51 // layout
52 template <typename view_type>
53 struct is_view_fad_contiguous { static const bool value = false; };
54 
55 // Template function for extracting sacado dimension
56 template <typename view_type>
58 constexpr unsigned
59 dimension_scalar(const view_type& /* view */) {
60  return 0;
61 }
62 
63 // Template function for extracting aligned sacado dimension
64 template <typename view_type>
66 constexpr unsigned
67 dimension_scalar_aligned(const view_type& view) {
68  return dimension_scalar(view);
69 }
70 
71 }
72 
73 // Make sure the user really wants these View specializations
74 #if defined(HAVE_SACADO_VIEW_SPEC) && !defined(SACADO_DISABLE_FAD_VIEW_SPEC)
75 
76 //----------------------------------------------------------------------------
77 
78 namespace Kokkos {
79 namespace Impl {
80 
81 struct ViewSpecializeSacadoFad {};
82 struct ViewSpecializeSacadoFadContiguous {};
83 
84 template< class ... Args >
85 struct is_ViewSpecializeSacadoFad { enum { value = false }; };
86 
87 template< class D , class ... P , class ... Args >
88 struct is_ViewSpecializeSacadoFad< Kokkos::View<D,P...> , Args... > {
89  enum { value =
90  std::is_same< typename Kokkos::ViewTraits<D,P...>::specialize
91  , ViewSpecializeSacadoFad >::value
92  &&
93  ( ( sizeof...(Args) == 0 ) ||
94  is_ViewSpecializeSacadoFad< Args... >::value ) };
95 };
96 
97 } // namespace Impl
98 } // namespace Kokkos
99 
100 namespace Kokkos {
101 
102 template <typename T, typename ... P>
103 struct is_view_fad< View<T,P...> > {
104  typedef View<T,P...> view_type;
105  static const bool value =
106  std::is_same< typename view_type::specialize,
107  Impl::ViewSpecializeSacadoFad >::value ||
108  std::is_same< typename view_type::specialize,
109  Impl::ViewSpecializeSacadoFadContiguous >::value;
110 };
111 
112 template <typename T, typename ... P>
113 struct is_view_fad_contiguous< View<T,P...> > {
114  typedef View<T,P...> view_type;
115  static const bool value =
116  std::is_same< typename view_type::specialize,
117  Impl::ViewSpecializeSacadoFadContiguous >::value;
118 };
119 
120 }
121 
122 namespace Kokkos {
123 namespace Impl {
124 
125 // Overload view_copy for Fad View's:
126 // 1. Should be faster than using Fad directly
127 // 2. Fixes issues with hierarchical parallelism since the default
128 // implementation uses MDRangePolicy which doesn't work with hierarchical
129 // parallelism.
130 // Needs to go before include of Kokkos_Core.hpp so it is in scope when
131 // Kokkos_CopyViews.hpp is included by Kokkos_Core.hpp, which internally
132 // calls view_copy().
133 template<class DT, class ... DP,
134  class ST, class ... SP>
135 typename std::enable_if< is_view_fad< Kokkos::View<DT,DP...> >::value &&
136  is_view_fad< Kokkos::View<ST,SP...> >::value
137  >::type
138 view_copy(const Kokkos::View<DT,DP...>& dst, const Kokkos::View<ST,SP...>& src);
139 
140 template<class Space, class T, class ... P>
141 struct MirrorType;
142 
143 } // namespace Impl
144 
145 // Declare overloads of create_mirror() so they are in scope
146 // Kokkos_Core.hpp is included below
147 
148 template< class T , class ... P >
149 inline
150 typename Kokkos::View<T,P...>::HostMirror
151 create_mirror(
152  const Kokkos::View<T,P...> & src,
153  typename std::enable_if<
154  ( std::is_same< typename ViewTraits<T,P...>::specialize ,
155  Kokkos::Impl::ViewSpecializeSacadoFad >::value ||
156  std::is_same< typename ViewTraits<T,P...>::specialize ,
157  Kokkos::Impl::ViewSpecializeSacadoFadContiguous >::value ) &&
158  !std::is_same< typename Kokkos::ViewTraits<T,P...>::array_layout,
159  Kokkos::LayoutStride >::value >::type * = 0);
160 
161 
162 template< class T , class ... P >
163 inline
164 typename Kokkos::View<T,P...>::HostMirror
165 create_mirror(
166  const Kokkos::View<T,P...> & src,
167  typename std::enable_if<
168  ( std::is_same< typename ViewTraits<T,P...>::specialize ,
169  Kokkos::Impl::ViewSpecializeSacadoFad >::value ||
170  std::is_same< typename ViewTraits<T,P...>::specialize ,
171  Kokkos::Impl::ViewSpecializeSacadoFadContiguous >::value ) &&
172  std::is_same< typename Kokkos::ViewTraits<T,P...>::array_layout,
173  Kokkos::LayoutStride >::value >::type * = 0);
174 
175 template<class Space, class T, class ... P>
176 typename Impl::MirrorType<Space,T,P ...>::view_type
177 create_mirror(
178  const Space&,
179  const Kokkos::View<T,P...> & src,
180  typename std::enable_if<
181  std::is_same< typename ViewTraits<T,P...>::specialize ,
182  Kokkos::Impl::ViewSpecializeSacadoFad >::value ||
183  std::is_same< typename ViewTraits<T,P...>::specialize ,
184  Kokkos::Impl::ViewSpecializeSacadoFadContiguous >::value >::type * = 0);
185 
186 } // namespace Kokkos
187 
188 #include "Sacado_Traits.hpp"
189 #include "Kokkos_Core.hpp"
191 #include "Kokkos_LayoutNatural.hpp"
192 
193 namespace Kokkos {
194 namespace Impl {
195 
196 // Define our overload of view_copy above. Needs to happen after including
197 // Kokkos_Core.hpp since it calls the default implementation
198 template<class DT, class ... DP,
199  class ST, class ... SP>
200 typename std::enable_if< is_view_fad< Kokkos::View<DT,DP...> >::value &&
201  is_view_fad< Kokkos::View<ST,SP...> >::value
202  >::type
203 view_copy(const Kokkos::View<DT,DP...>& dst, const Kokkos::View<ST,SP...>& src)
204 {
205  typedef typename Kokkos::View<DT,DP...>::array_type dst_array_type;
206  typedef typename Kokkos::View<ST,SP...>::array_type src_array_type;
207  view_copy( dst_array_type(dst) , src_array_type(src) );
208 }
209 
210 } // namespace Impl
211 } // namespace Kokkos
212 
213 namespace Kokkos {
214 
215 template <typename T, typename ... P>
217 constexpr typename
218 std::enable_if< is_view_fad< View<T,P...> >::value, unsigned >::type
219 dimension_scalar(const View<T,P...>& view) {
220 #ifdef KOKKOS_ENABLE_DEPRECATED_CODE
221  return view.implementation_map().dimension_scalar();
222 #else
223  return view.impl_map().dimension_scalar();
224 #endif
225 }
226 
227 template <typename Layout>
228 struct ApplyNatural {
229  typedef LayoutNatural<Layout> type;
230 };
231 
232 template <typename Layout>
233 struct ApplyNatural< LayoutNatural<Layout> > {
234  typedef LayoutNatural<Layout> type;
235 };
236 
237 template < typename T, typename Enable = void >
238 struct ArrayScalar;
239 
240 template < typename T >
241 struct ArrayScalar< T, typename std::enable_if< !Sacado::IsFad<T>::value >::type > {
242  typedef T type;
243 };
244 
245 template < typename T >
246 struct ArrayScalar< T, typename std::enable_if< Sacado::IsFad<T>::value >::type > {
247  typedef typename ArrayScalar< typename Sacado::ValueType<T>::type >::type* type;
248 };
249 
250 
251 template < typename DataType, int Rank >
252 struct AppendRankToConvertedFad {
253  static_assert( Rank > -1, "Sacado AppendRankToConvertedFad Error: Rank < 0" );
254  typedef typename AppendRankToConvertedFad<DataType,Rank-1>::type* type;
255 };
256 
257 // terminating specialization
258 template < typename DataType >
259 struct AppendRankToConvertedFad< DataType, 0 > {
260  typedef DataType type;
261 };
262 
263 
264 template < class ArrayLayout, class Enable = void >
265 struct ViewArrayLayoutSelector;
266 
267 template < class ArrayLayout >
268 struct ViewArrayLayoutSelector< ArrayLayout, typename std::enable_if< std::is_same<ArrayLayout, Kokkos::LayoutLeft>::value >::type >
269 {
270  using type = Kokkos::LayoutLeft;
271 };
272 
273 template < class ArrayLayout >
274 struct ViewArrayLayoutSelector< ArrayLayout, typename std::enable_if< std::is_same<ArrayLayout, Kokkos::LayoutRight>::value >::type >
275 {
276  using type = Kokkos::LayoutRight;
277 };
278 
279 template < class ArrayLayout >
280 struct ViewArrayLayoutSelector< ArrayLayout, typename std::enable_if< std::is_same<ArrayLayout, Kokkos::LayoutStride>::value >::type >
281 {
282  using type = Kokkos::LayoutStride;
283 };
284 
285 template < typename ViewType, typename Enable = void >
286 struct PODViewDeepCopyType;
287 
288 template < typename ViewType >
289 struct PODViewDeepCopyType< ViewType, typename std::enable_if< is_view_fad<ViewType>::value >::type >
290 {
291 
292  typedef ViewType view_type;
293  typedef typename ArrayScalar< typename view_type::value_type >::type fad_converted_type;
294  typedef typename AppendRankToConvertedFad< fad_converted_type, view_type::Rank >::type new_data_type;
295 
296  typedef typename ViewArrayLayoutSelector<typename view_type::array_layout>::type layout;
297  //typedef typename view_type::array_layout layout;
298  typedef typename view_type::device_type device;
299  typedef typename view_type::memory_traits memory;
300 
301  typedef Kokkos::View< new_data_type, layout, device, memory > type;
302 };
303 
304 // Not a Fad type
305 template < typename ViewType >
306 struct PODViewDeepCopyType< ViewType, typename std::enable_if< !is_view_fad<ViewType>::value >::type >
307 {
308  typedef ViewType type;
309 };
310 
311 
312 template <typename ViewType, typename Enabled = void>
313 struct NaturalArrayType {
314  typedef ViewType type;
315 };
316 
317 template <typename D, typename ... P>
318 struct NaturalArrayType< View<D,P...>,
319  typename std::enable_if< is_view_fad< View<D,P...> >::value >::type > {
320  typedef View<D,P...> view_type;
321  typedef typename view_type::data_type data_type;
322  typedef typename view_type::array_layout layout;
323  typedef typename view_type::device_type device;
324  typedef typename view_type::memory_traits memory;
325  //typedef typename ApplyNatural<layout>::type natural_layout;
326  typedef typename ViewArrayLayoutSelector<layout>::type natural_layout;
327  typedef View<data_type,natural_layout,device,memory> type;
328 };
329 
330 namespace Impl {
331 
332 template <class OutputView, typename Enabled = void>
333 struct SacadoViewFill
334 {
335  typedef typename OutputView::const_value_type const_value_type ;
336  typedef typename OutputView::execution_space execution_space ;
337 
338  const OutputView output ;
339  const_value_type input ;
340 
342  void operator()( const size_t i0 ) const
343  {
344  const size_t n1 = output.extent(1);
345  const size_t n2 = output.extent(2);
346  const size_t n3 = output.extent(3);
347  const size_t n4 = output.extent(4);
348  const size_t n5 = output.extent(5);
349  const size_t n6 = output.extent(6);
350  const size_t n7 = output.extent(7);
351 
352  for ( size_t i1 = 0 ; i1 < n1 ; ++i1 ) {
353  for ( size_t i2 = 0 ; i2 < n2 ; ++i2 ) {
354  for ( size_t i3 = 0 ; i3 < n3 ; ++i3 ) {
355  for ( size_t i4 = 0 ; i4 < n4 ; ++i4 ) {
356  for ( size_t i5 = 0 ; i5 < n5 ; ++i5 ) {
357  for ( size_t i6 = 0 ; i6 < n6 ; ++i6 ) {
358  for ( size_t i7 = 0 ; i7 < n7 ; ++i7 ) {
359  output.access(i0,i1,i2,i3,i4,i5,i6,i7) = input ;
360  }}}}}}}
361  }
362 
363  SacadoViewFill( const OutputView & arg_out , const_value_type & arg_in )
364  : output( arg_out ), input( arg_in )
365  {
366  const size_t n0 = output.extent(0);
367  Kokkos::RangePolicy<execution_space> policy( 0, n0 );
368  Kokkos::parallel_for( policy, *this );
369  execution_space::fence();
370  }
371 };
372 
373 }
374 
375 // Overload of deep_copy for Fad views intializing to a constant scalar
376 template< class DT, class ... DP >
377 void deep_copy(
378  const View<DT,DP...> & view ,
379  const typename Sacado::ScalarType< typename View<DT,DP...>::value_type >::type & value
380  , typename std::enable_if<(
381  std::is_same< typename ViewTraits<DT,DP...>::specialize
382  , Kokkos::Impl::ViewSpecializeSacadoFad >::value ||
383  std::is_same< typename ViewTraits<DT,DP...>::specialize
384  , Kokkos::Impl::ViewSpecializeSacadoFadContiguous >::value
385  )>::type * = 0 )
386 {
387  static_assert(
388  std::is_same< typename ViewTraits<DT,DP...>::value_type ,
389  typename ViewTraits<DT,DP...>::non_const_value_type >::value
390  , "Can only deep copy into non-const type" );
391 
392  Impl::SacadoViewFill< View<DT,DP...> >( view , value );
393 }
394 
395 
396 // Overload of deep_copy for Fad views intializing to a constant Fad
397 template< class DT, class ... DP >
398 void deep_copy(
399  const View<DT,DP...> & view ,
400  const typename View<DT,DP...>::value_type & value
401  , typename std::enable_if<(
402  std::is_same< typename ViewTraits<DT,DP...>::specialize
403  , Kokkos::Impl::ViewSpecializeSacadoFad >::value ||
404  std::is_same< typename ViewTraits<DT,DP...>::specialize
405  , Kokkos::Impl::ViewSpecializeSacadoFadContiguous >::value
406  )>::type * = 0 )
407 {
408  static_assert(
409  std::is_same< typename ViewTraits<DT,DP...>::value_type ,
410  typename ViewTraits<DT,DP...>::non_const_value_type >::value
411  , "Can only deep copy into non-const type" );
412 
413  Impl::SacadoViewFill< View<DT,DP...> >( view , value );
414 }
415 
416 
417 /* Specialize for deep copy of FAD */
418 template< class DT , class ... DP , class ST , class ... SP >
419 inline
420 void deep_copy( const View<DT,DP...> & dst ,
421  const View<ST,SP...> & src
422  , typename std::enable_if<(
423  ( std::is_same< typename ViewTraits<DT,DP...>::specialize
424  , Kokkos::Impl::ViewSpecializeSacadoFad >::value
425  ||
426  std::is_same< typename ViewTraits<DT,DP...>::specialize
427  , Kokkos::Impl::ViewSpecializeSacadoFadContiguous >::value )
428  &&
429  ( std::is_same< typename ViewTraits<ST,SP...>::specialize
430  , Kokkos::Impl::ViewSpecializeSacadoFad >::value
431  ||
432  std::is_same< typename ViewTraits<ST,SP...>::specialize
433  , Kokkos::Impl::ViewSpecializeSacadoFadContiguous >::value )
434  )>::type * = 0 )
435 {
436  static_assert(
437  std::is_same< typename ViewTraits<DT,DP...>::value_type ,
438  typename ViewTraits<DT,DP...>::non_const_value_type >::value
439  , "Deep copy destination must be non-const" );
440 
441  static_assert(
442  ( unsigned(ViewTraits<DT,DP...>::rank) ==
443  unsigned(ViewTraits<ST,SP...>::rank) )
444  , "Deep copy destination and source must have same rank" );
445 
446 #if 0
447  // Current impl
448  typedef typename View<DT,DP...>::array_type dst_array_type;
449  typedef typename View<ST,SP...>::array_type src_array_type;
450  typename NaturalArrayType< dst_array_type >::type dst_array( dst );
451  typename NaturalArrayType< src_array_type >::type src_array( src );
452 #else
453  // Copy-assign Views of FadType to Kokkos Views to use Kokkos' deep_copy routine
454  typename PODViewDeepCopyType< View<DT,DP...> >::type dst_array( dst );
455  typename PODViewDeepCopyType< View<ST,SP...> >::type src_array( src );
456 #endif
457  Kokkos::deep_copy( dst_array , src_array );
458 }
459 
460 template< class T , class ... P >
461 inline
462 typename Kokkos::View<T,P...>::HostMirror
463 create_mirror( const Kokkos::View<T,P...> & src
464  , typename std::enable_if<
465  ( std::is_same< typename ViewTraits<T,P...>::specialize ,
466  Kokkos::Impl::ViewSpecializeSacadoFad >::value ||
467  std::is_same< typename ViewTraits<T,P...>::specialize ,
468  Kokkos::Impl::ViewSpecializeSacadoFadContiguous >::value )
469  &&
470  ! std::is_same< typename Kokkos::ViewTraits<T,P...>::array_layout
471  , Kokkos::LayoutStride >::value
472  >::type *
473  )
474 {
475  typedef View<T,P...> src_type ;
476  typedef typename src_type::HostMirror dst_type ;
477 
478  typename src_type::array_layout layout = src.layout();
479  layout.dimension[src_type::rank] = Kokkos::dimension_scalar(src);
480 
481  return dst_type(std::string(src.label()).append("_mirror"), layout);
482 }
483 
484 template< class T , class ... P >
485 inline
486 typename Kokkos::View<T,P...>::HostMirror
487 create_mirror( const Kokkos::View<T,P...> & src
488  , typename std::enable_if<
489  ( std::is_same< typename ViewTraits<T,P...>::specialize ,
490  Kokkos::Impl::ViewSpecializeSacadoFad >::value ||
491  std::is_same< typename ViewTraits<T,P...>::specialize ,
492  Kokkos::Impl::ViewSpecializeSacadoFadContiguous >::value )
493  &&
494  std::is_same< typename Kokkos::ViewTraits<T,P...>::array_layout
495  , Kokkos::LayoutStride >::value
496  >::type *
497  )
498 {
499  typedef View<T,P...> src_type ;
500  typedef typename src_type::HostMirror dst_type ;
501 
502  Kokkos::LayoutStride layout ;
503 
504  layout.dimension[0] = src.extent(0);
505  layout.dimension[1] = src.extent(1);
506  layout.dimension[2] = src.extent(2);
507  layout.dimension[3] = src.extent(3);
508  layout.dimension[4] = src.extent(4);
509  layout.dimension[5] = src.extent(5);
510  layout.dimension[6] = src.extent(6);
511  layout.dimension[7] = src.extent(7);
512 
513  layout.stride[0] = src.stride_0();
514  layout.stride[1] = src.stride_1();
515  layout.stride[2] = src.stride_2();
516  layout.stride[3] = src.stride_3();
517  layout.stride[4] = src.stride_4();
518  layout.stride[5] = src.stride_5();
519  layout.stride[6] = src.stride_6();
520  layout.stride[7] = src.stride_7();
521 
522  layout.dimension[src_type::rank] = Kokkos::dimension_scalar(src);
523 
524  return dst_type(std::string(src.label()).append("_mirror"), layout);
525 }
526 
527 template<class Space, class T, class ... P>
528 typename Impl::MirrorType<Space,T,P ...>::view_type
529 create_mirror(const Space& , const Kokkos::View<T,P...> & src
530  , typename std::enable_if<
531  ( std::is_same< typename ViewTraits<T,P...>::specialize ,
532  Kokkos::Impl::ViewSpecializeSacadoFad >::value ||
533  std::is_same< typename ViewTraits<T,P...>::specialize ,
534  Kokkos::Impl::ViewSpecializeSacadoFadContiguous >::value )
535  >::type *) {
536  typedef View<T,P...> src_type ;
537  typename src_type::array_layout layout = src.layout();
538  layout.dimension[src_type::rank] = Kokkos::dimension_scalar(src);
539  return typename Impl::MirrorType<Space,T,P ...>::view_type(src.label(),layout);
540 }
541 
542 } // namespace Kokkos
543 
544 //----------------------------------------------------------------------------
545 
546 namespace Kokkos {
547 namespace Impl {
548 
549 template< class DataType , class ArrayLayout , class ScalarType , unsigned DimFad >
550 struct FadViewDataAnalysis
551 {
552 private:
553 
554  typedef ViewArrayAnalysis< DataType > array_analysis ;
555 
556 public:
557 
558  // Specialized view data mapping:
559  typedef ViewSpecializeSacadoFad specialize ;
560 
561  typedef typename array_analysis::dimension dimension ;
562  typedef typename array_analysis::value_type value_type ;
563  typedef typename array_analysis::const_value_type const_value_type ;
564  typedef typename array_analysis::non_const_value_type non_const_value_type ;
565 
566  // Generate analogous multidimensional array specification type.
567  typedef typename
568  ViewDataType< value_type , dimension >::type type ;
569  typedef typename
570  ViewDataType< const_value_type , dimension >::type const_type ;
571  typedef typename
572  ViewDataType< non_const_value_type , dimension >::type non_const_type ;
573 
574 private:
575 
576  // A const ?
577  enum { is_const = std::is_same< value_type , const_value_type >::value };
578 
579  // The unwrapped scalar types:
580  typedef typename
581  std::conditional< is_const , const ScalarType , ScalarType >::type
582  scalar_type ;
583 
584  typedef ScalarType non_const_scalar_type ;
585  typedef const ScalarType const_scalar_type ;
586 
587  // Append the FAD static dimension
588  typedef typename array_analysis::dimension::
589  template append<( DimFad ? DimFad + 1 : 0 )>::type
590  scalar_dimension ;
591 
592 public:
593 
594  // Generate "flattened" multidimensional array specification type.
595  typedef typename
596  ViewDataType< scalar_type , scalar_dimension >::type scalar_array_type ;
597 
598  typedef typename
599  ViewDataType< const_scalar_type , scalar_dimension >::type
600  const_scalar_array_type ;
601 
602  typedef typename
603  ViewDataType< non_const_scalar_type , scalar_dimension >::type
604  non_const_scalar_array_type ;
605 };
606 
607 // Specialization for LayoutContiguous, where the Fad type is kept contiguous.
608 // This requires a separate view specialization.
609 template< class DataType , class ArrayLayout , class ScalarType , unsigned DimFad, unsigned Stride >
610 struct FadViewDataAnalysis<DataType, LayoutContiguous<ArrayLayout,Stride>, ScalarType, DimFad>
611 {
612 private:
613 
614  typedef ViewArrayAnalysis< DataType > array_analysis ;
615 
616 public:
617 
618  // For now use the default mapping
619  typedef ViewSpecializeSacadoFadContiguous specialize ;
620 
621  typedef typename array_analysis::dimension dimension ;
622  typedef typename array_analysis::value_type value_type ;
623  typedef typename array_analysis::const_value_type const_value_type ;
624  typedef typename array_analysis::non_const_value_type non_const_value_type ;
625 
626  // Generate analogous multidimensional array specification type.
627  typedef typename
628  ViewDataType< value_type , dimension >::type type ;
629  typedef typename
630  ViewDataType< const_value_type , dimension >::type const_type ;
631  typedef typename
632  ViewDataType< non_const_value_type , dimension >::type non_const_type ;
633 
634 private:
635 
636  // A const ?
637  enum { is_const = std::is_same< value_type , const_value_type >::value };
638 
639  // The unwrapped scalar types:
640  typedef typename
641  std::conditional< is_const , const ScalarType , ScalarType >::type
642  scalar_type ;
643 
644  typedef ScalarType non_const_scalar_type ;
645  typedef const ScalarType const_scalar_type ;
646 
647  // Prepend/append the FAD dimension
648  typedef typename std::conditional<
649  std::is_same< ArrayLayout, Kokkos::LayoutLeft >::value,
650  typename array_analysis::dimension::
651  template prepend<0>::type,
652  typename array_analysis::dimension::
653  template append<0>::type >::type
654  scalar_dimension ;
655 
656 public:
657 
658  // Generate "flattened" multidimensional array specification type.
659  typedef typename
660  ViewDataType< scalar_type , scalar_dimension >::type scalar_array_type ;
661 
662  typedef typename
663  ViewDataType< const_scalar_type , scalar_dimension >::type
664  const_scalar_array_type ;
665 
666  typedef typename
667  ViewDataType< non_const_scalar_type , scalar_dimension >::type
668  non_const_scalar_array_type ;
669 
670 };
671 
672 // Specialization for LayoutNatural, where we don't allow striding within
673 // the FadType.
674 //
675 // Currently this is implemented by choosing the default ViewMapping
676 // specialization.
677 template< class DataType , class ArrayLayout , class ScalarType , unsigned DimFad >
678 struct FadViewDataAnalysis<DataType, LayoutNatural<ArrayLayout>, ScalarType, DimFad>
679 {
680 private:
681 
682  typedef ViewArrayAnalysis< DataType > array_analysis ;
683 
684 public:
685 
686  // For now use the default mapping
687  typedef void specialize ;
688 
689  typedef typename array_analysis::dimension dimension ;
690  typedef typename array_analysis::value_type value_type ;
691  typedef typename array_analysis::const_value_type const_value_type ;
692  typedef typename array_analysis::non_const_value_type non_const_value_type ;
693 
694  // Generate analogous multidimensional array specification type.
695  typedef typename
696  ViewDataType< value_type , dimension >::type type ;
697  typedef typename
698  ViewDataType< const_value_type , dimension >::type const_type ;
699  typedef typename
700  ViewDataType< non_const_value_type , dimension >::type non_const_type ;
701 
702  // Generate "flattened" multidimensional array specification type.
703  typedef type scalar_array_type ;
704  typedef const_type const_scalar_array_type ;
705  typedef non_const_type non_const_scalar_array_type ;
706 
707 };
708 
709 } // namespace Impl
710 } // namespace Kokkos
711 
712 //----------------------------------------------------------------------------
713 
714 namespace Sacado {
715 
716 namespace Fad { namespace Exp { template< typename > class GeneralFad ; } }
717 
718 #ifndef SACADO_NEW_FAD_DESIGN_IS_DEFAULT
719 namespace Fad { template< typename > class DFad ; }
720 namespace Fad { template< typename , int > class SFad ; }
721 namespace Fad { template< typename , int > class SLFad ; }
722 #endif
723 
724 namespace CacheFad { template< typename > class DFad ; }
725 namespace ELRFad { template< typename > class DFad ; }
726 namespace ELRCacheFad { template< typename > class DFad ; }
727 
728 namespace CacheFad { template< typename , int > class SFad ; }
729 namespace ELRFad { template< typename , int > class SFad ; }
730 namespace ELRCacheFad { template< typename , int > class SFad ; }
731 
732 
733 namespace CacheFad { template< typename , int > class SLFad ; }
734 namespace ELRFad { template< typename , int > class SLFad ; }
735 namespace ELRCacheFad { template< typename , int > class SLFad ; }
736 }
737 
738 namespace Kokkos {
739 namespace Impl {
740 
741 #define KOKKOS_VIEW_DATA_ANALYSIS_SACADO_FAD( NS ) \
742 template< class DataType , class ArrayLayout , typename ScalarType > \
743 struct ViewDataAnalysis \
744  < DataType /* Original view data type */ \
745  , ArrayLayout \
746  , Sacado:: NS ::DFad< ScalarType > \
747  > : public FadViewDataAnalysis< DataType, ArrayLayout, ScalarType , 0 > {}; \
748 \
749 template< class DataType , class ArrayLayout , typename ScalarType , int N > \
750 struct ViewDataAnalysis \
751  < DataType /* Original view data type */ \
752  , ArrayLayout \
753  , Sacado:: NS ::SFad< ScalarType , N > \
754  > : public FadViewDataAnalysis< DataType, ArrayLayout, ScalarType , \
755  int(Sacado::StaticSize< Sacado:: NS ::SFad< ScalarType , N > >::value) \
756  > {}; \
757 \
758 template< class DataType , class ArrayLayout , typename ScalarType , int N > \
759 struct ViewDataAnalysis \
760  < DataType /* Original view data type */ \
761  , ArrayLayout \
762  , Sacado:: NS ::SLFad< ScalarType , N > \
763  > : public FadViewDataAnalysis< DataType, ArrayLayout, ScalarType , \
764  int(Sacado::StaticSize< Sacado:: NS ::SLFad< ScalarType , N > >::value) \
765  > {}; \
766 
767 template< class DataType , class ArrayLayout , typename StorageType >
768 struct ViewDataAnalysis
769  < DataType /* Original view data type */
770  , ArrayLayout
771  , Sacado::Fad::Exp::GeneralFad< StorageType >
772  > : public FadViewDataAnalysis< DataType, ArrayLayout, typename StorageType::value_type , 0 > {};
773 
774 #ifndef SACADO_NEW_FAD_DESIGN_IS_DEFAULT
775 KOKKOS_VIEW_DATA_ANALYSIS_SACADO_FAD( Fad )
776 #endif
777 
778 KOKKOS_VIEW_DATA_ANALYSIS_SACADO_FAD( CacheFad )
779 KOKKOS_VIEW_DATA_ANALYSIS_SACADO_FAD( ELRFad )
780 KOKKOS_VIEW_DATA_ANALYSIS_SACADO_FAD( ELRCacheFad )
781 
782 #undef KOKKOS_VIEW_DATA_ANALYSIS_SACADO_FAD
783 
784 } // namespace Impl
785 } // namespace Kokkos
786 
787 //----------------------------------------------------------------------------
788 
789 namespace Kokkos {
790 
791 // Copied from Sacado_ViewFactory
792 template <class View, class ... ViewPack>
794 unsigned dimension_scalar(const View& v, const ViewPack&... views) {
795  const unsigned dim0 = dimension_scalar(v);
796  const unsigned dim1 = dimension_scalar(views...);
797  return dim0 >= dim1 ? dim0 : dim1 ;
798 }
799 
800 } // namespace Kokkos
801 
802 //----------------------------------------------------------------------------
803 
804 namespace Kokkos { namespace Impl {
805 
806 template < typename Specialize, typename A, typename B >
807 struct CommonViewValueType;
808 
809 template < typename A, typename B >
810 struct CommonViewValueType< Kokkos::Impl::ViewSpecializeSacadoFad, A, B >
811 {
812  using value_type = typename Sacado::Promote<A,B>::type ;
813 };
814 
815 template < typename A, typename B >
816 struct CommonViewValueType< Kokkos::Impl::ViewSpecializeSacadoFadContiguous, A, B >
817 {
818  using value_type = typename Sacado::Promote<A,B>::type ;
819 };
820 
821 
822 template < class Specialize, class ValueType >
823 struct CommonViewAllocProp;
824 
825 template < class ValueType >
826 struct CommonViewAllocProp< Kokkos::Impl::ViewSpecializeSacadoFad, ValueType >
827 {
828  using value_type = ValueType;
829  using scalar_array_type = typename Sacado::ValueType< value_type >::type;
830  unsigned fad_dim;
831  bool is_view_type;
832 
834  CommonViewAllocProp()
835  : fad_dim(0) , is_view_type(false) {}
836 
837  // Assume all views are View or DynRankView
838  // TODO If assumption is insufficient, better deduction on is_view...
839  template < class View >
841  CommonViewAllocProp( const View & view )
842  : fad_dim ( dimension_scalar(view) )
843  {
844  is_view_type = (Kokkos::is_view<View>::value || Kokkos::is_view_fad<View>::value);
845  }
846 
847  // TODO If assumption is insufficient, better deduction on is_view...
848  template < class View, class ... Views >
850  CommonViewAllocProp( const View & view, const Views & ... views )
851  : fad_dim ( dimension_scalar(view, views... ) )
852  {
853  is_view_type = (Kokkos::is_view<View>::value || Kokkos::is_view_fad<View>::value);
854  }
855 
856 };
857 
858 template < class ValueType >
859 struct CommonViewAllocProp< Kokkos::Impl::ViewSpecializeSacadoFadContiguous, ValueType >
860 {
861  using value_type = ValueType;
862  using scalar_array_type = typename Sacado::ValueType< value_type >::type;
863  unsigned fad_dim;
864  bool is_view_type;
865 
867  CommonViewAllocProp()
868  : fad_dim(0) , is_view_type(false) {}
869 
870  // Assume all views are View or DynRankView
871  // TODO If assumption is insufficient, better deduction on is_view...
872  template < class View >
874  CommonViewAllocProp( const View & view )
875  : fad_dim ( dimension_scalar(view) )
876  {
877  is_view_type = (Kokkos::is_view<View>::value || Kokkos::is_view_fad<View>::value);
878  }
879 
880  // TODO If assumption is insufficient, better deduction on is_view...
881  template < class View, class ... Views >
883  CommonViewAllocProp( const View & view, const Views & ... views )
884  : fad_dim ( dimension_scalar(view, views... ) )
885  {
886  is_view_type = (Kokkos::is_view<View>::value || Kokkos::is_view_fad<View>::value);
887  }
888 };
889 
890 // Detect if a ViewCtorProp contains a CommonViewAllocProp
891 template < typename ... P >
892 struct has_common_view_alloc_prop : public std::false_type {};
893 
894 template < class Specialize, class ValueType >
895 struct has_common_view_alloc_prop< CommonViewAllocProp<Specialize, ValueType> > : public std::true_type {};
896 
897 
898 // Check for CommonViewAllocProp in pack of properties
899 template < typename ... >
900 struct check_has_common_view_alloc_prop;
901 
902 template <>
903 struct check_has_common_view_alloc_prop<>
904 {
905  enum { value = false };
906 };
907 
908 template < typename P >
909 struct check_has_common_view_alloc_prop<P>
910 {
911  enum { value = has_common_view_alloc_prop< P >::value };
912 };
913 
914 template < typename P0, typename ... P >
915 struct check_has_common_view_alloc_prop<P0, P...>
916 {
917  enum { value = ( (has_common_view_alloc_prop<P0>::value == true) ? true : check_has_common_view_alloc_prop<P...>::value ) };
918 };
919 
920 template < typename ... >
921 struct compute_fad_dim_from_alloc_prop;
922 
923 template < >
924 struct compute_fad_dim_from_alloc_prop<> {
925  template <typename CtorProp>
927  static unsigned eval(const CtorProp&) { return 0; }
928 };
929 
930 template < typename P >
931 struct compute_fad_dim_from_alloc_prop<P> {
932  template <typename CtorProp>
934  static unsigned eval(const CtorProp&) { return 0; }
935 };
936 
937 template < typename P0, typename ... P >
938 struct compute_fad_dim_from_alloc_prop<P0,P...> {
939  template <typename CtorProp>
941  static unsigned eval(const CtorProp& prop) {
942  unsigned d1 = compute_fad_dim_from_alloc_prop<P0>::eval(prop);
943  unsigned d2 = compute_fad_dim_from_alloc_prop<P...>::eval(prop);
944  return d1 > d2 ? d1 : d2;
945  }
946 };
947 
948 template < class ValueType >
949 struct compute_fad_dim_from_alloc_prop<
950  CommonViewAllocProp<ViewSpecializeSacadoFad, ValueType>
951  > {
952  template <typename CtorProp>
954  static unsigned eval(const CtorProp& prop) {
955  using specialize = ViewSpecializeSacadoFad;
956  using CVAP = CommonViewAllocProp< specialize, ValueType >;
957  auto cast_prop = ((Kokkos::Impl::ViewCtorProp<void, CVAP> const &)prop).value;
958  return cast_prop.fad_dim;
959  }
960 };
961 
962 template < class ValueType >
963 struct compute_fad_dim_from_alloc_prop<
964  CommonViewAllocProp<ViewSpecializeSacadoFadContiguous, ValueType>
965  > {
966  template <typename CtorProp>
968  static unsigned eval(const CtorProp& prop) {
969  using specialize = ViewSpecializeSacadoFadContiguous;
970  using CVAP = CommonViewAllocProp< specialize, ValueType >;
971  auto cast_prop = ((Kokkos::Impl::ViewCtorProp<void, CVAP> const &)prop).value;
972  return cast_prop.fad_dim;
973  }
974 };
975 
976 template <typename Traits, typename ... P >
977 struct appendFadToLayoutViewAllocHelper
978 {
979  using layout_type = typename Traits::array_layout;
980  using specialize = typename Traits::specialize;
981  using CtorProp = ViewCtorProp< P... >;
982 
984  static layout_type returnNewLayoutPlusFad( const CtorProp & arg_prop, const layout_type & arg_layout ) {
985 
986  layout_type appended_layout( arg_layout );
987 
988  // Static View case - DynRankView layout handled within createLayout calls
989 
990  const unsigned fad_dim =
991  compute_fad_dim_from_alloc_prop<P...>::eval(arg_prop);
992  appended_layout.dimension[ Traits::rank ] = (fad_dim > 0) ? fad_dim : 1;
993 
994  return appended_layout;
995  }
996 };
997 
998 template <typename Layout>
999 struct prependFadToLayout
1000 {
1001  using layout_type = Layout;
1002 
1003  template < typename FadSizeType >
1005  static layout_type returnNewLayoutPlusFad( const layout_type & arg_layout, const FadSizeType fad_dim ) {
1006 
1007  layout_type prepended_layout(0,0,0,0,0,0,0,0);
1008 
1009  prepended_layout.dimension[0] = fad_dim;
1010 
1011  for ( int i = 1; i < ARRAY_LAYOUT_MAX_RANK; ++i ) {
1012  prepended_layout.dimension[i] = arg_layout.dimension[i-1];
1013  }
1014 
1015  return prepended_layout;
1016  }
1017 };
1018 
1019 } } // namespace Kokkos::Impl
1020 
1021 
1022 //----------------------------------------------------------------------------
1023 
1024 
1025 namespace Kokkos {
1026 namespace Impl {
1027 
1028 template< class Traits >
1029 class ViewMapping< Traits , /* View internal mapping */
1030  typename std::enable_if<
1031  ( std::is_same< typename Traits::specialize
1032  , ViewSpecializeSacadoFad >::value
1033  &&
1034  ( std::is_same< typename Traits::array_layout
1035  , Kokkos::LayoutLeft >::value
1036  ||
1037  std::is_same< typename Traits::array_layout
1038  , Kokkos::LayoutRight >::value
1039  ||
1040  std::is_same< typename Traits::array_layout
1041  , Kokkos::LayoutStride >::value
1042  )
1043  )
1044  , typename Traits::specialize
1045  >::type >
1046 {
1047 private:
1048 
1049  template< class , class ... > friend class ViewMapping ;
1050  template< class , class ... > friend class Kokkos::View ;
1051 
1052  typedef typename Traits::value_type fad_type ;
1053  typedef typename Sacado::ValueType< fad_type >::type fad_value_type ;
1054  typedef typename
1055  std::add_const< fad_value_type >::type const_fad_value_type ;
1056 
1057  enum { FadStaticDimension = Sacado::StaticSize< fad_type >::value };
1059 
1060  // Only LayoutRight has a static stride one
1061  enum { FadStaticStride =
1062  std::is_same< typename Traits::array_layout
1063  , Kokkos::LayoutRight >::value ? 1 : 0 };
1064 
1065  typedef Sacado::integral_nonzero< unsigned , FadStaticStride > sacado_stride_type;
1066 
1067  typedef fad_value_type * handle_type ;
1068 
1069  typedef ViewArrayAnalysis< typename Traits::data_type > array_analysis ;
1070 
1071  // Offset without Fad dimension
1072  typedef ViewOffset< typename Traits::dimension
1073  , typename Traits::array_layout
1074  , void
1075  > offset_type ;
1076 
1077  // Append the fad dimension for the internal offset mapping.
1078  typedef ViewOffset
1079  < typename array_analysis::dimension::
1080  template append<( unsigned(FadStaticDimension) > 0 ? unsigned(FadStaticDimension) + 1 : 0 )>::type
1081  , typename Traits::array_layout
1082  , void
1083  > array_offset_type ;
1084 
1085  handle_type m_impl_handle ;
1086  offset_type m_impl_offset ;
1087  array_offset_type m_array_offset ;
1088  sacado_size_type m_fad_size ;
1089  sacado_stride_type m_fad_stride ;
1090 
1091 public:
1092 
1093  //----------------------------------------
1094  // Domain dimensions
1095 
1096  enum { Rank = Traits::dimension::rank };
1097 
1098  // Using the internal offset mapping so limit to public rank:
1099  template< typename iType >
1100  KOKKOS_INLINE_FUNCTION constexpr size_t extent( const iType & r ) const
1101  { return m_impl_offset.m_dim.extent(r) ; }
1102 
1103  KOKKOS_INLINE_FUNCTION constexpr
1104  typename Traits::array_layout layout() const
1105  { return m_impl_offset.layout(); }
1106 
1107  KOKKOS_INLINE_FUNCTION constexpr size_t dimension_0() const
1108  { return m_impl_offset.dimension_0(); }
1109  KOKKOS_INLINE_FUNCTION constexpr size_t dimension_1() const
1110  { return m_impl_offset.dimension_1(); }
1111  KOKKOS_INLINE_FUNCTION constexpr size_t dimension_2() const
1112  { return m_impl_offset.dimension_2(); }
1113  KOKKOS_INLINE_FUNCTION constexpr size_t dimension_3() const
1114  { return m_impl_offset.dimension_3(); }
1115  KOKKOS_INLINE_FUNCTION constexpr size_t dimension_4() const
1116  { return m_impl_offset.dimension_4(); }
1117  KOKKOS_INLINE_FUNCTION constexpr size_t dimension_5() const
1118  { return m_impl_offset.dimension_5(); }
1119  KOKKOS_INLINE_FUNCTION constexpr size_t dimension_6() const
1120  { return m_impl_offset.dimension_6(); }
1121  KOKKOS_INLINE_FUNCTION constexpr size_t dimension_7() const
1122  { return m_impl_offset.dimension_7(); }
1123 
1124  // Can only be regular layout with uniform striding
1125  // when LayoutRight with contiguous values so not guaranteed true.
1126  using is_regular = std::false_type ;
1127 
1128  KOKKOS_INLINE_FUNCTION constexpr size_t stride_0() const
1129  { return m_impl_offset.stride_0(); }
1130  KOKKOS_INLINE_FUNCTION constexpr size_t stride_1() const
1131  { return m_impl_offset.stride_1(); }
1132  KOKKOS_INLINE_FUNCTION constexpr size_t stride_2() const
1133  { return m_impl_offset.stride_2(); }
1134  KOKKOS_INLINE_FUNCTION constexpr size_t stride_3() const
1135  { return m_impl_offset.stride_3(); }
1136  KOKKOS_INLINE_FUNCTION constexpr size_t stride_4() const
1137  { return m_impl_offset.stride_4(); }
1138  KOKKOS_INLINE_FUNCTION constexpr size_t stride_5() const
1139  { return m_impl_offset.stride_5(); }
1140  KOKKOS_INLINE_FUNCTION constexpr size_t stride_6() const
1141  { return m_impl_offset.stride_6(); }
1142  KOKKOS_INLINE_FUNCTION constexpr size_t stride_7() const
1143  { return m_impl_offset.stride_7(); }
1144 
1145  template< typename iType >
1146  KOKKOS_INLINE_FUNCTION void stride( iType * const s ) const
1147  { m_impl_offset.stride(s) ; }
1148 
1149  // Size of sacado scalar dimension
1150  KOKKOS_FORCEINLINE_FUNCTION constexpr unsigned dimension_scalar() const
1151  { return m_fad_size.value+1; }
1152 
1153  // trode of sacado scalar dimension
1154  KOKKOS_FORCEINLINE_FUNCTION constexpr unsigned stride_scalar() const
1155  { return m_fad_stride.value; }
1156 
1157  //----------------------------------------
1158  // Range of mapping
1159 
1160  // Return type of reference operators
1161  typedef typename
1163 
1165  typedef fad_value_type * pointer_type ;
1166 
1168  KOKKOS_INLINE_FUNCTION constexpr size_t span() const
1169  { return m_array_offset.span(); }
1170 
1172  KOKKOS_INLINE_FUNCTION constexpr bool span_is_contiguous() const
1173  { return m_array_offset.span_is_contiguous() ; }
1174 
1176  KOKKOS_INLINE_FUNCTION constexpr pointer_type data() const
1177  { return m_impl_handle ; }
1178 
1179  //----------------------------------------
1180 
1182  reference_type reference() const
1183  { return reference_type( m_impl_handle
1184  , m_fad_size.value
1185  , m_fad_stride.value ); }
1186 
1187  template< typename I0 >
1189  reference_type
1190  reference( const I0 & i0 ) const
1191  { return reference_type( m_impl_handle + m_array_offset(i0,0)
1192  , m_fad_size.value
1193  , m_fad_stride.value ); }
1194 
1195  template< typename I0 , typename I1 >
1197  reference_type reference( const I0 & i0 , const I1 & i1 ) const
1198  { return reference_type( m_impl_handle + m_array_offset(i0,i1,0)
1199  , m_fad_size.value
1200  , m_fad_stride.value ); }
1201 
1202 
1203  template< typename I0 , typename I1 , typename I2 >
1205  reference_type reference( const I0 & i0 , const I1 & i1 , const I2 & i2 ) const
1206  { return reference_type( m_impl_handle + m_array_offset(i0,i1,i2,0)
1207  , m_fad_size.value
1208  , m_fad_stride.value ); }
1209 
1210  template< typename I0 , typename I1 , typename I2 , typename I3 >
1212  reference_type reference( const I0 & i0 , const I1 & i1 , const I2 & i2 , const I3 & i3 ) const
1213  { return reference_type( m_impl_handle + m_array_offset(i0,i1,i2,i3,0)
1214  , m_fad_size.value
1215  , m_fad_stride.value ); }
1216 
1217  template< typename I0 , typename I1 , typename I2 , typename I3
1218  , typename I4 >
1220  reference_type reference( const I0 & i0 , const I1 & i1 , const I2 & i2 , const I3 & i3
1221  , const I4 & i4 ) const
1222  { return reference_type( m_impl_handle + m_array_offset(i0,i1,i2,i3,i4,0)
1223  , m_fad_size.value
1224  , m_fad_stride.value ); }
1225 
1226  template< typename I0 , typename I1 , typename I2 , typename I3
1227  , typename I4 , typename I5 >
1229  reference_type reference( const I0 & i0 , const I1 & i1 , const I2 & i2 , const I3 & i3
1230  , const I4 & i4 , const I5 & i5 ) const
1231  { return reference_type( m_impl_handle + m_array_offset(i0,i1,i2,i3,i4,i5,0)
1232  , m_fad_size.value
1233  , m_fad_stride.value ); }
1234 
1235 
1236  template< typename I0 , typename I1 , typename I2 , typename I3
1237  , typename I4 , typename I5 , typename I6 >
1239  reference_type reference( const I0 & i0 , const I1 & i1 , const I2 & i2 , const I3 & i3
1240  , const I4 & i4 , const I5 & i5 , const I6 & i6 ) const
1241  { return reference_type( m_impl_handle + m_array_offset(i0,i1,i2,i3,i4,i5,i6,0)
1242  , m_fad_size.value
1243  , m_fad_stride.value ); }
1244 
1245  //----------------------------------------
1246 
1249  static size_t memory_span( typename Traits::array_layout const & layout )
1250  {
1251  size_t dims[8];
1252  for (int i=0; i<8; ++i)
1253  dims[i] = layout.dimension[i];
1254  if (unsigned(FadStaticDimension) > 0)
1255  dims[unsigned(Rank)] = FadStaticDimension+1;
1256 
1257  typename Traits::array_layout alayout(
1258  dims[0], dims[1], dims[2], dims[3],
1259  dims[4], dims[5], dims[6], dims[7] );
1260 
1261  // Do not introduce padding...
1262  typedef std::integral_constant< unsigned , 0 > padding ;
1263  return array_offset_type( padding() , alayout ).span() * sizeof(fad_value_type);
1264  }
1265 
1266  //----------------------------------------
1267 
1268  KOKKOS_INLINE_FUNCTION ~ViewMapping() {}
1269  KOKKOS_INLINE_FUNCTION ViewMapping() : m_impl_handle(0) , m_impl_offset() , m_array_offset() , m_fad_size(0) , m_fad_stride(0) {}
1270 
1271  KOKKOS_INLINE_FUNCTION ViewMapping( const ViewMapping & ) = default ;
1272  KOKKOS_INLINE_FUNCTION ViewMapping & operator = ( const ViewMapping & ) = default ;
1273 
1274  KOKKOS_INLINE_FUNCTION ViewMapping( ViewMapping && ) = default ;
1275  KOKKOS_INLINE_FUNCTION ViewMapping & operator = ( ViewMapping && ) = default ;
1276 
1277  template< class ... P >
1279  ViewMapping
1280  ( ViewCtorProp< P ... > const & prop
1281  , typename Traits::array_layout const & local_layout
1282  )
1283  : m_impl_handle( ( (ViewCtorProp<void,pointer_type> const &) prop ).value )
1284  , m_impl_offset( std::integral_constant< unsigned , 0 >()
1285  , local_layout )
1286  , m_array_offset( std::integral_constant< unsigned , 0 >()
1287  , local_layout )
1288  // Query m_array_offset, not input, in case of static dimension
1289  , m_fad_size(
1290  ( Rank == 0 ? m_array_offset.dimension_0() :
1291  ( Rank == 1 ? m_array_offset.dimension_1() :
1292  ( Rank == 2 ? m_array_offset.dimension_2() :
1293  ( Rank == 3 ? m_array_offset.dimension_3() :
1294  ( Rank == 4 ? m_array_offset.dimension_4() :
1295  ( Rank == 5 ? m_array_offset.dimension_5() :
1296  ( Rank == 6 ? m_array_offset.dimension_6() :
1297  m_array_offset.dimension_7() ))))))) - 1 )
1298  , m_fad_stride(
1299  ( Rank == 0 ? m_array_offset.stride_0() :
1300  ( Rank == 1 ? m_array_offset.stride_1() :
1301  ( Rank == 2 ? m_array_offset.stride_2() :
1302  ( Rank == 3 ? m_array_offset.stride_3() :
1303  ( Rank == 4 ? m_array_offset.stride_4() :
1304  ( Rank == 5 ? m_array_offset.stride_5() :
1305  ( Rank == 6 ? m_array_offset.stride_6() :
1306  m_array_offset.stride_7() ))))))))
1307 
1308  {
1309  const unsigned fad_dim =
1310  ( Rank == 0 ? m_array_offset.dimension_0() :
1311  ( Rank == 1 ? m_array_offset.dimension_1() :
1312  ( Rank == 2 ? m_array_offset.dimension_2() :
1313  ( Rank == 3 ? m_array_offset.dimension_3() :
1314  ( Rank == 4 ? m_array_offset.dimension_4() :
1315  ( Rank == 5 ? m_array_offset.dimension_5() :
1316  ( Rank == 6 ? m_array_offset.dimension_6() :
1317  m_array_offset.dimension_7() )))))));
1318  if (unsigned(FadStaticDimension) == 0 && fad_dim == 0)
1319  Kokkos::abort("invalid fad dimension (0) supplied!");
1320  }
1321 
1322  //----------------------------------------
1323  /* Allocate and construct mapped array.
1324  * Allocate via shared allocation record and
1325  * return that record for allocation tracking.
1326  */
1327  template< class ... P >
1328  SharedAllocationRecord<> *
1329  allocate_shared( ViewCtorProp< P... > const & prop
1330  , typename Traits::array_layout const & local_layout )
1331  {
1332  typedef ViewCtorProp< P... > ctor_prop ;
1333 
1334  typedef typename ctor_prop::execution_space execution_space ;
1335  typedef typename Traits::memory_space memory_space ;
1336  typedef ViewValueFunctor< execution_space , fad_value_type > functor_type ;
1337  typedef SharedAllocationRecord< memory_space , functor_type > record_type ;
1338 
1339  // Disallow padding
1340  typedef std::integral_constant< unsigned , 0 > padding ;
1341 
1342  // Check if ViewCtorProp has CommonViewAllocProp - if so, retrieve the fad_size and append to layout
1343  enum { test_traits_check = Kokkos::Impl::check_has_common_view_alloc_prop< P... >::value };
1344 
1345  m_impl_offset = offset_type( padding(), local_layout );
1346 
1347  typename Traits::array_layout internal_layout =
1348  (test_traits_check == true)
1349  ? Kokkos::Impl::appendFadToLayoutViewAllocHelper< Traits, P... >::returnNewLayoutPlusFad(prop, local_layout)
1350  : local_layout;
1351 
1352  m_array_offset = array_offset_type( padding(), internal_layout );
1353 
1354  const unsigned fad_dim =
1355  ( Rank == 0 ? m_array_offset.dimension_0() :
1356  ( Rank == 1 ? m_array_offset.dimension_1() :
1357  ( Rank == 2 ? m_array_offset.dimension_2() :
1358  ( Rank == 3 ? m_array_offset.dimension_3() :
1359  ( Rank == 4 ? m_array_offset.dimension_4() :
1360  ( Rank == 5 ? m_array_offset.dimension_5() :
1361  ( Rank == 6 ? m_array_offset.dimension_6() :
1362  m_array_offset.dimension_7() )))))));
1363  if (unsigned(FadStaticDimension) == 0 && fad_dim == 0)
1364  Kokkos::abort("invalid fad dimension (0) supplied!");
1365  m_fad_size = fad_dim - 1 ;
1366 
1367  m_fad_stride =
1368  ( Rank == 0 ? m_array_offset.stride_0() :
1369  ( Rank == 1 ? m_array_offset.stride_1() :
1370  ( Rank == 2 ? m_array_offset.stride_2() :
1371  ( Rank == 3 ? m_array_offset.stride_3() :
1372  ( Rank == 4 ? m_array_offset.stride_4() :
1373  ( Rank == 5 ? m_array_offset.stride_5() :
1374  ( Rank == 6 ? m_array_offset.stride_6() :
1375  m_array_offset.stride_7() )))))));
1376 
1377  const size_t alloc_size = m_array_offset.span() * sizeof(fad_value_type);
1378 
1379  // Create shared memory tracking record with allocate memory from the memory space
1380  record_type * const record =
1381  record_type::allocate( ( (ViewCtorProp<void,memory_space> const &) prop ).value
1382  , ( (ViewCtorProp<void,std::string> const &) prop ).value
1383  , alloc_size );
1384 
1385  // Only set the the pointer and initialize if the allocation is non-zero.
1386  // May be zero if one of the dimensions is zero.
1387  if ( alloc_size ) {
1388 
1389  m_impl_handle = handle_type( reinterpret_cast< pointer_type >( record->data() ) );
1390 
1391  if ( ctor_prop::initialize ) {
1392  // Assume destruction is only required when construction is requested.
1393  // The ViewValueFunctor has both value construction and destruction operators.
1394  record->m_destroy = functor_type( ( (ViewCtorProp<void,execution_space> const &) prop).value
1395  , (fad_value_type *) m_impl_handle
1396  , m_array_offset.span()
1397  );
1398 
1399  // Construct values
1400  record->m_destroy.construct_shared_allocation();
1401  }
1402  }
1403 
1404  return record ;
1405  }
1406 
1407 };
1408 
1409 } // namespace Impl
1410 } // namespace Kokkos
1411 
1412 //----------------------------------------------------------------------------
1413 
1414 namespace Kokkos {
1415 namespace Impl {
1416 
1421 template< class DstTraits , class SrcTraits >
1422 class ViewMapping< DstTraits , SrcTraits ,
1423  typename std::enable_if<(
1424  Kokkos::Impl::MemorySpaceAccess
1425  < typename DstTraits::memory_space
1426  , typename SrcTraits::memory_space >::assignable
1427  &&
1428  // Destination view has FAD
1429  std::is_same< typename DstTraits::specialize
1430  , ViewSpecializeSacadoFad >::value
1431  &&
1432  // Source view has FAD
1433  std::is_same< typename SrcTraits::specialize
1434  , ViewSpecializeSacadoFad >::value
1435 
1436  )
1437  , typename DstTraits::specialize
1438  >::type >
1439 {
1440 public:
1441 
1442  enum { is_assignable = true };
1443 
1444  typedef Kokkos::Impl::SharedAllocationTracker TrackType ;
1445  typedef ViewMapping< DstTraits , typename DstTraits::specialize > DstType ;
1446  typedef ViewMapping< SrcTraits , typename SrcTraits::specialize > SrcFadType ;
1447 
1448  template< class DstType >
1449  KOKKOS_INLINE_FUNCTION static
1450  void assign( DstType & dst
1451  , const SrcFadType & src
1452  , const TrackType & )
1453  {
1454  static_assert(
1455  (
1456  std::is_same< typename DstTraits::array_layout
1457  , Kokkos::LayoutLeft >::value ||
1458  std::is_same< typename DstTraits::array_layout
1459  , Kokkos::LayoutRight >::value ||
1460  std::is_same< typename DstTraits::array_layout
1461  , Kokkos::LayoutStride >::value
1462  )
1463  &&
1464  (
1465  std::is_same< typename SrcTraits::array_layout
1466  , Kokkos::LayoutLeft >::value ||
1467  std::is_same< typename SrcTraits::array_layout
1468  , Kokkos::LayoutRight >::value ||
1469  std::is_same< typename SrcTraits::array_layout
1470  , Kokkos::LayoutStride >::value
1471  )
1472  , "View of FAD requires LayoutLeft, LayoutRight, or LayoutStride" );
1473 
1474  static_assert(
1475  std::is_same< typename DstTraits::array_layout
1476  , typename SrcTraits::array_layout >::value ||
1477  std::is_same< typename DstTraits::array_layout
1478  , Kokkos::LayoutStride >::value ,
1479  "View assignment must have compatible layout" );
1480 
1481  static_assert(
1482  std::is_same< typename DstTraits::scalar_array_type
1483  , typename SrcTraits::scalar_array_type >::value ||
1484  std::is_same< typename DstTraits::scalar_array_type
1485  , typename SrcTraits::const_scalar_array_type >::value ,
1486  "View assignment must have same value type or const = non-const" );
1487 
1488  static_assert(
1489  ViewDimensionAssignable
1490  < typename DstType::offset_type::dimension_type
1491  , typename SrcFadType::offset_type::dimension_type >::value ,
1492  "View assignment must have compatible dimensions" );
1493 
1494  static_assert(
1495  ViewDimensionAssignable
1496  < typename DstType::array_offset_type::dimension_type
1497  , typename SrcFadType::array_offset_type::dimension_type >::value ,
1498  "View assignment must have compatible dimensions" );
1499 
1500  typedef typename DstType::offset_type dst_offset_type ;
1501  typedef typename DstType::array_offset_type dst_array_offset_type ;
1502 
1503  dst.m_impl_handle = src.m_impl_handle ;
1504  dst.m_impl_offset = dst_offset_type( src.m_impl_offset );
1505  dst.m_array_offset = dst_array_offset_type( src.m_array_offset );
1506  dst.m_fad_size = src.m_fad_size.value ;
1507  dst.m_fad_stride = src.m_fad_stride.value ;
1508  }
1509 };
1510 
1511 // Integer argument is the actual rank => ranks 0 to Rank-1 will be assigned
1517 template< class DstTraits , class SrcTraits >
1518 class ViewMapping< DstTraits , SrcTraits ,
1519  typename std::enable_if<(
1520  Kokkos::Impl::MemorySpaceAccess
1521  < typename DstTraits::memory_space
1522  , typename SrcTraits::memory_space >::assignable
1523  &&
1524  // Destination view has ordinary
1525  std::is_same< typename DstTraits::specialize , void >::value
1526  &&
1527  // Source view has FAD only
1528  std::is_same< typename SrcTraits::specialize
1529  , ViewSpecializeSacadoFad >::value
1530  )
1531  , typename DstTraits::specialize
1532  >::type >
1533 {
1534 public:
1535 
1536  enum { is_assignable = true };
1537 
1538 
1539  typedef Kokkos::Impl::SharedAllocationTracker TrackType ;
1540  typedef ViewMapping< DstTraits , typename DstTraits::specialize > DstType ;
1541  typedef ViewMapping< SrcTraits , typename SrcTraits::specialize > SrcFadType ;
1542 
1543 
1544  // Helpers to assign, and generate if necessary, ViewOffset to the dst map
1545  // These are necessary to use Kokkos' deep_copy with nested fads
1546  template < class DstType, class SrcFadType, class Truth = void >
1547  struct AssignOffset;
1548 
1549  template < class DstType, class SrcFadType >
1550  struct AssignOffset< DstType, SrcFadType, typename std::enable_if< ((int)DstType::offset_type::dimension_type::rank != (int)SrcFadType::array_offset_type::dimension_type::rank) >::type >
1551  {
1552  // ViewOffset's Dimensions Ranks do not match
1554  static void assign( DstType & dst, const SrcFadType & src )
1555  {
1556  typedef typename SrcTraits::value_type TraitsValueType;
1557 
1560  )
1561  {
1562  typedef typename DstType::offset_type::array_layout DstLayoutType;
1563  //typedef typename ViewArrayLayoutSelector<typename DstType::offset_type::array_layout>::type DstLayoutType;
1564  typedef typename SrcFadType::array_offset_type::dimension_type SrcViewDimension;
1565 
1566  // This is the static dimension of the inner fad, missing from ViewDimension
1567  const size_t InnerStaticDim = Sacado::StaticSize< typename Sacado::ValueType< TraitsValueType >::type >::value;
1568 
1569  static constexpr bool is_layout_left =
1570  std::is_same< DstLayoutType, Kokkos::LayoutLeft>::value;
1571 
1572  typedef typename std::conditional< is_layout_left,
1573  typename SrcViewDimension:: template prepend< InnerStaticDim+1 >::type,
1574  typename SrcViewDimension:: template append < InnerStaticDim+1 >::type
1575  >::type SrcViewDimensionAppended;
1576 
1577  typedef std::integral_constant< unsigned , 0 > padding ;
1578 
1579  typedef ViewOffset< SrcViewDimensionAppended, DstLayoutType > TmpOffsetType;
1580 
1581  auto src_layout = src.m_array_offset.layout();
1582 
1583  if ( is_layout_left ) {
1584  auto prepend_layout = Kokkos::Impl::prependFadToLayout< DstLayoutType >::returnNewLayoutPlusFad(src_layout, InnerStaticDim+1);
1585  TmpOffsetType offset_tmp( padding(), prepend_layout );
1586  dst.m_impl_offset = offset_tmp;
1587  }
1588  else {
1589  TmpOffsetType offset_tmp( padding(), src_layout );
1590  dst.m_impl_offset = offset_tmp;
1591  }
1592 
1593  } else {
1594  Kokkos::abort("Sacado error: Applying AssignOffset for case with nested Fads, but without nested Fads - something went wrong");
1595  }
1596  }
1597  };
1598 
1599  template < class DstType, class SrcFadType >
1600  struct AssignOffset< DstType, SrcFadType, typename std::enable_if< ((int)DstType::offset_type::dimension_type::rank == (int)SrcFadType::array_offset_type::dimension_type::rank) >::type >
1601  {
1603  static void assign( DstType & dst, const SrcFadType & src )
1604  {
1605 
1606  typedef typename DstType::offset_type dst_offset_type ;
1607  dst.m_impl_offset = dst_offset_type( src.m_array_offset );
1608  }
1609  };
1610 
1611 
1612 // If the dst and src mappings are not equal in Rank, the src should come from a View of nested fads
1613 // In the case of two nested fads, the innermost must be an SFad (static Fad)
1614 // The offset_type's are not compatible in the case of nested fads because the ViewDimension's ranks will not agree
1615 // In this case, rather than trying to construct an offset_type from src (which will fail at compile time)
1616 // and assign to dst.m_impl_offset, manually assign the ViewDimension arguments to dst;
1617 // requires appending the missing inner SFad dim + 1 to the Rank-1 ViewDimension
1618  // DstType and SrcFadType are MAPS...
1619  template < class DstType >
1620  KOKKOS_INLINE_FUNCTION static
1621  void
1622  assign( DstType & dst
1623  , const SrcFadType & src
1624  , const TrackType &
1625  )
1626  {
1627 
1628  static_assert(
1629  (
1630  std::is_same< typename DstTraits::array_layout
1631  , Kokkos::LayoutLeft >::value ||
1632  std::is_same< typename DstTraits::array_layout
1633  , Kokkos::LayoutRight >::value ||
1634  std::is_same< typename DstTraits::array_layout
1635  , Kokkos::LayoutStride >::value
1636  )
1637  &&
1638  (
1639  std::is_same< typename SrcTraits::array_layout
1640  , Kokkos::LayoutLeft >::value ||
1641  std::is_same< typename SrcTraits::array_layout
1642  , Kokkos::LayoutRight >::value ||
1643  std::is_same< typename SrcTraits::array_layout
1644  , Kokkos::LayoutStride >::value
1645  )
1646  , "View of FAD requires LayoutLeft, LayoutRight, or LayoutStride" );
1647 
1648  static_assert(
1649  std::is_same< typename DstTraits::array_layout
1650  , typename SrcTraits::array_layout >::value ||
1651  std::is_same< typename DstTraits::array_layout
1652  , Kokkos::LayoutStride >::value ,
1653  "View assignment must have compatible layout" );
1654 #if 0
1655  static_assert(
1656  std::is_same< typename DstTraits::scalar_array_type
1657  , typename SrcTraits::scalar_array_type >::value ||
1658  std::is_same< typename DstTraits::scalar_array_type
1659  , typename SrcTraits::const_scalar_array_type >::value ,
1660  "View assignment must have same value type or const = non-const" );
1661 #endif
1662 
1663  AssignOffset< DstType, SrcFadType >::assign( dst, src );
1664 
1665  dst.m_impl_handle = reinterpret_cast< typename DstType::handle_type >(src.m_impl_handle) ;
1666  }
1667 };
1668 
1669 } // namespace Impl
1670 } // namespace Kokkos
1671 
1672 //----------------------------------------------------------------------------
1673 
1674 namespace Kokkos {
1675 namespace Impl {
1676 
1677 // Subview mapping
1678 
1679 template< class SrcTraits , class ... Args >
1680 struct ViewMapping
1681  < typename std::enable_if<(
1682  // Source view has FAD only
1683  std::is_same< typename SrcTraits::specialize
1684  , ViewSpecializeSacadoFad >::value
1685  &&
1686  (
1687  std::is_same< typename SrcTraits::array_layout
1688  , Kokkos::LayoutLeft >::value ||
1689  std::is_same< typename SrcTraits::array_layout
1690  , Kokkos::LayoutRight >::value ||
1691  std::is_same< typename SrcTraits::array_layout
1692  , Kokkos::LayoutStride >::value
1693  )
1694  )
1695  >::type
1696  , SrcTraits
1697  , Args ... >
1698 {
1699 private:
1700 
1701  static_assert( SrcTraits::rank == sizeof...(Args) , "" );
1702 
1703  enum
1704  { RZ = false
1705  , R0 = bool(is_integral_extent<0,Args...>::value)
1706  , R1 = bool(is_integral_extent<1,Args...>::value)
1707  , R2 = bool(is_integral_extent<2,Args...>::value)
1708  , R3 = bool(is_integral_extent<3,Args...>::value)
1709  , R4 = bool(is_integral_extent<4,Args...>::value)
1710  , R5 = bool(is_integral_extent<5,Args...>::value)
1711  , R6 = bool(is_integral_extent<6,Args...>::value)
1712  };
1713 
1714  // Public rank
1715  enum { rank = unsigned(R0) + unsigned(R1) + unsigned(R2) + unsigned(R3)
1716  + unsigned(R4) + unsigned(R5) + unsigned(R6) };
1717 
1718  // Whether right-most non-FAD rank is a range.
1719  enum { R0_rev = ( 0 == SrcTraits::rank ? RZ : (
1720  1 == SrcTraits::rank ? R0 : (
1721  2 == SrcTraits::rank ? R1 : (
1722  3 == SrcTraits::rank ? R2 : (
1723  4 == SrcTraits::rank ? R3 : (
1724  5 == SrcTraits::rank ? R4 : (
1725  6 == SrcTraits::rank ? R5 : R6 ))))))) };
1726 
1727  // Subview's layout
1728  // If LayoutRight then FAD is contiguous
1729  // For LayoutLeft, result is LayoutLeft only if 1st arg is a range,
1730  // and since last (FAD) dimension is also a range, and these
1731  // ranges must be consecutive, the input rank must be 1
1732  typedef typename std::conditional<
1733  ( /* Same layout IF */
1734  ( rank == 0 )
1735  ||
1736  ( std::is_same< typename SrcTraits::array_layout
1737  , Kokkos::LayoutRight >::value
1738  &&
1739  ( rank == 1 ) && R0_rev
1740  )
1741  ||
1742  ( std::is_same< typename SrcTraits::array_layout
1743  , Kokkos::LayoutLeft >::value
1744  &&
1745  ( rank == 1 ) && (SrcTraits::rank == 1) && R0
1746  )
1747  ), typename SrcTraits::array_layout , Kokkos::LayoutStride
1748  >::type array_layout ;
1749 
1750  typedef typename SrcTraits::value_type fad_type ;
1751 
1752  typedef typename std::conditional< rank == 0 , fad_type ,
1753  typename std::conditional< rank == 1 , fad_type * ,
1754  typename std::conditional< rank == 2 , fad_type ** ,
1755  typename std::conditional< rank == 3 , fad_type *** ,
1756  typename std::conditional< rank == 4 , fad_type **** ,
1757  typename std::conditional< rank == 5 , fad_type ***** ,
1758  typename std::conditional< rank == 6 , fad_type ****** ,
1759  fad_type *******
1760  >::type >::type >::type >::type >::type >::type >::type
1761  data_type ;
1762 
1763 public:
1764 
1765  typedef Kokkos::ViewTraits
1766  < data_type
1767  , array_layout
1768  , typename SrcTraits::device_type
1769  , typename SrcTraits::memory_traits > traits_type ;
1770 
1771  typedef Kokkos::View
1772  < data_type
1773  , array_layout
1774  , typename SrcTraits::device_type
1775  , typename SrcTraits::memory_traits > type ;
1776 
1777 
1779  static void assign( ViewMapping< traits_type , typename traits_type::specialize > & dst
1780  , ViewMapping< SrcTraits ,typename SrcTraits::specialize > const & src
1781  , Args ... args )
1782  {
1783  typedef ViewMapping< traits_type , typename traits_type::specialize > DstType ;
1784  typedef typename DstType::offset_type dst_offset_type ;
1785  typedef typename DstType::array_offset_type dst_array_offset_type ;
1786  typedef typename DstType::handle_type dst_handle_type ;
1787 
1788  const SubviewExtents< SrcTraits::rank , rank >
1789  extents( src.m_impl_offset.m_dim , args... );
1790  const SubviewExtents< SrcTraits::rank + 1 , rank + 1 >
1791  array_extents( src.m_array_offset.m_dim , args... , Kokkos::ALL() );
1792 
1793  dst.m_impl_offset = dst_offset_type( src.m_impl_offset , extents );
1794  dst.m_array_offset = dst_array_offset_type( src.m_array_offset , array_extents );
1795  dst.m_impl_handle =
1796  dst_handle_type( src.m_impl_handle +
1797  src.m_array_offset( array_extents.domain_offset(0)
1798  , array_extents.domain_offset(1)
1799  , array_extents.domain_offset(2)
1800  , array_extents.domain_offset(3)
1801  , array_extents.domain_offset(4)
1802  , array_extents.domain_offset(5)
1803  , array_extents.domain_offset(6)
1804  , array_extents.domain_offset(7) ) );
1805  dst.m_fad_size = src.m_fad_size;
1806  dst.m_fad_stride = src.m_fad_stride.value;
1807  }
1808 
1809 };
1810 
1811 } // namespace Impl
1812 } // namespace Kokkos
1813 
1814 //----------------------------------------------------------------------------
1815 //----------------------------------------------------------------------------
1816 
1817 #if defined(HAVE_SACADO_KOKKOSCORE) && \
1818  defined(HAVE_SACADO_TEUCHOSKOKKOSCOMM) && \
1819  defined(HAVE_SACADO_VIEW_SPEC) && \
1820  ! defined(SACADO_DISABLE_FAD_VIEW_SPEC)
1821 
1822 #include "Kokkos_TeuchosCommAdapters.hpp"
1823 
1824 namespace Teuchos {
1825 
1826 template< typename Ordinal , class SD , class ... SP , class RD , class ... RP >
1827 typename std::enable_if<Kokkos::is_view_fad< Kokkos::View<SD,SP...> >::value &&
1828  Kokkos::is_view_fad< Kokkos::View<RD,RP...> >::value
1829  >::type
1830 reduceAll
1831  ( const Comm<Ordinal>& comm,
1832  const EReductionType reductType ,
1833  const Ordinal count,
1834  const Kokkos::View<SD,SP...> & sendBuffer ,
1835  const Kokkos::View<RD,RP...> & recvBuffer )
1836 {
1837  // We can't implement reduceAll by extracting the underlying array (since we
1838  // can't reduce across the derivative dimension) and we can't just extract
1839  // a pointer due to ViewFad. In principle we could handle ViewFad in the
1840  // serializer, but for the time being we just copy the view's into local
1841  // buffers (on the host).
1842  typedef Kokkos::View<SD,SP...> SendViewType;
1843  typedef Kokkos::View<RD,RP...> RecvViewType;
1844  typedef typename SendViewType::value_type send_value_type;
1845  typedef typename RecvViewType::value_type recv_value_type;
1846 
1848  SendViewType::rank > 1 || RecvViewType::rank > 1, std::invalid_argument,
1849  "Teuchos::reduceAll: Both send and receive Views must have rank 1. "
1850  "The send View's rank is " << SendViewType::rank << " and the receive "
1851  "View's rank is " << RecvViewType::rank << ".");
1852 
1853  // Copy send buffer into local array
1854  Teuchos::Array<send_value_type> localSendBuffer(count);
1855  typename SendViewType::HostMirror hostSendBuffer =
1856  Kokkos::create_mirror_view(sendBuffer);
1857  Kokkos::deep_copy(hostSendBuffer, sendBuffer);
1858  for (Ordinal i=0; i<count; ++i)
1859  localSendBuffer[i] = hostSendBuffer(i);
1860 
1861  // Copy receive buffer into local array (necessary to initialize Fad types
1862  // properly)
1863  Teuchos::Array<recv_value_type> localRecvBuffer(count);
1864  typename RecvViewType::HostMirror hostRecvBuffer =
1865  Kokkos::create_mirror_view(recvBuffer);
1866  Kokkos::deep_copy(hostRecvBuffer, recvBuffer);
1867  for (Ordinal i=0; i<count; ++i)
1868  localRecvBuffer[i] = hostRecvBuffer(i);
1869 
1870  // Do reduce-all
1871  reduceAll(comm, reductType, count,
1872  localSendBuffer.getRawPtr(),
1873  localRecvBuffer.getRawPtr());
1874 
1875  // Copy back into original buffer
1876  for (Ordinal i=0; i<count; ++i)
1877  hostRecvBuffer(i) = localRecvBuffer[i];
1878  Kokkos::deep_copy(recvBuffer, hostRecvBuffer);
1879 }
1880 
1881 
1882 template< typename Ordinal , typename Serializer ,
1883  class SD , class ... SP , class RD , class ... RP >
1884 typename std::enable_if<Kokkos::is_view_fad< Kokkos::View<SD,SP...> >::value &&
1885  Kokkos::is_view_fad< Kokkos::View<RD,RP...> >::value
1886  >::type
1887 reduceAll
1888  ( const Comm<Ordinal>& comm,
1889  const Serializer& serializer,
1890  const EReductionType reductType ,
1891  const Ordinal count,
1892  const Kokkos::View<SD,SP...> & sendBuffer ,
1893  const Kokkos::View<RD,RP...> & recvBuffer )
1894 {
1895  // We can't implement reduceAll by extracting the underlying array (since we
1896  // can't reduce across the derivative dimension) and we can't just extract
1897  // a pointer due to ViewFad. In principle we could handle ViewFad in the
1898  // serializer, but for the time being we just copy the view's into local
1899  // buffers (on the host).
1900  typedef Kokkos::View<SD,SP...> SendViewType;
1901  typedef Kokkos::View<RD,RP...> RecvViewType;
1902  typedef typename SendViewType::value_type send_value_type;
1903  typedef typename RecvViewType::value_type recv_value_type;
1904 
1906  SendViewType::rank > 1 || RecvViewType::rank > 1, std::invalid_argument,
1907  "Teuchos::reduceAll: Both send and receive Views must have rank 1. "
1908  "The send View's rank is " << SendViewType::rank << " and the receive " "View's rank is " << RecvViewType::rank << ".");
1909 
1910  // Copy send buffer into local array
1911  Teuchos::Array<send_value_type> localSendBuffer(count);
1912  typename SendViewType::HostMirror hostSendBuffer =
1913  Kokkos::create_mirror_view(sendBuffer);
1914  Kokkos::deep_copy(hostSendBuffer, sendBuffer);
1915  for (Ordinal i=0; i<count; ++i)
1916  localSendBuffer[i] = hostSendBuffer(i);
1917 
1918  // Copy receive buffer into local array (necessary to initialize Fad types
1919  // properly)
1920  Teuchos::Array<recv_value_type> localRecvBuffer(count);
1921  typename RecvViewType::HostMirror hostRecvBuffer =
1922  Kokkos::create_mirror_view(recvBuffer);
1923  Kokkos::deep_copy(hostRecvBuffer, recvBuffer);
1924  for (Ordinal i=0; i<count; ++i)
1925  localRecvBuffer[i] = hostRecvBuffer(i);
1926 
1927  // Do reduce-all
1928  reduceAll(comm, serializer, reductType, count,
1929  localSendBuffer.getRawPtr(),
1930  localRecvBuffer.getRawPtr());
1931 
1932  // Copy back into original buffer
1933  for (Ordinal i=0; i<count; ++i)
1934  hostRecvBuffer(i) = localRecvBuffer[i];
1935  Kokkos::deep_copy(recvBuffer, hostRecvBuffer);
1936 }
1937 
1938 
1939 template<typename Ordinal, class D, class ... P >
1940 typename std::enable_if<Kokkos::is_view_fad< Kokkos::View<D,P...> >::value>::type
1941 broadcast
1942  ( const Comm<Ordinal>& comm,
1943  const int rootRank ,
1944  const Ordinal count,
1945  const Kokkos::View<D,P...>& buffer)
1946 {
1947  typedef Kokkos::View<D,P...> view_type;
1948  typename view_type::array_type array_buffer = buffer;
1949  Ordinal array_count = count * Kokkos::dimension_scalar(buffer);
1950  broadcast( comm, rootRank, array_count, array_buffer );
1951 }
1952 
1953 template<typename Ordinal,
1954  typename Serializer ,
1955  class D, class ... P >
1956 typename std::enable_if<Kokkos::is_view_fad< Kokkos::View<D,P...> >::value>::type
1957 broadcast
1958  ( const Comm<Ordinal>& comm,
1959  const Serializer& serializer,
1960  const int rootRank ,
1961  const Ordinal count,
1962  const Kokkos::View<D,P...>& buffer)
1963 {
1964  typedef Kokkos::View<D,P...> view_type;
1965  typename view_type::array_type array_buffer = buffer;
1966  Ordinal array_count = count * Kokkos::dimension_scalar(buffer);
1967  broadcast( comm, *(serializer.getValueSerializer()), rootRank,
1968  array_count, array_buffer );
1969 }
1970 
1971 } // namespace Teuchos
1972 
1973 #endif
1974 
1975 //----------------------------------------------------------------------------
1976 
1977 #endif // defined(HAVE_SACADO_VIEW_SPEC) && !defined(SACADO_DISABLE_FAD_VIEW_SPEC)
1978 
1979 #endif // defined(HAVE_SACADO_KOKKOSCORE)
1980 
1982 
1983 #endif /* #ifndef KOKKOS_EXPERIMENTAL_VIEW_SACADO_FAD_HPP */
Base template specification for ScalarType.
Base template specification for whether a type is a Fad type.
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
GeneralFad< StaticStorage< T, Num > > SLFad
Base template specification for static size.
#define KOKKOS_INLINE_FUNCTION
#define T
Definition: Sacado_rad.hpp:573
#define D
Definition: Sacado_rad.hpp:577
GeneralFad< DynamicStorage< T > > DFad
TEUCHOS_DEPRECATED void reduceAll(const Comm< Ordinal > &comm, const EReductionType reductType, const Packet &send, Packet *globalReduct)
int Ordinal
void
Definition: uninit.c:96
expr expr expr bar false
#define KOKKOS_FORCEINLINE_FUNCTION
GeneralFad< StaticFixedStorage< T, Num > > SFad
Base template specification for Promote.
if(first)
Definition: uninit.c:101
Base template specification for testing whether type is statically sized.
Get view type for any Fad type.