11#ifndef EIGEN_TENSOR_TENSOR_FORCED_EVAL_H
12#define EIGEN_TENSOR_TENSOR_FORCED_EVAL_H
15#include "./InternalHeaderCheck.h"
22template <
typename XprType>
23struct traits<TensorForcedEvalOp<XprType>> {
24 using Scalar =
typename XprType::Scalar;
25 using XprTraits = traits<XprType>;
26 using StorageKind =
typename traits<XprType>::StorageKind;
27 using Index =
typename traits<XprType>::Index;
28 static constexpr int NumDimensions = XprTraits::NumDimensions;
29 static constexpr int Layout = XprTraits::Layout;
30 using PointerType =
typename XprTraits::PointerType;
35template <
typename XprType>
36struct eval<TensorForcedEvalOp<XprType>, Eigen::Dense> {
37 using type =
const TensorForcedEvalOp<XprType>&;
47template <
typename XprType>
48class TensorForcedEvalOp :
public TensorBase<TensorForcedEvalOp<XprType>, ReadOnlyAccessors> {
50 using Scalar =
typename Eigen::internal::traits<TensorForcedEvalOp>::Scalar;
52 using CoeffReturnType = std::remove_const_t<typename XprType::CoeffReturnType>;
53 using Nested =
typename Eigen::internal::ref_selector<TensorForcedEvalOp>::type;
54 using StorageKind =
typename Eigen::internal::traits<TensorForcedEvalOp>::StorageKind;
55 using Index =
typename Eigen::internal::traits<TensorForcedEvalOp>::Index;
57 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorForcedEvalOp(
const XprType& expr) : m_xpr(expr) {}
59 EIGEN_DEVICE_FUNC
const internal::remove_all_t<typename XprType::Nested>& expression()
const {
return m_xpr; }
62 typename XprType::Nested m_xpr;
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);
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 {
92template <
typename Device>
93class DeviceTempPointerHolder {
95 DeviceTempPointerHolder(
const Device& device,
size_t size)
96 : device_(device), size_(size), ptr_(device.allocate_temp(size)) {}
98 ~DeviceTempPointerHolder() {
99 device_.deallocate_temp(ptr_);
104 void* ptr() {
return ptr_; }
112template <
typename ArgType_,
typename Device>
114 using ArgType =
const internal::remove_all_t<ArgType_>;
115 using XprType = TensorForcedEvalOp<ArgType>;
116 using Scalar =
typename ArgType::Scalar;
117 using Dimensions =
typename TensorEvaluator<ArgType, Device>::Dimensions;
118 using Index =
typename XprType::Index;
119 using CoeffReturnType =
typename XprType::CoeffReturnType;
120 using PacketReturnType =
typename PacketType<CoeffReturnType, Device>::type;
121 static constexpr int PacketSize = PacketType<CoeffReturnType, Device>::size;
122 using TensorPointerType =
typename Eigen::internal::traits<XprType>::PointerType;
123 using Storage = StorageMemory<CoeffReturnType, Device>;
124 using EvaluatorPointerType =
typename Storage::Type;
128 PacketAccess = (PacketType<CoeffReturnType, Device>::size > 1),
129 BlockAccess = internal::is_arithmetic<CoeffReturnType>::value,
130 PreferBlockAccess =
false,
134 static constexpr int Layout = TensorEvaluator<ArgType, Device>::Layout;
135 static constexpr int NumDims = internal::traits<ArgType>::NumDimensions;
138 using TensorBlockDesc = internal::TensorBlockDescriptor<NumDims, Index>;
139 using TensorBlockScratch = internal::TensorBlockScratchAllocator<Device>;
141 using TensorBlock =
typename internal::TensorMaterializedBlock<CoeffReturnType, NumDims, Layout, Index>;
144 TensorEvaluator(
const XprType& op,
const Device& device)
145 : m_impl(op.expression(), device),
146 m_op(op.expression()),
148 m_buffer_holder(nullptr),
150 m_placement_constructed(false) {}
152 ~TensorEvaluator() { cleanup(); }
154 EIGEN_DEVICE_FUNC
const Dimensions& dimensions()
const {
return m_impl.dimensions(); }
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());
161 m_placement_constructed = internal::non_integral_type_placement_new<Device, CoeffReturnType>()(numValues, m_buffer);
163 using EvalTo = TensorEvalToOp<const std::remove_const_t<ArgType>>;
164 EvalTo evalToTmp(m_device.get(m_buffer), m_op);
166 internal::TensorExecutor<const EvalTo, std::remove_const_t<Device>,
167 internal::IsVectorizable<Device, const ArgType>::value,
168 internal::IsTileable<Device, const ArgType>::value>::run(evalToTmp, m_device);
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());
180 m_placement_constructed = internal::non_integral_type_placement_new<Device, CoeffReturnType>()(numValues, m_buffer);
182 using EvalTo = TensorEvalToOp<const std::remove_const_t<ArgType>>;
183 EvalTo evalToTmp(m_device.get(m_buffer), m_op);
185 auto on_done = std::bind([](EvalSubExprsCallback done_) { done_(
true); }, std::move(done));
186 internal::TensorAsyncExecutor<
187 const EvalTo, std::remove_const_t<Device>,
decltype(on_done),
188 internal::IsVectorizable<Device, const ArgType>::value,
189 internal::IsTileable<Device, const ArgType>::value>::runAsync(evalToTmp, m_device,
194 EIGEN_STRONG_INLINE
void cleanup() {
196 if (m_placement_constructed && m_buffer_holder.use_count() == 1) {
197 internal::destruct_elements_of_array(m_buffer, internal::array_prod(m_impl.dimensions()));
198 m_placement_constructed =
false;
200 m_buffer_holder =
nullptr;
204 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE CoeffReturnType coeff(Index index)
const {
return m_buffer[index]; }
206 template <
int LoadMode>
207 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE PacketReturnType packet(Index index)
const {
208 return internal::ploadt<PacketReturnType, LoadMode>(m_buffer + index);
211 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE internal::TensorBlockResourceRequirements getResourceRequirements()
const {
212 return internal::TensorBlockResourceRequirements::any();
215 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorBlock block(TensorBlockDesc& desc, TensorBlockScratch& scratch,
216 bool =
false)
const {
217 eigen_assert(m_buffer !=
nullptr);
218 return TensorBlock::materialize(m_buffer, m_impl.dimensions(), desc, scratch);
221 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorOpCost costPerCoeff(
bool vectorized)
const {
222 return TensorOpCost(
sizeof(CoeffReturnType), 0, 0, vectorized, PacketSize);
225 EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE EvaluatorPointerType data()
const {
return m_buffer; }
228 TensorEvaluator<ArgType, Device> m_impl;
230 const Device EIGEN_DEVICE_REF m_device;
231 std::shared_ptr<DeviceTempPointerHolder<Device>> m_buffer_holder;
232 EvaluatorPointerType m_buffer;
233 bool m_placement_constructed;
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