10 #ifndef EIGEN_CXX11_TENSOR_TENSOR_CUSTOM_OP_H
11 #define EIGEN_CXX11_TENSOR_TENSOR_CUSTOM_OP_H
13 #include "./InternalHeaderCheck.h"
25 template<
typename CustomUnaryFunc,
typename XprType>
26 struct traits<TensorCustomUnaryOp<CustomUnaryFunc, XprType> >
28 typedef typename XprType::Scalar Scalar;
29 typedef typename XprType::StorageKind StorageKind;
30 typedef typename XprType::Index
Index;
31 typedef typename XprType::Nested Nested;
32 typedef std::remove_reference_t<Nested> Nested_;
33 static constexpr
int NumDimensions = traits<XprType>::NumDimensions;
34 static constexpr
int Layout = traits<XprType>::Layout;
35 typedef typename traits<XprType>::PointerType PointerType;
38 template<
typename CustomUnaryFunc,
typename XprType>
39 struct eval<TensorCustomUnaryOp<CustomUnaryFunc, XprType>,
Eigen::Dense>
41 typedef const TensorCustomUnaryOp<CustomUnaryFunc, XprType>EIGEN_DEVICE_REF type;
44 template<
typename CustomUnaryFunc,
typename XprType>
45 struct nested<TensorCustomUnaryOp<CustomUnaryFunc, XprType> >
47 typedef TensorCustomUnaryOp<CustomUnaryFunc, XprType> type;
54 template<
typename CustomUnaryFunc,
typename XprType>
58 typedef typename internal::traits<TensorCustomUnaryOp>::Scalar Scalar;
60 typedef typename XprType::CoeffReturnType CoeffReturnType;
61 typedef typename internal::nested<TensorCustomUnaryOp>::type Nested;
62 typedef typename internal::traits<TensorCustomUnaryOp>::StorageKind StorageKind;
63 typedef typename internal::traits<TensorCustomUnaryOp>::Index Index;
65 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
TensorCustomUnaryOp(
const XprType& expr,
const CustomUnaryFunc& func)
66 : m_expr(expr), m_func(func) {}
69 const CustomUnaryFunc& func()
const {
return m_func; }
72 const internal::remove_all_t<typename XprType::Nested>&
73 expression()
const {
return m_expr; }
76 typename XprType::Nested m_expr;
77 const CustomUnaryFunc m_func;
82 template<
typename CustomUnaryFunc,
typename XprType,
typename Device>
86 typedef typename internal::traits<ArgType>::Index
Index;
87 static constexpr
int NumDims = internal::traits<ArgType>::NumDimensions;
88 typedef DSizes<Index, NumDims> Dimensions;
89 typedef std::remove_const_t<typename ArgType::Scalar> Scalar;
90 typedef std::remove_const_t<typename XprType::CoeffReturnType> CoeffReturnType;
91 typedef typename PacketType<CoeffReturnType, Device>::type PacketReturnType;
92 static constexpr
int PacketSize = PacketType<CoeffReturnType, Device>::size;
93 typedef typename Eigen::internal::traits<XprType>::PointerType TensorPointerType;
94 typedef StorageMemory<CoeffReturnType, Device> Storage;
95 typedef typename Storage::Type EvaluatorPointerType;
100 PacketAccess = (PacketType<CoeffReturnType, Device>::size > 1),
102 PreferBlockAccess =
false,
108 typedef internal::TensorBlockNotImplemented TensorBlock;
111 EIGEN_STRONG_INLINE TensorEvaluator(
const ArgType& op,
const Device& device)
112 : m_op(op), m_device(device), m_result(NULL)
114 m_dimensions = op.func().dimensions(op.expression());
117 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
const Dimensions& dimensions()
const {
return m_dimensions; }
119 EIGEN_STRONG_INLINE
bool evalSubExprsIfNeeded(EvaluatorPointerType data) {
124 m_result =
static_cast<EvaluatorPointerType
>(m_device.get( (CoeffReturnType*)
125 m_device.allocate_temp(dimensions().TotalSize() *
sizeof(Scalar))));
131 EIGEN_STRONG_INLINE
void cleanup() {
133 m_device.deallocate_temp(m_result);
138 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE CoeffReturnType coeff(Index index)
const {
139 return m_result[index];
142 template<
int LoadMode>
143 EIGEN_DEVICE_FUNC PacketReturnType packet(Index index)
const {
144 return internal::ploadt<PacketReturnType, LoadMode>(m_result + index);
147 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorOpCost costPerCoeff(
bool vectorized)
const {
149 return TensorOpCost(
sizeof(CoeffReturnType), 0, 0, vectorized, PacketSize);
152 EIGEN_DEVICE_FUNC EvaluatorPointerType data()
const {
return m_result; }
154 #ifdef EIGEN_USE_SYCL
156 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
void bind(cl::sycl::handler &cgh)
const {
162 void evalTo(EvaluatorPointerType data) {
163 TensorMap<Tensor<CoeffReturnType, NumDims, Layout, Index> > result(m_device.get(data), m_dimensions);
164 m_op.func().eval(m_op.expression(), result, m_device);
167 Dimensions m_dimensions;
169 const Device EIGEN_DEVICE_REF m_device;
170 EvaluatorPointerType m_result;
183 template<
typename CustomBinaryFunc,
typename LhsXprType,
typename RhsXprType>
184 struct traits<TensorCustomBinaryOp<CustomBinaryFunc, LhsXprType, RhsXprType> >
186 typedef typename internal::promote_storage_type<
typename LhsXprType::Scalar,
187 typename RhsXprType::Scalar>::ret Scalar;
188 typedef typename internal::promote_storage_type<
typename LhsXprType::CoeffReturnType,
189 typename RhsXprType::CoeffReturnType>::ret CoeffReturnType;
190 typedef typename promote_storage_type<typename traits<LhsXprType>::StorageKind,
191 typename traits<RhsXprType>::StorageKind>::ret StorageKind;
192 typedef typename promote_index_type<typename traits<LhsXprType>::Index,
193 typename traits<RhsXprType>::Index>::type
Index;
194 typedef typename LhsXprType::Nested LhsNested;
195 typedef typename RhsXprType::Nested RhsNested;
196 typedef std::remove_reference_t<LhsNested> LhsNested_;
197 typedef std::remove_reference_t<RhsNested> RhsNested_;
198 static constexpr
int NumDimensions = traits<LhsXprType>::NumDimensions;
199 static constexpr
int Layout = traits<LhsXprType>::Layout;
200 typedef std::conditional_t<Pointer_type_promotion<typename LhsXprType::Scalar, Scalar>::val,
201 typename traits<LhsXprType>::PointerType,
typename traits<RhsXprType>::PointerType> PointerType;
204 template<
typename CustomBinaryFunc,
typename LhsXprType,
typename RhsXprType>
205 struct eval<TensorCustomBinaryOp<CustomBinaryFunc, LhsXprType, RhsXprType>,
Eigen::Dense>
207 typedef const TensorCustomBinaryOp<CustomBinaryFunc, LhsXprType, RhsXprType>& type;
210 template<
typename CustomBinaryFunc,
typename LhsXprType,
typename RhsXprType>
211 struct nested<TensorCustomBinaryOp<CustomBinaryFunc, LhsXprType, RhsXprType> >
213 typedef TensorCustomBinaryOp<CustomBinaryFunc, LhsXprType, RhsXprType> type;
220 template<
typename CustomBinaryFunc,
typename LhsXprType,
typename RhsXprType>
224 typedef typename internal::traits<TensorCustomBinaryOp>::Scalar Scalar;
226 typedef typename internal::traits<TensorCustomBinaryOp>::CoeffReturnType CoeffReturnType;
227 typedef typename internal::nested<TensorCustomBinaryOp>::type Nested;
228 typedef typename internal::traits<TensorCustomBinaryOp>::StorageKind StorageKind;
229 typedef typename internal::traits<TensorCustomBinaryOp>::Index Index;
231 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
TensorCustomBinaryOp(
const LhsXprType& lhs,
const RhsXprType& rhs,
const CustomBinaryFunc& func)
233 : m_lhs_xpr(lhs), m_rhs_xpr(rhs), m_func(func) {}
236 const CustomBinaryFunc& func()
const {
return m_func; }
239 const internal::remove_all_t<typename LhsXprType::Nested>&
240 lhsExpression()
const {
return m_lhs_xpr; }
243 const internal::remove_all_t<typename RhsXprType::Nested>&
244 rhsExpression()
const {
return m_rhs_xpr; }
247 typename LhsXprType::Nested m_lhs_xpr;
248 typename RhsXprType::Nested m_rhs_xpr;
249 const CustomBinaryFunc m_func;
254 template<
typename CustomBinaryFunc,
typename LhsXprType,
typename RhsXprType,
typename Device>
258 typedef typename internal::traits<XprType>::Index
Index;
259 static constexpr
int NumDims = internal::traits<XprType>::NumDimensions;
260 typedef DSizes<Index, NumDims> Dimensions;
261 typedef typename XprType::Scalar Scalar;
262 typedef std::remove_const_t<typename XprType::CoeffReturnType> CoeffReturnType;
263 typedef typename PacketType<CoeffReturnType, Device>::type PacketReturnType;
264 static constexpr
int PacketSize = PacketType<CoeffReturnType, Device>::size;
266 typedef typename Eigen::internal::traits<XprType>::PointerType TensorPointerType;
267 typedef StorageMemory<CoeffReturnType, Device> Storage;
268 typedef typename Storage::Type EvaluatorPointerType;
273 PacketAccess = (PacketType<CoeffReturnType, Device>::size > 1),
275 PreferBlockAccess =
false,
281 typedef internal::TensorBlockNotImplemented TensorBlock;
284 EIGEN_STRONG_INLINE TensorEvaluator(
const XprType& op,
const Device& device)
285 : m_op(op), m_device(device), m_result(NULL)
287 m_dimensions = op.func().dimensions(op.lhsExpression(), op.rhsExpression());
290 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
const Dimensions& dimensions()
const {
return m_dimensions; }
292 EIGEN_STRONG_INLINE
bool evalSubExprsIfNeeded(EvaluatorPointerType data) {
297 m_result =
static_cast<EvaluatorPointerType
>(m_device.get( (CoeffReturnType*)
298 m_device.allocate_temp(dimensions().TotalSize() *
sizeof(CoeffReturnType))));
304 EIGEN_STRONG_INLINE
void cleanup() {
305 if (m_result != NULL) {
306 m_device.deallocate_temp(m_result);
311 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE CoeffReturnType coeff(Index index)
const {
312 return m_result[index];
315 template<
int LoadMode>
316 EIGEN_DEVICE_FUNC PacketReturnType packet(Index index)
const {
317 return internal::ploadt<PacketReturnType, LoadMode>(m_result + index);
320 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorOpCost costPerCoeff(
bool vectorized)
const {
322 return TensorOpCost(
sizeof(CoeffReturnType), 0, 0, vectorized, PacketSize);
325 EIGEN_DEVICE_FUNC EvaluatorPointerType data()
const {
return m_result; }
327 #ifdef EIGEN_USE_SYCL
329 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
void bind(cl::sycl::handler &cgh)
const {
335 void evalTo(EvaluatorPointerType data) {
336 TensorMap<Tensor<CoeffReturnType, NumDims, Layout> > result(m_device.get(data), m_dimensions);
337 m_op.func().eval(m_op.lhsExpression(), m_op.rhsExpression(), result, m_device);
340 Dimensions m_dimensions;
342 const Device EIGEN_DEVICE_REF m_device;
343 EvaluatorPointerType m_result;
The tensor base class.
Definition: TensorForwardDeclarations.h:58
Tensor custom class.
Definition: TensorCustomOp.h:222
Tensor custom class.
Definition: TensorCustomOp.h:56
Namespace containing all symbols from the Eigen library.
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
A cost model used to limit the number of threads used for evaluating tensor expression.
Definition: TensorEvaluator.h:31