2007-10-12 05:15:25 +00:00
|
|
|
// This file is part of Eigen, a lightweight C++ template library
|
|
|
|
|
// for linear algebra. Eigen itself is part of the KDE project.
|
2007-10-01 17:07:38 +00:00
|
|
|
//
|
|
|
|
|
// Copyright (C) 2007 Michael Olbrich <michael.olbrich@gmx.net>
|
2008-01-07 09:34:21 +00:00
|
|
|
// Copyright (C) 2006-2008 Benoit Jacob <jacob@math.jussieu.fr>
|
2008-04-11 08:18:47 +00:00
|
|
|
// Copyright (C) 2008 Gael Guennebaud <g.gael@free.fr>
|
2007-10-01 17:07:38 +00:00
|
|
|
//
|
2008-02-28 15:44:45 +00:00
|
|
|
// Eigen is free software; you can redistribute it and/or
|
|
|
|
|
// modify it under the terms of the GNU Lesser General Public
|
2008-03-05 13:18:19 +00:00
|
|
|
// License as published by the Free Software Foundation; either
|
2008-02-28 15:44:45 +00:00
|
|
|
// version 3 of the License, or (at your option) any later version.
|
|
|
|
|
//
|
|
|
|
|
// Alternatively, you can redistribute it and/or
|
|
|
|
|
// modify it under the terms of the GNU General Public License as
|
2008-03-05 13:18:19 +00:00
|
|
|
// published by the Free Software Foundation; either version 2 of
|
2008-02-28 15:44:45 +00:00
|
|
|
// the License, or (at your option) any later version.
|
2007-10-01 17:07:38 +00:00
|
|
|
//
|
2007-10-12 05:15:25 +00:00
|
|
|
// Eigen is distributed in the hope that it will be useful, but WITHOUT ANY
|
2007-10-01 17:07:38 +00:00
|
|
|
// WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
2008-02-28 15:44:45 +00:00
|
|
|
// FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License or the
|
|
|
|
|
// GNU General Public License for more details.
|
2007-10-01 17:07:38 +00:00
|
|
|
//
|
2008-03-05 13:18:19 +00:00
|
|
|
// You should have received a copy of the GNU Lesser General Public
|
2008-02-28 15:44:45 +00:00
|
|
|
// License and a copy of the GNU General Public License along with
|
|
|
|
|
// Eigen. If not, see <http://www.gnu.org/licenses/>.
|
2007-10-01 17:07:38 +00:00
|
|
|
|
2008-04-10 09:01:28 +00:00
|
|
|
#ifndef EIGEN_ASSIGN_H
|
|
|
|
|
#define EIGEN_ASSIGN_H
|
2007-10-01 17:07:38 +00:00
|
|
|
|
2008-01-06 13:57:29 +00:00
|
|
|
template<typename Derived1, typename Derived2, int UnrollCount>
|
2008-04-11 14:28:42 +00:00
|
|
|
struct ei_matrix_assignment_unroller
|
2007-10-01 17:07:38 +00:00
|
|
|
{
|
2008-01-10 20:45:35 +00:00
|
|
|
enum {
|
2008-03-12 17:17:36 +00:00
|
|
|
col = (UnrollCount-1) / Derived1::RowsAtCompileTime,
|
|
|
|
|
row = (UnrollCount-1) % Derived1::RowsAtCompileTime
|
2008-01-10 20:45:35 +00:00
|
|
|
};
|
2007-10-01 17:07:38 +00:00
|
|
|
|
2007-10-08 07:17:54 +00:00
|
|
|
static void run(Derived1 &dst, const Derived2 &src)
|
|
|
|
|
{
|
2008-04-11 14:28:42 +00:00
|
|
|
ei_matrix_assignment_unroller<Derived1, Derived2, UnrollCount-1>::run(dst, src);
|
2007-12-17 07:25:11 +00:00
|
|
|
dst.coeffRef(row, col) = src.coeff(row, col);
|
2007-10-08 07:17:54 +00:00
|
|
|
}
|
2007-10-01 17:07:38 +00:00
|
|
|
};
|
|
|
|
|
|
2008-01-06 13:57:29 +00:00
|
|
|
template<typename Derived1, typename Derived2>
|
2008-04-11 14:28:42 +00:00
|
|
|
struct ei_matrix_assignment_unroller<Derived1, Derived2, 1>
|
2007-10-01 17:07:38 +00:00
|
|
|
{
|
2007-10-08 07:17:54 +00:00
|
|
|
static void run(Derived1 &dst, const Derived2 &src)
|
|
|
|
|
{
|
2007-12-17 07:25:11 +00:00
|
|
|
dst.coeffRef(0, 0) = src.coeff(0, 0);
|
2007-10-08 07:17:54 +00:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2008-01-05 10:57:14 +00:00
|
|
|
// prevent buggy user code from causing an infinite recursion
|
2008-01-06 13:57:29 +00:00
|
|
|
template<typename Derived1, typename Derived2>
|
2008-04-11 14:28:42 +00:00
|
|
|
struct ei_matrix_assignment_unroller<Derived1, Derived2, 0>
|
2008-01-05 10:57:14 +00:00
|
|
|
{
|
|
|
|
|
static void run(Derived1 &, const Derived2 &) {}
|
|
|
|
|
};
|
|
|
|
|
|
2008-01-06 13:57:29 +00:00
|
|
|
template<typename Derived1, typename Derived2>
|
2008-04-11 14:28:42 +00:00
|
|
|
struct ei_matrix_assignment_unroller<Derived1, Derived2, Dynamic>
|
2007-12-24 11:14:25 +00:00
|
|
|
{
|
|
|
|
|
static void run(Derived1 &, const Derived2 &) {}
|
|
|
|
|
};
|
|
|
|
|
|
2008-04-09 12:31:55 +00:00
|
|
|
//----
|
|
|
|
|
|
2008-04-09 18:04:26 +00:00
|
|
|
template<typename Derived1, typename Derived2, int Index>
|
2008-04-11 14:28:42 +00:00
|
|
|
struct ei_matrix_assignment_packet_unroller
|
2008-04-09 12:31:55 +00:00
|
|
|
{
|
|
|
|
|
enum {
|
2008-04-09 18:04:26 +00:00
|
|
|
row = Derived1::Flags&RowMajorBit ? Index / Derived1::ColsAtCompileTime : Index % Derived1::RowsAtCompileTime,
|
|
|
|
|
col = Derived1::Flags&RowMajorBit ? Index % Derived1::ColsAtCompileTime : Index / Derived1::RowsAtCompileTime
|
2008-04-09 12:31:55 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
static void run(Derived1 &dst, const Derived2 &src)
|
|
|
|
|
{
|
2008-04-11 14:28:42 +00:00
|
|
|
ei_matrix_assignment_packet_unroller<Derived1, Derived2,
|
2008-04-09 18:04:26 +00:00
|
|
|
Index-ei_packet_traits<typename Derived1::Scalar>::size>::run(dst, src);
|
2008-04-09 12:31:55 +00:00
|
|
|
dst.writePacketCoeff(row, col, src.packetCoeff(row, col));
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
template<typename Derived1, typename Derived2>
|
2008-04-11 14:28:42 +00:00
|
|
|
struct ei_matrix_assignment_packet_unroller<Derived1, Derived2, 0 >
|
2008-04-09 12:31:55 +00:00
|
|
|
{
|
|
|
|
|
static void run(Derived1 &dst, const Derived2 &src)
|
|
|
|
|
{
|
|
|
|
|
dst.writePacketCoeff(0, 0, src.packetCoeff(0, 0));
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
template<typename Derived1, typename Derived2>
|
2008-04-11 14:28:42 +00:00
|
|
|
struct ei_matrix_assignment_packet_unroller<Derived1, Derived2, Dynamic>
|
2008-04-09 12:31:55 +00:00
|
|
|
{
|
2008-04-11 14:28:42 +00:00
|
|
|
static void run(Derived1 &, const Derived2 &) { ei_internal_assert(false && "ei_matrix_assignment_packet_unroller"); }
|
2007-10-01 17:07:38 +00:00
|
|
|
};
|
|
|
|
|
|
2008-04-09 12:31:55 +00:00
|
|
|
template <typename Derived, typename OtherDerived,
|
|
|
|
|
bool Vectorize = (Derived::Flags & OtherDerived::Flags & VectorizableBit)
|
|
|
|
|
&& ((Derived::Flags&RowMajorBit)==(OtherDerived::Flags&RowMajorBit))>
|
2008-04-11 14:28:42 +00:00
|
|
|
struct ei_assignment_impl;
|
2008-04-09 12:31:55 +00:00
|
|
|
|
2008-03-10 17:23:11 +00:00
|
|
|
template<typename Derived>
|
2007-10-07 16:40:49 +00:00
|
|
|
template<typename OtherDerived>
|
2008-03-10 17:23:11 +00:00
|
|
|
Derived& MatrixBase<Derived>
|
2008-04-05 11:10:54 +00:00
|
|
|
::lazyAssign(const MatrixBase<OtherDerived>& other)
|
2007-10-07 16:40:49 +00:00
|
|
|
{
|
2008-04-11 14:28:42 +00:00
|
|
|
ei_assignment_impl<Derived,OtherDerived>::execute(derived(),other.derived());
|
2008-04-09 12:31:55 +00:00
|
|
|
return derived();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template<typename Derived>
|
|
|
|
|
template<typename OtherDerived>
|
|
|
|
|
Derived& MatrixBase<Derived>
|
|
|
|
|
::operator=(const MatrixBase<OtherDerived>& other)
|
|
|
|
|
{
|
2008-04-11 14:28:42 +00:00
|
|
|
const bool need_to_transpose = Derived::IsVectorAtCompileTime
|
|
|
|
|
&& OtherDerived::IsVectorAtCompileTime
|
|
|
|
|
&& (int)Derived::RowsAtCompileTime != (int)OtherDerived::RowsAtCompileTime;
|
2008-04-09 12:31:55 +00:00
|
|
|
if(OtherDerived::Flags & EvalBeforeAssigningBit)
|
2007-12-24 11:14:25 +00:00
|
|
|
{
|
2008-04-11 14:28:42 +00:00
|
|
|
if(need_to_transpose)
|
|
|
|
|
return lazyAssign(other.transpose().eval());
|
|
|
|
|
else
|
|
|
|
|
return lazyAssign(other.eval());
|
2007-12-24 11:14:25 +00:00
|
|
|
}
|
2008-04-09 12:31:55 +00:00
|
|
|
else
|
2008-04-11 14:28:42 +00:00
|
|
|
{
|
|
|
|
|
if(need_to_transpose)
|
|
|
|
|
return lazyAssign(other.transpose());
|
|
|
|
|
else
|
|
|
|
|
return lazyAssign(other.derived());
|
|
|
|
|
}
|
2008-04-09 12:31:55 +00:00
|
|
|
}
|
|
|
|
|
|
2008-04-14 08:20:24 +00:00
|
|
|
template<typename T1, typename T2> bool ei_should_parallelize_assignment(const T1& t, const T2&)
|
|
|
|
|
{
|
|
|
|
|
return (T1::Flags & T2::Flags & LargeBit) && t.size() >= EIGEN_PARALLELIZATION_TRESHOLD;
|
|
|
|
|
}
|
|
|
|
|
|
2008-04-09 12:31:55 +00:00
|
|
|
template <typename Derived, typename OtherDerived>
|
2008-04-11 14:28:42 +00:00
|
|
|
struct ei_assignment_impl<Derived, OtherDerived, false>
|
2008-04-09 12:31:55 +00:00
|
|
|
{
|
|
|
|
|
static void execute(Derived & dst, const OtherDerived & src)
|
2007-12-24 11:14:25 +00:00
|
|
|
{
|
2008-04-09 12:31:55 +00:00
|
|
|
const bool unroll = Derived::SizeAtCompileTime * OtherDerived::CoeffReadCost <= EIGEN_UNROLLING_LIMIT;
|
2008-04-11 14:28:42 +00:00
|
|
|
ei_assert(dst.rows() == src.rows() && dst.cols() == src.cols());
|
|
|
|
|
if(unroll)
|
2008-01-11 15:08:04 +00:00
|
|
|
{
|
2008-04-11 14:28:42 +00:00
|
|
|
ei_matrix_assignment_unroller
|
|
|
|
|
<Derived, OtherDerived,
|
|
|
|
|
unroll ? Derived::SizeAtCompileTime : Dynamic
|
|
|
|
|
>::run(dst.derived(), src.derived());
|
2008-01-11 15:08:04 +00:00
|
|
|
}
|
2008-04-11 14:28:42 +00:00
|
|
|
else
|
2008-01-11 15:08:04 +00:00
|
|
|
{
|
2008-04-11 14:28:42 +00:00
|
|
|
if(Derived::ColsAtCompileTime == Dynamic || Derived::RowsAtCompileTime != Dynamic)
|
2008-01-11 15:08:04 +00:00
|
|
|
{
|
2008-04-11 14:28:42 +00:00
|
|
|
#define EIGEN_THE_PARALLELIZABLE_LOOP \
|
|
|
|
|
for(int j = 0; j < dst.cols(); j++) \
|
|
|
|
|
for(int i = 0; i < dst.rows(); i++) \
|
|
|
|
|
dst.coeffRef(i, j) = src.coeff(i, j);
|
2008-04-14 08:20:24 +00:00
|
|
|
EIGEN_RUN_PARALLELIZABLE_LOOP(ei_should_parallelize_assignment(dst, src))
|
2008-04-11 14:28:42 +00:00
|
|
|
#undef EIGEN_THE_PARALLELIZABLE_LOOP
|
2008-01-11 15:08:04 +00:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2008-04-11 14:28:42 +00:00
|
|
|
// traverse in row-major order
|
|
|
|
|
// in order to allow the compiler to unroll the inner loop
|
|
|
|
|
#define EIGEN_THE_PARALLELIZABLE_LOOP \
|
|
|
|
|
for(int i = 0; i < dst.rows(); i++) \
|
|
|
|
|
for(int j = 0; j < dst.cols(); j++) \
|
|
|
|
|
dst.coeffRef(i, j) = src.coeff(i, j);
|
2008-04-14 08:20:24 +00:00
|
|
|
EIGEN_RUN_PARALLELIZABLE_LOOP(ei_should_parallelize_assignment(dst, src))
|
2008-04-11 14:28:42 +00:00
|
|
|
#undef EIGEN_THE_PARALLELIZABLE_LOOP
|
2008-01-11 15:08:04 +00:00
|
|
|
}
|
|
|
|
|
}
|
2007-12-24 11:14:25 +00:00
|
|
|
}
|
2008-04-09 12:31:55 +00:00
|
|
|
};
|
2008-04-05 11:10:54 +00:00
|
|
|
|
2008-04-09 12:31:55 +00:00
|
|
|
template <typename Derived, typename OtherDerived>
|
2008-04-11 14:28:42 +00:00
|
|
|
struct ei_assignment_impl<Derived, OtherDerived, true>
|
2008-04-05 11:10:54 +00:00
|
|
|
{
|
2008-04-09 12:31:55 +00:00
|
|
|
static void execute(Derived & dst, const OtherDerived & src)
|
2008-04-05 11:10:54 +00:00
|
|
|
{
|
2008-04-09 12:31:55 +00:00
|
|
|
const bool unroll = Derived::SizeAtCompileTime * OtherDerived::CoeffReadCost <= EIGEN_UNROLLING_LIMIT;
|
|
|
|
|
ei_assert(dst.rows() == src.rows() && dst.cols() == src.cols());
|
|
|
|
|
if(unroll)
|
|
|
|
|
{
|
2008-04-11 14:28:42 +00:00
|
|
|
ei_matrix_assignment_packet_unroller
|
2008-04-09 12:31:55 +00:00
|
|
|
<Derived, OtherDerived,
|
2008-04-09 18:04:26 +00:00
|
|
|
unroll && int(Derived::SizeAtCompileTime)>=ei_packet_traits<typename Derived::Scalar>::size
|
|
|
|
|
? Derived::SizeAtCompileTime-ei_packet_traits<typename Derived::Scalar>::size
|
|
|
|
|
: Dynamic>::run(dst.const_cast_derived(), src.derived());
|
2008-04-09 12:31:55 +00:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
if(OtherDerived::Flags&RowMajorBit)
|
|
|
|
|
{
|
2008-04-11 14:28:42 +00:00
|
|
|
#define EIGEN_THE_PARALLELIZABLE_LOOP \
|
|
|
|
|
for(int i = 0; i < dst.rows(); i++) \
|
|
|
|
|
for(int j = 0; j < dst.cols(); j+=ei_packet_traits<typename Derived::Scalar>::size) \
|
2008-04-09 12:31:55 +00:00
|
|
|
dst.writePacketCoeff(i, j, src.packetCoeff(i, j));
|
2008-04-14 08:20:24 +00:00
|
|
|
EIGEN_RUN_PARALLELIZABLE_LOOP(ei_should_parallelize_assignment(dst, src))
|
2008-04-11 14:28:42 +00:00
|
|
|
#undef EIGEN_THE_PARALLELIZABLE_LOOP
|
2008-04-09 12:31:55 +00:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2008-04-11 14:28:42 +00:00
|
|
|
#define EIGEN_THE_PARALLELIZABLE_LOOP \
|
|
|
|
|
for(int j = 0; j < dst.cols(); j++) \
|
|
|
|
|
for(int i = 0; i < dst.rows(); i+=ei_packet_traits<typename Derived::Scalar>::size) \
|
2008-04-09 12:31:55 +00:00
|
|
|
dst.writePacketCoeff(i, j, src.packetCoeff(i, j));
|
2008-04-14 08:20:24 +00:00
|
|
|
EIGEN_RUN_PARALLELIZABLE_LOOP(ei_should_parallelize_assignment(dst, src))
|
2008-04-11 14:28:42 +00:00
|
|
|
#undef EIGEN_THE_PARALLELIZABLE_LOOP
|
2008-04-09 12:31:55 +00:00
|
|
|
}
|
|
|
|
|
}
|
2008-04-05 11:10:54 +00:00
|
|
|
}
|
2008-04-09 12:31:55 +00:00
|
|
|
};
|
2007-10-07 16:40:49 +00:00
|
|
|
|
2008-04-10 09:01:28 +00:00
|
|
|
#endif // EIGEN_ASSIGN_H
|