11#ifndef EIGEN_TENSOR_TENSOR_CUSTOM_OP_H
12#define EIGEN_TENSOR_TENSOR_CUSTOM_OP_H
15#include "./InternalHeaderCheck.h"
20template <
typename CustomUnaryFunc,
typename XprType>
21struct traits<TensorCustomUnaryOp<CustomUnaryFunc, XprType> > {
22 using Scalar =
typename XprType::Scalar;
23 using StorageKind =
typename XprType::StorageKind;
24 using Index =
typename XprType::Index;
28 using CustomDimensions = remove_all_t<decltype(std::declval<const CustomUnaryFunc&>().dimensions(
29 std::declval<
const remove_all_t<typename XprType::Nested>&>()))>;
30 static constexpr ptrdiff_t CustomRank = array_size<CustomDimensions>::value;
31 static_assert(CustomRank >= 0,
32 "The dimensions() method of a custom tensor functor must return a fixed-rank "
33 "array-like type such as DSizes<Index, Rank>.");
35 static constexpr int NumDimensions = CustomRank < 0 ? 1 : static_cast<int>(CustomRank);
36 static constexpr int Layout = traits<XprType>::Layout;
37 using PointerType =
typename traits<XprType>::PointerType;
41template <
typename CustomUnaryFunc,
typename XprType>
42struct eval<TensorCustomUnaryOp<CustomUnaryFunc, XprType>, Eigen::Dense> {
43 using type =
const TensorCustomUnaryOp<CustomUnaryFunc, XprType> EIGEN_DEVICE_REF;
53template <
typename CustomUnaryFunc,
typename XprType>
54class TensorCustomUnaryOp :
public TensorBase<TensorCustomUnaryOp<CustomUnaryFunc, XprType>, ReadOnlyAccessors> {
56 using Scalar =
typename internal::traits<TensorCustomUnaryOp>::Scalar;
58 using CoeffReturnType =
typename XprType::CoeffReturnType;
59 using Nested =
typename internal::ref_selector<TensorCustomUnaryOp>::non_const_type;
60 using StorageKind =
typename internal::traits<TensorCustomUnaryOp>::StorageKind;
61 using Index =
typename internal::traits<TensorCustomUnaryOp>::Index;
63 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorCustomUnaryOp(
const XprType& expr,
const CustomUnaryFunc& func)
64 : m_expr(expr), m_func(func) {}
66 EIGEN_DEVICE_FUNC
const CustomUnaryFunc& func()
const {
return m_func; }
68 EIGEN_DEVICE_FUNC
const internal::remove_all_t<typename XprType::Nested>& expression()
const {
return m_expr; }
71 typename XprType::Nested m_expr;
72 const CustomUnaryFunc m_func;
76template <
typename CustomUnaryFunc,
typename XprType,
typename Device>
79 using Index =
typename internal::traits<ArgType>::Index;
80 static constexpr int NumDims = internal::traits<ArgType>::NumDimensions;
81 using Dimensions = DSizes<Index, NumDims>;
82 using Scalar = std::remove_const_t<typename ArgType::Scalar>;
83 using CoeffReturnType = std::remove_const_t<typename XprType::CoeffReturnType>;
84 using PacketReturnType =
typename PacketType<CoeffReturnType, Device>::type;
85 static constexpr int PacketSize = PacketType<CoeffReturnType, Device>::size;
86 using TensorPointerType =
typename Eigen::internal::traits<XprType>::PointerType;
87 using Storage = StorageMemory<CoeffReturnType, Device>;
88 using EvaluatorPointerType =
typename Storage::Type;
90 static constexpr int Layout = TensorEvaluator<XprType, Device>::Layout;
93 PacketAccess = (PacketType<CoeffReturnType, Device>::size > 1),
99 BlockAccess = internal::is_arithmetic<CoeffReturnType>::value,
100 PreferBlockAccess =
false,
106 using TensorBlockDesc = internal::TensorBlockDescriptor<NumDims, Index>;
107 using TensorBlockScratch = internal::TensorBlockScratchAllocator<Device>;
109 using TensorBlock =
typename internal::TensorMaterializedBlock<CoeffReturnType, NumDims, Layout, Index>;
113 EIGEN_STRONG_INLINE TensorEvaluator(
const ArgType& op,
const Device& device)
114 : m_dimensions(op.func().dimensions(op.expression())), m_op(op), m_device(device), m_result(nullptr) {}
116 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
const Dimensions& dimensions()
const {
return m_dimensions; }
118 EIGEN_STRONG_INLINE
bool evalSubExprsIfNeeded(EvaluatorPointerType data) {
123 m_result =
static_cast<EvaluatorPointerType
>(
124 m_device.get((CoeffReturnType*)m_device.allocate_temp(dimensions().TotalSize() *
sizeof(CoeffReturnType))));
130 EIGEN_STRONG_INLINE
void cleanup() {
132 m_device.deallocate_temp(m_result);
137 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE CoeffReturnType coeff(Index index)
const {
return m_result[index]; }
139 template <
int LoadMode>
140 EIGEN_DEVICE_FUNC PacketReturnType packet(Index index)
const {
141 return internal::ploadt<PacketReturnType, LoadMode>(m_result + index);
144 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorOpCost costPerCoeff(
bool vectorized)
const {
146 return TensorOpCost(
sizeof(CoeffReturnType), 0, 0, vectorized, PacketSize);
149 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE internal::TensorBlockResourceRequirements getResourceRequirements()
const {
150 return internal::TensorBlockResourceRequirements::any();
153 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorBlock block(TensorBlockDesc& desc, TensorBlockScratch& scratch,
154 bool =
false)
const {
155 eigen_assert(m_result !=
nullptr);
156 return TensorBlock::materialize(m_result, m_dimensions, desc, scratch);
159 EIGEN_DEVICE_FUNC EvaluatorPointerType data()
const {
return m_result; }
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;
181template <
typename CustomBinaryFunc,
typename LhsXprType,
typename RhsXprType>
182struct traits<TensorCustomBinaryOp<CustomBinaryFunc, LhsXprType, RhsXprType> > {
183 using Scalar =
typename internal::promote_storage_type<typename LhsXprType::Scalar, typename RhsXprType::Scalar>::ret;
184 using CoeffReturnType =
typename internal::promote_storage_type<
typename LhsXprType::CoeffReturnType,
185 typename RhsXprType::CoeffReturnType>::ret;
186 using StorageKind =
typename promote_storage_type<typename traits<LhsXprType>::StorageKind,
187 typename traits<RhsXprType>::StorageKind>::ret;
189 typename promote_index_type<typename traits<LhsXprType>::Index,
typename traits<RhsXprType>::Index>::type;
193 using CustomDimensions = remove_all_t<decltype(std::declval<const CustomBinaryFunc&>().dimensions(
194 std::declval<
const remove_all_t<typename LhsXprType::Nested>&>(),
195 std::declval<
const remove_all_t<typename RhsXprType::Nested>&>()))>;
196 static constexpr ptrdiff_t CustomRank = array_size<CustomDimensions>::value;
197 static_assert(CustomRank >= 0,
198 "The dimensions() method of a custom tensor functor must return a fixed-rank "
199 "array-like type such as DSizes<Index, Rank>.");
201 static constexpr int NumDimensions = CustomRank < 0 ? 1 : static_cast<int>(CustomRank);
202 static constexpr int Layout = traits<LhsXprType>::Layout;
204 std::conditional_t<Pointer_type_promotion<typename LhsXprType::Scalar, Scalar>::val,
205 typename traits<LhsXprType>::PointerType,
typename traits<RhsXprType>::PointerType>;
209template <
typename CustomBinaryFunc,
typename LhsXprType,
typename RhsXprType>
210struct eval<TensorCustomBinaryOp<CustomBinaryFunc, LhsXprType, RhsXprType>, Eigen::Dense> {
211 using type =
const TensorCustomBinaryOp<CustomBinaryFunc, LhsXprType, RhsXprType>&;
216template <
typename CustomBinaryFunc,
typename LhsXprType,
typename RhsXprType>
217class TensorCustomBinaryOp
218 :
public TensorBase<TensorCustomBinaryOp<CustomBinaryFunc, LhsXprType, RhsXprType>, ReadOnlyAccessors> {
220 using Scalar =
typename internal::traits<TensorCustomBinaryOp>::Scalar;
222 using CoeffReturnType =
typename internal::traits<TensorCustomBinaryOp>::CoeffReturnType;
223 using Nested =
typename internal::ref_selector<TensorCustomBinaryOp>::non_const_type;
224 using StorageKind =
typename internal::traits<TensorCustomBinaryOp>::StorageKind;
225 using Index =
typename internal::traits<TensorCustomBinaryOp>::Index;
227 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorCustomBinaryOp(
const LhsXprType& lhs,
const RhsXprType& rhs,
228 const CustomBinaryFunc& func)
230 : m_lhs_xpr(lhs), m_rhs_xpr(rhs), m_func(func) {}
232 EIGEN_DEVICE_FUNC
const CustomBinaryFunc& func()
const {
return m_func; }
234 EIGEN_DEVICE_FUNC
const internal::remove_all_t<typename LhsXprType::Nested>& lhsExpression()
const {
238 EIGEN_DEVICE_FUNC
const internal::remove_all_t<typename RhsXprType::Nested>& rhsExpression()
const {
243 typename LhsXprType::Nested m_lhs_xpr;
244 typename RhsXprType::Nested m_rhs_xpr;
245 const CustomBinaryFunc m_func;
249template <
typename CustomBinaryFunc,
typename LhsXprType,
typename RhsXprType,
typename Device>
252 using Index =
typename internal::traits<XprType>::Index;
253 static constexpr int NumDims = internal::traits<XprType>::NumDimensions;
254 using Dimensions = DSizes<Index, NumDims>;
255 using Scalar =
typename XprType::Scalar;
256 using CoeffReturnType = std::remove_const_t<typename XprType::CoeffReturnType>;
257 using PacketReturnType =
typename PacketType<CoeffReturnType, Device>::type;
258 static constexpr int PacketSize = PacketType<CoeffReturnType, Device>::size;
260 using TensorPointerType =
typename Eigen::internal::traits<XprType>::PointerType;
261 using Storage = StorageMemory<CoeffReturnType, Device>;
262 using EvaluatorPointerType =
typename Storage::Type;
264 static constexpr int Layout = TensorEvaluator<LhsXprType, Device>::Layout;
267 PacketAccess = (PacketType<CoeffReturnType, Device>::size > 1),
270 BlockAccess = internal::is_arithmetic<CoeffReturnType>::value,
271 PreferBlockAccess =
false,
277 using TensorBlockDesc = internal::TensorBlockDescriptor<NumDims, Index>;
278 using TensorBlockScratch = internal::TensorBlockScratchAllocator<Device>;
280 using TensorBlock =
typename internal::TensorMaterializedBlock<CoeffReturnType, NumDims, Layout, Index>;
284 EIGEN_STRONG_INLINE TensorEvaluator(
const XprType& op,
const Device& device)
285 : 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
>(
298 m_device.get((CoeffReturnType*)m_device.allocate_temp(dimensions().TotalSize() *
sizeof(CoeffReturnType))));
304 EIGEN_STRONG_INLINE
void cleanup() {
305 if (m_result !=
nullptr) {
306 m_device.deallocate_temp(m_result);
311 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE CoeffReturnType coeff(Index index)
const {
return m_result[index]; }
313 template <
int LoadMode>
314 EIGEN_DEVICE_FUNC PacketReturnType packet(Index index)
const {
315 return internal::ploadt<PacketReturnType, LoadMode>(m_result + index);
318 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorOpCost costPerCoeff(
bool vectorized)
const {
320 return TensorOpCost(
sizeof(CoeffReturnType), 0, 0, vectorized, PacketSize);
323 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE internal::TensorBlockResourceRequirements getResourceRequirements()
const {
324 return internal::TensorBlockResourceRequirements::any();
327 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorBlock block(TensorBlockDesc& desc, TensorBlockScratch& scratch,
328 bool =
false)
const {
329 eigen_assert(m_result !=
nullptr);
330 return TensorBlock::materialize(m_result, m_dimensions, desc, scratch);
333 EIGEN_DEVICE_FUNC EvaluatorPointerType data()
const {
return m_result; }
336 void evalTo(EvaluatorPointerType data) {
343 using MapIndex =
typename internal::promote_index_type<DenseIndex, Index>::type;
344 TensorMap<Tensor<CoeffReturnType, NumDims, Layout, MapIndex> > result(m_device.get(data), m_dimensions);
345 m_op.func().eval(m_op.lhsExpression(), m_op.rhsExpression(), result, m_device);
348 Dimensions m_dimensions;
350 const Device EIGEN_DEVICE_REF m_device;
351 EvaluatorPointerType m_result;
The tensor base class.
Definition TensorForwardDeclarations.h:69
Tensor custom class.
Definition TensorCustomOp.h:218
Tensor custom class.
Definition TensorCustomOp.h:54
Namespace containing all symbols from the Eigen library.
The tensor evaluator class.
Definition TensorEvaluator.h:47