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
13namespace Eigen {
14
15namespace internal {
16template<typename PatchDim, typename XprType>
17struct traits<TensorPatchOp<PatchDim, 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 + 1;
26 static const int Layout = XprTraits::Layout;
27};
28
29template<typename PatchDim, typename XprType>
30struct eval<TensorPatchOp<PatchDim, XprType>, Eigen::Dense>
31{
32 typedef const TensorPatchOp<PatchDim, XprType>& type;
33};
34
35template<typename PatchDim, typename XprType>
36struct nested<TensorPatchOp<PatchDim, XprType>, 1, typename eval<TensorPatchOp<PatchDim, XprType> >::type>
37{
38 typedef TensorPatchOp<PatchDim, XprType> type;
39};
40
41} // end namespace internal
42
48template <typename PatchDim, typename XprType>
49class TensorPatchOp : public TensorBase<TensorPatchOp<PatchDim, XprType>, ReadOnlyAccessors> {
50 public:
51 typedef typename Eigen::internal::traits<TensorPatchOp>::Scalar Scalar;
52 typedef typename Eigen::NumTraits<Scalar>::Real RealScalar;
53 typedef typename XprType::CoeffReturnType CoeffReturnType;
54 typedef typename Eigen::internal::nested<TensorPatchOp>::type Nested;
55 typedef typename Eigen::internal::traits<TensorPatchOp>::StorageKind StorageKind;
56 typedef typename Eigen::internal::traits<TensorPatchOp>::Index Index;
57
58 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorPatchOp(const XprType& expr, const PatchDim& patch_dims)
59 : m_xpr(expr), m_patch_dims(patch_dims) {}
60
61 EIGEN_DEVICE_FUNC
62 const PatchDim& patch_dims() const { return m_patch_dims; }
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 PatchDim m_patch_dims;
71};
72
73
74// Eval as rvalue
75template<typename PatchDim, typename ArgType, typename Device>
76struct TensorEvaluator<const TensorPatchOp<PatchDim, ArgType>, Device>
77{
79 typedef typename XprType::Index Index;
80 static const int NumDims = internal::array_size<typename TensorEvaluator<ArgType, Device>::Dimensions>::value + 1;
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 = internal::unpacket_traits<PacketReturnType>::size;
86
87
88 enum {
89 IsAligned = false,
90 PacketAccess = TensorEvaluator<ArgType, Device>::PacketAccess,
91 Layout = TensorEvaluator<ArgType, Device>::Layout,
92 CoordAccess = false,
93 RawAccess = false
94 };
95
96 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorEvaluator(const XprType& op, const Device& device)
97 : m_impl(op.expression(), device)
98 {
99 Index num_patches = 1;
100 const typename TensorEvaluator<ArgType, Device>::Dimensions& input_dims = m_impl.dimensions();
101 const PatchDim& patch_dims = op.patch_dims();
102 if (static_cast<int>(Layout) == static_cast<int>(ColMajor)) {
103 for (int i = 0; i < NumDims-1; ++i) {
104 m_dimensions[i] = patch_dims[i];
105 num_patches *= (input_dims[i] - patch_dims[i] + 1);
106 }
107 m_dimensions[NumDims-1] = num_patches;
108
109 m_inputStrides[0] = 1;
110 m_patchStrides[0] = 1;
111 for (int i = 1; i < NumDims-1; ++i) {
112 m_inputStrides[i] = m_inputStrides[i-1] * input_dims[i-1];
113 m_patchStrides[i] = m_patchStrides[i-1] * (input_dims[i-1] - patch_dims[i-1] + 1);
114 }
115 m_outputStrides[0] = 1;
116 for (int i = 1; i < NumDims; ++i) {
117 m_outputStrides[i] = m_outputStrides[i-1] * m_dimensions[i-1];
118 }
119 } else {
120 for (int i = 0; i < NumDims-1; ++i) {
121 m_dimensions[i+1] = patch_dims[i];
122 num_patches *= (input_dims[i] - patch_dims[i] + 1);
123 }
124 m_dimensions[0] = num_patches;
125
126 m_inputStrides[NumDims-2] = 1;
127 m_patchStrides[NumDims-2] = 1;
128 for (int i = NumDims-3; i >= 0; --i) {
129 m_inputStrides[i] = m_inputStrides[i+1] * input_dims[i+1];
130 m_patchStrides[i] = m_patchStrides[i+1] * (input_dims[i+1] - patch_dims[i+1] + 1);
131 }
132 m_outputStrides[NumDims-1] = 1;
133 for (int i = NumDims-2; i >= 0; --i) {
134 m_outputStrides[i] = m_outputStrides[i+1] * m_dimensions[i+1];
135 }
136 }
137 }
138
139 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Dimensions& dimensions() const { return m_dimensions; }
140
141 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE bool evalSubExprsIfNeeded(Scalar* /*data*/) {
142 m_impl.evalSubExprsIfNeeded(NULL);
143 return true;
144 }
145
146 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void cleanup() {
147 m_impl.cleanup();
148 }
149
150 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE CoeffReturnType coeff(Index index) const
151 {
152 Index output_stride_index = (static_cast<int>(Layout) == static_cast<int>(ColMajor)) ? NumDims - 1 : 0;
153 // Find the location of the first element of the patch.
154 Index patchIndex = index / m_outputStrides[output_stride_index];
155 // Find the offset of the element wrt the location of the first element.
156 Index patchOffset = index - patchIndex * m_outputStrides[output_stride_index];
157 Index inputIndex = 0;
158 if (static_cast<int>(Layout) == static_cast<int>(ColMajor)) {
159 for (int i = NumDims - 2; i > 0; --i) {
160 const Index patchIdx = patchIndex / m_patchStrides[i];
161 patchIndex -= patchIdx * m_patchStrides[i];
162 const Index offsetIdx = patchOffset / m_outputStrides[i];
163 patchOffset -= offsetIdx * m_outputStrides[i];
164 inputIndex += (patchIdx + offsetIdx) * m_inputStrides[i];
165 }
166 } else {
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 {
182 EIGEN_STATIC_ASSERT((PacketSize > 1), YOU_MADE_A_PROGRAMMING_MISTAKE)
183 eigen_assert(index+PacketSize-1 < dimensions().TotalSize());
184
185 Index output_stride_index = (static_cast<int>(Layout) == static_cast<int>(ColMajor)) ? NumDims - 1 : 0;
186 Index indices[2] = {index, index + PacketSize - 1};
187 Index patchIndices[2] = {indices[0] / m_outputStrides[output_stride_index],
188 indices[1] / m_outputStrides[output_stride_index]};
189 Index patchOffsets[2] = {indices[0] - patchIndices[0] * m_outputStrides[output_stride_index],
190 indices[1] - patchIndices[1] * m_outputStrides[output_stride_index]};
191
192 Index inputIndices[2] = {0, 0};
193 if (static_cast<int>(Layout) == static_cast<int>(ColMajor)) {
194 for (int i = NumDims - 2; i > 0; --i) {
195 const Index patchIdx[2] = {patchIndices[0] / m_patchStrides[i],
196 patchIndices[1] / m_patchStrides[i]};
197 patchIndices[0] -= patchIdx[0] * m_patchStrides[i];
198 patchIndices[1] -= patchIdx[1] * m_patchStrides[i];
199
200 const Index offsetIdx[2] = {patchOffsets[0] / m_outputStrides[i],
201 patchOffsets[1] / m_outputStrides[i]};
202 patchOffsets[0] -= offsetIdx[0] * m_outputStrides[i];
203 patchOffsets[1] -= offsetIdx[1] * m_outputStrides[i];
204
205 inputIndices[0] += (patchIdx[0] + offsetIdx[0]) * m_inputStrides[i];
206 inputIndices[1] += (patchIdx[1] + offsetIdx[1]) * m_inputStrides[i];
207 }
208 } else {
209 for (int i = 0; i < NumDims - 2; ++i) {
210 const Index patchIdx[2] = {patchIndices[0] / m_patchStrides[i],
211 patchIndices[1] / m_patchStrides[i]};
212 patchIndices[0] -= patchIdx[0] * m_patchStrides[i];
213 patchIndices[1] -= patchIdx[1] * m_patchStrides[i];
214
215 const Index offsetIdx[2] = {patchOffsets[0] / m_outputStrides[i+1],
216 patchOffsets[1] / m_outputStrides[i+1]};
217 patchOffsets[0] -= offsetIdx[0] * m_outputStrides[i+1];
218 patchOffsets[1] -= offsetIdx[1] * m_outputStrides[i+1];
219
220 inputIndices[0] += (patchIdx[0] + offsetIdx[0]) * m_inputStrides[i];
221 inputIndices[1] += (patchIdx[1] + offsetIdx[1]) * m_inputStrides[i];
222 }
223 }
224 inputIndices[0] += (patchIndices[0] + patchOffsets[0]);
225 inputIndices[1] += (patchIndices[1] + patchOffsets[1]);
226
227 if (inputIndices[1] - inputIndices[0] == PacketSize - 1) {
228 PacketReturnType rslt = m_impl.template packet<Unaligned>(inputIndices[0]);
229 return rslt;
230 }
231 else {
232 EIGEN_ALIGN_MAX CoeffReturnType values[PacketSize];
233 values[0] = m_impl.coeff(inputIndices[0]);
234 values[PacketSize-1] = m_impl.coeff(inputIndices[1]);
235 for (int i = 1; i < PacketSize-1; ++i) {
236 values[i] = coeff(index+i);
237 }
238 PacketReturnType rslt = internal::pload<PacketReturnType>(values);
239 return rslt;
240 }
241 }
242
243 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorOpCost costPerCoeff(bool vectorized) const {
244 const double compute_cost = NumDims * (TensorOpCost::DivCost<Index>() +
245 TensorOpCost::MulCost<Index>() +
246 2 * TensorOpCost::AddCost<Index>());
247 return m_impl.costPerCoeff(vectorized) +
248 TensorOpCost(0, 0, compute_cost, vectorized, PacketSize);
249 }
250
251 EIGEN_DEVICE_FUNC Scalar* data() const { return NULL; }
252
253 protected:
254 Dimensions m_dimensions;
255 array<Index, NumDims> m_outputStrides;
256 array<Index, NumDims-1> m_inputStrides;
257 array<Index, NumDims-1> m_patchStrides;
258
259 TensorEvaluator<ArgType, Device> m_impl;
260};
261
262} // end namespace Eigen
263
264#endif // EIGEN_CXX11_TENSOR_TENSOR_PATCH_H
The tensor base class.
Definition TensorForwardDeclarations.h:29
Tensor patch class.
Definition TensorPatch.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