TR-mbed 1.0
Loading...
Searching...
No Matches
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
13namespace Eigen {
14
22namespace internal {
23template<typename CustomUnaryFunc, typename XprType>
25{
26 typedef typename XprType::Scalar Scalar;
27 typedef typename XprType::StorageKind StorageKind;
28 typedef typename XprType::Index Index;
29 typedef typename XprType::Nested Nested;
31 static const int NumDimensions = traits<XprType>::NumDimensions;
32 static const int Layout = traits<XprType>::Layout;
34};
35
36template<typename CustomUnaryFunc, typename XprType>
41
42template<typename CustomUnaryFunc, typename XprType>
47
48} // end namespace internal
49
50
51
52template<typename CustomUnaryFunc, typename XprType>
53class TensorCustomUnaryOp : public TensorBase<TensorCustomUnaryOp<CustomUnaryFunc, XprType>, ReadOnlyAccessors>
54{
55 public:
58 typedef typename XprType::CoeffReturnType CoeffReturnType;
62
64 : m_expr(expr), m_func(func) {}
65
67 const CustomUnaryFunc& func() const { return m_func; }
68
71 expression() const { return m_expr; }
72
73 protected:
74 typename XprType::Nested m_expr;
75 const CustomUnaryFunc m_func;
76};
77
78
79// Eval as rvalue
80template<typename CustomUnaryFunc, typename XprType, typename Device>
81struct TensorEvaluator<const TensorCustomUnaryOp<CustomUnaryFunc, XprType>, Device>
82{
85 static const int NumDims = internal::traits<ArgType>::NumDimensions;
94
95 enum {
96 IsAligned = false,
98 BlockAccess = false,
101 CoordAccess = false, // to be implemented
102 RawAccess = false
103 };
104
105 //===- Tensor block evaluation strategy (see TensorBlock.h) -------------===//
107 //===--------------------------------------------------------------------===//
108
109 EIGEN_STRONG_INLINE TensorEvaluator(const ArgType& op, const Device& device)
110 : m_op(op), m_device(device), m_result(NULL)
111 {
112 m_dimensions = op.func().dimensions(op.expression());
113 }
114
115 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Dimensions& dimensions() const { return m_dimensions; }
116
118 if (data) {
119 evalTo(data);
120 return false;
121 } else {
122 m_result = static_cast<EvaluatorPointerType>(m_device.get( (CoeffReturnType*)
123 m_device.allocate_temp(dimensions().TotalSize() * sizeof(Scalar))));
124 evalTo(m_result);
125 return true;
126 }
127 }
128
130 if (m_result) {
131 m_device.deallocate_temp(m_result);
132 m_result = NULL;
133 }
134 }
135
137 return m_result[index];
138 }
139
140 template<int LoadMode>
144
146 // TODO(rmlarsen): Extend CustomOp API to return its cost estimate.
147 return TensorOpCost(sizeof(CoeffReturnType), 0, 0, vectorized, PacketSize);
148 }
149
150 EIGEN_DEVICE_FUNC EvaluatorPointerType data() const { return m_result; }
151
152#ifdef EIGEN_USE_SYCL
153 // binding placeholder accessors to a command group handler for SYCL
154 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void bind(cl::sycl::handler &cgh) const {
155 m_result.bind(cgh);
156 }
157#endif
158
159 protected:
162 m_op.func().eval(m_op.expression(), result, m_device);
163 }
164
169};
170
171
172
180namespace internal {
181template<typename CustomBinaryFunc, typename LhsXprType, typename RhsXprType>
183{
184 typedef typename internal::promote_storage_type<typename LhsXprType::Scalar,
185 typename RhsXprType::Scalar>::ret Scalar;
186 typedef typename internal::promote_storage_type<typename LhsXprType::CoeffReturnType,
187 typename RhsXprType::CoeffReturnType>::ret CoeffReturnType;
192 typedef typename LhsXprType::Nested LhsNested;
193 typedef typename RhsXprType::Nested RhsNested;
196 static const int NumDimensions = traits<LhsXprType>::NumDimensions;
197 static const int Layout = traits<LhsXprType>::Layout;
200};
201
202template<typename CustomBinaryFunc, typename LhsXprType, typename RhsXprType>
207
208template<typename CustomBinaryFunc, typename LhsXprType, typename RhsXprType>
213
214} // end namespace internal
215
216
217
218template<typename CustomBinaryFunc, typename LhsXprType, typename RhsXprType>
219class TensorCustomBinaryOp : public TensorBase<TensorCustomBinaryOp<CustomBinaryFunc, LhsXprType, RhsXprType>, ReadOnlyAccessors>
220{
221 public:
228
229 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorCustomBinaryOp(const LhsXprType& lhs, const RhsXprType& rhs, const CustomBinaryFunc& func)
230
231 : m_lhs_xpr(lhs), m_rhs_xpr(rhs), m_func(func) {}
232
234 const CustomBinaryFunc& func() const { return m_func; }
235
238 lhsExpression() const { return m_lhs_xpr; }
239
242 rhsExpression() const { return m_rhs_xpr; }
243
244 protected:
245 typename LhsXprType::Nested m_lhs_xpr;
246 typename RhsXprType::Nested m_rhs_xpr;
247 const CustomBinaryFunc m_func;
248};
249
250
251// Eval as rvalue
252template<typename CustomBinaryFunc, typename LhsXprType, typename RhsXprType, typename Device>
253struct TensorEvaluator<const TensorCustomBinaryOp<CustomBinaryFunc, LhsXprType, RhsXprType>, Device>
254{
259 typedef typename XprType::Scalar Scalar;
263
267
268 enum {
269 IsAligned = false,
271 BlockAccess = false,
274 CoordAccess = false, // to be implemented
275 RawAccess = false
276 };
277
278 //===- Tensor block evaluation strategy (see TensorBlock.h) -------------===//
280 //===--------------------------------------------------------------------===//
281
282 EIGEN_STRONG_INLINE TensorEvaluator(const XprType& op, const Device& device)
283 : m_op(op), m_device(device), m_result(NULL)
284 {
285 m_dimensions = op.func().dimensions(op.lhsExpression(), op.rhsExpression());
286 }
287
288 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Dimensions& dimensions() const { return m_dimensions; }
289
291 if (data) {
292 evalTo(data);
293 return false;
294 } else {
295 m_result = static_cast<EvaluatorPointerType>(m_device.get( (CoeffReturnType*)
296 m_device.allocate_temp(dimensions().TotalSize() * sizeof(CoeffReturnType))));
297 evalTo(m_result);
298 return true;
299 }
300 }
301
303 if (m_result != NULL) {
304 m_device.deallocate_temp(m_result);
305 m_result = NULL;
306 }
307 }
308
310 return m_result[index];
311 }
312
313 template<int LoadMode>
317
319 // TODO(rmlarsen): Extend CustomOp API to return its cost estimate.
320 return TensorOpCost(sizeof(CoeffReturnType), 0, 0, vectorized, PacketSize);
321 }
322
323 EIGEN_DEVICE_FUNC EvaluatorPointerType data() const { return m_result; }
324
325#ifdef EIGEN_USE_SYCL
326 // binding placeholder accessors to a command group handler for SYCL
327 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void bind(cl::sycl::handler &cgh) const {
328 m_result.bind(cgh);
329 }
330#endif
331
332 protected:
335 m_op.func().eval(m_op.lhsExpression(), m_op.rhsExpression(), result, m_device);
336 }
337
342};
343
344
345} // end namespace Eigen
346
347#endif // EIGEN_CXX11_TENSOR_TENSOR_CUSTOM_OP_H
#define EIGEN_DEVICE_FUNC
Definition Macros.h:976
#define EIGEN_STRONG_INLINE
Definition Macros.h:917
#define EIGEN_DEVICE_REF
Definition TensorMacros.h:50
Generic expression where a coefficient-wise binary operator is applied to two expressions.
Definition CwiseBinaryOp.h:84
The tensor base class.
Definition TensorBase.h:973
Tensor custom class.
Definition TensorCustomOp.h:220
internal::traits< TensorCustomBinaryOp >::StorageKind StorageKind
Definition TensorCustomOp.h:226
const CustomBinaryFunc m_func
Definition TensorCustomOp.h:247
internal::traits< TensorCustomBinaryOp >::CoeffReturnType CoeffReturnType
Definition TensorCustomOp.h:224
EIGEN_DEVICE_FUNC const CustomBinaryFunc & func() const
Definition TensorCustomOp.h:234
internal::traits< TensorCustomBinaryOp >::Index Index
Definition TensorCustomOp.h:227
EIGEN_DEVICE_FUNC const internal::remove_all< typenameRhsXprType::Nested >::type & rhsExpression() const
Definition TensorCustomOp.h:242
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorCustomBinaryOp(const LhsXprType &lhs, const RhsXprType &rhs, const CustomBinaryFunc &func)
Definition TensorCustomOp.h:229
Eigen::NumTraits< Scalar >::Real RealScalar
Definition TensorCustomOp.h:223
EIGEN_DEVICE_FUNC const internal::remove_all< typenameLhsXprType::Nested >::type & lhsExpression() const
Definition TensorCustomOp.h:238
LhsXprType::Nested m_lhs_xpr
Definition TensorCustomOp.h:245
RhsXprType::Nested m_rhs_xpr
Definition TensorCustomOp.h:246
internal::nested< TensorCustomBinaryOp >::type Nested
Definition TensorCustomOp.h:225
internal::traits< TensorCustomBinaryOp >::Scalar Scalar
Definition TensorCustomOp.h:222
Tensor custom class.
Definition TensorCustomOp.h:54
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorCustomUnaryOp(const XprType &expr, const CustomUnaryFunc &func)
Definition TensorCustomOp.h:63
EIGEN_DEVICE_FUNC const internal::remove_all< typenameXprType::Nested >::type & expression() const
Definition TensorCustomOp.h:71
internal::traits< TensorCustomUnaryOp >::Index Index
Definition TensorCustomOp.h:61
Eigen::NumTraits< Scalar >::Real RealScalar
Definition TensorCustomOp.h:57
EIGEN_DEVICE_FUNC const CustomUnaryFunc & func() const
Definition TensorCustomOp.h:67
const CustomUnaryFunc m_func
Definition TensorCustomOp.h:75
XprType::Nested m_expr
Definition TensorCustomOp.h:74
XprType::CoeffReturnType CoeffReturnType
Definition TensorCustomOp.h:58
internal::traits< TensorCustomUnaryOp >::StorageKind StorageKind
Definition TensorCustomOp.h:60
internal::traits< TensorCustomUnaryOp >::Scalar Scalar
Definition TensorCustomOp.h:56
internal::nested< TensorCustomUnaryOp >::type Nested
Definition TensorCustomOp.h:59
A tensor expression mapping an existing array of data.
Definition TensorMap.h:30
Definition TensorCostModel.h:25
Definition TensorBlock.h:617
DenseIndex ret
Definition level1_cplx_impl.h:44
Namespace containing all symbols from the Eigen library.
Definition bench_norm.cpp:85
Definition BandTriangularSolver.h:13
Definition TensorDimensions.h:263
Definition Constants.h:507
Definition TensorMeta.h:50
Definition TensorForwardDeclarations.h:37
StorageMemory< CoeffReturnType, Device > Storage
Definition TensorCustomOp.h:265
internal::remove_const< typenameXprType::CoeffReturnType >::type CoeffReturnType
Definition TensorCustomOp.h:260
EIGEN_STRONG_INLINE bool evalSubExprsIfNeeded(EvaluatorPointerType data)
Definition TensorCustomOp.h:290
PacketType< CoeffReturnType, Device >::type PacketReturnType
Definition TensorCustomOp.h:261
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorOpCost costPerCoeff(bool vectorized) const
Definition TensorCustomOp.h:318
Eigen::internal::traits< XprType >::PointerType TensorPointerType
Definition TensorCustomOp.h:264
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE CoeffReturnType coeff(Index index) const
Definition TensorCustomOp.h:309
EIGEN_DEVICE_FUNC EvaluatorPointerType data() const
Definition TensorCustomOp.h:323
EIGEN_STRONG_INLINE TensorEvaluator(const XprType &op, const Device &device)
Definition TensorCustomOp.h:282
internal::TensorBlockNotImplemented TensorBlock
Definition TensorCustomOp.h:279
TensorCustomBinaryOp< CustomBinaryFunc, LhsXprType, RhsXprType > XprType
Definition TensorCustomOp.h:255
EIGEN_DEVICE_FUNC PacketReturnType packet(Index index) const
Definition TensorCustomOp.h:314
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Dimensions & dimensions() const
Definition TensorCustomOp.h:288
void evalTo(EvaluatorPointerType data)
Definition TensorCustomOp.h:160
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE CoeffReturnType coeff(Index index) const
Definition TensorCustomOp.h:136
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Dimensions & dimensions() const
Definition TensorCustomOp.h:115
Eigen::internal::traits< XprType >::PointerType TensorPointerType
Definition TensorCustomOp.h:91
internal::remove_const< typenameXprType::CoeffReturnType >::type CoeffReturnType
Definition TensorCustomOp.h:88
EIGEN_STRONG_INLINE void cleanup()
Definition TensorCustomOp.h:129
internal::remove_const< typenameArgType::Scalar >::type Scalar
Definition TensorCustomOp.h:87
EIGEN_STRONG_INLINE bool evalSubExprsIfNeeded(EvaluatorPointerType data)
Definition TensorCustomOp.h:117
EIGEN_STRONG_INLINE TensorEvaluator(const ArgType &op, const Device &device)
Definition TensorCustomOp.h:109
StorageMemory< CoeffReturnType, Device > Storage
Definition TensorCustomOp.h:92
PacketType< CoeffReturnType, Device >::type PacketReturnType
Definition TensorCustomOp.h:89
internal::TensorBlockNotImplemented TensorBlock
Definition TensorCustomOp.h:106
internal::traits< ArgType >::Index Index
Definition TensorCustomOp.h:84
EIGEN_DEVICE_FUNC EvaluatorPointerType data() const
Definition TensorCustomOp.h:150
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorOpCost costPerCoeff(bool vectorized) const
Definition TensorCustomOp.h:145
TensorCustomUnaryOp< CustomUnaryFunc, XprType > ArgType
Definition TensorCustomOp.h:83
EIGEN_DEVICE_FUNC PacketReturnType packet(Index index) const
Definition TensorCustomOp.h:141
const Device EIGEN_DEVICE_REF m_device
Definition TensorCustomOp.h:167
A cost model used to limit the number of threads used for evaluating tensor expression.
Definition TensorEvaluator.h:29
const Device EIGEN_DEVICE_REF m_device
Definition TensorEvaluator.h:192
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Dimensions & dimensions() const
Definition TensorEvaluator.h:73
EIGEN_DEVICE_FUNC EvaluatorPointerType data() const
Definition TensorEvaluator.h:181
@ BlockAccess
Definition TensorEvaluator.h:48
@ PreferBlockAccess
Definition TensorEvaluator.h:49
@ PacketAccess
Definition TensorEvaluator.h:47
@ Layout
Definition TensorEvaluator.h:50
@ IsAligned
Definition TensorEvaluator.h:46
static const int PacketSize
Definition TensorEvaluator.h:36
const TensorCustomBinaryOp< CustomBinaryFunc, LhsXprType, RhsXprType > & type
Definition TensorCustomOp.h:205
const TensorCustomUnaryOp< CustomUnaryFunc, XprType > EIGEN_DEVICE_REF type
Definition TensorCustomOp.h:39
Definition XprHelper.h:332
TensorCustomBinaryOp< CustomBinaryFunc, LhsXprType, RhsXprType > type
Definition TensorCustomOp.h:211
TensorCustomUnaryOp< CustomUnaryFunc, XprType > type
Definition TensorCustomOp.h:45
Definition TensorTraits.h:175
Definition XprHelper.h:518
promote_storage_type< typenametraits< LhsXprType >::StorageKind, typenametraits< RhsXprType >::StorageKind >::ret StorageKind
Definition TensorCustomOp.h:189
conditional< Pointer_type_promotion< typenameLhsXprType::Scalar, Scalar >::val, typenametraits< LhsXprType >::PointerType, typenametraits< RhsXprType >::PointerType >::type PointerType
Definition TensorCustomOp.h:199
internal::promote_storage_type< typenameLhsXprType::Scalar, typenameRhsXprType::Scalar >::ret Scalar
Definition TensorCustomOp.h:185
remove_reference< LhsNested >::type _LhsNested
Definition TensorCustomOp.h:194
promote_index_type< typenametraits< LhsXprType >::Index, typenametraits< RhsXprType >::Index >::type Index
Definition TensorCustomOp.h:191
remove_reference< RhsNested >::type _RhsNested
Definition TensorCustomOp.h:195
internal::promote_storage_type< typenameLhsXprType::CoeffReturnType, typenameRhsXprType::CoeffReturnType >::ret CoeffReturnType
Definition TensorCustomOp.h:187
traits< XprType >::PointerType PointerType
Definition TensorCustomOp.h:33
remove_reference< Nested >::type _Nested
Definition TensorCustomOp.h:30
XprType::StorageKind StorageKind
Definition TensorCustomOp.h:27
Definition ForwardDeclarations.h:17
Definition benchGeometry.cpp:23