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 namespace Eigen {
14 
23 namespace internal {
24 template<typename LhsXprType, typename RhsXprType>
25 struct traits<TensorAssignOp<LhsXprType, RhsXprType> >
26 {
27  typedef typename LhsXprType::Scalar Scalar;
31  typedef typename LhsXprType::Nested LhsNested;
32  typedef typename RhsXprType::Nested RhsNested;
36  static const int Layout = internal::traits<LhsXprType>::Layout;
38 
39  enum {
40  Flags = 0
41  };
42 };
43 
44 template<typename LhsXprType, typename RhsXprType>
45 struct eval<TensorAssignOp<LhsXprType, RhsXprType>, Eigen::Dense>
46 {
48 };
49 
50 template<typename LhsXprType, typename RhsXprType>
51 struct nested<TensorAssignOp<LhsXprType, RhsXprType>, 1, typename eval<TensorAssignOp<LhsXprType, RhsXprType> >::type>
52 {
54 };
55 
56 } // end namespace internal
57 
58 
59 
60 template<typename LhsXprType, typename RhsXprType>
61 class TensorAssignOp : public TensorBase<TensorAssignOp<LhsXprType, RhsXprType> >
62 {
63  public:
66  typedef typename LhsXprType::CoeffReturnType CoeffReturnType;
70 
72 
73  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorAssignOp(LhsXprType& lhs, const RhsXprType& rhs)
74  : m_lhs_xpr(lhs), m_rhs_xpr(rhs) {}
75 
79  lhsExpression() const { return *((typename internal::remove_all<typename LhsXprType::Nested>::type*)&m_lhs_xpr); }
80 
83  rhsExpression() const { return m_rhs_xpr; }
84 
85  protected:
88 };
89 
90 
91 template<typename LeftArgType, typename RightArgType, typename Device>
92 struct TensorEvaluator<const TensorAssignOp<LeftArgType, RightArgType>, Device>
93 {
95  typedef typename XprType::Index Index;
96  typedef typename XprType::Scalar Scalar;
102 
103  static const int PacketSize = PacketType<CoeffReturnType, Device>::size;
104  static const int NumDims = XprType::NumDims;
105 
106  enum {
117  };
118 
119  //===- Tensor block evaluation strategy (see TensorBlock.h) -------------===//
122 
125  //===--------------------------------------------------------------------===//
126 
127  TensorEvaluator(const XprType& op, const Device& device) :
128  m_leftImpl(op.lhsExpression(), device),
129  m_rightImpl(op.rhsExpression(), device)
130  {
134  YOU_MADE_A_PROGRAMMING_MISTAKE);
135  }
136 
137  EIGEN_DEVICE_FUNC const Dimensions& dimensions() const
138  {
139  // The dimensions of the lhs and the rhs tensors should be equal to prevent
140  // overflows and ensure the result is fully initialized.
141  // TODO: use left impl instead if right impl dimensions are known at compile time.
142  return m_rightImpl.dimensions();
143  }
144 
145  EIGEN_STRONG_INLINE bool evalSubExprsIfNeeded(EvaluatorPointerType) {
146  eigen_assert(dimensions_match(m_leftImpl.dimensions(), m_rightImpl.dimensions()));
147  m_leftImpl.evalSubExprsIfNeeded(NULL);
148  // If the lhs provides raw access to its storage area (i.e. if m_leftImpl.data() returns a non
149  // null value), attempt to evaluate the rhs expression in place. Returns true iff in place
150  // evaluation isn't supported and the caller still needs to manually assign the values generated
151  // by the rhs to the lhs.
152  return m_rightImpl.evalSubExprsIfNeeded(m_leftImpl.data());
153  }
154 
155 #ifdef EIGEN_USE_THREADS
156  template <typename EvalSubExprsCallback>
157  EIGEN_STRONG_INLINE void evalSubExprsIfNeededAsync(
158  EvaluatorPointerType, EvalSubExprsCallback done) {
159  m_leftImpl.evalSubExprsIfNeededAsync(nullptr, [this, done](bool) {
160  m_rightImpl.evalSubExprsIfNeededAsync(
161  m_leftImpl.data(), [done](bool need_assign) { done(need_assign); });
162  });
163  }
164 #endif // EIGEN_USE_THREADS
165 
167  m_leftImpl.cleanup();
168  m_rightImpl.cleanup();
169  }
170 
172  m_leftImpl.coeffRef(i) = m_rightImpl.coeff(i);
173  }
175 
178  m_leftImpl.template writePacket<LhsStoreMode>(i, m_rightImpl.template packet<RhsLoadMode>(i));
179  }
180  EIGEN_DEVICE_FUNC CoeffReturnType coeff(Index index) const
181  {
182  return m_leftImpl.coeff(index);
183  }
184  template<int LoadMode>
185  EIGEN_DEVICE_FUNC PacketReturnType packet(Index index) const
186  {
187  return m_leftImpl.template packet<LoadMode>(index);
188  }
189 
191  costPerCoeff(bool vectorized) const {
192  // We assume that evalPacket or evalScalar is called to perform the
193  // assignment and account for the cost of the write here, but reduce left
194  // cost by one load because we are using m_leftImpl.coeffRef.
195  TensorOpCost left = m_leftImpl.costPerCoeff(vectorized);
196  return m_rightImpl.costPerCoeff(vectorized) +
197  TensorOpCost(
198  numext::maxi(0.0, left.bytes_loaded() - sizeof(CoeffReturnType)),
199  left.bytes_stored(), left.compute_cycles()) +
200  TensorOpCost(0, sizeof(CoeffReturnType), 0, vectorized, PacketSize);
201  }
202 
206  m_leftImpl.getResourceRequirements(),
207  m_rightImpl.getResourceRequirements());
208  }
209 
211  TensorBlockDesc& desc, TensorBlockScratch& scratch) {
213  m_leftImpl.data() != NULL) {
214  // If destination has raw data access, we pass it as a potential
215  // destination for a block descriptor evaluation.
216  desc.template AddDestinationBuffer<Layout>(
217  /*dst_base=*/m_leftImpl.data() + desc.offset(),
218  /*dst_strides=*/internal::strides<Layout>(m_leftImpl.dimensions()));
219  }
220 
221  RightTensorBlock block = m_rightImpl.block(desc, scratch, /*root_of_expr_ast=*/true);
222  // If block was evaluated into a destination, there is no need to do assignment.
224  m_leftImpl.writeBlock(desc, block);
225  }
226  block.cleanup();
227  }
228 
229 #ifdef EIGEN_USE_SYCL
230  // binding placeholder accessors to a command group handler for SYCL
231  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void bind(cl::sycl::handler &cgh) const {
232  m_leftImpl.bind(cgh);
233  m_rightImpl.bind(cgh);
234  }
235 #endif
236 
237  EIGEN_DEVICE_FUNC EvaluatorPointerType data() const { return m_leftImpl.data(); }
238 
239  private:
242 };
243 
244 }
245 
246 
247 #endif // EIGEN_CXX11_TENSOR_TENSOR_ASSIGN_H
LhsXprType::CoeffReturnType CoeffReturnType
Definition: TensorAssign.h:66
SCALAR Scalar
Definition: bench_gemm.cpp:46
#define EIGEN_STRONG_INLINE
Definition: Macros.h:917
EIGEN_DEVICE_FUNC internal::remove_all< typename LhsXprType::Nested >::type & lhsExpression() const
Definition: TensorAssign.h:79
m m block(1, 0, 2, 2)<< 4
EIGEN_DEVICE_FUNC EvaluatorPointerType data() const
Eigen::NumTraits< Scalar >::Real RealScalar
Definition: TensorAssign.h:65
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE double compute_cycles() const
EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE bool dimensions_match(Dims1 dims1, Dims2 dims2)
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.
#define EIGEN_STATIC_ASSERT(CONDITION, MSG)
Definition: StaticAssert.h:127
EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE T maxi(const T &x, const T &y)
Eigen::internal::nested< TensorAssignOp >::type Nested
Definition: TensorAssign.h:67
EIGEN_DEVICE_FUNC const internal::remove_all< typename RhsXprType::Nested >::type & rhsExpression() const
Definition: TensorAssign.h:83
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE internal::TensorBlockResourceRequirements getResourceRequirements() const
Definition: TensorAssign.h:204
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE double bytes_stored() const
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorAssignOp(LhsXprType &lhs, const RhsXprType &rhs)
Definition: TensorAssign.h:73
static char left
Eigen::internal::traits< TensorAssignOp >::StorageKind StorageKind
Definition: TensorAssign.h:68
internal::remove_all< typename LhsXprType::Nested >::type & m_lhs_xpr
Definition: TensorAssign.h:86
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorOpCost costPerCoeff(bool vectorized) const
Definition: TensorAssign.h:191
const internal::remove_all< typename RhsXprType::Nested >::type & m_rhs_xpr
Definition: TensorAssign.h:87
#define eigen_assert(x)
Definition: Macros.h:1037
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE double bytes_loaded() const
TensorEvaluator< const RightArgType, Device >::TensorBlock RightTensorBlock
Definition: TensorAssign.h:124
promote_index_type< typename traits< LhsXprType >::Index, typename traits< RhsXprType >::Index >::type Index
Definition: TensorAssign.h:30
#define NULL
Definition: ccolamd.c:609
The tensor base class.
Definition: TensorBase.h:973
#define EIGEN_DEVICE_FUNC
Definition: Macros.h:976
Eigen::internal::traits< TensorAssignOp >::Index Index
Definition: TensorAssign.h:69
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void evalBlock(TensorBlockDesc &desc, TensorBlockScratch &scratch)
Definition: TensorAssign.h:210
Eigen::internal::traits< TensorAssignOp >::Scalar Scalar
Definition: TensorAssign.h:64
Generic expression where a coefficient-wise unary operator is applied to an expression.
Definition: CwiseUnaryOp.h:55
static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorBlockResourceRequirements merge(const TensorBlockResourceRequirements &lhs, const TensorBlockResourceRequirements &rhs)
Definition: TensorBlock.h:138
Definition: pytypes.h:1370


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