Eigen-Contrib  5.0.1-dev+master
 
Loading...
Searching...
No Matches
TensorRef.h
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// SPDX-License-Identifier: MPL-2.0
10
11#ifndef EIGEN_TENSOR_TENSOR_REF_H
12#define EIGEN_TENSOR_TENSOR_REF_H
13
14// IWYU pragma: private
15#include "./InternalHeaderCheck.h"
16
17namespace Eigen {
18
19namespace internal {
20
21template <typename Dimensions, typename Scalar>
22class TensorLazyBaseEvaluator {
23 public:
24 TensorLazyBaseEvaluator() = default;
25 virtual ~TensorLazyBaseEvaluator() = default;
26
27 EIGEN_DEVICE_FUNC virtual const Dimensions& dimensions() const = 0;
28 EIGEN_DEVICE_FUNC virtual const Scalar* data() const = 0;
29
30 EIGEN_DEVICE_FUNC virtual const Scalar coeff(DenseIndex index) const = 0;
31 EIGEN_DEVICE_FUNC virtual Scalar& coeffRef(DenseIndex index) = 0;
32
33 void incrRefCount() { ++m_refcount; }
34 void decrRefCount() { --m_refcount; }
35 int refCount() const { return m_refcount; }
36
37 private:
38 TensorLazyBaseEvaluator(const TensorLazyBaseEvaluator& other) = delete;
39 TensorLazyBaseEvaluator& operator=(const TensorLazyBaseEvaluator& other) = delete;
40
41 int m_refcount = 0;
42};
43
44template <typename Dimensions, typename Expr, typename Device>
45class TensorLazyEvaluatorReadOnly
46 : public TensorLazyBaseEvaluator<Dimensions, typename TensorEvaluator<Expr, Device>::Scalar> {
47 public:
48 EIGEN_MAKE_ALIGNED_OPERATOR_NEW
49 using Scalar = typename TensorEvaluator<Expr, Device>::Scalar;
50 using Storage = StorageMemory<Scalar, Device>;
51 using EvaluatorPointerType = typename Storage::Type;
52 using EvalType = TensorEvaluator<Expr, Device>;
53
54 TensorLazyEvaluatorReadOnly(const Expr& expr, const Device& device) : m_impl(expr, device), m_dummy() {
55 EIGEN_STATIC_ASSERT(
56 internal::array_size<Dimensions>::value == internal::array_size<typename EvalType::Dimensions>::value,
57 "Dimension sizes must match.");
58 const auto& other_dims = m_impl.dimensions();
59 for (std::size_t i = 0; i < m_dims.size(); ++i) {
60 m_dims[i] = other_dims[i];
61 }
62 m_impl.evalSubExprsIfNeeded(nullptr);
63 }
64 virtual ~TensorLazyEvaluatorReadOnly() { m_impl.cleanup(); }
65
66 EIGEN_DEVICE_FUNC virtual const Dimensions& dimensions() const { return m_dims; }
67 EIGEN_DEVICE_FUNC virtual const Scalar* data() const { return m_impl.data(); }
68
69 EIGEN_DEVICE_FUNC virtual const Scalar coeff(DenseIndex index) const {
70 return m_impl.coeff(internal::convert_index<typename EvalType::Index>(index));
71 }
72 EIGEN_DEVICE_FUNC virtual Scalar& coeffRef(DenseIndex /*index*/) {
73 eigen_assert(false && "can't reference the coefficient of a rvalue");
74 return m_dummy;
75 }
76
77 protected:
78 TensorEvaluator<Expr, Device> m_impl;
79 Dimensions m_dims;
80 Scalar m_dummy;
81};
82
83template <typename Dimensions, typename Expr, typename Device>
84class TensorLazyEvaluatorWritable : public TensorLazyEvaluatorReadOnly<Dimensions, Expr, Device> {
85 public:
86 using Base = TensorLazyEvaluatorReadOnly<Dimensions, Expr, Device>;
87 using Scalar = typename Base::Scalar;
88 using Storage = StorageMemory<Scalar, Device>;
89 using EvaluatorPointerType = typename Storage::Type;
90
91 TensorLazyEvaluatorWritable(const Expr& expr, const Device& device) : Base(expr, device) {}
92 virtual ~TensorLazyEvaluatorWritable() = default;
93
94 EIGEN_DEVICE_FUNC virtual Scalar& coeffRef(DenseIndex index) {
95 return this->m_impl.coeffRef(internal::convert_index<typename Base::EvalType::Index>(index));
96 }
97};
98
99template <typename Dimensions, typename Expr, typename Device, bool IsWritable>
100class TensorLazyEvaluator : public std::conditional_t<IsWritable, TensorLazyEvaluatorWritable<Dimensions, Expr, Device>,
101 TensorLazyEvaluatorReadOnly<Dimensions, const Expr, Device>> {
102 public:
103 using Base = std::conditional_t<IsWritable, TensorLazyEvaluatorWritable<Dimensions, Expr, Device>,
104 TensorLazyEvaluatorReadOnly<Dimensions, const Expr, Device>>;
105 using Scalar = typename Base::Scalar;
106
107 TensorLazyEvaluator(const Expr& expr, const Device& device) : Base(expr, device) {}
108 virtual ~TensorLazyEvaluator() = default;
109};
110
111template <typename Derived>
112class TensorRefBase : public TensorBase<Derived> {
113 public:
114 using PlainObjectType = typename traits<Derived>::PlainObjectType;
115 using Base = typename PlainObjectType::Base;
116 using Nested = typename Eigen::internal::ref_selector<Derived>::type;
117 using StorageKind = typename traits<PlainObjectType>::StorageKind;
118 using Index = typename traits<PlainObjectType>::Index;
119 using Scalar = typename traits<PlainObjectType>::Scalar;
120 using RealScalar = typename NumTraits<Scalar>::Real;
121 using CoeffReturnType = typename Base::CoeffReturnType;
122 using PointerType = Scalar*;
123 using PointerArgType = PointerType;
124
125 static constexpr Index NumIndices = PlainObjectType::NumIndices;
126 using Dimensions = typename PlainObjectType::Dimensions;
127
128 static constexpr int Layout = PlainObjectType::Layout;
129 enum {
130 IsAligned = false,
131 PacketAccess = false,
132 BlockAccess = false,
133 PreferBlockAccess = false,
134 CoordAccess = false, // to be implemented
135 RawAccess = false
136 };
137
138 //===- Tensor block evaluation strategy (see TensorBlock.h) -----------===//
139 using TensorBlock = TensorBlockNotImplemented;
140 //===------------------------------------------------------------------===//
141
142 EIGEN_STRONG_INLINE TensorRefBase() = default;
143
144 TensorRefBase(const TensorRefBase& other) : TensorBase<Derived>(other), m_evaluator(other.m_evaluator) {
145 if (m_evaluator) {
146 eigen_assert(m_evaluator->refCount() > 0);
147 m_evaluator->incrRefCount();
148 }
149 }
150
151 TensorRefBase& operator=(const TensorRefBase& other) {
152 if (this != &other) {
153 unrefEvaluator();
154 m_evaluator = other.m_evaluator;
155 if (m_evaluator) {
156 eigen_assert(m_evaluator->refCount() > 0);
157 m_evaluator->incrRefCount();
158 }
159 }
160 return *this;
161 }
162
163 template <typename Expression,
164 typename EnableIf = std::enable_if_t<!std::is_same<std::decay_t<Expression>, Derived>::value>>
165 EIGEN_STRONG_INLINE TensorRefBase(const Expression& expr)
166 : m_evaluator(new TensorLazyEvaluator<Dimensions, Expression, DefaultDevice,
167 /*IsWritable=*/!std::is_const<PlainObjectType>::value &&
168 bool(is_lvalue<Expression>::value)>(expr, DefaultDevice())) {
169 m_evaluator->incrRefCount();
170 }
171
172 template <typename Expression,
173 typename EnableIf = std::enable_if_t<!std::is_same<std::decay_t<Expression>, Derived>::value>>
174 EIGEN_STRONG_INLINE TensorRefBase& operator=(const Expression& expr) {
175 unrefEvaluator();
176 m_evaluator = new TensorLazyEvaluator < Dimensions, Expression, DefaultDevice,
177 /*IsWritable=*/!std::is_const<PlainObjectType>::value&& bool(is_lvalue<Expression>::value) >
178 (expr, DefaultDevice());
179 m_evaluator->incrRefCount();
180 return *this;
181 }
182
183 ~TensorRefBase() { unrefEvaluator(); }
184
185 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Index rank() const { return m_evaluator->dimensions().size(); }
186 template <Index N>
187 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Index dimension() const {
188 EIGEN_STATIC_ASSERT(0 <= N && N < NumIndices, INVALID_DIMENSION);
189 return m_evaluator->dimensions()[N];
190 }
191 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Index dimension(Index n) const {
192 eigen_assert(0 <= n && n < NumIndices);
193 return m_evaluator->dimensions()[n];
194 }
195 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Dimensions& dimensions() const { return m_evaluator->dimensions(); }
196 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Index size() const { return m_evaluator->dimensions().TotalSize(); }
197 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Scalar* data() const { return m_evaluator->data(); }
198
199 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Scalar operator()(Index index) const { return m_evaluator->coeff(index); }
200
201 template <typename... IndexTypes>
202 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Scalar operator()(Index firstIndex, IndexTypes... otherIndices) const {
203 eigen_assert(internal::indices_fit<Index>(otherIndices...));
204 const std::size_t num_indices = sizeof...(otherIndices) + 1;
205 const array<Index, num_indices> indices{{firstIndex, static_cast<Index>(otherIndices)...}};
206 return coeff(indices);
207 }
208
209 template <std::size_t NumIndices>
210 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Scalar coeff(const array<Index, NumIndices>& indices) const {
211 const Dimensions& dims = this->dimensions();
212 Index index = 0;
213 EIGEN_IF_CONSTEXPR (PlainObjectType::Options & RowMajor) {
214 index += indices[0];
215 for (size_t i = 1; i < NumIndices; ++i) {
216 index = index * dims[i] + indices[i];
217 }
218 } else {
219 index += indices[NumIndices - 1];
220 for (int i = NumIndices - 2; i >= 0; --i) {
221 index = index * dims[i] + indices[i];
222 }
223 }
224 return m_evaluator->coeff(index);
225 }
226
227 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Scalar coeff(Index index) const { return m_evaluator->coeff(index); }
228
229 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar& coeffRef(Index index) { return m_evaluator->coeffRef(index); }
230
231 protected:
232 TensorLazyBaseEvaluator<Dimensions, Scalar>* evaluator() { return m_evaluator; }
233
234 private:
235 EIGEN_STRONG_INLINE void unrefEvaluator() {
236 if (m_evaluator) {
237 m_evaluator->decrRefCount();
238 if (m_evaluator->refCount() == 0) {
239 delete m_evaluator;
240 }
241 }
242 }
243
244 TensorLazyBaseEvaluator<Dimensions, Scalar>* m_evaluator = nullptr;
245};
246
247#if EIGEN_COMP_CXXVER < 17
248template <typename Derived>
249constexpr typename TensorRefBase<Derived>::Index TensorRefBase<Derived>::NumIndices;
250template <typename Derived>
251constexpr int TensorRefBase<Derived>::Layout;
252#endif
253
254} // namespace internal
255
263template <typename PlainObjectType>
264class TensorRef : public internal::TensorRefBase<TensorRef<PlainObjectType>> {
265 using Base = internal::TensorRefBase<TensorRef<PlainObjectType>>;
266
267 public:
268 using Scalar = typename Base::Scalar;
269 using Dimensions = typename Base::Dimensions;
270 // Without this, unqualified Index below does not find the dependent base's typedef and resolves to Eigen::Index,
271 // giving the accessors a different index type than the rest of the class.
272 using Index = typename Base::Index;
273
274 EIGEN_STRONG_INLINE TensorRef() = default;
275
276 EIGEN_STRONG_INLINE TensorRef(const TensorRef& other) = default;
277
278 template <typename Expression>
279 EIGEN_STRONG_INLINE TensorRef(const Expression& expr) : Base(expr) {
280 EIGEN_STATIC_ASSERT(internal::is_lvalue<Expression>::value,
281 "Expression must be mutable to create a mutable TensorRef<Expression>. Did you mean "
282 "TensorRef<const Expression>?)");
283 }
284
285 TensorRef& operator=(const TensorRef& other) { return Base::operator=(other).derived(); }
286
287 template <typename Expression>
288 EIGEN_STRONG_INLINE TensorRef& operator=(const Expression& expr) {
289 EIGEN_STATIC_ASSERT(internal::is_lvalue<Expression>::value,
290 "Expression must be mutable to create a mutable TensorRef<Expression>. Did you mean "
291 "TensorRef<const Expression>?)");
292 return Base::operator=(expr).derived();
293 }
294
295 template <typename... IndexTypes>
296 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar& coeffRef(Index firstIndex, IndexTypes... otherIndices) {
297 eigen_assert(internal::indices_fit<Index>(otherIndices...));
298 const std::size_t num_indices = sizeof...(otherIndices) + 1;
299 const array<Index, num_indices> indices{{firstIndex, static_cast<Index>(otherIndices)...}};
300 return coeffRef(indices);
301 }
302
303 template <std::size_t NumIndices>
304 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar& coeffRef(const array<Index, NumIndices>& indices) {
305 const Dimensions& dims = this->dimensions();
306 Index index = 0;
307 EIGEN_IF_CONSTEXPR (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 Base::evaluator()->coeffRef(index);
319 }
320
321 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar& coeffRef(Index index) { return Base::evaluator()->coeffRef(index); }
322};
323
331template <typename PlainObjectType>
332class TensorRef<const PlainObjectType> : public internal::TensorRefBase<TensorRef<const PlainObjectType>> {
333 using Base = internal::TensorRefBase<TensorRef<const PlainObjectType>>;
334
335 public:
336 EIGEN_STRONG_INLINE TensorRef() = default;
337
338 EIGEN_STRONG_INLINE TensorRef(const TensorRef& other) = default;
339
340 template <typename Expression>
341 EIGEN_STRONG_INLINE TensorRef(const Expression& expr) : Base(expr) {}
342
343 TensorRef& operator=(const TensorRef& other) { return Base::operator=(other).derived(); }
344
345 template <typename Expression>
346 EIGEN_STRONG_INLINE TensorRef& operator=(const Expression& expr) {
347 return Base::operator=(expr).derived();
348 }
349};
350
351// evaluator for rvalues
352template <typename Derived, typename Device>
353struct TensorEvaluator<const TensorRef<Derived>, Device> {
354 using Index = typename Derived::Index;
355 using Scalar = typename Derived::Scalar;
356 using CoeffReturnType = typename Derived::Scalar;
357 using PacketReturnType = typename PacketType<CoeffReturnType, Device>::type;
358 using Dimensions = typename Derived::Dimensions;
359 using Storage = StorageMemory<CoeffReturnType, Device>;
360 using EvaluatorPointerType = typename Storage::Type;
361
362 static constexpr int Layout = TensorRef<Derived>::Layout;
363 enum {
364 IsAligned = false,
365 PacketAccess = false,
366 BlockAccess = false,
367 PreferBlockAccess = false,
368 CoordAccess = false, // to be implemented
369 RawAccess = false
370 };
371
372 //===- Tensor block evaluation strategy (see TensorBlock.h) -------------===//
373 using TensorBlock = internal::TensorBlockNotImplemented;
374 //===--------------------------------------------------------------------===//
375
376 EIGEN_STRONG_INLINE TensorEvaluator(const TensorRef<Derived>& m, const Device&) : m_ref(m) {}
377
378 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Dimensions& dimensions() const { return m_ref.dimensions(); }
379
380 EIGEN_STRONG_INLINE bool evalSubExprsIfNeeded(EvaluatorPointerType) { return true; }
381
382 EIGEN_STRONG_INLINE void cleanup() {}
383
384 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE CoeffReturnType coeff(Index index) const { return m_ref.coeff(index); }
385
386 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar& coeffRef(Index index) { return m_ref.coeffRef(index); }
387
388 EIGEN_DEVICE_FUNC const Scalar* data() const { return m_ref.data(); }
389
390 protected:
391 TensorRef<Derived> m_ref;
392};
393
394// evaluator for lvalues
395template <typename Derived, typename Device>
396struct TensorEvaluator<TensorRef<Derived>, Device> : public TensorEvaluator<const TensorRef<Derived>, Device> {
397 using Index = typename Derived::Index;
398 using Scalar = typename Derived::Scalar;
399 using CoeffReturnType = typename Derived::Scalar;
400 using PacketReturnType = typename PacketType<CoeffReturnType, Device>::type;
401 using Dimensions = typename Derived::Dimensions;
402
403 using Base = TensorEvaluator<const TensorRef<Derived>, Device>;
404
405 enum { IsAligned = false, PacketAccess = false, BlockAccess = false, PreferBlockAccess = false, RawAccess = false };
406
407 //===- Tensor block evaluation strategy (see TensorBlock.h) -------------===//
408 using TensorBlock = internal::TensorBlockNotImplemented;
409 //===--------------------------------------------------------------------===//
410
411 EIGEN_STRONG_INLINE TensorEvaluator(TensorRef<Derived>& m, const Device& d) : Base(m, d) {}
412
413 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar& coeffRef(Index index) { return this->m_ref.coeffRef(index); }
414};
415
416} // end namespace Eigen
417
418#endif // EIGEN_TENSOR_TENSOR_REF_H
A reference to a tensor expression The expression will be evaluated lazily (as much as possible).
Definition TensorRef.h:264
Namespace containing all symbols from the Eigen library.
The tensor evaluator class.
Definition TensorEvaluator.h:47