TensorArgMax.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 Eugene Brevdo <ebrevdo@gmail.com>
5 // Benoit Steiner <benoit.steiner.goog@gmail.com>
6 //
7 // This Source Code Form is subject to the terms of the Mozilla
8 // Public License v. 2.0. If a copy of the MPL was not distributed
9 // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
10 
11 #ifndef EIGEN_CXX11_TENSOR_TENSOR_ARG_MAX_H
12 #define EIGEN_CXX11_TENSOR_TENSOR_ARG_MAX_H
13 
14 namespace Eigen {
15 namespace internal {
16 
24 template<typename XprType>
25 struct traits<TensorIndexTupleOp<XprType> > : public traits<XprType>
26 {
28  typedef typename XprTraits::StorageKind StorageKind;
29  typedef typename XprTraits::Index Index;
31  typedef typename XprType::Nested Nested;
33  static const int NumDimensions = XprTraits::NumDimensions;
34  static const int Layout = XprTraits::Layout;
35 };
36 
37 template<typename XprType>
38 struct eval<TensorIndexTupleOp<XprType>, Eigen::Dense>
39 {
41 };
42 
43 template<typename XprType>
44 struct nested<TensorIndexTupleOp<XprType>, 1,
45  typename eval<TensorIndexTupleOp<XprType> >::type>
46 {
48 };
49 
50 } // end namespace internal
51 
52 template<typename XprType>
53 class TensorIndexTupleOp : public TensorBase<TensorIndexTupleOp<XprType>, ReadOnlyAccessors>
54 {
55  public:
62 
63  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorIndexTupleOp(const XprType& expr)
64  : m_xpr(expr) {}
65 
66  EIGEN_DEVICE_FUNC
68  expression() const { return m_xpr; }
69 
70  protected:
71  typename XprType::Nested m_xpr;
72 };
73 
74 // Eval as rvalue
75 template<typename ArgType, typename Device>
76 struct TensorEvaluator<const TensorIndexTupleOp<ArgType>, Device>
77 {
79  typedef typename XprType::Index Index;
80  typedef typename XprType::Scalar Scalar;
82 
84  static const int NumDims = internal::array_size<Dimensions>::value;
85 
86  enum {
87  IsAligned = /*TensorEvaluator<ArgType, Device>::IsAligned*/ false,
88  PacketAccess = /*TensorEvaluator<ArgType, Device>::PacketAccess*/ false,
89  BlockAccess = false,
91  CoordAccess = false, // to be implemented
92  RawAccess = false
93  };
94 
95  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorEvaluator(const XprType& op, const Device& device)
96  : m_impl(op.expression(), device) { }
97 
98  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Dimensions& dimensions() const {
99  return m_impl.dimensions();
100  }
101 
102  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE bool evalSubExprsIfNeeded(Scalar* /*data*/) {
103  m_impl.evalSubExprsIfNeeded(NULL);
104  return true;
105  }
106  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void cleanup() {
107  m_impl.cleanup();
108  }
109 
110  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE CoeffReturnType coeff(Index index) const
111  {
112  return CoeffReturnType(index, m_impl.coeff(index));
113  }
114 
115  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorOpCost
116  costPerCoeff(bool vectorized) const {
117  return m_impl.costPerCoeff(vectorized) + TensorOpCost(0, 0, 1);
118  }
119 
120  EIGEN_DEVICE_FUNC Scalar* data() const { return NULL; }
121 
122  protected:
124 };
125 
126 namespace internal {
127 
134 template<typename ReduceOp, typename Dims, typename XprType>
135 struct traits<TensorTupleReducerOp<ReduceOp, Dims, XprType> > : public traits<XprType>
136 {
138  typedef typename XprTraits::StorageKind StorageKind;
139  typedef typename XprTraits::Index Index;
140  typedef Index Scalar;
141  typedef typename XprType::Nested Nested;
143  static const int NumDimensions = XprTraits::NumDimensions - array_size<Dims>::value;
144  static const int Layout = XprTraits::Layout;
145 };
146 
147 template<typename ReduceOp, typename Dims, typename XprType>
148 struct eval<TensorTupleReducerOp<ReduceOp, Dims, XprType>, Eigen::Dense>
149 {
151 };
152 
153 template<typename ReduceOp, typename Dims, typename XprType>
154 struct nested<TensorTupleReducerOp<ReduceOp, Dims, XprType>, 1,
155  typename eval<TensorTupleReducerOp<ReduceOp, Dims, XprType> >::type>
156 {
158 };
159 
160 } // end namespace internal
161 
162 template<typename ReduceOp, typename Dims, typename XprType>
163 class TensorTupleReducerOp : public TensorBase<TensorTupleReducerOp<ReduceOp, Dims, XprType>, ReadOnlyAccessors>
164 {
165  public:
171  typedef Index CoeffReturnType;
172 
173  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorTupleReducerOp(const XprType& expr,
174  const ReduceOp& reduce_op,
175  const int return_dim,
176  const Dims& reduce_dims)
177  : m_xpr(expr), m_reduce_op(reduce_op), m_return_dim(return_dim), m_reduce_dims(reduce_dims) {}
178 
179  EIGEN_DEVICE_FUNC
181  expression() const { return m_xpr; }
182 
183  EIGEN_DEVICE_FUNC
184  const ReduceOp& reduce_op() const { return m_reduce_op; }
185 
186  EIGEN_DEVICE_FUNC
187  const Dims& reduce_dims() const { return m_reduce_dims; }
188 
189  EIGEN_DEVICE_FUNC
190  int return_dim() const { return m_return_dim; }
191 
192  protected:
193  typename XprType::Nested m_xpr;
194  const ReduceOp m_reduce_op;
195  const int m_return_dim;
196  const Dims m_reduce_dims;
197 };
198 
199 // Eval as rvalue
200 template<typename ReduceOp, typename Dims, typename ArgType, typename Device>
201 struct TensorEvaluator<const TensorTupleReducerOp<ReduceOp, Dims, ArgType>, Device>
202 {
204  typedef typename XprType::Index Index;
205  typedef typename XprType::Scalar Scalar;
212 
213  enum {
214  IsAligned = /*TensorEvaluator<ArgType, Device>::IsAligned*/ false,
215  PacketAccess = /*TensorEvaluator<ArgType, Device>::PacketAccess*/ false,
216  BlockAccess = false,
217  Layout = TensorEvaluator<const TensorReductionOp<ReduceOp, Dims, const TensorIndexTupleOp<ArgType> >, Device>::Layout,
218  CoordAccess = false, // to be implemented
219  RawAccess = false
220  };
221 
222  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorEvaluator(const XprType& op, const Device& device)
223  : m_orig_impl(op.expression(), device),
224  m_impl(op.expression().index_tuples().reduce(op.reduce_dims(), op.reduce_op()), device),
225  m_return_dim(op.return_dim()) {
226 
227  gen_strides(m_orig_impl.dimensions(), m_strides);
228  if (Layout == static_cast<int>(ColMajor)) {
229  const Index total_size = internal::array_prod(m_orig_impl.dimensions());
230  m_stride_mod = (m_return_dim < NumDims - 1) ? m_strides[m_return_dim + 1] : total_size;
231  } else {
232  const Index total_size = internal::array_prod(m_orig_impl.dimensions());
233  m_stride_mod = (m_return_dim > 0) ? m_strides[m_return_dim - 1] : total_size;
234  }
235  m_stride_div = m_strides[m_return_dim];
236  }
237 
238  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Dimensions& dimensions() const {
239  return m_impl.dimensions();
240  }
241 
242  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE bool evalSubExprsIfNeeded(Scalar* /*data*/) {
243  m_impl.evalSubExprsIfNeeded(NULL);
244  return true;
245  }
246  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void cleanup() {
247  m_impl.cleanup();
248  }
249 
250  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE CoeffReturnType coeff(Index index) const {
251  const TupleType v = m_impl.coeff(index);
252  return (m_return_dim < 0) ? v.first : (v.first % m_stride_mod) / m_stride_div;
253  }
254 
255  EIGEN_DEVICE_FUNC Scalar* data() const { return NULL; }
256 
257  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorOpCost
258  costPerCoeff(bool vectorized) const {
259  const double compute_cost = 1.0 +
260  (m_return_dim < 0 ? 0.0 : (TensorOpCost::ModCost<Index>() + TensorOpCost::DivCost<Index>()));
261  return m_orig_impl.costPerCoeff(vectorized) +
262  m_impl.costPerCoeff(vectorized) + TensorOpCost(0, 0, compute_cost);
263  }
264 
265  private:
266  EIGEN_DEVICE_FUNC void gen_strides(const InputDimensions& dims, StrideDims& strides) {
267  if (m_return_dim < 0) {
268  return; // Won't be using the strides.
269  }
270  eigen_assert(m_return_dim < NumDims &&
271  "Asking to convert index to a dimension outside of the rank");
272 
273  // Calculate m_stride_div and m_stride_mod, which are used to
274  // calculate the value of an index w.r.t. the m_return_dim.
275  if (Layout == static_cast<int>(ColMajor)) {
276  strides[0] = 1;
277  for (int i = 1; i < NumDims; ++i) {
278  strides[i] = strides[i-1] * dims[i-1];
279  }
280  } else {
281  strides[NumDims-1] = 1;
282  for (int i = NumDims - 2; i >= 0; --i) {
283  strides[i] = strides[i+1] * dims[i+1];
284  }
285  }
286  }
287 
288  protected:
289  TensorEvaluator<const TensorIndexTupleOp<ArgType>, Device> m_orig_impl;
290  TensorEvaluator<const TensorReductionOp<ReduceOp, Dims, const TensorIndexTupleOp<ArgType> >, Device> m_impl;
291  const int m_return_dim;
292  StrideDims m_strides;
295 };
296 
297 } // end namespace Eigen
298 
299 #endif // EIGEN_CXX11_TENSOR_TENSOR_ARG_MAX_H
Eigen::NumTraits< Scalar >::Real RealScalar
Definition: TensorArgMax.h:167
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE std::ptrdiff_t array_prod(const Sizes< Indices... > &)
#define EIGEN_STRONG_INLINE
Definition: Macros.h:494
EIGEN_DEVICE_FUNC int return_dim() const
Definition: TensorArgMax.h:190
TensorEvaluator< const TensorIndexTupleOp< ArgType >, Device > m_orig_impl
Definition: TensorArgMax.h:289
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE bool evalSubExprsIfNeeded(Scalar *)
Definition: TensorArgMax.h:102
ArrayXcf v
Definition: Cwise_arg.cpp:1
XprType::Nested m_xpr
Definition: TensorArgMax.h:71
Namespace containing all symbols from the Eigen library.
Definition: jet.h:637
TensorEvaluator< const TensorReductionOp< ReduceOp, Dims, const TensorIndexTupleOp< ArgType > >, Device >::Dimensions Dimensions
Definition: TensorArgMax.h:208
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Dimensions & dimensions() const
Definition: TensorArgMax.h:238
A cost model used to limit the number of threads used for evaluating tensor expression.
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorOpCost costPerCoeff(bool vectorized) const
Definition: TensorArgMax.h:116
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE CoeffReturnType coeff(Index index) const
Definition: TensorArgMax.h:110
Eigen::internal::traits< TensorTupleReducerOp >::StorageKind StorageKind
Definition: TensorArgMax.h:169
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorTupleReducerOp(const XprType &expr, const ReduceOp &reduce_op, const int return_dim, const Dims &reduce_dims)
Definition: TensorArgMax.h:173
Eigen::internal::traits< TensorTupleReducerOp >::Index Index
Definition: TensorArgMax.h:170
TensorEvaluator< const TensorIndexTupleOp< ArgType >, Device >::Dimensions InputDimensions
Definition: TensorArgMax.h:209
TensorEvaluator< const TensorReductionOp< ReduceOp, Dims, const TensorIndexTupleOp< ArgType > >, Device > m_impl
Definition: TensorArgMax.h:290
Eigen::internal::nested< TensorTupleReducerOp >::type Nested
Definition: TensorArgMax.h:168
Tuple< Index, typename XprType::CoeffReturnType > CoeffReturnType
Definition: TensorArgMax.h:61
Eigen::NumTraits< Scalar >::Real RealScalar
Definition: TensorArgMax.h:57
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:33
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorIndexTupleOp(const XprType &expr)
Definition: TensorArgMax.h:63
#define eigen_assert(x)
Definition: Macros.h:579
Tuple< Index, typename XprTraits::Scalar > Scalar
Definition: TensorArgMax.h:30
#define NULL
Definition: ccolamd.c:609
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorEvaluator(const XprType &op, const Device &device)
Definition: TensorArgMax.h:95
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void cleanup()
Definition: TensorArgMax.h:106
The tensor base class.
Definition: TensorBase.h:829
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE bool evalSubExprsIfNeeded(Scalar *)
Definition: TensorArgMax.h:242
EIGEN_DEVICE_FUNC const internal::remove_all< typename XprType::Nested >::type & expression() const
Definition: TensorArgMax.h:68
Eigen::internal::traits< TensorIndexTupleOp >::StorageKind StorageKind
Definition: TensorArgMax.h:59
Eigen::internal::traits< TensorTupleReducerOp >::Scalar Scalar
Definition: TensorArgMax.h:166
EIGEN_DEVICE_FUNC const ReduceOp & reduce_op() const
Definition: TensorArgMax.h:184
Eigen::internal::nested< TensorIndexTupleOp >::type Nested
Definition: TensorArgMax.h:58
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorOpCost costPerCoeff(bool vectorized) const
Definition: TensorArgMax.h:258
Eigen::internal::traits< TensorIndexTupleOp >::Scalar Scalar
Definition: TensorArgMax.h:56
EIGEN_DEVICE_FUNC void gen_strides(const InputDimensions &dims, StrideDims &strides)
Definition: TensorArgMax.h:266
TensorEvaluator< ArgType, Device >::Dimensions Dimensions
Definition: TensorArgMax.h:83
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Dimensions & dimensions() const
Definition: TensorArgMax.h:98
EIGEN_DEVICE_FUNC const internal::remove_all< typename XprType::Nested >::type & expression() const
Definition: TensorArgMax.h:181
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE CoeffReturnType coeff(Index index) const
Definition: TensorArgMax.h:250
EIGEN_DEVICE_FUNC const Dims & reduce_dims() const
Definition: TensorArgMax.h:187
Eigen::internal::traits< TensorIndexTupleOp >::Index Index
Definition: TensorArgMax.h:60
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorEvaluator(const XprType &op, const Device &device)
Definition: TensorArgMax.h:222


gtsam
Author(s):
autogenerated on Sat May 8 2021 02:45:10