TensorEvalTo.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_EVAL_TO_H
11 #define EIGEN_CXX11_TENSOR_TENSOR_EVAL_TO_H
12 
13 namespace Eigen {
14 
22 namespace internal {
23 template<typename XprType, template <class> class MakePointer_>
24 struct traits<TensorEvalToOp<XprType, MakePointer_> >
25 {
26  // Type promotion to handle the case where the types of the lhs and the rhs are different.
27  typedef typename XprType::Scalar Scalar;
29  typedef typename XprTraits::StorageKind StorageKind;
30  typedef typename XprTraits::Index Index;
31  typedef typename XprType::Nested Nested;
33  static const int NumDimensions = XprTraits::NumDimensions;
34  static const int Layout = XprTraits::Layout;
36 
37  enum {
38  Flags = 0
39  };
40  template <class T>
41  struct MakePointer {
42  // Intermediate typedef to workaround MSVC issue.
43  typedef MakePointer_<T> MakePointerT;
44  typedef typename MakePointerT::Type Type;
45 
46 
47  };
48 };
49 
50 template<typename XprType, template <class> class MakePointer_>
51 struct eval<TensorEvalToOp<XprType, MakePointer_>, Eigen::Dense>
52 {
54 };
55 
56 template<typename XprType, template <class> class MakePointer_>
57 struct nested<TensorEvalToOp<XprType, MakePointer_>, 1, typename eval<TensorEvalToOp<XprType, MakePointer_> >::type>
58 {
60 };
61 
62 } // end namespace internal
63 
64 
65 
66 
67 template<typename XprType, template <class> class MakePointer_>
68 class TensorEvalToOp : public TensorBase<TensorEvalToOp<XprType, MakePointer_>, ReadOnlyAccessors>
69 {
70  public:
78 
80 
82  : m_xpr(expr), m_buffer(buffer) {}
83 
86  expression() const { return m_xpr; }
87 
88  EIGEN_DEVICE_FUNC PointerType buffer() const { return m_buffer; }
89 
90  protected:
91  typename XprType::Nested m_xpr;
92  PointerType m_buffer;
93 };
94 
95 
96 
97 template<typename ArgType, typename Device, template <class> class MakePointer_>
98 struct TensorEvaluator<const TensorEvalToOp<ArgType, MakePointer_>, Device>
99 {
101  typedef typename ArgType::Scalar Scalar;
103  typedef typename XprType::Index Index;
106  static const int PacketSize = PacketType<CoeffReturnType, Device>::size;
110  enum {
113  BlockAccess = true,
114  PreferBlockAccess = false,
116  CoordAccess = false, // to be implemented
117  RawAccess = true
118  };
119 
120  static const int NumDims = internal::traits<ArgType>::NumDimensions;
121 
122  //===- Tensor block evaluation strategy (see TensorBlock.h) -------------===//
125 
128 
130  CoeffReturnType, NumDims, typename ArgTensorBlock::XprType, Index>
132  //===--------------------------------------------------------------------===//
133 
134  EIGEN_STRONG_INLINE TensorEvaluator(const XprType& op, const Device& device)
135  : m_impl(op.expression(), device), m_buffer(device.get(op.buffer())), m_expression(op.expression()){}
136 
137 
139  }
140 
141 
142  EIGEN_DEVICE_FUNC const Dimensions& dimensions() const { return m_impl.dimensions(); }
143 
144  EIGEN_STRONG_INLINE bool evalSubExprsIfNeeded(EvaluatorPointerType scalar) {
145  EIGEN_UNUSED_VARIABLE(scalar);
146  eigen_assert(scalar == NULL);
147  return m_impl.evalSubExprsIfNeeded(m_buffer);
148  }
149 
150 #ifdef EIGEN_USE_THREADS
151  template <typename EvalSubExprsCallback>
152  EIGEN_STRONG_INLINE void evalSubExprsIfNeededAsync(
153  EvaluatorPointerType scalar, EvalSubExprsCallback done) {
154  EIGEN_UNUSED_VARIABLE(scalar);
155  eigen_assert(scalar == NULL);
156  m_impl.evalSubExprsIfNeededAsync(m_buffer, std::move(done));
157  }
158 #endif
159 
161  m_buffer[i] = m_impl.coeff(i);
162  }
164  internal::pstoret<CoeffReturnType, PacketReturnType, Aligned>(m_buffer + i, m_impl.template packet<TensorEvaluator<ArgType, Device>::IsAligned ? Aligned : Unaligned>(i));
165  }
166 
169  return m_impl.getResourceRequirements();
170  }
171 
173  TensorBlockDesc& desc, TensorBlockScratch& scratch) {
174  // Add `m_buffer` as destination buffer to the block descriptor.
175  desc.template AddDestinationBuffer<Layout>(
176  /*dst_base=*/m_buffer + desc.offset(),
177  /*dst_strides=*/internal::strides<Layout>(m_impl.dimensions()));
178 
180  m_impl.block(desc, scratch, /*root_of_expr_ast=*/true);
181 
182  // If block was evaluated into a destination buffer, there is no need to do
183  // an assignment.
187  desc.dimensions(), internal::strides<Layout>(m_impl.dimensions()),
188  m_buffer, desc.offset()),
189  block.expr());
190  }
191  block.cleanup();
192  }
193 
195  m_impl.cleanup();
196  }
197 
198  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE CoeffReturnType coeff(Index index) const
199  {
200  return m_buffer[index];
201  }
202 
203  template<int LoadMode>
204  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE PacketReturnType packet(Index index) const
205  {
206  return internal::ploadt<PacketReturnType, LoadMode>(m_buffer + index);
207  }
208 
210  // We assume that evalPacket or evalScalar is called to perform the
211  // assignment and account for the cost of the write here.
212  return m_impl.costPerCoeff(vectorized) +
213  TensorOpCost(0, sizeof(CoeffReturnType), 0, vectorized, PacketSize);
214  }
215 
216  EIGEN_DEVICE_FUNC EvaluatorPointerType data() const { return m_buffer; }
217  ArgType expression() const { return m_expression; }
218  #ifdef EIGEN_USE_SYCL
219  // binding placeholder accessors to a command group handler for SYCL
220  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void bind(cl::sycl::handler &cgh) const {
221  m_impl.bind(cgh);
222  m_buffer.bind(cgh);
223  }
224  #endif
225 
226 
227  private:
229  EvaluatorPointerType m_buffer;
230  const ArgType m_expression;
231 };
232 
233 
234 } // end namespace Eigen
235 
236 #endif // EIGEN_CXX11_TENSOR_TENSOR_EVAL_TO_H
SCALAR Scalar
Definition: bench_gemm.cpp:46
EIGEN_STRONG_INLINE TensorEvaluator(const XprType &op, const Device &device)
Definition: TensorEvalTo.h:134
#define EIGEN_STRONG_INLINE
Definition: Macros.h:917
m m block(1, 0, 2, 2)<< 4
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE internal::TensorBlockResourceRequirements getResourceRequirements() const
Definition: TensorEvalTo.h:168
EIGEN_DEVICE_FUNC const internal::remove_all< typename XprType::Nested >::type & expression() const
Definition: TensorEvalTo.h:86
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void evalPacket(Index i)
Definition: TensorEvalTo.h:163
Namespace containing all symbols from the Eigen library.
Definition: jet.h:637
A cost model used to limit the number of threads used for evaluating tensor expression.
EIGEN_DEVICE_FUNC PointerType buffer() const
Definition: TensorEvalTo.h:88
Eigen::internal::traits< TensorEvalToOp >::Scalar Scalar
Definition: TensorEvalTo.h:71
static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void Run(const Target &target, const TensorBlockExpr &expr)
Definition: TensorBlock.h:1465
Generic expression where a coefficient-wise binary operator is applied to two expressions.
Definition: CwiseBinaryOp.h:77
Eigen::internal::nested< TensorEvalToOp >::type Nested
Definition: TensorEvalTo.h:75
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorEvalToOp(PointerType buffer, const XprType &expr)
Definition: TensorEvalTo.h:81
Eigen::internal::traits< TensorEvalToOp >::Index Index
Definition: TensorEvalTo.h:77
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorOpCost costPerCoeff(bool vectorized) const
Definition: TensorEvalTo.h:209
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:74
static Target target(const Dimensions &target_dims, const Dimensions &target_strides, Scalar *target_data, IndexType target_offset=0)
Definition: TensorBlock.h:1449
#define eigen_assert(x)
Definition: Macros.h:1037
MakePointer_< CoeffReturnType >::Type PointerType
Definition: TensorEvalTo.h:74
internal::TensorBlockAssignment< CoeffReturnType, NumDims, typename ArgTensorBlock::XprType, Index > TensorBlockAssignment
Definition: TensorEvalTo.h:131
#define NULL
Definition: ccolamd.c:609
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE PacketReturnType packet(Index index) const
Definition: TensorEvalTo.h:204
PointerType m_buffer
Definition: TensorEvalTo.h:92
The tensor base class.
Definition: TensorBase.h:973
#define EIGEN_DEVICE_FUNC
Definition: Macros.h:976
internal::remove_const< typename XprType::CoeffReturnType >::type CoeffReturnType
Definition: TensorEvalTo.h:73
CwiseBinaryOp< internal::scalar_sum_op< double, double >, const CpyMatrixXd, const CpyMatrixXd > XprType
Definition: nestbyvalue.cpp:15
const Dimensions & dimensions() const
Definition: TensorBlock.h:299
Eigen::internal::traits< TensorEvalToOp >::StorageKind StorageKind
Definition: TensorEvalTo.h:76
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void evalBlock(TensorBlockDesc &desc, TensorBlockScratch &scratch)
Definition: TensorEvalTo.h:172
Eigen::NumTraits< Scalar >::Real RealScalar
Definition: TensorEvalTo.h:72
EIGEN_STRONG_INLINE bool evalSubExprsIfNeeded(EvaluatorPointerType scalar)
Definition: TensorEvalTo.h:144
TensorEvaluator< const ArgType, Device >::TensorBlock ArgTensorBlock
Definition: TensorEvalTo.h:127
Generic expression where a coefficient-wise unary operator is applied to an expression.
Definition: CwiseUnaryOp.h:55
Container::iterator get(Container &c, Position position)
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void evalScalar(Index i)
Definition: TensorEvalTo.h:160
mxArray * scalar(mxClassID classid)
Definition: matlab.h:82
internal::remove_const< typename XprType::CoeffReturnType >::type CoeffReturnType
Definition: TensorEvalTo.h:104
#define EIGEN_UNUSED_VARIABLE(var)
Definition: Macros.h:1076
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE CoeffReturnType coeff(Index index) const
Definition: TensorEvalTo.h:198
internal::TensorBlockDescriptor< NumDims, Index > TensorBlockDesc
Definition: TensorEvalTo.h:123
internal::TensorBlockScratchAllocator< Device > TensorBlockScratch
Definition: TensorEvalTo.h:124
XprType::Nested m_xpr
Definition: TensorEvalTo.h:91


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