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 #include "./InternalHeaderCheck.h"
15 
16 namespace Eigen {
17 namespace internal {
18 
26 template<typename XprType>
27 struct traits<TensorIndexPairOp<XprType> > : public traits<XprType>
28 {
29  typedef traits<XprType> XprTraits;
30  typedef typename XprTraits::StorageKind StorageKind;
31  typedef typename XprTraits::Index Index;
32  typedef Pair<Index, typename XprTraits::Scalar> Scalar;
33  typedef typename XprType::Nested Nested;
34  typedef std::remove_reference_t<Nested> Nested_;
35  static constexpr int NumDimensions = XprTraits::NumDimensions;
36  static constexpr int Layout = XprTraits::Layout;
37 };
38 
39 template<typename XprType>
40 struct eval<TensorIndexPairOp<XprType>, Eigen::Dense>
41 {
42  typedef const TensorIndexPairOp<XprType>EIGEN_DEVICE_REF type;
43 };
44 
45 template<typename XprType>
46 struct nested<TensorIndexPairOp<XprType>, 1,
47  typename eval<TensorIndexPairOp<XprType> >::type>
48 {
49  typedef TensorIndexPairOp<XprType> type;
50 };
51 
52 } // end namespace internal
53 
54 template<typename XprType>
55 class TensorIndexPairOp : public TensorBase<TensorIndexPairOp<XprType>, ReadOnlyAccessors>
56 {
57  public:
58  typedef typename Eigen::internal::traits<TensorIndexPairOp>::Scalar Scalar;
60  typedef typename Eigen::internal::nested<TensorIndexPairOp>::type Nested;
61  typedef typename Eigen::internal::traits<TensorIndexPairOp>::StorageKind StorageKind;
62  typedef typename Eigen::internal::traits<TensorIndexPairOp>::Index Index;
64 
65  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorIndexPairOp(const XprType& expr)
66  : m_xpr(expr) {}
67 
70  expression() const { return m_xpr; }
71 
72  protected:
73  typename XprType::Nested m_xpr;
74 };
75 
76 // Eval as rvalue
77 template<typename ArgType, typename Device>
78 struct TensorEvaluator<const TensorIndexPairOp<ArgType>, Device>
79 {
81  typedef typename XprType::Index Index;
82  typedef typename XprType::Scalar Scalar;
84 
86  static constexpr int NumDims = internal::array_size<Dimensions>::value;
89 
90  enum {
91  IsAligned = /*TensorEvaluator<ArgType, Device>::IsAligned*/ false,
92  PacketAccess = /*TensorEvaluator<ArgType, Device>::PacketAccess*/ false,
93  BlockAccess = false,
95  CoordAccess = false, // to be implemented
96  RawAccess = false
97  };
99 
100  //===- Tensor block evaluation strategy (see TensorBlock.h) -------------===//
101  typedef internal::TensorBlockNotImplemented TensorBlock;
102  //===--------------------------------------------------------------------===//
103 
104  EIGEN_STRONG_INLINE TensorEvaluator(const XprType& op, const Device& device)
105  : m_impl(op.expression(), device) { }
106 
107  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Dimensions& dimensions() const {
108  return m_impl.dimensions();
109  }
110 
111  EIGEN_STRONG_INLINE bool evalSubExprsIfNeeded(EvaluatorPointerType /*data*/) {
112  m_impl.evalSubExprsIfNeeded(NULL);
113  return true;
114  }
115  EIGEN_STRONG_INLINE void cleanup() {
116  m_impl.cleanup();
117  }
118 
119  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE CoeffReturnType coeff(Index index) const
120  {
121  return CoeffReturnType(index, m_impl.coeff(index));
122  }
123 
124  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorOpCost
125  costPerCoeff(bool vectorized) const {
126  return m_impl.costPerCoeff(vectorized) + TensorOpCost(0, 0, 1);
127  }
128 
129  EIGEN_DEVICE_FUNC EvaluatorPointerType data() const { return NULL; }
130 
131  protected:
133 };
134 
135 namespace internal {
136 
143 template<typename ReduceOp, typename Dims, typename XprType>
144 struct traits<TensorPairReducerOp<ReduceOp, Dims, XprType> > : public traits<XprType>
145 {
146  typedef traits<XprType> XprTraits;
147  typedef typename XprTraits::StorageKind StorageKind;
148  typedef typename XprTraits::Index Index;
149  typedef Index Scalar;
150  typedef typename XprType::Nested Nested;
151  typedef std::remove_reference_t<Nested> Nested_;
152  static constexpr int NumDimensions = XprTraits::NumDimensions - array_size<Dims>::value;
153  static constexpr int Layout = XprTraits::Layout;
154 };
155 
156 template<typename ReduceOp, typename Dims, typename XprType>
157 struct eval<TensorPairReducerOp<ReduceOp, Dims, XprType>, Eigen::Dense>
158 {
159  typedef const TensorPairReducerOp<ReduceOp, Dims, XprType>EIGEN_DEVICE_REF type;
160 };
161 
162 template<typename ReduceOp, typename Dims, typename XprType>
163 struct nested<TensorPairReducerOp<ReduceOp, Dims, XprType>, 1,
164  typename eval<TensorPairReducerOp<ReduceOp, Dims, XprType> >::type>
165 {
166  typedef TensorPairReducerOp<ReduceOp, Dims, XprType> type;
167 };
168 
169 } // end namespace internal
170 
171 template<typename ReduceOp, typename Dims, typename XprType>
172 class TensorPairReducerOp : public TensorBase<TensorPairReducerOp<ReduceOp, Dims, XprType>, ReadOnlyAccessors>
173 {
174  public:
175  typedef typename Eigen::internal::traits<TensorPairReducerOp>::Scalar Scalar;
177  typedef typename Eigen::internal::nested<TensorPairReducerOp>::type Nested;
178  typedef typename Eigen::internal::traits<TensorPairReducerOp>::StorageKind StorageKind;
179  typedef typename Eigen::internal::traits<TensorPairReducerOp>::Index Index;
181 
182  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorPairReducerOp(const XprType& expr,
183  const ReduceOp& reduce_op,
184  const Index return_dim,
185  const Dims& reduce_dims)
187 
190  expression() const { return m_xpr; }
191 
193  const ReduceOp& reduce_op() const { return m_reduce_op; }
194 
196  const Dims& reduce_dims() const { return m_reduce_dims; }
197 
199  Index return_dim() const { return m_return_dim; }
200 
201  protected:
202  typename XprType::Nested m_xpr;
203  const ReduceOp m_reduce_op;
205  const Dims m_reduce_dims;
206 };
207 
208 // Eval as rvalue
209 template<typename ReduceOp, typename Dims, typename ArgType, typename Device>
210 struct TensorEvaluator<const TensorPairReducerOp<ReduceOp, Dims, ArgType>, Device>
211 {
213  typedef typename XprType::Index Index;
214  typedef typename XprType::Scalar Scalar;
219  static constexpr int NumDims = internal::array_size<InputDimensions>::value;
224 
225  enum {
226  IsAligned = /*TensorEvaluator<ArgType, Device>::IsAligned*/ false,
227  PacketAccess = /*TensorEvaluator<ArgType, Device>::PacketAccess*/ false,
228  BlockAccess = false,
230  CoordAccess = false, // to be implemented
231  RawAccess = false
232  };
234  //===- Tensor block evaluation strategy (see TensorBlock.h) -------------===//
235  typedef internal::TensorBlockNotImplemented TensorBlock;
236  //===--------------------------------------------------------------------===//
237 
238  EIGEN_STRONG_INLINE TensorEvaluator(const XprType& op, const Device& device)
239  : m_orig_impl(op.expression(), device),
240  m_impl(op.expression().index_pairs().reduce(op.reduce_dims(), op.reduce_op()), device),
241  m_return_dim(op.return_dim())
242  {
243  gen_strides(m_orig_impl.dimensions(), m_strides);
244  if (Layout == static_cast<int>(ColMajor)) {
245  const Index total_size = internal::array_prod(m_orig_impl.dimensions());
246  m_stride_mod = (m_return_dim < NumDims - 1) ? m_strides[m_return_dim + 1] : total_size;
247  } else {
248  const Index total_size = internal::array_prod(m_orig_impl.dimensions());
249  m_stride_mod = (m_return_dim > 0) ? m_strides[m_return_dim - 1] : total_size;
250  }
251  // If m_return_dim is not a valid index, returns 1 or this can crash on Windows.
252  m_stride_div = ((m_return_dim >= 0) &&
253  (m_return_dim < static_cast<Index>(m_strides.size())))
254  ? m_strides[m_return_dim] : 1;
255  }
256 
257  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Dimensions& dimensions() const {
258  return m_impl.dimensions();
259  }
260 
261  EIGEN_STRONG_INLINE bool evalSubExprsIfNeeded(EvaluatorPointerType /*data*/) {
262  m_impl.evalSubExprsIfNeeded(NULL);
263  return true;
264  }
265  EIGEN_STRONG_INLINE void cleanup() {
266  m_impl.cleanup();
267  }
268 
269  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE CoeffReturnType coeff(Index index) const {
270  const PairType v = m_impl.coeff(index);
271  return (m_return_dim < 0) ? v.first : (v.first % m_stride_mod) / m_stride_div;
272  }
273 
274  EIGEN_DEVICE_FUNC EvaluatorPointerType data() const { return NULL; }
275 
276  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorOpCost
277  costPerCoeff(bool vectorized) const {
278  const double compute_cost = 1.0 +
279  (m_return_dim < 0 ? 0.0 : (TensorOpCost::ModCost<Index>() + TensorOpCost::DivCost<Index>()));
280  return m_orig_impl.costPerCoeff(vectorized) +
281  m_impl.costPerCoeff(vectorized) + TensorOpCost(0, 0, compute_cost);
282  }
283 
284  private:
286  if (m_return_dim < 0) {
287  return; // Won't be using the strides.
288  }
289  eigen_assert(m_return_dim < NumDims &&
290  "Asking to convert index to a dimension outside of the rank");
291 
292  // Calculate m_stride_div and m_stride_mod, which are used to
293  // calculate the value of an index w.r.t. the m_return_dim.
294  if (Layout == static_cast<int>(ColMajor)) {
295  strides[0] = 1;
296  for (int i = 1; i < NumDims; ++i) {
297  strides[i] = strides[i-1] * dims[i-1];
298  }
299  } else {
300  strides[NumDims-1] = 1;
301  for (int i = NumDims - 2; i >= 0; --i) {
302  strides[i] = strides[i+1] * dims[i+1];
303  }
304  }
305  }
306 
307  protected:
314 };
315 
316 } // end namespace Eigen
317 
318 #endif // EIGEN_CXX11_TENSOR_TENSOR_ARG_MAX_H
Array< int, Dynamic, 1 > v
int i
#define EIGEN_DEVICE_FUNC
#define eigen_assert(x)
#define EIGEN_DEVICE_REF
Definition: TensorMacros.h:36
The tensor base class.
Eigen::internal::traits< TensorIndexPairOp >::Scalar Scalar
Definition: TensorArgMax.h:58
Eigen::internal::traits< TensorIndexPairOp >::Index Index
Definition: TensorArgMax.h:62
XprType::Nested m_xpr
Definition: TensorArgMax.h:73
Eigen::internal::traits< TensorIndexPairOp >::StorageKind StorageKind
Definition: TensorArgMax.h:61
TensorIndexPairOp(const XprType &expr)
Definition: TensorArgMax.h:65
const internal::remove_all_t< typename XprType::Nested > & expression() const
Definition: TensorArgMax.h:70
Eigen::NumTraits< Scalar >::Real RealScalar
Definition: TensorArgMax.h:59
Eigen::internal::nested< TensorIndexPairOp >::type Nested
Definition: TensorArgMax.h:60
Pair< Index, typename XprType::CoeffReturnType > CoeffReturnType
Definition: TensorArgMax.h:63
const Dims & reduce_dims() const
Definition: TensorArgMax.h:196
Eigen::internal::traits< TensorPairReducerOp >::StorageKind StorageKind
Definition: TensorArgMax.h:178
const internal::remove_all_t< typename XprType::Nested > & expression() const
Definition: TensorArgMax.h:190
TensorPairReducerOp(const XprType &expr, const ReduceOp &reduce_op, const Index return_dim, const Dims &reduce_dims)
Definition: TensorArgMax.h:182
Eigen::internal::traits< TensorPairReducerOp >::Index Index
Definition: TensorArgMax.h:179
Eigen::NumTraits< Scalar >::Real RealScalar
Definition: TensorArgMax.h:176
const ReduceOp & reduce_op() const
Definition: TensorArgMax.h:193
Eigen::internal::traits< TensorPairReducerOp >::Scalar Scalar
Definition: TensorArgMax.h:175
Eigen::internal::nested< TensorPairReducerOp >::type Nested
Definition: TensorArgMax.h:177
const ReduceOp m_reduce_op
Definition: TensorArgMax.h:203
typename remove_all< T >::type remove_all_t
EIGEN_ALWAYS_INLINE DSizes< IndexType, NumDims > strides(const DSizes< IndexType, NumDims > &dimensions)
Definition: TensorBlock.h:28
constexpr auto array_prod(const array< T, N > &arr) -> decltype(array_reduce< product_op, T, N >(arr, static_cast< T >(1)))
: TensorContractionSycl.h, provides various tensor contraction kernel for SYCL backend
std::array< T, N > array
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
TensorEvaluator< ArgType, Device >::Dimensions Dimensions
Definition: TensorArgMax.h:85
TensorEvaluator< const TensorReductionOp< ReduceOp, Dims, const TensorIndexPairOp< ArgType > >, Device >::Dimensions Dimensions
Definition: TensorArgMax.h:217
TensorEvaluator< const TensorIndexPairOp< ArgType >, Device > m_orig_impl
Definition: TensorArgMax.h:308
void gen_strides(const InputDimensions &dims, StrideDims &strides)
Definition: TensorArgMax.h:285
TensorEvaluator< const TensorReductionOp< ReduceOp, Dims, const TensorIndexPairOp< ArgType > >, Device > m_impl
Definition: TensorArgMax.h:309
TensorEvaluator< const TensorIndexPairOp< ArgType >, Device >::Dimensions InputDimensions
Definition: TensorArgMax.h:218
A cost model used to limit the number of threads used for evaluating tensor expression.
static constexpr int Layout
Derived::Scalar CoeffReturnType