eigen_sparse.cpp
Go to the documentation of this file.
1 
9 /*****************************************************************************
10 ** Preprocessing
11 *****************************************************************************/
12 
13 #define EIGEN_YES_I_KNOW_SPARSE_MODULE_IS_NOT_STABLE_YET
14 
15 /*****************************************************************************
16 ** Includes
17 *****************************************************************************/
18 
19 #include <iostream>
20 #include <ecl/time.hpp>
21 #include <ecl/threads/priority.hpp>
22 #include <ecl/linear_algebra.hpp>
23 #include <ecl/linear_algebra/sparse.hpp>
24 
25 /*****************************************************************************
26 ** Using
27 *****************************************************************************/
28 
29 using ecl::linear_algebra::SparseMatrix;
30 using ecl::linear_algebra::DynamicSparseMatrix;
31 using ecl::linear_algebra::MatrixXd;
32 using ecl::StopWatch;
33 
34 /*****************************************************************************
35 ** Main
36 *****************************************************************************/
37 #define LM (303)
38 void test_sparse_matrix( int num )
39 {
40  DynamicSparseMatrix<double> hh(20, LM);
41  MatrixXd P(LM,LM);
42  hh.setZero();
43  P.setZero();
44  for( int i=0; i<20; i++ )
45  for( int j=0; j<3; j++ )
46  hh.coeffRef(i,j) = 1;
47 
48  for( int i=0; i<10; i++)
49  {
50  for( int j=0; j<2; j++ )
51  {
52  for( int k=0; k<3; k++ )
53  {
54  hh.coeffRef( i*2+j, (i*4)+k ) = 1;
55  }
56  }
57  }
58 
59  SparseMatrix<double> H(hh);
60  ecl::StopWatch time;
61  for( int i=0; i<num; i++ )
62  {
63  MatrixXd S = H*P*H.transpose();
64  S(4,4)= i;
65  }
66  std::cout << time.elapsed();
67 }
68 
69 void test_dense_matrix( int num )
70 {
71  MatrixXd H(20, LM);
72  MatrixXd P(LM,LM);
73  H.setZero();
74  P.setZero();
75  for( int i=0; i<20; i++ )
76  for( int j=0; j<3; j++ )
77  H(i,j) = 1;
78 
79  for( int i=0; i<10; i++)
80  {
81  for( int j=0; j<2; j++ )
82  {
83  for( int k=0; k<3; k++ )
84  {
85  H( i*2+j, (i*4)+k ) = 1;
86  }
87  }
88  }
89 
90  ecl::StopWatch time;
91  for( int i=0; i<num; i++ )
92  {
93  MatrixXd S = H*P*H.transpose();
94  S(4,4)= i;
95  }
96  std::cout << time.elapsed();
97 }
98 
100 {
101  std::cout << "==========================" << std::endl;
102  std::cout << "S=H P T'" << std::endl;
103  std::cout << "Start sparse: ";
104  test_sparse_matrix(1000);
105  std::cout << std::endl;
106 
107  std::cout << "Start dense: ";
108  test_dense_matrix(1000);
109  std::cout << std::endl;
110  std::cout << "==========================" << std::endl;
111 }
112 
113 void fill_sparse( int num )
114 {
115  ecl::StopWatch time;
116  DynamicSparseMatrix<double> hh(20, LM);
117  for( int i=0; i<num; i++)
118  {
119  for( int j=0; j<20; j++ )
120  for( int k=0; k<LM; k++ )
121  hh.coeffRef(j,k) = (double)j*k;
122  }
123 
124  std::cout << time.elapsed();
125 }
126 
127 void fill_dense( int num )
128 {
129  ecl::StopWatch time;
130  MatrixXd hh(20, LM);
131  for( int i=0; i<num; i++ )
132  {
133  for( int j=0; j<20; j++ )
134  for( int k=0; k<LM; k++ )
135  hh(j,k) = (double)j*k;
136  }
137 
138  std::cout << time.elapsed();
139 }
140 
142 {
143  std::cout << "==========================" << std::endl;
144  std::cout << "Filling " << std::endl;
145  std::cout << "fill sparse: ";
146  fill_sparse(1000);
147  std::cout << std::endl;
148  std::cout << "fill dense: ";
149  fill_dense(1000);
150  std::cout << std::endl;
151  std::cout << "==========================" << std::endl;
152 }
153 
154 int main(int argc, char **argv) {
155 
156  try {
157  ecl::set_priority(ecl::RealTimePriority4);
158  } catch ( ecl::StandardException &e ) {
159  // dont worry about it.
160  }
161 
163  compare_filling();
164 
165  return 0;
166 }
fill_sparse
void fill_sparse(int num)
Definition: eigen_sparse.cpp:113
LM
#define LM
Definition: eigen_sparse.cpp:37
test_dense_matrix
void test_dense_matrix(int num)
Definition: eigen_sparse.cpp:69
fill_dense
void fill_dense(int num)
Definition: eigen_sparse.cpp:127
linear_algebra.hpp
ecl::StandardException
compare_filling
void compare_filling()
Definition: eigen_sparse.cpp:141
time.hpp
compare_multiplication
void compare_multiplication()
Definition: eigen_sparse.cpp:99
test_sparse_matrix
void test_sparse_matrix(int num)
Definition: eigen_sparse.cpp:38
priority.hpp
main
int main(int argc, char **argv)
Definition: eigen_sparse.cpp:154
ecl::RealTimePriority4
RealTimePriority4


ecl_core_apps
Author(s): Daniel Stonier
autogenerated on Wed Mar 2 2022 00:16:52