11#ifndef EIGEN_TENSOR_TENSOR_REF_H
12#define EIGEN_TENSOR_TENSOR_REF_H
15#include "./InternalHeaderCheck.h"
21template <
typename Dimensions,
typename Scalar>
22class TensorLazyBaseEvaluator {
24 TensorLazyBaseEvaluator() =
default;
25 virtual ~TensorLazyBaseEvaluator() =
default;
27 EIGEN_DEVICE_FUNC
virtual const Dimensions& dimensions()
const = 0;
28 EIGEN_DEVICE_FUNC
virtual const Scalar* data()
const = 0;
30 EIGEN_DEVICE_FUNC
virtual const Scalar coeff(DenseIndex index)
const = 0;
31 EIGEN_DEVICE_FUNC
virtual Scalar& coeffRef(DenseIndex index) = 0;
33 void incrRefCount() { ++m_refcount; }
34 void decrRefCount() { --m_refcount; }
35 int refCount()
const {
return m_refcount; }
38 TensorLazyBaseEvaluator(
const TensorLazyBaseEvaluator& other) =
delete;
39 TensorLazyBaseEvaluator& operator=(
const TensorLazyBaseEvaluator& other) =
delete;
44template <
typename Dimensions,
typename Expr,
typename Device>
45class TensorLazyEvaluatorReadOnly
46 :
public TensorLazyBaseEvaluator<Dimensions, typename TensorEvaluator<Expr, Device>::Scalar> {
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>;
54 TensorLazyEvaluatorReadOnly(
const Expr& expr,
const Device& device) : m_impl(expr, device), m_dummy() {
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];
62 m_impl.evalSubExprsIfNeeded(
nullptr);
64 virtual ~TensorLazyEvaluatorReadOnly() { m_impl.cleanup(); }
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(); }
69 EIGEN_DEVICE_FUNC
virtual const Scalar coeff(DenseIndex index)
const {
70 return m_impl.coeff(internal::convert_index<typename EvalType::Index>(index));
72 EIGEN_DEVICE_FUNC
virtual Scalar& coeffRef(DenseIndex ) {
73 eigen_assert(
false &&
"can't reference the coefficient of a rvalue");
78 TensorEvaluator<Expr, Device> m_impl;
83template <
typename Dimensions,
typename Expr,
typename Device>
84class TensorLazyEvaluatorWritable :
public TensorLazyEvaluatorReadOnly<Dimensions, Expr, Device> {
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;
91 TensorLazyEvaluatorWritable(
const Expr& expr,
const Device& device) : Base(expr, device) {}
92 virtual ~TensorLazyEvaluatorWritable() =
default;
94 EIGEN_DEVICE_FUNC
virtual Scalar& coeffRef(DenseIndex index) {
95 return this->m_impl.coeffRef(internal::convert_index<typename Base::EvalType::Index>(index));
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>> {
103 using Base = std::conditional_t<IsWritable, TensorLazyEvaluatorWritable<Dimensions, Expr, Device>,
104 TensorLazyEvaluatorReadOnly<Dimensions, const Expr, Device>>;
105 using Scalar =
typename Base::Scalar;
107 TensorLazyEvaluator(
const Expr& expr,
const Device& device) : Base(expr, device) {}
108 virtual ~TensorLazyEvaluator() =
default;
111template <
typename Derived>
112class TensorRefBase :
public TensorBase<Derived> {
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;
125 static constexpr Index NumIndices = PlainObjectType::NumIndices;
126 using Dimensions =
typename PlainObjectType::Dimensions;
128 static constexpr int Layout = PlainObjectType::Layout;
131 PacketAccess =
false,
133 PreferBlockAccess =
false,
139 using TensorBlock = TensorBlockNotImplemented;
142 EIGEN_STRONG_INLINE TensorRefBase() =
default;
144 TensorRefBase(
const TensorRefBase& other) : TensorBase<Derived>(other), m_evaluator(other.m_evaluator) {
146 eigen_assert(m_evaluator->refCount() > 0);
147 m_evaluator->incrRefCount();
151 TensorRefBase& operator=(
const TensorRefBase& other) {
152 if (
this != &other) {
154 m_evaluator = other.m_evaluator;
156 eigen_assert(m_evaluator->refCount() > 0);
157 m_evaluator->incrRefCount();
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 !std::is_const<PlainObjectType>::value &&
168 bool(is_lvalue<Expression>::value)>(expr, DefaultDevice())) {
169 m_evaluator->incrRefCount();
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) {
176 m_evaluator =
new TensorLazyEvaluator < Dimensions, Expression, DefaultDevice,
177 !std::is_const<PlainObjectType>::value&& bool(is_lvalue<Expression>::value) >
178 (expr, DefaultDevice());
179 m_evaluator->incrRefCount();
183 ~TensorRefBase() { unrefEvaluator(); }
185 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Index rank()
const {
return m_evaluator->dimensions().size(); }
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];
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];
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(); }
199 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
const Scalar operator()(Index index)
const {
return m_evaluator->coeff(index); }
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);
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();
213 EIGEN_IF_CONSTEXPR (PlainObjectType::Options &
RowMajor) {
215 for (
size_t i = 1; i < NumIndices; ++i) {
216 index = index * dims[i] + indices[i];
219 index += indices[NumIndices - 1];
220 for (
int i = NumIndices - 2; i >= 0; --i) {
221 index = index * dims[i] + indices[i];
224 return m_evaluator->coeff(index);
227 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
const Scalar coeff(Index index)
const {
return m_evaluator->coeff(index); }
229 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar& coeffRef(Index index) {
return m_evaluator->coeffRef(index); }
232 TensorLazyBaseEvaluator<Dimensions, Scalar>* evaluator() {
return m_evaluator; }
235 EIGEN_STRONG_INLINE
void unrefEvaluator() {
237 m_evaluator->decrRefCount();
238 if (m_evaluator->refCount() == 0) {
244 TensorLazyBaseEvaluator<Dimensions, Scalar>* m_evaluator =
nullptr;
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;
263template <
typename PlainObjectType>
264class TensorRef :
public internal::TensorRefBase<TensorRef<PlainObjectType>> {
265 using Base = internal::TensorRefBase<TensorRef<PlainObjectType>>;
268 using Scalar =
typename Base::Scalar;
269 using Dimensions =
typename Base::Dimensions;
272 using Index =
typename Base::Index;
274 EIGEN_STRONG_INLINE TensorRef() =
default;
276 EIGEN_STRONG_INLINE TensorRef(
const TensorRef& other) =
default;
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>?)");
285 TensorRef& operator=(
const TensorRef& other) {
return Base::operator=(other).derived(); }
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();
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);
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();
307 EIGEN_IF_CONSTEXPR (PlainObjectType::Options &
RowMajor) {
309 for (
size_t i = 1; i < NumIndices; ++i) {
310 index = index * dims[i] + indices[i];
313 index += indices[NumIndices - 1];
314 for (
int i = NumIndices - 2; i >= 0; --i) {
315 index = index * dims[i] + indices[i];
318 return Base::evaluator()->coeffRef(index);
321 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar& coeffRef(Index index) {
return Base::evaluator()->coeffRef(index); }
331template <
typename PlainObjectType>
332class TensorRef<const PlainObjectType> :
public internal::TensorRefBase<TensorRef<const PlainObjectType>> {
333 using Base = internal::TensorRefBase<TensorRef<const PlainObjectType>>;
336 EIGEN_STRONG_INLINE TensorRef() =
default;
338 EIGEN_STRONG_INLINE TensorRef(
const TensorRef& other) =
default;
340 template <
typename Expression>
341 EIGEN_STRONG_INLINE TensorRef(
const Expression& expr) : Base(expr) {}
343 TensorRef& operator=(
const TensorRef& other) {
return Base::operator=(other).derived(); }
345 template <
typename Expression>
346 EIGEN_STRONG_INLINE TensorRef& operator=(
const Expression& expr) {
347 return Base::operator=(expr).derived();
352template <
typename Derived,
typename 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;
362 static constexpr int Layout = TensorRef<Derived>::Layout;
365 PacketAccess =
false,
367 PreferBlockAccess =
false,
373 using TensorBlock = internal::TensorBlockNotImplemented;
376 EIGEN_STRONG_INLINE TensorEvaluator(
const TensorRef<Derived>& m,
const Device&) : m_ref(m) {}
378 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
const Dimensions& dimensions()
const {
return m_ref.dimensions(); }
380 EIGEN_STRONG_INLINE
bool evalSubExprsIfNeeded(EvaluatorPointerType) {
return true; }
382 EIGEN_STRONG_INLINE
void cleanup() {}
384 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE CoeffReturnType coeff(Index index)
const {
return m_ref.coeff(index); }
386 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar& coeffRef(Index index) {
return m_ref.coeffRef(index); }
388 EIGEN_DEVICE_FUNC
const Scalar* data()
const {
return m_ref.data(); }
391 TensorRef<Derived> m_ref;
395template <
typename Derived,
typename 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;
403 using Base = TensorEvaluator<const TensorRef<Derived>, Device>;
405 enum { IsAligned =
false, PacketAccess =
false, BlockAccess =
false, PreferBlockAccess =
false, RawAccess =
false };
408 using TensorBlock = internal::TensorBlockNotImplemented;
411 EIGEN_STRONG_INLINE TensorEvaluator(TensorRef<Derived>& m,
const Device& d) : Base(m, d) {}
413 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar& coeffRef(Index index) {
return this->m_ref.coeffRef(index); }
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