TensorAssign.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_ASSIGN_H
11 #define EIGEN_CXX11_TENSOR_TENSOR_ASSIGN_H
12 
13 #include "./InternalHeaderCheck.h"
14 
15 namespace Eigen {
16 
25 namespace internal {
26 template<typename LhsXprType, typename RhsXprType>
27 struct traits<TensorAssignOp<LhsXprType, RhsXprType> >
28 {
29  typedef typename LhsXprType::Scalar Scalar;
30  typedef typename traits<LhsXprType>::StorageKind StorageKind;
31  typedef typename promote_index_type<typename traits<LhsXprType>::Index,
32  typename traits<RhsXprType>::Index>::type Index;
33  typedef typename LhsXprType::Nested LhsNested;
34  typedef typename RhsXprType::Nested RhsNested;
35  typedef std::remove_reference_t<LhsNested> LhsNested_;
36  typedef std::remove_reference_t<RhsNested> RhsNested_;
37  static constexpr std::size_t NumDimensions = internal::traits<LhsXprType>::NumDimensions;
38  static constexpr int Layout = internal::traits<LhsXprType>::Layout;
39  typedef typename traits<LhsXprType>::PointerType PointerType;
40 
41  enum {
42  Flags = 0
43  };
44 };
45 
46 template<typename LhsXprType, typename RhsXprType>
47 struct eval<TensorAssignOp<LhsXprType, RhsXprType>, Eigen::Dense>
48 {
49  typedef const TensorAssignOp<LhsXprType, RhsXprType>& type;
50 };
51 
52 template<typename LhsXprType, typename RhsXprType>
53 struct nested<TensorAssignOp<LhsXprType, RhsXprType>, 1, typename eval<TensorAssignOp<LhsXprType, RhsXprType> >::type>
54 {
55  typedef TensorAssignOp<LhsXprType, RhsXprType> type;
56 };
57 
58 } // end namespace internal
59 
60 
61 
62 template<typename LhsXprType, typename RhsXprType>
63 class TensorAssignOp : public TensorBase<TensorAssignOp<LhsXprType, RhsXprType> >
64 {
65  public:
66  typedef typename Eigen::internal::traits<TensorAssignOp>::Scalar Scalar;
68  typedef typename LhsXprType::CoeffReturnType CoeffReturnType;
69  typedef typename Eigen::internal::nested<TensorAssignOp>::type Nested;
70  typedef typename Eigen::internal::traits<TensorAssignOp>::StorageKind StorageKind;
71  typedef typename Eigen::internal::traits<TensorAssignOp>::Index Index;
72 
73  static constexpr int NumDims = Eigen::internal::traits<TensorAssignOp>::NumDimensions;
74 
75  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorAssignOp(LhsXprType& lhs, const RhsXprType& rhs)
76  : m_lhs_xpr(lhs), m_rhs_xpr(rhs) {}
77 
82 
85  rhsExpression() const { return m_rhs_xpr; }
86 
87  protected:
90 };
91 
92 
93 template<typename LeftArgType, typename RightArgType, typename Device>
94 struct TensorEvaluator<const TensorAssignOp<LeftArgType, RightArgType>, Device>
95 {
97  typedef typename XprType::Index Index;
98  typedef typename XprType::Scalar Scalar;
104 
106  static constexpr int NumDims = XprType::NumDims;
108 
109  enum {
119  };
120 
121  //===- Tensor block evaluation strategy (see TensorBlock.h) -------------===//
122  typedef internal::TensorBlockDescriptor<NumDims, Index> TensorBlockDesc;
123  typedef internal::TensorBlockScratchAllocator<Device> TensorBlockScratch;
124 
127  //===--------------------------------------------------------------------===//
128 
129  TensorEvaluator(const XprType& op, const Device& device) :
130  m_leftImpl(op.lhsExpression(), device),
131  m_rightImpl(op.rhsExpression(), device)
132  {
136  YOU_MADE_A_PROGRAMMING_MISTAKE);
137  }
138 
140  {
141  // The dimensions of the lhs and the rhs tensors should be equal to prevent
142  // overflows and ensure the result is fully initialized.
143  // TODO: use left impl instead if right impl dimensions are known at compile time.
144  return m_rightImpl.dimensions();
145  }
146 
147  EIGEN_STRONG_INLINE bool evalSubExprsIfNeeded(EvaluatorPointerType) {
148  eigen_assert(dimensions_match(m_leftImpl.dimensions(), m_rightImpl.dimensions()));
149  m_leftImpl.evalSubExprsIfNeeded(NULL);
150  // If the lhs provides raw access to its storage area (i.e. if m_leftImpl.data() returns a non
151  // null value), attempt to evaluate the rhs expression in place. Returns true iff in place
152  // evaluation isn't supported and the caller still needs to manually assign the values generated
153  // by the rhs to the lhs.
154  return m_rightImpl.evalSubExprsIfNeeded(m_leftImpl.data());
155  }
156 
157 #ifdef EIGEN_USE_THREADS
158  template <typename EvalSubExprsCallback>
159  EIGEN_STRONG_INLINE void evalSubExprsIfNeededAsync(
160  EvaluatorPointerType, EvalSubExprsCallback done) {
161  m_leftImpl.evalSubExprsIfNeededAsync(nullptr, [this, done](bool) {
162  m_rightImpl.evalSubExprsIfNeededAsync(
163  m_leftImpl.data(), [done](bool need_assign) { done(need_assign); });
164  });
165  }
166 #endif // EIGEN_USE_THREADS
167 
168  EIGEN_STRONG_INLINE void cleanup() {
169  m_leftImpl.cleanup();
170  m_rightImpl.cleanup();
171  }
172 
173  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void evalScalar(Index i) const {
174  m_leftImpl.coeffRef(i) = m_rightImpl.coeff(i);
175  }
176  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void evalPacket(Index i) const {
177 
180  m_leftImpl.template writePacket<LhsStoreMode>(i, m_rightImpl.template packet<RhsLoadMode>(i));
181  }
183  {
184  return m_leftImpl.coeff(index);
185  }
186  template<int LoadMode>
188  {
189  return m_leftImpl.template packet<LoadMode>(index);
190  }
191 
192  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorOpCost
193  costPerCoeff(bool vectorized) const {
194  // We assume that evalPacket or evalScalar is called to perform the
195  // assignment and account for the cost of the write here, but reduce left
196  // cost by one load because we are using m_leftImpl.coeffRef.
197  TensorOpCost left = m_leftImpl.costPerCoeff(vectorized);
198  return m_rightImpl.costPerCoeff(vectorized) +
199  TensorOpCost(
200  numext::maxi(0.0, left.bytes_loaded() - sizeof(CoeffReturnType)),
201  left.bytes_stored(), left.compute_cycles()) +
202  TensorOpCost(0, sizeof(CoeffReturnType), 0, vectorized, PacketSize);
203  }
204 
205  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
206  internal::TensorBlockResourceRequirements getResourceRequirements() const {
207  return internal::TensorBlockResourceRequirements::merge(
208  m_leftImpl.getResourceRequirements(),
209  m_rightImpl.getResourceRequirements());
210  }
211 
212  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void evalBlock(
213  TensorBlockDesc& desc, TensorBlockScratch& scratch) {
215  m_leftImpl.data() != NULL) {
216  // If destination has raw data access, we pass it as a potential
217  // destination for a block descriptor evaluation.
218  desc.template AddDestinationBuffer<Layout>(
219  /*dst_base=*/m_leftImpl.data() + desc.offset(),
220  /*dst_strides=*/internal::strides<Layout>(m_leftImpl.dimensions()));
221  }
222 
223  RightTensorBlock block = m_rightImpl.block(desc, scratch, /*root_of_expr_ast=*/true);
224  // If block was evaluated into a destination, there is no need to do assignment.
226  m_leftImpl.writeBlock(desc, block);
227  }
228  block.cleanup();
229  }
230 
231  EIGEN_DEVICE_FUNC EvaluatorPointerType data() const { return m_leftImpl.data(); }
232 
233  private:
236 };
237 
238 }
239 
240 
241 #endif // EIGEN_CXX11_TENSOR_TENSOR_ASSIGN_H
int i
#define EIGEN_DEVICE_FUNC
#define eigen_assert(x)
#define EIGEN_STATIC_ASSERT(X, MSG)
const internal::remove_all_t< typename RhsXprType::Nested > & m_rhs_xpr
Definition: TensorAssign.h:89
internal::remove_all_t< typename LhsXprType::Nested > & m_lhs_xpr
Definition: TensorAssign.h:88
LhsXprType::CoeffReturnType CoeffReturnType
Definition: TensorAssign.h:68
Eigen::internal::traits< TensorAssignOp >::StorageKind StorageKind
Definition: TensorAssign.h:70
Eigen::internal::traits< TensorAssignOp >::Scalar Scalar
Definition: TensorAssign.h:66
static constexpr int NumDims
Definition: TensorAssign.h:73
Eigen::internal::nested< TensorAssignOp >::type Nested
Definition: TensorAssign.h:69
TensorAssignOp(LhsXprType &lhs, const RhsXprType &rhs)
Definition: TensorAssign.h:75
const internal::remove_all_t< typename RhsXprType::Nested > & rhsExpression() const
Definition: TensorAssign.h:85
internal::remove_all_t< typename LhsXprType::Nested > & lhsExpression() const
Definition: TensorAssign.h:81
Eigen::NumTraits< Scalar >::Real RealScalar
Definition: TensorAssign.h:67
Eigen::internal::traits< TensorAssignOp >::Index Index
Definition: TensorAssign.h:71
The tensor base class.
double bytes_stored() const
double bytes_loaded() const
double compute_cycles() const
typename remove_all< T >::type remove_all_t
EIGEN_ALWAYS_INLINE T maxi(const T &x, const T &y)
: TensorContractionSycl.h, provides various tensor contraction kernel for SYCL backend
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
EIGEN_ALWAYS_INLINE bool dimensions_match(Dims1 dims1, Dims2 dims2)
internal::packet_traits< Scalar >::type type
Definition: TensorMeta.h:55
void evalBlock(TensorBlockDesc &desc, TensorBlockScratch &scratch)
Definition: TensorAssign.h:212
TensorEvaluator< const RightArgType, Device >::TensorBlock RightTensorBlock
Definition: TensorAssign.h:126
internal::TensorBlockResourceRequirements getResourceRequirements() const
Definition: TensorAssign.h:206
A cost model used to limit the number of threads used for evaluating tensor expression.
static constexpr int Layout
TensorBlock block(TensorBlockDesc &desc, TensorBlockScratch &scratch, bool=false) const
Storage::Type EvaluatorPointerType
static constexpr int PacketSize
EvaluatorPointerType data() const
internal::TensorMaterializedBlock< ScalarNoConst, NumCoords, Layout, Index > TensorBlock