[eigen] Propagating Max*AtCompileTime template parameters to decompositions

[ Thread Index | Date Index | More lists.tuxfamily.org/eigen Archives ]


Hi all,

I've been performing some tests with dynamic matrices that have a compile-time max size, and found out that for most matrix decompositions, the MaxRowsAtCompileTime and MaxColsAtCompileTime template parameters of the input matrix are not propagated to the decomposition's members and temporaries. This leads to undesired and preventable heap allocations. The following is a minimal example of what I mean:

typedef Matrix<double, dynamic, dynamic, 0, 10, 10> Mat;
Mat A(Mat::Random(5, 5));

FullPivLU<Mat> ldlt(A); // Allocates 4 temps
LDLT<Mat> ldlt(A);      // Allocates 3 temps

I'm attaching a patch against the devel branch (commit 2589:8a8f4e9e04bf) that deals with this issue for the Cholesky, LU, QR and SVD modules. Since most of these modules are experimental and undergoing heavy work, the style of the modifications changes form file to file (some use convenience enums, some not). However, I tried to stay consistent within the context of each file. Please let me know your thoughts on the matter. One very specific note: in line 65 of HouseHolderQR.h I left the Options template parameter as-is, but don't know if it should be changed to that of MatrixType.

Cheers,

Adolfo.

P.S. Is there a better way to submit small patches, something like a bugtracker, or is this the currently recommended way?

--
Adolfo Rodríguez Tsouroukdissian, Ph. D.

Robotics engineer
PAL ROBOTICS S.L
http://www.pal-robotics.com
Tel. +34.93.414.53.47
Fax.+34.93.209.11.09
AVISO DE CONFIDENCIALIDAD: Este mensaje y sus documentos adjuntos, pueden contener información privilegiada y/o confidencial que está dirigida exclusivamente a su destinatario. Si usted recibe este mensaje y no es el destinatario indicado, o el empleado encargado de su entrega a dicha persona, por favor, notifíquelo inmediatamente y remita el mensaje original a la dirección de correo electrónico indicada. Cualquier copia, uso o distribución no autorizados de esta comunicación queda estrictamente prohibida.

CONFIDENTIALITY NOTICE: This e-mail and the accompanying document(s) may contain confidential information which is privileged and intended only for the individual or entity to whom they are addressed.  If you are not the intended recipient, you are hereby notified that any disclosure, copying, distribution or use of this e-mail and/or accompanying document(s) is strictly prohibited.  If you have received this e-mail in error, please immediately notify the sender at the above e-mail address.
diff -r 8a8f4e9e04bf Eigen/src/Cholesky/LDLT.h
--- a/Eigen/src/Cholesky/LDLT.h	Wed Mar 03 12:15:34 2010 -0600
+++ b/Eigen/src/Cholesky/LDLT.h	Thu Mar 04 14:32:22 2010 +0100
@@ -58,9 +58,9 @@
     typedef _MatrixType MatrixType;
     typedef typename MatrixType::Scalar Scalar;
     typedef typename NumTraits<typename MatrixType::Scalar>::Real RealScalar;
