Eigen-Contrib  5.0.1
 
Loading...
Searching...
No Matches
TensorEvalTo.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_EVAL_TO_H
12#define EIGEN_TENSOR_TENSOR_EVAL_TO_H
13
14// IWYU pragma: private
15#include "./InternalHeaderCheck.h"
16
17namespace Eigen {
18
19namespace internal {
20template <typename XprType, template <class> class MakePointer_>
21struct traits<TensorEvalToOp<XprType, MakePointer_> > {
22 typedef typename XprType::Scalar Scalar;
23 typedef traits<XprType> XprTraits;
24 typedef typename XprTraits::StorageKind StorageKind;
25 typedef typename XprTraits::Index Index;
26 static constexpr int NumDimensions = XprTraits::NumDimensions;
27 static constexpr int Layout = XprTraits::Layout;
28 typedef typename MakePointer_<Scalar>::Type PointerType;
29
30 enum { Flags = 0 };
31 template <class T>
32 struct MakePointer {
33 typedef typename MakePointer_<T>::Type Type;
34 };
35};
36
37template <typename XprType, template <class> class MakePointer_>
38struct eval<TensorEvalToOp<XprType, MakePointer_>, Eigen::Dense> {
39 typedef const TensorEvalToOp<XprType, MakePointer_>& type;
40};
41
42} // end namespace internal
43
44template <typename XprType, template <class> class MakePointer_>
45class TensorEvalToOp : public TensorBase<TensorEvalToOp<XprType, MakePointer_>, ReadOnlyAccessors> {
46 public:
47 typedef typename Eigen::internal::traits<TensorEvalToOp>::Scalar Scalar;
48 typedef typename Eigen::NumTraits<Scalar>::Real RealScalar;
49 typedef std::remove_const_t<typename XprType::CoeffReturnType> CoeffReturnType;
50 typedef typename MakePointer_<CoeffReturnType>::Type PointerType;
51 typedef typename Eigen::internal::ref_selector<TensorEvalToOp>::type Nested;
52 typedef typename Eigen::internal::traits<TensorEvalToOp>::StorageKind StorageKind;
53 typedef typename Eigen::internal::traits<TensorEvalToOp>::Index Index;
54
55 static constexpr int NumDims = Eigen::internal::traits<TensorEvalToOp>::NumDimensions;
56
57 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorEvalToOp(PointerType buffer, const XprType& expr)
58 : m_xpr(expr), m_buffer(buffer) {}
59
60 EIGEN_DEVICE_FUNC const internal::remove_all_t<typename XprType::Nested>& expression() const { return m_xpr; }
61
62 EIGEN_DEVICE_FUNC PointerType buffer() const { return m_buffer; }
63
64 protected:
65 typename XprType::Nested m_xpr;
66 PointerType m_buffer;
67};
68
69template <typename ArgType, typename Device, template <class> class MakePointer_>
70struct TensorEvaluator<const TensorEvalToOp<ArgType, MakePointer_>, Device> {
71 typedef TensorEvalToOp<ArgType, MakePointer_> XprType;
72 typedef typename ArgType::Scalar Scalar;
73 typedef typename TensorEvaluator<ArgType, Device>::Dimensions Dimensions;
74 typedef typename XprType::Index Index;
75 typedef std::remove_const_t<typename XprType::CoeffReturnType> CoeffReturnType;
76 typedef typename PacketType<CoeffReturnType, Device>::type PacketReturnType;
77 static constexpr int PacketSize = PacketType<CoeffReturnType, Device>::size;
78 typedef typename Eigen::internal::traits<XprType>::PointerType TensorPointerType;
79 typedef StorageMemory<CoeffReturnType, Device> Storage;
80 typedef typename Storage::Type EvaluatorPointerType;
81 enum {
82 IsAligned = TensorEvaluator<ArgType, Device>::IsAligned,
83 PacketAccess = TensorEvaluator<ArgType, Device>::PacketAccess,
84 BlockAccess = true,
85 PreferBlockAccess = false,
86 CoordAccess = false, // to be implemented
87 RawAccess = true
88 };
89
90 static constexpr int Layout = TensorEvaluator<ArgType, Device>::Layout;
91 static constexpr int NumDims = internal::traits<ArgType>::NumDimensions;
92
93 //===- Tensor block evaluation strategy (see TensorBlock.h) -------------===//
94 typedef internal::TensorBlockDescriptor<NumDims, Index> TensorBlockDesc;
95 typedef internal::TensorBlockScratchAllocator<Device> TensorBlockScratch;
96
97 typedef typename TensorEvaluator<const ArgType, Device>::TensorBlock ArgTensorBlock;
98
99 typedef internal::TensorBlockAssignment<CoeffReturnType, NumDims, typename ArgTensorBlock::XprType, Index>
100 TensorBlockAssignment;
101 //===--------------------------------------------------------------------===//
102
103 EIGEN_STRONG_INLINE TensorEvaluator(const XprType& op, const Device& device)
104 : m_impl(op.expression(), device), m_buffer(device.get(op.buffer())), m_expression(op.expression()) {}
105
106 EIGEN_DEVICE_FUNC const Dimensions& dimensions() const { return m_impl.dimensions(); }
107
108 EIGEN_STRONG_INLINE bool evalSubExprsIfNeeded(EvaluatorPointerType scalar) {
109 EIGEN_UNUSED_VARIABLE(scalar);
110 eigen_assert(scalar == nullptr);
111 return m_impl.evalSubExprsIfNeeded(m_buffer);
112 }
113
114#ifdef EIGEN_USE_THREADS
115 template <typename EvalSubExprsCallback>
116 EIGEN_STRONG_INLINE void evalSubExprsIfNeededAsync(EvaluatorPointerType scalar, EvalSubExprsCallback done) {
117 EIGEN_UNUSED_VARIABLE(scalar);
118 eigen_assert(scalar == nullptr);
119 m_impl.evalSubExprsIfNeededAsync(m_buffer, std::move(done));
120 }
121#endif
122
123 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void evalScalar(Index i) const { m_buffer[i] = m_impl.coeff(i); }
124 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void evalPacket(Index i) const {
125 internal::pstoret<CoeffReturnType, PacketReturnType, Aligned>(
126 m_buffer + i, m_impl.template packet < TensorEvaluator<ArgType, Device>::IsAligned ? Aligned : Unaligned > (i));
127 }
128
129 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE internal::TensorBlockResourceRequirements getResourceRequirements() const {
130 return m_impl.getResourceRequirements();
131 }
132
133 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void evalBlock(TensorBlockDesc& desc, TensorBlockScratch& scratch) {
134 // Add `m_buffer` as destination buffer to the block descriptor.
135 desc.template AddDestinationBuffer<Layout>(
136 /*dst_base=*/m_buffer + desc.offset(),
137 /*dst_strides=*/internal::strides<Layout>(m_impl.dimensions()));
138
139 ArgTensorBlock block = m_impl.block(desc, scratch, /*root_of_expr_ast=*/true);
140
141 // If block was evaluated into a destination buffer, there is no need to do
142 // an assignment.
143 if (block.kind() != internal::TensorBlockKind::kMaterializedInOutput) {
144 TensorBlockAssignment::Run(
145 TensorBlockAssignment::target(desc.dimensions(), internal::strides<Layout>(m_impl.dimensions()), m_buffer,
146 desc.offset()),
147 block.expr());
148 }
149 block.cleanup();
150 }
151
152 EIGEN_STRONG_INLINE void cleanup() { m_impl.cleanup(); }
153
154 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE CoeffReturnType coeff(Index index) const { return m_buffer[index]; }
155
156 template <int LoadMode>
157 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE PacketReturnType packet(Index index) const {
158 return internal::ploadt<PacketReturnType, LoadMode>(m_buffer + index);
159 }
160
161 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorOpCost costPerCoeff(bool vectorized) const {
162 // We assume that evalPacket or evalScalar is called to perform the
163 // assignment and account for the cost of the write here.
164 return m_impl.costPerCoeff(vectorized) + TensorOpCost(0, sizeof(CoeffReturnType), 0, vectorized, PacketSize);
165 }
166
167 EIGEN_DEVICE_FUNC EvaluatorPointerType data() const { return m_buffer; }
168 ArgType expression() const { return m_expression; }
169
170 private:
171 TensorEvaluator<ArgType, Device> m_impl;
172 EvaluatorPointerType m_buffer;
173 const ArgType m_expression;
174};
175
176} // end namespace Eigen
177
178#endif // EIGEN_TENSOR_TENSOR_EVAL_TO_H
The tensor base class.
Definition TensorForwardDeclarations.h:69
Namespace containing all symbols from the Eigen library.
The tensor evaluator class.
Definition TensorEvaluator.h:47