TR-mbed 1.0
Loading...
Searching...
No Matches
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
13namespace Eigen {
14
15namespace internal {
16
17template <typename Dimensions, typename Scalar>
19 public:
20 TensorLazyBaseEvaluator() : m_refcount(0) { }
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;
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
38 int m_refcount;
39};
40
41
42template <typename Dimensions, typename Expr, typename Device>
43class TensorLazyEvaluatorReadOnly : public TensorLazyBaseEvaluator<Dimensions, typename TensorEvaluator<Expr, Device>::Scalar> {
44 public:
45 // typedef typename TensorEvaluator<Expr, Device>::Dimensions Dimensions;
48 typedef typename Storage::Type EvaluatorPointerType;
50
51 TensorLazyEvaluatorReadOnly(const Expr& expr, const Device& device) : m_impl(expr, device), m_dummy(Scalar(0)) {
54 }
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 }
70 eigen_assert(false && "can't reference the coefficient of a rvalue");
71 return m_dummy;
72 };
73
74 protected:
76 Dimensions m_dims;
78};
79
80template <typename Dimensions, typename Expr, typename Device>
81class TensorLazyEvaluatorWritable : public TensorLazyEvaluatorReadOnly<Dimensions, Expr, Device> {
82 public:
84 typedef typename Base::Scalar Scalar;
86 typedef typename Storage::Type EvaluatorPointerType;
87
88 TensorLazyEvaluatorWritable(const Expr& expr, const Device& device) : Base(expr, device) {
89 }
92
94 return this->m_impl.coeffRef(index);
95 }
96};
97
98template <typename Dimensions, typename Expr, typename Device>
99class 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
124template<typename PlainObjectType> class TensorRef : public TensorBase<TensorRef<PlainObjectType> >
125{
126 public:
128 typedef typename PlainObjectType::Base Base;
134 typedef typename Base::CoeffReturnType CoeffReturnType;
137
138 static const Index NumIndices = PlainObjectType::NumIndices;
139 typedef typename PlainObjectType::Dimensions Dimensions;
140
141 enum {
142 IsAligned = false,
144 BlockAccess = 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
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
226 {
227 array<Index, 2> indices;
228 indices[0] = i0;
229 indices[1] = i1;
230 return coeff(indices);
231 }
234 {
235 array<Index, 3> indices;
236 indices[0] = i0;
237 indices[1] = i1;
238 indices[2] = i2;
239 return coeff(indices);
240 }
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 }
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 }
264 {
265 array<Index, 2> indices;
266 indices[0] = i0;
267 indices[1] = i1;
268 return coeffRef(indices);
269 }
272 {
273 array<Index, 3> indices;
274 indices[0] = i0;
275 indices[1] = i1;
276 indices[2] = i2;
277 return coeffRef(indices);
278 }
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 }
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
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
341 {
342 return m_evaluator->coeff(index);
343 }
344
347 {
348 return m_evaluator->coeffRef(index);
349 }
350
351 private:
352 EIGEN_STRONG_INLINE void unrefEvaluator() {
353 if (m_evaluator) {
354 m_evaluator->decrRefCount();
355 if (m_evaluator->refCount() == 0) {
356 delete m_evaluator;
357 }
358 }
359 }
360
361 internal::TensorLazyBaseEvaluator<Dimensions, Scalar>* m_evaluator;
362};
363
364
365// evaluator for rvalues
366template<typename Derived, typename Device>
367struct TensorEvaluator<const TensorRef<Derived>, Device>
368{
369 typedef typename Derived::Index Index;
370 typedef typename Derived::Scalar Scalar;
371 typedef typename Derived::Scalar CoeffReturnType;
373 typedef typename Derived::Dimensions Dimensions;
376
377 enum {
378 IsAligned = false,
380 BlockAccess = 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
400
402
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
419template<typename Derived, typename Device>
420struct TensorEvaluator<TensorRef<Derived>, Device> : public TensorEvaluator<const TensorRef<Derived>, Device>
421{
422 typedef typename Derived::Index Index;
423 typedef typename Derived::Scalar Scalar;
424 typedef typename Derived::Scalar CoeffReturnType;
426 typedef typename Derived::Dimensions Dimensions;
427
429
430 enum {
431 IsAligned = false,
433 BlockAccess = false,
435 RawAccess = false
436 };
437
438 //===- Tensor block evaluation strategy (see TensorBlock.h) -------------===//
440 //===--------------------------------------------------------------------===//
441
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
Definition AngleAxis_mimic_euler.cpp:1
int n
Definition BiCGSTAB_simple.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
SCALAR Scalar
Definition bench_gemm.cpp:46
The tensor base class.
Definition TensorBase.h:973
A reference to a tensor expression The expression will be evaluated lazily (as much as possible).
Definition TensorRef.h:125
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Scalar coeff(Index index) const
Definition TensorRef.h:340
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Index dimension(Index n) const
Definition TensorRef.h:193
internal::traits< PlainObjectType >::Index Index
Definition TensorRef.h:131
TensorRef(const TensorRef &other)
Definition TensorRef.h:175
PlainObjectType::Dimensions Dimensions
Definition TensorRef.h:139
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar & coeffRef(const array< Index, NumIndices > &indices)
Definition TensorRef.h:321
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Dimensions & dimensions() const
Definition TensorRef.h:195
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Scalar operator()(Index i0, Index i1, Index i2, Index i3, Index i4) const
Definition TensorRef.h:252
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar & operator()(Index i0, Index i1, Index i2, Index i3)
Definition TensorRef.h:280
~TensorRef()
Definition TensorRef.h:171
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Scalar coeff(const array< Index, NumIndices > &indices) const
Definition TensorRef.h:303
@ PacketAccess
Definition TensorRef.h:143
@ Layout
Definition TensorRef.h:146
@ PreferBlockAccess
Definition TensorRef.h:145
@ BlockAccess
Definition TensorRef.h:144
@ IsAligned
Definition TensorRef.h:142
@ CoordAccess
Definition TensorRef.h:147
@ RawAccess
Definition TensorRef.h:148
Base::CoeffReturnType CoeffReturnType
Definition TensorRef.h:134
NumTraits< Scalar >::Real RealScalar
Definition TensorRef.h:133
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Index rank() const
Definition TensorRef.h:191
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar & coeffRef(Index i0, Index i1, Index i2)
Definition TensorRef.h:271
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar & coeffRef(Index i0, Index i1, Index i2, Index i3, Index i4)
Definition TensorRef.h:290
static const Index NumIndices
Definition TensorRef.h:138
EIGEN_STRONG_INLINE TensorRef()
Definition TensorRef.h:155
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
PointerType PointerArgType
Definition TensorRef.h:136
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Scalar operator()(Index i0, Index i1, Index i2) const
Definition TensorRef.h:233
TensorRef< PlainObjectType > Self
Definition TensorRef.h:127
Eigen::internal::nested< Self >::type Nested
Definition TensorRef.h:129
internal::TensorBlockNotImplemented TensorBlock
Definition TensorRef.h:152
EIGEN_STRONG_INLINE TensorRef(const Expression &expr)
Definition TensorRef.h:159
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Scalar operator()(Index i0, Index i1) const
Definition TensorRef.h:225
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar & coeffRef(Index i0, Index i1)
Definition TensorRef.h:263
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Scalar * data() const
Definition TensorRef.h:199
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar & coeffRef(Index index)
Definition TensorRef.h:346
EIGEN_STRONG_INLINE TensorRef & operator=(const Expression &expr)
Definition TensorRef.h:164
internal::traits< PlainObjectType >::StorageKind StorageKind
Definition TensorRef.h:130
internal::traits< PlainObjectType >::Scalar Scalar
Definition TensorRef.h:132
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Scalar operator()(Index index) const
Definition TensorRef.h:202
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Index size() const
Definition TensorRef.h:197
Scalar * PointerType
Definition TensorRef.h:135
Definition EmulateArray.h:21
Definition TensorBlock.h:617
int refCount() const
Definition TensorRef.h:31
void decrRefCount()
Definition TensorRef.h:30
virtual EIGEN_DEVICE_FUNC const Scalar coeff(DenseIndex index) const =0
virtual ~TensorLazyBaseEvaluator()
Definition TensorRef.h:21
virtual EIGEN_DEVICE_FUNC const Dimensions & dimensions() const =0
virtual EIGEN_DEVICE_FUNC const Scalar * data() const =0
void incrRefCount()
Definition TensorRef.h:29
TensorLazyBaseEvaluator()
Definition TensorRef.h:20
virtual EIGEN_DEVICE_FUNC Scalar & coeffRef(DenseIndex index)=0
virtual EIGEN_DEVICE_FUNC Scalar & coeffRef(DenseIndex)
Definition TensorRef.h:69
virtual EIGEN_DEVICE_FUNC const Scalar coeff(DenseIndex index) const
Definition TensorRef.h:66
TensorLazyEvaluatorReadOnly(const Expr &expr, const Device &device)
Definition TensorRef.h:51
Storage::Type EvaluatorPointerType
Definition TensorRef.h:48
TensorEvaluator< Expr, Device >::Scalar Scalar
Definition TensorRef.h:46
Scalar m_dummy
Definition TensorRef.h:77
virtual ~TensorLazyEvaluatorReadOnly()
Definition TensorRef.h:55
StorageMemory< Scalar, Device > Storage
Definition TensorRef.h:47
virtual EIGEN_DEVICE_FUNC const Dimensions & dimensions() const
Definition TensorRef.h:59
virtual EIGEN_DEVICE_FUNC const Scalar * data() const
Definition TensorRef.h:62
TensorEvaluator< Expr, Device > m_impl
Definition TensorRef.h:75
TensorEvaluator< Expr, Device > EvalType
Definition TensorRef.h:49
Dimensions m_dims
Definition TensorRef.h:76
Storage::Type EvaluatorPointerType
Definition TensorRef.h:86
TensorLazyEvaluatorReadOnly< Dimensions, Expr, Device > Base
Definition TensorRef.h:83
virtual ~TensorLazyEvaluatorWritable()
Definition TensorRef.h:90
TensorLazyEvaluatorWritable(const Expr &expr, const Device &device)
Definition TensorRef.h:88
virtual EIGEN_DEVICE_FUNC Scalar & coeffRef(DenseIndex index)
Definition TensorRef.h:93
StorageMemory< Scalar, Device > Storage
Definition TensorRef.h:85
Base::Scalar Scalar
Definition TensorRef.h:84
Definition TensorRef.h:101
TensorLazyEvaluator(const Expr &expr, const Device &device)
Definition TensorRef.h:108
virtual ~TensorLazyEvaluator()
Definition TensorRef.h:110
Base::Scalar Scalar
Definition TensorRef.h:106
internal::conditional< bool(internal::is_lvalue< Expr >::value), TensorLazyEvaluatorWritable< Dimensions, Expr, Device >, TensorLazyEvaluatorReadOnly< Dimensions, constExpr, Device > >::type Base
Definition TensorRef.h:105
@ RowMajor
Definition Constants.h:321
Namespace containing all symbols from the Eigen library.
Definition bench_norm.cpp:85
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition Meta.h:74
EIGEN_DEFAULT_DENSE_INDEX_TYPE DenseIndex
Definition Meta.h:66
Definition BandTriangularSolver.h:13
Definition TensorDeviceDefault.h:17
Definition TensorForwardDeclarations.h:37
internal::TensorBlockNotImplemented TensorBlock
Definition TensorRef.h:439
TensorEvaluator< const TensorRef< Derived >, Device > Base
Definition TensorRef.h:428
PacketType< CoeffReturnType, Device >::type PacketReturnType
Definition TensorRef.h:425
Derived::Scalar CoeffReturnType
Definition TensorRef.h:424
EIGEN_STRONG_INLINE TensorEvaluator(TensorRef< Derived > &m, const Device &d)
Definition TensorRef.h:442
Derived::Dimensions Dimensions
Definition TensorRef.h:426
Derived::Index Index
Definition TensorRef.h:422
Derived::Scalar Scalar
Definition TensorRef.h:423
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar & coeffRef(Index index)
Definition TensorRef.h:445
Derived::Index Index
Definition TensorRef.h:369
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Dimensions & dimensions() const
Definition TensorRef.h:395
TensorRef< Derived > m_ref
Definition TensorRef.h:414
EIGEN_STRONG_INLINE void cleanup()
Definition TensorRef.h:401
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE CoeffReturnType coeff(Index index) const
Definition TensorRef.h:403
Derived::Scalar Scalar
Definition TensorRef.h:370
EIGEN_STRONG_INLINE bool evalSubExprsIfNeeded(EvaluatorPointerType)
Definition TensorRef.h:397
PacketType< CoeffReturnType, Device >::type PacketReturnType
Definition TensorRef.h:372
EIGEN_STRONG_INLINE TensorEvaluator(const TensorRef< Derived > &m, const Device &)
Definition TensorRef.h:391
Derived::Dimensions Dimensions
Definition TensorRef.h:373
internal::TensorBlockNotImplemented TensorBlock
Definition TensorRef.h:388
Derived::Scalar CoeffReturnType
Definition TensorRef.h:371
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar & coeffRef(Index index)
Definition TensorRef.h:407
EIGEN_DEVICE_FUNC const Scalar * data() const
Definition TensorRef.h:411
Storage::Type EvaluatorPointerType
Definition TensorRef.h:375
StorageMemory< CoeffReturnType, Device > Storage
Definition TensorRef.h:374
A cost model used to limit the number of threads used for evaluating tensor expression.
Definition TensorEvaluator.h:29
EIGEN_STRONG_INLINE bool evalSubExprsIfNeeded(EvaluatorPointerType dest)
Definition TensorEvaluator.h:75
Derived::Scalar Scalar
Definition TensorEvaluator.h:31
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE CoeffReturnType & coeffRef(Index index)
Definition TensorEvaluator.h:99
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Dimensions & dimensions() const
Definition TensorEvaluator.h:73
EIGEN_DEVICE_FUNC EvaluatorPointerType data() const
Definition TensorEvaluator.h:181
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE CoeffReturnType coeff(Index index) const
Definition TensorEvaluator.h:94
EIGEN_STRONG_INLINE void cleanup()
Definition TensorEvaluator.h:92
@ 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
Definition Meta.h:109
Definition XprHelper.h:660
Definition ForwardDeclarations.h:17