Eigen-unsupported  5.0.1-dev+284dcc12
 
Loading...
Searching...
No Matches
TensorForcedEval.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_FORCED_EVAL_H
11#define EIGEN_CXX11_TENSOR_TENSOR_FORCED_EVAL_H
12
13// IWYU pragma: private
14#include "./InternalHeaderCheck.h"
15
16#include <memory>
17
18namespace Eigen {
19
20namespace internal {
21template <typename XprType>
22struct traits<TensorForcedEvalOp<XprType>> {
23 // Type promotion to handle the case where the types of the lhs and the rhs are different.
24 typedef typename XprType::Scalar Scalar;
25 typedef traits<XprType> XprTraits;
26 typedef typename traits<XprType>::StorageKind StorageKind;
27 typedef typename traits<XprType>::Index Index;
28 typedef typename XprType::Nested Nested;
29 typedef std::remove_reference_t<Nested> Nested_;
30 static constexpr int NumDimensions = XprTraits::NumDimensions;
31 static constexpr int Layout = XprTraits::Layout;
32 typedef typename XprTraits::PointerType PointerType;
33
34 enum { Flags = 0 };
35};
36
37template <typename XprType>
38struct eval<TensorForcedEvalOp<XprType>, Eigen::Dense> {
39 typedef const TensorForcedEvalOp<XprType>& type;
40};
41
42template <typename XprType>
43struct nested<TensorForcedEvalOp<XprType>, 1, typename eval<TensorForcedEvalOp<XprType>>::type> {
44 typedef TensorForcedEvalOp<XprType> type;
45};
46
47} // end namespace internal
48
54template <typename XprType>
55class TensorForcedEvalOp : public TensorBase<TensorForcedEvalOp<XprType>, ReadOnlyAccessors> {
56 public:
57 typedef typename Eigen::internal::traits<TensorForcedEvalOp>::Scalar Scalar;
58 typedef typename Eigen::NumTraits<Scalar>::Real RealScalar;
59 typedef std::remove_const_t<typename XprType::CoeffReturnType> CoeffReturnType;
60 typedef typename Eigen::internal::nested<TensorForcedEvalOp>::type Nested;
61 typedef typename Eigen::internal::traits<TensorForcedEvalOp>::StorageKind StorageKind;
62 typedef typename Eigen::internal::traits<TensorForcedEvalOp>::Index Index;
63
64 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorForcedEvalOp(const XprType& expr) : m_xpr(expr) {}
65
66 EIGEN_DEVICE_FUNC const internal::remove_all_t<typename XprType::Nested>& expression() const { return m_xpr; }
67
68 protected:
69 typename XprType::Nested m_xpr;
70};
71
72namespace internal {
73template <typename Device, typename CoeffReturnType>
74struct non_integral_type_placement_new {
75 template <typename StorageType>
76 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void operator()(Index numValues, StorageType m_buffer) {
77 // Initialize non-trivially constructible types.
78 if (!internal::is_arithmetic<CoeffReturnType>::value) {
79 for (Index i = 0; i < numValues; ++i) new (m_buffer + i) CoeffReturnType();
80 }
81 }
82};
83
84// SYCL does not support non-integral types
85// having new (m_buffer + i) CoeffReturnType() causes the following compiler error for SYCL Devices
86// no matching function for call to 'operator new'
87template <typename CoeffReturnType>
88struct non_integral_type_placement_new<Eigen::SyclDevice, CoeffReturnType> {
89 template <typename StorageType>
90 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void operator()(Index, StorageType) {}
91};
92} // end namespace internal
93
94template <typename Device>
95class DeviceTempPointerHolder {
96 public:
97 DeviceTempPointerHolder(const Device& device, size_t size)
98 : device_(device), size_(size), ptr_(device.allocate_temp(size)) {}
99
100 ~DeviceTempPointerHolder() {
101 device_.deallocate_temp(ptr_);
102 size_ = 0;
103 ptr_ = nullptr;
104 }
105
106 void* ptr() { return ptr_; }
107
108 private:
109 Device device_;
110 size_t size_;
111 void* ptr_;
112};
113
114template <typename ArgType_, typename Device>
115struct TensorEvaluator<const TensorForcedEvalOp<ArgType_>, Device> {
116 typedef const internal::remove_all_t<ArgType_> ArgType;
117 typedef TensorForcedEvalOp<ArgType> XprType;
118 typedef typename ArgType::Scalar Scalar;
119 typedef typename TensorEvaluator<ArgType, Device>::Dimensions Dimensions;
120 typedef typename XprType::Index Index;
121 typedef typename XprType::CoeffReturnType CoeffReturnType;
122 typedef typename PacketType<CoeffReturnType, Device>::type PacketReturnType;
123 static constexpr int PacketSize = PacketType<CoeffReturnType, Device>::size;
124 typedef typename Eigen::internal::traits<XprType>::PointerType TensorPointerType;
125 typedef StorageMemory<CoeffReturnType, Device> Storage;
126 typedef typename Storage::Type EvaluatorPointerType;
127
128 enum {
129 IsAligned = true,
130 PacketAccess = (PacketType<CoeffReturnType, Device>::size > 1),
131 BlockAccess = internal::is_arithmetic<CoeffReturnType>::value,
132 PreferBlockAccess = false,
133 RawAccess = true
134 };
135
136 static constexpr int Layout = TensorEvaluator<ArgType, Device>::Layout;
137 static constexpr int NumDims = internal::traits<ArgType>::NumDimensions;
138
139 //===- Tensor block evaluation strategy (see TensorBlock.h) -------------===//
140 typedef internal::TensorBlockDescriptor<NumDims, Index> TensorBlockDesc;
141 typedef internal::TensorBlockScratchAllocator<Device> TensorBlockScratch;
142
143 typedef typename internal::TensorMaterializedBlock<CoeffReturnType, NumDims, Layout, Index> TensorBlock;
144 //===--------------------------------------------------------------------===//
145
146 TensorEvaluator(const XprType& op, const Device& device)
147 : m_impl(op.expression(), device),
148 m_op(op.expression()),
149 m_device(device),
150 m_buffer_holder(nullptr),
151 m_buffer(nullptr) {}
152
153 ~TensorEvaluator() { cleanup(); }
154
155 EIGEN_DEVICE_FUNC const Dimensions& dimensions() const { return m_impl.dimensions(); }
156
157 EIGEN_STRONG_INLINE bool evalSubExprsIfNeeded(EvaluatorPointerType) {
158 const Index numValues = internal::array_prod(m_impl.dimensions());
159 m_buffer_holder = std::make_shared<DeviceTempPointerHolder<Device>>(m_device, numValues * sizeof(CoeffReturnType));
160 m_buffer = static_cast<EvaluatorPointerType>(m_buffer_holder->ptr());
161
162 internal::non_integral_type_placement_new<Device, CoeffReturnType>()(numValues, m_buffer);
163
164 typedef TensorEvalToOp<const std::remove_const_t<ArgType>> EvalTo;
165 EvalTo evalToTmp(m_device.get(m_buffer), m_op);
166
167 internal::TensorExecutor<const EvalTo, std::remove_const_t<Device>,
168 /*Vectorizable=*/internal::IsVectorizable<Device, const ArgType>::value,
169 /*Tiling=*/internal::IsTileable<Device, const ArgType>::value>::run(evalToTmp, m_device);
170
171 return true;
172 }
173
174#ifdef EIGEN_USE_THREADS
175 template <typename EvalSubExprsCallback>
176 EIGEN_STRONG_INLINE void evalSubExprsIfNeededAsync(EvaluatorPointerType, EvalSubExprsCallback done) {
177 const Index numValues = internal::array_prod(m_impl.dimensions());
178 m_buffer_holder = std::make_shared<DeviceTempPointerHolder<Device>>(m_device, numValues * sizeof(CoeffReturnType));
179 m_buffer = static_cast<EvaluatorPointerType>(m_buffer_holder->ptr());
180
181 typedef TensorEvalToOp<const std::remove_const_t<ArgType>> EvalTo;
182 EvalTo evalToTmp(m_device.get(m_buffer), m_op);
183
184 auto on_done = std::bind([](EvalSubExprsCallback done_) { done_(true); }, std::move(done));
185 internal::TensorAsyncExecutor<
186 const EvalTo, std::remove_const_t<Device>, decltype(on_done),
187 /*Vectorizable=*/internal::IsVectorizable<Device, const ArgType>::value,
188 /*Tiling=*/internal::IsTileable<Device, const ArgType>::value>::runAsync(evalToTmp, m_device,
189 std::move(on_done));
190 }
191#endif
192
193 EIGEN_STRONG_INLINE void cleanup() {
194 m_buffer_holder = nullptr;
195 m_buffer = nullptr;
196 }
197
198 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE CoeffReturnType coeff(Index index) const { return m_buffer[index]; }
199
200 template <int LoadMode>
201 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE PacketReturnType packet(Index index) const {
202 return internal::ploadt<PacketReturnType, LoadMode>(m_buffer + index);
203 }
204
205 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE internal::TensorBlockResourceRequirements getResourceRequirements() const {
206 return internal::TensorBlockResourceRequirements::any();
207 }
208
209 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorBlock block(TensorBlockDesc& desc, TensorBlockScratch& scratch,
210 bool /*root_of_expr_ast*/ = false) const {
211 eigen_assert(m_buffer != nullptr);
212 return TensorBlock::materialize(m_buffer, m_impl.dimensions(), desc, scratch);
213 }
214
215 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorOpCost costPerCoeff(bool vectorized) const {
216 return TensorOpCost(sizeof(CoeffReturnType), 0, 0, vectorized, PacketSize);
217 }
218
219 EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE EvaluatorPointerType data() const { return m_buffer; }
220
221 private:
222 TensorEvaluator<ArgType, Device> m_impl;
223 const ArgType m_op;
224 const Device EIGEN_DEVICE_REF m_device;
225 std::shared_ptr<DeviceTempPointerHolder<Device>> m_buffer_holder;
226 EvaluatorPointerType m_buffer; // Cached copy of the value stored in m_buffer_holder.
227};
228
229} // end namespace Eigen
230
231#endif // EIGEN_CXX11_TENSOR_TENSOR_FORCED_EVAL_H
The tensor base class.
Definition TensorForwardDeclarations.h:68
Tensor reshaping class.
Definition TensorForcedEval.h:55
Namespace containing all symbols from the Eigen library.
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The tensor evaluator class.
Definition TensorEvaluator.h:30