2008-03-16 14:36:25 +00:00
|
|
|
// This file is part of Eigen, a lightweight C++ template library
|
2009-05-22 20:25:33 +02:00
|
|
|
// for linear algebra.
|
2008-03-16 14:36:25 +00:00
|
|
|
//
|
2010-06-24 23:21:58 +02:00
|
|
|
// Copyright (C) 2008 Gael Guennebaud <gael.guennebaud@inria.fr>
|
2008-11-24 13:40:43 +00:00
|
|
|
// Copyright (C) 2006-2008 Benoit Jacob <jacob.benoit.1@gmail.com>
|
2008-03-16 14:36:25 +00:00
|
|
|
//
|
2012-07-13 14:42:47 -04:00
|
|
|
// This Source Code Form is subject to the terms of the Mozilla
|
|
|
|
|
// Public License v. 2.0. If a copy of the MPL was not distributed
|
|
|
|
|
// with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
2008-03-16 14:36:25 +00:00
|
|
|
|
|
|
|
|
#ifndef EIGEN_REDUX_H
|
|
|
|
|
#define EIGEN_REDUX_H
|
|
|
|
|
|
2012-04-15 11:06:28 +01:00
|
|
|
namespace Eigen {
|
|
|
|
|
|
2010-10-25 10:15:22 -04:00
|
|
|
namespace internal {
|
|
|
|
|
|
2009-02-12 15:18:59 +00:00
|
|
|
// TODO
|
|
|
|
|
// * implement other kind of vectorization
|
|
|
|
|
// * factorize code
|
|
|
|
|
|
|
|
|
|
/***************************************************************************
|
|
|
|
|
* Part 1 : the logic deciding a strategy for vectorization and unrolling
|
|
|
|
|
***************************************************************************/
|
|
|
|
|
|
|
|
|
|
template<typename Func, typename Derived>
|
2010-10-25 10:15:22 -04:00
|
|
|
struct redux_traits
|
2009-02-12 15:18:59 +00:00
|
|
|
{
|
2010-03-21 11:28:03 -04:00
|
|
|
public:
|
2009-02-12 15:18:59 +00:00
|
|
|
enum {
|
2010-10-25 10:15:22 -04:00
|
|
|
PacketSize = packet_traits<typename Derived::Scalar>::size,
|
2010-02-26 21:46:43 -05:00
|
|
|
InnerMaxSize = int(Derived::IsRowMajor)
|
2009-03-09 23:16:39 +00:00
|
|
|
? Derived::MaxColsAtCompileTime
|
|
|
|
|
: Derived::MaxRowsAtCompileTime
|
2009-02-12 15:18:59 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
enum {
|
2009-03-09 23:16:39 +00:00
|
|
|
MightVectorize = (int(Derived::Flags)&ActualPacketAccessBit)
|
2010-10-25 10:15:22 -04:00
|
|
|
&& (functor_traits<Func>::PacketAccess),
|
2009-03-09 23:16:39 +00:00
|
|
|
MayLinearVectorize = MightVectorize && (int(Derived::Flags)&LinearAccessBit),
|
2010-02-04 18:51:29 +01:00
|
|
|
MaySliceVectorize = MightVectorize && int(InnerMaxSize)>=3*PacketSize
|
2009-02-12 15:18:59 +00:00
|
|
|
};
|
|
|
|
|
|
2009-03-09 23:16:39 +00:00
|
|
|
public:
|
|
|
|
|
enum {
|
2009-11-18 11:57:07 -05:00
|
|
|
Traversal = int(MayLinearVectorize) ? int(LinearVectorizedTraversal)
|
2010-03-21 11:28:03 -04:00
|
|
|
: int(MaySliceVectorize) ? int(SliceVectorizedTraversal)
|
|
|
|
|
: int(DefaultTraversal)
|
2009-03-09 23:16:39 +00:00
|
|
|
};
|
2010-02-04 18:51:29 +01:00
|
|
|
|
2010-03-21 11:28:03 -04:00
|
|
|
public:
|
2009-02-12 15:18:59 +00:00
|
|
|
enum {
|
2015-10-28 13:39:02 +01:00
|
|
|
Cost = Derived::SizeAtCompileTime == Dynamic ? HugeCost
|
|
|
|
|
: Derived::SizeAtCompileTime * Derived::CoeffReadCost + (Derived::SizeAtCompileTime-1) * functor_traits<Func>::Cost,
|
2009-11-18 11:57:07 -05:00
|
|
|
UnrollingLimit = EIGEN_UNROLLING_LIMIT * (int(Traversal) == int(DefaultTraversal) ? 1 : int(PacketSize))
|
2009-02-12 15:18:59 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
enum {
|
2015-10-28 13:39:02 +01:00
|
|
|
Unrolling = Cost <= UnrollingLimit ? CompleteUnrolling : NoUnrolling
|
2009-02-12 15:18:59 +00:00
|
|
|
};
|
2014-03-12 18:13:18 +01:00
|
|
|
|
|
|
|
|
#ifdef EIGEN_DEBUG_ASSIGN
|
|
|
|
|
static void debug()
|
|
|
|
|
{
|
|
|
|
|
std::cerr << "Xpr: " << typeid(typename Derived::XprType).name() << std::endl;
|
|
|
|
|
std::cerr.setf(std::ios::hex, std::ios::basefield);
|
|
|
|
|
EIGEN_DEBUG_VAR(Derived::Flags)
|
|
|
|
|
std::cerr.unsetf(std::ios::hex);
|
|
|
|
|
EIGEN_DEBUG_VAR(InnerMaxSize)
|
|
|
|
|
EIGEN_DEBUG_VAR(PacketSize)
|
|
|
|
|
EIGEN_DEBUG_VAR(MightVectorize)
|
|
|
|
|
EIGEN_DEBUG_VAR(MayLinearVectorize)
|
|
|
|
|
EIGEN_DEBUG_VAR(MaySliceVectorize)
|
|
|
|
|
EIGEN_DEBUG_VAR(Traversal)
|
|
|
|
|
EIGEN_DEBUG_VAR(UnrollingLimit)
|
|
|
|
|
EIGEN_DEBUG_VAR(Unrolling)
|
|
|
|
|
std::cerr << std::endl;
|
|
|
|
|
}
|
|
|
|
|
#endif
|
2009-02-12 15:18:59 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/***************************************************************************
|
|
|
|
|
* Part 2 : unrollers
|
|
|
|
|
***************************************************************************/
|
|
|
|
|
|
|
|
|
|
/*** no vectorization ***/
|
|
|
|
|
|
|
|
|
|
template<typename Func, typename Derived, int Start, int Length>
|
2010-10-25 10:15:22 -04:00
|
|
|
struct redux_novec_unroller
|
2008-03-16 14:36:25 +00:00
|
|
|
{
|
|
|
|
|
enum {
|
|
|
|
|
HalfLength = Length/2
|
|
|
|
|
};
|
|
|
|
|
|
2009-02-12 15:18:59 +00:00
|
|
|
typedef typename Derived::Scalar Scalar;
|
2008-03-16 14:36:25 +00:00
|
|
|
|
2013-02-07 19:06:14 +01:00
|
|
|
EIGEN_DEVICE_FUNC
|
2012-01-31 12:58:52 +01:00
|
|
|
static EIGEN_STRONG_INLINE Scalar run(const Derived &mat, const Func& func)
|
2008-03-16 14:36:25 +00:00
|
|
|
{
|
2010-10-25 10:15:22 -04:00
|
|
|
return func(redux_novec_unroller<Func, Derived, Start, HalfLength>::run(mat,func),
|
|
|
|
|
redux_novec_unroller<Func, Derived, Start+HalfLength, Length-HalfLength>::run(mat,func));
|
2008-03-16 14:36:25 +00:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2009-02-12 15:18:59 +00:00
|
|
|
template<typename Func, typename Derived, int Start>
|
2010-10-25 10:15:22 -04:00
|
|
|
struct redux_novec_unroller<Func, Derived, Start, 1>
|
2008-03-16 14:36:25 +00:00
|
|
|
{
|
|
|
|
|
enum {
|
2010-02-26 21:46:43 -05:00
|
|
|
outer = Start / Derived::InnerSizeAtCompileTime,
|
|
|
|
|
inner = Start % Derived::InnerSizeAtCompileTime
|
2008-03-16 14:36:25 +00:00
|
|
|
};
|
|
|
|
|
|
2009-02-12 15:18:59 +00:00
|
|
|
typedef typename Derived::Scalar Scalar;
|
2008-03-16 14:36:25 +00:00
|
|
|
|
2013-02-07 19:06:14 +01:00
|
|
|
EIGEN_DEVICE_FUNC
|
2012-01-31 12:58:52 +01:00
|
|
|
static EIGEN_STRONG_INLINE Scalar run(const Derived &mat, const Func&)
|
2008-03-16 14:36:25 +00:00
|
|
|
{
|
2010-02-26 21:46:43 -05:00
|
|
|
return mat.coeffByOuterInner(outer, inner);
|
2008-03-16 14:36:25 +00:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2009-10-15 21:07:14 +02:00
|
|
|
// This is actually dead code and will never be called. It is required
|
|
|
|
|
// to prevent false warnings regarding failed inlining though
|
|
|
|
|
// for 0 length run() will never be called at all.
|
|
|
|
|
template<typename Func, typename Derived, int Start>
|
2010-10-25 10:15:22 -04:00
|
|
|
struct redux_novec_unroller<Func, Derived, Start, 0>
|
2009-10-15 21:07:14 +02:00
|
|
|
{
|
|
|
|
|
typedef typename Derived::Scalar Scalar;
|
2013-02-07 19:06:14 +01:00
|
|
|
EIGEN_DEVICE_FUNC
|
2012-01-31 12:58:52 +01:00
|
|
|
static EIGEN_STRONG_INLINE Scalar run(const Derived&, const Func&) { return Scalar(); }
|
2009-10-15 21:07:14 +02:00
|
|
|
};
|
|
|
|
|
|
2009-02-12 15:18:59 +00:00
|
|
|
/*** vectorization ***/
|
2010-02-04 18:51:29 +01:00
|
|
|
|
2009-02-12 15:18:59 +00:00
|
|
|
template<typename Func, typename Derived, int Start, int Length>
|
2010-10-25 10:15:22 -04:00
|
|
|
struct redux_vec_unroller
|
2009-02-12 15:18:59 +00:00
|
|
|
{
|
|
|
|
|
enum {
|
2010-10-25 10:15:22 -04:00
|
|
|
PacketSize = packet_traits<typename Derived::Scalar>::size,
|
2009-02-12 15:18:59 +00:00
|
|
|
HalfLength = Length/2
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
typedef typename Derived::Scalar Scalar;
|
2010-10-25 10:15:22 -04:00
|
|
|
typedef typename packet_traits<Scalar>::type PacketScalar;
|
2009-02-12 15:18:59 +00:00
|
|
|
|
2012-01-31 12:58:52 +01:00
|
|
|
static EIGEN_STRONG_INLINE PacketScalar run(const Derived &mat, const Func& func)
|
2009-02-12 15:18:59 +00:00
|
|
|
{
|
|
|
|
|
return func.packetOp(
|
2010-10-25 10:15:22 -04:00
|
|
|
redux_vec_unroller<Func, Derived, Start, HalfLength>::run(mat,func),
|
|
|
|
|
redux_vec_unroller<Func, Derived, Start+HalfLength, Length-HalfLength>::run(mat,func) );
|
2009-02-12 15:18:59 +00:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
template<typename Func, typename Derived, int Start>
|
2010-10-25 10:15:22 -04:00
|
|
|
struct redux_vec_unroller<Func, Derived, Start, 1>
|
2009-02-12 15:18:59 +00:00
|
|
|
{
|
|
|
|
|
enum {
|
2010-10-25 10:15:22 -04:00
|
|
|
index = Start * packet_traits<typename Derived::Scalar>::size,
|
2010-02-26 21:46:43 -05:00
|
|
|
outer = index / int(Derived::InnerSizeAtCompileTime),
|
|
|
|
|
inner = index % int(Derived::InnerSizeAtCompileTime),
|
First part of a big refactoring of alignment control to enable the handling of arbitrarily aligned buffers. It includes:
- AlignedBit flag is deprecated. Alignment is now specified by the evaluator through the 'Alignment' enum, e.g., evaluator<Xpr>::Alignment. Its value is in Bytes.
- Add several enums to specify alignment: Aligned8, Aligned16, Aligned32, Aligned64, Aligned128. AlignedMax corresponds to EIGEN_MAX_ALIGN_BYTES. Such enums are used to define the above Alignment value, and as the 'Options' template parameter of Map<> and Ref<>.
- The Aligned enum is now deprecated. It is now an alias for Aligned16.
- Currently, traits<Matrix<>>, traits<Array<>>, traits<Ref<>>, traits<Map<>>, and traits<Block<>> also expose the Alignment enum.
2015-08-06 15:31:07 +02:00
|
|
|
alignment = Derived::Alignment
|
2009-02-12 15:18:59 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
typedef typename Derived::Scalar Scalar;
|
2010-10-25 10:15:22 -04:00
|
|
|
typedef typename packet_traits<Scalar>::type PacketScalar;
|
2009-02-12 15:18:59 +00:00
|
|
|
|
2012-01-31 12:58:52 +01:00
|
|
|
static EIGEN_STRONG_INLINE PacketScalar run(const Derived &mat, const Func&)
|
2009-02-12 15:18:59 +00:00
|
|
|
{
|
2015-08-07 12:01:39 +02:00
|
|
|
return mat.template packetByOuterInner<alignment,PacketScalar>(outer, inner);
|
2009-02-12 15:18:59 +00:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/***************************************************************************
|
|
|
|
|
* Part 3 : implementation of all cases
|
|
|
|
|
***************************************************************************/
|
|
|
|
|
|
|
|
|
|
template<typename Func, typename Derived,
|
2010-10-25 10:15:22 -04:00
|
|
|
int Traversal = redux_traits<Func, Derived>::Traversal,
|
|
|
|
|
int Unrolling = redux_traits<Func, Derived>::Unrolling
|
2009-02-12 15:18:59 +00:00
|
|
|
>
|
2010-10-25 10:15:22 -04:00
|
|
|
struct redux_impl;
|
2009-02-12 15:18:59 +00:00
|
|
|
|
|
|
|
|
template<typename Func, typename Derived>
|
2010-10-25 10:15:22 -04:00
|
|
|
struct redux_impl<Func, Derived, DefaultTraversal, NoUnrolling>
|
2008-03-16 14:36:25 +00:00
|
|
|
{
|
2009-02-12 15:18:59 +00:00
|
|
|
typedef typename Derived::Scalar Scalar;
|
2013-02-07 19:06:14 +01:00
|
|
|
EIGEN_DEVICE_FUNC
|
2013-12-02 17:54:38 +01:00
|
|
|
static EIGEN_STRONG_INLINE Scalar run(const Derived &mat, const Func& func)
|
2008-06-07 01:07:48 +00:00
|
|
|
{
|
2010-10-25 10:15:22 -04:00
|
|
|
eigen_assert(mat.rows()>0 && mat.cols()>0 && "you are using an empty matrix");
|
2008-06-07 01:07:48 +00:00
|
|
|
Scalar res;
|
2010-02-26 21:46:43 -05:00
|
|
|
res = mat.coeffByOuterInner(0, 0);
|
2010-05-30 16:00:58 -04:00
|
|
|
for(Index i = 1; i < mat.innerSize(); ++i)
|
2010-02-26 21:46:43 -05:00
|
|
|
res = func(res, mat.coeffByOuterInner(0, i));
|
2010-05-30 16:00:58 -04:00
|
|
|
for(Index i = 1; i < mat.outerSize(); ++i)
|
|
|
|
|
for(Index j = 0; j < mat.innerSize(); ++j)
|
2010-02-26 21:46:43 -05:00
|
|
|
res = func(res, mat.coeffByOuterInner(i, j));
|
2008-06-07 01:07:48 +00:00
|
|
|
return res;
|
|
|
|
|
}
|
2008-03-16 14:36:25 +00:00
|
|
|
};
|
|
|
|
|
|
2009-02-12 15:18:59 +00:00
|
|
|
template<typename Func, typename Derived>
|
2010-10-25 10:15:22 -04:00
|
|
|
struct redux_impl<Func,Derived, DefaultTraversal, CompleteUnrolling>
|
|
|
|
|
: public redux_novec_unroller<Func,Derived, 0, Derived::SizeAtCompileTime>
|
2009-02-12 15:18:59 +00:00
|
|
|
{};
|
|
|
|
|
|
|
|
|
|
template<typename Func, typename Derived>
|
2010-10-25 10:15:22 -04:00
|
|
|
struct redux_impl<Func, Derived, LinearVectorizedTraversal, NoUnrolling>
|
2009-02-12 15:18:59 +00:00
|
|
|
{
|
|
|
|
|
typedef typename Derived::Scalar Scalar;
|
2010-10-25 10:15:22 -04:00
|
|
|
typedef typename packet_traits<Scalar>::type PacketScalar;
|
2009-02-12 15:18:59 +00:00
|
|
|
|
2013-12-02 17:54:38 +01:00
|
|
|
static Scalar run(const Derived &mat, const Func& func)
|
2009-02-12 15:18:59 +00:00
|
|
|
{
|
2010-05-30 16:00:58 -04:00
|
|
|
const Index size = mat.size();
|
2013-12-02 17:54:38 +01:00
|
|
|
|
2015-08-07 10:44:01 +02:00
|
|
|
const Index packetSize = packet_traits<Scalar>::size;
|
|
|
|
|
const int packetAlignment = unpacket_traits<PacketScalar>::alignment;
|
2009-02-12 15:18:59 +00:00
|
|
|
enum {
|
2015-08-07 10:44:01 +02:00
|
|
|
alignment0 = (bool(Derived::Flags & DirectAccessBit) && bool(packet_traits<Scalar>::AlignedOnScalar)) ? int(packetAlignment) : int(Unaligned),
|
First part of a big refactoring of alignment control to enable the handling of arbitrarily aligned buffers. It includes:
- AlignedBit flag is deprecated. Alignment is now specified by the evaluator through the 'Alignment' enum, e.g., evaluator<Xpr>::Alignment. Its value is in Bytes.
- Add several enums to specify alignment: Aligned8, Aligned16, Aligned32, Aligned64, Aligned128. AlignedMax corresponds to EIGEN_MAX_ALIGN_BYTES. Such enums are used to define the above Alignment value, and as the 'Options' template parameter of Map<> and Ref<>.
- The Aligned enum is now deprecated. It is now an alias for Aligned16.
- Currently, traits<Matrix<>>, traits<Array<>>, traits<Ref<>>, traits<Map<>>, and traits<Block<>> also expose the Alignment enum.
2015-08-06 15:31:07 +02:00
|
|
|
alignment = EIGEN_PLAIN_ENUM_MAX(alignment0, Derived::Alignment)
|
2009-02-12 15:18:59 +00:00
|
|
|
};
|
2015-08-06 17:52:01 +02:00
|
|
|
const Index alignedStart = internal::first_default_aligned(mat.nestedExpression());
|
2011-11-12 09:19:48 +01:00
|
|
|
const Index alignedSize2 = ((size-alignedStart)/(2*packetSize))*(2*packetSize);
|
|
|
|
|
const Index alignedSize = ((size-alignedStart)/(packetSize))*(packetSize);
|
|
|
|
|
const Index alignedEnd2 = alignedStart + alignedSize2;
|
|
|
|
|
const Index alignedEnd = alignedStart + alignedSize;
|
2009-02-12 15:18:59 +00:00
|
|
|
Scalar res;
|
|
|
|
|
if(alignedSize)
|
|
|
|
|
{
|
2015-08-07 12:01:39 +02:00
|
|
|
PacketScalar packet_res0 = mat.template packet<alignment,PacketScalar>(alignedStart);
|
2011-11-12 09:19:48 +01:00
|
|
|
if(alignedSize>packetSize) // we have at least two packets to partly unroll the loop
|
|
|
|
|
{
|
2015-08-07 12:01:39 +02:00
|
|
|
PacketScalar packet_res1 = mat.template packet<alignment,PacketScalar>(alignedStart+packetSize);
|
2011-11-12 09:19:48 +01:00
|
|
|
for(Index index = alignedStart + 2*packetSize; index < alignedEnd2; index += 2*packetSize)
|
|
|
|
|
{
|
2015-08-07 12:01:39 +02:00
|
|
|
packet_res0 = func.packetOp(packet_res0, mat.template packet<alignment,PacketScalar>(index));
|
|
|
|
|
packet_res1 = func.packetOp(packet_res1, mat.template packet<alignment,PacketScalar>(index+packetSize));
|
2011-11-12 09:19:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
packet_res0 = func.packetOp(packet_res0,packet_res1);
|
|
|
|
|
if(alignedEnd>alignedEnd2)
|
2015-08-07 12:01:39 +02:00
|
|
|
packet_res0 = func.packetOp(packet_res0, mat.template packet<alignment,PacketScalar>(alignedEnd2));
|
2011-11-12 09:19:48 +01:00
|
|
|
}
|
|
|
|
|
res = func.predux(packet_res0);
|
2010-02-04 18:51:29 +01:00
|
|
|
|
2010-05-30 16:00:58 -04:00
|
|
|
for(Index index = 0; index < alignedStart; ++index)
|
2009-02-12 15:18:59 +00:00
|
|
|
res = func(res,mat.coeff(index));
|
|
|
|
|
|
2010-05-30 16:00:58 -04:00
|
|
|
for(Index index = alignedEnd; index < size; ++index)
|
2009-02-12 15:18:59 +00:00
|
|
|
res = func(res,mat.coeff(index));
|
|
|
|
|
}
|
|
|
|
|
else // too small to vectorize anything.
|
|
|
|
|
// since this is dynamic-size hence inefficient anyway for such small sizes, don't try to optimize.
|
|
|
|
|
{
|
|
|
|
|
res = mat.coeff(0);
|
2010-05-30 16:00:58 -04:00
|
|
|
for(Index index = 1; index < size; ++index)
|
2009-02-12 15:18:59 +00:00
|
|
|
res = func(res,mat.coeff(index));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return res;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2015-10-21 20:58:33 +02:00
|
|
|
// NOTE: for SliceVectorizedTraversal we simply bypass unrolling
|
|
|
|
|
template<typename Func, typename Derived, int Unrolling>
|
|
|
|
|
struct redux_impl<Func, Derived, SliceVectorizedTraversal, Unrolling>
|
2009-03-09 23:16:39 +00:00
|
|
|
{
|
|
|
|
|
typedef typename Derived::Scalar Scalar;
|
2015-08-07 12:01:39 +02:00
|
|
|
typedef typename packet_traits<Scalar>::type PacketType;
|
2009-03-09 23:16:39 +00:00
|
|
|
|
2014-10-13 17:18:26 +02:00
|
|
|
EIGEN_DEVICE_FUNC static Scalar run(const Derived &mat, const Func& func)
|
2009-03-09 23:16:39 +00:00
|
|
|
{
|
2010-10-25 10:15:22 -04:00
|
|
|
eigen_assert(mat.rows()>0 && mat.cols()>0 && "you are using an empty matrix");
|
2010-05-30 16:00:58 -04:00
|
|
|
const Index innerSize = mat.innerSize();
|
|
|
|
|
const Index outerSize = mat.outerSize();
|
2009-03-09 23:16:39 +00:00
|
|
|
enum {
|
2010-10-25 10:15:22 -04:00
|
|
|
packetSize = packet_traits<Scalar>::size
|
2009-03-09 23:16:39 +00:00
|
|
|
};
|
2010-05-30 16:00:58 -04:00
|
|
|
const Index packetedInnerSize = ((innerSize)/packetSize)*packetSize;
|
2009-03-09 23:16:39 +00:00
|
|
|
Scalar res;
|
|
|
|
|
if(packetedInnerSize)
|
|
|
|
|
{
|
2015-08-07 12:01:39 +02:00
|
|
|
PacketType packet_res = mat.template packet<Unaligned,PacketType>(0,0);
|
2010-05-30 16:00:58 -04:00
|
|
|
for(Index j=0; j<outerSize; ++j)
|
|
|
|
|
for(Index i=(j==0?packetSize:0); i<packetedInnerSize; i+=Index(packetSize))
|
2015-08-07 12:01:39 +02:00
|
|
|
packet_res = func.packetOp(packet_res, mat.template packetByOuterInner<Unaligned,PacketType>(j,i));
|
2010-02-04 18:51:29 +01:00
|
|
|
|
2009-03-09 23:16:39 +00:00
|
|
|
res = func.predux(packet_res);
|
2010-05-30 16:00:58 -04:00
|
|
|
for(Index j=0; j<outerSize; ++j)
|
|
|
|
|
for(Index i=packetedInnerSize; i<innerSize; ++i)
|
2010-02-26 21:46:43 -05:00
|
|
|
res = func(res, mat.coeffByOuterInner(j,i));
|
2009-03-09 23:16:39 +00:00
|
|
|
}
|
|
|
|
|
else // too small to vectorize anything.
|
|
|
|
|
// since this is dynamic-size hence inefficient anyway for such small sizes, don't try to optimize.
|
|
|
|
|
{
|
2010-10-25 10:15:22 -04:00
|
|
|
res = redux_impl<Func, Derived, DefaultTraversal, NoUnrolling>::run(mat, func);
|
2009-03-09 23:16:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return res;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2009-02-12 15:18:59 +00:00
|
|
|
template<typename Func, typename Derived>
|
2010-10-25 10:15:22 -04:00
|
|
|
struct redux_impl<Func, Derived, LinearVectorizedTraversal, CompleteUnrolling>
|
2009-02-12 15:18:59 +00:00
|
|
|
{
|
|
|
|
|
typedef typename Derived::Scalar Scalar;
|
2010-10-25 10:15:22 -04:00
|
|
|
typedef typename packet_traits<Scalar>::type PacketScalar;
|
2009-02-12 15:18:59 +00:00
|
|
|
enum {
|
2010-10-25 10:15:22 -04:00
|
|
|
PacketSize = packet_traits<Scalar>::size,
|
2009-02-12 15:18:59 +00:00
|
|
|
Size = Derived::SizeAtCompileTime,
|
2009-11-18 11:57:07 -05:00
|
|
|
VectorizedSize = (Size / PacketSize) * PacketSize
|
2009-02-12 15:18:59 +00:00
|
|
|
};
|
2014-10-13 17:18:26 +02:00
|
|
|
EIGEN_DEVICE_FUNC static EIGEN_STRONG_INLINE Scalar run(const Derived &mat, const Func& func)
|
2009-02-12 15:18:59 +00:00
|
|
|
{
|
2010-10-25 10:15:22 -04:00
|
|
|
eigen_assert(mat.rows()>0 && mat.cols()>0 && "you are using an empty matrix");
|
2014-01-29 11:43:05 -08:00
|
|
|
if (VectorizedSize > 0) {
|
|
|
|
|
Scalar res = func.predux(redux_vec_unroller<Func, Derived, 0, Size / PacketSize>::run(mat,func));
|
|
|
|
|
if (VectorizedSize != Size)
|
|
|
|
|
res = func(res,redux_novec_unroller<Func, Derived, VectorizedSize, Size-VectorizedSize>::run(mat,func));
|
|
|
|
|
return res;
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
return redux_novec_unroller<Func, Derived, 0, Size>::run(mat,func);
|
|
|
|
|
}
|
2009-02-12 15:18:59 +00:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2013-12-02 17:54:38 +01:00
|
|
|
// evaluator adaptor
|
2014-03-12 18:13:18 +01:00
|
|
|
template<typename _XprType>
|
2013-12-02 17:54:38 +01:00
|
|
|
class redux_evaluator
|
|
|
|
|
{
|
|
|
|
|
public:
|
2014-03-12 18:13:18 +01:00
|
|
|
typedef _XprType XprType;
|
2014-10-13 17:18:26 +02:00
|
|
|
EIGEN_DEVICE_FUNC explicit redux_evaluator(const XprType &xpr) : m_evaluator(xpr), m_xpr(xpr) {}
|
2013-12-02 17:54:38 +01:00
|
|
|
|
|
|
|
|
typedef typename XprType::Scalar Scalar;
|
|
|
|
|
typedef typename XprType::CoeffReturnType CoeffReturnType;
|
|
|
|
|
typedef typename XprType::PacketScalar PacketScalar;
|
|
|
|
|
typedef typename XprType::PacketReturnType PacketReturnType;
|
|
|
|
|
|
|
|
|
|
enum {
|
|
|
|
|
MaxRowsAtCompileTime = XprType::MaxRowsAtCompileTime,
|
|
|
|
|
MaxColsAtCompileTime = XprType::MaxColsAtCompileTime,
|
|
|
|
|
// TODO we should not remove DirectAccessBit and rather find an elegant way to query the alignment offset at runtime from the evaluator
|
2014-03-12 18:14:08 +01:00
|
|
|
Flags = evaluator<XprType>::Flags & ~DirectAccessBit,
|
2013-12-02 17:54:38 +01:00
|
|
|
IsRowMajor = XprType::IsRowMajor,
|
|
|
|
|
SizeAtCompileTime = XprType::SizeAtCompileTime,
|
|
|
|
|
InnerSizeAtCompileTime = XprType::InnerSizeAtCompileTime,
|
First part of a big refactoring of alignment control to enable the handling of arbitrarily aligned buffers. It includes:
- AlignedBit flag is deprecated. Alignment is now specified by the evaluator through the 'Alignment' enum, e.g., evaluator<Xpr>::Alignment. Its value is in Bytes.
- Add several enums to specify alignment: Aligned8, Aligned16, Aligned32, Aligned64, Aligned128. AlignedMax corresponds to EIGEN_MAX_ALIGN_BYTES. Such enums are used to define the above Alignment value, and as the 'Options' template parameter of Map<> and Ref<>.
- The Aligned enum is now deprecated. It is now an alias for Aligned16.
- Currently, traits<Matrix<>>, traits<Array<>>, traits<Ref<>>, traits<Map<>>, and traits<Block<>> also expose the Alignment enum.
2015-08-06 15:31:07 +02:00
|
|
|
CoeffReadCost = evaluator<XprType>::CoeffReadCost,
|
|
|
|
|
Alignment = evaluator<XprType>::Alignment
|
2013-12-02 17:54:38 +01:00
|
|
|
};
|
|
|
|
|
|
2014-10-13 17:18:26 +02:00
|
|
|
EIGEN_DEVICE_FUNC Index rows() const { return m_xpr.rows(); }
|
|
|
|
|
EIGEN_DEVICE_FUNC Index cols() const { return m_xpr.cols(); }
|
|
|
|
|
EIGEN_DEVICE_FUNC Index size() const { return m_xpr.size(); }
|
|
|
|
|
EIGEN_DEVICE_FUNC Index innerSize() const { return m_xpr.innerSize(); }
|
|
|
|
|
EIGEN_DEVICE_FUNC Index outerSize() const { return m_xpr.outerSize(); }
|
2013-12-02 17:54:38 +01:00
|
|
|
|
2014-10-13 17:18:26 +02:00
|
|
|
EIGEN_DEVICE_FUNC
|
2013-12-02 17:54:38 +01:00
|
|
|
CoeffReturnType coeff(Index row, Index col) const
|
|
|
|
|
{ return m_evaluator.coeff(row, col); }
|
|
|
|
|
|
2014-10-13 17:18:26 +02:00
|
|
|
EIGEN_DEVICE_FUNC
|
2013-12-02 17:54:38 +01:00
|
|
|
CoeffReturnType coeff(Index index) const
|
|
|
|
|
{ return m_evaluator.coeff(index); }
|
|
|
|
|
|
2015-08-07 12:01:39 +02:00
|
|
|
template<int LoadMode, typename PacketType>
|
2013-12-02 17:54:38 +01:00
|
|
|
PacketReturnType packet(Index row, Index col) const
|
2015-08-07 12:01:39 +02:00
|
|
|
{ return m_evaluator.template packet<LoadMode,PacketType>(row, col); }
|
2013-12-02 17:54:38 +01:00
|
|
|
|
2015-08-07 12:01:39 +02:00
|
|
|
template<int LoadMode, typename PacketType>
|
2013-12-02 17:54:38 +01:00
|
|
|
PacketReturnType packet(Index index) const
|
2015-08-07 12:01:39 +02:00
|
|
|
{ return m_evaluator.template packet<LoadMode,PacketType>(index); }
|
2013-12-02 17:54:38 +01:00
|
|
|
|
2014-10-13 17:18:26 +02:00
|
|
|
EIGEN_DEVICE_FUNC
|
2013-12-02 17:54:38 +01:00
|
|
|
CoeffReturnType coeffByOuterInner(Index outer, Index inner) const
|
|
|
|
|
{ return m_evaluator.coeff(IsRowMajor ? outer : inner, IsRowMajor ? inner : outer); }
|
|
|
|
|
|
2015-08-07 12:01:39 +02:00
|
|
|
template<int LoadMode, typename PacketType>
|
2013-12-02 17:54:38 +01:00
|
|
|
PacketReturnType packetByOuterInner(Index outer, Index inner) const
|
2015-08-07 12:01:39 +02:00
|
|
|
{ return m_evaluator.template packet<LoadMode,PacketType>(IsRowMajor ? outer : inner, IsRowMajor ? inner : outer); }
|
2013-12-02 17:54:38 +01:00
|
|
|
|
First part of a big refactoring of alignment control to enable the handling of arbitrarily aligned buffers. It includes:
- AlignedBit flag is deprecated. Alignment is now specified by the evaluator through the 'Alignment' enum, e.g., evaluator<Xpr>::Alignment. Its value is in Bytes.
- Add several enums to specify alignment: Aligned8, Aligned16, Aligned32, Aligned64, Aligned128. AlignedMax corresponds to EIGEN_MAX_ALIGN_BYTES. Such enums are used to define the above Alignment value, and as the 'Options' template parameter of Map<> and Ref<>.
- The Aligned enum is now deprecated. It is now an alias for Aligned16.
- Currently, traits<Matrix<>>, traits<Array<>>, traits<Ref<>>, traits<Map<>>, and traits<Block<>> also expose the Alignment enum.
2015-08-06 15:31:07 +02:00
|
|
|
const XprType & nestedExpression() const { return m_xpr; }
|
|
|
|
|
|
2013-12-02 17:54:38 +01:00
|
|
|
protected:
|
2015-09-02 22:10:39 +02:00
|
|
|
internal::evaluator<XprType> m_evaluator;
|
2013-12-02 17:54:38 +01:00
|
|
|
const XprType &m_xpr;
|
|
|
|
|
};
|
|
|
|
|
|
2010-10-25 10:15:22 -04:00
|
|
|
} // end namespace internal
|
|
|
|
|
|
|
|
|
|
/***************************************************************************
|
|
|
|
|
* Part 4 : public API
|
|
|
|
|
***************************************************************************/
|
|
|
|
|
|
2009-02-12 15:18:59 +00:00
|
|
|
|
2008-03-16 14:36:25 +00:00
|
|
|
/** \returns the result of a full redux operation on the whole matrix or vector using \a func
|
|
|
|
|
*
|
|
|
|
|
* The template parameter \a BinaryOp is the type of the functor \a func which must be
|
2013-12-02 17:54:38 +01:00
|
|
|
* an associative operator. Both current C++98 and C++11 functor styles are handled.
|
2008-03-16 14:36:25 +00:00
|
|
|
*
|
2009-12-04 23:17:14 +01:00
|
|
|
* \sa DenseBase::sum(), DenseBase::minCoeff(), DenseBase::maxCoeff(), MatrixBase::colwise(), MatrixBase::rowwise()
|
2008-03-16 14:36:25 +00:00
|
|
|
*/
|
|
|
|
|
template<typename Derived>
|
2009-02-12 15:18:59 +00:00
|
|
|
template<typename Func>
|
2015-06-15 22:40:18 +02:00
|
|
|
typename internal::traits<Derived>::Scalar
|
2009-12-04 23:17:14 +01:00
|
|
|
DenseBase<Derived>::redux(const Func& func) const
|
2008-03-16 14:36:25 +00:00
|
|
|
{
|
2013-12-02 17:54:38 +01:00
|
|
|
eigen_assert(this->rows()>0 && this->cols()>0 && "you are using an empty matrix");
|
2015-10-09 12:07:42 +02:00
|
|
|
|
2013-12-02 17:54:38 +01:00
|
|
|
typedef typename internal::redux_evaluator<Derived> ThisEvaluator;
|
|
|
|
|
ThisEvaluator thisEval(derived());
|
2014-03-12 13:34:11 +01:00
|
|
|
|
2013-12-02 17:54:38 +01:00
|
|
|
return internal::redux_impl<Func, ThisEvaluator>::run(thisEval, func);
|
2008-03-16 14:36:25 +00:00
|
|
|
}
|
|
|
|
|
|
2013-04-09 11:27:54 +02:00
|
|
|
/** \returns the minimum of all coefficients of \c *this.
|
|
|
|
|
* \warning the result is undefined if \c *this contains NaN.
|
2008-03-16 14:36:25 +00:00
|
|
|
*/
|
|
|
|
|
template<typename Derived>
|
2010-10-25 10:15:22 -04:00
|
|
|
EIGEN_STRONG_INLINE typename internal::traits<Derived>::Scalar
|
2009-12-04 23:17:14 +01:00
|
|
|
DenseBase<Derived>::minCoeff() const
|
2008-03-16 14:36:25 +00:00
|
|
|
{
|
2014-08-01 17:00:20 +02:00
|
|
|
return derived().redux(Eigen::internal::scalar_min_op<Scalar>());
|
2008-03-16 14:36:25 +00:00
|
|
|
}
|
|
|
|
|
|
2013-04-09 11:27:54 +02:00
|
|
|
/** \returns the maximum of all coefficients of \c *this.
|
|
|
|
|
* \warning the result is undefined if \c *this contains NaN.
|
2008-03-16 14:36:25 +00:00
|
|
|
*/
|
|
|
|
|
template<typename Derived>
|
2010-10-25 10:15:22 -04:00
|
|
|
EIGEN_STRONG_INLINE typename internal::traits<Derived>::Scalar
|
2009-12-04 23:17:14 +01:00
|
|
|
DenseBase<Derived>::maxCoeff() const
|
2008-03-16 14:36:25 +00:00
|
|
|
{
|
2014-08-01 17:00:20 +02:00
|
|
|
return derived().redux(Eigen::internal::scalar_max_op<Scalar>());
|
2008-03-16 14:36:25 +00:00
|
|
|
}
|
|
|
|
|
|
2009-02-12 15:18:59 +00:00
|
|
|
/** \returns the sum of all coefficients of *this
|
|
|
|
|
*
|
2009-10-29 19:56:58 +01:00
|
|
|
* \sa trace(), prod(), mean()
|
2009-02-12 15:18:59 +00:00
|
|
|
*/
|
|
|
|
|
template<typename Derived>
|
2010-10-25 10:15:22 -04:00
|
|
|
EIGEN_STRONG_INLINE typename internal::traits<Derived>::Scalar
|
2009-12-04 23:17:14 +01:00
|
|
|
DenseBase<Derived>::sum() const
|
2009-02-12 15:18:59 +00:00
|
|
|
{
|
2010-07-16 14:02:20 +02:00
|
|
|
if(SizeAtCompileTime==0 || (SizeAtCompileTime==Dynamic && size()==0))
|
|
|
|
|
return Scalar(0);
|
2014-08-01 17:00:20 +02:00
|
|
|
return derived().redux(Eigen::internal::scalar_sum_op<Scalar>());
|
2009-02-12 15:18:59 +00:00
|
|
|
}
|
|
|
|
|
|
2009-10-29 19:56:58 +01:00
|
|
|
/** \returns the mean of all coefficients of *this
|
|
|
|
|
*
|
|
|
|
|
* \sa trace(), prod(), sum()
|
|
|
|
|
*/
|
|
|
|
|
template<typename Derived>
|
2010-10-25 10:15:22 -04:00
|
|
|
EIGEN_STRONG_INLINE typename internal::traits<Derived>::Scalar
|
2009-12-04 23:17:14 +01:00
|
|
|
DenseBase<Derived>::mean() const
|
2009-10-29 19:56:58 +01:00
|
|
|
{
|
2014-08-01 17:00:20 +02:00
|
|
|
return Scalar(derived().redux(Eigen::internal::scalar_sum_op<Scalar>())) / Scalar(this->size());
|
2009-10-29 19:56:58 +01:00
|
|
|
}
|
|
|
|
|
|
2009-02-12 15:18:59 +00:00
|
|
|
/** \returns the product of all coefficients of *this
|
|
|
|
|
*
|
|
|
|
|
* Example: \include MatrixBase_prod.cpp
|
|
|
|
|
* Output: \verbinclude MatrixBase_prod.out
|
|
|
|
|
*
|
2009-10-29 19:56:58 +01:00
|
|
|
* \sa sum(), mean(), trace()
|
2009-02-12 15:18:59 +00:00
|
|
|
*/
|
|
|
|
|
template<typename Derived>
|
2010-10-25 10:15:22 -04:00
|
|
|
EIGEN_STRONG_INLINE typename internal::traits<Derived>::Scalar
|
2009-12-04 23:17:14 +01:00
|
|
|
DenseBase<Derived>::prod() const
|
2009-02-12 15:18:59 +00:00
|
|
|
{
|
2010-07-16 14:02:20 +02:00
|
|
|
if(SizeAtCompileTime==0 || (SizeAtCompileTime==Dynamic && size()==0))
|
|
|
|
|
return Scalar(1);
|
2014-08-01 17:00:20 +02:00
|
|
|
return derived().redux(Eigen::internal::scalar_product_op<Scalar>());
|
2009-02-12 15:18:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** \returns the trace of \c *this, i.e. the sum of the coefficients on the main diagonal.
|
|
|
|
|
*
|
|
|
|
|
* \c *this can be any matrix, not necessarily square.
|
|
|
|
|
*
|
|
|
|
|
* \sa diagonal(), sum()
|
|
|
|
|
*/
|
|
|
|
|
template<typename Derived>
|
2010-10-25 10:15:22 -04:00
|
|
|
EIGEN_STRONG_INLINE typename internal::traits<Derived>::Scalar
|
2009-02-12 15:18:59 +00:00
|
|
|
MatrixBase<Derived>::trace() const
|
|
|
|
|
{
|
2010-02-04 18:51:29 +01:00
|
|
|
return derived().diagonal().sum();
|
2009-02-12 15:18:59 +00:00
|
|
|
}
|
|
|
|
|
|
2012-04-15 11:06:28 +01:00
|
|
|
} // end namespace Eigen
|
|
|
|
|
|
2008-03-16 14:36:25 +00:00
|
|
|
#endif // EIGEN_REDUX_H
|