12#ifndef EIGEN_TENSOR_TENSOR_TRACE_H
13#define EIGEN_TENSOR_TENSOR_TRACE_H
16#include "./InternalHeaderCheck.h"
21template <
typename Dims,
typename XprType>
22struct traits<TensorTraceOp<Dims, XprType> > :
public traits<XprType> {
23 using Scalar =
typename XprType::Scalar;
24 using XprTraits = traits<XprType>;
25 using StorageKind =
typename XprTraits::StorageKind;
26 using Index =
typename XprTraits::Index;
27 static constexpr int NumDimensions = XprTraits::NumDimensions - array_size<Dims>::value;
28 static constexpr int Layout = XprTraits::Layout;
31 Flags = traits<XprType>::Flags & ~LvalueBit
35template <
typename Dims,
typename XprType>
36struct eval<TensorTraceOp<Dims, XprType>, Eigen::Dense> {
37 using type =
const TensorTraceOp<Dims, XprType>&;
47template <
typename Dims,
typename XprType>
48class TensorTraceOp :
public TensorBase<TensorTraceOp<Dims, XprType> > {
50 using Scalar =
typename Eigen::internal::traits<TensorTraceOp>::Scalar;
52 using CoeffReturnType =
typename XprType::CoeffReturnType;
53 using Nested =
typename Eigen::internal::ref_selector<TensorTraceOp>::type;
54 using StorageKind =
typename Eigen::internal::traits<TensorTraceOp>::StorageKind;
55 using Index =
typename Eigen::internal::traits<TensorTraceOp>::Index;
57 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorTraceOp(
const XprType& expr,
const Dims& dims)
58 : m_xpr(expr), m_dims(dims) {}
60 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
const Dims& dims()
const {
return m_dims; }
62 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
const internal::remove_all_t<typename XprType::Nested>& expression()
const {
67 typename XprType::Nested m_xpr;
72template <
typename Dims,
typename ArgType,
typename Device>
75 static constexpr int NumInputDims =
76 internal::array_size<typename TensorEvaluator<ArgType, Device>::Dimensions>::value;
77 static constexpr int NumReducedDims = internal::array_size<Dims>::value;
78 static constexpr int NumOutputDims = NumInputDims - NumReducedDims;
79 using Index =
typename XprType::Index;
80 using Dimensions = DSizes<Index, NumOutputDims>;
81 using Scalar =
typename XprType::Scalar;
82 using CoeffReturnType =
typename XprType::CoeffReturnType;
83 using PacketReturnType =
typename PacketType<CoeffReturnType, Device>::type;
84 static constexpr int PacketSize = internal::unpacket_traits<PacketReturnType>::size;
85 using Storage = StorageMemory<CoeffReturnType, Device>;
86 using EvaluatorPointerType =
typename Storage::Type;
88 static constexpr int Layout = TensorEvaluator<ArgType, Device>::Layout;
91 PacketAccess = TensorEvaluator<ArgType, Device>::PacketAccess,
99 using TensorBlock = internal::TensorBlockNotImplemented;
102 EIGEN_STRONG_INLINE TensorEvaluator(
const XprType& op,
const Device& device)
103 : m_impl(op.expression(), device), m_traceDim(1), m_device(device) {
104 EIGEN_STATIC_ASSERT((NumOutputDims >= 0), YOU_MADE_A_PROGRAMMING_MISTAKE);
105 EIGEN_STATIC_ASSERT((NumReducedDims >= 2) || ((NumReducedDims == 0) && (NumInputDims == 0)),
106 YOU_MADE_A_PROGRAMMING_MISTAKE);
108 for (
int i = 0; i < NumInputDims; ++i) {
109 m_reduced[i] =
false;
112 const Dims& op_dims = op.dims();
113 for (
int i = 0; i < NumReducedDims; ++i) {
114 eigen_assert(op_dims[i] >= 0);
115 eigen_assert(op_dims[i] < NumInputDims);
116 m_reduced[op_dims[i]] =
true;
120 int num_distinct_reduce_dims = 0;
121 for (
int i = 0; i < NumInputDims; ++i) {
123 ++num_distinct_reduce_dims;
127 EIGEN_ONLY_USED_FOR_DEBUG(num_distinct_reduce_dims);
128 eigen_assert(num_distinct_reduce_dims == NumReducedDims);
131 const typename TensorEvaluator<ArgType, Device>::Dimensions& input_dims = m_impl.dimensions();
133 int output_index = 0;
134 int reduced_index = 0;
135 for (
int i = 0; i < NumInputDims; ++i) {
137 m_reducedDims[reduced_index] = input_dims[i];
138 if (reduced_index > 0) {
140 eigen_assert(m_reducedDims[0] == m_reducedDims[reduced_index]);
144 m_dimensions[output_index] = input_dims[i];
149 EIGEN_IF_CONSTEXPR (NumReducedDims != 0) {
150 m_traceDim = m_reducedDims[0];
154 EIGEN_IF_CONSTEXPR (NumOutputDims > 0) {
155 EIGEN_IF_CONSTEXPR (
static_cast<int>(Layout) ==
static_cast<int>(
ColMajor)) {
156 m_outputStrides[0] = 1;
157 for (
int i = 1; i < NumOutputDims; ++i) {
158 m_outputStrides[i] = m_outputStrides[i - 1] * m_dimensions[i - 1];
161 m_outputStrides.back() = 1;
162 for (
int i = NumOutputDims - 2; i >= 0; --i) {
163 m_outputStrides[i] = m_outputStrides[i + 1] * m_dimensions[i + 1];
169 EIGEN_IF_CONSTEXPR (NumInputDims > 0) {
170 array<Index, NumInputDims> input_strides;
171 EIGEN_IF_CONSTEXPR (
static_cast<int>(Layout) ==
static_cast<int>(
ColMajor)) {
172 input_strides[0] = 1;
173 for (
int i = 1; i < NumInputDims; ++i) {
174 input_strides[i] = input_strides[i - 1] * input_dims[i - 1];
177 input_strides.back() = 1;
178 for (
int i = NumInputDims - 2; i >= 0; --i) {
179 input_strides[i] = input_strides[i + 1] * input_dims[i + 1];
185 for (
int i = 0; i < NumInputDims; ++i) {
187 m_reducedStrides[reduced_index] = input_strides[i];
190 m_preservedStrides[output_index] = input_strides[i];
197 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
const Dimensions& dimensions()
const {
return m_dimensions; }
199#ifdef EIGEN_USE_THREADS
200 template <
typename EvalSubExprsCallback>
201 EIGEN_STRONG_INLINE
void evalSubExprsIfNeededAsync(EvaluatorPointerType, EvalSubExprsCallback done) {
202 m_impl.evalSubExprsIfNeededAsync(
nullptr, [done](
bool) { done(
true); });
206 EIGEN_STRONG_INLINE
bool evalSubExprsIfNeeded(EvaluatorPointerType ) {
207 m_impl.evalSubExprsIfNeeded(
nullptr);
211 EIGEN_DEVICE_FUNC EvaluatorPointerType data()
const {
return nullptr; }
213 EIGEN_STRONG_INLINE
void cleanup() { m_impl.cleanup(); }
217 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorOpCost costPerCoeff(
bool )
const {
218 const double compute_cost = NumOutputDims * (TensorOpCost::DivCost<Index>() + TensorOpCost::MulCost<Index>() +
219 TensorOpCost::AddCost<Index>()) +
220 m_traceDim * (TensorOpCost::AddCost<Scalar>() + TensorOpCost::AddCost<Index>());
221 return m_impl.costPerCoeff(
false) * m_traceDim + TensorOpCost(0, 0, compute_cost);
224 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE CoeffReturnType coeff(Index index)
const {
226 CoeffReturnType result = internal::cast<int, CoeffReturnType>(0);
227 Index index_stride = 0;
228 for (
int i = 0; i < NumReducedDims; ++i) {
229 index_stride += m_reducedStrides[i];
234 EIGEN_IF_CONSTEXPR (NumOutputDims != 0) {
235 cur_index = firstInput(index);
237 for (Index i = 0; i < m_traceDim; ++i) {
238 result += m_impl.coeff(cur_index);
239 cur_index += index_stride;
245 template <
int LoadMode>
246 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE PacketReturnType packet(Index index)
const {
247 eigen_assert(index + PacketSize - 1 < dimensions().TotalSize());
249 EIGEN_ALIGN_MAX std::remove_const_t<CoeffReturnType> values[PacketSize];
250 for (
int i = 0; i < PacketSize; ++i) {
251 values[i] = coeff(index + i);
253 PacketReturnType result = internal::ploadt<PacketReturnType, LoadMode>(values);
259 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Index firstInput(Index index)
const {
return firstInputImpl(index); }
261 template <
int ND = NumOutputDims>
262 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE std::enable_if_t<ND == 0, Index> firstInputImpl(Index )
const {
266 template <
int ND = NumOutputDims>
267 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE std::enable_if_t<(ND > 0), Index> firstInputImpl(Index index)
const {
268 Index startInput = 0;
269 EIGEN_IF_CONSTEXPR (
static_cast<int>(Layout) ==
static_cast<int>(
ColMajor)) {
270 for (
int i = ND - 1; i > 0; --i) {
271 const Index idx = index / m_outputStrides[i];
272 startInput += idx * m_preservedStrides[i];
273 index -= idx * m_outputStrides[i];
275 startInput += index * m_preservedStrides[0];
277 for (
int i = 0; i < ND - 1; ++i) {
278 const Index idx = index / m_outputStrides[i];
279 startInput += idx * m_preservedStrides[i];
280 index -= idx * m_outputStrides[i];
282 startInput += index * m_preservedStrides[ND - 1];
287 Dimensions m_dimensions;
288 TensorEvaluator<ArgType, Device> m_impl;
291 const Device EIGEN_DEVICE_REF m_device;
292 array<bool, NumInputDims> m_reduced;
293 array<Index, NumReducedDims> m_reducedDims;
294 array<Index, NumOutputDims> m_outputStrides;
295 array<Index, NumReducedDims> m_reducedStrides;
296 array<Index, NumOutputDims> m_preservedStrides;
The tensor base class.
Definition TensorForwardDeclarations.h:69
Tensor Trace class.
Definition TensorTrace.h:48
Namespace containing all symbols from the Eigen library.
The tensor evaluator class.
Definition TensorEvaluator.h:47