TensorRef.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) 2014 Benoit Steiner <benoit.steiner.goog@gmail.com>
5 //
6 // This Source Code Form is subject to the terms of the Mozilla
7 // Public License v. 2.0. If a copy of the MPL was not distributed
8 // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 
10 #ifndef EIGEN_CXX11_TENSOR_TENSOR_REF_H
11 #define EIGEN_CXX11_TENSOR_TENSOR_REF_H
12 
13 namespace Eigen {
14 
15 namespace internal {
16 
17 template <typename Dimensions, typename Scalar>
19  public:
22 
23  EIGEN_DEVICE_FUNC virtual const Dimensions& dimensions() const = 0;
24  EIGEN_DEVICE_FUNC virtual const Scalar* data() const = 0;
25 
26  EIGEN_DEVICE_FUNC virtual const Scalar coeff(DenseIndex index) const = 0;
27  EIGEN_DEVICE_FUNC virtual Scalar& coeffRef(DenseIndex index) = 0;
28 
29  void incrRefCount() { ++m_refcount; }
30  void decrRefCount() { --m_refcount; }
31  int refCount() const { return m_refcount; }
32 
33  private:
34  // No copy, no assignment;
37 
39 };
40 
41 
42 template <typename Dimensions, typename Expr, typename Device>
43 class TensorLazyEvaluatorReadOnly : public TensorLazyBaseEvaluator<Dimensions, typename TensorEvaluator<Expr, Device>::Scalar> {
44  public:
45  // typedef typename TensorEvaluator<Expr, Device>::Dimensions Dimensions;
50 
51  TensorLazyEvaluatorReadOnly(const Expr& expr, const Device& device) : m_impl(expr, device), m_dummy(Scalar(0)) {
52  m_dims = m_impl.dimensions();
53  m_impl.evalSubExprsIfNeeded(NULL);
54  }
56  m_impl.cleanup();
57  }
58 
59  EIGEN_DEVICE_FUNC virtual const Dimensions& dimensions() const {
60  return m_dims;
61  }
62  EIGEN_DEVICE_FUNC virtual const Scalar* data() const {
63  return m_impl.data();
64  }
65 
66  EIGEN_DEVICE_FUNC virtual const Scalar coeff(DenseIndex index) const {
67  return m_impl.coeff(index);
68  }
69  EIGEN_DEVICE_FUNC virtual Scalar& coeffRef(DenseIndex /*index*/) {
70  eigen_assert(false && "can't reference the coefficient of a rvalue");
71  return m_dummy;
72  };
73 
74  protected:
76  Dimensions m_dims;
77  Scalar m_dummy;
78 };
79 
80 template <typename Dimensions, typename Expr, typename Device>
81 class TensorLazyEvaluatorWritable : public TensorLazyEvaluatorReadOnly<Dimensions, Expr, Device> {
82  public:
84  typedef typename Base::Scalar Scalar;
87 
88  TensorLazyEvaluatorWritable(const Expr& expr, const Device& device) : Base(expr, device) {
89  }
91  }
92 
93  EIGEN_DEVICE_FUNC virtual Scalar& coeffRef(DenseIndex index) {
94  return this->m_impl.coeffRef(index);
95  }
96 };
97 
98 template <typename Dimensions, typename Expr, typename Device>
99 class TensorLazyEvaluator : public internal::conditional<bool(internal::is_lvalue<Expr>::value),
100  TensorLazyEvaluatorWritable<Dimensions, Expr, Device>,
101  TensorLazyEvaluatorReadOnly<Dimensions, const Expr, Device> >::type {
102  public:
106  typedef typename Base::Scalar Scalar;
107 
108  TensorLazyEvaluator(const Expr& expr, const Device& device) : Base(expr, device) {
109  }
111  }
112 };
113 
114 } // namespace internal
115 
116 
124 template<typename PlainObjectType> class TensorRef : public TensorBase<TensorRef<PlainObjectType> >
125 {
126  public:
128  typedef typename PlainObjectType::Base Base;
135  typedef Scalar* PointerType;
136  typedef PointerType PointerArgType;
137 
138  static const Index NumIndices = PlainObjectType::NumIndices;
139  typedef typename PlainObjectType::Dimensions Dimensions;
140 
141  enum {
142  IsAligned = false,
143  PacketAccess = false,
144  BlockAccess = false,
145  PreferBlockAccess = false,
146  Layout = PlainObjectType::Layout,
147  CoordAccess = false, // to be implemented
148  RawAccess = false
149  };
150 
151  //===- Tensor block evaluation strategy (see TensorBlock.h) -----------===//
153  //===------------------------------------------------------------------===//
154 
155  EIGEN_STRONG_INLINE TensorRef() : m_evaluator(NULL) {
156  }
157 
158  template <typename Expression>
159  EIGEN_STRONG_INLINE TensorRef(const Expression& expr) : m_evaluator(new internal::TensorLazyEvaluator<Dimensions, Expression, DefaultDevice>(expr, DefaultDevice())) {
160  m_evaluator->incrRefCount();
161  }
162 
163  template <typename Expression>
164  EIGEN_STRONG_INLINE TensorRef& operator = (const Expression& expr) {
165  unrefEvaluator();
167  m_evaluator->incrRefCount();
168  return *this;
169  }
170 
172  unrefEvaluator();
173  }
174 
175  TensorRef(const TensorRef& other) : m_evaluator(other.m_evaluator) {
176  eigen_assert(m_evaluator->refCount() > 0);
177  m_evaluator->incrRefCount();
178  }
179 
181  if (this != &other) {
182  unrefEvaluator();
183  m_evaluator = other.m_evaluator;
184  eigen_assert(m_evaluator->refCount() > 0);
185  m_evaluator->incrRefCount();
186  }
187  return *this;
188  }
189 
191  EIGEN_STRONG_INLINE Index rank() const { return m_evaluator->dimensions().size(); }
193  EIGEN_STRONG_INLINE Index dimension(Index n) const { return m_evaluator->dimensions()[n]; }
195  EIGEN_STRONG_INLINE const Dimensions& dimensions() const { return m_evaluator->dimensions(); }
197  EIGEN_STRONG_INLINE Index size() const { return m_evaluator->dimensions().TotalSize(); }
199  EIGEN_STRONG_INLINE const Scalar* data() const { return m_evaluator->data(); }
200 
202  EIGEN_STRONG_INLINE const Scalar operator()(Index index) const
203  {
204  return m_evaluator->coeff(index);
205  }
206 
207 #if EIGEN_HAS_VARIADIC_TEMPLATES
208  template<typename... IndexTypes> EIGEN_DEVICE_FUNC
209  EIGEN_STRONG_INLINE const Scalar operator()(Index firstIndex, IndexTypes... otherIndices) const
210  {
211  const std::size_t num_indices = (sizeof...(otherIndices) + 1);
212  const array<Index, num_indices> indices{{firstIndex, otherIndices...}};
213  return coeff(indices);
214  }
215  template<typename... IndexTypes> EIGEN_DEVICE_FUNC
216  EIGEN_STRONG_INLINE Scalar& coeffRef(Index firstIndex, IndexTypes... otherIndices)
217  {
218  const std::size_t num_indices = (sizeof...(otherIndices) + 1);
219  const array<Index, num_indices> indices{{firstIndex, otherIndices...}};
220  return coeffRef(indices);
221  }
222 #else
223 
225  EIGEN_STRONG_INLINE const Scalar operator()(Index i0, Index i1) const
226  {
227  array<Index, 2> indices;
228  indices[0] = i0;
229  indices[1] = i1;
230  return coeff(indices);
231  }
233  EIGEN_STRONG_INLINE const Scalar operator()(Index i0, Index i1, Index i2) const
234  {
235  array<Index, 3> indices;
236  indices[0] = i0;
237  indices[1] = i1;
238  indices[2] = i2;
239  return coeff(indices);
240  }
242  EIGEN_STRONG_INLINE const Scalar operator()(Index i0, Index i1, Index i2, Index i3) const
243  {
244  array<Index, 4> indices;
245  indices[0] = i0;
246  indices[1] = i1;
247  indices[2] = i2;
248  indices[3] = i3;
249  return coeff(indices);
250  }
252  EIGEN_STRONG_INLINE const Scalar operator()(Index i0, Index i1, Index i2, Index i3, Index i4) const
253  {
254  array<Index, 5> indices;
255  indices[0] = i0;
256  indices[1] = i1;
257  indices[2] = i2;
258  indices[3] = i3;
259  indices[4] = i4;
260  return coeff(indices);
261  }
263  EIGEN_STRONG_INLINE Scalar& coeffRef(Index i0, Index i1)
264  {
265  array<Index, 2> indices;
266  indices[0] = i0;
267  indices[1] = i1;
268  return coeffRef(indices);
269  }
271  EIGEN_STRONG_INLINE Scalar& coeffRef(Index i0, Index i1, Index i2)
272  {
273  array<Index, 3> indices;
274  indices[0] = i0;
275  indices[1] = i1;
276  indices[2] = i2;
277  return coeffRef(indices);
278  }
280  EIGEN_STRONG_INLINE Scalar& operator()(Index i0, Index i1, Index i2, Index i3)
281  {
282  array<Index, 4> indices;
283  indices[0] = i0;
284  indices[1] = i1;
285  indices[2] = i2;
286  indices[3] = i3;
287  return coeffRef(indices);
288  }
290  EIGEN_STRONG_INLINE Scalar& coeffRef(Index i0, Index i1, Index i2, Index i3, Index i4)
291  {
292  array<Index, 5> indices;
293  indices[0] = i0;
294  indices[1] = i1;
295  indices[2] = i2;
296  indices[3] = i3;
297  indices[4] = i4;
298  return coeffRef(indices);
299  }
300 #endif
301 
302  template <std::size_t NumIndices> EIGEN_DEVICE_FUNC
303  EIGEN_STRONG_INLINE const Scalar coeff(const array<Index, NumIndices>& indices) const
304  {
305  const Dimensions& dims = this->dimensions();
306  Index index = 0;
307  if (PlainObjectType::Options & RowMajor) {
308  index += indices[0];
309  for (size_t i = 1; i < NumIndices; ++i) {
310  index = index * dims[i] + indices[i];
311  }
312  } else {
313  index += indices[NumIndices-1];
314  for (int i = NumIndices-2; i >= 0; --i) {
315  index = index * dims[i] + indices[i];
316  }
317  }
318  return m_evaluator->coeff(index);
319  }
320  template <std::size_t NumIndices> EIGEN_DEVICE_FUNC
322  {
323  const Dimensions& dims = this->dimensions();
324  Index index = 0;
325  if (PlainObjectType::Options & RowMajor) {
326  index += indices[0];
327  for (size_t i = 1; i < NumIndices; ++i) {
328  index = index * dims[i] + indices[i];
329  }
330  } else {
331  index += indices[NumIndices-1];
332  for (int i = NumIndices-2; i >= 0; --i) {
333  index = index * dims[i] + indices[i];
334  }
335  }
336  return m_evaluator->coeffRef(index);
337  }
338 
340  EIGEN_STRONG_INLINE const Scalar coeff(Index index) const
341  {
342  return m_evaluator->coeff(index);
343  }
344 
346  EIGEN_STRONG_INLINE Scalar& coeffRef(Index index)
347  {
348  return m_evaluator->coeffRef(index);
349  }
350 
351  private:
353  if (m_evaluator) {
354  m_evaluator->decrRefCount();
355  if (m_evaluator->refCount() == 0) {
356  delete m_evaluator;
357  }
358  }
359  }
360 
362 };
363 
364 
365 // evaluator for rvalues
366 template<typename Derived, typename Device>
367 struct TensorEvaluator<const TensorRef<Derived>, Device>
368 {
369  typedef typename Derived::Index Index;
370  typedef typename Derived::Scalar Scalar;
373  typedef typename Derived::Dimensions Dimensions;
376 
377  enum {
378  IsAligned = false,
379  PacketAccess = false,
380  BlockAccess = false,
381  PreferBlockAccess = false,
383  CoordAccess = false, // to be implemented
384  RawAccess = false
385  };
386 
387  //===- Tensor block evaluation strategy (see TensorBlock.h) -------------===//
389  //===--------------------------------------------------------------------===//
390 
392  : m_ref(m)
393  { }
394 
395  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Dimensions& dimensions() const { return m_ref.dimensions(); }
396 
397  EIGEN_STRONG_INLINE bool evalSubExprsIfNeeded(EvaluatorPointerType) {
398  return true;
399  }
400 
402 
403  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE CoeffReturnType coeff(Index index) const {
404  return m_ref.coeff(index);
405  }
406 
408  return m_ref.coeffRef(index);
409  }
410 
411  EIGEN_DEVICE_FUNC const Scalar* data() const { return m_ref.data(); }
412 
413  protected:
415 };
416 
417 
418 // evaluator for lvalues
419 template<typename Derived, typename Device>
420 struct TensorEvaluator<TensorRef<Derived>, Device> : public TensorEvaluator<const TensorRef<Derived>, Device>
421 {
422  typedef typename Derived::Index Index;
423  typedef typename Derived::Scalar Scalar;
426  typedef typename Derived::Dimensions Dimensions;
427 
429 
430  enum {
431  IsAligned = false,
432  PacketAccess = false,
433  BlockAccess = false,
434  PreferBlockAccess = false,
435  RawAccess = false
436  };
437 
438  //===- Tensor block evaluation strategy (see TensorBlock.h) -------------===//
440  //===--------------------------------------------------------------------===//
441 
443  { }
444 
446  return this->m_ref.coeffRef(index);
447  }
448 };
449 
450 
451 
452 } // end namespace Eigen
453 
454 #endif // EIGEN_CXX11_TENSOR_TENSOR_REF_H
Matrix3f m
TensorLazyBaseEvaluator & operator=(const TensorLazyBaseEvaluator &other)
PacketType< CoeffReturnType, Device >::type PacketReturnType
Definition: TensorRef.h:425
SCALAR Scalar
Definition: bench_gemm.cpp:46
TensorRef< PlainObjectType > Self
Definition: TensorRef.h:127
#define EIGEN_STRONG_INLINE
Definition: Macros.h:917
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar & coeffRef(Index index)
Definition: TensorRef.h:346
virtual EIGEN_DEVICE_FUNC Scalar & coeffRef(DenseIndex index)
Definition: TensorRef.h:93
virtual EIGEN_DEVICE_FUNC const Dimensions & dimensions() const =0
TensorRef(const TensorRef &other)
Definition: TensorRef.h:175
internal::TensorBlockNotImplemented TensorBlock
Definition: TensorRef.h:152
virtual EIGEN_DEVICE_FUNC Scalar & coeffRef(DenseIndex)
Definition: TensorRef.h:69
internal::TensorBlockNotImplemented TensorBlock
Definition: TensorRef.h:439
TensorEvaluator< Expr, Device > EvalType
Definition: TensorRef.h:49
int n
Namespace containing all symbols from the Eigen library.
Definition: jet.h:637
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar & coeffRef(Index i0, Index i1, Index i2, Index i3, Index i4)
Definition: TensorRef.h:290
Scalar CoeffReturnType
Definition: TensorBase.h:979
A cost model used to limit the number of threads used for evaluating tensor expression.
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Scalar operator()(Index index) const
Definition: TensorRef.h:202
internal::traits< PlainObjectType >::Index Index
Definition: TensorRef.h:131
Holds information about the various numeric (i.e. scalar) types allowed by Eigen. ...
Definition: NumTraits.h:232
internal::traits< PlainObjectType >::StorageKind StorageKind
Definition: TensorRef.h:130
TensorLazyEvaluator(const Expr &expr, const Device &device)
Definition: TensorRef.h:108
Scalar * PointerType
Definition: TensorRef.h:135
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Index size() const
Definition: TensorRef.h:197
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Scalar coeff(Index index) const
Definition: TensorRef.h:340
internal::traits< PlainObjectType >::Scalar Scalar
Definition: TensorRef.h:132
EIGEN_DEVICE_FUNC const Scalar * data() const
Definition: TensorRef.h:411
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Dimensions & dimensions() const
Definition: TensorRef.h:195
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar & coeffRef(Index index)
Definition: TensorRef.h:407
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Scalar operator()(Index i0, Index i1, Index i2, Index i3) const
Definition: TensorRef.h:242
PlainObjectType::Base Base
Definition: TensorRef.h:128
EIGEN_STRONG_INLINE TensorEvaluator(TensorRef< Derived > &m, const Device &d)
Definition: TensorRef.h:442
PacketType< CoeffReturnType, Device >::type PacketReturnType
Definition: TensorRef.h:372
TensorEvaluator< Expr, Device > m_impl
Definition: TensorRef.h:72
StorageMemory< Scalar, Device > Storage
Definition: TensorRef.h:85
TensorLazyEvaluatorReadOnly< Dimensions, Expr, Device > Base
Definition: TensorRef.h:83
Eigen::internal::nested< Self >::type Nested
Definition: TensorRef.h:129
PointerType PointerArgType
Definition: TensorRef.h:136
PlainObjectType::Dimensions Dimensions
Definition: TensorRef.h:139
internal::TensorLazyBaseEvaluator< Dimensions, Scalar > * m_evaluator
Definition: TensorRef.h:361
EIGEN_STRONG_INLINE TensorEvaluator(const TensorRef< Derived > &m, const Device &)
Definition: TensorRef.h:391
EIGEN_STRONG_INLINE TensorRef(const Expression &expr)
Definition: TensorRef.h:159
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:74
#define eigen_assert(x)
Definition: Macros.h:1037
EIGEN_STRONG_INLINE void unrefEvaluator()
Definition: TensorRef.h:352
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar & coeffRef(const array< Index, NumIndices > &indices)
Definition: TensorRef.h:321
TensorLazyEvaluatorWritable(const Expr &expr, const Device &device)
Definition: TensorRef.h:88
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Scalar coeff(const array< Index, NumIndices > &indices) const
Definition: TensorRef.h:303
virtual EIGEN_DEVICE_FUNC const Scalar * data() const =0
#define NULL
Definition: ccolamd.c:609
virtual EIGEN_DEVICE_FUNC const Scalar coeff(DenseIndex index) const =0
internal::TensorBlockNotImplemented TensorBlock
Definition: TensorRef.h:388
StorageMemory< Scalar, Device > Storage
Definition: TensorRef.h:47
virtual EIGEN_DEVICE_FUNC const Scalar coeff(DenseIndex index) const
Definition: TensorRef.h:66
The tensor base class.
Definition: TensorBase.h:973
#define EIGEN_DEVICE_FUNC
Definition: Macros.h:976
EIGEN_DEFAULT_DENSE_INDEX_TYPE DenseIndex
Definition: Meta.h:66
TensorEvaluator< const TensorRef< Derived >, Device > Base
Definition: TensorRef.h:428
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Scalar operator()(Index i0, Index i1) const
Definition: TensorRef.h:225
NumTraits< Scalar >::Real RealScalar
Definition: TensorRef.h:133
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar & coeffRef(Index i0, Index i1)
Definition: TensorRef.h:263
virtual EIGEN_DEVICE_FUNC Scalar & coeffRef(DenseIndex index)=0
TensorLazyEvaluatorReadOnly(const Expr &expr, const Device &device)
Definition: TensorRef.h:51
StorageMemory< CoeffReturnType, Device > Storage
Definition: TensorRef.h:374
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Index dimension(Index n) const
Definition: TensorRef.h:193
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Scalar operator()(Index i0, Index i1, Index i2) const
Definition: TensorRef.h:233
EIGEN_STRONG_INLINE TensorRef()
Definition: TensorRef.h:155
A reference to a tensor expression The expression will be evaluated lazily (as much as possible)...
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar & coeffRef(Index index)
Definition: TensorRef.h:445
Generic expression where a coefficient-wise unary operator is applied to an expression.
Definition: CwiseUnaryOp.h:55
Base::CoeffReturnType CoeffReturnType
Definition: TensorRef.h:134
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Index rank() const
Definition: TensorRef.h:191
TensorEvaluator< Expr, Device >::Scalar Scalar
Definition: TensorRef.h:46
EIGEN_STRONG_INLINE bool evalSubExprsIfNeeded(EvaluatorPointerType)
Definition: TensorRef.h:397
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Dimensions & dimensions() const
Definition: TensorRef.h:395
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar & operator()(Index i0, Index i1, Index i2, Index i3)
Definition: TensorRef.h:280
internal::enable_if< internal::valid_indexed_view_overload< RowIndices, ColIndices >::value &&internal::traits< typename EIGEN_INDEXED_VIEW_METHOD_TYPE< RowIndices, ColIndices >::type >::ReturnAsIndexedView, typename EIGEN_INDEXED_VIEW_METHOD_TYPE< RowIndices, ColIndices >::type >::type operator()(const RowIndices &rowIndices, const ColIndices &colIndices) EIGEN_INDEXED_VIEW_METHOD_CONST
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Scalar * data() const
Definition: TensorRef.h:199
virtual EIGEN_DEVICE_FUNC const Scalar * data() const
Definition: TensorRef.h:62
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE CoeffReturnType coeff(Index index) const
Definition: TensorRef.h:403
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar & coeffRef(Index i0, Index i1, Index i2)
Definition: TensorRef.h:271
virtual EIGEN_DEVICE_FUNC const Dimensions & dimensions() const
Definition: TensorRef.h:59
Definition: pytypes.h:1370
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Scalar operator()(Index i0, Index i1, Index i2, Index i3, Index i4) const
Definition: TensorRef.h:252


gtsam
Author(s):
autogenerated on Tue Jul 4 2023 02:37:35