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 #include "./InternalHeaderCheck.h"
14 
15 namespace Eigen {
16 
24 namespace internal {
25 template<typename XprType, template <class> class MakePointer_>
26 struct traits<TensorEvalToOp<XprType, MakePointer_> >
27 {
28  // Type promotion to handle the case where the types of the lhs and the rhs are different.
29  typedef typename XprType::Scalar Scalar;
30  typedef traits<XprType> XprTraits;
31  typedef typename XprTraits::StorageKind StorageKind;
32  typedef typename XprTraits::Index Index;
33  typedef typename XprType::Nested Nested;
34  typedef std::remove_reference_t<Nested> Nested_;
35  static constexpr int NumDimensions = XprTraits::NumDimensions;
36  static constexpr int Layout = XprTraits::Layout;
37  typedef typename MakePointer_<Scalar>::Type PointerType;
38 
39  enum {
40  Flags = 0
41  };
42  template <class T>
43  struct MakePointer {
44  // Intermediate typedef to workaround MSVC issue.
45  typedef MakePointer_<T> MakePointerT;
46  typedef typename MakePointerT::Type Type;
47 
48 
49  };
50 };
51 
52 template<typename XprType, template <class> class MakePointer_>
53 struct eval<TensorEvalToOp<XprType, MakePointer_>, Eigen::Dense>
54 {
55  typedef const TensorEvalToOp<XprType, MakePointer_>& type;
56 };
57 
58 template<typename XprType, template <class> class MakePointer_>
59 struct nested<TensorEvalToOp<XprType, MakePointer_>, 1, typename eval<TensorEvalToOp<XprType, MakePointer_> >::type>
60 {
61  typedef TensorEvalToOp<XprType, MakePointer_> type;
62 };
63 
64 } // end namespace internal
65 
66 
67 
68 
69 template<typename XprType, template <class> class MakePointer_>
70 class TensorEvalToOp : public TensorBase<TensorEvalToOp<XprType, MakePointer_>, ReadOnlyAccessors>
71 {
72  public:
73  typedef typename Eigen::internal::traits<TensorEvalToOp>::Scalar Scalar;
75  typedef std::remove_const_t<typename XprType::CoeffReturnType> CoeffReturnType;
76  typedef typename MakePointer_<CoeffReturnType>::Type PointerType;
77  typedef typename Eigen::internal::nested<TensorEvalToOp>::type Nested;
78  typedef typename Eigen::internal::traits<TensorEvalToOp>::StorageKind StorageKind;
79  typedef typename Eigen::internal::traits<TensorEvalToOp>::Index Index;
80 
81  static constexpr int NumDims = Eigen::internal::traits<TensorEvalToOp>::NumDimensions;
82 
83  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorEvalToOp(PointerType buffer, const XprType& expr)
84  : m_xpr(expr), m_buffer(buffer) {}
85 
88  expression() const { return m_xpr; }
89 
91 
92  protected:
93  typename XprType::Nested m_xpr;
95 };
96 
97 
98 
99 template<typename ArgType, typename Device, template <class> class MakePointer_>
100 struct TensorEvaluator<const TensorEvalToOp<ArgType, MakePointer_>, Device>
101 {
103  typedef typename ArgType::Scalar Scalar;
105  typedef typename XprType::Index Index;
106  typedef std::remove_const_t<typename XprType::CoeffReturnType> CoeffReturnType;
109  typedef typename Eigen::internal::traits<XprType>::PointerType TensorPointerType;
112  enum {
115  BlockAccess = true,
116  PreferBlockAccess = false,
117  CoordAccess = false, // to be implemented
118  RawAccess = true
119  };
120 
122  static constexpr int NumDims = internal::traits<ArgType>::NumDimensions;
123 
124  //===- Tensor block evaluation strategy (see TensorBlock.h) -------------===//
125  typedef internal::TensorBlockDescriptor<NumDims, Index> TensorBlockDesc;
126  typedef internal::TensorBlockScratchAllocator<Device> TensorBlockScratch;
127 
130 
131  typedef internal::TensorBlockAssignment<
132  CoeffReturnType, NumDims, typename ArgTensorBlock::XprType, Index>
134  //===--------------------------------------------------------------------===//
135 
136  EIGEN_STRONG_INLINE TensorEvaluator(const XprType& op, const Device& device)
137  : m_impl(op.expression(), device), m_buffer(device.get(op.buffer())), m_expression(op.expression()){}
138 
139 
140  EIGEN_STRONG_INLINE ~TensorEvaluator() {
141  }
142 
143 
144  EIGEN_DEVICE_FUNC const Dimensions& dimensions() const { return m_impl.dimensions(); }
145 
146  EIGEN_STRONG_INLINE bool evalSubExprsIfNeeded(EvaluatorPointerType scalar) {
147  EIGEN_UNUSED_VARIABLE(scalar);
148  eigen_assert(scalar == NULL);
149  return m_impl.evalSubExprsIfNeeded(m_buffer);
150  }
151 
152 #ifdef EIGEN_USE_THREADS
153  template <typename EvalSubExprsCallback>
154  EIGEN_STRONG_INLINE void evalSubExprsIfNeededAsync(
155  EvaluatorPointerType scalar, EvalSubExprsCallback done) {
156  EIGEN_UNUSED_VARIABLE(scalar);
157  eigen_assert(scalar == NULL);
158  m_impl.evalSubExprsIfNeededAsync(m_buffer, std::move(done));
159  }
160 #endif
161 
162  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void evalScalar(Index i) const {
163  m_buffer[i] = m_impl.coeff(i);
164  }
165  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void evalPacket(Index i) const {
166  internal::pstoret<CoeffReturnType, PacketReturnType, Aligned>(m_buffer + i, m_impl.template packet<TensorEvaluator<ArgType, Device>::IsAligned ? Aligned : Unaligned>(i));
167  }
168 
169  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
170  internal::TensorBlockResourceRequirements getResourceRequirements() const {
171  return m_impl.getResourceRequirements();
172  }
173 
174  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void evalBlock(
175  TensorBlockDesc& desc, TensorBlockScratch& scratch) {
176  // Add `m_buffer` as destination buffer to the block descriptor.
177  desc.template AddDestinationBuffer<Layout>(
178  /*dst_base=*/m_buffer + desc.offset(),
179  /*dst_strides=*/internal::strides<Layout>(m_impl.dimensions()));
180 
182  m_impl.block(desc, scratch, /*root_of_expr_ast=*/true);
183 
184  // If block was evaluated into a destination buffer, there is no need to do
185  // an assignment.
187  TensorBlockAssignment::Run(
188  TensorBlockAssignment::target(
189  desc.dimensions(), internal::strides<Layout>(m_impl.dimensions()),
190  m_buffer, desc.offset()),
191  block.expr());
192  }
193  block.cleanup();
194  }
195 
196  EIGEN_STRONG_INLINE void cleanup() {
197  m_impl.cleanup();
198  }
199 
200  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE CoeffReturnType coeff(Index index) const
201  {
202  return m_buffer[index];
203  }
204 
205  template<int LoadMode>
206  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE PacketReturnType packet(Index index) const
207  {
208  return internal::ploadt<PacketReturnType, LoadMode>(m_buffer + index);
209  }
210 
211  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorOpCost costPerCoeff(bool vectorized) const {
212  // We assume that evalPacket or evalScalar is called to perform the
213  // assignment and account for the cost of the write here.
214  return m_impl.costPerCoeff(vectorized) +
215  TensorOpCost(0, sizeof(CoeffReturnType), 0, vectorized, PacketSize);
216  }
217 
218  EIGEN_DEVICE_FUNC EvaluatorPointerType data() const { return m_buffer; }
219  ArgType expression() const { return m_expression; }
220 
221 
222 
223 
224  private:
227  const ArgType m_expression;
228 };
229 
230 
231 } // end namespace Eigen
232 
233 #endif // EIGEN_CXX11_TENSOR_TENSOR_EVAL_TO_H
int i
#define EIGEN_UNUSED_VARIABLE(var)
#define EIGEN_DEVICE_FUNC
#define eigen_assert(x)
The tensor base class.
XprType::Nested m_xpr
Definition: TensorEvalTo.h:93
Eigen::internal::traits< TensorEvalToOp >::StorageKind StorageKind
Definition: TensorEvalTo.h:78
MakePointer_< CoeffReturnType >::Type PointerType
Definition: TensorEvalTo.h:76
TensorEvalToOp(PointerType buffer, const XprType &expr)
Definition: TensorEvalTo.h:83
Eigen::NumTraits< Scalar >::Real RealScalar
Definition: TensorEvalTo.h:74
Eigen::internal::traits< TensorEvalToOp >::Index Index
Definition: TensorEvalTo.h:79
PointerType m_buffer
Definition: TensorEvalTo.h:94
const internal::remove_all_t< typename XprType::Nested > & expression() const
Definition: TensorEvalTo.h:88
PointerType buffer() const
Definition: TensorEvalTo.h:90
static constexpr int NumDims
Definition: TensorEvalTo.h:81
Eigen::internal::nested< TensorEvalToOp >::type Nested
Definition: TensorEvalTo.h:77
Eigen::internal::traits< TensorEvalToOp >::Scalar Scalar
Definition: TensorEvalTo.h:73
std::remove_const_t< typename XprType::CoeffReturnType > CoeffReturnType
Definition: TensorEvalTo.h:75
A tensor expression mapping an existing array of data.
Definition: TensorMap.h:32
Type
typename remove_all< T >::type remove_all_t
: TensorContractionSycl.h, provides various tensor contraction kernel for SYCL backend
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
internal::packet_traits< Scalar >::type type
Definition: TensorMeta.h:55
void evalBlock(TensorBlockDesc &desc, TensorBlockScratch &scratch)
Definition: TensorEvalTo.h:174
TensorEvaluator< const ArgType, Device >::TensorBlock ArgTensorBlock
Definition: TensorEvalTo.h:129
internal::TensorBlockResourceRequirements getResourceRequirements() const
Definition: TensorEvalTo.h:170
internal::TensorBlockScratchAllocator< Device > TensorBlockScratch
Definition: TensorEvalTo.h:126
std::remove_const_t< typename XprType::CoeffReturnType > CoeffReturnType
Definition: TensorEvalTo.h:106
internal::TensorBlockDescriptor< NumDims, Index > TensorBlockDesc
Definition: TensorEvalTo.h:125
internal::TensorBlockAssignment< CoeffReturnType, NumDims, typename ArgTensorBlock::XprType, Index > TensorBlockAssignment
Definition: TensorEvalTo.h:133
A cost model used to limit the number of threads used for evaluating tensor expression.
PacketReturnType packet(Index index) const
static constexpr int Layout
TensorBlock block(TensorBlockDesc &desc, TensorBlockScratch &scratch, bool=false) const
Storage::Type EvaluatorPointerType
static constexpr int PacketSize
Derived::Scalar CoeffReturnType
internal::TensorMaterializedBlock< ScalarNoConst, NumCoords, Layout, Index > TensorBlock