-    typedef Matrix<Scalar, MatrixType::ColsAtCompileTime, 1> VectorType;
-    typedef Matrix<int, MatrixType::RowsAtCompileTime, 1> IntColVectorType;
-    typedef Matrix<int, 1, MatrixType::RowsAtCompileTime> IntRowVectorType;
+    typedef Matrix<Scalar, MatrixType::ColsAtCompileTime, 1,  MatrixType::Options, MatrixType::MaxColsAtCompileTime, 1> VectorType;
+    typedef Matrix<int, MatrixType::RowsAtCompileTime, 1, MatrixType::Options, MatrixType::MaxRowsAtCompileTime, 1> IntColVectorType;
+    typedef Matrix<int, 1, MatrixType::RowsAtCompileTime, MatrixType::Options, 1, MatrixType::MaxRowsAtCompileTime> IntRowVectorType;
 
     /** \brief Default Constructor.
       *
@@ -201,7 +201,7 @@
   // By using a temorary, packet-aligned products are guarenteed. In the LLT
   // case this is unnecessary because the diagonal is included and will always
   // have optimal alignment.
-  Matrix<Scalar,MatrixType::RowsAtCompileTime,1> _temporary(size);
+  Matrix<Scalar, MatrixType::RowsAtCompileTime, 1, MatrixType::Options, MatrixType::MaxRowsAtCompileTime, 1> _temporary(size);
 
   for (int j = 0; j < size; ++j)
   {
diff -r 8a8f4e9e04bf Eigen/src/Cholesky/LLT.h
--- a/Eigen/src/Cholesky/LLT.h	Wed Mar 03 12:15:34 2010 -0600
+++ b/Eigen/src/Cholesky/LLT.h	Thu Mar 04 14:32:22 2010 +0100
@@ -59,7 +59,7 @@
     typedef _MatrixType MatrixType;
     typedef typename MatrixType::Scalar Scalar;
     typedef typename NumTraits<typename MatrixType::Scalar>::Real RealScalar;
-    typedef Matrix<Scalar, MatrixType::ColsAtCompileTime, 1> VectorType;
+    typedef Matrix<Scalar, MatrixType::ColsAtCompileTime, 1, MatrixType::Options, MatrixType::MaxColsAtCompileTime, 1> VectorType;
 
     enum {
       PacketSize = ei_packet_traits<Scalar>::size,
diff -r 8a8f4e9e04bf Eigen/src/LU/FullPivLU.h
--- a/Eigen/src/LU/FullPivLU.h	Wed Mar 03 12:15:34 2010 -0600
+++ b/Eigen/src/LU/FullPivLU.h	Thu Mar 04 14:32:22 2010 +0100
@@ -61,10 +61,10 @@
     typedef _MatrixType MatrixType;
     typedef typename MatrixType::Scalar Scalar;
     typedef typename NumTraits<typename MatrixType::Scalar>::Real RealScalar;
-    typedef Matrix<int, 1, MatrixType::ColsAtCompileTime> IntRowVectorType;
-    typedef Matrix<int, MatrixType::RowsAtCompileTime, 1> IntColVectorType;
-    typedef PermutationMatrix<MatrixType::ColsAtCompileTime> PermutationQType;
-    typedef PermutationMatrix<MatrixType::RowsAtCompileTime> PermutationPType;
+    typedef Matrix<int, 1, MatrixType::ColsAtCompileTime, MatrixType::Options, 1, MatrixType::MaxColsAtCompileTime> IntRowVectorType;
+    typedef Matrix<int, MatrixType::RowsAtCompileTime, 1, MatrixType::Options, MatrixType::MaxRowsAtCompileTime, 1> IntColVectorType;
+    typedef PermutationMatrix<MatrixType::ColsAtCompileTime, MatrixType::MaxColsAtCompileTime> PermutationQType;
+    typedef PermutationMatrix<MatrixType::RowsAtCompileTime, MatrixType::MaxRowsAtCompileTime> PermutationPType;
 
     /**
       * \brief Default Constructor.
diff -r 8a8f4e9e04bf Eigen/src/LU/PartialPivLU.h
--- a/Eigen/src/LU/PartialPivLU.h	Wed Mar 03 12:15:34 2010 -0600
+++ b/Eigen/src/LU/PartialPivLU.h	Thu Mar 04 14:32:22 2010 +0100
@@ -64,8 +64,8 @@
     typedef _MatrixType MatrixType;
     typedef typename MatrixType::Scalar Scalar;
     typedef typename NumTraits<typename MatrixType::Scalar>::Real RealScalar;
-    typedef Matrix<int, MatrixType::RowsAtCompileTime, 1> PermutationVectorType;
-    typedef PermutationMatrix<MatrixType::RowsAtCompileTime> PermutationType;
+    typedef Matrix<int, MatrixType::RowsAtCompileTime, 1, MatrixType::Options, MatrixType::MaxRowsAtCompileTime, 1> PermutationVectorType;
+    typedef PermutationMatrix<MatrixType::RowsAtCompileTime, MatrixType::MaxRowsAtCompileTime> PermutationType;
 
     enum { MaxSmallDimAtCompileTime = EIGEN_SIZE_MIN(
              MatrixType::MaxColsAtCompileTime,
diff -r 8a8f4e9e04bf Eigen/src/QR/ColPivHouseholderQR.h
--- a/Eigen/src/QR/ColPivHouseholderQR.h	Wed Mar 03 12:15:34 2010 -0600
+++ b/Eigen/src/QR/ColPivHouseholderQR.h	Thu Mar 04 14:32:22 2010 +0100
@@ -51,16 +51,19 @@
       RowsAtCompileTime = MatrixType::RowsAtCompileTime,
       ColsAtCompileTime = MatrixType::ColsAtCompileTime,
       Options = MatrixType::Options,
-      DiagSizeAtCompileTime = EIGEN_SIZE_MIN(ColsAtCompileTime,RowsAtCompileTime)
+      MaxRowsAtCompileTime = MatrixType::MaxRowsAtCompileTime,
+      MaxColsAtCompileTime = MatrixType::MaxColsAtCompileTime,
+      DiagSizeAtCompileTime = EIGEN_SIZE_MIN(ColsAtCompileTime,RowsAtCompileTime),
+      MaxDiagSizeAtCompileTime = EIGEN_SIZE_MIN(MaxColsAtCompileTime,MaxRowsAtCompileTime)
     };
     typedef typename MatrixType::Scalar Scalar;
     typedef typename MatrixType::RealScalar RealScalar;
-    typedef Matrix<Scalar, RowsAtCompileTime, RowsAtCompileTime> MatrixQType;
-    typedef Matrix<Scalar, DiagSizeAtCompileTime, 1> HCoeffsType;
-    typedef PermutationMatrix<ColsAtCompileTime> PermutationType;
-    typedef Matrix<int, 1, ColsAtCompileTime> IntRowVectorType;
-    typedef Matrix<Scalar, 1, ColsAtCompileTime> RowVectorType;
-    typedef Matrix<RealScalar, 1, ColsAtCompileTime> RealRowVectorType;
+    typedef Matrix<Scalar, RowsAtCompileTime, RowsAtCompileTime, Options, MaxRowsAtCompileTime, MaxRowsAtCompileTime> MatrixQType;
+    typedef Matrix<Scalar, DiagSizeAtCompileTime, 1, Options, MaxDiagSizeAtCompileTime, 1> HCoeffsType;
+    typedef PermutationMatrix<ColsAtCompileTime, MaxColsAtCompileTime> PermutationType;
+    typedef Matrix<int, 1, ColsAtCompileTime, Options, 1, MaxColsAtCompileTime> IntRowVectorType;
+    typedef Matrix<Scalar, 1, ColsAtCompileTime, Options, 1, MaxColsAtCompileTime> RowVectorType;
+    typedef Matrix<RealScalar, 1, ColsAtCompileTime, Options, 1, MaxColsAtCompileTime> RealRowVectorType;
     typedef typename HouseholderSequence<MatrixType,HCoeffsType>::ConjugateReturnType HouseholderSequenceType;
 
     /**
diff -r 8a8f4e9e04bf Eigen/src/QR/FullPivHouseholderQR.h
--- a/Eigen/src/QR/FullPivHouseholderQR.h	Wed Mar 03 12:15:34 2010 -0600
+++ b/Eigen/src/QR/FullPivHouseholderQR.h	Thu Mar 04 14:32:22 2010 +0100
@@ -51,17 +51,20 @@
       RowsAtCompileTime = MatrixType::RowsAtCompileTime,
       ColsAtCompileTime = MatrixType::ColsAtCompileTime,
       Options = MatrixType::Options,
-      DiagSizeAtCompileTime = EIGEN_SIZE_MIN(ColsAtCompileTime,RowsAtCompileTime)
+      MaxRowsAtCompileTime = MatrixType::MaxRowsAtCompileTime,
+      MaxColsAtCompileTime = MatrixType::MaxColsAtCompileTime,
+      DiagSizeAtCompileTime = EIGEN_SIZE_MIN(ColsAtCompileTime,RowsAtCompileTime),
+      MaxDiagSizeAtCompileTime = EIGEN_SIZE_MIN(MaxColsAtCompileTime,MaxRowsAtCompileTime)
     };
     typedef typename MatrixType::Scalar Scalar;
     typedef typename MatrixType::RealScalar RealScalar;
-    typedef Matrix<Scalar, RowsAtCompileTime, RowsAtCompileTime> MatrixQType;
-    typedef Matrix<Scalar, DiagSizeAtCompileTime, 1> HCoeffsType;
-    typedef Matrix<int, 1, ColsAtCompileTime> IntRowVectorType;
-    typedef PermutationMatrix<ColsAtCompileTime> PermutationType;
-    typedef Matrix<int, RowsAtCompileTime, 1> IntColVectorType;
-    typedef Matrix<Scalar, 1, ColsAtCompileTime> RowVectorType;
-    typedef Matrix<Scalar, RowsAtCompileTime, 1> ColVectorType;
+    typedef Matrix<Scalar, RowsAtCompileTime, RowsAtCompileTime, Options, MaxRowsAtCompileTime, MaxRowsAtCompileTime> MatrixQType;
+    typedef Matrix<Scalar, DiagSizeAtCompileTime, 1, Options, MaxDiagSizeAtCompileTime, 1> HCoeffsType;
+    typedef Matrix<int, 1, ColsAtCompileTime, Options, 1, MaxColsAtCompileTime> IntRowVectorType;
+    typedef PermutationMatrix<ColsAtCompileTime, MaxColsAtCompileTime> PermutationType;
+    typedef Matrix<int, RowsAtCompileTime, 1, Options, MaxRowsAtCompileTime, 1> IntColVectorType;
+    typedef Matrix<Scalar, 1, ColsAtCompileTime, Options, 1, MaxColsAtCompileTime> RowVectorType;
+    typedef Matrix<Scalar, RowsAtCompileTime, 1, Options, MaxRowsAtCompileTime, 1> ColVectorType;
 
     /** \brief Default Constructor.
       *
diff -r 8a8f4e9e04bf Eigen/src/QR/HouseholderQR.h
--- a/Eigen/src/QR/HouseholderQR.h	Wed Mar 03 12:15:34 2010 -0600
+++ b/Eigen/src/QR/HouseholderQR.h	Thu Mar 04 14:32:22 2010 +0100
@@ -55,13 +55,16 @@
       RowsAtCompileTime = MatrixType::RowsAtCompileTime,
       ColsAtCompileTime = MatrixType::ColsAtCompileTime,
       Options = MatrixType::Options,
-      DiagSizeAtCompileTime = EIGEN_SIZE_MIN(ColsAtCompileTime,RowsAtCompileTime)
+      MaxRowsAtCompileTime = MatrixType::MaxRowsAtCompileTime,
+      MaxColsAtCompileTime = MatrixType::MaxColsAtCompileTime,
+      DiagSizeAtCompileTime = EIGEN_SIZE_MIN(ColsAtCompileTime,RowsAtCompileTime),
+      MaxDiagSizeAtCompileTime = EIGEN_SIZE_MIN(MaxColsAtCompileTime,MaxRowsAtCompileTime)
     };
     typedef typename MatrixType::Scalar Scalar;
     typedef typename MatrixType::RealScalar RealScalar;
-    typedef Matrix<Scalar, RowsAtCompileTime, RowsAtCompileTime, ei_traits<MatrixType>::Flags&RowMajorBit ? RowMajor : ColMajor> MatrixQType;
-    typedef Matrix<Scalar, DiagSizeAtCompileTime, 1> HCoeffsType;
-    typedef Matrix<Scalar, 1, ColsAtCompileTime> RowVectorType;
+    typedef Matrix<Scalar, RowsAtCompileTime, RowsAtCompileTime, ei_traits<MatrixType>::Flags&RowMajorBit ? RowMajor : ColMajor, MaxRowsAtCompileTime, MaxRowsAtCompileTime> MatrixQType;
+    typedef Matrix<Scalar, DiagSizeAtCompileTime, 1, Options, MaxDiagSizeAtCompileTime, 1> HCoeffsType;
+    typedef Matrix<Scalar, 1, ColsAtCompileTime, Options, 1, MaxColsAtCompileTime> RowVectorType;
     typedef typename HouseholderSequence<MatrixType,HCoeffsType>::ConjugateReturnType HouseholderSequenceType;
 
     /**
diff -r 8a8f4e9e04bf Eigen/src/SVD/SVD.h
--- a/Eigen/src/SVD/SVD.h	Wed Mar 03 12:15:34 2010 -0600
+++ b/Eigen/src/SVD/SVD.h	Thu Mar 04 14:32:22 2010 +0100
@@ -52,15 +52,18 @@
       ColsAtCompileTime = MatrixType::ColsAtCompileTime,
       PacketSize = ei_packet_traits<Scalar>::size,
       AlignmentMask = int(PacketSize)-1,
-      MinSize = EIGEN_SIZE_MIN(RowsAtCompileTime, ColsAtCompileTime)
+      MinSize = EIGEN_SIZE_MIN(RowsAtCompileTime, ColsAtCompileTime),
+      MaxRowsAtCompileTime = MatrixType::MaxRowsAtCompileTime,
+      MaxColsAtCompileTime = MatrixType::MaxColsAtCompileTime,
+      MatrixOptions = MatrixType::Options
     };
 
-    typedef Matrix<Scalar, RowsAtCompileTime, 1> ColVector;
-    typedef Matrix<Scalar, ColsAtCompileTime, 1> RowVector;
+    typedef Matrix<Scalar, RowsAtCompileTime, 1, MatrixOptions, MaxRowsAtCompileTime, 1> ColVector;
+    typedef Matrix<Scalar, ColsAtCompileTime, 1, MatrixOptions, MaxColsAtCompileTime, 1> RowVector;
 
-    typedef Matrix<Scalar, RowsAtCompileTime, RowsAtCompileTime> MatrixUType;
-    typedef Matrix<Scalar, ColsAtCompileTime, ColsAtCompileTime> MatrixVType;
-    typedef Matrix<Scalar, ColsAtCompileTime, 1> SingularValuesType;
+    typedef Matrix<Scalar, RowsAtCompileTime, RowsAtCompileTime, MatrixOptions, MaxRowsAtCompileTime, MaxRowsAtCompileTime> MatrixUType;
+    typedef Matrix<Scalar, ColsAtCompileTime, ColsAtCompileTime, MatrixOptions, MaxColsAtCompileTime, MaxColsAtCompileTime> MatrixVType;
+    typedef Matrix<Scalar, ColsAtCompileTime, 1, MatrixOptions, MaxColsAtCompileTime, 1> SingularValuesType;
 
     /**
     * \brief Default Constructor.
@@ -195,7 +198,7 @@
   bool convergence = true;
   Scalar eps = NumTraits<Scalar>::dummy_precision();
 
-  Matrix<Scalar,Dynamic,1> rv1(n);
+  RowVector rv1(n);
   g = scale = anorm = 0;
   // Householder reduction to bidiagonal form.
   for (i=0; i<n; i++)


Mail converted by MHonArc 2.6.19+ http://listengine.tuxfamily.org/