Loading...
Searching...
No Matches
TensorInflation.h
1// This file is part of Eigen, a lightweight C++ template library
2// for linear algebra.
3//
4// Copyright (C) 2015 Ke Yang <yangke@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_INFLATION_H
11#define EIGEN_CXX11_TENSOR_TENSOR_INFLATION_H
12
13namespace Eigen {
14
15namespace internal {
16template<typename Strides, typename XprType>
17struct traits<TensorInflationOp<Strides, XprType> > : public traits<XprType>
18{
19 typedef typename XprType::Scalar Scalar;
20 typedef traits<XprType> XprTraits;
21 typedef typename XprTraits::StorageKind StorageKind;
22 typedef typename XprTraits::Index Index;
23 typedef typename XprType::Nested Nested;
24 typedef typename remove_reference<Nested>::type _Nested;
25 static const int NumDimensions = XprTraits::NumDimensions;
26 static const int Layout = XprTraits::Layout;
27};
28
29template<typename Strides, typename XprType>
30struct eval<TensorInflationOp<Strides, XprType>, Eigen::Dense>
31{
32 typedef const TensorInflationOp<Strides, XprType>& type;
33};
34
35template<typename Strides, typename XprType>
36struct nested<TensorInflationOp<Strides, XprType>, 1, typename eval<TensorInflationOp<Strides, XprType> >::type>
37{
38 typedef TensorInflationOp<Strides, XprType> type;
39};
40
41} // end namespace internal
42
48template <typename Strides, typename XprType>
49class TensorInflationOp : public TensorBase<TensorInflationOp<Strides, XprType>, ReadOnlyAccessors> {
50 public:
51 typedef typename Eigen::internal::traits<TensorInflationOp>::Scalar Scalar;
52 typedef typename Eigen::NumTraits<Scalar>::Real RealScalar;
53 typedef typename XprType::CoeffReturnType CoeffReturnType;
54 typedef typename Eigen::internal::nested<TensorInflationOp>::type Nested;
55 typedef typename Eigen::internal::traits<TensorInflationOp>::StorageKind StorageKind;
56 typedef typename Eigen::internal::traits<TensorInflationOp>::Index Index;
57
58 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorInflationOp(const XprType& expr, const Strides& strides)
59 : m_xpr(expr), m_strides(strides) {}
60
61 EIGEN_DEVICE_FUNC
62 const Strides& strides() const { return m_strides; }
63
64 EIGEN_DEVICE_FUNC
65 const typename internal::remove_all<typename XprType::Nested>::type&
66 expression() const { return m_xpr; }
67
68 protected:
69 typename XprType::Nested m_xpr;
70 const Strides m_strides;
71};
72
73// Eval as rvalue
74template<typename Strides, typename ArgType, typename Device>
75struct TensorEvaluator<const TensorInflationOp<Strides, ArgType>, Device>
76{
78 typedef typename XprType::Index Index;
79 static const int NumDims = internal::array_size<typename TensorEvaluator<ArgType, Device>::Dimensions>::value;
80 typedef DSizes<Index, NumDims> Dimensions;
81 typedef typename XprType::Scalar Scalar;
82 typedef typename XprType::CoeffReturnType CoeffReturnType;
83 typedef typename PacketType<CoeffReturnType, Device>::type PacketReturnType;
84 static const int PacketSize = internal::unpacket_traits<PacketReturnType>::size;
85
86 enum {
87 IsAligned = /*TensorEvaluator<ArgType, Device>::IsAligned*/ false,
88 PacketAccess = TensorEvaluator<ArgType, Device>::PacketAccess,
89 BlockAccess = false,
90 Layout = TensorEvaluator<ArgType, Device>::Layout,
91 CoordAccess = false, // to be implemented
92 RawAccess = false
93 };
94
95 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorEvaluator(const XprType& op, const Device& device)
96 : m_impl(op.expression(), device), m_strides(op.strides())
97 {
98 m_dimensions = m_impl.dimensions();
99 // Expand each dimension to the inflated dimension.
100 for (int i = 0; i < NumDims; ++i) {
101 m_dimensions[i] = (m_dimensions[i] - 1) * op.strides()[i] + 1;
102 }
103
104 // Remember the strides for fast division.
105 for (int i = 0; i < NumDims; ++i) {
106 m_fastStrides[i] = internal::TensorIntDivisor<Index>(m_strides[i]);
107 }
108
109 const typename TensorEvaluator<ArgType, Device>::Dimensions& input_dims = m_impl.dimensions();
110 if (static_cast<int>(Layout) == static_cast<int>(ColMajor)) {
111 m_outputStrides[0] = 1;
112 m_inputStrides[0] = 1;
113 for (int i = 1; i < NumDims; ++i) {
114 m_outputStrides[i] = m_outputStrides[i-1] * m_dimensions[i-1];
115 m_inputStrides[i] = m_inputStrides[i-1] * input_dims[i-1];
116 }
117 } else { // RowMajor
118 m_outputStrides[NumDims-1] = 1;
119 m_inputStrides[NumDims-1] = 1;
120 for (int i = NumDims - 2; i >= 0; --i) {
121 m_outputStrides[i] = m_outputStrides[i+1] * m_dimensions[i+1];
122 m_inputStrides[i] = m_inputStrides[i+1] * input_dims[i+1];
123 }
124 }
125 }
126
127 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Dimensions& dimensions() const { return m_dimensions; }
128
129 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE bool evalSubExprsIfNeeded(Scalar* /*data*/) {
130 m_impl.evalSubExprsIfNeeded(NULL);
131 return true;
132 }
133 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void cleanup() {
134 m_impl.cleanup();
135 }
136
137 // Computes the input index given the output index. Returns true if the output
138 // index doesn't fall into a hole.
139 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE bool getInputIndex(Index index, Index* inputIndex) const
140 {
141 eigen_assert(index < dimensions().TotalSize());
142 *inputIndex = 0;
143 if (static_cast<int>(Layout) == static_cast<int>(ColMajor)) {
144 for (int i = NumDims - 1; i > 0; --i) {
145 const Index idx = index / m_outputStrides[i];
146 if (idx != idx / m_fastStrides[i] * m_strides[i]) {
147 return false;
148 }
149 *inputIndex += idx / m_strides[i] * m_inputStrides[i];
150 index -= idx * m_outputStrides[i];
151 }
152 if (index != index / m_fastStrides[0] * m_strides[0]) {
153 return false;
154 }
155 *inputIndex += index / m_strides[0];
156 return true;
157 } else {
158 for (int i = 0; i < NumDims - 1; ++i) {
159 const Index idx = index / m_outputStrides[i];
160 if (idx != idx / m_fastStrides[i] * m_strides[i]) {
161 return false;
162 }
163 *inputIndex += idx / m_strides[i] * m_inputStrides[i];
164 index -= idx * m_outputStrides[i];
165 }
166 if (index != index / m_fastStrides[NumDims-1] * m_strides[NumDims-1]) {
167 return false;
168 }
169 *inputIndex += index / m_strides[NumDims - 1];
170 }
171 return true;
172 }
173
174 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE CoeffReturnType coeff(Index index) const
175 {
176 Index inputIndex = 0;
177 if (getInputIndex(index, &inputIndex)) {
178 return m_impl.coeff(inputIndex);
179 } else {
180 return Scalar(0);
181 }
182 }
183
184 // TODO(yangke): optimize this function so that we can detect and produce
185 // all-zero packets
186 template<int LoadMode>
187 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE PacketReturnType packet(Index index) const
188 {
189 EIGEN_STATIC_ASSERT((PacketSize > 1), YOU_MADE_A_PROGRAMMING_MISTAKE)
190 eigen_assert(index+PacketSize-1 < dimensions().TotalSize());
191
192 EIGEN_ALIGN_MAX typename internal::remove_const<CoeffReturnType>::type values[PacketSize];
193 for (int i = 0; i < PacketSize; ++i) {
194 values[i] = coeff(index+i);
195 }
196 PacketReturnType rslt = internal::pload<PacketReturnType>(values);
197 return rslt;
198 }
199
200 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorOpCost costPerCoeff(bool vectorized) const {
201 const double compute_cost = NumDims * (3 * TensorOpCost::DivCost<Index>() +
202 3 * TensorOpCost::MulCost<Index>() +
203 2 * TensorOpCost::AddCost<Index>());
204 const double input_size = m_impl.dimensions().TotalSize();
205 const double output_size = m_dimensions.TotalSize();
206 if (output_size == 0)
207 return TensorOpCost();
208 return m_impl.costPerCoeff(vectorized) +
209 TensorOpCost(sizeof(CoeffReturnType) * input_size / output_size, 0,
210 compute_cost, vectorized, PacketSize);
211 }
212
213 EIGEN_DEVICE_FUNC Scalar* data() const { return NULL; }
214
215 protected:
216 Dimensions m_dimensions;
217 array<Index, NumDims> m_outputStrides;
218 array<Index, NumDims> m_inputStrides;
219 TensorEvaluator<ArgType, Device> m_impl;
220 const Strides m_strides;
221 array<internal::TensorIntDivisor<Index>, NumDims> m_fastStrides;
222};
223
224} // end namespace Eigen
225
226#endif // EIGEN_CXX11_TENSOR_TENSOR_INFLATION_H
The tensor base class.
Definition TensorForwardDeclarations.h:29
Tensor inflation class.
Definition TensorInflation.h:49
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