Loading...
Searching...
No Matches
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
13namespace Eigen {
14
15namespace internal {
16template<typename LhsXprType, typename RhsXprType>
17struct traits<TensorAssignOp<LhsXprType, RhsXprType> >
18{
19 typedef typename LhsXprType::Scalar Scalar;
20 typedef typename traits<LhsXprType>::StorageKind StorageKind;
21 typedef typename promote_index_type<typename traits<LhsXprType>::Index,
22 typename traits<RhsXprType>::Index>::type Index;
23 typedef typename LhsXprType::Nested LhsNested;
24 typedef typename RhsXprType::Nested RhsNested;
25 typedef typename remove_reference<LhsNested>::type _LhsNested;
26 typedef typename remove_reference<RhsNested>::type _RhsNested;
27 static const std::size_t NumDimensions = internal::traits<LhsXprType>::NumDimensions;
28 static const int Layout = internal::traits<LhsXprType>::Layout;
29
30 enum {
31 Flags = 0
32 };
33};
34
35template<typename LhsXprType, typename RhsXprType>
36struct eval<TensorAssignOp<LhsXprType, RhsXprType>, Eigen::Dense>
37{
38 typedef const TensorAssignOp<LhsXprType, RhsXprType>& type;
39};
40
41template<typename LhsXprType, typename RhsXprType>
42struct nested<TensorAssignOp<LhsXprType, RhsXprType>, 1, typename eval<TensorAssignOp<LhsXprType, RhsXprType> >::type>
43{
44 typedef TensorAssignOp<LhsXprType, RhsXprType> type;
45};
46
47} // end namespace internal
48
55template <typename LhsXprType, typename RhsXprType>
56class TensorAssignOp : public TensorBase<TensorAssignOp<LhsXprType, RhsXprType> > {
57 public:
58 typedef typename Eigen::internal::traits<TensorAssignOp>::Scalar Scalar;
59 typedef typename Eigen::NumTraits<Scalar>::Real RealScalar;
60 typedef typename LhsXprType::CoeffReturnType CoeffReturnType;
61 typedef typename Eigen::internal::nested<TensorAssignOp>::type Nested;
62 typedef typename Eigen::internal::traits<TensorAssignOp>::StorageKind StorageKind;
63 typedef typename Eigen::internal::traits<TensorAssignOp>::Index Index;
64
65 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorAssignOp(LhsXprType& lhs, const RhsXprType& rhs)
66 : m_lhs_xpr(lhs), m_rhs_xpr(rhs) {}
67
69 EIGEN_DEVICE_FUNC
70 typename internal::remove_all<typename LhsXprType::Nested>::type&
71 lhsExpression() const { return *((typename internal::remove_all<typename LhsXprType::Nested>::type*)&m_lhs_xpr); }
72
73 EIGEN_DEVICE_FUNC
74 const typename internal::remove_all<typename RhsXprType::Nested>::type&
75 rhsExpression() const { return m_rhs_xpr; }
76
77 protected:
78 typename internal::remove_all<typename LhsXprType::Nested>::type& m_lhs_xpr;
79 const typename internal::remove_all<typename RhsXprType::Nested>::type& m_rhs_xpr;
80};
81
82
83template<typename LeftArgType, typename RightArgType, typename Device>
84struct TensorEvaluator<const TensorAssignOp<LeftArgType, RightArgType>, Device>
85{
86 typedef TensorAssignOp<LeftArgType, RightArgType> XprType;
87 typedef typename XprType::Index Index;
88 typedef typename XprType::Scalar Scalar;
89 typedef typename XprType::CoeffReturnType CoeffReturnType;
90 typedef typename PacketType<CoeffReturnType, Device>::type PacketReturnType;
91 typedef typename TensorEvaluator<RightArgType, Device>::Dimensions Dimensions;
92 static const int PacketSize = internal::unpacket_traits<PacketReturnType>::size;
93
94 enum {
95 IsAligned = TensorEvaluator<LeftArgType, Device>::IsAligned & TensorEvaluator<RightArgType, Device>::IsAligned,
96 PacketAccess = TensorEvaluator<LeftArgType, Device>::PacketAccess & TensorEvaluator<RightArgType, Device>::PacketAccess,
97 Layout = TensorEvaluator<LeftArgType, Device>::Layout,
98 RawAccess = TensorEvaluator<LeftArgType, Device>::RawAccess
99 };
100
101 EIGEN_DEVICE_FUNC TensorEvaluator(const XprType& op, const Device& device) :
102 m_leftImpl(op.lhsExpression(), device),
103 m_rightImpl(op.rhsExpression(), device)
104 {
105 EIGEN_STATIC_ASSERT((static_cast<int>(TensorEvaluator<LeftArgType, Device>::Layout) == static_cast<int>(TensorEvaluator<RightArgType, Device>::Layout)), YOU_MADE_A_PROGRAMMING_MISTAKE);
106 }
107
108 EIGEN_DEVICE_FUNC const Dimensions& dimensions() const
109 {
110 // The dimensions of the lhs and the rhs tensors should be equal to prevent
111 // overflows and ensure the result is fully initialized.
112 // TODO: use left impl instead if right impl dimensions are known at compile time.
113 return m_rightImpl.dimensions();
114 }
115
116 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE bool evalSubExprsIfNeeded(Scalar*) {
117 eigen_assert(dimensions_match(m_leftImpl.dimensions(), m_rightImpl.dimensions()));
118 m_leftImpl.evalSubExprsIfNeeded(NULL);
119 // If the lhs provides raw access to its storage area (i.e. if m_leftImpl.data() returns a non
120 // null value), attempt to evaluate the rhs expression in place. Returns true iff in place
121 // evaluation isn't supported and the caller still needs to manually assign the values generated
122 // by the rhs to the lhs.
123 return m_rightImpl.evalSubExprsIfNeeded(m_leftImpl.data());
124 }
125 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void cleanup() {
126 m_leftImpl.cleanup();
127 m_rightImpl.cleanup();
128 }
129
130 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void evalScalar(Index i) {
131 m_leftImpl.coeffRef(i) = m_rightImpl.coeff(i);
132 }
133 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void evalPacket(Index i) {
134 const int LhsStoreMode = TensorEvaluator<LeftArgType, Device>::IsAligned ? Aligned : Unaligned;
135 const int RhsLoadMode = TensorEvaluator<RightArgType, Device>::IsAligned ? Aligned : Unaligned;
136 m_leftImpl.template writePacket<LhsStoreMode>(i, m_rightImpl.template packet<RhsLoadMode>(i));
137 }
138 EIGEN_DEVICE_FUNC CoeffReturnType coeff(Index index) const
139 {
140 return m_leftImpl.coeff(index);
141 }
142 template<int LoadMode>
143 EIGEN_DEVICE_FUNC PacketReturnType packet(Index index) const
144 {
145 return m_leftImpl.template packet<LoadMode>(index);
146 }
147
148 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorOpCost
149 costPerCoeff(bool vectorized) const {
150 // We assume that evalPacket or evalScalar is called to perform the
151 // assignment and account for the cost of the write here, but reduce left
152 // cost by one load because we are using m_leftImpl.coeffRef.
153 TensorOpCost left = m_leftImpl.costPerCoeff(vectorized);
154 return m_rightImpl.costPerCoeff(vectorized) +
155 TensorOpCost(
156 numext::maxi(0.0, left.bytes_loaded() - sizeof(CoeffReturnType)),
157 left.bytes_stored(), left.compute_cycles()) +
158 TensorOpCost(0, sizeof(CoeffReturnType), 0, vectorized, PacketSize);
159 }
160
162 const TensorEvaluator<LeftArgType, Device>& left_impl() const { return m_leftImpl; }
164 const TensorEvaluator<RightArgType, Device>& right_impl() const { return m_rightImpl; }
165
166 EIGEN_DEVICE_FUNC CoeffReturnType* data() const { return m_leftImpl.data(); }
167
168 private:
169 TensorEvaluator<LeftArgType, Device> m_leftImpl;
170 TensorEvaluator<RightArgType, Device> m_rightImpl;
171};
172
173}
174
175
176#endif // EIGEN_CXX11_TENSOR_TENSOR_ASSIGN_H
Definition TensorAssign.h:56
internal::remove_all< typenameLhsXprType::Nested >::type & lhsExpression() const
Definition TensorAssign.h:71
The tensor base class.
Definition TensorForwardDeclarations.h:29
Namespace containing all symbols from the Eigen library.
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The tensor evaluator class.
Definition TensorEvaluator.h:27
const Device & device() const
required by sycl in order to construct sycl buffer from raw pointer
Definition TensorEvaluator.h:112