TensorMeta.h
Go to the documentation of this file.
1 // This file is part of Eigen, a lightweight C++ template library
2 // for linear algebra.
3 //
4 // Copyright (C) 2015 Benoit Steiner <benoit.steiner.goog@gmail.com>
5 //
6 // This Source Code Form is subject to the terms of the Mozilla
7 // Public License v. 2.0. If a copy of the MPL was not distributed
8 // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 
10 #ifndef EIGEN_CXX11_TENSOR_TENSOR_META_H
11 #define EIGEN_CXX11_TENSOR_TENSOR_META_H
12 
13 #include "./InternalHeaderCheck.h"
14 
15 namespace Eigen {
16 
17 template<bool cond> struct Cond {};
18 
19 template<typename T1, typename T2> EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE
20 const T1& choose(Cond<true>, const T1& first, const T2&) {
21  return first;
22 }
23 
24 template<typename T1, typename T2> EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE
25 const T2& choose(Cond<false>, const T1&, const T2& second) {
26  return second;
27 }
28 
29 
30 template <typename T, typename X, typename Y>
32 T divup(const X x, const Y y) {
33  // Note: This form is used because it cannot overflow.
34  return static_cast<T>(x == 0 ? 0 : (x - 1) / y + 1);
35 }
36 
37 template <typename T>
39 T divup(const T x, const T y) {
40  // Note: This form is used because it cannot overflow.
41  return static_cast<T>(x == 0 ? 0 : (x - 1) / y + 1);
42 }
43 
44 template <size_t n> struct max_n_1 {
45  static const size_t size = n;
46 };
47 template <> struct max_n_1<0> {
48  static const size_t size = 1;
49 };
50 
51 
52 // Default packet types
53 template <typename Scalar, typename Device>
54 struct PacketType : internal::packet_traits<Scalar> {
55  typedef typename internal::packet_traits<Scalar>::type type;
56 };
57 
58 // For CUDA packet types when using a GpuDevice
59 #if defined(EIGEN_USE_GPU) && defined(EIGEN_HAS_GPU_FP16) && defined(EIGEN_GPU_COMPILE_PHASE)
60 
61 typedef ulonglong2 Packet4h2;
62 template<>
63 struct PacketType<half, GpuDevice> {
64  typedef Packet4h2 type;
65  static const int size = 8;
66  enum {
67  HasAdd = 1,
68  HasSub = 1,
69  HasMul = 1,
70  HasNegate = 1,
71  HasAbs = 1,
72  HasArg = 0,
73  HasAbs2 = 0,
74  HasMin = 1,
75  HasMax = 1,
76  HasConj = 0,
77  HasSetLinear = 0,
78  HasBlend = 0,
79 
80  HasDiv = 1,
81  HasSqrt = 1,
82  HasRsqrt = 1,
83  HasExp = 1,
84  HasExpm1 = 0,
85  HasLog = 1,
86  HasLog1p = 0,
87  HasLog10 = 0,
88  HasPow = 1,
89  };
90 };
91 #endif
92 
93 #if defined(EIGEN_USE_SYCL)
94 
95 namespace TensorSycl {
96 namespace internal {
97 
98 template <typename Index, Index A, Index B> struct PlusOp {
99  static constexpr Index Value = A + B;
100 };
101 
102 template <typename Index, Index A, Index B> struct DivOp {
103  static constexpr Index Value = A / B;
104 };
105 
106 template <typename Index, Index start, Index end, Index step,
107  template <class Indx, Indx...> class StepOp>
108 struct static_for {
109  template <typename UnaryOperator>
110  static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void loop(UnaryOperator op) {
111  op(start);
112  static_for<Index, StepOp<Index, start, step>::Value, end, step,
113  StepOp>::loop(op);
114  }
115 };
116 template <typename Index, Index end, Index step,
117  template <class Indx, Indx...> class StepOp>
118 struct static_for<Index, end, end, step, StepOp> {
119  template <typename UnaryOperator>
120  static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void loop(UnaryOperator) {}
121 };
122 
123 template <typename OutScalar, typename Device, bool Vectorizable>
124 struct Vectorise {
125  static constexpr int PacketSize = 1;
126  typedef OutScalar PacketReturnType;
127 };
128 
129 template <typename OutScalar, typename Device>
130 struct Vectorise<OutScalar, Device, true> {
131  static constexpr int PacketSize = Eigen::PacketType<OutScalar, Device>::size;
132  typedef typename Eigen::PacketType<OutScalar, Device>::type PacketReturnType;
133 };
134 
136  return ((((x) + (y)-1) / (y)) * (y));
137 }
138 
139 } // namespace internal
140 } // namespace TensorSycl
141 
142 template <>
143  struct PacketType<half, SyclDevice> {
144  typedef half type;
145  static const int size = 1;
146  enum {
147  HasAdd = 0,
148  HasSub = 0,
149  HasMul = 0,
150  HasNegate = 0,
151  HasAbs = 0,
152  HasArg = 0,
153  HasAbs2 = 0,
154  HasMin = 0,
155  HasMax = 0,
156  HasConj = 0,
157  HasSetLinear = 0,
158  HasBlend = 0
159  };
160 };
161 template <typename Scalar>
162 struct PacketType<Scalar, SyclDevice> : internal::default_packet_traits {
163  typedef Scalar type;
164  typedef Scalar half;
165  enum {
166  Vectorizable = 0,
167  size = 1,
168  AlignedOnScalar = 0,
169  };
170  enum {
171  HasAdd = 0,
172  HasSub = 0,
173  HasMul = 0,
174  HasNegate = 0,
175  HasAbs = 0,
176  HasAbs2 = 0,
177  HasMin = 0,
178  HasMax = 0,
179  HasConj = 0,
180  HasSetLinear = 0
181  };
182 
183 };
184 
185 template <typename Scalar>
186 struct PacketType<Scalar, const SyclDevice> : PacketType<Scalar, SyclDevice>{};
187 
188 #ifndef EIGEN_DONT_VECTORIZE_SYCL
189 #define PACKET_TYPE(CVQual, Type, val, lengths, DEV)\
190 template<> struct PacketType<CVQual Type, DEV> : internal::sycl_packet_traits<val, lengths> \
191 {\
192  typedef typename internal::packet_traits<Type>::type type;\
193  typedef typename internal::packet_traits<Type>::half half;\
194 };
195 
196 
197 PACKET_TYPE(const, float, 1, 4, SyclDevice)
198 PACKET_TYPE(, float, 1, 4, SyclDevice)
199 PACKET_TYPE(const, float, 1, 4, const SyclDevice)
200 PACKET_TYPE(, float, 1, 4, const SyclDevice)
201 
202 PACKET_TYPE(const, double, 0, 2, SyclDevice)
203 PACKET_TYPE(, double, 0, 2, SyclDevice)
204 PACKET_TYPE(const, double, 0, 2, const SyclDevice)
205 PACKET_TYPE(, double, 0, 2, const SyclDevice)
206 #undef PACKET_TYPE
207 
208 template<> struct PacketType<half, const SyclDevice>: PacketType<half, SyclDevice>{};
209 template<> struct PacketType<const half, const SyclDevice>: PacketType<half, SyclDevice>{};
210 #endif
211 #endif
212 
213 // Pair mimics std::pair but works on e.g. nvcc.
214 template <typename U, typename V> struct Pair {
215  public:
217 
218  U first;
220 
221  typedef U first_type;
222  typedef V second_type;
223 
224  EIGEN_CONSTEXPR EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
225  Pair() : first(), second() {}
226 
227  EIGEN_CONSTEXPR EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
228  Pair(const U& f, const V& s) : first(f), second(s) {}
229 
230  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
231  void swap(Pair& rhs) {
232  using numext::swap;
233  swap(first, rhs.first);
234  swap(second, rhs.second);
235  }
236 };
237 
238 template <typename U, typename V>
239 EIGEN_CONSTEXPR EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
240 bool operator==(const Pair<U, V>& x, const Pair<U, V>& y) {
241  return (x.first == y.first && x.second == y.second);
242 }
243 
244 template <typename U, typename V>
245 EIGEN_CONSTEXPR EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
246 bool operator!=(const Pair<U, V>& x, const Pair<U, V>& y) {
247  return !(x == y);
248 }
249 
250 
251 // Can't use std::pairs on cuda devices
252 template <typename Idx> struct IndexPair {
255 
257  first = val.first;
258  second = val.second;
259  }
260 
261  Idx first;
262  Idx second;
263 };
264 
265 
266 namespace internal {
267 
268  template<typename IndexType, typename Index, Index First, Index... Is>
269  EIGEN_CONSTEXPR EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
270  array<Index, 1 + sizeof...(Is)> customIndices2Array(IndexType& idx, numeric_list<Index, First, Is...>) {
271  return { static_cast<Index>(idx[First]), static_cast<Index>(idx[Is])... };
272  }
273  template<typename IndexType, typename Index>
274  EIGEN_CONSTEXPR EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
275  array<Index, 0> customIndices2Array(IndexType&, numeric_list<Index>) {
276  return array<Index, 0>();
277  }
278 
280  template<typename Index, std::size_t NumIndices, typename IndexType>
281  EIGEN_CONSTEXPR EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
283  return customIndices2Array(idx, typename gen_numeric_list<Index, NumIndices>::type{});
284  }
285 
286 
287  template <typename B, typename D>
288  struct is_base_of
289  {
290 
291  typedef char (&yes)[1];
292  typedef char (&no)[2];
293 
294  template <typename BB, typename DD>
295  struct Host
296  {
297  operator BB*() const;
298  operator DD*();
299  };
300 
301  template<typename T>
302  static yes check(D*, T);
303  static no check(B*, int);
304 
305  static const bool value = sizeof(check(Host<B,D>(), int())) == sizeof(yes);
306  };
307 
308 }
309 
310 } // namespace Eigen
311 
312 #endif // EIGEN_CXX11_TENSOR_TENSOR_META_H
int n
SparseMatrix< double > A(n, n)
Matrix3f y
MatrixXcd V
MatrixXf B
#define EIGEN_ALWAYS_INLINE
#define EIGEN_CONSTEXPR
#define EIGEN_DEVICE_FUNC
#define EIGEN_MAKE_ALIGNED_OPERATOR_NEW
static const lastp1_t end
EIGEN_CONSTEXPR array< Index, 1+sizeof...(Is)> customIndices2Array(IndexType &idx, numeric_list< Index, First, Is... >)
Definition: TensorMeta.h:270
EIGEN_CONSTEXPR Index first(const T &x) EIGEN_NOEXCEPT
void swap(T &a, T &b)
: TensorContractionSycl.h, provides various tensor contraction kernel for SYCL backend
EIGEN_CONSTEXPR bool operator==(const Pair< U, V > &x, const Pair< U, V > &y)
Definition: TensorMeta.h:240
std::array< T, N > array
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
EIGEN_ALWAYS_INLINE T divup(const X x, const Y y)
Definition: TensorMeta.h:32
EIGEN_CONSTEXPR bool operator!=(const Pair< U, V > &x, const Pair< U, V > &y)
Definition: TensorMeta.h:246
EIGEN_ALWAYS_INLINE const T1 & choose(Cond< true >, const T1 &first, const T2 &)
Definition: TensorMeta.h:20
EIGEN_CONSTEXPR EIGEN_ALWAYS_INLINE IndexPair()
Definition: TensorMeta.h:253
void set(IndexPair< Idx > val)
Definition: TensorMeta.h:256
EIGEN_CONSTEXPR EIGEN_ALWAYS_INLINE IndexPair(Idx f, Idx s)
Definition: TensorMeta.h:254
internal::packet_traits< Scalar >::type type
Definition: TensorMeta.h:55
void swap(Pair &rhs)
Definition: TensorMeta.h:231
EIGEN_CONSTEXPR Pair(const U &f, const V &s)
Definition: TensorMeta.h:228
EIGEN_CONSTEXPR Pair()
Definition: TensorMeta.h:225
EIGEN_MAKE_ALIGNED_OPERATOR_NEW U first
Definition: TensorMeta.h:218
SparseMat::Index size
static const size_t size
Definition: TensorMeta.h:45