Eigen-unsupported  5.0.1-dev+284dcc12
 
Loading...
Searching...
No Matches
TensorPatch.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_PATCH_H
11#define EIGEN_CXX11_TENSOR_TENSOR_PATCH_H
12
13// IWYU pragma: private
14#include "./InternalHeaderCheck.h"
15
16namespace Eigen {
17
18namespace internal {
19template <typename PatchDim, typename XprType>
20struct traits<TensorPatchOp<PatchDim, XprType> > : public traits<XprType> {
21 typedef typename XprType::Scalar Scalar;
22 typedef traits<XprType> XprTraits;
23 typedef typename XprTraits::StorageKind StorageKind;
24 typedef typename XprTraits::Index Index;
25 typedef typename XprType::Nested Nested;
26 typedef std::remove_reference_t<Nested> Nested_;
27 static constexpr int NumDimensions = XprTraits::NumDimensions + 1;
28 static constexpr int Layout = XprTraits::Layout;
29 typedef typename XprTraits::PointerType PointerType;
30};
31
32template <typename PatchDim, typename XprType>
33struct eval<TensorPatchOp<PatchDim, XprType>, Eigen::Dense> {
34 typedef const TensorPatchOp<PatchDim, XprType>& type;
35};
36
37template <typename PatchDim, typename XprType>
38struct nested<TensorPatchOp<PatchDim, XprType>, 1, typename eval<TensorPatchOp<PatchDim, XprType> >::type> {
39 typedef TensorPatchOp<PatchDim, XprType> type;
40};
41
42} // end namespace internal
43
49template <typename PatchDim, typename XprType>
50class TensorPatchOp : public TensorBase<TensorPatchOp<PatchDim, XprType>, ReadOnlyAccessors> {
51 public:
52 typedef typename Eigen::internal::traits<TensorPatchOp>::Scalar Scalar;
53 typedef typename Eigen::NumTraits<Scalar>::Real RealScalar;
54 typedef typename XprType::CoeffReturnType CoeffReturnType;
55 typedef typename Eigen::internal::nested<TensorPatchOp>::type Nested;
56 typedef typename Eigen::internal::traits<TensorPatchOp>::StorageKind StorageKind;
57 typedef typename Eigen::internal::traits<TensorPatchOp>::Index Index;
58
59 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorPatchOp(const XprType& expr, const PatchDim& patch_dims)
60 : m_xpr(expr), m_patch_dims(patch_dims) {}
61
62 EIGEN_DEVICE_FUNC const PatchDim& patch_dims() const { return m_patch_dims; }
63
64 EIGEN_DEVICE_FUNC const internal::remove_all_t<typename XprType::Nested>& expression() const { return m_xpr; }
65
66 protected:
67 typename XprType::Nested m_xpr;
68 const PatchDim m_patch_dims;
69};
70
71// Eval as rvalue
72template <typename PatchDim, typename ArgType, typename Device>
73struct TensorEvaluator<const TensorPatchOp<PatchDim, ArgType>, Device> {
75 typedef typename XprType::Index Index;
76 static constexpr int NumDims = internal::array_size<typename TensorEvaluator<ArgType, Device>::Dimensions>::value + 1;
77 typedef DSizes<Index, NumDims> Dimensions;
78 typedef typename XprType::Scalar Scalar;
79 typedef typename XprType::CoeffReturnType CoeffReturnType;
80 typedef typename PacketType<CoeffReturnType, Device>::type PacketReturnType;
81 static constexpr int PacketSize = PacketType<CoeffReturnType, Device>::size;
82 typedef StorageMemory<CoeffReturnType, Device> Storage;
83 typedef typename Storage::Type EvaluatorPointerType;
84
85 static constexpr int Layout = TensorEvaluator<ArgType, Device>::Layout;
86 enum {
87 IsAligned = false,
88 PacketAccess = TensorEvaluator<ArgType, Device>::PacketAccess,
89 BlockAccess = false,
91 CoordAccess = false,
92 RawAccess = false
93 };
94
95 //===- Tensor block evaluation strategy (see TensorBlock.h) -------------===//
96 typedef internal::TensorBlockNotImplemented TensorBlock;
97 //===--------------------------------------------------------------------===//
98
99 EIGEN_STRONG_INLINE TensorEvaluator(const XprType& op, const Device& device) : m_impl(op.expression(), device) {
100 Index num_patches = 1;
101 const typename TensorEvaluator<ArgType, Device>::Dimensions& input_dims = m_impl.dimensions();
102 const PatchDim& patch_dims = op.patch_dims();
103 if (static_cast<int>(Layout) == static_cast<int>(ColMajor)) {
104 for (int i = 0; i < NumDims - 1; ++i) {
105 m_dimensions[i] = patch_dims[i];
106 num_patches *= (input_dims[i] - patch_dims[i] + 1);
107 }
108 m_dimensions[NumDims - 1] = num_patches;
109
110 m_inputStrides[0] = 1;
111 m_patchStrides[0] = 1;
112 for (int i = 1; i < NumDims - 1; ++i) {
113 m_inputStrides[i] = m_inputStrides[i - 1] * input_dims[i - 1];
114 m_patchStrides[i] = m_patchStrides[i - 1] * (input_dims[i - 1] - patch_dims[i - 1] + 1);
115 }
116 m_outputStrides[0] = 1;
117 for (int i = 1; i < NumDims; ++i) {
118 m_outputStrides[i] = m_outputStrides[i - 1] * m_dimensions[i - 1];
119 }
120 } else {
121 for (int i = 0; i < NumDims - 1; ++i) {
122 m_dimensions[i + 1] = patch_dims[i];
123 num_patches *= (input_dims[i] - patch_dims[i] + 1);
124 }
125 m_dimensions[0] = num_patches;
126
127 m_inputStrides[NumDims - 2] = 1;
128 m_patchStrides[NumDims - 2] = 1;
129 for (int i = NumDims - 3; i >= 0; --i) {
130 m_inputStrides[i] = m_inputStrides[i + 1] * input_dims[i + 1];
131 m_patchStrides[i] = m_patchStrides[i + 1] * (input_dims[i + 1] - patch_dims[i + 1] + 1);
132 }
133 m_outputStrides[NumDims - 1] = 1;
134 for (int i = NumDims - 2; i >= 0; --i) {
135 m_outputStrides[i] = m_outputStrides[i + 1] * m_dimensions[i + 1];
136 }
137 }
138 }
139
140 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Dimensions& dimensions() const { return m_dimensions; }
141
142 EIGEN_STRONG_INLINE bool evalSubExprsIfNeeded(EvaluatorPointerType /*data*/) {
143 m_impl.evalSubExprsIfNeeded(NULL);
144 return true;
145 }
146
147 EIGEN_STRONG_INLINE void cleanup() { m_impl.cleanup(); }
148
149 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE CoeffReturnType coeff(Index index) const {
150 Index output_stride_index = (static_cast<int>(Layout) == static_cast<int>(ColMajor)) ? NumDims - 1 : 0;
151 // Find the location of the first element of the patch.
152 Index patchIndex = index / m_outputStrides[output_stride_index];
153 // Find the offset of the element wrt the location of the first element.
154 Index patchOffset = index - patchIndex * m_outputStrides[output_stride_index];
155 Index inputIndex = 0;
156 if (static_cast<int>(Layout) == static_cast<int>(ColMajor)) {
157 EIGEN_UNROLL_LOOP
158 for (int i = NumDims - 2; i > 0; --i) {
159 const Index patchIdx = patchIndex / m_patchStrides[i];
160 patchIndex -= patchIdx * m_patchStrides[i];
161 const Index offsetIdx = patchOffset / m_outputStrides[i];
162 patchOffset -= offsetIdx * m_outputStrides[i];
163 inputIndex += (patchIdx + offsetIdx) * m_inputStrides[i];
164 }
165 } else {
166 EIGEN_UNROLL_LOOP
167 for (int i = 0; i < NumDims - 2; ++i) {
168 const Index patchIdx = patchIndex / m_patchStrides[i];
169 patchIndex -= patchIdx * m_patchStrides[i];
170 const Index offsetIdx = patchOffset / m_outputStrides[i + 1];
171 patchOffset -= offsetIdx * m_outputStrides[i + 1];
172 inputIndex += (patchIdx + offsetIdx) * m_inputStrides[i];
173 }
174 }
175 inputIndex += (patchIndex + patchOffset);
176 return m_impl.coeff(inputIndex);
177 }
178
179 template <int LoadMode>
180 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE PacketReturnType packet(Index index) const {
181 eigen_assert(index + PacketSize - 1 < dimensions().TotalSize());
182
183 Index output_stride_index = (static_cast<int>(Layout) == static_cast<int>(ColMajor)) ? NumDims - 1 : 0;
184 Index indices[2] = {index, index + PacketSize - 1};
185 Index patchIndices[2] = {indices[0] / m_outputStrides[output_stride_index],
186 indices[1] / m_outputStrides[output_stride_index]};
187 Index patchOffsets[2] = {indices[0] - patchIndices[0] * m_outputStrides[output_stride_index],
188 indices[1] - patchIndices[1] * m_outputStrides[output_stride_index]};
189
190 Index inputIndices[2] = {0, 0};
191 if (static_cast<int>(Layout) == static_cast<int>(ColMajor)) {
192 EIGEN_UNROLL_LOOP
193 for (int i = NumDims - 2; i > 0; --i) {
194 const Index patchIdx[2] = {patchIndices[0] / m_patchStrides[i], patchIndices[1] / m_patchStrides[i]};
195 patchIndices[0] -= patchIdx[0] * m_patchStrides[i];
196 patchIndices[1] -= patchIdx[1] * m_patchStrides[i];
197
198 const Index offsetIdx[2] = {patchOffsets[0] / m_outputStrides[i], patchOffsets[1] / m_outputStrides[i]};
199 patchOffsets[0] -= offsetIdx[0] * m_outputStrides[i];
200 patchOffsets[1] -= offsetIdx[1] * m_outputStrides[i];
201
202 inputIndices[0] += (patchIdx[0] + offsetIdx[0]) * m_inputStrides[i];
203 inputIndices[1] += (patchIdx[1] + offsetIdx[1]) * m_inputStrides[i];
204 }
205 } else {
206 EIGEN_UNROLL_LOOP
207 for (int i = 0; i < NumDims - 2; ++i) {
208 const Index patchIdx[2] = {patchIndices[0] / m_patchStrides[i], patchIndices[1] / m_patchStrides[i]};
209 patchIndices[0] -= patchIdx[0] * m_patchStrides[i];
210 patchIndices[1] -= patchIdx[1] * m_patchStrides[i];
211
212 const Index offsetIdx[2] = {patchOffsets[0] / m_outputStrides[i + 1], patchOffsets[1] / m_outputStrides[i + 1]};
213 patchOffsets[0] -= offsetIdx[0] * m_outputStrides[i + 1];
214 patchOffsets[1] -= offsetIdx[1] * m_outputStrides[i + 1];
215
216 inputIndices[0] += (patchIdx[0] + offsetIdx[0]) * m_inputStrides[i];
217 inputIndices[1] += (patchIdx[1] + offsetIdx[1]) * m_inputStrides[i];
218 }
219 }
220 inputIndices[0] += (patchIndices[0] + patchOffsets[0]);
221 inputIndices[1] += (patchIndices[1] + patchOffsets[1]);
222
223 if (inputIndices[1] - inputIndices[0] == PacketSize - 1) {
224 PacketReturnType rslt = m_impl.template packet<Unaligned>(inputIndices[0]);
225 return rslt;
226 } else {
227 EIGEN_ALIGN_MAX CoeffReturnType values[PacketSize];
228 values[0] = m_impl.coeff(inputIndices[0]);
229 values[PacketSize - 1] = m_impl.coeff(inputIndices[1]);
230 EIGEN_UNROLL_LOOP
231 for (int i = 1; i < PacketSize - 1; ++i) {
232 values[i] = coeff(index + i);
233 }
234 PacketReturnType rslt = internal::pload<PacketReturnType>(values);
235 return rslt;
236 }
237 }
238
239 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorOpCost costPerCoeff(bool vectorized) const {
240 const double compute_cost = NumDims * (TensorOpCost::DivCost<Index>() + TensorOpCost::MulCost<Index>() +
241 2 * TensorOpCost::AddCost<Index>());
242 return m_impl.costPerCoeff(vectorized) + TensorOpCost(0, 0, compute_cost, vectorized, PacketSize);
243 }
244
245 EIGEN_DEVICE_FUNC EvaluatorPointerType data() const { return NULL; }
246
247 protected:
248 Dimensions m_dimensions;
249 array<Index, NumDims> m_outputStrides;
250 array<Index, NumDims - 1> m_inputStrides;
251 array<Index, NumDims - 1> m_patchStrides;
252
253 TensorEvaluator<ArgType, Device> m_impl;
254};
255
256} // end namespace Eigen
257
258#endif // EIGEN_CXX11_TENSOR_TENSOR_PATCH_H
The tensor base class.
Definition TensorForwardDeclarations.h:68
Tensor patch class.
Definition TensorPatch.h:50
Namespace containing all symbols from the Eigen library.
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The tensor evaluator class.
Definition TensorEvaluator.h:30