TensorCustomOp.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) 2014 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_CUSTOM_OP_H
11 #define EIGEN_CXX11_TENSOR_TENSOR_CUSTOM_OP_H
12 
13 // IWYU pragma: private
14 #include "./InternalHeaderCheck.h"
15 
16 namespace Eigen {
17 
25 namespace internal {
26 template <typename CustomUnaryFunc, typename XprType>
27 struct traits<TensorCustomUnaryOp<CustomUnaryFunc, XprType> > {
28  typedef typename XprType::Scalar Scalar;
29  typedef typename XprType::StorageKind StorageKind;
30  typedef typename XprType::Index Index;
31  typedef typename XprType::Nested Nested;
32  typedef std::remove_reference_t<Nested> Nested_;
33  static constexpr int NumDimensions = traits<XprType>::NumDimensions;
34  static constexpr int Layout = traits<XprType>::Layout;
36 };
37 
38 template <typename CustomUnaryFunc, typename XprType>
39 struct eval<TensorCustomUnaryOp<CustomUnaryFunc, XprType>, Eigen::Dense> {
41 };
42 
43 template <typename CustomUnaryFunc, typename XprType>
44 struct nested<TensorCustomUnaryOp<CustomUnaryFunc, XprType> > {
46 };
47 
48 } // end namespace internal
49 
50 template <typename CustomUnaryFunc, typename XprType>
51 class TensorCustomUnaryOp : public TensorBase<TensorCustomUnaryOp<CustomUnaryFunc, XprType>, ReadOnlyAccessors> {
52  public:
55  typedef typename XprType::CoeffReturnType CoeffReturnType;
59 
61  : m_expr(expr), m_func(func) {}
62 
63  EIGEN_DEVICE_FUNC const CustomUnaryFunc& func() const { return m_func; }
64 
66 
67  protected:
68  typename XprType::Nested m_expr;
69  const CustomUnaryFunc m_func;
70 };
71 
72 // Eval as rvalue
73 template <typename CustomUnaryFunc, typename XprType, typename Device>
74 struct TensorEvaluator<const TensorCustomUnaryOp<CustomUnaryFunc, XprType>, Device> {
77  static constexpr int NumDims = internal::traits<ArgType>::NumDimensions;
79  typedef std::remove_const_t<typename ArgType::Scalar> Scalar;
80  typedef std::remove_const_t<typename XprType::CoeffReturnType> CoeffReturnType;
86 
88  enum {
89  IsAligned = false,
91  BlockAccess = false,
92  PreferBlockAccess = false,
93  CoordAccess = false, // to be implemented
94  RawAccess = false
95  };
96 
97  //===- Tensor block evaluation strategy (see TensorBlock.h) -------------===//
99  //===--------------------------------------------------------------------===//
100 
101  EIGEN_STRONG_INLINE TensorEvaluator(const ArgType& op, const Device& device)
102  : m_op(op), m_device(device), m_result(NULL) {
103  m_dimensions = op.func().dimensions(op.expression());
104  }
105 
106  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Dimensions& dimensions() const { return m_dimensions; }
107 
109  if (data) {
110  evalTo(data);
111  return false;
112  } else {
113  m_result = static_cast<EvaluatorPointerType>(
114  m_device.get((CoeffReturnType*)m_device.allocate_temp(dimensions().TotalSize() * sizeof(Scalar))));
115  evalTo(m_result);
116  return true;
117  }
118  }
119 
121  if (m_result) {
122  m_device.deallocate_temp(m_result);
123  m_result = NULL;
124  }
125  }
126 
127  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE CoeffReturnType coeff(Index index) const { return m_result[index]; }
128 
129  template <int LoadMode>
131  return internal::ploadt<PacketReturnType, LoadMode>(m_result + index);
132  }
133 
135  // TODO(rmlarsen): Extend CustomOp API to return its cost estimate.
136  return TensorOpCost(sizeof(CoeffReturnType), 0, 0, vectorized, PacketSize);
137  }
138 
139  EIGEN_DEVICE_FUNC EvaluatorPointerType data() const { return m_result; }
140 
141  protected:
144  m_op.func().eval(m_op.expression(), result, m_device);
145  }
146 
148  const ArgType m_op;
151 };
152 
160 namespace internal {
161 template <typename CustomBinaryFunc, typename LhsXprType, typename RhsXprType>
162 struct traits<TensorCustomBinaryOp<CustomBinaryFunc, LhsXprType, RhsXprType> > {
164  typedef typename internal::promote_storage_type<typename LhsXprType::CoeffReturnType,
165  typename RhsXprType::CoeffReturnType>::ret CoeffReturnType;
168  typedef
170  typedef typename LhsXprType::Nested LhsNested;
171  typedef typename RhsXprType::Nested RhsNested;
172  typedef std::remove_reference_t<LhsNested> LhsNested_;
173  typedef std::remove_reference_t<RhsNested> RhsNested_;
174  static constexpr int NumDimensions = traits<LhsXprType>::NumDimensions;
175  static constexpr int Layout = traits<LhsXprType>::Layout;
179 };
180 
181 template <typename CustomBinaryFunc, typename LhsXprType, typename RhsXprType>
182 struct eval<TensorCustomBinaryOp<CustomBinaryFunc, LhsXprType, RhsXprType>, Eigen::Dense> {
184 };
185 
186 template <typename CustomBinaryFunc, typename LhsXprType, typename RhsXprType>
187 struct nested<TensorCustomBinaryOp<CustomBinaryFunc, LhsXprType, RhsXprType> > {
189 };
190 
191 } // end namespace internal
192 
193 template <typename CustomBinaryFunc, typename LhsXprType, typename RhsXprType>
195  : public TensorBase<TensorCustomBinaryOp<CustomBinaryFunc, LhsXprType, RhsXprType>, ReadOnlyAccessors> {
196  public:
203 
204  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorCustomBinaryOp(const LhsXprType& lhs, const RhsXprType& rhs,
205  const CustomBinaryFunc& func)
206 
207  : m_lhs_xpr(lhs), m_rhs_xpr(rhs), m_func(func) {}
208 
209  EIGEN_DEVICE_FUNC const CustomBinaryFunc& func() const { return m_func; }
210 
212  return m_lhs_xpr;
213  }
214 
216  return m_rhs_xpr;
217  }
218 
219  protected:
220  typename LhsXprType::Nested m_lhs_xpr;
221  typename RhsXprType::Nested m_rhs_xpr;
222  const CustomBinaryFunc m_func;
223 };
224 
225 // Eval as rvalue
226 template <typename CustomBinaryFunc, typename LhsXprType, typename RhsXprType, typename Device>
227 struct TensorEvaluator<const TensorCustomBinaryOp<CustomBinaryFunc, LhsXprType, RhsXprType>, Device> {
230  static constexpr int NumDims = internal::traits<XprType>::NumDimensions;
232  typedef typename XprType::Scalar Scalar;
233  typedef std::remove_const_t<typename XprType::CoeffReturnType> CoeffReturnType;
236 
240 
242  enum {
243  IsAligned = false,
245  BlockAccess = false,
246  PreferBlockAccess = false,
247  CoordAccess = false, // to be implemented
248  RawAccess = false
249  };
250 
251  //===- Tensor block evaluation strategy (see TensorBlock.h) -------------===//
253  //===--------------------------------------------------------------------===//
254 
255  EIGEN_STRONG_INLINE TensorEvaluator(const XprType& op, const Device& device)
256  : m_op(op), m_device(device), m_result(NULL) {
257  m_dimensions = op.func().dimensions(op.lhsExpression(), op.rhsExpression());
258  }
259 
260  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Dimensions& dimensions() const { return m_dimensions; }
261 
263  if (data) {
264  evalTo(data);
265  return false;
266  } else {
267  m_result = static_cast<EvaluatorPointerType>(
268  m_device.get((CoeffReturnType*)m_device.allocate_temp(dimensions().TotalSize() * sizeof(CoeffReturnType))));
269  evalTo(m_result);
270  return true;
271  }
272  }
273 
275  if (m_result != NULL) {
276  m_device.deallocate_temp(m_result);
277  m_result = NULL;
278  }
279  }
280 
281  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE CoeffReturnType coeff(Index index) const { return m_result[index]; }
282 
283  template <int LoadMode>
285  return internal::ploadt<PacketReturnType, LoadMode>(m_result + index);
286  }
287 
289  // TODO(rmlarsen): Extend CustomOp API to return its cost estimate.
290  return TensorOpCost(sizeof(CoeffReturnType), 0, 0, vectorized, PacketSize);
291  }
292 
293  EIGEN_DEVICE_FUNC EvaluatorPointerType data() const { return m_result; }
294 
295  protected:
298  m_op.func().eval(m_op.lhsExpression(), m_op.rhsExpression(), result, m_device);
299  }
300 
302  const XprType m_op;
305 };
306 
307 } // end namespace Eigen
308 
309 #endif // EIGEN_CXX11_TENSOR_TENSOR_CUSTOM_OP_H
#define EIGEN_DEVICE_FUNC
Definition: Macros.h:892
#define EIGEN_STRONG_INLINE
Definition: Macros.h:834
#define EIGEN_DEVICE_REF
Definition: TensorMacros.h:34
SCALAR Scalar
Definition: bench_gemm.cpp:45
Generic expression where a coefficient-wise binary operator is applied to two expressions.
Definition: CwiseBinaryOp.h:79
The tensor base class.
Definition: TensorBase.h:1026
Tensor custom class.
Definition: TensorCustomOp.h:195
internal::traits< TensorCustomBinaryOp >::StorageKind StorageKind
Definition: TensorCustomOp.h:201
const CustomBinaryFunc m_func
Definition: TensorCustomOp.h:222
internal::traits< TensorCustomBinaryOp >::CoeffReturnType CoeffReturnType
Definition: TensorCustomOp.h:199
internal::traits< TensorCustomBinaryOp >::Index Index
Definition: TensorCustomOp.h:202
EIGEN_DEVICE_FUNC const internal::remove_all_t< typename RhsXprType::Nested > & rhsExpression() const
Definition: TensorCustomOp.h:215
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorCustomBinaryOp(const LhsXprType &lhs, const RhsXprType &rhs, const CustomBinaryFunc &func)
Definition: TensorCustomOp.h:204
Eigen::NumTraits< Scalar >::Real RealScalar
Definition: TensorCustomOp.h:198
EIGEN_DEVICE_FUNC const internal::remove_all_t< typename LhsXprType::Nested > & lhsExpression() const
Definition: TensorCustomOp.h:211
EIGEN_DEVICE_FUNC const CustomBinaryFunc & func() const
Definition: TensorCustomOp.h:209
LhsXprType::Nested m_lhs_xpr
Definition: TensorCustomOp.h:220
RhsXprType::Nested m_rhs_xpr
Definition: TensorCustomOp.h:221
internal::nested< TensorCustomBinaryOp >::type Nested
Definition: TensorCustomOp.h:200
internal::traits< TensorCustomBinaryOp >::Scalar Scalar
Definition: TensorCustomOp.h:197
Tensor custom class.
Definition: TensorCustomOp.h:51
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorCustomUnaryOp(const XprType &expr, const CustomUnaryFunc &func)
Definition: TensorCustomOp.h:60
internal::traits< TensorCustomUnaryOp >::Index Index
Definition: TensorCustomOp.h:58
EIGEN_DEVICE_FUNC const CustomUnaryFunc & func() const
Definition: TensorCustomOp.h:63
Eigen::NumTraits< Scalar >::Real RealScalar
Definition: TensorCustomOp.h:54
const CustomUnaryFunc m_func
Definition: TensorCustomOp.h:69
XprType::Nested m_expr
Definition: TensorCustomOp.h:68
EIGEN_DEVICE_FUNC const internal::remove_all_t< typename XprType::Nested > & expression() const
Definition: TensorCustomOp.h:65
XprType::CoeffReturnType CoeffReturnType
Definition: TensorCustomOp.h:55
internal::traits< TensorCustomUnaryOp >::StorageKind StorageKind
Definition: TensorCustomOp.h:57
internal::traits< TensorCustomUnaryOp >::Scalar Scalar
Definition: TensorCustomOp.h:53
internal::nested< TensorCustomUnaryOp >::type Nested
Definition: TensorCustomOp.h:56
A tensor expression mapping an existing array of data.
Definition: TensorMap.h:33
Definition: TensorCostModel.h:28
Definition: TensorBlock.h:566
Eigen::DenseIndex ret
Definition: level1_cplx_impl.h:43
char char * op
Definition: level2_impl.h:374
typename remove_all< T >::type remove_all_t
Definition: Meta.h:142
Namespace containing all symbols from the Eigen library.
Definition: bench_norm.cpp:70
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:83
val
Definition: calibrate.py:119
type
Definition: compute_granudrum_aor.py:141
Definition: Eigen_Colamd.h:49
Definition: Constants.h:519
T Real
Definition: NumTraits.h:183
Definition: TensorMeta.h:47
Definition: TensorForwardDeclarations.h:42
StorageMemory< CoeffReturnType, Device > Storage
Definition: TensorCustomOp.h:238
EIGEN_STRONG_INLINE bool evalSubExprsIfNeeded(EvaluatorPointerType data)
Definition: TensorCustomOp.h:262
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorOpCost costPerCoeff(bool vectorized) const
Definition: TensorCustomOp.h:288
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Dimensions & dimensions() const
Definition: TensorCustomOp.h:260
std::remove_const_t< typename XprType::CoeffReturnType > CoeffReturnType
Definition: TensorCustomOp.h:233
Eigen::internal::traits< XprType >::PointerType TensorPointerType
Definition: TensorCustomOp.h:237
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE CoeffReturnType coeff(Index index) const
Definition: TensorCustomOp.h:281
PacketType< CoeffReturnType, Device >::type PacketReturnType
Definition: TensorCustomOp.h:234
EIGEN_DEVICE_FUNC EvaluatorPointerType data() const
Definition: TensorCustomOp.h:293
EIGEN_STRONG_INLINE TensorEvaluator(const XprType &op, const Device &device)
Definition: TensorCustomOp.h:255
internal::TensorBlockNotImplemented TensorBlock
Definition: TensorCustomOp.h:252
TensorCustomBinaryOp< CustomBinaryFunc, LhsXprType, RhsXprType > XprType
Definition: TensorCustomOp.h:228
EIGEN_DEVICE_FUNC PacketReturnType packet(Index index) const
Definition: TensorCustomOp.h:284
void evalTo(EvaluatorPointerType data)
Definition: TensorCustomOp.h:142
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE CoeffReturnType coeff(Index index) const
Definition: TensorCustomOp.h:127
Eigen::internal::traits< XprType >::PointerType TensorPointerType
Definition: TensorCustomOp.h:83
std::remove_const_t< typename ArgType::Scalar > Scalar
Definition: TensorCustomOp.h:79
EIGEN_STRONG_INLINE void cleanup()
Definition: TensorCustomOp.h:120
EIGEN_STRONG_INLINE bool evalSubExprsIfNeeded(EvaluatorPointerType data)
Definition: TensorCustomOp.h:108
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Dimensions & dimensions() const
Definition: TensorCustomOp.h:106
EIGEN_STRONG_INLINE TensorEvaluator(const ArgType &op, const Device &device)
Definition: TensorCustomOp.h:101
StorageMemory< CoeffReturnType, Device > Storage
Definition: TensorCustomOp.h:84
internal::TensorBlockNotImplemented TensorBlock
Definition: TensorCustomOp.h:98
DSizes< Index, NumDims > Dimensions
Definition: TensorCustomOp.h:78
internal::traits< ArgType >::Index Index
Definition: TensorCustomOp.h:76
EIGEN_DEVICE_FUNC EvaluatorPointerType data() const
Definition: TensorCustomOp.h:139
PacketType< CoeffReturnType, Device >::type PacketReturnType
Definition: TensorCustomOp.h:81
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorOpCost costPerCoeff(bool vectorized) const
Definition: TensorCustomOp.h:134
TensorCustomUnaryOp< CustomUnaryFunc, XprType > ArgType
Definition: TensorCustomOp.h:75
EIGEN_DEVICE_FUNC PacketReturnType packet(Index index) const
Definition: TensorCustomOp.h:130
const Device EIGEN_DEVICE_REF m_device
Definition: TensorCustomOp.h:149
std::remove_const_t< typename XprType::CoeffReturnType > CoeffReturnType
Definition: TensorCustomOp.h:80
A cost model used to limit the number of threads used for evaluating tensor expression.
Definition: TensorEvaluator.h:31
static constexpr int Layout
Definition: TensorEvaluator.h:46
const Device EIGEN_DEVICE_REF m_device
Definition: TensorEvaluator.h:170
@ PacketAccess
Definition: TensorEvaluator.h:50
@ IsAligned
Definition: TensorEvaluator.h:49
static constexpr int PacketSize
Definition: TensorEvaluator.h:38
EIGEN_DEVICE_FUNC EvaluatorPointerType data() const
Definition: TensorEvaluator.h:165
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Dimensions & dimensions() const
Definition: TensorEvaluator.h:69
const TensorCustomBinaryOp< CustomBinaryFunc, LhsXprType, RhsXprType > & type
Definition: TensorCustomOp.h:183
const TensorCustomUnaryOp< CustomUnaryFunc, XprType > EIGEN_DEVICE_REF type
Definition: TensorCustomOp.h:40
Definition: XprHelper.h:427
TensorCustomBinaryOp< CustomBinaryFunc, LhsXprType, RhsXprType > type
Definition: TensorCustomOp.h:188
TensorCustomUnaryOp< CustomUnaryFunc, XprType > type
Definition: TensorCustomOp.h:45
Definition: TensorTraits.h:152
ref_selector< T >::type type
Definition: TensorTraits.h:153
Definition: XprHelper.h:145
Definition: XprHelper.h:591
std::conditional_t< Pointer_type_promotion< typename LhsXprType::Scalar, Scalar >::val, typename traits< LhsXprType >::PointerType, typename traits< RhsXprType >::PointerType > PointerType
Definition: TensorCustomOp.h:178
promote_storage_type< typename traits< LhsXprType >::StorageKind, typename traits< RhsXprType >::StorageKind >::ret StorageKind
Definition: TensorCustomOp.h:167
std::remove_reference_t< RhsNested > RhsNested_
Definition: TensorCustomOp.h:173
internal::promote_storage_type< typename LhsXprType::Scalar, typename RhsXprType::Scalar >::ret Scalar
Definition: TensorCustomOp.h:163
std::remove_reference_t< LhsNested > LhsNested_
Definition: TensorCustomOp.h:172
promote_index_type< typename traits< LhsXprType >::Index, typename traits< RhsXprType >::Index >::type Index
Definition: TensorCustomOp.h:169
internal::promote_storage_type< typename LhsXprType::CoeffReturnType, typename RhsXprType::CoeffReturnType >::ret CoeffReturnType
Definition: TensorCustomOp.h:165
traits< XprType >::PointerType PointerType
Definition: TensorCustomOp.h:35
XprType::StorageKind StorageKind
Definition: TensorCustomOp.h:29
std::remove_reference_t< Nested > Nested_
Definition: TensorCustomOp.h:32
Definition: ForwardDeclarations.h:21
Definition: benchGeometry.cpp:21