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 namespace Eigen {
14 
22 namespace internal {
23 template<typename CustomUnaryFunc, typename XprType>
24 struct traits<TensorCustomUnaryOp<CustomUnaryFunc, 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 
36 template<typename CustomUnaryFunc, typename XprType>
37 struct eval<TensorCustomUnaryOp<CustomUnaryFunc, XprType>, Eigen::Dense>
38 {
40 };
41 
42 template<typename CustomUnaryFunc, typename XprType>
43 struct nested<TensorCustomUnaryOp<CustomUnaryFunc, XprType> >
44 {
46 };
47 
48 } // end namespace internal
49 
50 
51 
52 template<typename CustomUnaryFunc, typename XprType>
53 class 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
80 template<typename CustomUnaryFunc, typename XprType, typename Device>
81 struct TensorEvaluator<const TensorCustomUnaryOp<CustomUnaryFunc, XprType>, Device>
82 {
85  static const int NumDims = internal::traits<ArgType>::NumDimensions;
90  static const int PacketSize = PacketType<CoeffReturnType, Device>::size;
94 
95  enum {
96  IsAligned = false,
98  BlockAccess = false,
99  PreferBlockAccess = 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 
117  EIGEN_STRONG_INLINE bool evalSubExprsIfNeeded(EvaluatorPointerType data) {
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 
136  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE CoeffReturnType coeff(Index index) const {
137  return m_result[index];
138  }
139 
140  template<int LoadMode>
141  EIGEN_DEVICE_FUNC PacketReturnType packet(Index index) const {
142  return internal::ploadt<PacketReturnType, LoadMode>(m_result + index);
143  }
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:
160  void evalTo(EvaluatorPointerType data) {
161  TensorMap<Tensor<CoeffReturnType, NumDims, Layout, Index> > result(m_device.get(data), m_dimensions);
162  m_op.func().eval(m_op.expression(), result, m_device);
163  }
164 
165  Dimensions m_dimensions;
166  const ArgType m_op;
168  EvaluatorPointerType m_result;
169 };
170 
171 
172 
180 namespace internal {
181 template<typename CustomBinaryFunc, typename LhsXprType, typename RhsXprType>
182 struct traits<TensorCustomBinaryOp<CustomBinaryFunc, LhsXprType, RhsXprType> >
183 {
184  typedef typename internal::promote_storage_type<typename LhsXprType::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 
202 template<typename CustomBinaryFunc, typename LhsXprType, typename RhsXprType>
203 struct eval<TensorCustomBinaryOp<CustomBinaryFunc, LhsXprType, RhsXprType>, Eigen::Dense>
204 {
206 };
207 
208 template<typename CustomBinaryFunc, typename LhsXprType, typename RhsXprType>
209 struct nested<TensorCustomBinaryOp<CustomBinaryFunc, LhsXprType, RhsXprType> >
210 {
212 };
213 
214 } // end namespace internal
215 
216 
217 
218 template<typename CustomBinaryFunc, typename LhsXprType, typename RhsXprType>
219 class 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
252 template<typename CustomBinaryFunc, typename LhsXprType, typename RhsXprType, typename Device>
253 struct TensorEvaluator<const TensorCustomBinaryOp<CustomBinaryFunc, LhsXprType, RhsXprType>, Device>
254 {
257  static const int NumDims = internal::traits<XprType>::NumDimensions;
259  typedef typename XprType::Scalar Scalar;
262  static const int PacketSize = PacketType<CoeffReturnType, Device>::size;
263 
267 
268  enum {
269  IsAligned = false,
271  BlockAccess = false,
272  PreferBlockAccess = 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 
290  EIGEN_STRONG_INLINE bool evalSubExprsIfNeeded(EvaluatorPointerType data) {
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 
309  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE CoeffReturnType coeff(Index index) const {
310  return m_result[index];
311  }
312 
313  template<int LoadMode>
314  EIGEN_DEVICE_FUNC PacketReturnType packet(Index index) const {
315  return internal::ploadt<PacketReturnType, LoadMode>(m_result + index);
316  }
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:
333  void evalTo(EvaluatorPointerType data) {
334  TensorMap<Tensor<CoeffReturnType, NumDims, Layout> > result(m_device.get(data), m_dimensions);
335  m_op.func().eval(m_op.lhsExpression(), m_op.rhsExpression(), result, m_device);
336  }
337 
338  Dimensions m_dimensions;
339  const XprType m_op;
341  EvaluatorPointerType m_result;
342 };
343 
344 
345 } // end namespace Eigen
346 
347 #endif // EIGEN_CXX11_TENSOR_TENSOR_CUSTOM_OP_H
EIGEN_DEVICE_FUNC const internal::remove_all< typename LhsXprType::Nested >::type & lhsExpression() const
SCALAR Scalar
Definition: bench_gemm.cpp:46
Tensor custom class.
#define EIGEN_STRONG_INLINE
Definition: Macros.h:917
EIGEN_DEVICE_FUNC const CustomUnaryFunc & func() const
Tensor custom class.
internal::traits< TensorCustomBinaryOp >::StorageKind StorageKind
internal::nested< TensorCustomBinaryOp >::type Nested
internal::traits< TensorCustomBinaryOp >::Scalar Scalar
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorCustomUnaryOp(const XprType &expr, const CustomUnaryFunc &func)
const TensorCustomUnaryOp< CustomUnaryFunc, XprType > EIGEN_DEVICE_REF type
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorOpCost costPerCoeff(bool vectorized) const
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorCustomBinaryOp(const LhsXprType &lhs, const RhsXprType &rhs, const CustomBinaryFunc &func)
Namespace containing all symbols from the Eigen library.
Definition: jet.h:637
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Dimensions & dimensions() const
A cost model used to limit the number of threads used for evaluating tensor expression.
EIGEN_DEVICE_FUNC const internal::remove_all< typename RhsXprType::Nested >::type & rhsExpression() const
RhsXprType::Nested m_rhs_xpr
XprType::CoeffReturnType CoeffReturnType
internal::traits< TensorCustomBinaryOp >::CoeffReturnType CoeffReturnType
const CustomUnaryFunc m_func
Generic expression where a coefficient-wise binary operator is applied to two expressions.
Definition: CwiseBinaryOp.h:77
TensorCustomBinaryOp< CustomBinaryFunc, LhsXprType, RhsXprType > type
Values result
A tensor expression mapping an existing array of data.
EIGEN_DEVICE_FUNC const CustomBinaryFunc & func() const
const CustomBinaryFunc m_func
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorOpCost costPerCoeff(bool vectorized) const
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:74
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE CoeffReturnType coeff(Index index) const
int data[]
internal::traits< TensorCustomBinaryOp >::Index Index
EIGEN_DEVICE_FUNC const internal::remove_all< typename XprType::Nested >::type & expression() const
Eigen::NumTraits< Scalar >::Real RealScalar
#define NULL
Definition: ccolamd.c:609
EIGEN_STRONG_INLINE TensorEvaluator(const ArgType &op, const Device &device)
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE CoeffReturnType coeff(Index index) const
DenseIndex ret
internal::promote_storage_type< typename LhsXprType::CoeffReturnType, typename RhsXprType::CoeffReturnType >::ret CoeffReturnType
The tensor base class.
Definition: TensorBase.h:973
LhsXprType::Nested m_lhs_xpr
#define EIGEN_DEVICE_FUNC
Definition: Macros.h:976
internal::promote_storage_type< typename LhsXprType::Scalar, typename RhsXprType::Scalar >::ret Scalar
const TensorCustomBinaryOp< CustomBinaryFunc, LhsXprType, RhsXprType > & type
internal::nested< TensorCustomUnaryOp >::type Nested
#define EIGEN_DEVICE_REF
Definition: TensorMacros.h:50
internal::traits< TensorCustomUnaryOp >::StorageKind StorageKind
Generic expression where a coefficient-wise unary operator is applied to an expression.
Definition: CwiseUnaryOp.h:55
Eigen::NumTraits< Scalar >::Real RealScalar
conditional< Pointer_type_promotion< typename LhsXprType::Scalar, Scalar >::val, typename traits< LhsXprType >::PointerType, typename traits< RhsXprType >::PointerType >::type PointerType
internal::remove_const< typename XprType::CoeffReturnType >::type CoeffReturnType
const std::vector< size_t > dimensions
internal::traits< TensorCustomUnaryOp >::Scalar Scalar
promote_index_type< typename traits< LhsXprType >::Index, typename traits< RhsXprType >::Index >::type Index
promote_storage_type< typename traits< LhsXprType >::StorageKind, typename traits< RhsXprType >::StorageKind >::ret StorageKind
Definition: pytypes.h:1370
internal::traits< TensorCustomUnaryOp >::Index Index


gtsam
Author(s):
autogenerated on Tue Jul 4 2023 02:36:51