TensorMeta.h
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 namespace Eigen {
14 
15 template<bool cond> struct Cond {};
16 
17 template<typename T1, typename T2> EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE
18 const T1& choose(Cond<true>, const T1& first, const T2&) {
19  return first;
20 }
21 
22 template<typename T1, typename T2> EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE
23 const T2& choose(Cond<false>, const T1&, const T2& second) {
24  return second;
25 }
26 
27 
28 template <typename T, typename X, typename Y>
29 EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE
30 T divup(const X x, const Y y) {
31  return static_cast<T>((x + y - 1) / y);
32 }
33 
34 template <typename T>
35 EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE
36 T divup(const T x, const T y) {
37  return static_cast<T>((x + y - 1) / y);
38 }
39 
40 template <size_t n> struct max_n_1 {
41  static const size_t size = n;
42 };
43 template <> struct max_n_1<0> {
44  static const size_t size = 1;
45 };
46 
47 
48 // Default packet types
49 template <typename Scalar, typename Device>
50 struct PacketType : internal::packet_traits<Scalar> {
51  typedef typename internal::packet_traits<Scalar>::type type;
52 };
53 
54 // For CUDA packet types when using a GpuDevice
55 #if defined(EIGEN_USE_GPU) && defined(__CUDACC__) && defined(EIGEN_HAS_CUDA_FP16)
56 template <>
57 struct PacketType<half, GpuDevice> {
58  typedef half2 type;
59  static const int size = 2;
60  enum {
61  HasAdd = 1,
62  HasSub = 1,
63  HasMul = 1,
64  HasNegate = 1,
65  HasAbs = 1,
66  HasArg = 0,
67  HasAbs2 = 0,
68  HasMin = 1,
69  HasMax = 1,
70  HasConj = 0,
71  HasSetLinear = 0,
72  HasBlend = 0,
73 
74  HasDiv = 1,
75  HasSqrt = 1,
76  HasRsqrt = 1,
77  HasExp = 1,
78  HasLog = 1,
79  HasLog1p = 0,
80  HasLog10 = 0,
81  HasPow = 1,
82  };
83 };
84 #endif
85 
86 
87 
88 // Tuple mimics std::pair but works on e.g. nvcc.
89 template <typename U, typename V> struct Tuple {
90  public:
91  U first;
92  V second;
93 
94  typedef U first_type;
95  typedef V second_type;
96 
97  EIGEN_CONSTEXPR EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
98  Tuple() : first(), second() {}
99 
100  EIGEN_CONSTEXPR EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
101  Tuple(const U& f, const V& s) : first(f), second(s) {}
102 
103  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
104  Tuple& operator= (const Tuple& rhs) {
105  if (&rhs == this) return *this;
106  first = rhs.first;
107  second = rhs.second;
108  return *this;
109  }
110 
111  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
112  void swap(Tuple& rhs) {
113  using numext::swap;
114  swap(first, rhs.first);
115  swap(second, rhs.second);
116  }
117 };
118 
119 template <typename U, typename V>
120 EIGEN_CONSTEXPR EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
121 bool operator==(const Tuple<U, V>& x, const Tuple<U, V>& y) {
122  return (x.first == y.first && x.second == y.second);
123 }
124 
125 template <typename U, typename V>
126 EIGEN_CONSTEXPR EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
127 bool operator!=(const Tuple<U, V>& x, const Tuple<U, V>& y) {
128  return !(x == y);
129 }
130 
131 
132 // Can't use std::pairs on cuda devices
133 template <typename Idx> struct IndexPair {
134  EIGEN_CONSTEXPR EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE IndexPair() : first(0), second(0) {}
135  EIGEN_CONSTEXPR EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE IndexPair(Idx f, Idx s) : first(f), second(s) {}
136 
137  EIGEN_DEVICE_FUNC void set(IndexPair<Idx> val) {
138  first = val.first;
139  second = val.second;
140  }
141 
142  Idx first;
143  Idx second;
144 };
145 
146 
147 #ifdef EIGEN_HAS_SFINAE
148 namespace internal {
149 
150  template<typename IndexType, Index... Is>
151  EIGEN_CONSTEXPR EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
152  array<Index, sizeof...(Is)> customIndices2Array(IndexType& idx, numeric_list<Index, Is...>) {
153  return { idx[Is]... };
154  }
155  template<typename IndexType>
156  EIGEN_CONSTEXPR EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
157  array<Index, 0> customIndices2Array(IndexType&, numeric_list<Index>) {
158  return array<Index, 0>();
159  }
160 
162  template<typename Index, std::size_t NumIndices, typename IndexType>
163  EIGEN_CONSTEXPR EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
164  array<Index, NumIndices> customIndices2Array(IndexType& idx) {
165  return customIndices2Array(idx, typename gen_numeric_list<Index, NumIndices>::type{});
166  }
167 
168 
169  template <typename B, typename D>
170  struct is_base_of
171  {
172 
173  typedef char (&yes)[1];
174  typedef char (&no)[2];
175 
176  template <typename BB, typename DD>
177  struct Host
178  {
179  operator BB*() const;
180  operator DD*();
181  };
182 
183  template<typename T>
184  static yes check(D*, T);
185  static no check(B*, int);
186 
187  static const bool value = sizeof(check(Host<B,D>(), int())) == sizeof(yes);
188  };
189 
190 }
191 #endif
192 
193 
194 
195 } // namespace Eigen
196 
197 #endif // EIGEN_CXX11_TENSOR_TENSOR_META_H
Namespace containing all symbols from the Eigen library.
Definition: AdolcForward:45