TR-mbed 1.0
Loading...
Searching...
No Matches
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
14namespace Eigen {
15namespace internal {
16
24template<typename XprType>
25struct 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
37template<typename XprType>
42
43template<typename XprType>
49
50} // end namespace internal
51
52template<typename XprType>
73
74// Eval as rvalue
75template<typename ArgType, typename Device>
76struct 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;
87
88 enum {
89 IsAligned = /*TensorEvaluator<ArgType, Device>::IsAligned*/ false,
90 PacketAccess = /*TensorEvaluator<ArgType, Device>::PacketAccess*/ false,
91 BlockAccess = false,
94 CoordAccess = false, // to be implemented
95 RawAccess = false
96 };
97
98 //===- Tensor block evaluation strategy (see TensorBlock.h) -------------===//
100 //===--------------------------------------------------------------------===//
101
102 EIGEN_STRONG_INLINE TensorEvaluator(const XprType& op, const Device& device)
103 : m_impl(op.expression(), device) { }
104
106 return m_impl.dimensions();
107 }
108
110 m_impl.evalSubExprsIfNeeded(NULL);
111 return true;
112 }
114 m_impl.cleanup();
115 }
116
118 {
119 return CoeffReturnType(index, m_impl.coeff(index));
120 }
121
123 costPerCoeff(bool vectorized) const {
124 return m_impl.costPerCoeff(vectorized) + TensorOpCost(0, 0, 1);
125 }
126
128
129#ifdef EIGEN_USE_SYCL
130 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void bind(cl::sycl::handler &cgh) const {
131 m_impl.bind(cgh);
132 }
133#endif
134
135 protected:
137};
138
139namespace internal {
140
147template<typename ReduceOp, typename Dims, typename XprType>
149{
151 typedef typename XprTraits::StorageKind StorageKind;
152 typedef typename XprTraits::Index Index;
153 typedef Index Scalar;
154 typedef typename XprType::Nested Nested;
156 static const int NumDimensions = XprTraits::NumDimensions - array_size<Dims>::value;
157 static const int Layout = XprTraits::Layout;
158};
159
160template<typename ReduceOp, typename Dims, typename XprType>
165
166template<typename ReduceOp, typename Dims, typename XprType>
172
173} // end namespace internal
174
175template<typename ReduceOp, typename Dims, typename XprType>
176class TensorTupleReducerOp : public TensorBase<TensorTupleReducerOp<ReduceOp, Dims, XprType>, ReadOnlyAccessors>
177{
178 public:
185
191
194 expression() const { return m_xpr; }
195
197 const ReduceOp& reduce_op() const { return m_reduce_op; }
198
200 const Dims& reduce_dims() const { return m_reduce_dims; }
201
203 Index return_dim() const { return m_return_dim; }
204
205 protected:
206 typename XprType::Nested m_xpr;
207 const ReduceOp m_reduce_op;
209 const Dims m_reduce_dims;
210};
211
212// Eval as rvalue
213template<typename ReduceOp, typename Dims, typename ArgType, typename Device>
214struct TensorEvaluator<const TensorTupleReducerOp<ReduceOp, Dims, ArgType>, Device>
215{
217 typedef typename XprType::Index Index;
218 typedef typename XprType::Scalar Scalar;
228
229 enum {
230 IsAligned = /*TensorEvaluator<ArgType, Device>::IsAligned*/ false,
231 PacketAccess = /*TensorEvaluator<ArgType, Device>::PacketAccess*/ false,
232 BlockAccess = false,
235 CoordAccess = false, // to be implemented
236 RawAccess = false
237 };
238
239 //===- Tensor block evaluation strategy (see TensorBlock.h) -------------===//
241 //===--------------------------------------------------------------------===//
242
243 EIGEN_STRONG_INLINE TensorEvaluator(const XprType& op, const Device& device)
244 : m_orig_impl(op.expression(), device),
245 m_impl(op.expression().index_tuples().reduce(op.reduce_dims(), op.reduce_op()), device),
246 m_return_dim(op.return_dim())
247 {
248 gen_strides(m_orig_impl.dimensions(), m_strides);
249 if (Layout == static_cast<int>(ColMajor)) {
250 const Index total_size = internal::array_prod(m_orig_impl.dimensions());
251 m_stride_mod = (m_return_dim < NumDims - 1) ? m_strides[m_return_dim + 1] : total_size;
252 } else {
253 const Index total_size = internal::array_prod(m_orig_impl.dimensions());
254 m_stride_mod = (m_return_dim > 0) ? m_strides[m_return_dim - 1] : total_size;
255 }
256 // If m_return_dim is not a valid index, returns 1 or this can crash on Windows.
257 m_stride_div = ((m_return_dim >= 0) &&
258 (m_return_dim < static_cast<Index>(m_strides.size())))
259 ? m_strides[m_return_dim] : 1;
260 }
261
263 return m_impl.dimensions();
264 }
265
267 m_impl.evalSubExprsIfNeeded(NULL);
268 return true;
269 }
271 m_impl.cleanup();
272 }
273
275 const TupleType v = m_impl.coeff(index);
276 return (m_return_dim < 0) ? v.first : (v.first % m_stride_mod) / m_stride_div;
277 }
278
280#ifdef EIGEN_USE_SYCL
281 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void bind(cl::sycl::handler &cgh) const {
282 m_impl.bind(cgh);
283 m_orig_impl.bind(cgh);
284 }
285#endif
286
288 costPerCoeff(bool vectorized) const {
289 const double compute_cost = 1.0 +
290 (m_return_dim < 0 ? 0.0 : (TensorOpCost::ModCost<Index>() + TensorOpCost::DivCost<Index>()));
291 return m_orig_impl.costPerCoeff(vectorized) +
292 m_impl.costPerCoeff(vectorized) + TensorOpCost(0, 0, compute_cost);
293 }
294
295 private:
296 EIGEN_DEVICE_FUNC void gen_strides(const InputDimensions& dims, StrideDims& strides) {
297 if (m_return_dim < 0) {
298 return; // Won't be using the strides.
299 }
300 eigen_assert(m_return_dim < NumDims &&
301 "Asking to convert index to a dimension outside of the rank");
302
303 // Calculate m_stride_div and m_stride_mod, which are used to
304 // calculate the value of an index w.r.t. the m_return_dim.
305 if (Layout == static_cast<int>(ColMajor)) {
306 strides[0] = 1;
307 for (int i = 1; i < NumDims; ++i) {
308 strides[i] = strides[i-1] * dims[i-1];
309 }
310 } else {
311 strides[NumDims-1] = 1;
312 for (int i = NumDims - 2; i >= 0; --i) {
313 strides[i] = strides[i+1] * dims[i+1];
314 }
315 }
316 }
317
318 protected:
325};
326
327} // end namespace Eigen
328
329#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:976
#define eigen_assert(x)
Definition Macros.h:1037
#define EIGEN_STRONG_INLINE
Definition Macros.h:917
#define EIGEN_DEVICE_REF
Definition TensorMacros.h:50
Generic expression where a coefficient-wise binary operator is applied to two expressions.
Definition CwiseBinaryOp.h:84
The tensor base class.
Definition TensorBase.h:973
Definition TensorArgMax.h:54
Eigen::NumTraits< Scalar >::Real RealScalar
Definition TensorArgMax.h:57
Tuple< Index, typename XprType::CoeffReturnType > CoeffReturnType
Definition TensorArgMax.h:61
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorIndexTupleOp(const XprType &expr)
Definition TensorArgMax.h:63
Eigen::internal::nested< TensorIndexTupleOp >::type Nested
Definition TensorArgMax.h:58
EIGEN_DEVICE_FUNC const internal::remove_all< typenameXprType::Nested >::type & expression() const
Definition TensorArgMax.h:68
Eigen::internal::traits< TensorIndexTupleOp >::Index Index
Definition TensorArgMax.h:60
Eigen::internal::traits< TensorIndexTupleOp >::StorageKind StorageKind
Definition TensorArgMax.h:59
Eigen::internal::traits< TensorIndexTupleOp >::Scalar Scalar
Definition TensorArgMax.h:56
XprType::Nested m_xpr
Definition TensorArgMax.h:71
Definition TensorCostModel.h:25
Definition TensorArgMax.h:177
Eigen::internal::nested< TensorTupleReducerOp >::type Nested
Definition TensorArgMax.h:181
EIGEN_DEVICE_FUNC Index return_dim() const
Definition TensorArgMax.h:203
EIGEN_DEVICE_FUNC const Dims & reduce_dims() const
Definition TensorArgMax.h:200
Index CoeffReturnType
Definition TensorArgMax.h:184
const ReduceOp m_reduce_op
Definition TensorArgMax.h:207
Eigen::internal::traits< TensorTupleReducerOp >::StorageKind StorageKind
Definition TensorArgMax.h:182
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorTupleReducerOp(const XprType &expr, const ReduceOp &reduce_op, const Index return_dim, const Dims &reduce_dims)
Definition TensorArgMax.h:186
Eigen::internal::traits< TensorTupleReducerOp >::Scalar Scalar
Definition TensorArgMax.h:179
EIGEN_DEVICE_FUNC const ReduceOp & reduce_op() const
Definition TensorArgMax.h:197
const Index m_return_dim
Definition TensorArgMax.h:208
EIGEN_DEVICE_FUNC const internal::remove_all< typenameXprType::Nested >::type & expression() const
Definition TensorArgMax.h:194
Eigen::internal::traits< TensorTupleReducerOp >::Index Index
Definition TensorArgMax.h:183
XprType::Nested m_xpr
Definition TensorArgMax.h:206
const Dims m_reduce_dims
Definition TensorArgMax.h:209
Eigen::NumTraits< Scalar >::Real RealScalar
Definition TensorArgMax.h:180
Definition EmulateArray.h:21
Definition TensorBlock.h:617
@ ColMajor
Definition Constants.h:319
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE std::ptrdiff_t array_prod(const Sizes< Indices... > &)
Definition TensorDimensions.h:140
EIGEN_ALWAYS_INLINE DSizes< IndexType, NumDims > strides(const DSizes< IndexType, NumDims > &dimensions)
Definition TensorBlock.h:26
Namespace containing all symbols from the Eigen library.
Definition bench_norm.cpp:85
Definition BandTriangularSolver.h:13
Definition Constants.h:507
Definition TensorForwardDeclarations.h:37
TensorIndexTupleOp< ArgType > XprType
Definition TensorArgMax.h:78
StorageMemory< CoeffReturnType, Device > Storage
Definition TensorArgMax.h:85
Storage::Type EvaluatorPointerType
Definition TensorArgMax.h:86
EIGEN_STRONG_INLINE void cleanup()
Definition TensorArgMax.h:113
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Dimensions & dimensions() const
Definition TensorArgMax.h:105
internal::TensorBlockNotImplemented TensorBlock
Definition TensorArgMax.h:99
EIGEN_DEVICE_FUNC EvaluatorPointerType data() const
Definition TensorArgMax.h:127
TensorEvaluator< ArgType, Device >::Dimensions Dimensions
Definition TensorArgMax.h:83
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE CoeffReturnType coeff(Index index) const
Definition TensorArgMax.h:117
XprType::CoeffReturnType CoeffReturnType
Definition TensorArgMax.h:81
TensorEvaluator< ArgType, Device > m_impl
Definition TensorArgMax.h:136
EIGEN_STRONG_INLINE TensorEvaluator(const XprType &op, const Device &device)
Definition TensorArgMax.h:102
EIGEN_STRONG_INLINE bool evalSubExprsIfNeeded(EvaluatorPointerType)
Definition TensorArgMax.h:109
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorOpCost costPerCoeff(bool vectorized) const
Definition TensorArgMax.h:123
TensorEvaluator< constTensorReductionOp< ReduceOp, Dims, constTensorIndexTupleOp< ArgType > >, Device >::Dimensions Dimensions
Definition TensorArgMax.h:221
EIGEN_STRONG_INLINE bool evalSubExprsIfNeeded(EvaluatorPointerType)
Definition TensorArgMax.h:266
EIGEN_STRONG_INLINE TensorEvaluator(const XprType &op, const Device &device)
Definition TensorArgMax.h:243
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Dimensions & dimensions() const
Definition TensorArgMax.h:262
StorageMemory< TupleType, Device > TupleStorageMem
Definition TensorArgMax.h:227
StorageMemory< CoeffReturnType, Device > Storage
Definition TensorArgMax.h:225
TensorIndexTupleOp< ArgType >::CoeffReturnType TupleType
Definition TensorArgMax.h:220
TensorTupleReducerOp< ReduceOp, Dims, ArgType > XprType
Definition TensorArgMax.h:216
internal::TensorBlockNotImplemented TensorBlock
Definition TensorArgMax.h:240
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE CoeffReturnType coeff(Index index) const
Definition TensorArgMax.h:274
EIGEN_DEVICE_FUNC EvaluatorPointerType data() const
Definition TensorArgMax.h:279
TensorEvaluator< const TensorReductionOp< ReduceOp, Dims, const TensorIndexTupleOp< ArgType > >, Device > m_impl
Definition TensorArgMax.h:320
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorOpCost costPerCoeff(bool vectorized) const
Definition TensorArgMax.h:288
TensorEvaluator< constTensorIndexTupleOp< ArgType >, Device >::Dimensions InputDimensions
Definition TensorArgMax.h:222
TensorEvaluator< const TensorIndexTupleOp< ArgType >, Device > m_orig_impl
Definition TensorArgMax.h:319
A cost model used to limit the number of threads used for evaluating tensor expression.
Definition TensorEvaluator.h:29
Derived::Scalar CoeffReturnType
Definition TensorEvaluator.h:32
@ BlockAccess
Definition TensorEvaluator.h:48
@ PreferBlockAccess
Definition TensorEvaluator.h:49
@ PacketAccess
Definition TensorEvaluator.h:47
@ Layout
Definition TensorEvaluator.h:50
@ IsAligned
Definition TensorEvaluator.h:46
Derived::Dimensions Dimensions
Definition TensorEvaluator.h:34
Definition TensorMeta.h:211
U first
Definition TensorMeta.h:213
Definition Meta.h:445
const TensorIndexTupleOp< XprType > EIGEN_DEVICE_REF type
Definition TensorArgMax.h:40
const TensorTupleReducerOp< ReduceOp, Dims, XprType > EIGEN_DEVICE_REF type
Definition TensorArgMax.h:163
Definition XprHelper.h:332
Definition TensorTraits.h:175
remove_reference< Nested >::type _Nested
Definition TensorArgMax.h:32
traits< XprType > XprTraits
Definition TensorArgMax.h:27
XprType::Nested Nested
Definition TensorArgMax.h:31
XprTraits::StorageKind StorageKind
Definition TensorArgMax.h:28
XprTraits::Index Index
Definition TensorArgMax.h:29
Tuple< Index, typename XprTraits::Scalar > Scalar
Definition TensorArgMax.h:30
XprTraits::StorageKind StorageKind
Definition TensorArgMax.h:151
remove_reference< Nested >::type _Nested
Definition TensorArgMax.h:155
Definition ForwardDeclarations.h:17