sparse_lu.cpp
Go to the documentation of this file.
1 
2 // g++ -I.. sparse_lu.cpp -O3 -g0 -I /usr/include/superlu/ -lsuperlu -lgfortran -DSIZE=1000 -DDENSITY=.05 && ./a.out
3 
4 #define EIGEN_SUPERLU_SUPPORT
5 #define EIGEN_UMFPACK_SUPPORT
6 #include <Eigen/Sparse>
7 
8 #define NOGMM
9 #define NOMTL
10 
11 #ifndef SIZE
12 #define SIZE 10
13 #endif
14 
15 #ifndef DENSITY
16 #define DENSITY 0.01
17 #endif
18 
19 #ifndef REPEAT
20 #define REPEAT 1
21 #endif
22 
23 #include "BenchSparseUtil.h"
24 
25 #ifndef MINDENSITY
26 #define MINDENSITY 0.0004
27 #endif
28 
29 #ifndef NBTRIES
30 #define NBTRIES 10
31 #endif
32 
33 #define BENCH(X) \
34  timer.reset(); \
35  for (int _j=0; _j<NBTRIES; ++_j) { \
36  timer.start(); \
37  for (int _k=0; _k<REPEAT; ++_k) { \
38  X \
39  } timer.stop(); }
40 
42 
43 #include <Eigen/LU>
44 
45 template<int Backend>
46 void doEigen(const char* name, const EigenSparseMatrix& sm1, const VectorX& b, VectorX& x, int flags = 0)
47 {
48  std::cout << name << "..." << std::flush;
51  timer.stop();
52  if (lu.succeeded())
53  std::cout << ":\t" << timer.value() << endl;
54  else
55  {
56  std::cout << ":\t FAILED" << endl;
57  return;
58  }
59 
60  bool ok;
61  timer.reset(); timer.start();
62  ok = lu.solve(b,&x);
63  timer.stop();
64  if (ok)
65  std::cout << " solve:\t" << timer.value() << endl;
66  else
67  std::cout << " solve:\t" << " FAILED" << endl;
68 
69  //std::cout << x.transpose() << "\n";
70 }
71 
72 int main(int argc, char *argv[])
73 {
74  int rows = SIZE;
75  int cols = SIZE;
76  float density = DENSITY;
78 
79  VectorX b = VectorX::Random(cols);
80  VectorX x = VectorX::Random(cols);
81 
82  bool densedone = false;
83 
84  //for (float density = DENSITY; density>=MINDENSITY; density*=0.5)
85 // float density = 0.5;
86  {
88  fillMatrix(density, rows, cols, sm1);
89 
90  // dense matrices
91  #ifdef DENSEMATRIX
92  if (!densedone)
93  {
94  densedone = true;
95  std::cout << "Eigen Dense\t" << density*100 << "%\n";
97  eiToDense(sm1, m1);
98 
100  timer.start();
102  timer.stop();
103  std::cout << "Eigen/dense:\t" << timer.value() << endl;
104 
105  timer.reset();
106  timer.start();
107  lu.solve(b,&x);
108  timer.stop();
109  std::cout << " solve:\t" << timer.value() << endl;
110 // std::cout << b.transpose() << "\n";
111 // std::cout << x.transpose() << "\n";
112  }
113  #endif
114 
115  #ifdef EIGEN_UMFPACK_SUPPORT
116  x.setZero();
117  doEigen<Eigen::UmfPack>("Eigen/UmfPack (auto)", sm1, b, x, 0);
118  #endif
119 
120  #ifdef EIGEN_SUPERLU_SUPPORT
121  x.setZero();
122  doEigen<Eigen::SuperLU>("Eigen/SuperLU (nat)", sm1, b, x, Eigen::NaturalOrdering);
123 // doEigen<Eigen::SuperLU>("Eigen/SuperLU (MD AT+A)", sm1, b, x, Eigen::MinimumDegree_AT_PLUS_A);
124 // doEigen<Eigen::SuperLU>("Eigen/SuperLU (MD ATA)", sm1, b, x, Eigen::MinimumDegree_ATA);
125  doEigen<Eigen::SuperLU>("Eigen/SuperLU (COLAMD)", sm1, b, x, Eigen::ColApproxMinimumDegree);
126  #endif
127 
128  }
129 
130  return 0;
131 }
132 
doEigen
void doEigen(const char *name, const EigenSparseMatrix &sm1, const VectorX &b, VectorX &x, int flags=0)
Definition: sparse_lu.cpp:46
main
int main(int argc, char *argv[])
Definition: sparse_lu.cpp:72
name
Annotation for function names.
Definition: attr.h:51
Eigen::SparseMatrix< Scalar >
eiToDense
void eiToDense(const EigenSparseMatrix &src, DenseMatrix &dst)
Definition: BenchSparseUtil.h:62
fillMatrix
void fillMatrix(float density, int rows, int cols, EigenSparseMatrix &dst)
Definition: BenchSparseUtil.h:27
timer
static BenchTimer timer
Definition: benchmark-blocking-sizes.cpp:31
b
Scalar * b
Definition: benchVecAdd.cpp:17
x
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
Definition: gnuplot_common_settings.hh:12
m1
Matrix3d m1
Definition: IOFormat.cpp:2
Eigen::FullPivLU
LU decomposition of a matrix with complete pivoting, and related features.
Definition: ForwardDeclarations.h:268
rows
int rows
Definition: Tutorial_commainit_02.cpp:1
Eigen::BenchTimer::value
double value(int TIMER=CPU_TIMER) const
Definition: BenchTimer.h:104
DENSITY
#define DENSITY
Definition: sparse_lu.cpp:16
SIZE
#define SIZE
Definition: sparse_lu.cpp:12
Eigen::BenchTimer::start
void start()
Definition: BenchTimer.h:81
Eigen::BenchTimer
Definition: BenchTimer.h:59
density
Definition: testGaussianConditional.cpp:127
Eigen::BenchTimer::reset
void reset()
Definition: BenchTimer.h:75
lu
cout<< "Here is the matrix m:"<< endl<< m<< endl;Eigen::FullPivLU< Matrix5x3 > lu(m)
DenseMatrix
Matrix< Scalar, Dynamic, Dynamic > DenseMatrix
Definition: BenchSparseUtil.h:23
Eigen::BenchTimer::stop
void stop()
Definition: BenchTimer.h:86
VectorX
Matrix< Scalar, Dynamic, 1 > VectorX
Definition: sparse_lu.cpp:41
Eigen::Matrix< Scalar, Dynamic, 1 >
Eigen::SparseLU
Sparse supernodal LU factorization for general matrices.
Definition: SparseLU.h:17
cols
int cols
Definition: Tutorial_commainit_02.cpp:1
Eigen::NaturalOrdering
Definition: 3rdparty/Eigen/Eigen/src/OrderingMethods/Ordering.h:91
BenchSparseUtil.h


gtsam
Author(s):
autogenerated on Sat Nov 16 2024 04:04:24