• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // This file is part of Eigen, a lightweight C++ template library
2 // for linear algebra.
3 //
4 // Copyright (C) 2006-2008 Benoit Jacob <jacob.benoit.1@gmail.com>
5 //
6 // This Source Code Form is subject to the terms of the Mozilla
7 // Public License v. 2.0. If a copy of the MPL was not distributed
8 // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 
10 #define TEST_ENABLE_TEMPORARY_TRACKING
11 
12 #include "main.h"
13 
product_notemporary(const MatrixType & m)14 template<typename MatrixType> void product_notemporary(const MatrixType& m)
15 {
16   /* This test checks the number of temporaries created
17    * during the evaluation of a complex expression */
18   typedef typename MatrixType::Index Index;
19   typedef typename MatrixType::Scalar Scalar;
20   typedef typename MatrixType::RealScalar RealScalar;
21   typedef Matrix<Scalar, 1, Dynamic> RowVectorType;
22   typedef Matrix<Scalar, Dynamic, 1> ColVectorType;
23   typedef Matrix<Scalar, Dynamic, Dynamic, ColMajor> ColMajorMatrixType;
24   typedef Matrix<Scalar, Dynamic, Dynamic, RowMajor> RowMajorMatrixType;
25 
26   Index rows = m.rows();
27   Index cols = m.cols();
28 
29   ColMajorMatrixType m1 = MatrixType::Random(rows, cols),
30                      m2 = MatrixType::Random(rows, cols),
31                      m3(rows, cols);
32   RowVectorType rv1 = RowVectorType::Random(rows), rvres(rows);
33   ColVectorType cv1 = ColVectorType::Random(cols), cvres(cols);
34   RowMajorMatrixType rm3(rows, cols);
35 
36   Scalar s1 = internal::random<Scalar>(),
37          s2 = internal::random<Scalar>(),
38          s3 = internal::random<Scalar>();
39 
40   Index c0 = internal::random<Index>(4,cols-8),
41         c1 = internal::random<Index>(8,cols-c0),
42         r0 = internal::random<Index>(4,cols-8),
43         r1 = internal::random<Index>(8,rows-r0);
44 
45   VERIFY_EVALUATION_COUNT( m3 = (m1 * m2.adjoint()), 1);
46   VERIFY_EVALUATION_COUNT( m3 = (m1 * m2.adjoint()).transpose(), 1);
47   VERIFY_EVALUATION_COUNT( m3.noalias() = m1 * m2.adjoint(), 0);
48 
49   VERIFY_EVALUATION_COUNT( m3 = s1 * (m1 * m2.transpose()), 1);
50 //   VERIFY_EVALUATION_COUNT( m3 = m3 + s1 * (m1 * m2.transpose()), 1);
51   VERIFY_EVALUATION_COUNT( m3.noalias() = s1 * (m1 * m2.transpose()), 0);
52 
53   VERIFY_EVALUATION_COUNT( m3 = m3 + (m1 * m2.adjoint()), 1);
54   VERIFY_EVALUATION_COUNT( m3 = m3 - (m1 * m2.adjoint()), 1);
55 
56   VERIFY_EVALUATION_COUNT( m3 = m3 + (m1 * m2.adjoint()).transpose(), 1);
57   VERIFY_EVALUATION_COUNT( m3.noalias() = m3 + m1 * m2.transpose(), 0);
58   VERIFY_EVALUATION_COUNT( m3.noalias() += m3 + m1 * m2.transpose(), 0);
59   VERIFY_EVALUATION_COUNT( m3.noalias() -= m3 + m1 * m2.transpose(), 0);
60   VERIFY_EVALUATION_COUNT( m3.noalias() =  m3 - m1 * m2.transpose(), 0);
61   VERIFY_EVALUATION_COUNT( m3.noalias() += m3 - m1 * m2.transpose(), 0);
62   VERIFY_EVALUATION_COUNT( m3.noalias() -= m3 - m1 * m2.transpose(), 0);
63 
64   VERIFY_EVALUATION_COUNT( m3.noalias() = s1 * m1 * s2 * m2.adjoint(), 0);
65   VERIFY_EVALUATION_COUNT( m3.noalias() = s1 * m1 * s2 * (m1*s3+m2*s2).adjoint(), 1);
66   VERIFY_EVALUATION_COUNT( m3.noalias() = (s1 * m1).adjoint() * s2 * m2, 0);
67   VERIFY_EVALUATION_COUNT( m3.noalias() += s1 * (-m1*s3).adjoint() * (s2 * m2 * s3), 0);
68   VERIFY_EVALUATION_COUNT( m3.noalias() -= s1 * (m1.transpose() * m2), 0);
69 
70   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);
71   VERIFY_EVALUATION_COUNT(( m3.block(r0,r0,r1,r1).noalias() -= s1 * m1.block(r0,c0,r1,c1) * m2.block(c0,r0,c1,r1) ), 0);
72 
73   // NOTE this is because the Block expression is not handled yet by our expression analyser
74   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);
75 
76   VERIFY_EVALUATION_COUNT( m3.noalias() -= (s1 * m1).template triangularView<Lower>() * m2, 0);
77   VERIFY_EVALUATION_COUNT( rm3.noalias() = (s1 * m1.adjoint()).template triangularView<Upper>() * (m2+m2), 1);
78   VERIFY_EVALUATION_COUNT( rm3.noalias() = (s1 * m1.adjoint()).template triangularView<UnitUpper>() * m2.adjoint(), 0);
79 
80   VERIFY_EVALUATION_COUNT( m3.template triangularView<Upper>() = (m1 * m2.adjoint()), 0);
81   VERIFY_EVALUATION_COUNT( m3.template triangularView<Upper>() -= (m1 * m2.adjoint()), 0);
82 
83   // 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
84   VERIFY_EVALUATION_COUNT( rm3.col(c0).noalias() = (s1 * m1.adjoint()).template triangularView<UnitUpper>() * (s2*m2.row(c0)).adjoint(), 1);
85 
86   VERIFY_EVALUATION_COUNT( m1.template triangularView<Lower>().solveInPlace(m3), 0);
87   VERIFY_EVALUATION_COUNT( m1.adjoint().template triangularView<Lower>().solveInPlace(m3.transpose()), 0);
88 
89   VERIFY_EVALUATION_COUNT( m3.noalias() -= (s1 * m1).adjoint().template selfadjointView<Lower>() * (-m2*s3).adjoint(), 0);
90   VERIFY_EVALUATION_COUNT( m3.noalias() = s2 * m2.adjoint() * (s1 * m1.adjoint()).template selfadjointView<Upper>(), 0);
91   VERIFY_EVALUATION_COUNT( rm3.noalias() = (s1 * m1.adjoint()).template selfadjointView<Lower>() * m2.adjoint(), 0);
92 
93   // 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
94   VERIFY_EVALUATION_COUNT( m3.col(c0).noalias() = (s1 * m1).adjoint().template selfadjointView<Lower>() * (-m2.row(c0)*s3).adjoint(), 1);
95   VERIFY_EVALUATION_COUNT( m3.col(c0).noalias() -= (s1 * m1).adjoint().template selfadjointView<Upper>() * (-m2.row(c0)*s3).adjoint(), 1);
96 
97   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);
98   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);
99 
100   VERIFY_EVALUATION_COUNT( m3.template selfadjointView<Lower>().rankUpdate(m2.adjoint()), 0);
101 
102   // Here we will get 1 temporary for each resize operation of the lhs operator; resize(r1,c1) would lead to zero temporaries
103   m3.resize(1,1);
104   VERIFY_EVALUATION_COUNT( m3.noalias() = m1.block(r0,r0,r1,r1).template selfadjointView<Lower>() * m2.block(r0,c0,r1,c1), 1);
105   m3.resize(1,1);
106   VERIFY_EVALUATION_COUNT( m3.noalias() = m1.block(r0,r0,r1,r1).template triangularView<UnitUpper>()  * m2.block(r0,c0,r1,c1), 1);
107 
108   // Zero temporaries for lazy products ...
109   VERIFY_EVALUATION_COUNT( Scalar tmp = 0; tmp += Scalar(RealScalar(1)) /  (m3.transpose().lazyProduct(m3)).diagonal().sum(), 0 );
110 
111   // ... and even no temporary for even deeply (>=2) nested products
112   VERIFY_EVALUATION_COUNT( Scalar tmp = 0; tmp += Scalar(RealScalar(1)) /  (m3.transpose() * m3).diagonal().sum(), 0 );
113   VERIFY_EVALUATION_COUNT( Scalar tmp = 0; tmp += Scalar(RealScalar(1)) /  (m3.transpose() * m3).diagonal().array().abs().sum(), 0 );
114 
115   // Zero temporaries for ... CoeffBasedProductMode
116   VERIFY_EVALUATION_COUNT( m3.col(0).template head<5>() * m3.col(0).transpose() + m3.col(0).template head<5>() * m3.col(0).transpose(), 0 );
117 
118   // Check matrix * vectors
119   VERIFY_EVALUATION_COUNT( cvres.noalias() = m1 * cv1, 0 );
120   VERIFY_EVALUATION_COUNT( cvres.noalias() -= m1 * cv1, 0 );
121   VERIFY_EVALUATION_COUNT( cvres.noalias() -= m1 * m2.col(0), 0 );
122   VERIFY_EVALUATION_COUNT( cvres.noalias() -= m1 * rv1.adjoint(), 0 );
123   VERIFY_EVALUATION_COUNT( cvres.noalias() -= m1 * m2.row(0).transpose(), 0 );
124 
125   VERIFY_EVALUATION_COUNT( cvres.noalias() = (m1+m1) * cv1, 0 );
126   VERIFY_EVALUATION_COUNT( cvres.noalias() = (rm3+rm3) * cv1, 0 );
127   VERIFY_EVALUATION_COUNT( cvres.noalias() = (m1+m1) * (m1*cv1), 1 );
128   VERIFY_EVALUATION_COUNT( cvres.noalias() = (rm3+rm3) * (m1*cv1), 1 );
129 
130   // Check outer products
131   m3 = cv1 * rv1;
132   VERIFY_EVALUATION_COUNT( m3.noalias() = cv1 * rv1, 0 );
133   VERIFY_EVALUATION_COUNT( m3.noalias() = (cv1+cv1) * (rv1+rv1), 1 );
134   VERIFY_EVALUATION_COUNT( m3.noalias() = (m1*cv1) * (rv1), 1 );
135   VERIFY_EVALUATION_COUNT( m3.noalias() += (m1*cv1) * (rv1), 1 );
136   VERIFY_EVALUATION_COUNT( rm3.noalias() = (cv1) * (rv1 * m1), 1 );
137   VERIFY_EVALUATION_COUNT( rm3.noalias() -= (cv1) * (rv1 * m1), 1 );
138   VERIFY_EVALUATION_COUNT( rm3.noalias() = (m1*cv1) * (rv1 * m1), 2 );
139   VERIFY_EVALUATION_COUNT( rm3.noalias() += (m1*cv1) * (rv1 * m1), 2 );
140 
141   // Check nested products
142   VERIFY_EVALUATION_COUNT( cvres.noalias() = m1.adjoint() * m1 * cv1, 1 );
143   VERIFY_EVALUATION_COUNT( rvres.noalias() = rv1 * (m1 * m2.adjoint()), 1 );
144 }
145 
test_product_notemporary()146 void test_product_notemporary()
147 {
148   int s;
149   for(int i = 0; i < g_repeat; i++) {
150     s = internal::random<int>(16,EIGEN_TEST_MAX_SIZE);
151     CALL_SUBTEST_1( product_notemporary(MatrixXf(s, s)) );
152     CALL_SUBTEST_2( product_notemporary(MatrixXd(s, s)) );
153     TEST_SET_BUT_UNUSED_VARIABLE(s)
154 
155     s = internal::random<int>(16,EIGEN_TEST_MAX_SIZE/2);
156     CALL_SUBTEST_3( product_notemporary(MatrixXcf(s,s)) );
157     CALL_SUBTEST_4( product_notemporary(MatrixXcd(s,s)) );
158     TEST_SET_BUT_UNUSED_VARIABLE(s)
159   }
160 }
161