diff --git a/Eigen/Core b/Eigen/Core index 743e21bd7..6a315b09f 100644 --- a/Eigen/Core +++ b/Eigen/Core @@ -10,7 +10,7 @@ #endif #endif -#ifndef EIGEN_DONT_USE_OPENMP +#ifndef EIGEN_DONT_PARALLELIZE #ifdef _OPENMP #define EIGEN_USE_OPENMP #include diff --git a/Eigen/LU b/Eigen/LU new file mode 100644 index 000000000..9d92196d5 --- /dev/null +++ b/Eigen/LU @@ -0,0 +1,12 @@ +#ifndef EIGEN_LU_H +#define EIGEN_LU_H + +#include "Core" + +namespace Eigen { + +#include "Eigen/src/LU/Inverse.h" + +} // namespace Eigen + +#endif // EIGEN_LU_H diff --git a/Eigen/src/CMakeLists.txt b/Eigen/src/CMakeLists.txt index 941dff553..e811772e5 100644 --- a/Eigen/src/CMakeLists.txt +++ b/Eigen/src/CMakeLists.txt @@ -1 +1,2 @@ ADD_SUBDIRECTORY(Core) +ADD_SUBDIRECTORY(LU) diff --git a/Eigen/src/Core/Assign.h b/Eigen/src/Core/Assign.h index 37ad2c0cf..1b6e928d2 100644 --- a/Eigen/src/Core/Assign.h +++ b/Eigen/src/Core/Assign.h @@ -135,6 +135,11 @@ Derived& MatrixBase } } +template bool ei_should_parallelize_assignment(const T1& t, const T2&) +{ + return (T1::Flags & T2::Flags & LargeBit) && t.size() >= EIGEN_PARALLELIZATION_TRESHOLD; +} + template struct ei_assignment_impl { @@ -157,7 +162,7 @@ struct ei_assignment_impl for(int j = 0; j < dst.cols(); j++) \ for(int i = 0; i < dst.rows(); i++) \ dst.coeffRef(i, j) = src.coeff(i, j); - EIGEN_RUN_PARALLELIZABLE_LOOP(Derived::Flags & OtherDerived::Flags & LargeBit) + EIGEN_RUN_PARALLELIZABLE_LOOP(ei_should_parallelize_assignment(dst, src)) #undef EIGEN_THE_PARALLELIZABLE_LOOP } else @@ -168,7 +173,7 @@ struct ei_assignment_impl for(int i = 0; i < dst.rows(); i++) \ for(int j = 0; j < dst.cols(); j++) \ dst.coeffRef(i, j) = src.coeff(i, j); - EIGEN_RUN_PARALLELIZABLE_LOOP(Derived::Flags & OtherDerived::Flags & LargeBit) + EIGEN_RUN_PARALLELIZABLE_LOOP(ei_should_parallelize_assignment(dst, src)) #undef EIGEN_THE_PARALLELIZABLE_LOOP } } @@ -198,7 +203,7 @@ struct ei_assignment_impl for(int i = 0; i < dst.rows(); i++) \ for(int j = 0; j < dst.cols(); j+=ei_packet_traits::size) \ dst.writePacketCoeff(i, j, src.packetCoeff(i, j)); - EIGEN_RUN_PARALLELIZABLE_LOOP(Derived::Flags & OtherDerived::Flags & LargeBit) + EIGEN_RUN_PARALLELIZABLE_LOOP(ei_should_parallelize_assignment(dst, src)) #undef EIGEN_THE_PARALLELIZABLE_LOOP } else @@ -207,7 +212,7 @@ struct ei_assignment_impl for(int j = 0; j < dst.cols(); j++) \ for(int i = 0; i < dst.rows(); i+=ei_packet_traits::size) \ dst.writePacketCoeff(i, j, src.packetCoeff(i, j)); - EIGEN_RUN_PARALLELIZABLE_LOOP(Derived::Flags & OtherDerived::Flags & LargeBit) + EIGEN_RUN_PARALLELIZABLE_LOOP(ei_should_parallelize_assignment(dst, src)) #undef EIGEN_THE_PARALLELIZABLE_LOOP } } diff --git a/Eigen/src/Core/Block.h b/Eigen/src/Core/Block.h index c026b174c..0ff72075e 100644 --- a/Eigen/src/Core/Block.h +++ b/Eigen/src/Core/Block.h @@ -67,11 +67,11 @@ struct ei_traits > : (BlockRows==Dynamic ? MatrixType::MaxRowsAtCompileTime : BlockRows), MaxColsAtCompileTime = ColsAtCompileTime == 1 ? 1 : (BlockCols==Dynamic ? MatrixType::MaxColsAtCompileTime : BlockCols), - FlagsLargeBit = ((RowsAtCompileTime != Dynamic && MatrixType::RowsAtCompileTime == Dynamic) - || (ColsAtCompileTime != Dynamic && MatrixType::ColsAtCompileTime == Dynamic)) - ? ~LargeBit - : ~(unsigned int)0, - Flags = MatrixType::Flags & FlagsLargeBit & ~VectorizableBit, + FlagsMaskLargeBit = ((RowsAtCompileTime != Dynamic && MatrixType::RowsAtCompileTime == Dynamic) + || (ColsAtCompileTime != Dynamic && MatrixType::ColsAtCompileTime == Dynamic)) + ? ~LargeBit + : ~(unsigned int)0, + Flags = MatrixType::Flags & FlagsMaskLargeBit & ~VectorizableBit, CoeffReadCost = MatrixType::CoeffReadCost }; }; diff --git a/Eigen/src/Core/Matrix.h b/Eigen/src/Core/Matrix.h index e79cbcbfb..2f65413da 100644 --- a/Eigen/src/Core/Matrix.h +++ b/Eigen/src/Core/Matrix.h @@ -95,11 +95,8 @@ struct ei_traits > }; }; -template -class Matrix : public MatrixBase > +template +class Matrix : public MatrixBase > { public: diff --git a/Eigen/src/Core/MatrixBase.h b/Eigen/src/Core/MatrixBase.h index 151ee74d9..d5912c9e6 100644 --- a/Eigen/src/Core/MatrixBase.h +++ b/Eigen/src/Core/MatrixBase.h @@ -506,6 +506,15 @@ template class MatrixBase { return *static_cast(const_cast(this)); } //@} + /** \name LU module + * + * \code #include \endcode + */ + //@{ + const Inverse inverse() const; + const Inverse quickInverse() const; + //@} + private: PacketScalar _packetCoeff(int , int) const { ei_internal_assert(false && "_packetCoeff not defined"); } diff --git a/Eigen/src/Core/Product.h b/Eigen/src/Core/Product.h index 6771b5d30..b593825f8 100644 --- a/Eigen/src/Core/Product.h +++ b/Eigen/src/Core/Product.h @@ -280,6 +280,8 @@ void Product::_cacheOptimalEval(DestDerived& res) const { res.setZero(); const int cols4 = m_lhs.cols() & 0xfffffffC; + const bool should_parallelize = (Flags & DestDerived::Flags & LargeBit) + && res.size() >= EIGEN_PARALLELIZATION_TRESHOLD; #ifdef EIGEN_VECTORIZE if( (Flags & VectorizableBit) && (!(Lhs::Flags & RowMajorBit)) ) { @@ -318,7 +320,7 @@ void Product::_cacheOptimalEval(DestDerived& res) const res.writePacketCoeff(i,k,ei_pmul(tmp, m_lhs.packetCoeff(i,j))); \ } \ } - EIGEN_RUN_PARALLELIZABLE_LOOP(Flags & DestDerived::Flags & LargeBit) + EIGEN_RUN_PARALLELIZABLE_LOOP(should_parallelize) #undef EIGEN_THE_PARALLELIZABLE_LOOP } else @@ -345,7 +347,7 @@ void Product::_cacheOptimalEval(DestDerived& res) const res.coeffRef(i,k) += tmp * m_lhs.coeff(i,j); \ } \ } - EIGEN_RUN_PARALLELIZABLE_LOOP(Flags & DestDerived::Flags & LargeBit) + EIGEN_RUN_PARALLELIZABLE_LOOP(should_parallelize) #undef EIGEN_THE_PARALLELIZABLE_LOOP } } diff --git a/Eigen/src/Core/util/ForwardDeclarations.h b/Eigen/src/Core/util/ForwardDeclarations.h index 6167f13bd..86eaf4e54 100644 --- a/Eigen/src/Core/util/ForwardDeclarations.h +++ b/Eigen/src/Core/util/ForwardDeclarations.h @@ -29,7 +29,11 @@ template struct ei_traits; template struct ei_product_eval_mode; template struct NumTraits; -template class Matrix; +template +class Matrix; + template class Lazy; template class Temporary; template class Minor; @@ -47,7 +51,6 @@ template class DiagonalCoeffs; template class Identity; template class Map; template class Eval; -template class EvalOMP; template class PartialRedux; template struct ei_scalar_sum_op; @@ -70,4 +73,6 @@ template struct ei_scalar_quotient1_op; template struct ei_scalar_min_op; template struct ei_scalar_max_op; +template class Inverse; + #endif // EIGEN_FORWARDDECLARATIONS_H diff --git a/Eigen/src/Core/util/Macros.h b/Eigen/src/Core/util/Macros.h index 43d595451..fad046766 100644 --- a/Eigen/src/Core/util/Macros.h +++ b/Eigen/src/Core/util/Macros.h @@ -37,6 +37,10 @@ #define EIGEN_UNROLLING_LIMIT 400 #endif +#ifndef EIGEN_PARALLELIZATION_TRESHOLD +#define EIGEN_PARALLELIZATION_TRESHOLD 2000 +#endif + #ifdef EIGEN_DEFAULT_TO_ROW_MAJOR #define EIGEN_DEFAULT_MATRIX_STORAGE_ORDER RowMajorBit #else diff --git a/Eigen/src/Core/util/Meta.h b/Eigen/src/Core/util/Meta.h index faf177473..4939128eb 100644 --- a/Eigen/src/Core/util/Meta.h +++ b/Eigen/src/Core/util/Meta.h @@ -182,5 +182,4 @@ template struct ei_packet_traits enum {size=1}; }; - #endif // EIGEN_META_H diff --git a/Eigen/src/LU/CMakeLists.txt b/Eigen/src/LU/CMakeLists.txt new file mode 100644 index 000000000..4af65430f --- /dev/null +++ b/Eigen/src/LU/CMakeLists.txt @@ -0,0 +1,6 @@ +FILE(GLOB Eigen_LU_SRCS "*.h") + +INSTALL(FILES + ${Eigen_LU_SRCS} + DESTINATION ${INCLUDE_INSTALL_DIR}/Eigen/src/LU + ) diff --git a/Eigen/src/LU/Inverse.h b/Eigen/src/LU/Inverse.h new file mode 100644 index 000000000..4a0cd9f40 --- /dev/null +++ b/Eigen/src/LU/Inverse.h @@ -0,0 +1,296 @@ +// This file is part of Eigen, a lightweight C++ template library +// for linear algebra. Eigen itself is part of the KDE project. +// +// Copyright (C) 2008 Benoit Jacob +// +// Eigen is free software; you can redistribute it and/or +// modify it under the terms of the GNU Lesser General Public +// License as published by the Free Software Foundation; either +// 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 +// published by the Free Software Foundation; either version 2 of +// the License, or (at your option) any later version. +// +// Eigen is distributed in the hope that it will be useful, but WITHOUT ANY +// WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS +// FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License or the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public +// License and a copy of the GNU General Public License along with +// Eigen. If not, see . + +#ifndef EIGEN_INVERSE_H +#define EIGEN_INVERSE_H + +/** \class Inverse + * + * \brief Inverse of a matrix + * + * \param ExpressionType the type of the matrix/expression of which we are taking the inverse + * \param CheckExistence whether or not to check the existence of the inverse while computing it + * + * This class represents the inverse of a matrix. It is the return + * type of MatrixBase::inverse() and most of the time this is the only way it + * is used. + * + * \sa MatrixBase::inverse(), MatrixBase::quickInverse() + */ +template +struct ei_traits > +{ + typedef typename ExpressionType::Scalar Scalar; + typedef typename ExpressionType::Eval MatrixType; + enum { + RowsAtCompileTime = MatrixType::RowsAtCompileTime, + ColsAtCompileTime = MatrixType::ColsAtCompileTime, + MaxRowsAtCompileTime = MatrixType::MaxRowsAtCompileTime, + MaxColsAtCompileTime = MatrixType::MaxColsAtCompileTime, + Flags = MatrixType::Flags, + CoeffReadCost = MatrixType::CoeffReadCost + }; +}; + +template class Inverse : ei_no_assignment_operator, + public MatrixBase > +{ + public: + + EIGEN_GENERIC_PUBLIC_INTERFACE(Inverse) + typedef typename ei_traits::MatrixType MatrixType; + + Inverse(const ExpressionType& xpr) + : m_exists(true), + m_inverse(MatrixType::identity(xpr.rows(), xpr.cols())) + { + ei_assert(xpr.rows() == xpr.cols()); + _compute(xpr); + } + + /** \returns whether or not the inverse exists. + * + * \note This method is only available if CheckExistence is set to true, which is the default value. + * For instance, when using quickInverse(), this method is not available. + */ + bool exists() const { assert(CheckExistence); return m_exists; } + + private: + + int _rows() const { return m_inverse.rows(); } + int _cols() const { return m_inverse.cols(); } + + const Scalar _coeff(int row, int col) const + { + return m_inverse.coeff(row, col); + } + + void _compute(const ExpressionType& xpr); + void _compute_not_unrolled(MatrixType& matrix, const RealScalar& max); + template struct _unroll_first_loop; + template struct _unroll_second_loop; + template struct _unroll_third_loop; + + protected: + bool m_exists; + MatrixType m_inverse; +}; + +template +void Inverse +::_compute_not_unrolled(MatrixType& matrix, const RealScalar& max) +{ + const int size = matrix.rows(); + for(int k = 0; k < size-1; k++) + { + int rowOfBiggest; + const RealScalar max_in_this_col + = matrix.col(k).end(size-k).cwiseAbs().maxCoeff(&rowOfBiggest); + if(CheckExistence && ei_isMuchSmallerThan(max_in_this_col, max)) + { m_exists = false; return; } + + m_inverse.row(k).swap(m_inverse.row(k+rowOfBiggest)); + matrix.row(k).swap(matrix.row(k+rowOfBiggest)); + + const Scalar d = matrix(k,k); + m_inverse.block(k+1, 0, size-k-1, size) + -= matrix.col(k).end(size-k-1) * (m_inverse.row(k) / d); + matrix.corner(BottomRight, size-k-1, size-k) + -= matrix.col(k).end(size-k-1) * (matrix.row(k).end(size-k) / d); + } + + for(int k = 0; k < size-1; k++) + { + const Scalar d = static_cast(1)/matrix(k,k); + matrix.row(k).end(size-k) *= d; + m_inverse.row(k) *= d; + } + if(CheckExistence && ei_isMuchSmallerThan(matrix(size-1,size-1), max)) + { m_exists = false; return; } + m_inverse.row(size-1) /= matrix(size-1,size-1); + + for(int k = size-1; k >= 1; k--) + { + m_inverse.block(0,0,k,size) -= matrix.col(k).start(k) * m_inverse.row(k); + } +} + +template +template +struct Inverse::_unroll_first_loop +{ + typedef Inverse Inv; + typedef typename Inv::MatrixType MatrixType; + typedef typename Inv::RealScalar RealScalar; + + static void run(Inv& object, MatrixType& matrix, const RealScalar& max) + { + MatrixType& inverse = object.m_inverse; + int rowOfBiggest; + const RealScalar max_in_this_col + = matrix.col(Step).template end().cwiseAbs().maxCoeff(&rowOfBiggest); + if(CheckExistence && ei_isMuchSmallerThan(max_in_this_col, max)) + { object.m_exists = false; return; } + + inverse.row(Step).swap(inverse.row(Step+rowOfBiggest)); + matrix.row(Step).swap(matrix.row(Step+rowOfBiggest)); + + const Scalar d = matrix(Step,Step); + inverse.template block(Step+1, 0) + -= matrix.col(Step).template end() * (inverse.row(Step) / d); + matrix.template corner(BottomRight) + -= matrix.col(Step).template end() + * (matrix.row(Step).template end() / d); + + _unroll_first_loop= Size-2>::run(object, matrix, max); + } +}; + +template +template +struct Inverse::_unroll_first_loop +{ + typedef Inverse Inv; + typedef typename Inv::MatrixType MatrixType; + typedef typename Inv::RealScalar RealScalar; + static void run(Inv&, MatrixType&, const RealScalar&) {} +}; + +template +template +struct Inverse::_unroll_second_loop +{ + typedef Inverse Inv; + typedef typename Inv::MatrixType MatrixType; + typedef typename Inv::RealScalar RealScalar; + + static void run(Inv& object, MatrixType& matrix, const RealScalar& max) + { + MatrixType& inverse = object.m_inverse; + + if(Step == Size-1) + { + if(CheckExistence && ei_isMuchSmallerThan(matrix(Size-1,Size-1), max)) + { object.m_exists = false; return; } + inverse.row(Size-1) /= matrix(Size-1,Size-1); + } + else + { + const Scalar d = static_cast(1)/matrix(Step,Step); + matrix.row(Step).template end() *= d; + inverse.row(Step) *= d; + } + + _unroll_second_loop= Size-1>::run(object, matrix, max); + } +}; + +template +template +struct Inverse::_unroll_second_loop +{ + typedef Inverse Inv; + typedef typename Inv::MatrixType MatrixType; + typedef typename Inv::RealScalar RealScalar; + static void run(Inv&, MatrixType&, const RealScalar&) {} +}; + +template +template +struct Inverse::_unroll_third_loop +{ + typedef Inverse Inv; + typedef typename Inv::MatrixType MatrixType; + typedef typename Inv::RealScalar RealScalar; + + static void run(Inv& object, MatrixType& matrix, const RealScalar& max) + { + MatrixType& inverse = object.m_inverse; + inverse.template block(0,0) + -= matrix.col(Size-Step).template start() * inverse.row(Size-Step); + _unroll_third_loop= Size-1>::run(object, matrix, max); + } +}; + +template +template +struct Inverse::_unroll_third_loop +{ + typedef Inverse Inv; + typedef typename Inv::MatrixType MatrixType; + typedef typename Inv::RealScalar RealScalar; + static void run(Inv&, MatrixType&, const RealScalar&) {} +}; + +template +void Inverse::_compute(const ExpressionType& xpr) +{ + MatrixType matrix(xpr); + const RealScalar max = CheckExistence ? matrix.cwiseAbs().maxCoeff() + : static_cast(0); + + if(MatrixType::RowsAtCompileTime <= 4) + { + const int size = MatrixType::RowsAtCompileTime; + _unroll_first_loop::run(*this, matrix, max); + if(CheckExistence && !m_exists) return; + _unroll_second_loop::run(*this, matrix, max); + if(CheckExistence && !m_exists) return; + _unroll_third_loop::run(*this, matrix, max); + } + else + { + _compute_not_unrolled(matrix, max); + } +} + +/** \return the matrix inverse of \c *this, if it exists. + * + * Example: \include MatrixBase_inverse.cpp + * Output: \verbinclude MatrixBase_inverse.out + * + * \sa class Inverse + */ +template +const Inverse +MatrixBase::inverse() const +{ + return Inverse(derived()); +} + +/** \return the matrix inverse of \c *this, which is assumed to exist. + * + * Example: \include MatrixBase_quickInverse.cpp + * Output: \verbinclude MatrixBase_quickInverse.out + * + * \sa class Inverse + */ +template +const Inverse +MatrixBase::quickInverse() const +{ + return Inverse(derived()); +} + +#endif // EIGEN_INVERSE_H diff --git a/doc/echelon.cpp b/doc/echelon.cpp index 04b1907cd..3c9e58e59 100644 --- a/doc/echelon.cpp +++ b/doc/echelon.cpp @@ -49,7 +49,7 @@ struct unroll_echelon { static void run(MatrixBase& m) { - for(int k = 0; k < m.diagonal().size(); k++) + for(int k = 0; k < m.diagonal().size() - 1; k++) { int rowOfBiggest, colOfBiggest; int cornerRows = m.rows()-k, cornerCols = m.cols()-k; @@ -63,13 +63,14 @@ struct unroll_echelon } } }; + using namespace std; template void echelon(MatrixBase& m) { const int size = DiagonalCoeffs::SizeAtCompileTime; const bool unroll = size <= 4; - unroll_echelon::run(m); + unroll_echelon::run(m); } template @@ -100,7 +101,7 @@ using namespace std; int main(int, char **) { srand((unsigned int)time(0)); - const int Rows = 6, Cols = 6; + const int Rows = 6, Cols = 4; typedef Matrix Mat; const int N = Rows < Cols ? Rows : Cols;