sp_solver.cpp File Reference
#include <iostream>
#include <fstream>
#include <iomanip>
#include <Eigen/Jacobi>
#include <Eigen/Householder>
#include <Eigen/IterativeLinearSolvers>
#include <Eigen/LU>
#include <unsupported/Eigen/SparseExtra>
#include <Eigen/SuperLUSupport>
#include <bench/BenchTimer.h>
#include <unsupported/Eigen/IterativeSolvers>

Functions

int main (int argc, char **args)
 

Function Documentation

◆ main()

int main ( int argc  ,
char **  args 
)
20  {
24  typedef Matrix<double, Dynamic, 1> DenseRhs;
25  VectorXd b, x, tmp;
26  BenchTimer timer, totaltime;
27  // SparseLU<SparseMatrix<double, ColMajor> > solver;
28  // SuperLU<SparseMatrix<double, ColMajor> > solver;
30  ifstream matrix_file;
31  string line;
32  int n;
33  // Set parameters
34  // solver.iparm(IPARM_THREAD_NBR) = 4;
35  /* Fill the matrix with sparse matrix stored in Matrix-Market coordinate column-oriented format */
36  if (argc < 2) assert(false && "please, give the matrix market file ");
37 
38  timer.start();
39  totaltime.start();
40  loadMarket(A, args[1]);
41  cout << "End charging matrix " << endl;
42  bool iscomplex = false, isvector = false;
43  int sym;
44  getMarketHeader(args[1], sym, iscomplex, isvector);
45  if (iscomplex) {
46  cout << " Not for complex matrices \n";
47  return -1;
48  }
49  if (isvector) {
50  cout << "The provided file is not a matrix file\n";
51  return -1;
52  }
53  if (sym != 0) { // symmetric matrices, only the lower part is stored
55  temp = A;
56  A = temp.selfadjointView<Lower>();
57  }
58  timer.stop();
59 
60  n = A.cols();
61  // ====== TESTS FOR SPARSE TUTORIAL ======
62  // cout<< "OuterSize " << A.outerSize() << " inner " << A.innerSize() << endl;
63  // SparseMatrix<double, RowMajor> mat1(A);
64  // SparseMatrix<double, RowMajor> mat2;
65  // cout << " norm of A " << mat1.norm() << endl; ;
66  // PermutationMatrix<Dynamic, Dynamic, int> perm(n);
67  // perm.resize(n,1);
68  // perm.indices().setLinSpaced(n, 0, n-1);
69  // mat2 = perm * mat1;
70  // mat.subrows();
71  // mat2.resize(n,n);
72  // mat2.reserve(10);
73  // mat2.setConstant();
74  // std::cout<< "NORM " << mat1.squaredNorm()<< endl;
75 
76  cout << "Time to load the matrix " << timer.value() << endl;
77  /* Fill the right hand side */
78 
79  // solver.set_restart(374);
80  if (argc > 2)
81  loadMarketVector(b, args[2]);
82  else {
83  b.resize(n);
84  tmp.resize(n);
85  // tmp.setRandom();
86  for (int i = 0; i < n; i++) tmp(i) = i;
87  b = A * tmp;
88  }
89  // Scaling<SparseMatrix<double> > scal;
90  // scal.computeRef(A);
91  // b = scal.LeftScaling().cwiseProduct(b);
92 
93  /* Compute the factorization */
94  cout << "Starting the factorization " << endl;
95  timer.reset();
96  timer.start();
97  cout << "Size of Input Matrix " << b.size() << "\n\n";
98  cout << "Rows and columns " << A.rows() << " " << A.cols() << "\n";
99  solver.compute(A);
100  // solver.analyzePattern(A);
101  // solver.factorize(A);
102  if (solver.info() != Success) {
103  std::cout << "The solver failed \n";
104  return -1;
105  }
106  timer.stop();
107  float time_comp = timer.value();
108  cout << " Compute Time " << time_comp << endl;
109 
110  timer.reset();
111  timer.start();
112  x = solver.solve(b);
113  // x = scal.RightScaling().cwiseProduct(x);
114  timer.stop();
115  float time_solve = timer.value();
116  cout << " Time to solve " << time_solve << endl;
117 
118  /* Check the accuracy */
119  VectorXd tmp2 = b - A * x;
120  double tempNorm = tmp2.norm() / b.norm();
121  cout << "Relative norm of the computed solution : " << tempNorm << "\n";
122  // cout << "Iterations : " << solver.iterations() << "\n";
123 
124  totaltime.stop();
125  cout << "Total time " << totaltime.value() << "\n";
126  // std::cout<<x.transpose()<<"\n";
127 
128  return 0;
129 }
Matrix< Scalar, Dynamic, Dynamic > DenseMatrix
Definition: BenchSparseUtil.h:23
BiCGSTAB< SparseMatrix< double > > solver
Definition: BiCGSTAB_simple.cpp:5
int i
Definition: BiCGSTAB_step_by_step.cpp:9
const unsigned n
Definition: CG3DPackingUnitTest.cpp:11
#define assert(e,...)
Definition: Logger.h:744
Scalar * b
Definition: benchVecAdd.cpp:17
Matrix< SCALARA, Dynamic, Dynamic, opt_A > A
Definition: bench_gemm.cpp:47
Definition: BenchTimer.h:55
double value(int TIMER=CPU_TIMER) const
Definition: BenchTimer.h:94
void stop()
Definition: BenchTimer.h:77
void start()
Definition: BenchTimer.h:73
A conjugate gradient solver for sparse (or dense) self-adjoint problems.
Definition: ConjugateGradient.h:152
Modified Incomplete Cholesky with dual threshold.
Definition: IncompleteCholesky.h:51
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE EIGEN_CONSTEXPR Index cols() const EIGEN_NOEXCEPT
Definition: PlainObjectBase.h:192
EIGEN_DEVICE_FUNC constexpr EIGEN_STRONG_INLINE void resize(Index rows, Index cols)
Definition: PlainObjectBase.h:294
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE EIGEN_CONSTEXPR Index rows() const EIGEN_NOEXCEPT
Definition: PlainObjectBase.h:191
ConstSelfAdjointViewReturnType< UpLo >::Type selfadjointView() const
A versatible sparse matrix representation.
Definition: SparseMatrix.h:121
@ Lower
Definition: Constants.h:211
@ Success
Definition: Constants.h:440
Eigen::Matrix< Scalar, Dynamic, Dynamic, ColMajor > tmp
Definition: level3_impl.h:365
bool loadMarketVector(VectorType &vec, const std::string &filename)
Same functionality as loadMarketDense, deprecated.
Definition: MarketIO.h:284
bool loadMarket(SparseMatrixType &mat, const std::string &filename)
Loads a sparse matrix from a matrixmarket format file.
Definition: MarketIO.h:156
bool getMarketHeader(const std::string &filename, int &sym, bool &iscomplex, bool &isdense)
Reads the header of a matrixmarket file and determines the properties of a matrix.
Definition: MarketIO.h:122
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:83
line
Definition: calibrate.py:103
args
Definition: compute_granudrum_aor.py:143
list x
Definition: plotDoE.py:28
double timer
Definition: oomph_metis_from_parmetis_3.1.1/struct.h:210

References compute_granudrum_aor::args, assert, b, Eigen::PlainObjectBase< Derived >::cols(), Eigen::getMarketHeader(), i, calibrate::line, Eigen::loadMarket(), Eigen::loadMarketVector(), Eigen::Lower, n, Eigen::PlainObjectBase< Derived >::resize(), Eigen::PlainObjectBase< Derived >::rows(), Eigen::SparseMatrixBase< Derived >::selfadjointView(), solver, Eigen::BenchTimer::start(), Eigen::BenchTimer::stop(), Eigen::Success, tmp, Eigen::BenchTimer::value(), and plotDoE::x.