product_large.cpp
Go to the documentation of this file.
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>
15 {
16  int rows = internal::random<int>(1,12);
17  int cols = internal::random<int>(1,12);
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>
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 }
Matrix< SCALARB, Dynamic, Dynamic, opt_B > B
Definition: bench_gemm.cpp:49
EIGEN_DECLARE_TEST(product_large)
Matrix3f m
void setCpuCacheSizes(std::ptrdiff_t l1, std::ptrdiff_t l2, std::ptrdiff_t l3)
#define CALL_SUBTEST_9(FUNC)
#define CALL_SUBTEST_6(FUNC)
#define CALL_SUBTEST_4(FUNC)
Scalar * y
Scalar * b
Definition: benchVecAdd.cpp:17
#define CALL_SUBTEST_3(FUNC)
#define CALL_SUBTEST_7(FUNC)
Scalar Scalar * c
Definition: benchVecAdd.cpp:17
gtsam::Key l2
void bug_1622()
MatrixXf MatrixType
MatrixXd mat1(size, size)
Matrix< SCALARA, Dynamic, Dynamic, opt_A > A
Definition: bench_gemm.cpp:48
#define N
Definition: gksort.c:12
void test_aliasing()
#define CALL_SUBTEST_10(FUNC)
Point3 l3(2, 2, 0)
static const Cal3Bundler K2(625, 1e-3, 1e-3)
#define VERIFY_IS_APPROX(a, b)
#define CALL_SUBTEST_1(FUNC)
std::ptrdiff_t l2CacheSize()
Matrix3d m1
Definition: IOFormat.cpp:2
static int g_repeat
Definition: main.h:169
Array< int, Dynamic, 1 > v
Eigen::Triplet< double > T
#define CALL_SUBTEST_8(FUNC)
static const double r2
static Cal3_S2::shared_ptr K1(new Cal3_S2(fov, w, h))
Matrix< Scalar, Dynamic, Dynamic > C
Definition: bench_gemm.cpp:50
gtsam::Key l1
void omp_set_dynamic(int num_threads)
#define CALL_SUBTEST_5(FUNC)
static const double r1
#define VERIFY(a)
Definition: main.h:380
#define EIGEN_TEST_MAX_SIZE
void product_large_regressions()
#define CALL_SUBTEST_2(FUNC)
internal::nested_eval< T, 1 >::type eval(const T &xpr)
const int Dynamic
Definition: Constants.h:22
The matrix class, also used for vectors and row-vectors.
set noclip points set clip one set noclip two set bar set border lt lw set xdata set ydata set zdata set x2data set y2data set boxwidth set dummy x
std::ptrdiff_t l1CacheSize()
void product(const MatrixType &m)
Definition: product.h:20


gtsam
Author(s):
autogenerated on Tue Jul 4 2023 02:35:18