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 // IWYU pragma: private
15 #include "./InternalHeaderCheck.h"
16 
17 namespace Eigen {
18 namespace internal {
19 
27 template <typename XprType>
28 struct traits<TensorIndexPairOp<XprType>> : public traits<XprType> {
30  typedef typename XprTraits::StorageKind StorageKind;
31  typedef typename XprTraits::Index Index;
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>
42 };
43 
44 template <typename XprType>
47 };
48 
49 } // end namespace internal
50 
51 template <typename XprType>
52 class TensorIndexPairOp : public TensorBase<TensorIndexPairOp<XprType>, ReadOnlyAccessors> {
53  public:
60 
62 
64 
65  protected:
66  typename XprType::Nested m_xpr;
67 };
68 
69 // Eval as rvalue
70 template <typename ArgType, typename Device>
71 struct TensorEvaluator<const TensorIndexPairOp<ArgType>, Device> {
73  typedef typename XprType::Index Index;
74  typedef typename XprType::Scalar Scalar;
76 
78  static constexpr int NumDims = internal::array_size<Dimensions>::value;
81 
82  enum {
83  IsAligned = /*TensorEvaluator<ArgType, Device>::IsAligned*/ false,
84  PacketAccess = /*TensorEvaluator<ArgType, Device>::PacketAccess*/ false,
85  BlockAccess = false,
87  CoordAccess = false, // to be implemented
88  RawAccess = false
89  };
91 
92  //===- Tensor block evaluation strategy (see TensorBlock.h) -------------===//
94  //===--------------------------------------------------------------------===//
95 
96  EIGEN_STRONG_INLINE TensorEvaluator(const XprType& op, const Device& device) : m_impl(op.expression(), device) {}
97 
98  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Dimensions& dimensions() const { return m_impl.dimensions(); }
99 
101  m_impl.evalSubExprsIfNeeded(NULL);
102  return true;
103  }
104  EIGEN_STRONG_INLINE void cleanup() { m_impl.cleanup(); }
105 
107  return CoeffReturnType(index, m_impl.coeff(index));
108  }
109 
111  return m_impl.costPerCoeff(vectorized) + TensorOpCost(0, 0, 1);
112  }
113 
114  EIGEN_DEVICE_FUNC EvaluatorPointerType data() const { return NULL; }
115 
116  protected:
118 };
119 
120 namespace internal {
121 
128 template <typename ReduceOp, typename Dims, typename XprType>
129 struct traits<TensorPairReducerOp<ReduceOp, Dims, XprType>> : public traits<XprType> {
131  typedef typename XprTraits::StorageKind StorageKind;
132  typedef typename XprTraits::Index Index;
133  typedef Index Scalar;
134  typedef typename XprType::Nested Nested;
135  typedef std::remove_reference_t<Nested> Nested_;
136  static constexpr int NumDimensions = XprTraits::NumDimensions - array_size<Dims>::value;
137  static constexpr int Layout = XprTraits::Layout;
138 };
139 
140 template <typename ReduceOp, typename Dims, typename XprType>
141 struct eval<TensorPairReducerOp<ReduceOp, Dims, XprType>, Eigen::Dense> {
143 };
144 
145 template <typename ReduceOp, typename Dims, typename XprType>
146 struct nested<TensorPairReducerOp<ReduceOp, Dims, XprType>, 1,
147  typename eval<TensorPairReducerOp<ReduceOp, Dims, XprType>>::type> {
149 };
150 
151 } // end namespace internal
152 
153 template <typename ReduceOp, typename Dims, typename XprType>
154 class TensorPairReducerOp : public TensorBase<TensorPairReducerOp<ReduceOp, Dims, XprType>, ReadOnlyAccessors> {
155  public:
162 
164  const Index return_dim, const Dims& reduce_dims)
166 
168 
169  EIGEN_DEVICE_FUNC const ReduceOp& reduce_op() const { return m_reduce_op; }
170 
171  EIGEN_DEVICE_FUNC const Dims& reduce_dims() const { return m_reduce_dims; }
172 
174 
175  protected:
176  typename XprType::Nested m_xpr;
177  const ReduceOp m_reduce_op;
179  const Dims m_reduce_dims;
180 };
181 
182 // Eval as rvalue
183 template <typename ReduceOp, typename Dims, typename ArgType, typename Device>
184 struct TensorEvaluator<const TensorPairReducerOp<ReduceOp, Dims, ArgType>, Device> {
186  typedef typename XprType::Index Index;
187  typedef typename XprType::Scalar Scalar;
193  static constexpr int NumDims = internal::array_size<InputDimensions>::value;
198 
199  enum {
200  IsAligned = /*TensorEvaluator<ArgType, Device>::IsAligned*/ false,
201  PacketAccess = /*TensorEvaluator<ArgType, Device>::PacketAccess*/ false,
202  BlockAccess = false,
204  CoordAccess = false, // to be implemented
205  RawAccess = false
206  };
207  static constexpr int Layout =
209  //===- Tensor block evaluation strategy (see TensorBlock.h) -------------===//
211  //===--------------------------------------------------------------------===//
212 
213  EIGEN_STRONG_INLINE TensorEvaluator(const XprType& op, const Device& device)
214  : m_orig_impl(op.expression(), device),
215  m_impl(op.expression().index_pairs().reduce(op.reduce_dims(), op.reduce_op()), device),
216  m_return_dim(op.return_dim()) {
217  gen_strides(m_orig_impl.dimensions(), m_strides);
218  if (Layout == static_cast<int>(ColMajor)) {
219  const Index total_size = internal::array_prod(m_orig_impl.dimensions());
220  m_stride_mod = (m_return_dim < NumDims - 1) ? m_strides[m_return_dim + 1] : total_size;
221  } else {
222  const Index total_size = internal::array_prod(m_orig_impl.dimensions());
223  m_stride_mod = (m_return_dim > 0) ? m_strides[m_return_dim - 1] : total_size;
224  }
225  // If m_return_dim is not a valid index, returns 1 or this can crash on Windows.
226  m_stride_div =
227  ((m_return_dim >= 0) && (m_return_dim < static_cast<Index>(m_strides.size()))) ? m_strides[m_return_dim] : 1;
228  }
229 
230  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Dimensions& dimensions() const { return m_impl.dimensions(); }
231 
233  m_impl.evalSubExprsIfNeeded(NULL);
234  return true;
235  }
236  EIGEN_STRONG_INLINE void cleanup() { m_impl.cleanup(); }
237 
239  const PairType v = m_impl.coeff(index);
240  return (m_return_dim < 0) ? v.first : (v.first % m_stride_mod) / m_stride_div;
241  }
242 
243  EIGEN_DEVICE_FUNC EvaluatorPointerType data() const { return NULL; }
244 
246  const double compute_cost =
247  1.0 + (m_return_dim < 0 ? 0.0 : (TensorOpCost::ModCost<Index>() + TensorOpCost::DivCost<Index>()));
248  return m_orig_impl.costPerCoeff(vectorized) + m_impl.costPerCoeff(vectorized) + TensorOpCost(0, 0, compute_cost);
249  }
250 
251  private:
253  if (m_return_dim < 0) {
254  return; // Won't be using the strides.
255  }
256  eigen_assert(m_return_dim < NumDims && "Asking to convert index to a dimension outside of the rank");
257 
258  // Calculate m_stride_div and m_stride_mod, which are used to
259  // calculate the value of an index w.r.t. the m_return_dim.
260  if (Layout == static_cast<int>(ColMajor)) {
261  strides[0] = 1;
262  for (int i = 1; i < NumDims; ++i) {
263  strides[i] = strides[i - 1] * dims[i - 1];
264  }
265  } else {
266  strides[NumDims - 1] = 1;
267  for (int i = NumDims - 2; i >= 0; --i) {
268  strides[i] = strides[i + 1] * dims[i + 1];
269  }
270  }
271  }
272 
273  protected:
280 };
281 
282 } // end namespace Eigen
283 
284 #endif // EIGEN_CXX11_TENSOR_TENSOR_ARG_MAX_H
Array< int, Dynamic, 1 > v
Definition: Array_initializer_list_vector_cxx11.cpp:1
int i
Definition: BiCGSTAB_step_by_step.cpp:9
#define EIGEN_DEVICE_FUNC
Definition: Macros.h:892
#define eigen_assert(x)
Definition: Macros.h:910
#define EIGEN_STRONG_INLINE
Definition: Macros.h:834
#define EIGEN_DEVICE_REF
Definition: TensorMacros.h:34
Generic expression where a coefficient-wise binary operator is applied to two expressions.
Definition: CwiseBinaryOp.h:79
The tensor base class.
Definition: TensorBase.h:1026
Definition: TensorArgMax.h:52
Eigen::internal::traits< TensorIndexPairOp >::Scalar Scalar
Definition: TensorArgMax.h:54
Eigen::internal::traits< TensorIndexPairOp >::Index Index
Definition: TensorArgMax.h:58
XprType::Nested m_xpr
Definition: TensorArgMax.h:66
Eigen::internal::traits< TensorIndexPairOp >::StorageKind StorageKind
Definition: TensorArgMax.h:57
Eigen::NumTraits< Scalar >::Real RealScalar
Definition: TensorArgMax.h:55
Eigen::internal::nested< TensorIndexPairOp >::type Nested
Definition: TensorArgMax.h:56
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorIndexPairOp(const XprType &expr)
Definition: TensorArgMax.h:61
Pair< Index, typename XprType::CoeffReturnType > CoeffReturnType
Definition: TensorArgMax.h:59
EIGEN_DEVICE_FUNC const internal::remove_all_t< typename XprType::Nested > & expression() const
Definition: TensorArgMax.h:63
Definition: TensorCostModel.h:28
Definition: TensorArgMax.h:154
EIGEN_DEVICE_FUNC Index return_dim() const
Definition: TensorArgMax.h:173
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorPairReducerOp(const XprType &expr, const ReduceOp &reduce_op, const Index return_dim, const Dims &reduce_dims)
Definition: TensorArgMax.h:163
Eigen::internal::traits< TensorPairReducerOp >::StorageKind StorageKind
Definition: TensorArgMax.h:159
Eigen::internal::traits< TensorPairReducerOp >::Index Index
Definition: TensorArgMax.h:160
Eigen::NumTraits< Scalar >::Real RealScalar
Definition: TensorArgMax.h:157
XprType::Nested m_xpr
Definition: TensorArgMax.h:176
Eigen::internal::traits< TensorPairReducerOp >::Scalar Scalar
Definition: TensorArgMax.h:156
const Dims m_reduce_dims
Definition: TensorArgMax.h:179
const Index m_return_dim
Definition: TensorArgMax.h:178
Eigen::internal::nested< TensorPairReducerOp >::type Nested
Definition: TensorArgMax.h:158
EIGEN_DEVICE_FUNC const ReduceOp & reduce_op() const
Definition: TensorArgMax.h:169
EIGEN_DEVICE_FUNC const internal::remove_all_t< typename XprType::Nested > & expression() const
Definition: TensorArgMax.h:167
const ReduceOp m_reduce_op
Definition: TensorArgMax.h:177
EIGEN_DEVICE_FUNC const Dims & reduce_dims() const
Definition: TensorArgMax.h:171
Index CoeffReturnType
Definition: TensorArgMax.h:161
Definition: TensorBlock.h:566
@ ColMajor
Definition: Constants.h:318
char char * op
Definition: level2_impl.h:374
constexpr EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE auto array_prod(const array< T, N > &arr) -> decltype(array_reduce< product_op, T, N >(arr, static_cast< T >(1)))
Definition: MoreMeta.h:497
typename remove_all< T >::type remove_all_t
Definition: Meta.h:142
EIGEN_ALWAYS_INLINE DSizes< IndexType, NumDims > strides(const DSizes< IndexType, NumDims > &dimensions)
Definition: TensorBlock.h:29
Namespace containing all symbols from the Eigen library.
Definition: bench_norm.cpp:70
std::array< T, N > array
Definition: EmulateArray.h:231
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:83
Definition: Eigen_Colamd.h:49
Definition: Constants.h:519
T Real
Definition: NumTraits.h:183
Definition: TensorMeta.h:205
Definition: TensorForwardDeclarations.h:42
XprType::CoeffReturnType CoeffReturnType
Definition: TensorArgMax.h:75
EIGEN_DEVICE_FUNC EvaluatorPointerType data() const
Definition: TensorArgMax.h:114
EIGEN_STRONG_INLINE void cleanup()
Definition: TensorArgMax.h:104
TensorEvaluator< ArgType, Device > m_impl
Definition: TensorArgMax.h:117
TensorEvaluator< ArgType, Device >::Dimensions Dimensions
Definition: TensorArgMax.h:77
StorageMemory< CoeffReturnType, Device > Storage
Definition: TensorArgMax.h:79
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Dimensions & dimensions() const
Definition: TensorArgMax.h:98
EIGEN_STRONG_INLINE bool evalSubExprsIfNeeded(EvaluatorPointerType)
Definition: TensorArgMax.h:100
XprType::Scalar Scalar
Definition: TensorArgMax.h:74
Storage::Type EvaluatorPointerType
Definition: TensorArgMax.h:80
EIGEN_STRONG_INLINE TensorEvaluator(const XprType &op, const Device &device)
Definition: TensorArgMax.h:96
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE CoeffReturnType coeff(Index index) const
Definition: TensorArgMax.h:106
internal::TensorBlockNotImplemented TensorBlock
Definition: TensorArgMax.h:93
TensorIndexPairOp< ArgType > XprType
Definition: TensorArgMax.h:72
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorOpCost costPerCoeff(bool vectorized) const
Definition: TensorArgMax.h:110
EIGEN_DEVICE_FUNC void gen_strides(const InputDimensions &dims, StrideDims &strides)
Definition: TensorArgMax.h:252
EIGEN_STRONG_INLINE bool evalSubExprsIfNeeded(EvaluatorPointerType)
Definition: TensorArgMax.h:232
EIGEN_STRONG_INLINE TensorEvaluator(const XprType &op, const Device &device)
Definition: TensorArgMax.h:213
TensorIndexPairOp< ArgType >::CoeffReturnType PairType
Definition: TensorArgMax.h:189
EIGEN_STRONG_INLINE void cleanup()
Definition: TensorArgMax.h:236
TensorEvaluator< const TensorIndexPairOp< ArgType >, Device >::Dimensions InputDimensions
Definition: TensorArgMax.h:192
TensorEvaluator< const TensorReductionOp< ReduceOp, Dims, const TensorIndexPairOp< ArgType > >, Device >::Dimensions Dimensions
Definition: TensorArgMax.h:191
StorageMemory< PairType, Device > PairStorageMem
Definition: TensorArgMax.h:197
TensorPairReducerOp< ReduceOp, Dims, ArgType > XprType
Definition: TensorArgMax.h:185
EIGEN_DEVICE_FUNC EvaluatorPointerType data() const
Definition: TensorArgMax.h:243
StorageMemory< CoeffReturnType, Device > Storage
Definition: TensorArgMax.h:195
TensorEvaluator< const TensorIndexPairOp< ArgType >, Device > m_orig_impl
Definition: TensorArgMax.h:274
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Dimensions & dimensions() const
Definition: TensorArgMax.h:230
internal::TensorBlockNotImplemented TensorBlock
Definition: TensorArgMax.h:210
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE CoeffReturnType coeff(Index index) const
Definition: TensorArgMax.h:238
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorOpCost costPerCoeff(bool vectorized) const
Definition: TensorArgMax.h:245
TensorEvaluator< const TensorReductionOp< ReduceOp, Dims, const TensorIndexPairOp< ArgType > >, Device > m_impl
Definition: TensorArgMax.h:275
XprType::CoeffReturnType CoeffReturnType
Definition: TensorArgMax.h:188
A cost model used to limit the number of threads used for evaluating tensor expression.
Definition: TensorEvaluator.h:31
static constexpr int Layout
Definition: TensorEvaluator.h:46
@ PacketAccess
Definition: TensorEvaluator.h:50
@ IsAligned
Definition: TensorEvaluator.h:49
Derived::Scalar CoeffReturnType
Definition: TensorEvaluator.h:34
Definition: Meta.h:305
const TensorIndexPairOp< XprType > EIGEN_DEVICE_REF type
Definition: TensorArgMax.h:41
const TensorPairReducerOp< ReduceOp, Dims, XprType > EIGEN_DEVICE_REF type
Definition: TensorArgMax.h:142
Definition: XprHelper.h:427
Definition: TensorTraits.h:152
ref_selector< T >::type type
Definition: TensorTraits.h:153
XprTraits::StorageKind StorageKind
Definition: TensorArgMax.h:30
XprTraits::Index Index
Definition: TensorArgMax.h:31
traits< XprType > XprTraits
Definition: TensorArgMax.h:29
XprType::Nested Nested
Definition: TensorArgMax.h:33
Pair< Index, typename XprTraits::Scalar > Scalar
Definition: TensorArgMax.h:32
std::remove_reference_t< Nested > Nested_
Definition: TensorArgMax.h:34
std::remove_reference_t< Nested > Nested_
Definition: TensorArgMax.h:135
XprTraits::StorageKind StorageKind
Definition: TensorArgMax.h:131
traits< XprType > XprTraits
Definition: TensorArgMax.h:130
Definition: ForwardDeclarations.h:21