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;
34 void incrRefCount() { m_refcount.fetch_add(1, std::memory_order_relaxed); }
36 bool decrRefCount() {
return m_refcount.fetch_sub(1, std::memory_order_acq_rel) == 1; }
37 int refCount()
const {
return m_refcount.load(std::memory_order_relaxed); }
40 TensorLazyBaseEvaluator(
const TensorLazyBaseEvaluator& other) =
delete;
41 TensorLazyBaseEvaluator& operator=(
const TensorLazyBaseEvaluator& other) =
delete;
43 std::atomic<int> m_refcount{0};
46template <
typename Dimensions,
typename Expr,
typename Device>
47class TensorLazyEvaluatorReadOnly
48 :
public TensorLazyBaseEvaluator<Dimensions, typename TensorEvaluator<Expr, Device>::Scalar> {
50 EIGEN_MAKE_ALIGNED_OPERATOR_NEW
51 using Scalar =
typename TensorEvaluator<Expr, Device>::Scalar;
52 using Storage = StorageMemory<Scalar, Device>;
53 using EvaluatorPointerType =
typename Storage::Type;
54 using EvalType = TensorEvaluator<Expr, Device>;
56 TensorLazyEvaluatorReadOnly(
const Expr& expr,
const Device& device) : m_impl(expr, device), m_dummy() {
58 internal::array_size<Dimensions>::value == internal::array_size<typename EvalType::Dimensions>::value,
59 "Dimension sizes must match.");
60 const auto& other_dims = m_impl.dimensions();
61 for (std::size_t i = 0; i < m_dims.size(); ++i) {
62 m_dims[i] = other_dims[i];
64 m_impl.evalSubExprsIfNeeded(
nullptr);
66 virtual ~TensorLazyEvaluatorReadOnly() { m_impl.cleanup(); }
68 EIGEN_DEVICE_FUNC
virtual const Dimensions& dimensions()
const {
return m_dims; }
69 EIGEN_DEVICE_FUNC
virtual const Scalar* data()
const {
return m_impl.data(); }
71 EIGEN_DEVICE_FUNC
virtual const Scalar coeff(DenseIndex index)
const {
72 return m_impl.coeff(internal::convert_index<typename EvalType::Index>(index));
74 EIGEN_DEVICE_FUNC
virtual Scalar& coeffRef(DenseIndex ) {
75 eigen_assert(
false &&
"can't reference the coefficient of a rvalue");
80 TensorEvaluator<Expr, Device> m_impl;
85template <
typename Dimensions,
typename Expr,
typename Device>
86class TensorLazyEvaluatorWritable :
public TensorLazyEvaluatorReadOnly<Dimensions, Expr, Device> {
88 using Base = TensorLazyEvaluatorReadOnly<Dimensions, Expr, Device>;
89 using Scalar =
typename Base::Scalar;
90 using Storage = StorageMemory<Scalar, Device>;
91 using EvaluatorPointerType =
typename Storage::Type;
93 TensorLazyEvaluatorWritable(
const Expr& expr,
const Device& device) : Base(expr, device) {}
94 virtual ~TensorLazyEvaluatorWritable() =
default;
96 EIGEN_DEVICE_FUNC
virtual Scalar& coeffRef(DenseIndex index) {
97 return this->m_impl.coeffRef(internal::convert_index<typename Base::EvalType::Index>(index));
101template <
typename Dimensions,
typename Expr,
typename Device,
bool IsWritable>
102class TensorLazyEvaluator :
public std::conditional_t<IsWritable, TensorLazyEvaluatorWritable<Dimensions, Expr, Device>,
103 TensorLazyEvaluatorReadOnly<Dimensions, const Expr, Device>> {
105 using Base = std::conditional_t<IsWritable, TensorLazyEvaluatorWritable<Dimensions, Expr, Device>,
106 TensorLazyEvaluatorReadOnly<Dimensions, const Expr, Device>>;
107 using Scalar =
typename Base::Scalar;
109 TensorLazyEvaluator(
const Expr& expr,
const Device& device) : Base(expr, device) {}
110 virtual ~TensorLazyEvaluator() =
default;
113template <
typename Derived>
114class TensorRefBase :
public TensorBase<Derived> {
116 using PlainObjectType =
typename traits<Derived>::PlainObjectType;
117 using Base =
typename PlainObjectType::Base;
118 using Nested =
typename Eigen::internal::ref_selector<Derived>::type;
119 using StorageKind =
typename traits<PlainObjectType>::StorageKind;
120 using Index =
typename traits<PlainObjectType>::Index;
121 using Scalar =
typename traits<PlainObjectType>::Scalar;
122 using RealScalar =
typename NumTraits<Scalar>::Real;
123 using CoeffReturnType =
typename Base::CoeffReturnType;
124 using PointerType = Scalar*;
125 using PointerArgType = PointerType;
127 static constexpr Index NumIndices = PlainObjectType::NumIndices;
128 using Dimensions =
typename PlainObjectType::Dimensions;
130 static constexpr int Layout = PlainObjectType::Layout;
133 PacketAccess =
false,
135 PreferBlockAccess =
false,
141 using TensorBlock = TensorBlockNotImplemented;
144 EIGEN_STRONG_INLINE TensorRefBase() =
default;
146 TensorRefBase(
const TensorRefBase& other) : TensorBase<Derived>(other), m_evaluator(other.m_evaluator) {
148 eigen_assert(m_evaluator->refCount() > 0);
149 m_evaluator->incrRefCount();
153 TensorRefBase& operator=(
const TensorRefBase& other) {
154 if (
this != &other) {
156 m_evaluator = other.m_evaluator;
158 eigen_assert(m_evaluator->refCount() > 0);
159 m_evaluator->incrRefCount();
165 template <
typename Expression,
166 typename EnableIf = std::enable_if_t<!std::is_same<std::decay_t<Expression>, Derived>::value>>
167 EIGEN_STRONG_INLINE TensorRefBase(
const Expression& expr)
168 : m_evaluator(new TensorLazyEvaluator<Dimensions, Expression, DefaultDevice,
169 !std::is_const<PlainObjectType>::value &&
170 bool(is_lvalue<Expression>::value)>(expr, DefaultDevice())) {
171 m_evaluator->incrRefCount();
174 template <
typename Expression,
175 typename EnableIf = std::enable_if_t<!std::is_same<std::decay_t<Expression>, Derived>::value>>
176 EIGEN_STRONG_INLINE TensorRefBase& operator=(
const Expression& expr) {
178 m_evaluator =
new TensorLazyEvaluator < Dimensions, Expression, DefaultDevice,
179 !std::is_const<PlainObjectType>::value&& bool(is_lvalue<Expression>::value) >
180 (expr, DefaultDevice());
181 m_evaluator->incrRefCount();
185 ~TensorRefBase() { unrefEvaluator(); }
187 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Index rank()
const {
return m_evaluator->dimensions().size(); }
189 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Index dimension()
const {
190 EIGEN_STATIC_ASSERT(0 <= N && N < NumIndices, INVALID_DIMENSION);
191 return m_evaluator->dimensions()[N];
193 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Index dimension(Index n)
const {
194 eigen_assert(0 <= n && n < NumIndices);
195 return m_evaluator->dimensions()[n];
197 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
const Dimensions& dimensions()
const {
return m_evaluator->dimensions(); }
198 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Index size()
const {
return m_evaluator->dimensions().TotalSize(); }
199 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
const Scalar* data()
const {
return m_evaluator->data(); }
201 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
const Scalar operator()(Index index)
const {
return m_evaluator->coeff(index); }
203 template <
typename... IndexTypes>
204 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
const Scalar operator()(Index firstIndex, IndexTypes... otherIndices)
const {
205 eigen_assert(internal::indices_fit<Index>(otherIndices...));
206 const std::size_t num_indices =
sizeof...(otherIndices) + 1;
207 const array<Index, num_indices> indices{{firstIndex,
static_cast<Index
>(otherIndices)...}};
208 return coeff(indices);
211 template <std::
size_t NumIndices>
212 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
const Scalar coeff(
const array<Index, NumIndices>& indices)
const {
213 const Dimensions& dims = this->dimensions();
215 EIGEN_IF_CONSTEXPR (PlainObjectType::Options &
RowMajor) {
217 for (
size_t i = 1; i < NumIndices; ++i) {
218 index = index * dims[i] + indices[i];
221 index += indices[NumIndices - 1];
222 for (
int i = NumIndices - 2; i >= 0; --i) {
223 index = index * dims[i] + indices[i];
226 return m_evaluator->coeff(index);
229 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
const Scalar coeff(Index index)
const {
return m_evaluator->coeff(index); }
231 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar& coeffRef(Index index) {
return m_evaluator->coeffRef(index); }
234 TensorLazyBaseEvaluator<Dimensions, Scalar>* evaluator() {
return m_evaluator; }
237 EIGEN_STRONG_INLINE
void unrefEvaluator() {
239 if (m_evaluator->decrRefCount()) {
245 TensorLazyBaseEvaluator<Dimensions, Scalar>* m_evaluator =
nullptr;
248#if EIGEN_COMP_CXXVER < 17
249template <
typename Derived>
250constexpr typename TensorRefBase<Derived>::Index TensorRefBase<Derived>::NumIndices;
251template <
typename Derived>
252constexpr int TensorRefBase<Derived>::Layout;
264template <
typename PlainObjectType>
265class TensorRef :
public internal::TensorRefBase<TensorRef<PlainObjectType>> {
266 using Base = internal::TensorRefBase<TensorRef<PlainObjectType>>;
269 using Scalar =
typename Base::Scalar;
270 using Dimensions =
typename Base::Dimensions;
273 using Index =
typename Base::Index;
275 EIGEN_STRONG_INLINE TensorRef() =
default;
277 EIGEN_STRONG_INLINE TensorRef(
const TensorRef& other) =
default;
279 template <
typename Expression>
280 EIGEN_STRONG_INLINE TensorRef(
const Expression& expr) : Base(expr) {
281 EIGEN_STATIC_ASSERT(internal::is_lvalue<Expression>::value,
282 "Expression must be mutable to create a mutable TensorRef<Expression>. Did you mean "
283 "TensorRef<const Expression>?)");
286 TensorRef& operator=(
const TensorRef& other) {
return Base::operator=(other).derived(); }
288 template <
typename Expression>
289 EIGEN_STRONG_INLINE TensorRef& operator=(
const Expression& expr) {
290 EIGEN_STATIC_ASSERT(internal::is_lvalue<Expression>::value,
291 "Expression must be mutable to create a mutable TensorRef<Expression>. Did you mean "
292 "TensorRef<const Expression>?)");
293 return Base::operator=(expr).derived();
296 template <
typename... IndexTypes>
297 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar& coeffRef(Index firstIndex, IndexTypes... otherIndices) {
298 eigen_assert(internal::indices_fit<Index>(otherIndices...));
299 const std::size_t num_indices =
sizeof...(otherIndices) + 1;
300 const array<Index, num_indices> indices{{firstIndex,
static_cast<Index
>(otherIndices)...}};
301 return coeffRef(indices);
304 template <std::
size_t NumIndices>
305 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar& coeffRef(
const array<Index, NumIndices>& indices) {
306 const Dimensions& dims = this->dimensions();
308 EIGEN_IF_CONSTEXPR (PlainObjectType::Options &
RowMajor) {
310 for (
size_t i = 1; i < NumIndices; ++i) {
311 index = index * dims[i] + indices[i];
314 index += indices[NumIndices - 1];
315 for (
int i = NumIndices - 2; i >= 0; --i) {
316 index = index * dims[i] + indices[i];
319 return Base::evaluator()->coeffRef(index);
322 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar& coeffRef(Index index) {
return Base::evaluator()->coeffRef(index); }
332template <
typename PlainObjectType>
333class TensorRef<const PlainObjectType> :
public internal::TensorRefBase<TensorRef<const PlainObjectType>> {
334 using Base = internal::TensorRefBase<TensorRef<const PlainObjectType>>;
337 EIGEN_STRONG_INLINE TensorRef() =
default;
339 EIGEN_STRONG_INLINE TensorRef(
const TensorRef& other) =
default;
341 template <
typename Expression>
342 EIGEN_STRONG_INLINE TensorRef(
const Expression& expr) : Base(expr) {}
344 TensorRef& operator=(
const TensorRef& other) {
return Base::operator=(other).derived(); }
346 template <
typename Expression>
347 EIGEN_STRONG_INLINE TensorRef& operator=(
const Expression& expr) {
348 return Base::operator=(expr).derived();
353template <
typename Derived,
typename Device>
355 using Index =
typename Derived::Index;
356 using Scalar =
typename Derived::Scalar;
357 using CoeffReturnType =
typename Derived::Scalar;
358 using PacketReturnType =
typename PacketType<CoeffReturnType, Device>::type;
359 using Dimensions =
typename Derived::Dimensions;
360 using Storage = StorageMemory<CoeffReturnType, Device>;
361 using EvaluatorPointerType =
typename Storage::Type;
363 static constexpr int Layout = TensorRef<Derived>::Layout;
366 PacketAccess =
false,
368 PreferBlockAccess =
false,
374 using TensorBlock = internal::TensorBlockNotImplemented;
377 EIGEN_STRONG_INLINE TensorEvaluator(
const TensorRef<Derived>& m,
const Device&) : m_ref(m) {}
379 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
const Dimensions& dimensions()
const {
return m_ref.dimensions(); }
381#ifdef EIGEN_USE_THREADS
382 template <
typename EvalSubExprsCallback>
383 EIGEN_STRONG_INLINE
void evalSubExprsIfNeededAsync(EvaluatorPointerType, EvalSubExprsCallback done) {
388 EIGEN_STRONG_INLINE
bool evalSubExprsIfNeeded(EvaluatorPointerType) {
return true; }
390 EIGEN_STRONG_INLINE
void cleanup() {}
393 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorOpCost costPerCoeff(
bool )
const {
394 return TensorOpCost(
sizeof(CoeffReturnType), 0, 0);
397 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE CoeffReturnType coeff(Index index)
const {
return m_ref.coeff(index); }
399 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar& coeffRef(Index index) {
return m_ref.coeffRef(index); }
401 EIGEN_DEVICE_FUNC
const Scalar* data()
const {
return m_ref.data(); }
404 TensorRef<Derived> m_ref;
408template <
typename Derived,
typename Device>
410 using Index =
typename Derived::Index;
411 using Scalar =
typename Derived::Scalar;
412 using CoeffReturnType =
typename Derived::Scalar;
413 using PacketReturnType =
typename PacketType<CoeffReturnType, Device>::type;
414 using Dimensions =
typename Derived::Dimensions;
416 using Base = TensorEvaluator<const TensorRef<Derived>, Device>;
418 enum { IsAligned =
false, PacketAccess =
false, BlockAccess =
false, PreferBlockAccess =
false, RawAccess =
false };
421 using TensorBlock = internal::TensorBlockNotImplemented;
424 EIGEN_STRONG_INLINE TensorEvaluator(TensorRef<Derived>& m,
const Device& d) : Base(m, d) {}
426 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:265
Namespace containing all symbols from the Eigen library.
The tensor evaluator class.
Definition TensorEvaluator.h:47