product_large.cpp (5395B)
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 #include "product.h" 11 #include <Eigen/LU> 12 13 template<typename T> 14 void test_aliasing() 15 { 16 int rows = internal::random<int>(1,12); 17 int cols = internal::random<int>(1,12); 18 typedef Matrix<T,Dynamic,Dynamic> MatrixType; 19 typedef Matrix<T,Dynamic,1> VectorType; 20 VectorType x(cols); x.setRandom(); 21 VectorType z(x); 22 VectorType y(rows); y.setZero(); 23 MatrixType A(rows,cols); A.setRandom(); 24 // CwiseBinaryOp 25 VERIFY_IS_APPROX(x = y + A*x, A*z); // OK because "y + A*x" is marked as "assume-aliasing" 26 x = z; 27 // CwiseUnaryOp 28 VERIFY_IS_APPROX(x = T(1.)*(A*x), A*z); // OK because 1*(A*x) is replaced by (1*A*x) which is a Product<> expression 29 x = z; 30 // VERIFY_IS_APPROX(x = y-A*x, -A*z); // Not OK in 3.3 because x is resized before A*x gets evaluated 31 x = z; 32 } 33 34 template<int> 35 void product_large_regressions() 36 { 37 { 38 // test a specific issue in DiagonalProduct 39 int N = 1000000; 40 VectorXf v = VectorXf::Ones(N); 41 MatrixXf m = MatrixXf::Ones(N,3); 42 m = (v+v).asDiagonal() * m; 43 VERIFY_IS_APPROX(m, MatrixXf::Constant(N,3,2)); 44 } 45 46 { 47 // test deferred resizing in Matrix::operator= 48 MatrixXf a = MatrixXf::Random(10,4), b = MatrixXf::Random(4,10), c = a; 49 VERIFY_IS_APPROX((a = a * b), (c * b).eval()); 50 } 51 52 { 53 // check the functions to setup blocking sizes compile and do not segfault 54 // FIXME check they do what they are supposed to do !! 55 std::ptrdiff_t l1 = internal::random<int>(10000,20000); 56 std::ptrdiff_t l2 = internal::random<int>(100000,200000); 57 std::ptrdiff_t l3 = internal::random<int>(1000000,2000000); 58 setCpuCacheSizes(l1,l2,l3); 59 VERIFY(l1==l1CacheSize()); 60 VERIFY(l2==l2CacheSize()); 61 std::ptrdiff_t k1 = internal::random<int>(10,100)*16; 62 std::ptrdiff_t m1 = internal::random<int>(10,100)*16; 63 std::ptrdiff_t n1 = internal::random<int>(10,100)*16; 64 // only makes sure it compiles fine 65 internal::computeProductBlockingSizes<float,float,std::ptrdiff_t>(k1,m1,n1,1); 66 } 67 68 { 69 // test regression in row-vector by matrix (bad Map type) 70 MatrixXf mat1(10,32); mat1.setRandom(); 71 MatrixXf mat2(32,32); mat2.setRandom(); 72 MatrixXf r1 = mat1.row(2)*mat2.transpose(); 73 VERIFY_IS_APPROX(r1, (mat1.row(2)*mat2.transpose()).eval()); 74 75 MatrixXf r2 = mat1.row(2)*mat2; 76 VERIFY_IS_APPROX(r2, (mat1.row(2)*mat2).eval()); 77 } 78 79 { 80 Eigen::MatrixXd A(10,10), B, C; 81 A.setRandom(); 82 C = A; 83 for(int k=0; k<79; ++k) 84 C = C * A; 85 B.noalias() = (((A*A)*(A*A))*((A*A)*(A*A))*((A*A)*(A*A))*((A*A)*(A*A))*((A*A)*(A*A)) * ((A*A)*(A*A))*((A*A)*(A*A))*((A*A)*(A*A))*((A*A)*(A*A))*((A*A)*(A*A))) 86 * (((A*A)*(A*A))*((A*A)*(A*A))*((A*A)*(A*A))*((A*A)*(A*A))*((A*A)*(A*A)) * ((A*A)*(A*A))*((A*A)*(A*A))*((A*A)*(A*A))*((A*A)*(A*A))*((A*A)*(A*A))); 87 VERIFY_IS_APPROX(B,C); 88 } 89 } 90 91 template<int> 92 void bug_1622() { 93 typedef Matrix<double, 2, -1, 0, 2, -1> Mat2X; 94 Mat2X x(2,2); x.setRandom(); 95 MatrixXd y(2,2); y.setRandom(); 96 const Mat2X K1 = x * y.inverse(); 97 const Matrix2d K2 = x * y.inverse(); 98 VERIFY_IS_APPROX(K1,K2); 99 } 100 101 EIGEN_DECLARE_TEST(product_large) 102 { 103 for(int i = 0; i < g_repeat; i++) { 104 CALL_SUBTEST_1( product(MatrixXf(internal::random<int>(1,EIGEN_TEST_MAX_SIZE), internal::random<int>(1,EIGEN_TEST_MAX_SIZE))) ); 105 CALL_SUBTEST_2( product(MatrixXd(internal::random<int>(1,EIGEN_TEST_MAX_SIZE), internal::random<int>(1,EIGEN_TEST_MAX_SIZE))) ); 106 CALL_SUBTEST_2( product(MatrixXd(internal::random<int>(1,10), internal::random<int>(1,10))) ); 107 108 CALL_SUBTEST_3( product(MatrixXi(internal::random<int>(1,EIGEN_TEST_MAX_SIZE), internal::random<int>(1,EIGEN_TEST_MAX_SIZE))) ); 109 CALL_SUBTEST_4( product(MatrixXcf(internal::random<int>(1,EIGEN_TEST_MAX_SIZE/2), internal::random<int>(1,EIGEN_TEST_MAX_SIZE/2))) ); 110 CALL_SUBTEST_5( product(Matrix<float,Dynamic,Dynamic,RowMajor>(internal::random<int>(1,EIGEN_TEST_MAX_SIZE), internal::random<int>(1,EIGEN_TEST_MAX_SIZE))) ); 111 112 CALL_SUBTEST_1( test_aliasing<float>() ); 113 114 CALL_SUBTEST_6( bug_1622<1>() ); 115 116 CALL_SUBTEST_7( product(MatrixXcd(internal::random<int>(1,EIGEN_TEST_MAX_SIZE/2), internal::random<int>(1,EIGEN_TEST_MAX_SIZE/2))) ); 117 CALL_SUBTEST_8( product(Matrix<double,Dynamic,Dynamic,RowMajor>(internal::random<int>(1,EIGEN_TEST_MAX_SIZE), internal::random<int>(1,EIGEN_TEST_MAX_SIZE))) ); 118 CALL_SUBTEST_9( product(Matrix<std::complex<float>,Dynamic,Dynamic,RowMajor>(internal::random<int>(1,EIGEN_TEST_MAX_SIZE), internal::random<int>(1,EIGEN_TEST_MAX_SIZE))) ); 119 CALL_SUBTEST_10( product(Matrix<std::complex<double>,Dynamic,Dynamic,RowMajor>(internal::random<int>(1,EIGEN_TEST_MAX_SIZE), internal::random<int>(1,EIGEN_TEST_MAX_SIZE))) ); 120 } 121 122 CALL_SUBTEST_6( product_large_regressions<0>() ); 123 124 // Regression test for bug 714: 125 #if defined EIGEN_HAS_OPENMP 126 omp_set_dynamic(1); 127 for(int i = 0; i < g_repeat; i++) { 128 CALL_SUBTEST_6( product(Matrix<float,Dynamic,Dynamic>(internal::random<int>(1,EIGEN_TEST_MAX_SIZE), internal::random<int>(1,EIGEN_TEST_MAX_SIZE))) ); 129 } 130 #endif 131 }