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;
35  static const std::size_t NumDimensions = internal::traits<LhsXprType>::NumDimensions;
36  static const int Layout = internal::traits<LhsXprType>::Layout;
37 
38  enum {
39  Flags = 0
40  };
41 };
42 
43 template<typename LhsXprType, typename RhsXprType>
44 struct eval<TensorAssignOp<LhsXprType, RhsXprType>, Eigen::Dense>
45 {
47 };
48 
49 template<typename LhsXprType, typename RhsXprType>
50 struct nested<TensorAssignOp<LhsXprType, RhsXprType>, 1, typename eval<TensorAssignOp<LhsXprType, RhsXprType> >::type>
51 {
53 };
54 
55 } // end namespace internal
56 
57 
58 
59 template<typename LhsXprType, typename RhsXprType>
60 class TensorAssignOp : public TensorBase<TensorAssignOp<LhsXprType, RhsXprType> >
61 {
62  public:
65  typedef typename LhsXprType::CoeffReturnType CoeffReturnType;
69 
70  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorAssignOp(LhsXprType& lhs, const RhsXprType& rhs)
71  : m_lhs_xpr(lhs), m_rhs_xpr(rhs) {}
72 
74  EIGEN_DEVICE_FUNC
76  lhsExpression() const { return *((typename internal::remove_all<typename LhsXprType::Nested>::type*)&m_lhs_xpr); }
77 
78  EIGEN_DEVICE_FUNC
80  rhsExpression() const { return m_rhs_xpr; }
81 
82  protected:
85 };
86 
87 
88 template<typename LeftArgType, typename RightArgType, typename Device>
89 struct TensorEvaluator<const TensorAssignOp<LeftArgType, RightArgType>, Device>
90 {
92  typedef typename XprType::Index Index;
93  typedef typename XprType::Scalar Scalar;
98 
99  enum {
104  };
105 
106  EIGEN_DEVICE_FUNC TensorEvaluator(const XprType& op, const Device& device) :
107  m_leftImpl(op.lhsExpression(), device),
108  m_rightImpl(op.rhsExpression(), device)
109  {
110  EIGEN_STATIC_ASSERT((static_cast<int>(TensorEvaluator<LeftArgType, Device>::Layout) == static_cast<int>(TensorEvaluator<RightArgType, Device>::Layout)), YOU_MADE_A_PROGRAMMING_MISTAKE);
111  }
112 
113  EIGEN_DEVICE_FUNC const Dimensions& dimensions() const
114  {
115  // The dimensions of the lhs and the rhs tensors should be equal to prevent
116  // overflows and ensure the result is fully initialized.
117  // TODO: use left impl instead if right impl dimensions are known at compile time.
118  return m_rightImpl.dimensions();
119  }
120 
121  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE bool evalSubExprsIfNeeded(Scalar*) {
122  eigen_assert(dimensions_match(m_leftImpl.dimensions(), m_rightImpl.dimensions()));
123  m_leftImpl.evalSubExprsIfNeeded(NULL);
124  // If the lhs provides raw access to its storage area (i.e. if m_leftImpl.data() returns a non
125  // null value), attempt to evaluate the rhs expression in place. Returns true iff in place
126  // evaluation isn't supported and the caller still needs to manually assign the values generated
127  // by the rhs to the lhs.
128  return m_rightImpl.evalSubExprsIfNeeded(m_leftImpl.data());
129  }
130  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void cleanup() {
131  m_leftImpl.cleanup();
132  m_rightImpl.cleanup();
133  }
134 
135  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void evalScalar(Index i) {
136  m_leftImpl.coeffRef(i) = m_rightImpl.coeff(i);
137  }
138  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void evalPacket(Index i) {
141  m_leftImpl.template writePacket<LhsStoreMode>(i, m_rightImpl.template packet<RhsLoadMode>(i));
142  }
143  EIGEN_DEVICE_FUNC CoeffReturnType coeff(Index index) const
144  {
145  return m_leftImpl.coeff(index);
146  }
147  template<int LoadMode>
148  EIGEN_DEVICE_FUNC PacketReturnType packet(Index index) const
149  {
150  return m_leftImpl.template packet<LoadMode>(index);
151  }
152 
153  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorOpCost
154  costPerCoeff(bool vectorized) const {
155  // We assume that evalPacket or evalScalar is called to perform the
156  // assignment and account for the cost of the write here, but reduce left
157  // cost by one load because we are using m_leftImpl.coeffRef.
158  TensorOpCost left = m_leftImpl.costPerCoeff(vectorized);
159  return m_rightImpl.costPerCoeff(vectorized) +
160  TensorOpCost(
161  numext::maxi(0.0, left.bytes_loaded() - sizeof(CoeffReturnType)),
162  left.bytes_stored(), left.compute_cycles()) +
163  TensorOpCost(0, sizeof(CoeffReturnType), 0, vectorized, PacketSize);
164  }
165 
167  const TensorEvaluator<LeftArgType, Device>& left_impl() const { return m_leftImpl; }
169  const TensorEvaluator<RightArgType, Device>& right_impl() const { return m_rightImpl; }
170 
171  EIGEN_DEVICE_FUNC CoeffReturnType* data() const { return m_leftImpl.data(); }
172 
173  private:
176 };
177 
178 }
179 
180 
181 #endif // EIGEN_CXX11_TENSOR_TENSOR_ASSIGN_H
LhsXprType::CoeffReturnType CoeffReturnType
Definition: TensorAssign.h:65
const TensorEvaluator< RightArgType, Device > & right_impl() const
required by sycl in order to extract the accessor
Definition: TensorAssign.h:169
#define EIGEN_STRONG_INLINE
Definition: Macros.h:493
EIGEN_DEVICE_FUNC PacketReturnType packet(Index index) const
Definition: TensorAssign.h:148
EIGEN_DEVICE_FUNC internal::remove_all< typename LhsXprType::Nested >::type & lhsExpression() const
Definition: TensorAssign.h:76
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE double bytes_loaded() const
Eigen::NumTraits< Scalar >::Real RealScalar
Definition: TensorAssign.h:64
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE double compute_cycles() const
Definition: LDLT.h:16
A cost model used to limit the number of threads used for evaluating tensor expression.
const TensorEvaluator< LeftArgType, Device > & left_impl() const
required by sycl in order to extract the accessor
Definition: TensorAssign.h:167
#define EIGEN_STATIC_ASSERT(CONDITION, MSG)
Definition: StaticAssert.h:122
Eigen::internal::nested< TensorAssignOp >::type Nested
Definition: TensorAssign.h:66
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorAssignOp(LhsXprType &lhs, const RhsXprType &rhs)
Definition: TensorAssign.h:70
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE bool evalSubExprsIfNeeded(Scalar *)
Definition: TensorAssign.h:121
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorOpCost costPerCoeff(bool vectorized) const
Definition: TensorAssign.h:154
Eigen::internal::traits< TensorAssignOp >::StorageKind StorageKind
Definition: TensorAssign.h:67
internal::remove_all< typename LhsXprType::Nested >::type & m_lhs_xpr
Definition: TensorAssign.h:83
const internal::remove_all< typename RhsXprType::Nested >::type & m_rhs_xpr
Definition: TensorAssign.h:84
#define eigen_assert(x)
Definition: Macros.h:577
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE double bytes_stored() const
promote_index_type< typename traits< LhsXprType >::Index, typename traits< RhsXprType >::Index >::type Index
Definition: TensorAssign.h:30
EIGEN_DEVICE_FUNC bool dimensions_match(Dims1 &dims1, Dims2 &dims2)
The tensor base class.
Definition: TensorBase.h:827
Eigen::internal::traits< TensorAssignOp >::Index Index
Definition: TensorAssign.h:68
EIGEN_DEVICE_FUNC const internal::remove_all< typename RhsXprType::Nested >::type & rhsExpression() const
Definition: TensorAssign.h:80
Eigen::internal::traits< TensorAssignOp >::Scalar Scalar
Definition: TensorAssign.h:63
internal::packet_traits< Scalar >::type type
Definition: TensorMeta.h:51
EIGEN_DEVICE_FUNC TensorEvaluator(const XprType &op, const Device &device)
Definition: TensorAssign.h:106


hebiros
Author(s): Xavier Artache , Matthew Tesch
autogenerated on Thu Sep 3 2020 04:09:11