TR-mbed 1.0
Loading...
Searching...
No Matches
TensorPatch.h
Go to the documentation of this file.
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
22namespace internal {
23template<typename PatchDim, typename XprType>
24struct traits<TensorPatchOp<PatchDim, XprType> > : public traits<XprType>
25{
26 typedef typename XprType::Scalar Scalar;
28 typedef typename XprTraits::StorageKind StorageKind;
29 typedef typename XprTraits::Index Index;
30 typedef typename XprType::Nested Nested;
32 static const int NumDimensions = XprTraits::NumDimensions + 1;
33 static const int Layout = XprTraits::Layout;
34 typedef typename XprTraits::PointerType PointerType;
35};
36
37template<typename PatchDim, typename XprType>
42
43template<typename PatchDim, typename XprType>
48
49} // end namespace internal
50
51
52
53template<typename PatchDim, typename XprType>
54class TensorPatchOp : public TensorBase<TensorPatchOp<PatchDim, XprType>, ReadOnlyAccessors>
55{
56 public:
59 typedef typename XprType::CoeffReturnType CoeffReturnType;
63
66
68 const PatchDim& patch_dims() const { return m_patch_dims; }
69
72 expression() const { return m_xpr; }
73
74 protected:
75 typename XprType::Nested m_xpr;
76 const PatchDim m_patch_dims;
77};
78
79
80// Eval as rvalue
81template<typename PatchDim, typename ArgType, typename Device>
82struct TensorEvaluator<const TensorPatchOp<PatchDim, ArgType>, Device>
83{
85 typedef typename XprType::Index Index;
88 typedef typename XprType::Scalar Scalar;
94
95
96 enum {
97 IsAligned = false,
99 BlockAccess = false,
102 CoordAccess = false,
103 RawAccess = false
104 };
105
106 //===- Tensor block evaluation strategy (see TensorBlock.h) -------------===//
108 //===--------------------------------------------------------------------===//
109
110 EIGEN_STRONG_INLINE TensorEvaluator(const XprType& op, const Device& device)
111 : m_impl(op.expression(), device)
112 {
113 Index num_patches = 1;
114 const typename TensorEvaluator<ArgType, Device>::Dimensions& input_dims = m_impl.dimensions();
115 const PatchDim& patch_dims = op.patch_dims();
116 if (static_cast<int>(Layout) == static_cast<int>(ColMajor)) {
117 for (int i = 0; i < NumDims-1; ++i) {
118 m_dimensions[i] = patch_dims[i];
119 num_patches *= (input_dims[i] - patch_dims[i] + 1);
120 }
121 m_dimensions[NumDims-1] = num_patches;
122
123 m_inputStrides[0] = 1;
124 m_patchStrides[0] = 1;
125 for (int i = 1; i < NumDims-1; ++i) {
126 m_inputStrides[i] = m_inputStrides[i-1] * input_dims[i-1];
127 m_patchStrides[i] = m_patchStrides[i-1] * (input_dims[i-1] - patch_dims[i-1] + 1);
128 }
129 m_outputStrides[0] = 1;
130 for (int i = 1; i < NumDims; ++i) {
131 m_outputStrides[i] = m_outputStrides[i-1] * m_dimensions[i-1];
132 }
133 } else {
134 for (int i = 0; i < NumDims-1; ++i) {
135 m_dimensions[i+1] = patch_dims[i];
136 num_patches *= (input_dims[i] - patch_dims[i] + 1);
137 }
138 m_dimensions[0] = num_patches;
139
140 m_inputStrides[NumDims-2] = 1;
141 m_patchStrides[NumDims-2] = 1;
142 for (int i = NumDims-3; i >= 0; --i) {
143 m_inputStrides[i] = m_inputStrides[i+1] * input_dims[i+1];
144 m_patchStrides[i] = m_patchStrides[i+1] * (input_dims[i+1] - patch_dims[i+1] + 1);
145 }
146 m_outputStrides[NumDims-1] = 1;
147 for (int i = NumDims-2; i >= 0; --i) {
148 m_outputStrides[i] = m_outputStrides[i+1] * m_dimensions[i+1];
149 }
150 }
151 }
152
153 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Dimensions& dimensions() const { return m_dimensions; }
154
156 m_impl.evalSubExprsIfNeeded(NULL);
157 return true;
158 }
159
161 m_impl.cleanup();
162 }
163
165 {
166 Index output_stride_index = (static_cast<int>(Layout) == static_cast<int>(ColMajor)) ? NumDims - 1 : 0;
167 // Find the location of the first element of the patch.
168 Index patchIndex = index / m_outputStrides[output_stride_index];
169 // Find the offset of the element wrt the location of the first element.
170 Index patchOffset = index - patchIndex * m_outputStrides[output_stride_index];
171 Index inputIndex = 0;
172 if (static_cast<int>(Layout) == static_cast<int>(ColMajor)) {
174 for (int i = NumDims - 2; i > 0; --i) {
175 const Index patchIdx = patchIndex / m_patchStrides[i];
176 patchIndex -= patchIdx * m_patchStrides[i];
177 const Index offsetIdx = patchOffset / m_outputStrides[i];
178 patchOffset -= offsetIdx * m_outputStrides[i];
179 inputIndex += (patchIdx + offsetIdx) * m_inputStrides[i];
180 }
181 } else {
183 for (int i = 0; i < NumDims - 2; ++i) {
184 const Index patchIdx = patchIndex / m_patchStrides[i];
185 patchIndex -= patchIdx * m_patchStrides[i];
186 const Index offsetIdx = patchOffset / m_outputStrides[i+1];
187 patchOffset -= offsetIdx * m_outputStrides[i+1];
188 inputIndex += (patchIdx + offsetIdx) * m_inputStrides[i];
189 }
190 }
191 inputIndex += (patchIndex + patchOffset);
192 return m_impl.coeff(inputIndex);
193 }
194
195 template<int LoadMode>
197 {
198 EIGEN_STATIC_ASSERT((PacketSize > 1), YOU_MADE_A_PROGRAMMING_MISTAKE)
199 eigen_assert(index+PacketSize-1 < dimensions().TotalSize());
200
201 Index output_stride_index = (static_cast<int>(Layout) == static_cast<int>(ColMajor)) ? NumDims - 1 : 0;
202 Index indices[2] = {index, index + PacketSize - 1};
203 Index patchIndices[2] = {indices[0] / m_outputStrides[output_stride_index],
204 indices[1] / m_outputStrides[output_stride_index]};
205 Index patchOffsets[2] = {indices[0] - patchIndices[0] * m_outputStrides[output_stride_index],
206 indices[1] - patchIndices[1] * m_outputStrides[output_stride_index]};
207
208 Index inputIndices[2] = {0, 0};
209 if (static_cast<int>(Layout) == static_cast<int>(ColMajor)) {
211 for (int i = NumDims - 2; i > 0; --i) {
212 const Index patchIdx[2] = {patchIndices[0] / m_patchStrides[i],
213 patchIndices[1] / m_patchStrides[i]};
214 patchIndices[0] -= patchIdx[0] * m_patchStrides[i];
215 patchIndices[1] -= patchIdx[1] * m_patchStrides[i];
216
217 const Index offsetIdx[2] = {patchOffsets[0] / m_outputStrides[i],
218 patchOffsets[1] / m_outputStrides[i]};
219 patchOffsets[0] -= offsetIdx[0] * m_outputStrides[i];
220 patchOffsets[1] -= offsetIdx[1] * m_outputStrides[i];
221
222 inputIndices[0] += (patchIdx[0] + offsetIdx[0]) * m_inputStrides[i];
223 inputIndices[1] += (patchIdx[1] + offsetIdx[1]) * m_inputStrides[i];
224 }
225 } else {
227 for (int i = 0; i < NumDims - 2; ++i) {
228 const Index patchIdx[2] = {patchIndices[0] / m_patchStrides[i],
229 patchIndices[1] / m_patchStrides[i]};
230 patchIndices[0] -= patchIdx[0] * m_patchStrides[i];
231 patchIndices[1] -= patchIdx[1] * m_patchStrides[i];
232
233 const Index offsetIdx[2] = {patchOffsets[0] / m_outputStrides[i+1],
234 patchOffsets[1] / m_outputStrides[i+1]};
235 patchOffsets[0] -= offsetIdx[0] * m_outputStrides[i+1];
236 patchOffsets[1] -= offsetIdx[1] * m_outputStrides[i+1];
237
238 inputIndices[0] += (patchIdx[0] + offsetIdx[0]) * m_inputStrides[i];
239 inputIndices[1] += (patchIdx[1] + offsetIdx[1]) * m_inputStrides[i];
240 }
241 }
242 inputIndices[0] += (patchIndices[0] + patchOffsets[0]);
243 inputIndices[1] += (patchIndices[1] + patchOffsets[1]);
244
245 if (inputIndices[1] - inputIndices[0] == PacketSize - 1) {
246 PacketReturnType rslt = m_impl.template packet<Unaligned>(inputIndices[0]);
247 return rslt;
248 }
249 else {
251 values[0] = m_impl.coeff(inputIndices[0]);
252 values[PacketSize-1] = m_impl.coeff(inputIndices[1]);
254 for (int i = 1; i < PacketSize-1; ++i) {
255 values[i] = coeff(index+i);
256 }
258 return rslt;
259 }
260 }
261
263 const double compute_cost = NumDims * (TensorOpCost::DivCost<Index>() +
264 TensorOpCost::MulCost<Index>() +
265 2 * TensorOpCost::AddCost<Index>());
266 return m_impl.costPerCoeff(vectorized) +
267 TensorOpCost(0, 0, compute_cost, vectorized, PacketSize);
268 }
269
271
272#ifdef EIGEN_USE_SYCL
273 // binding placeholder accessors to a command group handler for SYCL
274 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void bind(cl::sycl::handler &cgh) const {
275 m_impl.bind(cgh);
276 }
277#endif
278
279 protected:
284
286
287};
288
289} // end namespace Eigen
290
291#endif // EIGEN_CXX11_TENSOR_TENSOR_PATCH_H
int i
Definition BiCGSTAB_step_by_step.cpp:9
#define EIGEN_ALIGN_MAX
Definition ConfigureVectorization.h:157
#define EIGEN_UNROLL_LOOP
Definition Macros.h:1461
#define EIGEN_DEVICE_FUNC
Definition Macros.h:976
#define eigen_assert(x)
Definition Macros.h:1037
#define EIGEN_STRONG_INLINE
Definition Macros.h:917
#define EIGEN_STATIC_ASSERT(CONDITION, MSG)
Definition StaticAssert.h:127
Generic expression where a coefficient-wise binary operator is applied to two expressions.
Definition CwiseBinaryOp.h:84
The tensor base class.
Definition TensorBase.h:973
Definition TensorCostModel.h:25
Definition TensorPatch.h:55
Eigen::internal::traits< TensorPatchOp >::StorageKind StorageKind
Definition TensorPatch.h:61
EIGEN_DEVICE_FUNC const PatchDim & patch_dims() const
Definition TensorPatch.h:68
const PatchDim m_patch_dims
Definition TensorPatch.h:76
EIGEN_DEVICE_FUNC const internal::remove_all< typenameXprType::Nested >::type & expression() const
Definition TensorPatch.h:72
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorPatchOp(const XprType &expr, const PatchDim &patch_dims)
Definition TensorPatch.h:64
Eigen::internal::traits< TensorPatchOp >::Index Index
Definition TensorPatch.h:62
Eigen::internal::traits< TensorPatchOp >::Scalar Scalar
Definition TensorPatch.h:57
Eigen::NumTraits< Scalar >::Real RealScalar
Definition TensorPatch.h:58
XprType::CoeffReturnType CoeffReturnType
Definition TensorPatch.h:59
Eigen::internal::nested< TensorPatchOp >::type Nested
Definition TensorPatch.h:60
XprType::Nested m_xpr
Definition TensorPatch.h:75
Definition EmulateArray.h:21
Definition TensorBlock.h:617
@ ColMajor
Definition Constants.h:319
Namespace containing all symbols from the Eigen library.
Definition bench_norm.cpp:85
Definition BandTriangularSolver.h:13
Definition TensorDimensions.h:263
Definition Constants.h:507
Definition TensorMeta.h:50
Definition TensorForwardDeclarations.h:37
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorOpCost costPerCoeff(bool vectorized) const
Definition TensorPatch.h:262
EIGEN_STRONG_INLINE void cleanup()
Definition TensorPatch.h:160
DSizes< Index, NumDims > Dimensions
Definition TensorPatch.h:87
TensorEvaluator< ArgType, Device > m_impl
Definition TensorPatch.h:285
EIGEN_STRONG_INLINE TensorEvaluator(const XprType &op, const Device &device)
Definition TensorPatch.h:110
TensorPatchOp< PatchDim, ArgType > XprType
Definition TensorPatch.h:84
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Dimensions & dimensions() const
Definition TensorPatch.h:153
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE PacketReturnType packet(Index index) const
Definition TensorPatch.h:196
array< Index, NumDims > m_outputStrides
Definition TensorPatch.h:281
XprType::CoeffReturnType CoeffReturnType
Definition TensorPatch.h:89
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE CoeffReturnType coeff(Index index) const
Definition TensorPatch.h:164
StorageMemory< CoeffReturnType, Device > Storage
Definition TensorPatch.h:92
EIGEN_STRONG_INLINE bool evalSubExprsIfNeeded(EvaluatorPointerType)
Definition TensorPatch.h:155
PacketType< CoeffReturnType, Device >::type PacketReturnType
Definition TensorPatch.h:90
array< Index, NumDims-1 > m_inputStrides
Definition TensorPatch.h:282
EIGEN_DEVICE_FUNC EvaluatorPointerType data() const
Definition TensorPatch.h:270
array< Index, NumDims-1 > m_patchStrides
Definition TensorPatch.h:283
internal::TensorBlockNotImplemented TensorBlock
Definition TensorPatch.h:107
A cost model used to limit the number of threads used for evaluating tensor expression.
Definition TensorEvaluator.h:29
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Dimensions & dimensions() const
Definition TensorEvaluator.h:73
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE CoeffReturnType coeff(Index index) const
Definition TensorEvaluator.h:94
@ BlockAccess
Definition TensorEvaluator.h:48
@ PreferBlockAccess
Definition TensorEvaluator.h:49
@ PacketAccess
Definition TensorEvaluator.h:47
@ Layout
Definition TensorEvaluator.h:50
@ IsAligned
Definition TensorEvaluator.h:46
Derived::Index Index
Definition TensorEvaluator.h:30
Derived::Dimensions Dimensions
Definition TensorEvaluator.h:34
static const int PacketSize
Definition TensorEvaluator.h:36
const TensorPatchOp< PatchDim, XprType > & type
Definition TensorPatch.h:40
Definition XprHelper.h:332
Definition TensorTraits.h:175
XprType::Scalar Scalar
Definition TensorPatch.h:26
XprTraits::StorageKind StorageKind
Definition TensorPatch.h:28
XprTraits::Index Index
Definition TensorPatch.h:29
XprType::Nested Nested
Definition TensorPatch.h:30
remove_reference< Nested >::type _Nested
Definition TensorPatch.h:31
XprTraits::PointerType PointerType
Definition TensorPatch.h:34
traits< XprType > XprTraits
Definition TensorPatch.h:27
Definition ForwardDeclarations.h:17