product_notemporary.cpp
Go to the documentation of this file.
00001 // This file is part of Eigen, a lightweight C++ template library
00002 // for linear algebra.
00003 //
00004 // Copyright (C) 2006-2008 Benoit Jacob <jacob.benoit.1@gmail.com>
00005 //
00006 // Eigen is free software; you can redistribute it and/or
00007 // modify it under the terms of the GNU Lesser General Public
00008 // License as published by the Free Software Foundation; either
00009 // version 3 of the License, or (at your option) any later version.
00010 //
00011 // Alternatively, you can redistribute it and/or
00012 // modify it under the terms of the GNU General Public License as
00013 // published by the Free Software Foundation; either version 2 of
00014 // the License, or (at your option) any later version.
00015 //
00016 // Eigen is distributed in the hope that it will be useful, but WITHOUT ANY
00017 // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
00018 // FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License or the
00019 // GNU General Public License for more details.
00020 //
00021 // You should have received a copy of the GNU Lesser General Public
00022 // License and a copy of the GNU General Public License along with
00023 // Eigen. If not, see <http://www.gnu.org/licenses/>.
00024 
00025 static int nb_temporaries;
00026 
00027 void on_temporary_creation(int size) {
00028   // here's a great place to set a breakpoint when debugging failures in this test!
00029   if(size!=0) nb_temporaries++;
00030 }
00031   
00032 
00033 #define EIGEN_DENSE_STORAGE_CTOR_PLUGIN { on_temporary_creation(size); }
00034 
00035 #include "main.h"
00036 
00037 #define VERIFY_EVALUATION_COUNT(XPR,N) {\
00038     nb_temporaries = 0; \
00039     XPR; \
00040     if(nb_temporaries!=N) std::cerr << "nb_temporaries == " << nb_temporaries << "\n"; \
00041     VERIFY( (#XPR) && nb_temporaries==N ); \
00042   }
00043 
00044 template<typename MatrixType> void product_notemporary(const MatrixType& m)
00045 {
00046   /* This test checks the number of temporaries created
00047    * during the evaluation of a complex expression */
00048   typedef typename MatrixType::Index Index;
00049   typedef typename MatrixType::Scalar Scalar;
00050   typedef typename MatrixType::RealScalar RealScalar;
00051   typedef Matrix<Scalar, 1, Dynamic> RowVectorType;
00052   typedef Matrix<Scalar, Dynamic, 1> ColVectorType;
00053   typedef Matrix<Scalar, Dynamic, Dynamic, ColMajor> ColMajorMatrixType;
00054   typedef Matrix<Scalar, Dynamic, Dynamic, RowMajor> RowMajorMatrixType;
00055 
00056   Index rows = m.rows();
00057   Index cols = m.cols();
00058 
00059   ColMajorMatrixType m1 = MatrixType::Random(rows, cols),
00060                      m2 = MatrixType::Random(rows, cols),
00061                      m3(rows, cols);
00062   RowVectorType rv1 = RowVectorType::Random(rows), rvres(rows);
00063   ColVectorType cv1 = ColVectorType::Random(cols), cvres(cols);
00064   RowMajorMatrixType rm3(rows, cols);
00065 
00066   Scalar s1 = internal::random<Scalar>(),
00067          s2 = internal::random<Scalar>(),
00068          s3 = internal::random<Scalar>();
00069 
00070   Index c0 = internal::random<Index>(4,cols-8),
00071         c1 = internal::random<Index>(8,cols-c0),
00072         r0 = internal::random<Index>(4,cols-8),
00073         r1 = internal::random<Index>(8,rows-r0);
00074 
00075   VERIFY_EVALUATION_COUNT( m3 = (m1 * m2.adjoint()), 1);
00076   VERIFY_EVALUATION_COUNT( m3.noalias() = m1 * m2.adjoint(), 0);
00077 
00078   VERIFY_EVALUATION_COUNT( m3.noalias() = s1 * (m1 * m2.transpose()), 0);
00079 
00080   VERIFY_EVALUATION_COUNT( m3.noalias() = s1 * m1 * s2 * m2.adjoint(), 0);
00081   VERIFY_EVALUATION_COUNT( m3.noalias() = s1 * m1 * s2 * (m1*s3+m2*s2).adjoint(), 1);
00082   VERIFY_EVALUATION_COUNT( m3.noalias() = (s1 * m1).adjoint() * s2 * m2, 0);
00083   VERIFY_EVALUATION_COUNT( m3.noalias() += s1 * (-m1*s3).adjoint() * (s2 * m2 * s3), 0);
00084   VERIFY_EVALUATION_COUNT( m3.noalias() -= s1 * (m1.transpose() * m2), 0);
00085 
00086   VERIFY_EVALUATION_COUNT(( m3.block(r0,r0,r1,r1).noalias() += -m1.block(r0,c0,r1,c1) * (s2*m2.block(r0,c0,r1,c1)).adjoint() ), 0);
00087   VERIFY_EVALUATION_COUNT(( m3.block(r0,r0,r1,r1).noalias() -= s1 * m1.block(r0,c0,r1,c1) * m2.block(c0,r0,c1,r1) ), 0);
00088 
00089   // NOTE this is because the Block expression is not handled yet by our expression analyser
00090   VERIFY_EVALUATION_COUNT(( m3.block(r0,r0,r1,r1).noalias() = s1 * m1.block(r0,c0,r1,c1) * (s1*m2).block(c0,r0,c1,r1) ), 1);
00091 
00092   VERIFY_EVALUATION_COUNT( m3.noalias() -= (s1 * m1).template triangularView<Lower>() * m2, 0);
00093   VERIFY_EVALUATION_COUNT( rm3.noalias() = (s1 * m1.adjoint()).template triangularView<Upper>() * (m2+m2), 1);
00094   VERIFY_EVALUATION_COUNT( rm3.noalias() = (s1 * m1.adjoint()).template triangularView<UnitUpper>() * m2.adjoint(), 0);
00095 
00096   // NOTE this is because the blas_traits require innerstride==1 to avoid a temporary, but that doesn't seem to be actually needed for the triangular products
00097   VERIFY_EVALUATION_COUNT( rm3.col(c0).noalias() = (s1 * m1.adjoint()).template triangularView<UnitUpper>() * (s2*m2.row(c0)).adjoint(), 1);
00098 
00099   VERIFY_EVALUATION_COUNT( m1.template triangularView<Lower>().solveInPlace(m3), 0);
00100   VERIFY_EVALUATION_COUNT( m1.adjoint().template triangularView<Lower>().solveInPlace(m3.transpose()), 0);
00101 
00102   VERIFY_EVALUATION_COUNT( m3.noalias() -= (s1 * m1).adjoint().template selfadjointView<Lower>() * (-m2*s3).adjoint(), 0);
00103   VERIFY_EVALUATION_COUNT( m3.noalias() = s2 * m2.adjoint() * (s1 * m1.adjoint()).template selfadjointView<Upper>(), 0);
00104   VERIFY_EVALUATION_COUNT( rm3.noalias() = (s1 * m1.adjoint()).template selfadjointView<Lower>() * m2.adjoint(), 0);
00105 
00106   // NOTE this is because the blas_traits require innerstride==1 to avoid a temporary, but that doesn't seem to be actually needed for the triangular products
00107   VERIFY_EVALUATION_COUNT( m3.col(c0).noalias() = (s1 * m1).adjoint().template selfadjointView<Lower>() * (-m2.row(c0)*s3).adjoint(), 1);
00108   VERIFY_EVALUATION_COUNT( m3.col(c0).noalias() -= (s1 * m1).adjoint().template selfadjointView<Upper>() * (-m2.row(c0)*s3).adjoint(), 1);
00109 
00110   VERIFY_EVALUATION_COUNT( m3.block(r0,c0,r1,c1).noalias() += m1.block(r0,r0,r1,r1).template selfadjointView<Upper>() * (s1*m2.block(r0,c0,r1,c1)), 0);
00111   VERIFY_EVALUATION_COUNT( m3.block(r0,c0,r1,c1).noalias() = m1.block(r0,r0,r1,r1).template selfadjointView<Upper>() * m2.block(r0,c0,r1,c1), 0);
00112 
00113   VERIFY_EVALUATION_COUNT( m3.template selfadjointView<Lower>().rankUpdate(m2.adjoint()), 0);
00114 
00115   // Here we will get 1 temporary for each resize operation of the lhs operator; resize(r1,c1) would lead to zero temporaries
00116   m3.resize(1,1);
00117   VERIFY_EVALUATION_COUNT( m3.noalias() = m1.block(r0,r0,r1,r1).template selfadjointView<Lower>() * m2.block(r0,c0,r1,c1), 1);
00118   m3.resize(1,1);
00119   VERIFY_EVALUATION_COUNT( m3.noalias() = m1.block(r0,r0,r1,r1).template triangularView<UnitUpper>()  * m2.block(r0,c0,r1,c1), 1);
00120 
00121   // Zero temporaries for lazy products ...
00122   VERIFY_EVALUATION_COUNT( Scalar tmp = 0; tmp += Scalar(RealScalar(1)) /  (m3.transpose().lazyProduct(m3)).diagonal().sum(), 0 );
00123 
00124   // ... and even no temporary for even deeply (>=2) nested products
00125   VERIFY_EVALUATION_COUNT( Scalar tmp = 0; tmp += Scalar(RealScalar(1)) /  (m3.transpose() * m3).diagonal().sum(), 0 );
00126   VERIFY_EVALUATION_COUNT( Scalar tmp = 0; tmp += Scalar(RealScalar(1)) /  (m3.transpose() * m3).diagonal().array().abs().sum(), 0 );
00127 
00128   // Zero temporaries for ... CoeffBasedProductMode
00129   // - does not work with GCC because of the <..>, we'ld need variadic macros ...
00130   //VERIFY_EVALUATION_COUNT( m3.col(0).head<5>() * m3.col(0).transpose() + m3.col(0).head<5>() * m3.col(0).transpose(), 0 );
00131 
00132   // Check matrix * vectors
00133   VERIFY_EVALUATION_COUNT( cvres.noalias() = m1 * cv1, 0 );
00134   VERIFY_EVALUATION_COUNT( cvres.noalias() -= m1 * cv1, 0 );
00135   VERIFY_EVALUATION_COUNT( cvres.noalias() -= m1 * m2.col(0), 0 );
00136   VERIFY_EVALUATION_COUNT( cvres.noalias() -= m1 * rv1.adjoint(), 0 );
00137   VERIFY_EVALUATION_COUNT( cvres.noalias() -= m1 * m2.row(0).transpose(), 0 );
00138 }
00139 
00140 void test_product_notemporary()
00141 {
00142   int s;
00143   for(int i = 0; i < g_repeat; i++) {
00144     s = internal::random<int>(16,320);
00145     CALL_SUBTEST_1( product_notemporary(MatrixXf(s, s)) );
00146     s = internal::random<int>(16,320);
00147     CALL_SUBTEST_2( product_notemporary(MatrixXd(s, s)) );
00148     s = internal::random<int>(16,120);
00149     CALL_SUBTEST_3( product_notemporary(MatrixXcf(s,s)) );
00150     s = internal::random<int>(16,120);
00151     CALL_SUBTEST_4( product_notemporary(MatrixXcd(s,s)) );
00152   }
00153 }


re_vision
Author(s): Dorian Galvez-Lopez
autogenerated on Sun Jan 5 2014 11:32:11