Eigen-unsupported  3.4.1 (git rev 28ded8800c26864e537852658428ab44c8399e87)
 
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
13namespace Eigen {
14
15namespace internal {
16template<typename XprType>
17struct traits<TensorForcedEvalOp<XprType> >
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 traits<XprType>::StorageKind StorageKind;
23 typedef typename traits<XprType>::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 XprTraits::PointerType PointerType;
29
30 enum {
31 Flags = 0
32 };
33};
34
35template<typename XprType>
36struct eval<TensorForcedEvalOp<XprType>, Eigen::Dense>
37{
38 typedef const TensorForcedEvalOp<XprType>& type;
39};
40
41template<typename XprType>
42struct nested<TensorForcedEvalOp<XprType>, 1, typename eval<TensorForcedEvalOp<XprType> >::type>
43{
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 typename internal::remove_const<typename XprType::CoeffReturnType>::type 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)
65 : m_xpr(expr) {}
66
67 EIGEN_DEVICE_FUNC
68 const typename internal::remove_all<typename XprType::Nested>::type&
69 expression() const { return m_xpr; }
70
71 protected:
72 typename XprType::Nested m_xpr;
73};
74
75namespace internal {
76template <typename Device, typename CoeffReturnType>
77struct non_integral_type_placement_new{
78 template <typename StorageType>
79EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void operator()(Index numValues, StorageType m_buffer) {
80 // Initialize non-trivially constructible types.
81 if (!internal::is_arithmetic<CoeffReturnType>::value) {
82 for (Index i = 0; i < numValues; ++i) new (m_buffer + i) CoeffReturnType();
83 }
84}
85};
86
87// SYCL does not support non-integral types
88// having new (m_buffer + i) CoeffReturnType() causes the following compiler error for SYCL Devices
89// no matching function for call to 'operator new'
90template <typename CoeffReturnType>
91struct non_integral_type_placement_new<Eigen::SyclDevice, CoeffReturnType> {
92 template <typename StorageType>
93EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void operator()(Index, StorageType) {
94}
95};
96} // end namespace internal
97
98template<typename ArgType_, typename Device>
99struct TensorEvaluator<const TensorForcedEvalOp<ArgType_>, Device>
100{
101 typedef const typename internal::remove_all<ArgType_>::type ArgType;
102 typedef TensorForcedEvalOp<ArgType> XprType;
103 typedef typename ArgType::Scalar Scalar;
104 typedef typename TensorEvaluator<ArgType, Device>::Dimensions Dimensions;
105 typedef typename XprType::Index Index;
106 typedef typename XprType::CoeffReturnType CoeffReturnType;
107 typedef typename PacketType<CoeffReturnType, Device>::type PacketReturnType;
108 static const int PacketSize = PacketType<CoeffReturnType, Device>::size;
109 typedef typename Eigen::internal::traits<XprType>::PointerType TensorPointerType;
110 typedef StorageMemory<CoeffReturnType, Device> Storage;
111 typedef typename Storage::Type EvaluatorPointerType;
112
113 enum {
114 IsAligned = true,
115 PacketAccess = (PacketType<CoeffReturnType, Device>::size > 1),
116 BlockAccess = internal::is_arithmetic<CoeffReturnType>::value,
117 PreferBlockAccess = false,
118 Layout = TensorEvaluator<ArgType, Device>::Layout,
119 RawAccess = true
120 };
121
122 static const int NumDims = internal::traits<ArgType>::NumDimensions;
123
124 //===- Tensor block evaluation strategy (see TensorBlock.h) -------------===//
125 typedef internal::TensorBlockDescriptor<NumDims, Index> TensorBlockDesc;
126 typedef internal::TensorBlockScratchAllocator<Device> TensorBlockScratch;
127
128 typedef typename internal::TensorMaterializedBlock<CoeffReturnType, NumDims,
129 Layout, Index>
130 TensorBlock;
131 //===--------------------------------------------------------------------===//
132
133 TensorEvaluator(const XprType& op, const Device& device)
134 : m_impl(op.expression(), device), m_op(op.expression()),
135 m_device(device), m_buffer(NULL)
136 { }
137
138 EIGEN_DEVICE_FUNC const Dimensions& dimensions() const { return m_impl.dimensions(); }
139
140 EIGEN_STRONG_INLINE bool evalSubExprsIfNeeded(EvaluatorPointerType) {
141 const Index numValues = internal::array_prod(m_impl.dimensions());
142 m_buffer = m_device.get((CoeffReturnType*)m_device.allocate_temp(numValues * sizeof(CoeffReturnType)));
143
144 internal::non_integral_type_placement_new<Device, CoeffReturnType>()(numValues, m_buffer);
145
146 typedef TensorEvalToOp< const typename internal::remove_const<ArgType>::type > EvalTo;
147 EvalTo evalToTmp(m_device.get(m_buffer), m_op);
148
149 internal::TensorExecutor<
150 const EvalTo, typename internal::remove_const<Device>::type,
151 /*Vectorizable=*/internal::IsVectorizable<Device, const ArgType>::value,
152 /*Tiling=*/internal::IsTileable<Device, const ArgType>::value>::
153 run(evalToTmp, m_device);
154
155 return true;
156 }
157
158#ifdef EIGEN_USE_THREADS
159 template <typename EvalSubExprsCallback>
160 EIGEN_STRONG_INLINE void evalSubExprsIfNeededAsync(
161 EvaluatorPointerType, EvalSubExprsCallback done) {
162 const Index numValues = internal::array_prod(m_impl.dimensions());
163 m_buffer = m_device.get((CoeffReturnType*)m_device.allocate_temp(
164 numValues * sizeof(CoeffReturnType)));
165 typedef TensorEvalToOp<const typename internal::remove_const<ArgType>::type>
166 EvalTo;
167 EvalTo evalToTmp(m_device.get(m_buffer), m_op);
168
169 auto on_done = std::bind([](EvalSubExprsCallback done_) { done_(true); },
170 std::move(done));
171 internal::TensorAsyncExecutor<
172 const EvalTo, typename internal::remove_const<Device>::type,
173 decltype(on_done),
174 /*Vectorizable=*/internal::IsVectorizable<Device, const ArgType>::value,
175 /*Tiling=*/internal::IsTileable<Device, const ArgType>::value>::
176 runAsync(evalToTmp, m_device, std::move(on_done));
177 }
178#endif
179
180 EIGEN_STRONG_INLINE void cleanup() {
181 m_device.deallocate_temp(m_buffer);
182 m_buffer = NULL;
183 }
184
185 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE CoeffReturnType coeff(Index index) const
186 {
187 return m_buffer[index];
188 }
189
190 template<int LoadMode>
191 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE PacketReturnType packet(Index index) const
192 {
193 return internal::ploadt<PacketReturnType, LoadMode>(m_buffer + index);
194 }
195
196 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
197 internal::TensorBlockResourceRequirements getResourceRequirements() const {
198 return internal::TensorBlockResourceRequirements::any();
199 }
200
201 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorBlock
202 block(TensorBlockDesc& desc, TensorBlockScratch& scratch,
203 bool /*root_of_expr_ast*/ = false) const {
204 assert(m_buffer != NULL);
205 return TensorBlock::materialize(m_buffer, m_impl.dimensions(), desc, scratch);
206 }
207
208 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorOpCost costPerCoeff(bool vectorized) const {
209 return TensorOpCost(sizeof(CoeffReturnType), 0, 0, vectorized, PacketSize);
210 }
211
212 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
213 EvaluatorPointerType data() const { return m_buffer; }
214
215#ifdef EIGEN_USE_SYCL
216 // binding placeholder accessors to a command group handler for SYCL
217 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void bind(cl::sycl::handler &cgh) const {
218 m_buffer.bind(cgh);
219 m_impl.bind(cgh);
220 }
221#endif
222 private:
223 TensorEvaluator<ArgType, Device> m_impl;
224 const ArgType m_op;
225 const Device EIGEN_DEVICE_REF m_device;
226 EvaluatorPointerType m_buffer;
227};
228
229
230} // end namespace Eigen
231
232#endif // EIGEN_CXX11_TENSOR_TENSOR_FORCED_EVAL_H
The tensor base class.
Definition TensorForwardDeclarations.h:56
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:27