Loading...
Searching...
No Matches
TensorVolumePatch.h
1// This file is part of Eigen, a lightweight C++ template library
2// for linear algebra.
3
4#ifndef EIGEN_CXX11_TENSOR_TENSOR_VOLUME_PATCH_H
5#define EIGEN_CXX11_TENSOR_TENSOR_VOLUME_PATCH_H
6
7namespace Eigen {
8
9namespace internal {
10template<DenseIndex Planes, DenseIndex Rows, DenseIndex Cols, typename XprType>
11struct traits<TensorVolumePatchOp<Planes, Rows, Cols, XprType> > : public traits<XprType>
12{
13 typedef typename internal::remove_const<typename XprType::Scalar>::type Scalar;
14 typedef traits<XprType> XprTraits;
15 typedef typename XprTraits::StorageKind StorageKind;
16 typedef typename XprTraits::Index Index;
17 typedef typename XprType::Nested Nested;
18 typedef typename remove_reference<Nested>::type _Nested;
19 static const int NumDimensions = XprTraits::NumDimensions + 1;
20 static const int Layout = XprTraits::Layout;
21};
22
23template<DenseIndex Planes, DenseIndex Rows, DenseIndex Cols, typename XprType>
24struct eval<TensorVolumePatchOp<Planes, Rows, Cols, XprType>, Eigen::Dense>
25{
26 typedef const TensorVolumePatchOp<Planes, Rows, Cols, XprType>& type;
27};
28
29template<DenseIndex Planes, DenseIndex Rows, DenseIndex Cols, typename XprType>
30struct nested<TensorVolumePatchOp<Planes, Rows, Cols, XprType>, 1, typename eval<TensorVolumePatchOp<Planes, Rows, Cols, XprType> >::type>
31{
32 typedef TensorVolumePatchOp<Planes, Rows, Cols, XprType> type;
33};
34
35} // end namespace internal
36
52template <DenseIndex Planes, DenseIndex Rows, DenseIndex Cols, typename XprType>
53class TensorVolumePatchOp : public TensorBase<TensorVolumePatchOp<Planes, Rows, Cols, XprType>, ReadOnlyAccessors> {
54 public:
55 typedef typename Eigen::internal::traits<TensorVolumePatchOp>::Scalar Scalar;
56 typedef typename Eigen::NumTraits<Scalar>::Real RealScalar;
57 typedef typename XprType::CoeffReturnType CoeffReturnType;
58 typedef typename Eigen::internal::nested<TensorVolumePatchOp>::type Nested;
59 typedef typename Eigen::internal::traits<TensorVolumePatchOp>::StorageKind StorageKind;
60 typedef typename Eigen::internal::traits<TensorVolumePatchOp>::Index Index;
61
62 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorVolumePatchOp(const XprType& expr, DenseIndex patch_planes, DenseIndex patch_rows, DenseIndex patch_cols,
63 DenseIndex plane_strides, DenseIndex row_strides, DenseIndex col_strides,
64 DenseIndex in_plane_strides, DenseIndex in_row_strides, DenseIndex in_col_strides,
65 DenseIndex plane_inflate_strides, DenseIndex row_inflate_strides, DenseIndex col_inflate_strides,
66 PaddingType padding_type, Scalar padding_value)
67 : m_xpr(expr), m_patch_planes(patch_planes), m_patch_rows(patch_rows), m_patch_cols(patch_cols),
68 m_plane_strides(plane_strides), m_row_strides(row_strides), m_col_strides(col_strides),
69 m_in_plane_strides(in_plane_strides), m_in_row_strides(in_row_strides), m_in_col_strides(in_col_strides),
70 m_plane_inflate_strides(plane_inflate_strides), m_row_inflate_strides(row_inflate_strides), m_col_inflate_strides(col_inflate_strides),
71 m_padding_explicit(false), m_padding_top_z(0), m_padding_bottom_z(0), m_padding_top(0), m_padding_bottom(0), m_padding_left(0), m_padding_right(0),
72 m_padding_type(padding_type), m_padding_value(padding_value) {}
73
74 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorVolumePatchOp(const XprType& expr, DenseIndex patch_planes, DenseIndex patch_rows, DenseIndex patch_cols,
75 DenseIndex plane_strides, DenseIndex row_strides, DenseIndex col_strides,
76 DenseIndex in_plane_strides, DenseIndex in_row_strides, DenseIndex in_col_strides,
77 DenseIndex plane_inflate_strides, DenseIndex row_inflate_strides, DenseIndex col_inflate_strides,
78 DenseIndex padding_top_z, DenseIndex padding_bottom_z,
79 DenseIndex padding_top, DenseIndex padding_bottom,
80 DenseIndex padding_left, DenseIndex padding_right,
81 Scalar padding_value)
82 : m_xpr(expr), m_patch_planes(patch_planes), m_patch_rows(patch_rows), m_patch_cols(patch_cols),
83 m_plane_strides(plane_strides), m_row_strides(row_strides), m_col_strides(col_strides),
84 m_in_plane_strides(in_plane_strides), m_in_row_strides(in_row_strides), m_in_col_strides(in_col_strides),
85 m_plane_inflate_strides(plane_inflate_strides), m_row_inflate_strides(row_inflate_strides), m_col_inflate_strides(col_inflate_strides),
86 m_padding_explicit(true), m_padding_top_z(padding_top_z), m_padding_bottom_z(padding_bottom_z), m_padding_top(padding_top), m_padding_bottom(padding_bottom),
87 m_padding_left(padding_left), m_padding_right(padding_right),
88 m_padding_type(PADDING_VALID), m_padding_value(padding_value) {}
89
90 EIGEN_DEVICE_FUNC
91 DenseIndex patch_planes() const { return m_patch_planes; }
92 EIGEN_DEVICE_FUNC
93 DenseIndex patch_rows() const { return m_patch_rows; }
94 EIGEN_DEVICE_FUNC
95 DenseIndex patch_cols() const { return m_patch_cols; }
96 EIGEN_DEVICE_FUNC
97 DenseIndex plane_strides() const { return m_plane_strides; }
98 EIGEN_DEVICE_FUNC
99 DenseIndex row_strides() const { return m_row_strides; }
100 EIGEN_DEVICE_FUNC
101 DenseIndex col_strides() const { return m_col_strides; }
102 EIGEN_DEVICE_FUNC
103 DenseIndex in_plane_strides() const { return m_in_plane_strides; }
104 EIGEN_DEVICE_FUNC
105 DenseIndex in_row_strides() const { return m_in_row_strides; }
106 EIGEN_DEVICE_FUNC
107 DenseIndex in_col_strides() const { return m_in_col_strides; }
108 EIGEN_DEVICE_FUNC
109 DenseIndex plane_inflate_strides() const { return m_plane_inflate_strides; }
110 EIGEN_DEVICE_FUNC
111 DenseIndex row_inflate_strides() const { return m_row_inflate_strides; }
112 EIGEN_DEVICE_FUNC
113 DenseIndex col_inflate_strides() const { return m_col_inflate_strides; }
114 EIGEN_DEVICE_FUNC
115 bool padding_explicit() const { return m_padding_explicit; }
116 EIGEN_DEVICE_FUNC
117 DenseIndex padding_top_z() const { return m_padding_top_z; }
118 EIGEN_DEVICE_FUNC
119 DenseIndex padding_bottom_z() const { return m_padding_bottom_z; }
120 EIGEN_DEVICE_FUNC
121 DenseIndex padding_top() const { return m_padding_top; }
122 EIGEN_DEVICE_FUNC
123 DenseIndex padding_bottom() const { return m_padding_bottom; }
124 EIGEN_DEVICE_FUNC
125 DenseIndex padding_left() const { return m_padding_left; }
126 EIGEN_DEVICE_FUNC
127 DenseIndex padding_right() const { return m_padding_right; }
128 EIGEN_DEVICE_FUNC
129 PaddingType padding_type() const { return m_padding_type; }
130 EIGEN_DEVICE_FUNC
131 Scalar padding_value() const { return m_padding_value; }
132
133 EIGEN_DEVICE_FUNC
134 const typename internal::remove_all<typename XprType::Nested>::type&
135 expression() const { return m_xpr; }
136
137 protected:
138 typename XprType::Nested m_xpr;
139 const DenseIndex m_patch_planes;
140 const DenseIndex m_patch_rows;
141 const DenseIndex m_patch_cols;
142 const DenseIndex m_plane_strides;
143 const DenseIndex m_row_strides;
144 const DenseIndex m_col_strides;
145 const DenseIndex m_in_plane_strides;
146 const DenseIndex m_in_row_strides;
147 const DenseIndex m_in_col_strides;
148 const DenseIndex m_plane_inflate_strides;
149 const DenseIndex m_row_inflate_strides;
150 const DenseIndex m_col_inflate_strides;
151 const bool m_padding_explicit;
152 const DenseIndex m_padding_top_z;
153 const DenseIndex m_padding_bottom_z;
154 const DenseIndex m_padding_top;
155 const DenseIndex m_padding_bottom;
156 const DenseIndex m_padding_left;
157 const DenseIndex m_padding_right;
158 const PaddingType m_padding_type;
159 const Scalar m_padding_value;
160};
161
162
163// Eval as rvalue
164template<DenseIndex Planes, DenseIndex Rows, DenseIndex Cols, typename ArgType, typename Device>
165struct TensorEvaluator<const TensorVolumePatchOp<Planes, Rows, Cols, ArgType>, Device>
166{
168 typedef typename XprType::Index Index;
169 static const int NumInputDims = internal::array_size<typename TensorEvaluator<ArgType, Device>::Dimensions>::value;
170 static const int NumDims = NumInputDims + 1;
171 typedef DSizes<Index, NumDims> Dimensions;
172 typedef typename internal::remove_const<typename XprType::Scalar>::type Scalar;
173 typedef typename XprType::CoeffReturnType CoeffReturnType;
174 typedef typename PacketType<CoeffReturnType, Device>::type PacketReturnType;
175 static const int PacketSize = internal::unpacket_traits<PacketReturnType>::size;
176
177 enum {
178 IsAligned = false,
179 PacketAccess = TensorEvaluator<ArgType, Device>::PacketAccess,
180 BlockAccess = false,
181 Layout = TensorEvaluator<ArgType, Device>::Layout,
182 CoordAccess = false,
183 RawAccess = false
184 };
185
186 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorEvaluator(const XprType& op, const Device& device)
187 : m_impl(op.expression(), device)
188 {
189 EIGEN_STATIC_ASSERT((NumDims >= 5), YOU_MADE_A_PROGRAMMING_MISTAKE);
190
191 m_paddingValue = op.padding_value();
192
193 const typename TensorEvaluator<ArgType, Device>::Dimensions& input_dims = m_impl.dimensions();
194
195 // Cache a few variables.
196 if (static_cast<int>(Layout) == static_cast<int>(ColMajor)) {
197 m_inputDepth = input_dims[0];
198 m_inputPlanes = input_dims[1];
199 m_inputRows = input_dims[2];
200 m_inputCols = input_dims[3];
201 } else {
202 m_inputDepth = input_dims[NumInputDims-1];
203 m_inputPlanes = input_dims[NumInputDims-2];
204 m_inputRows = input_dims[NumInputDims-3];
205 m_inputCols = input_dims[NumInputDims-4];
206 }
207
208 m_plane_strides = op.plane_strides();
209 m_row_strides = op.row_strides();
210 m_col_strides = op.col_strides();
211
212 // Input strides and effective input/patch size
213 m_in_plane_strides = op.in_plane_strides();
214 m_in_row_strides = op.in_row_strides();
215 m_in_col_strides = op.in_col_strides();
216 m_plane_inflate_strides = op.plane_inflate_strides();
217 m_row_inflate_strides = op.row_inflate_strides();
218 m_col_inflate_strides = op.col_inflate_strides();
219
220 // The "effective" spatial size after inflating data with zeros.
221 m_input_planes_eff = (m_inputPlanes - 1) * m_plane_inflate_strides + 1;
222 m_input_rows_eff = (m_inputRows - 1) * m_row_inflate_strides + 1;
223 m_input_cols_eff = (m_inputCols - 1) * m_col_inflate_strides + 1;
224 m_patch_planes_eff = op.patch_planes() + (op.patch_planes() - 1) * (m_in_plane_strides - 1);
225 m_patch_rows_eff = op.patch_rows() + (op.patch_rows() - 1) * (m_in_row_strides - 1);
226 m_patch_cols_eff = op.patch_cols() + (op.patch_cols() - 1) * (m_in_col_strides - 1);
227
228 if (op.padding_explicit()) {
229 m_outputPlanes = numext::ceil((m_input_planes_eff + op.padding_top_z() + op.padding_bottom_z() - m_patch_planes_eff + 1.f) / static_cast<float>(m_plane_strides));
230 m_outputRows = numext::ceil((m_input_rows_eff + op.padding_top() + op.padding_bottom() - m_patch_rows_eff + 1.f) / static_cast<float>(m_row_strides));
231 m_outputCols = numext::ceil((m_input_cols_eff + op.padding_left() + op.padding_right() - m_patch_cols_eff + 1.f) / static_cast<float>(m_col_strides));
232 m_planePaddingTop = op.padding_top_z();
233 m_rowPaddingTop = op.padding_top();
234 m_colPaddingLeft = op.padding_left();
235 } else {
236 // Computing padding from the type
237 switch (op.padding_type()) {
238 case PADDING_VALID:
239 m_outputPlanes = numext::ceil((m_input_planes_eff - m_patch_planes_eff + 1.f) / static_cast<float>(m_plane_strides));
240 m_outputRows = numext::ceil((m_input_rows_eff - m_patch_rows_eff + 1.f) / static_cast<float>(m_row_strides));
241 m_outputCols = numext::ceil((m_input_cols_eff - m_patch_cols_eff + 1.f) / static_cast<float>(m_col_strides));
242 m_planePaddingTop = 0;
243 m_rowPaddingTop = 0;
244 m_colPaddingLeft = 0;
245 break;
246 case PADDING_SAME: {
247 m_outputPlanes = numext::ceil(m_input_planes_eff / static_cast<float>(m_plane_strides));
248 m_outputRows = numext::ceil(m_input_rows_eff / static_cast<float>(m_row_strides));
249 m_outputCols = numext::ceil(m_input_cols_eff / static_cast<float>(m_col_strides));
250 const Index dz = m_outputPlanes * m_plane_strides + m_patch_planes_eff - 1 - m_input_planes_eff;
251 const Index dy = m_outputRows * m_row_strides + m_patch_rows_eff - 1 - m_input_rows_eff;
252 const Index dx = m_outputCols * m_col_strides + m_patch_cols_eff - 1 - m_input_cols_eff;
253 m_planePaddingTop = dz - dz / 2;
254 m_rowPaddingTop = dy - dy / 2;
255 m_colPaddingLeft = dx - dx / 2;
256 break;
257 }
258 default:
259 eigen_assert(false && "unexpected padding");
260 }
261 }
262 eigen_assert(m_outputRows > 0);
263 eigen_assert(m_outputCols > 0);
264 eigen_assert(m_outputPlanes > 0);
265
266 // Dimensions for result of extraction.
267 if (static_cast<int>(Layout) == static_cast<int>(ColMajor)) {
268 // ColMajor
269 // 0: depth
270 // 1: patch_planes
271 // 2: patch_rows
272 // 3: patch_cols
273 // 4: number of patches
274 // 5 and beyond: anything else (such as batch).
275 m_dimensions[0] = input_dims[0];
276 m_dimensions[1] = op.patch_planes();
277 m_dimensions[2] = op.patch_rows();
278 m_dimensions[3] = op.patch_cols();
279 m_dimensions[4] = m_outputPlanes * m_outputRows * m_outputCols;
280 for (int i = 5; i < NumDims; ++i) {
281 m_dimensions[i] = input_dims[i-1];
282 }
283 } else {
284 // RowMajor
285 // NumDims-1: depth
286 // NumDims-2: patch_planes
287 // NumDims-3: patch_rows
288 // NumDims-4: patch_cols
289 // NumDims-5: number of patches
290 // NumDims-6 and beyond: anything else (such as batch).
291 m_dimensions[NumDims-1] = input_dims[NumInputDims-1];
292 m_dimensions[NumDims-2] = op.patch_planes();
293 m_dimensions[NumDims-3] = op.patch_rows();
294 m_dimensions[NumDims-4] = op.patch_cols();
295 m_dimensions[NumDims-5] = m_outputPlanes * m_outputRows * m_outputCols;
296 for (int i = NumDims-6; i >= 0; --i) {
297 m_dimensions[i] = input_dims[i];
298 }
299 }
300
301 // Strides for the output tensor.
302 if (static_cast<int>(Layout) == static_cast<int>(ColMajor)) {
303 m_rowStride = m_dimensions[1];
304 m_colStride = m_dimensions[2] * m_rowStride;
305 m_patchStride = m_colStride * m_dimensions[3] * m_dimensions[0];
306 m_otherStride = m_patchStride * m_dimensions[4];
307 } else {
308 m_rowStride = m_dimensions[NumDims-2];
309 m_colStride = m_dimensions[NumDims-3] * m_rowStride;
310 m_patchStride = m_colStride * m_dimensions[NumDims-4] * m_dimensions[NumDims-1];
311 m_otherStride = m_patchStride * m_dimensions[NumDims-5];
312 }
313
314 // Strides for navigating through the input tensor.
315 m_planeInputStride = m_inputDepth;
316 m_rowInputStride = m_inputDepth * m_inputPlanes;
317 m_colInputStride = m_inputDepth * m_inputRows * m_inputPlanes;
318 m_otherInputStride = m_inputDepth * m_inputRows * m_inputCols * m_inputPlanes;
319
320 m_outputPlanesRows = m_outputPlanes * m_outputRows;
321
322 // Fast representations of different variables.
323 m_fastOtherStride = internal::TensorIntDivisor<Index>(m_otherStride);
324 m_fastPatchStride = internal::TensorIntDivisor<Index>(m_patchStride);
325 m_fastColStride = internal::TensorIntDivisor<Index>(m_colStride);
326 m_fastRowStride = internal::TensorIntDivisor<Index>(m_rowStride);
327 m_fastInputRowStride = internal::TensorIntDivisor<Index>(m_row_inflate_strides);
328 m_fastInputColStride = internal::TensorIntDivisor<Index>(m_col_inflate_strides);
329 m_fastInputPlaneStride = internal::TensorIntDivisor<Index>(m_plane_inflate_strides);
330 m_fastInputColsEff = internal::TensorIntDivisor<Index>(m_input_cols_eff);
331 m_fastOutputPlanes = internal::TensorIntDivisor<Index>(m_outputPlanes);
332 m_fastOutputPlanesRows = internal::TensorIntDivisor<Index>(m_outputPlanesRows);
333
334 if (static_cast<int>(Layout) == static_cast<int>(ColMajor)) {
335 m_fastOutputDepth = internal::TensorIntDivisor<Index>(m_dimensions[0]);
336 } else {
337 m_fastOutputDepth = internal::TensorIntDivisor<Index>(m_dimensions[NumDims-1]);
338 }
339 }
340
341 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Dimensions& dimensions() const { return m_dimensions; }
342
343 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE bool evalSubExprsIfNeeded(Scalar* /*data*/) {
344 m_impl.evalSubExprsIfNeeded(NULL);
345 return true;
346 }
347
348 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void cleanup() {
349 m_impl.cleanup();
350 }
351
352 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE CoeffReturnType coeff(Index index) const
353 {
354 // Patch index corresponding to the passed in index.
355 const Index patchIndex = index / m_fastPatchStride;
356
357 // Spatial offset within the patch. This has to be translated into 3D
358 // coordinates within the patch.
359 const Index patchOffset = (index - patchIndex * m_patchStride) / m_fastOutputDepth;
360
361 // Batch, etc.
362 const Index otherIndex = (NumDims == 5) ? 0 : index / m_fastOtherStride;
363 const Index patch3DIndex = (NumDims == 5) ? patchIndex : (index - otherIndex * m_otherStride) / m_fastPatchStride;
364
365 // Calculate column index in the input original tensor.
366 const Index colIndex = patch3DIndex / m_fastOutputPlanesRows;
367 const Index colOffset = patchOffset / m_fastColStride;
368 const Index inputCol = colIndex * m_col_strides + colOffset * m_in_col_strides - m_colPaddingLeft;
369 const Index origInputCol = (m_col_inflate_strides == 1) ? inputCol : ((inputCol >= 0) ? (inputCol / m_fastInputColStride) : 0);
370 if (inputCol < 0 || inputCol >= m_input_cols_eff ||
371 ((m_col_inflate_strides != 1) && (inputCol != origInputCol * m_col_inflate_strides))) {
372 return Scalar(m_paddingValue);
373 }
374
375 // Calculate row index in the original input tensor.
376 const Index rowIndex = (patch3DIndex - colIndex * m_outputPlanesRows) / m_fastOutputPlanes;
377 const Index rowOffset = (patchOffset - colOffset * m_colStride) / m_fastRowStride;
378 const Index inputRow = rowIndex * m_row_strides + rowOffset * m_in_row_strides - m_rowPaddingTop;
379 const Index origInputRow = (m_row_inflate_strides == 1) ? inputRow : ((inputRow >= 0) ? (inputRow / m_fastInputRowStride) : 0);
380 if (inputRow < 0 || inputRow >= m_input_rows_eff ||
381 ((m_row_inflate_strides != 1) && (inputRow != origInputRow * m_row_inflate_strides))) {
382 return Scalar(m_paddingValue);
383 }
384
385 // Calculate plane index in the original input tensor.
386 const Index planeIndex = (patch3DIndex - m_outputPlanes * (colIndex * m_outputRows + rowIndex));
387 const Index planeOffset = patchOffset - colOffset * m_colStride - rowOffset * m_rowStride;
388 const Index inputPlane = planeIndex * m_plane_strides + planeOffset * m_in_plane_strides - m_planePaddingTop;
389 const Index origInputPlane = (m_plane_inflate_strides == 1) ? inputPlane : ((inputPlane >= 0) ? (inputPlane / m_fastInputPlaneStride) : 0);
390 if (inputPlane < 0 || inputPlane >= m_input_planes_eff ||
391 ((m_plane_inflate_strides != 1) && (inputPlane != origInputPlane * m_plane_inflate_strides))) {
392 return Scalar(m_paddingValue);
393 }
394
395 const int depth_index = static_cast<int>(Layout) == static_cast<int>(ColMajor) ? 0 : NumDims - 1;
396 const Index depth = index - (index / m_fastOutputDepth) * m_dimensions[depth_index];
397
398 const Index inputIndex = depth +
399 origInputRow * m_rowInputStride +
400 origInputCol * m_colInputStride +
401 origInputPlane * m_planeInputStride +
402 otherIndex * m_otherInputStride;
403
404 return m_impl.coeff(inputIndex);
405 }
406
407 template<int LoadMode>
408 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE PacketReturnType packet(Index index) const
409 {
410 EIGEN_STATIC_ASSERT((PacketSize > 1), YOU_MADE_A_PROGRAMMING_MISTAKE)
411 eigen_assert(index+PacketSize-1 < dimensions().TotalSize());
412
413 if (m_in_row_strides != 1 || m_in_col_strides != 1 || m_row_inflate_strides != 1 || m_col_inflate_strides != 1 ||
414 m_in_plane_strides != 1 || m_plane_inflate_strides != 1) {
415 return packetWithPossibleZero(index);
416 }
417
418 const Index indices[2] = {index, index + PacketSize - 1};
419 const Index patchIndex = indices[0] / m_fastPatchStride;
420 if (patchIndex != indices[1] / m_fastPatchStride) {
421 return packetWithPossibleZero(index);
422 }
423 const Index otherIndex = (NumDims == 5) ? 0 : indices[0] / m_fastOtherStride;
424 eigen_assert(otherIndex == indices[1] / m_fastOtherStride);
425
426 // Find the offset of the element wrt the location of the first element.
427 const Index patchOffsets[2] = {(indices[0] - patchIndex * m_patchStride) / m_fastOutputDepth,
428 (indices[1] - patchIndex * m_patchStride) / m_fastOutputDepth};
429
430 const Index patch3DIndex = (NumDims == 5) ? patchIndex : (indices[0] - otherIndex * m_otherStride) / m_fastPatchStride;
431 eigen_assert(patch3DIndex == (indices[1] - otherIndex * m_otherStride) / m_fastPatchStride);
432
433 const Index colIndex = patch3DIndex / m_fastOutputPlanesRows;
434 const Index colOffsets[2] = {
435 patchOffsets[0] / m_fastColStride,
436 patchOffsets[1] / m_fastColStride};
437
438 // Calculate col indices in the original input tensor.
439 const Index inputCols[2] = {
440 colIndex * m_col_strides + colOffsets[0] - m_colPaddingLeft,
441 colIndex * m_col_strides + colOffsets[1] - m_colPaddingLeft};
442 if (inputCols[1] < 0 || inputCols[0] >= m_inputCols) {
443 return internal::pset1<PacketReturnType>(Scalar(m_paddingValue));
444 }
445
446 if (inputCols[0] != inputCols[1]) {
447 return packetWithPossibleZero(index);
448 }
449
450 const Index rowIndex = (patch3DIndex - colIndex * m_outputPlanesRows) / m_fastOutputPlanes;
451 const Index rowOffsets[2] = {
452 (patchOffsets[0] - colOffsets[0] * m_colStride) / m_fastRowStride,
453 (patchOffsets[1] - colOffsets[1] * m_colStride) / m_fastRowStride};
454 eigen_assert(rowOffsets[0] <= rowOffsets[1]);
455 // Calculate col indices in the original input tensor.
456 const Index inputRows[2] = {
457 rowIndex * m_row_strides + rowOffsets[0] - m_rowPaddingTop,
458 rowIndex * m_row_strides + rowOffsets[1] - m_rowPaddingTop};
459
460 if (inputRows[1] < 0 || inputRows[0] >= m_inputRows) {
461 return internal::pset1<PacketReturnType>(Scalar(m_paddingValue));
462 }
463
464 if (inputRows[0] != inputRows[1]) {
465 return packetWithPossibleZero(index);
466 }
467
468 const Index planeIndex = (patch3DIndex - m_outputPlanes * (colIndex * m_outputRows + rowIndex));
469 const Index planeOffsets[2] = {
470 patchOffsets[0] - colOffsets[0] * m_colStride - rowOffsets[0] * m_rowStride,
471 patchOffsets[1] - colOffsets[1] * m_colStride - rowOffsets[1] * m_rowStride};
472 eigen_assert(planeOffsets[0] <= planeOffsets[1]);
473 const Index inputPlanes[2] = {
474 planeIndex * m_plane_strides + planeOffsets[0] - m_planePaddingTop,
475 planeIndex * m_plane_strides + planeOffsets[1] - m_planePaddingTop};
476
477 if (inputPlanes[1] < 0 || inputPlanes[0] >= m_inputPlanes) {
478 return internal::pset1<PacketReturnType>(Scalar(m_paddingValue));
479 }
480
481 if (inputPlanes[0] >= 0 && inputPlanes[1] < m_inputPlanes) {
482 // no padding
483 const int depth_index = static_cast<int>(Layout) == static_cast<int>(ColMajor) ? 0 : NumDims - 1;
484 const Index depth = index - (index / m_fastOutputDepth) * m_dimensions[depth_index];
485 const Index inputIndex = depth +
486 inputRows[0] * m_rowInputStride +
487 inputCols[0] * m_colInputStride +
488 m_planeInputStride * inputPlanes[0] +
489 otherIndex * m_otherInputStride;
490 return m_impl.template packet<Unaligned>(inputIndex);
491 }
492
493 return packetWithPossibleZero(index);
494 }
495
496 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorOpCost
497 costPerCoeff(bool vectorized) const {
498 const double compute_cost =
499 10 * TensorOpCost::DivCost<Index>() + 21 * TensorOpCost::MulCost<Index>() +
500 8 * TensorOpCost::AddCost<Index>();
501 return TensorOpCost(0, 0, compute_cost, vectorized, PacketSize);
502 }
503
504 EIGEN_DEVICE_FUNC Scalar* data() const { return NULL; }
505
506 const TensorEvaluator<ArgType, Device>& impl() const { return m_impl; }
507
508 Index planePaddingTop() const { return m_planePaddingTop; }
509 Index rowPaddingTop() const { return m_rowPaddingTop; }
510 Index colPaddingLeft() const { return m_colPaddingLeft; }
511 Index outputPlanes() const { return m_outputPlanes; }
512 Index outputRows() const { return m_outputRows; }
513 Index outputCols() const { return m_outputCols; }
514 Index userPlaneStride() const { return m_plane_strides; }
515 Index userRowStride() const { return m_row_strides; }
516 Index userColStride() const { return m_col_strides; }
517 Index userInPlaneStride() const { return m_in_plane_strides; }
518 Index userInRowStride() const { return m_in_row_strides; }
519 Index userInColStride() const { return m_in_col_strides; }
520 Index planeInflateStride() const { return m_plane_inflate_strides; }
521 Index rowInflateStride() const { return m_row_inflate_strides; }
522 Index colInflateStride() const { return m_col_inflate_strides; }
523
524 protected:
525 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE PacketReturnType packetWithPossibleZero(Index index) const
526 {
527 EIGEN_ALIGN_MAX typename internal::remove_const<CoeffReturnType>::type values[PacketSize];
528 for (int i = 0; i < PacketSize; ++i) {
529 values[i] = coeff(index+i);
530 }
531 PacketReturnType rslt = internal::pload<PacketReturnType>(values);
532 return rslt;
533 }
534
535 Dimensions m_dimensions;
536
537 // Parameters passed to the costructor.
538 Index m_plane_strides;
539 Index m_row_strides;
540 Index m_col_strides;
541
542 Index m_outputPlanes;
543 Index m_outputRows;
544 Index m_outputCols;
545
546 Index m_planePaddingTop;
547 Index m_rowPaddingTop;
548 Index m_colPaddingLeft;
549
550 Index m_in_plane_strides;
551 Index m_in_row_strides;
552 Index m_in_col_strides;
553
554 Index m_plane_inflate_strides;
555 Index m_row_inflate_strides;
556 Index m_col_inflate_strides;
557
558 // Cached input size.
559 Index m_inputDepth;
560 Index m_inputPlanes;
561 Index m_inputRows;
562 Index m_inputCols;
563
564 // Other cached variables.
565 Index m_outputPlanesRows;
566
567 // Effective input/patch post-inflation size.
568 Index m_input_planes_eff;
569 Index m_input_rows_eff;
570 Index m_input_cols_eff;
571 Index m_patch_planes_eff;
572 Index m_patch_rows_eff;
573 Index m_patch_cols_eff;
574
575 // Strides for the output tensor.
576 Index m_otherStride;
577 Index m_patchStride;
578 Index m_rowStride;
579 Index m_colStride;
580
581 // Strides for the input tensor.
582 Index m_planeInputStride;
583 Index m_rowInputStride;
584 Index m_colInputStride;
585 Index m_otherInputStride;
586
587 internal::TensorIntDivisor<Index> m_fastOtherStride;
588 internal::TensorIntDivisor<Index> m_fastPatchStride;
589 internal::TensorIntDivisor<Index> m_fastColStride;
590 internal::TensorIntDivisor<Index> m_fastRowStride;
591 internal::TensorIntDivisor<Index> m_fastInputPlaneStride;
592 internal::TensorIntDivisor<Index> m_fastInputRowStride;
593 internal::TensorIntDivisor<Index> m_fastInputColStride;
594 internal::TensorIntDivisor<Index> m_fastInputColsEff;
595 internal::TensorIntDivisor<Index> m_fastOutputPlanesRows;
596 internal::TensorIntDivisor<Index> m_fastOutputPlanes;
597 internal::TensorIntDivisor<Index> m_fastOutputDepth;
598
599 Scalar m_paddingValue;
600
601 TensorEvaluator<ArgType, Device> m_impl;
602};
603
604
605} // end namespace Eigen
606
607#endif // EIGEN_CXX11_TENSOR_TENSOR_VOLUME_PATCH_H
The tensor base class.
Definition TensorForwardDeclarations.h:29
Patch extraction specialized for processing of volumetric data. This assumes that the input has a lea...
Definition TensorVolumePatch.h:53
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