TensorInflation.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) 2015 Ke Yang <yangke@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_INFLATION_H
11 #define EIGEN_CXX11_TENSOR_TENSOR_INFLATION_H
12 
13 namespace Eigen {
14 
22 namespace internal {
23 template<typename Strides, typename XprType>
24 struct traits<TensorInflationOp<Strides, 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;
33  static const int Layout = XprTraits::Layout;
34  typedef typename XprTraits::PointerType PointerType;
35 };
36 
37 template<typename Strides, typename XprType>
39 {
41 };
42 
43 template<typename Strides, typename XprType>
44 struct nested<TensorInflationOp<Strides, XprType>, 1, typename eval<TensorInflationOp<Strides, XprType> >::type>
45 {
47 };
48 
49 } // end namespace internal
50 
51 template<typename Strides, typename XprType>
52 class TensorInflationOp : public TensorBase<TensorInflationOp<Strides, XprType>, ReadOnlyAccessors>
53 {
54  public:
57  typedef typename XprType::CoeffReturnType CoeffReturnType;
61 
63  : m_xpr(expr), m_strides(strides) {}
64 
66  const Strides& strides() const { return m_strides; }
67 
70  expression() const { return m_xpr; }
71 
72  protected:
73  typename XprType::Nested m_xpr;
74  const Strides m_strides;
75 };
76 
77 // Eval as rvalue
78 template<typename Strides, typename ArgType, typename Device>
79 struct TensorEvaluator<const TensorInflationOp<Strides, ArgType>, Device>
80 {
82  typedef typename XprType::Index Index;
85  typedef typename XprType::Scalar Scalar;
91 
92  enum {
93  IsAligned = /*TensorEvaluator<ArgType, Device>::IsAligned*/ false,
95  BlockAccess = false,
98  CoordAccess = false, // to be implemented
99  RawAccess = false
100  };
101 
102  //===- Tensor block evaluation strategy (see TensorBlock.h) -------------===//
104  //===--------------------------------------------------------------------===//
105 
106  EIGEN_STRONG_INLINE TensorEvaluator(const XprType& op, const Device& device)
107  : m_impl(op.expression(), device), m_strides(op.strides())
108  {
109  m_dimensions = m_impl.dimensions();
110  // Expand each dimension to the inflated dimension.
111  for (int i = 0; i < NumDims; ++i) {
112  m_dimensions[i] = (m_dimensions[i] - 1) * op.strides()[i] + 1;
113  }
114 
115  // Remember the strides for fast division.
116  for (int i = 0; i < NumDims; ++i) {
117  m_fastStrides[i] = internal::TensorIntDivisor<Index>(m_strides[i]);
118  }
119 
120  const typename TensorEvaluator<ArgType, Device>::Dimensions& input_dims = m_impl.dimensions();
121  if (static_cast<int>(Layout) == static_cast<int>(ColMajor)) {
122  m_outputStrides[0] = 1;
123  m_inputStrides[0] = 1;
124  for (int i = 1; i < NumDims; ++i) {
125  m_outputStrides[i] = m_outputStrides[i-1] * m_dimensions[i-1];
126  m_inputStrides[i] = m_inputStrides[i-1] * input_dims[i-1];
127  }
128  } else { // RowMajor
129  m_outputStrides[NumDims-1] = 1;
130  m_inputStrides[NumDims-1] = 1;
131  for (int i = NumDims - 2; i >= 0; --i) {
132  m_outputStrides[i] = m_outputStrides[i+1] * m_dimensions[i+1];
133  m_inputStrides[i] = m_inputStrides[i+1] * input_dims[i+1];
134  }
135  }
136  }
137 
138  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Dimensions& dimensions() const { return m_dimensions; }
139 
141  m_impl.evalSubExprsIfNeeded(NULL);
142  return true;
143  }
145  m_impl.cleanup();
146  }
147 
148  // Computes the input index given the output index. Returns true if the output
149  // index doesn't fall into a hole.
151  {
152  eigen_assert(index < dimensions().TotalSize());
153  *inputIndex = 0;
154  if (static_cast<int>(Layout) == static_cast<int>(ColMajor)) {
156  for (int i = NumDims - 1; i > 0; --i) {
157  const Index idx = index / m_outputStrides[i];
158  if (idx != idx / m_fastStrides[i] * m_strides[i]) {
159  return false;
160  }
161  *inputIndex += idx / m_strides[i] * m_inputStrides[i];
162  index -= idx * m_outputStrides[i];
163  }
164  if (index != index / m_fastStrides[0] * m_strides[0]) {
165  return false;
166  }
167  *inputIndex += index / m_strides[0];
168  return true;
169  } else {
171  for (int i = 0; i < NumDims - 1; ++i) {
172  const Index idx = index / m_outputStrides[i];
173  if (idx != idx / m_fastStrides[i] * m_strides[i]) {
174  return false;
175  }
176  *inputIndex += idx / m_strides[i] * m_inputStrides[i];
177  index -= idx * m_outputStrides[i];
178  }
179  if (index != index / m_fastStrides[NumDims-1] * m_strides[NumDims-1]) {
180  return false;
181  }
182  *inputIndex += index / m_strides[NumDims - 1];
183  }
184  return true;
185  }
186 
188  {
189  Index inputIndex = 0;
190  if (getInputIndex(index, &inputIndex)) {
191  return m_impl.coeff(inputIndex);
192  } else {
193  return Scalar(0);
194  }
195  }
196 
197  // TODO(yangke): optimize this function so that we can detect and produce
198  // all-zero packets
199  template<int LoadMode>
201  {
202  EIGEN_STATIC_ASSERT((PacketSize > 1), YOU_MADE_A_PROGRAMMING_MISTAKE)
203  eigen_assert(index+PacketSize-1 < dimensions().TotalSize());
204 
207  for (int i = 0; i < PacketSize; ++i) {
208  values[i] = coeff(index+i);
209  }
210  PacketReturnType rslt = internal::pload<PacketReturnType>(values);
211  return rslt;
212  }
213 
215  const double compute_cost = NumDims * (3 * TensorOpCost::DivCost<Index>() +
216  3 * TensorOpCost::MulCost<Index>() +
217  2 * TensorOpCost::AddCost<Index>());
218  const double input_size = m_impl.dimensions().TotalSize();
219  const double output_size = m_dimensions.TotalSize();
220  if (output_size == 0)
221  return TensorOpCost();
222  return m_impl.costPerCoeff(vectorized) +
223  TensorOpCost(sizeof(CoeffReturnType) * input_size / output_size, 0,
224  compute_cost, vectorized, PacketSize);
225  }
226 
228 
229 #ifdef EIGEN_USE_SYCL
230  // binding placeholder accessors to a command group handler for SYCL
231  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void bind(cl::sycl::handler &cgh) const {
232  m_impl.bind(cgh);
233  }
234 #endif
235 
236  protected:
241  const Strides m_strides;
243 };
244 
245 } // end namespace Eigen
246 
247 #endif // EIGEN_CXX11_TENSOR_TENSOR_INFLATION_H
Eigen::TensorEvaluator::dimensions
EIGEN_DEVICE_FUNC const EIGEN_STRONG_INLINE Dimensions & dimensions() const
Definition: TensorEvaluator.h:73
Eigen::internal::traits< TensorInflationOp< Strides, XprType > >::_Nested
remove_reference< Nested >::type _Nested
Definition: TensorInflation.h:31
Eigen::TensorEvaluator< const TensorInflationOp< Strides, ArgType >, Device >::evalSubExprsIfNeeded
EIGEN_STRONG_INLINE bool evalSubExprsIfNeeded(EvaluatorPointerType)
Definition: TensorInflation.h:140
Eigen::TensorInflationOp::expression
const EIGEN_DEVICE_FUNC internal::remove_all< typename XprType::Nested >::type & expression() const
Definition: TensorInflation.h:70
Eigen::internal::traits< TensorInflationOp< Strides, XprType > >::StorageKind
XprTraits::StorageKind StorageKind
Definition: TensorInflation.h:28
Eigen::TensorEvaluator< const TensorInflationOp< Strides, ArgType >, Device >::packet
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE PacketReturnType packet(Index index) const
Definition: TensorInflation.h:200
EIGEN_DEVICE_FUNC
#define EIGEN_DEVICE_FUNC
Definition: Macros.h:976
Eigen::TensorEvaluator< const TensorInflationOp< Strides, ArgType >, Device >::PacketReturnType
PacketType< CoeffReturnType, Device >::type PacketReturnType
Definition: TensorInflation.h:87
Eigen
Namespace containing all symbols from the Eigen library.
Definition: jet.h:637
Eigen::TensorInflationOp::StorageKind
Eigen::internal::traits< TensorInflationOp >::StorageKind StorageKind
Definition: TensorInflation.h:59
Eigen::internal::TensorBlockNotImplemented
Definition: TensorBlock.h:617
Eigen::TensorEvaluator< const TensorInflationOp< Strides, ArgType >, Device >::coeff
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE CoeffReturnType coeff(Index index) const
Definition: TensorInflation.h:187
Eigen::internal::traits< TensorInflationOp< Strides, XprType > >::Nested
XprType::Nested Nested
Definition: TensorInflation.h:30
Eigen::internal::strides
EIGEN_ALWAYS_INLINE DSizes< IndexType, NumDims > strides(const DSizes< IndexType, NumDims > &dimensions)
Definition: TensorBlock.h:26
Eigen::CwiseBinaryOp
Generic expression where a coefficient-wise binary operator is applied to two expressions.
Definition: CwiseBinaryOp.h:77
Eigen::array< Index, NumDims >
Eigen::TensorInflationOp::Index
Eigen::internal::traits< TensorInflationOp >::Index Index
Definition: TensorInflation.h:60
Eigen::TensorInflationOp::m_strides
const Strides m_strides
Definition: TensorInflation.h:74
Eigen::internal::nested
Definition: TensorTraits.h:174
Eigen::TensorEvaluator< const TensorInflationOp< Strides, ArgType >, Device >::m_dimensions
Dimensions m_dimensions
Definition: TensorInflation.h:237
eigen_assert
#define eigen_assert(x)
Definition: Macros.h:1037
Eigen::TensorEvaluator< const TensorInflationOp< Strides, ArgType >, Device >::costPerCoeff
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorOpCost costPerCoeff(bool vectorized) const
Definition: TensorInflation.h:214
Eigen::internal::traits< TensorInflationOp< Strides, XprType > >::Index
XprTraits::Index Index
Definition: TensorInflation.h:29
Eigen::internal::nested< TensorInflationOp< Strides, XprType >, 1, typename eval< TensorInflationOp< Strides, XprType > >::type >::type
TensorInflationOp< Strides, XprType > type
Definition: TensorInflation.h:46
Eigen::TensorEvaluator::Layout
@ Layout
Definition: TensorEvaluator.h:50
Eigen::internal::eval< TensorInflationOp< Strides, XprType >, Eigen::Dense >::type
const typedef TensorInflationOp< Strides, XprType > & type
Definition: TensorInflation.h:40
Eigen::TensorEvaluator< const TensorInflationOp< Strides, ArgType >, Device >::Scalar
XprType::Scalar Scalar
Definition: TensorInflation.h:85
different_sigmas::values
HybridValues values
Definition: testHybridBayesNet.cpp:245
Eigen::TensorEvaluator::PacketSize
static const int PacketSize
Definition: TensorEvaluator.h:36
Eigen::TensorEvaluator< const TensorInflationOp< Strides, ArgType >, Device >::Dimensions
DSizes< Index, NumDims > Dimensions
Definition: TensorInflation.h:84
Eigen::internal::traits< TensorInflationOp< Strides, XprType > >::Scalar
XprType::Scalar Scalar
Definition: TensorInflation.h:26
Eigen::internal::TensorIntDivisor< Index >
Eigen::DSizes< Index, NumDims >
EIGEN_ALIGN_MAX
#define EIGEN_ALIGN_MAX
Definition: ConfigureVectorization.h:157
Eigen::PacketType
Definition: TensorMeta.h:50
Eigen::TensorEvaluator< const TensorInflationOp< Strides, ArgType >, Device >::cleanup
EIGEN_STRONG_INLINE void cleanup()
Definition: TensorInflation.h:144
Eigen::TensorEvaluator< const TensorInflationOp< Strides, ArgType >, Device >::TensorBlock
internal::TensorBlockNotImplemented TensorBlock
Definition: TensorInflation.h:103
Eigen::internal::true_type
Definition: Meta.h:96
Eigen::TensorEvaluator< const TensorInflationOp< Strides, ArgType >, Device >::CoeffReturnType
XprType::CoeffReturnType CoeffReturnType
Definition: TensorInflation.h:86
Eigen::TensorInflationOp::Scalar
Eigen::internal::traits< TensorInflationOp >::Scalar Scalar
Definition: TensorInflation.h:55
Eigen::TensorEvaluator< const TensorInflationOp< Strides, ArgType >, Device >::getInputIndex
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE bool getInputIndex(Index index, Index *inputIndex) const
Definition: TensorInflation.h:150
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::TensorInflationOp::Nested
Eigen::internal::nested< TensorInflationOp >::type Nested
Definition: TensorInflation.h:58
Eigen::TensorEvaluator< const TensorInflationOp< Strides, ArgType >, Device >::m_impl
TensorEvaluator< ArgType, Device > m_impl
Definition: TensorInflation.h:240
Eigen::TensorEvaluator< const TensorInflationOp< Strides, ArgType >, Device >::Index
XprType::Index Index
Definition: TensorInflation.h:82
Eigen::Triplet< double >
Eigen::TensorInflationOp::TensorInflationOp
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorInflationOp(const XprType &expr, const Strides &strides)
Definition: TensorInflation.h:62
Eigen::TensorInflationOp::m_xpr
XprType::Nested m_xpr
Definition: TensorInflation.h:73
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::TensorEvaluator::BlockAccess
@ BlockAccess
Definition: TensorEvaluator.h:48
Eigen::TensorEvaluator< const TensorInflationOp< Strides, ArgType >, Device >::data
EIGEN_DEVICE_FUNC EvaluatorPointerType data() const
Definition: TensorInflation.h:227
Eigen::TensorInflationOp::RealScalar
Eigen::NumTraits< Scalar >::Real RealScalar
Definition: TensorInflation.h:56
Eigen::internal::traits
Definition: ForwardDeclarations.h:17
Eigen::TensorEvaluator< const TensorInflationOp< Strides, ArgType >, Device >::Storage
StorageMemory< CoeffReturnType, Device > Storage
Definition: TensorInflation.h:89
EIGEN_STATIC_ASSERT
#define EIGEN_STATIC_ASSERT(CONDITION, MSG)
Definition: StaticAssert.h:127
Eigen::TensorEvaluator< const TensorInflationOp< Strides, ArgType >, Device >::dimensions
EIGEN_DEVICE_FUNC const EIGEN_STRONG_INLINE Dimensions & dimensions() const
Definition: TensorInflation.h:138
Eigen::TensorEvaluator< const TensorInflationOp< Strides, ArgType >, Device >::TensorEvaluator
EIGEN_STRONG_INLINE TensorEvaluator(const XprType &op, const Device &device)
Definition: TensorInflation.h:106
Eigen::TensorEvaluator::coeff
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE CoeffReturnType coeff(Index index) const
Definition: TensorEvaluator.h:94
Eigen::TensorInflationOp::CoeffReturnType
XprType::CoeffReturnType CoeffReturnType
Definition: TensorInflation.h:57
Eigen::TensorEvaluator
A cost model used to limit the number of threads used for evaluating tensor expression.
Definition: TensorEvaluator.h:28
Eigen::TensorEvaluator::Scalar
Derived::Scalar Scalar
Definition: TensorEvaluator.h:31
Eigen::TensorInflationOp
Definition: TensorForwardDeclarations.h:84
internal
Definition: BandTriangularSolver.h:13
NULL
#define NULL
Definition: ccolamd.c:609
Eigen::ColMajor
@ ColMajor
Definition: Constants.h:319
Eigen::TensorEvaluator::IsAligned
@ IsAligned
Definition: TensorEvaluator.h:46
Eigen::TensorEvaluator< const TensorInflationOp< Strides, ArgType >, Device >::XprType
TensorInflationOp< Strides, ArgType > XprType
Definition: TensorInflation.h:81
Eigen::internal::eval
Definition: XprHelper.h:332
Eigen::TensorEvaluator::PacketAccess
@ PacketAccess
Definition: TensorEvaluator.h:47
Eigen::TensorEvaluator< const TensorInflationOp< Strides, ArgType >, Device >::m_inputStrides
array< Index, NumDims > m_inputStrides
Definition: TensorInflation.h:239
Eigen::TensorInflationOp::strides
const EIGEN_DEVICE_FUNC Strides & strides() const
Definition: TensorInflation.h:66
Eigen::TensorOpCost
Definition: TensorCostModel.h:25
test_callbacks.value
value
Definition: test_callbacks.py:160
Eigen::TensorEvaluator< const TensorInflationOp< Strides, ArgType >, Device >::m_strides
const Strides m_strides
Definition: TensorInflation.h:241
i
int i
Definition: BiCGSTAB_step_by_step.cpp:9
Eigen::TensorEvaluator< const TensorInflationOp< Strides, ArgType >, Device >::m_fastStrides
array< internal::TensorIntDivisor< Index >, NumDims > m_fastStrides
Definition: TensorInflation.h:242
Eigen::TensorEvaluator< const TensorInflationOp< Strides, ArgType >, Device >::m_outputStrides
array< Index, NumDims > m_outputStrides
Definition: TensorInflation.h:238
Scalar
SCALAR Scalar
Definition: bench_gemm.cpp:46
Eigen::internal::traits< TensorInflationOp< Strides, XprType > >::XprTraits
traits< XprType > XprTraits
Definition: TensorInflation.h:27
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
Eigen::internal::traits< TensorInflationOp< Strides, XprType > >::PointerType
XprTraits::PointerType PointerType
Definition: TensorInflation.h:34
Eigen::TensorEvaluator< const TensorInflationOp< Strides, ArgType >, Device >::EvaluatorPointerType
Storage::Type EvaluatorPointerType
Definition: TensorInflation.h:90


gtsam
Author(s):
autogenerated on Fri Nov 1 2024 03:38:03