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