Eigen-unsupported  3.4.1 (git rev 28ded8800c26864e537852658428ab44c8399e87)
 
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 typedef typename XprTraits::PointerType PointerType;
28};
29
30template<typename Strides, typename XprType>
31struct eval<TensorInflationOp<Strides, XprType>, Eigen::Dense>
32{
33 typedef const TensorInflationOp<Strides, XprType>& type;
34};
35
36template<typename Strides, typename XprType>
37struct nested<TensorInflationOp<Strides, XprType>, 1, typename eval<TensorInflationOp<Strides, XprType> >::type>
38{
39 typedef TensorInflationOp<Strides, XprType> type;
40};
41
42} // end namespace internal
43
49template <typename Strides, typename XprType>
50class TensorInflationOp : public TensorBase<TensorInflationOp<Strides, XprType>, ReadOnlyAccessors> {
51 public:
52 typedef typename Eigen::internal::traits<TensorInflationOp>::Scalar Scalar;
53 typedef typename Eigen::NumTraits<Scalar>::Real RealScalar;
54 typedef typename XprType::CoeffReturnType CoeffReturnType;
55 typedef typename Eigen::internal::nested<TensorInflationOp>::type Nested;
56 typedef typename Eigen::internal::traits<TensorInflationOp>::StorageKind StorageKind;
57 typedef typename Eigen::internal::traits<TensorInflationOp>::Index Index;
58
59 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorInflationOp(const XprType& expr, const Strides& strides)
60 : m_xpr(expr), m_strides(strides) {}
61
62 EIGEN_DEVICE_FUNC
63 const Strides& strides() const { return m_strides; }
64
65 EIGEN_DEVICE_FUNC
66 const typename internal::remove_all<typename XprType::Nested>::type&
67 expression() const { return m_xpr; }
68
69 protected:
70 typename XprType::Nested m_xpr;
71 const Strides m_strides;
72};
73
74// Eval as rvalue
75template<typename Strides, typename ArgType, typename Device>
76struct TensorEvaluator<const TensorInflationOp<Strides, ArgType>, Device>
77{
79 typedef typename XprType::Index Index;
80 static const int NumDims = internal::array_size<typename TensorEvaluator<ArgType, Device>::Dimensions>::value;
81 typedef DSizes<Index, NumDims> Dimensions;
82 typedef typename XprType::Scalar Scalar;
83 typedef typename XprType::CoeffReturnType CoeffReturnType;
84 typedef typename PacketType<CoeffReturnType, Device>::type PacketReturnType;
85 static const int PacketSize = PacketType<CoeffReturnType, Device>::size;
86 typedef StorageMemory<CoeffReturnType, Device> Storage;
87 typedef typename Storage::Type EvaluatorPointerType;
88
89 enum {
90 IsAligned = /*TensorEvaluator<ArgType, Device>::IsAligned*/ false,
91 PacketAccess = TensorEvaluator<ArgType, Device>::PacketAccess,
92 BlockAccess = false,
93 PreferBlockAccess = false,
94 Layout = TensorEvaluator<ArgType, Device>::Layout,
95 CoordAccess = false, // to be implemented
96 RawAccess = false
97 };
98
99 //===- Tensor block evaluation strategy (see TensorBlock.h) -------------===//
100 typedef internal::TensorBlockNotImplemented TensorBlock;
101 //===--------------------------------------------------------------------===//
102
103 EIGEN_STRONG_INLINE TensorEvaluator(const XprType& op, const Device& device)
104 : m_impl(op.expression(), device), m_strides(op.strides())
105 {
106 m_dimensions = m_impl.dimensions();
107 // Expand each dimension to the inflated dimension.
108 for (int i = 0; i < NumDims; ++i) {
109 m_dimensions[i] = (m_dimensions[i] - 1) * op.strides()[i] + 1;
110 }
111
112 // Remember the strides for fast division.
113 for (int i = 0; i < NumDims; ++i) {
114 m_fastStrides[i] = internal::TensorIntDivisor<Index>(m_strides[i]);
115 }
116
117 const typename TensorEvaluator<ArgType, Device>::Dimensions& input_dims = m_impl.dimensions();
118 if (static_cast<int>(Layout) == static_cast<int>(ColMajor)) {
119 m_outputStrides[0] = 1;
120 m_inputStrides[0] = 1;
121 for (int i = 1; i < NumDims; ++i) {
122 m_outputStrides[i] = m_outputStrides[i-1] * m_dimensions[i-1];
123 m_inputStrides[i] = m_inputStrides[i-1] * input_dims[i-1];
124 }
125 } else { // RowMajor
126 m_outputStrides[NumDims-1] = 1;
127 m_inputStrides[NumDims-1] = 1;
128 for (int i = NumDims - 2; i >= 0; --i) {
129 m_outputStrides[i] = m_outputStrides[i+1] * m_dimensions[i+1];
130 m_inputStrides[i] = m_inputStrides[i+1] * input_dims[i+1];
131 }
132 }
133 }
134
135 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Dimensions& dimensions() const { return m_dimensions; }
136
137 EIGEN_STRONG_INLINE bool evalSubExprsIfNeeded(EvaluatorPointerType /*data*/) {
138 m_impl.evalSubExprsIfNeeded(NULL);
139 return true;
140 }
141 EIGEN_STRONG_INLINE void cleanup() {
142 m_impl.cleanup();
143 }
144
145 // Computes the input index given the output index. Returns true if the output
146 // index doesn't fall into a hole.
147 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE bool getInputIndex(Index index, Index* inputIndex) const
148 {
149 eigen_assert(index < dimensions().TotalSize());
150 *inputIndex = 0;
151 if (static_cast<int>(Layout) == static_cast<int>(ColMajor)) {
152 EIGEN_UNROLL_LOOP
153 for (int i = NumDims - 1; i > 0; --i) {
154 const Index idx = index / m_outputStrides[i];
155 if (idx != idx / m_fastStrides[i] * m_strides[i]) {
156 return false;
157 }
158 *inputIndex += idx / m_strides[i] * m_inputStrides[i];
159 index -= idx * m_outputStrides[i];
160 }
161 if (index != index / m_fastStrides[0] * m_strides[0]) {
162 return false;
163 }
164 *inputIndex += index / m_strides[0];
165 return true;
166 } else {
167 EIGEN_UNROLL_LOOP
168 for (int i = 0; i < NumDims - 1; ++i) {
169 const Index idx = index / m_outputStrides[i];
170 if (idx != idx / m_fastStrides[i] * m_strides[i]) {
171 return false;
172 }
173 *inputIndex += idx / m_strides[i] * m_inputStrides[i];
174 index -= idx * m_outputStrides[i];
175 }
176 if (index != index / m_fastStrides[NumDims-1] * m_strides[NumDims-1]) {
177 return false;
178 }
179 *inputIndex += index / m_strides[NumDims - 1];
180 }
181 return true;
182 }
183
184 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE CoeffReturnType coeff(Index index) const
185 {
186 Index inputIndex = 0;
187 if (getInputIndex(index, &inputIndex)) {
188 return m_impl.coeff(inputIndex);
189 } else {
190 return Scalar(0);
191 }
192 }
193
194 // TODO(yangke): optimize this function so that we can detect and produce
195 // all-zero packets
196 template<int LoadMode>
197 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE PacketReturnType packet(Index index) const
198 {
199 EIGEN_STATIC_ASSERT((PacketSize > 1), YOU_MADE_A_PROGRAMMING_MISTAKE)
200 eigen_assert(index+PacketSize-1 < dimensions().TotalSize());
201
202 EIGEN_ALIGN_MAX typename internal::remove_const<CoeffReturnType>::type values[PacketSize];
203 EIGEN_UNROLL_LOOP
204 for (int i = 0; i < PacketSize; ++i) {
205 values[i] = coeff(index+i);
206 }
207 PacketReturnType rslt = internal::pload<PacketReturnType>(values);
208 return rslt;
209 }
210
211 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorOpCost costPerCoeff(bool vectorized) const {
212 const double compute_cost = NumDims * (3 * TensorOpCost::DivCost<Index>() +
213 3 * TensorOpCost::MulCost<Index>() +
214 2 * TensorOpCost::AddCost<Index>());
215 const double input_size = m_impl.dimensions().TotalSize();
216 const double output_size = m_dimensions.TotalSize();
217 if (output_size == 0)
218 return TensorOpCost();
219 return m_impl.costPerCoeff(vectorized) +
220 TensorOpCost(sizeof(CoeffReturnType) * input_size / output_size, 0,
221 compute_cost, vectorized, PacketSize);
222 }
223
224 EIGEN_DEVICE_FUNC EvaluatorPointerType data() const { return NULL; }
225
226#ifdef EIGEN_USE_SYCL
227 // binding placeholder accessors to a command group handler for SYCL
228 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void bind(cl::sycl::handler &cgh) const {
229 m_impl.bind(cgh);
230 }
231#endif
232
233 protected:
234 Dimensions m_dimensions;
235 array<Index, NumDims> m_outputStrides;
236 array<Index, NumDims> m_inputStrides;
237 TensorEvaluator<ArgType, Device> m_impl;
238 const Strides m_strides;
239 array<internal::TensorIntDivisor<Index>, NumDims> m_fastStrides;
240};
241
242} // end namespace Eigen
243
244#endif // EIGEN_CXX11_TENSOR_TENSOR_INFLATION_H
The tensor base class.
Definition TensorForwardDeclarations.h:56
Tensor inflation class.
Definition TensorInflation.h:50
Namespace containing all symbols from the Eigen library.
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The tensor evaluator class.
Definition TensorEvaluator.h:27