11#ifndef EIGEN_PARTIALREDUX_H
12#define EIGEN_PARTIALREDUX_H
15#include "./InternalHeaderCheck.h"
44template <
typename Func,
typename Evaluator>
45struct packetwise_redux_traits {
46 static constexpr int OuterSize =
47 int(Evaluator::IsRowMajor) ? Evaluator::RowsAtCompileTime : Evaluator::ColsAtCompileTime;
48 static constexpr int Cost = OuterSize == Dynamic
50 : OuterSize * Evaluator::CoeffReadCost + (OuterSize - 1) * functor_traits<Func>::Cost;
51 static constexpr int Unrolling = Cost <= EIGEN_UNROLLING_LIMIT ? CompleteUnrolling : NoUnrolling;
55template <
typename PacketType,
typename Func>
56EIGEN_DEVICE_FUNC PacketType packetwise_redux_empty_value(
const Func&) {
57 const typename unpacket_traits<PacketType>::type zero(0);
58 return pset1<PacketType>(zero);
62template <
typename PacketType,
typename Scalar>
63EIGEN_DEVICE_FUNC PacketType packetwise_redux_empty_value(
const scalar_product_op<Scalar, Scalar>&) {
64 return pset1<PacketType>(Scalar(1));
68template <typename Func, typename Evaluator, int Unrolling = packetwise_redux_traits<Func, Evaluator>::Unrolling>
69struct packetwise_redux_impl;
72template <
typename Func,
typename Evaluator>
73struct packetwise_redux_impl<Func, Evaluator, CompleteUnrolling> {
74 using Base = redux_novec_unroller<Func, Evaluator, 0, Evaluator::SizeAtCompileTime>;
75 using Scalar =
typename Evaluator::Scalar;
77 template <
typename PacketType>
78 EIGEN_DEVICE_FUNC
static EIGEN_STRONG_INLINE PacketType run(
const Evaluator& eval,
const Func& func, Index ) {
79 return redux_vec_unroller<Func, Evaluator, 0,
80 packetwise_redux_traits<Func, Evaluator>::OuterSize>::template run<PacketType>(eval,
89template <
typename Func,
typename Evaluator, Index Start>
90struct redux_vec_unroller<Func, Evaluator, Start, 0> {
91 template <
typename PacketType>
92 EIGEN_DEVICE_FUNC
static EIGEN_STRONG_INLINE PacketType run(
const Evaluator&,
const Func& f) {
93 return packetwise_redux_empty_value<PacketType>(f);
98template <
typename Func,
typename Evaluator>
99struct packetwise_redux_impl<Func, Evaluator, NoUnrolling> {
100 using Scalar =
typename Evaluator::Scalar;
101 using PacketScalar =
typename redux_traits<Func, Evaluator>::PacketType;
103 template <
typename PacketType>
104 EIGEN_DEVICE_FUNC
static PacketType run(
const Evaluator& eval,
const Func& func, Index size) {
105 if (size == 0)
return packetwise_redux_empty_value<PacketType>(func);
107 const Index size4 = 1 + numext::round_down(size - 1, 4);
108 PacketType p = eval.template packetByOuterInner<Unaligned, PacketType>(0, 0);
112 for (Index i = 1; i < size4; i += 4)
114 p, func.packetOp(func.packetOp(eval.template packetByOuterInner<Unaligned, PacketType>(i + 0, 0),
115 eval.template packetByOuterInner<Unaligned, PacketType>(i + 1, 0)),
116 func.packetOp(eval.template packetByOuterInner<Unaligned, PacketType>(i + 2, 0),
117 eval.template packetByOuterInner<Unaligned, PacketType>(i + 3, 0))));
118 for (Index i = size4; i < size; ++i)
119 p = func.packetOp(p, eval.template packetByOuterInner<Unaligned, PacketType>(i, 0));
124template <
typename Func,
typename Evaluator>
125struct packetwise_segment_redux_impl {
126 using Scalar =
typename Evaluator::Scalar;
127 using PacketScalar =
typename redux_traits<Func, Evaluator>::PacketType;
129 template <
typename PacketType>
130 EIGEN_DEVICE_FUNC
static PacketType run(
const Evaluator& eval,
const Func& func, Index size, Index begin,
132 if (size == 0)
return packetwise_redux_empty_value<PacketType>(func);
134 const Index size4 = 1 + numext::round_down(size - 1, 4);
135 PacketType p = eval.template packetSegmentByOuterInner<Unaligned, PacketType>(0, 0, begin, count);
137 for (Index i = 1; i < size4; i += 4)
141 func.packetOp(eval.template packetSegmentByOuterInner<Unaligned, PacketType>(i + 0, 0, begin, count),
142 eval.template packetSegmentByOuterInner<Unaligned, PacketType>(i + 1, 0, begin, count)),
143 func.packetOp(eval.template packetSegmentByOuterInner<Unaligned, PacketType>(i + 2, 0, begin, count),
144 eval.template packetSegmentByOuterInner<Unaligned, PacketType>(i + 3, 0, begin, count))));
145 for (Index i = size4; i < size; ++i)
146 p = func.packetOp(p, eval.template packetSegmentByOuterInner<Unaligned, PacketType>(i, 0, begin, count));
151template <
typename ArgType,
typename MemberOp,
int Direction>
152struct evaluator<PartialReduxExpr<ArgType, MemberOp, Direction> >
153 : evaluator_base<PartialReduxExpr<ArgType, MemberOp, Direction> > {
154 using XprType = PartialReduxExpr<ArgType, MemberOp, Direction>;
155 using ArgTypeNested =
typename internal::nested_eval<ArgType, 1>::type;
156 using ConstArgTypeNested = add_const_on_value_type_t<ArgTypeNested>;
157 using ArgTypeNestedCleaned = internal::remove_all_t<ArgTypeNested>;
158 using InputScalar =
typename ArgType::Scalar;
159 using Scalar =
typename XprType::Scalar;
161 TraversalSize = Direction == int(
Vertical) ? int(ArgType::RowsAtCompileTime) : int(ArgType::ColsAtCompileTime)
163 using CostOpType =
typename MemberOp::template Cost<int(TraversalSize)>;
165 CoeffReadCost = TraversalSize == Dynamic ? HugeCost
168 : int(TraversalSize) * int(evaluator<ArgType>::CoeffReadCost) + int(CostOpType::value),
170 ArgFlags_ = evaluator<ArgType>::Flags,
172 Vectorizable_ = bool(
int(ArgFlags_) &
PacketAccessBit) && bool(MemberOp::Vectorizable) &&
174 (TraversalSize != 0),
176 Flags = (traits<XprType>::Flags &
RowMajorBit) | (evaluator<ArgType>::Flags & (HereditaryBits & (~
RowMajorBit))) |
182 EIGEN_DEVICE_FUNC
explicit evaluator(
const XprType& xpr) : m_arg(xpr.nestedExpression()), m_functor(xpr.functor()) {
183 EIGEN_INTERNAL_CHECK_COST_VALUE(TraversalSize == Dynamic ? HugeCost
184 : (TraversalSize == 0 ? 1 :
int(CostOpType::value)));
185 EIGEN_INTERNAL_CHECK_COST_VALUE(CoeffReadCost);
188 using CoeffReturnType =
typename XprType::CoeffReturnType;
190 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
const Scalar coeff(Index i, Index j)
const {
191 return coeff(Direction ==
Vertical ? j : i);
194 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
const Scalar coeff(Index index)
const {
195 return m_functor(m_arg.template subVector<
DirectionType(Direction)>(index));
198 template <
int LoadMode,
typename PacketType>
199 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE PacketType packet(Index i, Index j)
const {
200 return packet<LoadMode, PacketType>(Direction ==
Vertical ? j : i);
203 template <
int LoadMode,
typename PacketType>
204 EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC PacketType packet(Index idx)
const {
205 static constexpr int PacketSize = internal::unpacket_traits<PacketType>::size;
206 static constexpr int PanelRows = Direction ==
Vertical ? ArgType::RowsAtCompileTime : PacketSize;
207 static constexpr int PanelCols = Direction ==
Vertical ? PacketSize : ArgType::ColsAtCompileTime;
208 using PanelType = Block<
const ArgTypeNestedCleaned, PanelRows, PanelCols,
true >;
209 using PanelEvaluator =
typename internal::redux_evaluator<PanelType>;
210 using BinaryOp =
typename MemberOp::BinaryOp;
211 using Impl = internal::packetwise_redux_impl<BinaryOp, PanelEvaluator>;
216 EIGEN_IF_CONSTEXPR (PacketSize == 1) return internal::pset1<PacketType>(coeff(idx));
218 Index startRow = Direction ==
Vertical ? 0 : idx;
219 Index startCol = Direction ==
Vertical ? idx : 0;
220 Index numRows = Direction ==
Vertical ? m_arg.rows() : PacketSize;
221 Index numCols = Direction ==
Vertical ? PacketSize : m_arg.cols();
223 PanelType panel(m_arg, startRow, startCol, numRows, numCols);
224 PanelEvaluator panel_eval(panel);
225 PacketType p = Impl::template run<PacketType>(panel_eval, m_functor.binaryFunc(), m_arg.outerSize());
229 template <
int LoadMode, typename PacketType>
230 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE PacketType packetSegment(Index i, Index j, Index begin, Index count)
const {
231 return packetSegment<LoadMode, PacketType>(Direction ==
Vertical ? j : i, begin, count);
234 template <
int LoadMode,
typename PacketType>
235 EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC PacketType packetSegment(Index idx, Index begin, Index count)
const {
236 static constexpr int PanelRows = Direction ==
Vertical ? ArgType::RowsAtCompileTime : Dynamic;
237 static constexpr int PanelCols = Direction ==
Vertical ? Dynamic : ArgType::ColsAtCompileTime;
238 using PanelType = Block<
const ArgTypeNestedCleaned, PanelRows, PanelCols,
true >;
239 using PanelEvaluator =
typename internal::redux_evaluator<PanelType>;
240 using BinaryOp =
typename MemberOp::BinaryOp;
241 using Impl = internal::packetwise_segment_redux_impl<BinaryOp, PanelEvaluator>;
243 Index startRow = Direction ==
Vertical ? 0 : idx;
244 Index startCol = Direction ==
Vertical ? idx : 0;
245 Index numRows = Direction ==
Vertical ? m_arg.rows() : begin + count;
246 Index numCols = Direction ==
Vertical ? begin + count : m_arg.cols();
248 PanelType panel(m_arg, startRow, startCol, numRows, numCols);
249 PanelEvaluator panel_eval(panel);
250 PacketType p = Impl::template run<PacketType>(panel_eval, m_functor.binaryFunc(), m_arg.outerSize(), begin, count);
255 ConstArgTypeNested m_arg;
256 const MemberOp m_functor;
DirectionType
Definition Constants.h:264
@ Vertical
Definition Constants.h:267
constexpr unsigned int PacketAccessBit
Definition Constants.h:98
constexpr unsigned int LinearAccessBit
Definition Constants.h:134
constexpr unsigned int RowMajorBit
Definition Constants.h:71