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 
7 namespace Eigen {
8 
24 namespace internal {
25 template<DenseIndex Planes, DenseIndex Rows, DenseIndex Cols, typename XprType>
26 struct traits<TensorVolumePatchOp<Planes, Rows, Cols, XprType> > : public traits<XprType>
27 {
28  typedef typename internal::remove_const<typename XprType::Scalar>::type Scalar;
29  typedef traits<XprType> XprTraits;
30  typedef typename packet_traits<Scalar>::type Packet;
31  typedef typename XprTraits::StorageKind StorageKind;
32  typedef typename XprTraits::Index Index;
33  typedef typename XprType::Nested Nested;
34  typedef typename remove_reference<Nested>::type _Nested;
35  static const int NumDimensions = XprTraits::NumDimensions + 1;
36  static const int Layout = XprTraits::Layout;
37 };
38 
39 template<DenseIndex Planes, DenseIndex Rows, DenseIndex Cols, typename XprType>
40 struct eval<TensorVolumePatchOp<Planes, Rows, Cols, XprType>, Eigen::Dense>
41 {
42  typedef const TensorVolumePatchOp<Planes, Rows, Cols, XprType>& type;
43 };
44 
45 template<DenseIndex Planes, DenseIndex Rows, DenseIndex Cols, typename XprType>
46 struct nested<TensorVolumePatchOp<Planes, Rows, Cols, XprType>, 1, typename eval<TensorVolumePatchOp<Planes, Rows, Cols, XprType> >::type>
47 {
48  typedef TensorVolumePatchOp<Planes, Rows, Cols, XprType> type;
49 };
50 
51 } // end namespace internal
52 
53 template<DenseIndex Planes, DenseIndex Rows, DenseIndex Cols, typename XprType>
54 class TensorVolumePatchOp : public TensorBase<TensorVolumePatchOp<Planes, Rows, Cols, XprType>, ReadOnlyAccessors>
55 {
56  public:
57  typedef typename Eigen::internal::traits<TensorVolumePatchOp>::Scalar Scalar;
58  typedef typename Eigen::internal::traits<TensorVolumePatchOp>::Packet Packet;
59  typedef typename Eigen::NumTraits<Scalar>::Real RealScalar;
60  typedef typename XprType::CoeffReturnType CoeffReturnType;
61  typedef typename XprType::PacketReturnType PacketReturnType;
62  typedef typename Eigen::internal::nested<TensorVolumePatchOp>::type Nested;
63  typedef typename Eigen::internal::traits<TensorVolumePatchOp>::StorageKind StorageKind;
64  typedef typename Eigen::internal::traits<TensorVolumePatchOp>::Index Index;
65 
66  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorVolumePatchOp(const XprType& expr, DenseIndex patch_planes, DenseIndex patch_rows, DenseIndex patch_cols,
67  DenseIndex plane_strides, DenseIndex row_strides, DenseIndex col_strides,
68  DenseIndex in_plane_strides, DenseIndex in_row_strides, DenseIndex in_col_strides,
69  DenseIndex plane_inflate_strides, DenseIndex row_inflate_strides, DenseIndex col_inflate_strides,
70  PaddingType padding_type, Scalar padding_value)
71  : m_xpr(expr), m_patch_planes(patch_planes), m_patch_rows(patch_rows), m_patch_cols(patch_cols),
72  m_plane_strides(plane_strides), m_row_strides(row_strides), m_col_strides(col_strides),
73  m_in_plane_strides(in_plane_strides), m_in_row_strides(in_row_strides), m_in_col_strides(in_col_strides),
74  m_plane_inflate_strides(plane_inflate_strides), m_row_inflate_strides(row_inflate_strides), m_col_inflate_strides(col_inflate_strides),
75  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),
76  m_padding_type(padding_type), m_padding_value(padding_value) {}
77 
78  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorVolumePatchOp(const XprType& expr, DenseIndex patch_planes, DenseIndex patch_rows, DenseIndex patch_cols,
79  DenseIndex plane_strides, DenseIndex row_strides, DenseIndex col_strides,
80  DenseIndex in_plane_strides, DenseIndex in_row_strides, DenseIndex in_col_strides,
81  DenseIndex plane_inflate_strides, DenseIndex row_inflate_strides, DenseIndex col_inflate_strides,
82  DenseIndex padding_top_z, DenseIndex padding_bottom_z,
83  DenseIndex padding_top, DenseIndex padding_bottom,
84  DenseIndex padding_left, DenseIndex padding_right,
85  Scalar padding_value)
86  : m_xpr(expr), m_patch_planes(patch_planes), m_patch_rows(patch_rows), m_patch_cols(patch_cols),
87  m_plane_strides(plane_strides), m_row_strides(row_strides), m_col_strides(col_strides),
88  m_in_plane_strides(in_plane_strides), m_in_row_strides(in_row_strides), m_in_col_strides(in_col_strides),
89  m_plane_inflate_strides(plane_inflate_strides), m_row_inflate_strides(row_inflate_strides), m_col_inflate_strides(col_inflate_strides),
90  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),
91  m_padding_left(padding_left), m_padding_right(padding_right),
92  m_padding_type(PADDING_VALID), m_padding_value(padding_value) {}
93 
94  EIGEN_DEVICE_FUNC
95  DenseIndex patch_planes() const { return m_patch_planes; }
96  EIGEN_DEVICE_FUNC
97  DenseIndex patch_rows() const { return m_patch_rows; }
98  EIGEN_DEVICE_FUNC
99  DenseIndex patch_cols() const { return m_patch_cols; }
100  EIGEN_DEVICE_FUNC
101  DenseIndex plane_strides() const { return m_plane_strides; }
102  EIGEN_DEVICE_FUNC
103  DenseIndex row_strides() const { return m_row_strides; }
104  EIGEN_DEVICE_FUNC
105  DenseIndex col_strides() const { return m_col_strides; }
106  EIGEN_DEVICE_FUNC
107  DenseIndex in_plane_strides() const { return m_in_plane_strides; }
108  EIGEN_DEVICE_FUNC
109  DenseIndex in_row_strides() const { return m_in_row_strides; }
110  EIGEN_DEVICE_FUNC
111  DenseIndex in_col_strides() const { return m_in_col_strides; }
112  EIGEN_DEVICE_FUNC
113  DenseIndex plane_inflate_strides() const { return m_plane_inflate_strides; }
114  EIGEN_DEVICE_FUNC
115  DenseIndex row_inflate_strides() const { return m_row_inflate_strides; }
116  EIGEN_DEVICE_FUNC
117  DenseIndex col_inflate_strides() const { return m_col_inflate_strides; }
118  EIGEN_DEVICE_FUNC
119  bool padding_explicit() const { return m_padding_explicit; }
120  EIGEN_DEVICE_FUNC
121  DenseIndex padding_top_z() const { return m_padding_top_z; }
122  EIGEN_DEVICE_FUNC
123  DenseIndex padding_bottom_z() const { return m_padding_bottom_z; }
124  EIGEN_DEVICE_FUNC
125  DenseIndex padding_top() const { return m_padding_top; }
126  EIGEN_DEVICE_FUNC
127  DenseIndex padding_bottom() const { return m_padding_bottom; }
128  EIGEN_DEVICE_FUNC
129  DenseIndex padding_left() const { return m_padding_left; }
130  EIGEN_DEVICE_FUNC
131  DenseIndex padding_right() const { return m_padding_right; }
132  EIGEN_DEVICE_FUNC
133  PaddingType padding_type() const { return m_padding_type; }
134  EIGEN_DEVICE_FUNC
135  Scalar padding_value() const { return m_padding_value; }
136 
137  EIGEN_DEVICE_FUNC
138  const typename internal::remove_all<typename XprType::Nested>::type&
139  expression() const { return m_xpr; }
140 
141  protected:
142  typename XprType::Nested m_xpr;
143  const DenseIndex m_patch_planes;
144  const DenseIndex m_patch_rows;
145  const DenseIndex m_patch_cols;
146  const DenseIndex m_plane_strides;
147  const DenseIndex m_row_strides;
148  const DenseIndex m_col_strides;
149  const DenseIndex m_in_plane_strides;
150  const DenseIndex m_in_row_strides;
151  const DenseIndex m_in_col_strides;
152  const DenseIndex m_plane_inflate_strides;
153  const DenseIndex m_row_inflate_strides;
154  const DenseIndex m_col_inflate_strides;
155  const bool m_padding_explicit;
156  const DenseIndex m_padding_top_z;
157  const DenseIndex m_padding_bottom_z;
158  const DenseIndex m_padding_top;
159  const DenseIndex m_padding_bottom;
160  const DenseIndex m_padding_left;
161  const DenseIndex m_padding_right;
162  const PaddingType m_padding_type;
163  const Scalar m_padding_value;
164 };
165 
166 
167 // Eval as rvalue
168 template<DenseIndex Planes, DenseIndex Rows, DenseIndex Cols, typename ArgType, typename Device>
169 struct TensorEvaluator<const TensorVolumePatchOp<Planes, Rows, Cols, ArgType>, Device>
170 {
171  typedef TensorVolumePatchOp<Planes, Rows, Cols, ArgType> XprType;
172  typedef typename XprType::Index Index;
173  static const int NumInputDims = internal::array_size<typename TensorEvaluator<ArgType, Device>::Dimensions>::value;
174  static const int NumDims = NumInputDims + 1;
175  typedef DSizes<Index, NumDims> Dimensions;
176  typedef typename internal::remove_const<typename XprType::Scalar>::type Scalar;
177 
178  enum {
179  IsAligned = false,
180  PacketAccess = TensorEvaluator<ArgType, Device>::PacketAccess,
181  BlockAccess = false,
182  Layout = TensorEvaluator<ArgType, Device>::Layout,
183  CoordAccess = NumDims == 6,
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  typedef typename XprType::CoeffReturnType CoeffReturnType;
342  typedef typename XprType::PacketReturnType PacketReturnType;
343 
344  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Dimensions& dimensions() const { return m_dimensions; }
345 
346  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE bool evalSubExprsIfNeeded(Scalar* /*data*/) {
347  m_impl.evalSubExprsIfNeeded(NULL);
348  return true;
349  }
350 
351  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void cleanup() {
352  m_impl.cleanup();
353  }
354 
355  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE CoeffReturnType coeff(Index index) const
356  {
357  // Patch index corresponding to the passed in index.
358  const Index patchIndex = index / m_fastPatchStride;
359 
360  // Spatial offset within the patch. This has to be translated into 3D
361  // coordinates within the patch.
362  const Index patchOffset = (index - patchIndex * m_patchStride) / m_fastOutputDepth;
363 
364  // Batch, etc.
365  const Index otherIndex = (NumDims == 5) ? 0 : index / m_fastOtherStride;
366  const Index patch3DIndex = (NumDims == 5) ? patchIndex : (index - otherIndex * m_otherStride) / m_fastPatchStride;
367 
368  // Calculate column index in the input original tensor.
369  const Index colIndex = patch3DIndex / m_fastOutputPlanesRows;
370  const Index colOffset = patchOffset / m_fastColStride;
371  const Index inputCol = colIndex * m_col_strides + colOffset * m_in_col_strides - m_colPaddingLeft;
372  const Index origInputCol = (m_col_inflate_strides == 1) ? inputCol : ((inputCol >= 0) ? (inputCol / m_fastInputColStride) : 0);
373  if (inputCol < 0 || inputCol >= m_input_cols_eff ||
374  ((m_col_inflate_strides != 1) && (inputCol != origInputCol * m_col_inflate_strides))) {
375  return Scalar(m_paddingValue);
376  }
377 
378  // Calculate row index in the original input tensor.
379  const Index rowIndex = (patch3DIndex - colIndex * m_outputPlanesRows) / m_fastOutputPlanes;
380  const Index rowOffset = (patchOffset - colOffset * m_colStride) / m_fastRowStride;
381  const Index inputRow = rowIndex * m_row_strides + rowOffset * m_in_row_strides - m_rowPaddingTop;
382  const Index origInputRow = (m_row_inflate_strides == 1) ? inputRow : ((inputRow >= 0) ? (inputRow / m_fastInputRowStride) : 0);
383  if (inputRow < 0 || inputRow >= m_input_rows_eff ||
384  ((m_row_inflate_strides != 1) && (inputRow != origInputRow * m_row_inflate_strides))) {
385  return Scalar(m_paddingValue);
386  }
387 
388  // Calculate plane index in the original input tensor.
389  const Index planeIndex = (patch3DIndex - m_outputPlanes * (colIndex * m_outputRows + rowIndex));
390  const Index planeOffset = patchOffset - colOffset * m_colStride - rowOffset * m_rowStride;
391  const Index inputPlane = planeIndex * m_plane_strides + planeOffset * m_in_plane_strides - m_planePaddingTop;
392  const Index origInputPlane = (m_plane_inflate_strides == 1) ? inputPlane : ((inputPlane >= 0) ? (inputPlane / m_fastInputPlaneStride) : 0);
393  if (inputPlane < 0 || inputPlane >= m_input_planes_eff ||
394  ((m_plane_inflate_strides != 1) && (inputPlane != origInputPlane * m_plane_inflate_strides))) {
395  return Scalar(m_paddingValue);
396  }
397 
398  const int depth_index = static_cast<int>(Layout) == static_cast<int>(ColMajor) ? 0 : NumDims - 1;
399  const Index depth = index - (index / m_fastOutputDepth) * m_dimensions[depth_index];
400 
401  const Index inputIndex = depth +
402  origInputRow * m_rowInputStride +
403  origInputCol * m_colInputStride +
404  origInputPlane * m_planeInputStride +
405  otherIndex * m_otherInputStride;
406 
407  return m_impl.coeff(inputIndex);
408  }
409 
410  template<int LoadMode>
411  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE PacketReturnType packet(Index index) const
412  {
413  const Index packetSize = internal::unpacket_traits<PacketReturnType>::size;
414  EIGEN_STATIC_ASSERT(packetSize > 1, YOU_MADE_A_PROGRAMMING_MISTAKE)
415  eigen_assert(index+packetSize-1 < dimensions().TotalSize());
416 
417  if (m_in_row_strides != 1 || m_in_col_strides != 1 || m_row_inflate_strides != 1 || m_col_inflate_strides != 1 ||
418  m_in_plane_strides != 1 || m_plane_inflate_strides != 1) {
419  return packetWithPossibleZero(index);
420  }
421 
422  const Index indices[2] = {index, index + packetSize - 1};
423  const Index patchIndex = indices[0] / m_fastPatchStride;
424  if (patchIndex != indices[1] / m_fastPatchStride) {
425  return packetWithPossibleZero(index);
426  }
427  const Index otherIndex = (NumDims == 5) ? 0 : indices[0] / m_fastOtherStride;
428  eigen_assert(otherIndex == indices[1] / m_fastOtherStride);
429 
430  // Find the offset of the element wrt the location of the first element.
431  const Index patchOffsets[2] = {(indices[0] - patchIndex * m_patchStride) / m_fastOutputDepth,
432  (indices[1] - patchIndex * m_patchStride) / m_fastOutputDepth};
433 
434  const Index patch3DIndex = (NumDims == 5) ? patchIndex : (indices[0] - otherIndex * m_otherStride) / m_fastPatchStride;
435  eigen_assert(patch3DIndex == (indices[1] - otherIndex * m_otherStride) / m_fastPatchStride);
436 
437  const Index colIndex = patch3DIndex / m_fastOutputPlanesRows;
438  const Index colOffsets[2] = {
439  patchOffsets[0] / m_fastColStride,
440  patchOffsets[1] / m_fastColStride};
441 
442  // Calculate col indices in the original input tensor.
443  const Index inputCols[2] = {
444  colIndex * m_col_strides + colOffsets[0] - m_colPaddingLeft,
445  colIndex * m_col_strides + colOffsets[1] - m_colPaddingLeft};
446  if (inputCols[1] < 0 || inputCols[0] >= m_inputCols) {
447  return internal::pset1<PacketReturnType>(Scalar(m_paddingValue));
448  }
449 
450  if (inputCols[0] != inputCols[1]) {
451  return packetWithPossibleZero(index);
452  }
453 
454  const Index rowIndex = (patch3DIndex - colIndex * m_outputPlanesRows) / m_fastOutputPlanes;
455  const Index rowOffsets[2] = {
456  (patchOffsets[0] - colOffsets[0] * m_colStride) / m_fastRowStride,
457  (patchOffsets[1] - colOffsets[1] * m_colStride) / m_fastRowStride};
458  eigen_assert(rowOffsets[0] <= rowOffsets[1]);
459  // Calculate col indices in the original input tensor.
460  const Index inputRows[2] = {
461  rowIndex * m_row_strides + rowOffsets[0] - m_rowPaddingTop,
462  rowIndex * m_row_strides + rowOffsets[1] - m_rowPaddingTop};
463 
464  if (inputRows[1] < 0 || inputRows[0] >= m_inputRows) {
465  return internal::pset1<PacketReturnType>(Scalar(m_paddingValue));
466  }
467 
468  if (inputRows[0] != inputRows[1]) {
469  return packetWithPossibleZero(index);
470  }
471 
472  const Index planeIndex = (patch3DIndex - m_outputPlanes * (colIndex * m_outputRows + rowIndex));
473  const Index planeOffsets[2] = {
474  patchOffsets[0] - colOffsets[0] * m_colStride - rowOffsets[0] * m_rowStride,
475  patchOffsets[1] - colOffsets[1] * m_colStride - rowOffsets[1] * m_rowStride};
476  eigen_assert(planeOffsets[0] <= planeOffsets[1]);
477  const Index inputPlanes[2] = {
478  planeIndex * m_plane_strides + planeOffsets[0] - m_planePaddingTop,
479  planeIndex * m_plane_strides + planeOffsets[1] - m_planePaddingTop};
480 
481  if (inputPlanes[1] < 0 || inputPlanes[0] >= m_inputPlanes) {
482  return internal::pset1<PacketReturnType>(Scalar(m_paddingValue));
483  }
484 
485  if (inputPlanes[0] >= 0 && inputPlanes[1] < m_inputPlanes) {
486  // no padding
487  const int depth_index = static_cast<int>(Layout) == static_cast<int>(ColMajor) ? 0 : NumDims - 1;
488  const Index depth = index - (index / m_fastOutputDepth) * m_dimensions[depth_index];
489  const Index inputIndex = depth +
490  inputRows[0] * m_rowInputStride +
491  inputCols[0] * m_colInputStride +
492  m_planeInputStride * inputPlanes[0] +
493  otherIndex * m_otherInputStride;
494  return m_impl.template packet<Unaligned>(inputIndex);
495  }
496 
497  return packetWithPossibleZero(index);
498  }
499 
500  EIGEN_DEVICE_FUNC Scalar* data() const { return NULL; }
501 
502  const TensorEvaluator<ArgType, Device>& impl() const { return m_impl; }
503 
504  Index planePaddingTop() const { return m_planePaddingTop; }
505  Index rowPaddingTop() const { return m_rowPaddingTop; }
506  Index colPaddingLeft() const { return m_colPaddingLeft; }
507  Index outputPlanes() const { return m_outputPlanes; }
508  Index outputRows() const { return m_outputRows; }
509  Index outputCols() const { return m_outputCols; }
510  Index userPlaneStride() const { return m_plane_strides; }
511  Index userRowStride() const { return m_row_strides; }
512  Index userColStride() const { return m_col_strides; }
513  Index userInPlaneStride() const { return m_in_plane_strides; }
514  Index userInRowStride() const { return m_in_row_strides; }
515  Index userInColStride() const { return m_in_col_strides; }
516  Index planeInflateStride() const { return m_plane_inflate_strides; }
517  Index rowInflateStride() const { return m_row_inflate_strides; }
518  Index colInflateStride() const { return m_col_inflate_strides; }
519 
520  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE CoeffReturnType coeff(const array<Index, NumDims>& coords) const
521  {
522  // ColMajor
523  // 0: depth, 1: patch_planes, 2: patch_rows, 3: patch_cols, 4: number of patches, 5: batches
524  // RowMajor
525  // 0: batches, 1: number of patches, 2: patch_cols , 3: patch_rows, 4: patch_planes, 5: depth
526  const Index patch3DIndex = coords[static_cast<int>(Layout) == static_cast<int>(ColMajor) ? 4 : 1];
527  const Index colOffset = coords[static_cast<int>(Layout) == static_cast<int>(ColMajor) ? 3 : 2];
528  const Index rowOffset= coords[static_cast<int>(Layout) == static_cast<int>(ColMajor) ? 2 : 3];
529  const Index planeOffset = coords[static_cast<int>(Layout) == static_cast<int>(ColMajor) ? 1 : 4];
530 
531  array<Index, NumDims-1> inputCoords;
532 
533  const Index colIndex = patch3DIndex / m_fastOutputPlanesRows;
534  const Index inputCol = colIndex * m_col_strides + colOffset * m_in_col_strides - m_colPaddingLeft;
535  const Index origInputCol = (m_col_inflate_strides == 1) ? inputCol : ((inputCol >= 0) ? (inputCol / m_fastInputColStride) : 0);
536  if (inputCol < 0 || inputCol >= m_input_cols_eff ||
537  ((m_col_inflate_strides != 1) && (inputCol != origInputCol * m_col_inflate_strides))) {
538  return Scalar(m_paddingValue);
539  }
540 
541  const Index rowIndex = (patch3DIndex - colIndex * m_outputPlanesRows) / m_fastOutputPlanes;
542  const Index inputRow = rowIndex * m_row_strides + rowOffset * m_in_row_strides - m_rowPaddingTop;
543  const Index origInputRow = (m_row_inflate_strides == 1) ? inputRow : ((inputRow >= 0) ? (inputRow / m_fastInputRowStride) : 0);
544  if (inputRow < 0 || inputRow >= m_input_rows_eff ||
545  ((m_row_inflate_strides != 1) && (inputRow != origInputRow * m_row_inflate_strides))) {
546  return Scalar(m_paddingValue);
547  }
548 
549  const Index planeIndex = patch3DIndex - colIndex * m_outputPlanesRows - rowIndex * m_outputRows;
550  const Index inputPlane = planeIndex * m_plane_strides + planeOffset * m_in_plane_strides - m_planePaddingTop;
551  const Index origInputPlane = (m_plane_inflate_strides == 1) ? inputPlane : ((inputPlane >= 0) ? (inputPlane / m_fastInputPlaneStride) : 0);
552  if (inputPlane < 0 || inputPlane >= m_input_planes_eff ||
553  ((m_plane_inflate_strides != 1) && (inputPlane != origInputPlane * m_plane_inflate_strides))) {
554  return Scalar(m_paddingValue);
555  }
556 
557  if (static_cast<int>(Layout) == static_cast<int>(ColMajor)) {
558  inputCoords[0] = coords[0]; // depth
559  inputCoords[1] = origInputPlane;
560  inputCoords[2] = origInputRow;
561  inputCoords[3] = origInputCol;
562  inputCoords[4] = coords[5]; // batch
563  } else {
564  inputCoords[4] = coords[5]; // depth
565  inputCoords[3] = origInputPlane;
566  inputCoords[2] = origInputRow;
567  inputCoords[1] = origInputCol;
568  inputCoords[0] = coords[0]; // batch
569  }
570  if (TensorEvaluator<ArgType, Device>::CoordAccess) {
571  return m_impl.coeff(inputCoords);
572  } else {
573  Index inputIndex;
574  if (static_cast<int>(Layout) == static_cast<int>(ColMajor)) {
575  inputIndex =
576  inputCoords[4] * m_otherInputStride +
577  inputCoords[3] * m_colInputStride +
578  inputCoords[2] * m_rowInputStride +
579  inputCoords[1] * m_planeInputStride +
580  inputCoords[0];
581  } else {
582  inputIndex =
583  inputCoords[0] * m_otherInputStride +
584  inputCoords[1] * m_colInputStride +
585  inputCoords[2] * m_rowInputStride +
586  inputCoords[3] * m_planeInputStride +
587  inputCoords[4];
588  }
589  return m_impl.coeff(inputIndex);
590  }
591  }
592 
593  protected:
594  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE PacketReturnType packetWithPossibleZero(Index index) const
595  {
596  const int packetSize = internal::unpacket_traits<PacketReturnType>::size;
597  EIGEN_ALIGN_MAX typename internal::remove_const<CoeffReturnType>::type values[packetSize];
598  for (int i = 0; i < packetSize; ++i) {
599  values[i] = coeff(index+i);
600  }
601  PacketReturnType rslt = internal::pload<PacketReturnType>(values);
602  return rslt;
603  }
604 
605  Dimensions m_dimensions;
606 
607  // Parameters passed to the costructor.
608  Index m_plane_strides;
609  Index m_row_strides;
610  Index m_col_strides;
611 
612  Index m_outputPlanes;
613  Index m_outputRows;
614  Index m_outputCols;
615 
616  Index m_planePaddingTop;
617  Index m_rowPaddingTop;
618  Index m_colPaddingLeft;
619 
620  Index m_in_plane_strides;
621  Index m_in_row_strides;
622  Index m_in_col_strides;
623 
624  Index m_plane_inflate_strides;
625  Index m_row_inflate_strides;
626  Index m_col_inflate_strides;
627 
628  // Cached input size.
629  Index m_inputDepth;
630  Index m_inputPlanes;
631  Index m_inputRows;
632  Index m_inputCols;
633 
634  // Other cached variables.
635  Index m_outputPlanesRows;
636 
637  // Effective input/patch post-inflation size.
638  Index m_input_planes_eff;
639  Index m_input_rows_eff;
640  Index m_input_cols_eff;
641  Index m_patch_planes_eff;
642  Index m_patch_rows_eff;
643  Index m_patch_cols_eff;
644 
645  // Strides for the output tensor.
646  Index m_otherStride;
647  Index m_patchStride;
648  Index m_rowStride;
649  Index m_colStride;
650 
651  // Strides for the input tensor.
652  Index m_planeInputStride;
653  Index m_rowInputStride;
654  Index m_colInputStride;
655  Index m_otherInputStride;
656 
657  internal::TensorIntDivisor<Index> m_fastOtherStride;
658  internal::TensorIntDivisor<Index> m_fastPatchStride;
659  internal::TensorIntDivisor<Index> m_fastColStride;
660  internal::TensorIntDivisor<Index> m_fastRowStride;
661  internal::TensorIntDivisor<Index> m_fastInputPlaneStride;
662  internal::TensorIntDivisor<Index> m_fastInputRowStride;
663  internal::TensorIntDivisor<Index> m_fastInputColStride;
664  internal::TensorIntDivisor<Index> m_fastInputColsEff;
665  internal::TensorIntDivisor<Index> m_fastOutputPlanesRows;
666  internal::TensorIntDivisor<Index> m_fastOutputPlanes;
667  internal::TensorIntDivisor<Index> m_fastOutputDepth;
668 
669  Scalar m_paddingValue;
670 
671  TensorEvaluator<ArgType, Device> m_impl;
672 };
673 
674 
675 } // end namespace Eigen
676 
677 #endif // EIGEN_CXX11_TENSOR_TENSOR_VOLUME_PATCH_H
Namespace containing all symbols from the Eigen library.
Definition: CXX11Meta.h:13