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 
13 namespace Eigen {
14 
22 namespace internal {
23 template<typename PatchDim, typename XprType>
24 struct 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 
37 template<typename PatchDim, typename XprType>
38 struct eval<TensorPatchOp<PatchDim, XprType>, Eigen::Dense>
39 {
41 };
42 
43 template<typename PatchDim, typename XprType>
44 struct nested<TensorPatchOp<PatchDim, XprType>, 1, typename eval<TensorPatchOp<PatchDim, XprType> >::type>
45 {
47 };
48 
49 } // end namespace internal
50 
51 
52 
53 template<typename PatchDim, typename XprType>
54 class TensorPatchOp : public TensorBase<TensorPatchOp<PatchDim, XprType>, ReadOnlyAccessors>
55 {
56  public:
59  typedef typename XprType::CoeffReturnType CoeffReturnType;
63 
65  : m_xpr(expr), m_patch_dims(patch_dims) {}
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
81 template<typename PatchDim, typename ArgType, typename Device>
82 struct 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  }
257  PacketReturnType rslt = internal::pload<PacketReturnType>(values);
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
Eigen::TensorEvaluator::dimensions
EIGEN_DEVICE_FUNC const EIGEN_STRONG_INLINE Dimensions & dimensions() const
Definition: TensorEvaluator.h:73
Eigen::TensorEvaluator< const TensorPatchOp< PatchDim, ArgType >, Device >::costPerCoeff
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorOpCost costPerCoeff(bool vectorized) const
Definition: TensorPatch.h:262
Eigen::TensorEvaluator< const TensorPatchOp< PatchDim, ArgType >, Device >::m_impl
TensorEvaluator< ArgType, Device > m_impl
Definition: TensorPatch.h:285
EIGEN_DEVICE_FUNC
#define EIGEN_DEVICE_FUNC
Definition: Macros.h:976
Eigen
Namespace containing all symbols from the Eigen library.
Definition: jet.h:637
Eigen::TensorPatchOp::RealScalar
Eigen::NumTraits< Scalar >::Real RealScalar
Definition: TensorPatch.h:58
Eigen::internal::TensorBlockNotImplemented
Definition: TensorBlock.h:617
Eigen::TensorEvaluator< const TensorPatchOp< PatchDim, ArgType >, Device >::TensorEvaluator
EIGEN_STRONG_INLINE TensorEvaluator(const XprType &op, const Device &device)
Definition: TensorPatch.h:110
Eigen::TensorPatchOp::Index
Eigen::internal::traits< TensorPatchOp >::Index Index
Definition: TensorPatch.h:62
Eigen::internal::traits< TensorPatchOp< PatchDim, XprType > >::XprTraits
traits< XprType > XprTraits
Definition: TensorPatch.h:27
Eigen::CwiseBinaryOp
Generic expression where a coefficient-wise binary operator is applied to two expressions.
Definition: CwiseBinaryOp.h:77
Eigen::array< Index, NumDims >
Eigen::internal::nested< TensorPatchOp< PatchDim, XprType >, 1, typename eval< TensorPatchOp< PatchDim, XprType > >::type >::type
TensorPatchOp< PatchDim, XprType > type
Definition: TensorPatch.h:46
Eigen::internal::traits< TensorPatchOp< PatchDim, XprType > >::_Nested
remove_reference< Nested >::type _Nested
Definition: TensorPatch.h:31
Eigen::internal::nested
Definition: TensorTraits.h:174
Eigen::internal::traits< TensorPatchOp< PatchDim, XprType > >::PointerType
XprTraits::PointerType PointerType
Definition: TensorPatch.h:34
eigen_assert
#define eigen_assert(x)
Definition: Macros.h:1037
Eigen::TensorEvaluator< const TensorPatchOp< PatchDim, ArgType >, Device >::cleanup
EIGEN_STRONG_INLINE void cleanup()
Definition: TensorPatch.h:160
Eigen::TensorEvaluator< const TensorPatchOp< PatchDim, ArgType >, Device >::m_inputStrides
array< Index, NumDims-1 > m_inputStrides
Definition: TensorPatch.h:282
Eigen::TensorEvaluator::Index
Derived::Index Index
Definition: TensorEvaluator.h:30
Eigen::TensorEvaluator< const TensorPatchOp< PatchDim, ArgType >, Device >::Scalar
XprType::Scalar Scalar
Definition: TensorPatch.h:88
Eigen::internal::traits< TensorPatchOp< PatchDim, XprType > >::StorageKind
XprTraits::StorageKind StorageKind
Definition: TensorPatch.h:28
Eigen::TensorEvaluator::Layout
@ Layout
Definition: TensorEvaluator.h:50
Eigen::internal::traits< TensorPatchOp< PatchDim, XprType > >::Index
XprTraits::Index Index
Definition: TensorPatch.h:29
Eigen::TensorEvaluator< const TensorPatchOp< PatchDim, ArgType >, Device >::coeff
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE CoeffReturnType coeff(Index index) const
Definition: TensorPatch.h:164
Eigen::TensorPatchOp::CoeffReturnType
XprType::CoeffReturnType CoeffReturnType
Definition: TensorPatch.h:59
Eigen::TensorEvaluator< const TensorPatchOp< PatchDim, ArgType >, Device >::CoeffReturnType
XprType::CoeffReturnType CoeffReturnType
Definition: TensorPatch.h:89
Eigen::TensorEvaluator::PacketSize
static const int PacketSize
Definition: TensorEvaluator.h:36
Eigen::TensorPatchOp::Nested
Eigen::internal::nested< TensorPatchOp >::type Nested
Definition: TensorPatch.h:60
Eigen::TensorEvaluator< const TensorPatchOp< PatchDim, ArgType >, Device >::TensorBlock
internal::TensorBlockNotImplemented TensorBlock
Definition: TensorPatch.h:107
Eigen::DSizes< Index, NumDims >
test_eigen_tensor.indices
indices
Definition: test_eigen_tensor.py:31
EIGEN_ALIGN_MAX
#define EIGEN_ALIGN_MAX
Definition: ConfigureVectorization.h:157
Eigen::internal::traits< TensorPatchOp< PatchDim, XprType > >::Scalar
XprType::Scalar Scalar
Definition: TensorPatch.h:26
Eigen::PacketType
Definition: TensorMeta.h:50
Eigen::internal::true_type
Definition: Meta.h:96
Eigen::TensorEvaluator< const TensorPatchOp< PatchDim, ArgType >, Device >::Dimensions
DSizes< Index, NumDims > Dimensions
Definition: TensorPatch.h:87
Eigen::TensorEvaluator< const TensorPatchOp< PatchDim, ArgType >, Device >::m_outputStrides
array< Index, NumDims > m_outputStrides
Definition: TensorPatch.h:281
Eigen::TensorPatchOp::Scalar
Eigen::internal::traits< TensorPatchOp >::Scalar Scalar
Definition: TensorPatch.h:57
Eigen::TensorEvaluator< const TensorPatchOp< PatchDim, ArgType >, Device >::Index
XprType::Index Index
Definition: TensorPatch.h:85
EIGEN_STRONG_INLINE
#define EIGEN_STRONG_INLINE
Definition: Macros.h:917
EIGEN_UNROLL_LOOP
#define EIGEN_UNROLL_LOOP
Definition: Macros.h:1461
Eigen::TensorEvaluator::PreferBlockAccess
@ PreferBlockAccess
Definition: TensorEvaluator.h:49
Eigen::TensorPatchOp::m_xpr
XprType::Nested m_xpr
Definition: TensorPatch.h:75
Eigen::Triplet< double >
Eigen::TensorPatchOp::expression
const EIGEN_DEVICE_FUNC internal::remove_all< typename XprType::Nested >::type & expression() const
Definition: TensorPatch.h:72
Eigen::StorageMemory
Definition: TensorForwardDeclarations.h:37
Eigen::TensorBase
The tensor base class.
Definition: TensorBase.h:973
Eigen::internal::array_size
Definition: Meta.h:445
Eigen::internal::traits< TensorPatchOp< PatchDim, XprType > >::Nested
XprType::Nested Nested
Definition: TensorPatch.h:30
Eigen::TensorEvaluator::BlockAccess
@ BlockAccess
Definition: TensorEvaluator.h:48
Eigen::TensorPatchOp::patch_dims
const EIGEN_DEVICE_FUNC PatchDim & patch_dims() const
Definition: TensorPatch.h:68
Eigen::TensorPatchOp::StorageKind
Eigen::internal::traits< TensorPatchOp >::StorageKind StorageKind
Definition: TensorPatch.h:61
Eigen::internal::traits
Definition: ForwardDeclarations.h:17
EIGEN_STATIC_ASSERT
#define EIGEN_STATIC_ASSERT(CONDITION, MSG)
Definition: StaticAssert.h:127
Eigen::TensorEvaluator< const TensorPatchOp< PatchDim, ArgType >, Device >::XprType
TensorPatchOp< PatchDim, ArgType > XprType
Definition: TensorPatch.h:84
leaf::values
leaf::MyValues values
Eigen::TensorEvaluator< const TensorPatchOp< PatchDim, ArgType >, Device >::m_dimensions
Dimensions m_dimensions
Definition: TensorPatch.h:280
Eigen::TensorEvaluator< const TensorPatchOp< PatchDim, ArgType >, Device >::data
EIGEN_DEVICE_FUNC EvaluatorPointerType data() const
Definition: TensorPatch.h:270
Eigen::TensorEvaluator< const TensorPatchOp< PatchDim, ArgType >, Device >::PacketReturnType
PacketType< CoeffReturnType, Device >::type PacketReturnType
Definition: TensorPatch.h:90
Eigen::TensorEvaluator::coeff
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE CoeffReturnType coeff(Index index) const
Definition: TensorEvaluator.h:94
Eigen::TensorEvaluator< const TensorPatchOp< PatchDim, ArgType >, Device >::evalSubExprsIfNeeded
EIGEN_STRONG_INLINE bool evalSubExprsIfNeeded(EvaluatorPointerType)
Definition: TensorPatch.h:155
Eigen::TensorEvaluator
A cost model used to limit the number of threads used for evaluating tensor expression.
Definition: TensorEvaluator.h:28
Eigen::TensorEvaluator< const TensorPatchOp< PatchDim, ArgType >, Device >::Storage
StorageMemory< CoeffReturnType, Device > Storage
Definition: TensorPatch.h:92
internal
Definition: BandTriangularSolver.h:13
NULL
#define NULL
Definition: ccolamd.c:609
Eigen::TensorEvaluator< const TensorPatchOp< PatchDim, ArgType >, Device >::m_patchStrides
array< Index, NumDims-1 > m_patchStrides
Definition: TensorPatch.h:283
Eigen::ColMajor
@ ColMajor
Definition: Constants.h:319
Eigen::TensorPatchOp
Definition: TensorForwardDeclarations.h:71
Eigen::TensorEvaluator< const TensorPatchOp< PatchDim, ArgType >, Device >::dimensions
EIGEN_DEVICE_FUNC const EIGEN_STRONG_INLINE Dimensions & dimensions() const
Definition: TensorPatch.h:153
Eigen::TensorEvaluator::IsAligned
@ IsAligned
Definition: TensorEvaluator.h:46
Eigen::internal::eval< TensorPatchOp< PatchDim, XprType >, Eigen::Dense >::type
const typedef TensorPatchOp< PatchDim, XprType > & type
Definition: TensorPatch.h:40
Eigen::internal::eval
Definition: XprHelper.h:332
Eigen::TensorEvaluator::PacketAccess
@ PacketAccess
Definition: TensorEvaluator.h:47
Eigen::TensorEvaluator< const TensorPatchOp< PatchDim, ArgType >, Device >::EvaluatorPointerType
Storage::Type EvaluatorPointerType
Definition: TensorPatch.h:93
Eigen::TensorOpCost
Definition: TensorCostModel.h:25
test_callbacks.value
value
Definition: test_callbacks.py:158
Eigen::TensorEvaluator< const TensorPatchOp< PatchDim, ArgType >, Device >::packet
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE PacketReturnType packet(Index index) const
Definition: TensorPatch.h:196
Eigen::TensorPatchOp::m_patch_dims
const PatchDim m_patch_dims
Definition: TensorPatch.h:76
i
int i
Definition: BiCGSTAB_step_by_step.cpp:9
Eigen::TensorPatchOp::TensorPatchOp
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorPatchOp(const XprType &expr, const PatchDim &patch_dims)
Definition: TensorPatch.h:64
Scalar
SCALAR Scalar
Definition: bench_gemm.cpp:46
Eigen::Index
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:74
Eigen::Dense
Definition: Constants.h:507


gtsam
Author(s):
autogenerated on Tue Jun 25 2024 03:04:48