TensorTrace.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) 2017 Gagan Goel <gagan.nith@gmail.com>
5 // Copyright (C) 2017 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_TRACE_H
12 #define EIGEN_CXX11_TENSOR_TENSOR_TRACE_H
13 
14 namespace Eigen {
15 
24 namespace internal {
25 template<typename Dims, typename XprType>
26 struct traits<TensorTraceOp<Dims, XprType> > : public traits<XprType>
27 {
28  typedef typename XprType::Scalar Scalar;
30  typedef typename XprTraits::StorageKind StorageKind;
31  typedef typename XprTraits::Index Index;
32  typedef typename XprType::Nested Nested;
34  static const int NumDimensions = XprTraits::NumDimensions - array_size<Dims>::value;
35  static const int Layout = XprTraits::Layout;
36 };
37 
38 template<typename Dims, typename XprType>
40 {
42 };
43 
44 template<typename Dims, typename XprType>
46 {
48 };
49 
50 } // end namespace internal
51 
52 
53 template<typename Dims, typename XprType>
54 class TensorTraceOp : public TensorBase<TensorTraceOp<Dims, XprType> >
55 {
56  public:
59  typedef typename XprType::CoeffReturnType CoeffReturnType;
63 
65  : m_xpr(expr), m_dims(dims) {
66  }
67 
69  const Dims& dims() const { return m_dims; }
70 
73 
74  protected:
75  typename XprType::Nested m_xpr;
76  const Dims m_dims;
77 };
78 
79 
80 // Eval as rvalue
81 template<typename Dims, typename ArgType, typename Device>
82 struct TensorEvaluator<const TensorTraceOp<Dims, ArgType>, Device>
83 {
86  static const int NumReducedDims = internal::array_size<Dims>::value;
87  static const int NumOutputDims = NumInputDims - NumReducedDims;
88  typedef typename XprType::Index Index;
90  typedef typename XprType::Scalar Scalar;
96 
97  enum {
98  IsAligned = false,
100  BlockAccess = false,
103  CoordAccess = false,
104  RawAccess = false
105  };
106 
107  //===- Tensor block evaluation strategy (see TensorBlock.h) -------------===//
109  //===--------------------------------------------------------------------===//
110 
111  EIGEN_STRONG_INLINE TensorEvaluator(const XprType& op, const Device& device)
112  : m_impl(op.expression(), device), m_traceDim(1), m_device(device)
113  {
114 
115  EIGEN_STATIC_ASSERT((NumOutputDims >= 0), YOU_MADE_A_PROGRAMMING_MISTAKE);
116  EIGEN_STATIC_ASSERT((NumReducedDims >= 2) || ((NumReducedDims == 0) && (NumInputDims == 0)), YOU_MADE_A_PROGRAMMING_MISTAKE);
117 
118  for (int i = 0; i < NumInputDims; ++i) {
119  m_reduced[i] = false;
120  }
121 
122  const Dims& op_dims = op.dims();
123  for (int i = 0; i < NumReducedDims; ++i) {
124  eigen_assert(op_dims[i] >= 0);
125  eigen_assert(op_dims[i] < NumInputDims);
126  m_reduced[op_dims[i]] = true;
127  }
128 
129  // All the dimensions should be distinct to compute the trace
130  int num_distinct_reduce_dims = 0;
131  for (int i = 0; i < NumInputDims; ++i) {
132  if (m_reduced[i]) {
133  ++num_distinct_reduce_dims;
134  }
135  }
136 
137  eigen_assert(num_distinct_reduce_dims == NumReducedDims);
138 
139  // Compute the dimensions of the result.
140  const typename TensorEvaluator<ArgType, Device>::Dimensions& input_dims = m_impl.dimensions();
141 
142  int output_index = 0;
143  int reduced_index = 0;
144  for (int i = 0; i < NumInputDims; ++i) {
145  if (m_reduced[i]) {
146  m_reducedDims[reduced_index] = input_dims[i];
147  if (reduced_index > 0) {
148  // All the trace dimensions must have the same size
149  eigen_assert(m_reducedDims[0] == m_reducedDims[reduced_index]);
150  }
151  ++reduced_index;
152  }
153  else {
154  m_dimensions[output_index] = input_dims[i];
155  ++output_index;
156  }
157  }
158 
159  if (NumReducedDims != 0) {
160  m_traceDim = m_reducedDims[0];
161  }
162 
163  // Compute the output strides
164  if (NumOutputDims > 0) {
165  if (static_cast<int>(Layout) == static_cast<int>(ColMajor)) {
166  m_outputStrides[0] = 1;
167  for (int i = 1; i < NumOutputDims; ++i) {
168  m_outputStrides[i] = m_outputStrides[i - 1] * m_dimensions[i - 1];
169  }
170  }
171  else {
172  m_outputStrides.back() = 1;
173  for (int i = NumOutputDims - 2; i >= 0; --i) {
174  m_outputStrides[i] = m_outputStrides[i + 1] * m_dimensions[i + 1];
175  }
176  }
177  }
178 
179  // Compute the input strides
180  if (NumInputDims > 0) {
181  array<Index, NumInputDims> input_strides;
182  if (static_cast<int>(Layout) == static_cast<int>(ColMajor)) {
183  input_strides[0] = 1;
184  for (int i = 1; i < NumInputDims; ++i) {
185  input_strides[i] = input_strides[i - 1] * input_dims[i - 1];
186  }
187  }
188  else {
189  input_strides.back() = 1;
190  for (int i = NumInputDims - 2; i >= 0; --i) {
191  input_strides[i] = input_strides[i + 1] * input_dims[i + 1];
192  }
193  }
194 
195  output_index = 0;
196  reduced_index = 0;
197  for (int i = 0; i < NumInputDims; ++i) {
198  if(m_reduced[i]) {
199  m_reducedStrides[reduced_index] = input_strides[i];
200  ++reduced_index;
201  }
202  else {
203  m_preservedStrides[output_index] = input_strides[i];
204  ++output_index;
205  }
206  }
207  }
208  }
209 
211  return m_dimensions;
212  }
213 
215  m_impl.evalSubExprsIfNeeded(NULL);
216  return true;
217  }
218 
220  m_impl.cleanup();
221  }
222 
224  {
225  // Initialize the result
226  CoeffReturnType result = internal::cast<int, CoeffReturnType>(0);
227  Index index_stride = 0;
228  for (int i = 0; i < NumReducedDims; ++i) {
229  index_stride += m_reducedStrides[i];
230  }
231 
232  // If trace is requested along all dimensions, starting index would be 0
233  Index cur_index = 0;
234  if (NumOutputDims != 0)
235  cur_index = firstInput(index);
236  for (Index i = 0; i < m_traceDim; ++i) {
237  result += m_impl.coeff(cur_index);
238  cur_index += index_stride;
239  }
240 
241  return result;
242  }
243 
244  template<int LoadMode>
246 
247  EIGEN_STATIC_ASSERT((PacketSize > 1), YOU_MADE_A_PROGRAMMING_MISTAKE);
248  eigen_assert(index + PacketSize - 1 < dimensions().TotalSize());
249 
251  for (int i = 0; i < PacketSize; ++i) {
252  values[i] = coeff(index + i);
253  }
254  PacketReturnType result = internal::ploadt<PacketReturnType, LoadMode>(values);
255  return result;
256  }
257 
258 #ifdef EIGEN_USE_SYCL
259  // binding placeholder accessors to a command group handler for SYCL
260  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void bind(cl::sycl::handler &cgh) const {
261  m_impl.bind(cgh);
262  }
263 #endif
264 
265  protected:
266  // Given the output index, finds the first index in the input tensor used to compute the trace
268  Index startInput = 0;
269  if (static_cast<int>(Layout) == static_cast<int>(ColMajor)) {
270  for (int i = NumOutputDims - 1; i > 0; --i) {
271  const Index idx = index / m_outputStrides[i];
272  startInput += idx * m_preservedStrides[i];
273  index -= idx * m_outputStrides[i];
274  }
275  startInput += index * m_preservedStrides[0];
276  }
277  else {
278  for (int i = 0; i < NumOutputDims - 1; ++i) {
279  const Index idx = index / m_outputStrides[i];
280  startInput += idx * m_preservedStrides[i];
281  index -= idx * m_outputStrides[i];
282  }
283  startInput += index * m_preservedStrides[NumOutputDims - 1];
284  }
285  return startInput;
286  }
287 
290  // Initialize the size of the trace dimension
298 };
299 
300 
301 } // End namespace Eigen
302 
303 #endif // EIGEN_CXX11_TENSOR_TENSOR_TRACE_H
Eigen::TensorEvaluator::dimensions
EIGEN_DEVICE_FUNC const EIGEN_STRONG_INLINE Dimensions & dimensions() const
Definition: TensorEvaluator.h:73
Dims
std::vector< Eigen::Index > Dims
Definition: testGaussianConditional.cpp:46
Eigen::TensorTraceOp::Scalar
Eigen::internal::traits< TensorTraceOp >::Scalar Scalar
Definition: TensorTrace.h:57
EIGEN_DEVICE_FUNC
#define EIGEN_DEVICE_FUNC
Definition: Macros.h:976
Eigen
Namespace containing all symbols from the Eigen library.
Definition: jet.h:637
Eigen::TensorTraceOp::RealScalar
Eigen::NumTraits< Scalar >::Real RealScalar
Definition: TensorTrace.h:58
Eigen::internal::TensorBlockNotImplemented
Definition: TensorBlock.h:617
Eigen::TensorTraceOp::dims
EIGEN_DEVICE_FUNC const EIGEN_STRONG_INLINE Dims & dims() const
Definition: TensorTrace.h:69
Eigen::TensorEvaluator< const TensorTraceOp< Dims, ArgType >, Device >::m_reducedDims
array< Index, NumReducedDims > m_reducedDims
Definition: TensorTrace.h:294
Eigen::CwiseBinaryOp
Generic expression where a coefficient-wise binary operator is applied to two expressions.
Definition: CwiseBinaryOp.h:77
Eigen::TensorEvaluator< const TensorTraceOp< Dims, ArgType >, Device >::m_traceDim
Index m_traceDim
Definition: TensorTrace.h:291
Eigen::array
Definition: EmulateArray.h:21
Eigen::TensorTraceOp::Index
Eigen::internal::traits< TensorTraceOp >::Index Index
Definition: TensorTrace.h:62
Eigen::internal::nested
Definition: TensorTraits.h:174
Eigen::TensorEvaluator< const TensorTraceOp< Dims, ArgType >, Device >::PacketReturnType
PacketType< CoeffReturnType, Device >::type PacketReturnType
Definition: TensorTrace.h:92
Eigen::TensorEvaluator< const TensorTraceOp< Dims, ArgType >, Device >::evalSubExprsIfNeeded
EIGEN_STRONG_INLINE bool evalSubExprsIfNeeded(EvaluatorPointerType)
Definition: TensorTrace.h:214
eigen_assert
#define eigen_assert(x)
Definition: Macros.h:1037
Eigen::TensorTraceOp
Definition: TensorForwardDeclarations.h:88
Eigen::TensorEvaluator< const TensorTraceOp< Dims, ArgType >, Device >::m_reduced
array< bool, NumInputDims > m_reduced
Definition: TensorTrace.h:293
Eigen::TensorEvaluator::Layout
@ Layout
Definition: TensorEvaluator.h:50
Eigen::TensorEvaluator< const TensorTraceOp< Dims, ArgType >, Device >::XprType
TensorTraceOp< Dims, ArgType > XprType
Definition: TensorTrace.h:84
different_sigmas::values
HybridValues values
Definition: testHybridBayesNet.cpp:245
result
Values result
Definition: OdometryOptimize.cpp:8
Eigen::TensorEvaluator::PacketSize
static const int PacketSize
Definition: TensorEvaluator.h:36
Eigen::internal::traits< TensorTraceOp< Dims, XprType > >::_Nested
remove_reference< Nested >::type _Nested
Definition: TensorTrace.h:33
Eigen::internal::nested< TensorTraceOp< Dims, XprType >, 1, typename eval< TensorTraceOp< Dims, XprType > >::type >::type
TensorTraceOp< Dims, XprType > type
Definition: TensorTrace.h:47
Eigen::DSizes< Index, NumOutputDims >
Eigen::TensorEvaluator< const TensorTraceOp< Dims, ArgType >, Device >::m_reducedStrides
array< Index, NumReducedDims > m_reducedStrides
Definition: TensorTrace.h:296
Eigen::TensorEvaluator< const TensorTraceOp< Dims, ArgType >, Device >::m_outputStrides
array< Index, NumOutputDims > m_outputStrides
Definition: TensorTrace.h:295
Eigen::TensorEvaluator< const TensorTraceOp< Dims, ArgType >, Device >::firstInput
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Index firstInput(Index index) const
Definition: TensorTrace.h:267
EIGEN_ALIGN_MAX
#define EIGEN_ALIGN_MAX
Definition: ConfigureVectorization.h:157
Eigen::TensorEvaluator< const TensorTraceOp< Dims, ArgType >, Device >::Storage
StorageMemory< CoeffReturnType, Device > Storage
Definition: TensorTrace.h:94
Eigen::TensorEvaluator< const TensorTraceOp< Dims, ArgType >, Device >::packet
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE PacketReturnType packet(Index index) const
Definition: TensorTrace.h:245
Eigen::TensorTraceOp::expression
EIGEN_DEVICE_FUNC const EIGEN_STRONG_INLINE internal::remove_all< typename XprType::Nested >::type & expression() const
Definition: TensorTrace.h:72
Eigen::internal::true_type
Definition: Meta.h:96
Eigen::TensorEvaluator< const TensorTraceOp< Dims, ArgType >, Device >::m_device
const Device EIGEN_DEVICE_REF m_device
Definition: TensorTrace.h:292
Eigen::TensorEvaluator< const TensorTraceOp< Dims, ArgType >, Device >::Scalar
XprType::Scalar Scalar
Definition: TensorTrace.h:90
Eigen::TensorEvaluator< const TensorTraceOp< Dims, ArgType >, Device >::TensorEvaluator
EIGEN_STRONG_INLINE TensorEvaluator(const XprType &op, const Device &device)
Definition: TensorTrace.h:111
Eigen::internal::unpacket_traits
Definition: GenericPacketMath.h:132
Eigen::TensorEvaluator< const TensorTraceOp< Dims, ArgType >, Device >::dimensions
EIGEN_DEVICE_FUNC const EIGEN_STRONG_INLINE Dimensions & dimensions() const
Definition: TensorTrace.h:210
EIGEN_STRONG_INLINE
#define EIGEN_STRONG_INLINE
Definition: Macros.h:917
Eigen::TensorEvaluator::PreferBlockAccess
@ PreferBlockAccess
Definition: TensorEvaluator.h:49
Eigen::TensorEvaluator< const TensorTraceOp< Dims, ArgType >, Device >::Index
XprType::Index Index
Definition: TensorTrace.h:88
Eigen::TensorEvaluator< const TensorTraceOp< Dims, ArgType >, Device >::EvaluatorPointerType
Storage::Type EvaluatorPointerType
Definition: TensorTrace.h:95
Eigen::internal::traits< TensorTraceOp< Dims, XprType > >::Nested
XprType::Nested Nested
Definition: TensorTrace.h:32
Eigen::TensorEvaluator< const TensorTraceOp< Dims, ArgType >, Device >::coeff
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE CoeffReturnType coeff(Index index) const
Definition: TensorTrace.h:223
Eigen::Triplet< double >
Eigen::TensorTraceOp::Nested
Eigen::internal::nested< TensorTraceOp >::type Nested
Definition: TensorTrace.h:60
Eigen::TensorEvaluator< const TensorTraceOp< Dims, ArgType >, Device >::m_impl
TensorEvaluator< ArgType, Device > m_impl
Definition: TensorTrace.h:289
Eigen::TensorEvaluator::m_device
const Device EIGEN_DEVICE_REF m_device
Definition: TensorEvaluator.h:192
Eigen::TensorEvaluator< const TensorTraceOp< Dims, ArgType >, Device >::TensorBlock
internal::TensorBlockNotImplemented TensorBlock
Definition: TensorTrace.h:108
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::internal::traits< TensorTraceOp< Dims, XprType > >::StorageKind
XprTraits::StorageKind StorageKind
Definition: TensorTrace.h:30
Eigen::TensorTraceOp::m_xpr
XprType::Nested m_xpr
Definition: TensorTrace.h:75
Eigen::TensorTraceOp::m_dims
const Dims m_dims
Definition: TensorTrace.h:76
Eigen::TensorEvaluator< const TensorTraceOp< Dims, ArgType >, Device >::m_dimensions
Dimensions m_dimensions
Definition: TensorTrace.h:288
Eigen::internal::traits
Definition: ForwardDeclarations.h:17
Eigen::array::back
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE T & back()
Definition: EmulateArray.h:39
EIGEN_STATIC_ASSERT
#define EIGEN_STATIC_ASSERT(CONDITION, MSG)
Definition: StaticAssert.h:127
Eigen::TensorTraceOp::CoeffReturnType
XprType::CoeffReturnType CoeffReturnType
Definition: TensorTrace.h:59
Eigen::TensorEvaluator< const TensorTraceOp< Dims, ArgType >, Device >::CoeffReturnType
XprType::CoeffReturnType CoeffReturnType
Definition: TensorTrace.h:91
Eigen::TensorTraceOp::StorageKind
Eigen::internal::traits< TensorTraceOp >::StorageKind StorageKind
Definition: TensorTrace.h:61
Eigen::TensorEvaluator::coeff
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE CoeffReturnType coeff(Index index) const
Definition: TensorEvaluator.h:94
EIGEN_DEVICE_REF
#define EIGEN_DEVICE_REF
Definition: TensorMacros.h:50
Eigen::internal::eval< TensorTraceOp< Dims, XprType >, Eigen::Dense >::type
const typedef TensorTraceOp< Dims, XprType > & type
Definition: TensorTrace.h:41
Eigen::internal::traits< TensorTraceOp< Dims, XprType > >::XprTraits
traits< XprType > XprTraits
Definition: TensorTrace.h:29
Eigen::TensorEvaluator
A cost model used to limit the number of threads used for evaluating tensor expression.
Definition: TensorEvaluator.h:28
internal
Definition: BandTriangularSolver.h:13
NULL
#define NULL
Definition: ccolamd.c:609
Eigen::TensorEvaluator< const TensorTraceOp< Dims, ArgType >, Device >::m_preservedStrides
array< Index, NumOutputDims > m_preservedStrides
Definition: TensorTrace.h:297
Eigen::ColMajor
@ ColMajor
Definition: Constants.h:319
Eigen::TensorEvaluator::IsAligned
@ IsAligned
Definition: TensorEvaluator.h:46
Eigen::TensorEvaluator< const TensorTraceOp< Dims, ArgType >, Device >::Dimensions
DSizes< Index, NumOutputDims > Dimensions
Definition: TensorTrace.h:89
Eigen::internal::eval
Definition: XprHelper.h:332
Eigen::TensorEvaluator::PacketAccess
@ PacketAccess
Definition: TensorEvaluator.h:47
Eigen::TensorEvaluator< const TensorTraceOp< Dims, ArgType >, Device >::cleanup
EIGEN_STRONG_INLINE void cleanup()
Definition: TensorTrace.h:219
Eigen::internal::traits< TensorTraceOp< Dims, XprType > >::Scalar
XprType::Scalar Scalar
Definition: TensorTrace.h:28
Eigen::internal::traits< TensorTraceOp< Dims, XprType > >::Index
XprTraits::Index Index
Definition: TensorTrace.h:31
test_callbacks.value
value
Definition: test_callbacks.py:160
i
int i
Definition: BiCGSTAB_step_by_step.cpp:9
Eigen::TensorTraceOp::TensorTraceOp
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorTraceOp(const XprType &expr, const Dims &dims)
Definition: TensorTrace.h:64
Scalar
SCALAR Scalar
Definition: bench_gemm.cpp:46
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


gtsam
Author(s):
autogenerated on Sat Nov 16 2024 04:07:03