Eigen-unsupported  3.4.1 (git rev 28ded8800c26864e537852658428ab44c8399e87)
 
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
10#ifndef EIGEN_CXX11_TENSOR_TENSOR_EVAL_TO_H
11#define EIGEN_CXX11_TENSOR_TENSOR_EVAL_TO_H
12
13namespace Eigen {
14
15namespace internal {
16template<typename XprType, template <class> class MakePointer_>
17struct traits<TensorEvalToOp<XprType, MakePointer_> >
18{
19 // Type promotion to handle the case where the types of the lhs and the rhs are different.
20 typedef typename XprType::Scalar Scalar;
21 typedef traits<XprType> XprTraits;
22 typedef typename XprTraits::StorageKind StorageKind;
23 typedef typename XprTraits::Index Index;
24 typedef typename XprType::Nested Nested;
25 typedef typename remove_reference<Nested>::type _Nested;
26 static const int NumDimensions = XprTraits::NumDimensions;
27 static const int Layout = XprTraits::Layout;
28 typedef typename MakePointer_<Scalar>::Type PointerType;
29
30 enum {
31 Flags = 0
32 };
33 template <class T>
34 struct MakePointer {
35 // Intermediate typedef to workaround MSVC issue.
36 typedef MakePointer_<T> MakePointerT;
37 typedef typename MakePointerT::Type Type;
38
39
40 };
41};
42
43template<typename XprType, template <class> class MakePointer_>
44struct eval<TensorEvalToOp<XprType, MakePointer_>, Eigen::Dense>
45{
46 typedef const TensorEvalToOp<XprType, MakePointer_>& type;
47};
48
49template<typename XprType, template <class> class MakePointer_>
50struct nested<TensorEvalToOp<XprType, MakePointer_>, 1, typename eval<TensorEvalToOp<XprType, MakePointer_> >::type>
51{
52 typedef TensorEvalToOp<XprType, MakePointer_> type;
53};
54
55} // end namespace internal
56
57
58
59
60template<typename XprType, template <class> class MakePointer_>
61class TensorEvalToOp : public TensorBase<TensorEvalToOp<XprType, MakePointer_>, ReadOnlyAccessors>
62{
63 public:
64 typedef typename Eigen::internal::traits<TensorEvalToOp>::Scalar Scalar;
65 typedef typename Eigen::NumTraits<Scalar>::Real RealScalar;
66 typedef typename internal::remove_const<typename XprType::CoeffReturnType>::type CoeffReturnType;
67 typedef typename MakePointer_<CoeffReturnType>::Type PointerType;
68 typedef typename Eigen::internal::nested<TensorEvalToOp>::type Nested;
69 typedef typename Eigen::internal::traits<TensorEvalToOp>::StorageKind StorageKind;
70 typedef typename Eigen::internal::traits<TensorEvalToOp>::Index Index;
71
72 static const int NumDims = Eigen::internal::traits<TensorEvalToOp>::NumDimensions;
73
74 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorEvalToOp(PointerType buffer, const XprType& expr)
75 : m_xpr(expr), m_buffer(buffer) {}
76
77 EIGEN_DEVICE_FUNC
78 const typename internal::remove_all<typename XprType::Nested>::type&
79 expression() const { return m_xpr; }
80
81 EIGEN_DEVICE_FUNC PointerType buffer() const { return m_buffer; }
82
83 protected:
84 typename XprType::Nested m_xpr;
85 PointerType m_buffer;
86};
87
88
89
90template<typename ArgType, typename Device, template <class> class MakePointer_>
91struct TensorEvaluator<const TensorEvalToOp<ArgType, MakePointer_>, Device>
92{
93 typedef TensorEvalToOp<ArgType, MakePointer_> XprType;
94 typedef typename ArgType::Scalar Scalar;
95 typedef typename TensorEvaluator<ArgType, Device>::Dimensions Dimensions;
96 typedef typename XprType::Index Index;
97 typedef typename internal::remove_const<typename XprType::CoeffReturnType>::type CoeffReturnType;
98 typedef typename PacketType<CoeffReturnType, Device>::type PacketReturnType;
99 static const int PacketSize = PacketType<CoeffReturnType, Device>::size;
100 typedef typename Eigen::internal::traits<XprType>::PointerType TensorPointerType;
101 typedef StorageMemory<CoeffReturnType, Device> Storage;
102 typedef typename Storage::Type EvaluatorPointerType;
103 enum {
104 IsAligned = TensorEvaluator<ArgType, Device>::IsAligned,
105 PacketAccess = TensorEvaluator<ArgType, Device>::PacketAccess,
106 BlockAccess = true,
107 PreferBlockAccess = false,
108 Layout = TensorEvaluator<ArgType, Device>::Layout,
109 CoordAccess = false, // to be implemented
110 RawAccess = true
111 };
112
113 static const int NumDims = internal::traits<ArgType>::NumDimensions;
114
115 //===- Tensor block evaluation strategy (see TensorBlock.h) -------------===//
116 typedef internal::TensorBlockDescriptor<NumDims, Index> TensorBlockDesc;
117 typedef internal::TensorBlockScratchAllocator<Device> TensorBlockScratch;
118
119 typedef typename TensorEvaluator<const ArgType, Device>::TensorBlock
120 ArgTensorBlock;
121
122 typedef internal::TensorBlockAssignment<
123 CoeffReturnType, NumDims, typename ArgTensorBlock::XprType, Index>
124 TensorBlockAssignment;
125 //===--------------------------------------------------------------------===//
126
127 EIGEN_STRONG_INLINE TensorEvaluator(const XprType& op, const Device& device)
128 : m_impl(op.expression(), device), m_buffer(device.get(op.buffer())), m_expression(op.expression()){}
129
130
131 EIGEN_STRONG_INLINE ~TensorEvaluator() {
132 }
133
134
135 EIGEN_DEVICE_FUNC const Dimensions& dimensions() const { return m_impl.dimensions(); }
136
137 EIGEN_STRONG_INLINE bool evalSubExprsIfNeeded(EvaluatorPointerType scalar) {
138 EIGEN_UNUSED_VARIABLE(scalar);
139 eigen_assert(scalar == NULL);
140 return m_impl.evalSubExprsIfNeeded(m_buffer);
141 }
142
143#ifdef EIGEN_USE_THREADS
144 template <typename EvalSubExprsCallback>
145 EIGEN_STRONG_INLINE void evalSubExprsIfNeededAsync(
146 EvaluatorPointerType scalar, EvalSubExprsCallback done) {
147 EIGEN_UNUSED_VARIABLE(scalar);
148 eigen_assert(scalar == NULL);
149 m_impl.evalSubExprsIfNeededAsync(m_buffer, std::move(done));
150 }
151#endif
152
153 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void evalScalar(Index i) {
154 m_buffer[i] = m_impl.coeff(i);
155 }
156 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void evalPacket(Index i) {
157 internal::pstoret<CoeffReturnType, PacketReturnType, Aligned>(m_buffer + i, m_impl.template packet<TensorEvaluator<ArgType, Device>::IsAligned ? Aligned : Unaligned>(i));
158 }
159
160 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
161 internal::TensorBlockResourceRequirements getResourceRequirements() const {
162 return m_impl.getResourceRequirements();
163 }
164
165 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void evalBlock(
166 TensorBlockDesc& desc, TensorBlockScratch& scratch) {
167 // Add `m_buffer` as destination buffer to the block descriptor.
168 desc.template AddDestinationBuffer<Layout>(
169 /*dst_base=*/m_buffer + desc.offset(),
170 /*dst_strides=*/internal::strides<Layout>(m_impl.dimensions()));
171
172 ArgTensorBlock block =
173 m_impl.block(desc, scratch, /*root_of_expr_ast=*/true);
174
175 // If block was evaluated into a destination buffer, there is no need to do
176 // an assignment.
177 if (block.kind() != internal::TensorBlockKind::kMaterializedInOutput) {
178 TensorBlockAssignment::Run(
179 TensorBlockAssignment::target(
180 desc.dimensions(), internal::strides<Layout>(m_impl.dimensions()),
181 m_buffer, desc.offset()),
182 block.expr());
183 }
184 block.cleanup();
185 }
186
187 EIGEN_STRONG_INLINE void cleanup() {
188 m_impl.cleanup();
189 }
190
191 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE CoeffReturnType coeff(Index index) const
192 {
193 return m_buffer[index];
194 }
195
196 template<int LoadMode>
197 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE PacketReturnType packet(Index index) const
198 {
199 return internal::ploadt<PacketReturnType, LoadMode>(m_buffer + index);
200 }
201
202 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorOpCost costPerCoeff(bool vectorized) const {
203 // We assume that evalPacket or evalScalar is called to perform the
204 // assignment and account for the cost of the write here.
205 return m_impl.costPerCoeff(vectorized) +
206 TensorOpCost(0, sizeof(CoeffReturnType), 0, vectorized, PacketSize);
207 }
208
209 EIGEN_DEVICE_FUNC EvaluatorPointerType data() const { return m_buffer; }
210 ArgType expression() const { return m_expression; }
211 #ifdef EIGEN_USE_SYCL
212 // binding placeholder accessors to a command group handler for SYCL
213 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void bind(cl::sycl::handler &cgh) const {
214 m_impl.bind(cgh);
215 m_buffer.bind(cgh);
216 }
217 #endif
218
219
220 private:
221 TensorEvaluator<ArgType, Device> m_impl;
222 EvaluatorPointerType m_buffer;
223 const ArgType m_expression;
224};
225
226
227} // end namespace Eigen
228
229#endif // EIGEN_CXX11_TENSOR_TENSOR_EVAL_TO_H
The tensor base class.
Definition TensorForwardDeclarations.h:56
Namespace containing all symbols from the Eigen library.
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The tensor evaluator class.
Definition TensorEvaluator.h:27