Sacado Package Browser (Single Doxygen Collection)  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Sacado_Fad_Kokkos_ThreadLocalScalar.hpp
Go to the documentation of this file.
1 // @HEADER
2 // *****************************************************************************
3 // Sacado Package
4 //
5 // Copyright 2006 NTESS and the Sacado contributors.
6 // SPDX-License-Identifier: LGPL-2.1-or-later
7 // *****************************************************************************
8 // @HEADER
9 
10 #ifndef SACADO_FAD_KOKKOS_VIEW_SUPPORT_INCLUDES
11 #error "This file can only be included by Sacado_Fad_Kokkos_View_Support.hpp"
12 #endif
13 
14 // =====================================================================
15 // This file includes helpers to deal with local temporaries of Sacado
16 // Fad types.
17 // -- partition_scalar
18 // -- LocalScalarType
19 // -- ThreadLocalScalarType
20 
21 namespace Sacado {
22 
23 template <unsigned Stride, typename T>
24 KOKKOS_INLINE_FUNCTION const T &partition_scalar(const T &x) {
25  return x;
26 }
27 
28 // Type of local scalar type when partitioning a view
29 template <typename T, unsigned Stride> struct LocalScalarType {
30  typedef T type;
31 };
32 template <typename T, unsigned Stride> struct LocalScalarType<const T, Stride> {
34  typedef const lst type;
35 };
36 
37 // For DFad, the size is not part of the type, so the default implementation
38 // is sufficient
39 
40 // Type of local scalar type when partitioning a view
41 //
42 // For SLFad, divde the array size by the given stride
43 namespace Fad {
44 namespace Exp {
45 template <typename T, int N> class StaticStorage;
46 template <typename S> class GeneralFad;
47 } // namespace Exp
48 } // namespace Fad
49 template <typename T, int N, unsigned Stride>
50 struct LocalScalarType<Fad::Exp::GeneralFad<Fad::Exp::StaticStorage<T, N>>,
51  Stride> {
52  static const int Ns = (N + Stride - 1) / Stride;
54 };
55 #ifndef SACADO_NEW_FAD_DESIGN_IS_DEFAULT
56 namespace Fad {
57 template <typename T, int N> class SLFad;
58 }
59 template <typename T, int N, unsigned Stride>
60 struct LocalScalarType<Fad::SLFad<T, N>, Stride> {
61  static const int Ns = (N + Stride - 1) / Stride;
63 };
64 #endif
65 
66 // Type of local scalar type when partitioning a view
67 //
68 // For SFad, divde the array size by the given stride. If it divides evenly,
69 // use SFad, otherwise use SLFad
70 namespace Fad {
71 namespace Exp {
72 template <typename T, typename U> class DynamicStorage;
73 template <typename T, int N> class StaticFixedStorage;
74 template <typename T, int N> class StaticStorage;
75 template <typename S> class GeneralFad;
76 } // namespace Exp
77 } // namespace Fad
78 template <typename T, int N, unsigned Stride>
79 struct LocalScalarType<Fad::Exp::GeneralFad<Fad::Exp::StaticFixedStorage<T, N>>,
80  Stride> {
81  static const int Ns = (N + Stride - 1) / Stride;
82  typedef typename std::conditional<
83  Ns == N / Stride,
86 };
87 
88 #ifndef SACADO_NEW_FAD_DESIGN_IS_DEFAULT
89 namespace Fad {
90 template <typename T, int N> class SFad;
91 }
92 template <typename T, int N, unsigned Stride>
93 struct LocalScalarType<Fad::SFad<T, N>, Stride> {
94  static const int Ns = (N + Stride - 1) / Stride;
95  typedef typename std::conditional<Ns == N / Stride, Fad::SFad<T, Ns>,
97 };
98 #endif
99 
100 #if defined(__CUDA_ARCH__) || defined(__HIP_DEVICE_COMPILE__)
101 
102 #ifndef SACADO_VIEW_CUDA_HIERARCHICAL_DFAD
103 template <unsigned Stride, typename T, typename U>
104 KOKKOS_INLINE_FUNCTION typename LocalScalarType<
108  typedef typename LocalScalarType<
110  ret_type;
111  const int size = (x.size() + blockDim.x - threadIdx.x - 1) / blockDim.x;
112  const int offset = threadIdx.x;
113  ret_type xp(size, x.val());
114 
115  // Note: we can't use x.dx(offset+i*Stride) if
116  // SACADO_VIEW_CUDA_HIERARCHICAL_DFAD_STRIDED is defined because it already
117  // uses blockDim.x in its index calculation. This approach should work
118  // regardless
119  const T *dx = x.dx();
120  for (int i = 0; i < size; ++i)
121  xp.fastAccessDx(i) = dx[offset + i * Stride];
122 
123  return xp;
124 }
125 #endif
126 template <unsigned Stride, typename T, int N>
127 KOKKOS_INLINE_FUNCTION typename LocalScalarType<
128  Fad::Exp::GeneralFad<Fad::Exp::StaticStorage<T, N>>, Stride>::type
129 partition_scalar(const Fad::Exp::GeneralFad<Fad::Exp::StaticStorage<T, N>> &x) {
130  typedef typename LocalScalarType<
131  Fad::Exp::GeneralFad<Fad::Exp::StaticStorage<T, N>>, Stride>::type
132  ret_type;
133  const int size = (x.size() + blockDim.x - threadIdx.x - 1) / blockDim.x;
134  const int offset = threadIdx.x;
135  ret_type xp(size, x.val());
136  for (int i = 0; i < size; ++i)
137  xp.fastAccessDx(i) = x.fastAccessDx(offset + i * Stride);
138  return xp;
139 }
140 template <unsigned Stride, typename T, int N>
141 KOKKOS_INLINE_FUNCTION typename LocalScalarType<
142  Fad::Exp::GeneralFad<Fad::Exp::StaticFixedStorage<T, N>>, Stride>::type
144  const Fad::Exp::GeneralFad<Fad::Exp::StaticFixedStorage<T, N>> &x) {
145  typedef typename LocalScalarType<
146  Fad::Exp::GeneralFad<Fad::Exp::StaticFixedStorage<T, N>>, Stride>::type
147  ret_type;
148  const int size = (x.size() + blockDim.x - threadIdx.x - 1) / blockDim.x;
149  const int offset = threadIdx.x;
150  ret_type xp(size, x.val());
151  for (int i = 0; i < size; ++i)
152  xp.fastAccessDx(i) = x.fastAccessDx(offset + i * Stride);
153  return xp;
154 }
155 #endif
156 
157 template <typename ViewType, typename Enabled = void>
159  typedef typename ViewType::non_const_value_type type;
160 };
161 
162 template <typename ViewType>
164  ViewType,
165  typename std::enable_if<is_view_fad_contiguous<ViewType>::value>::type> {
166  typedef typename ViewType::traits TraitsType;
167  // typedef Impl::ViewMapping<TraitsType, typename TraitsType::specialize>
168  // MappingType; typedef typename MappingType::thread_local_scalar_type type;
169 
170  using fad_type = typename ViewType::value_type;
171  enum { FadStaticDimension = Sacado::StaticSize<fad_type>::value };
172  enum { PartitionedFadStride = TraitsType::array_layout::scalar_stride };
173 
174  // The partitioned static size -- this will be 0 if ParitionedFadStride
175  // does not evenly divide FadStaticDimension
176  enum {
177  PartitionedFadStaticDimension =
178  Impl::computeFadPartitionSize(FadStaticDimension, PartitionedFadStride)
179  };
180 #ifdef KOKKOS_ENABLE_CUDA
181  typedef typename Sacado::LocalScalarType<
182  fad_type, unsigned(PartitionedFadStride)>::type strided_scalar_type;
183  typedef typename std::conditional<
185  strided_scalar_type, fad_type>::type thread_local_scalar_type;
186 #elif defined(KOKKOS_ENABLE_HIP)
187  typedef typename Sacado::LocalScalarType<
188  fad_type, unsigned(PartitionedFadStride)>::type strided_scalar_type;
189  typedef typename std::conditional<
191  strided_scalar_type, fad_type>::type thread_local_scalar_type;
192 #else
194 #endif
196 };
197 } // namespace Sacado
KOKKOS_INLINE_FUNCTION const T & partition_scalar(const T &x)
expr expr dx(i)
GeneralFad< StaticStorage< T, Num > > SLFad
Base template specification for static size.
std::conditional< Ns==N/Stride, Fad::Exp::GeneralFad< Fad::Exp::StaticFixedStorage< T, Ns > >, Fad::Exp::GeneralFad< Fad::Exp::StaticStorage< T, Ns > > >::type type
#define T
Definition: Sacado_rad.hpp:553
Forward-mode AD class templated on the storage for the derivative array.
int value
const int N
Derivative array storage class using dynamic memory allocation.
std::conditional< Ns==N/Stride, Fad::SFad< T, Ns >, Fad::SLFad< T, Ns > >::type type
GeneralFad< StaticFixedStorage< T, Num > > SFad