Eigen-unsupported  3.4.90 (git rev 67eeba6e720c5745abc77ae6c92ce0a44aa7b7ae)
TensorAssign.h
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;
67  typedef typename Eigen::NumTraits<Scalar>::Real RealScalar;
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 
79  EIGEN_DEVICE_FUNC
80  internal::remove_all_t<typename LhsXprType::Nested>&
81  lhsExpression() const { return *((internal::remove_all_t<typename LhsXprType::Nested>*)&m_lhs_xpr); }
82 
83  EIGEN_DEVICE_FUNC
84  const internal::remove_all_t<typename RhsXprType::Nested>&
85  rhsExpression() const { return m_rhs_xpr; }
86 
87  protected:
88  internal::remove_all_t<typename LhsXprType::Nested>& m_lhs_xpr;
89  const internal::remove_all_t<typename RhsXprType::Nested>& m_rhs_xpr;
90 };
91 
92 
93 template<typename LeftArgType, typename RightArgType, typename Device>
94 struct TensorEvaluator<const TensorAssignOp<LeftArgType, RightArgType>, Device>
95 {
96  typedef TensorAssignOp<LeftArgType, RightArgType> XprType;
97  typedef typename XprType::Index Index;
98  typedef typename XprType::Scalar Scalar;
99  typedef typename XprType::CoeffReturnType CoeffReturnType;
100  typedef typename PacketType<CoeffReturnType, Device>::type PacketReturnType;
101  typedef typename TensorEvaluator<RightArgType, Device>::Dimensions Dimensions;
102  typedef StorageMemory<CoeffReturnType, Device> Storage;
103  typedef typename Storage::Type EvaluatorPointerType;
104 
105  static constexpr int PacketSize = PacketType<CoeffReturnType, Device>::size;
106  static constexpr int NumDims = XprType::NumDims;
107  static constexpr int Layout = TensorEvaluator<LeftArgType, Device>::Layout;
108 
109  enum {
110  IsAligned = int(TensorEvaluator<LeftArgType, Device>::IsAligned) &
111  int(TensorEvaluator<RightArgType, Device>::IsAligned),
112  PacketAccess = int(TensorEvaluator<LeftArgType, Device>::PacketAccess) &
113  int(TensorEvaluator<RightArgType, Device>::PacketAccess),
114  BlockAccess = int(TensorEvaluator<LeftArgType, Device>::BlockAccess) &
115  int(TensorEvaluator<RightArgType, Device>::BlockAccess),
116  PreferBlockAccess = int(TensorEvaluator<LeftArgType, Device>::PreferBlockAccess) |
117  int(TensorEvaluator<RightArgType, Device>::PreferBlockAccess),
118  RawAccess = TensorEvaluator<LeftArgType, Device>::RawAccess
119  };
120 
121  //===- Tensor block evaluation strategy (see TensorBlock.h) -------------===//
122  typedef internal::TensorBlockDescriptor<NumDims, Index> TensorBlockDesc;
123  typedef internal::TensorBlockScratchAllocator<Device> TensorBlockScratch;
124 
125  typedef typename TensorEvaluator<const RightArgType, Device>::TensorBlock
126  RightTensorBlock;
127  //===--------------------------------------------------------------------===//
128 
129  TensorEvaluator(const XprType& op, const Device& device) :
130  m_leftImpl(op.lhsExpression(), device),
131  m_rightImpl(op.rhsExpression(), device)
132  {
133  EIGEN_STATIC_ASSERT(
134  (static_cast<int>(TensorEvaluator<LeftArgType, Device>::Layout) ==
135  static_cast<int>(TensorEvaluator<RightArgType, Device>::Layout)),
136  YOU_MADE_A_PROGRAMMING_MISTAKE);
137  }
138 
139  EIGEN_DEVICE_FUNC const Dimensions& dimensions() const
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) {
174  m_leftImpl.coeffRef(i) = m_rightImpl.coeff(i);
175  }
176  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void evalPacket(Index i) {
177 
178  const int LhsStoreMode = TensorEvaluator<LeftArgType, Device>::IsAligned ? Aligned : Unaligned;
179  const int RhsLoadMode = TensorEvaluator<RightArgType, Device>::IsAligned ? Aligned : Unaligned;
180  m_leftImpl.template writePacket<LhsStoreMode>(i, m_rightImpl.template packet<RhsLoadMode>(i));
181  }
182  EIGEN_DEVICE_FUNC CoeffReturnType coeff(Index index) const
183  {
184  return m_leftImpl.coeff(index);
185  }
186  template<int LoadMode>
187  EIGEN_DEVICE_FUNC PacketReturnType packet(Index index) const
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) {
214  if (TensorEvaluator<LeftArgType, Device>::RawAccess &&
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.
225  if (block.kind() != internal::TensorBlockKind::kMaterializedInOutput) {
226  m_leftImpl.writeBlock(desc, block);
227  }
228  block.cleanup();
229  }
230 
231 #ifdef EIGEN_USE_SYCL
232  // binding placeholder accessors to a command group handler for SYCL
233  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void bind(cl::sycl::handler &cgh) const {
234  m_leftImpl.bind(cgh);
235  m_rightImpl.bind(cgh);
236  }
237 #endif
238 
239  EIGEN_DEVICE_FUNC EvaluatorPointerType data() const { return m_leftImpl.data(); }
240 
241  private:
242  TensorEvaluator<LeftArgType, Device> m_leftImpl;
243  TensorEvaluator<RightArgType, Device> m_rightImpl;
244 };
245 
246 }
247 
248 
249 #endif // EIGEN_CXX11_TENSOR_TENSOR_ASSIGN_H
Namespace containing all symbols from the Eigen library.
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index