1*bf2c3715SXin Li // Small bench routine for Eigen available in Eigen
2*bf2c3715SXin Li // (C) Desire NUENTSA WAKAM, INRIA
3*bf2c3715SXin Li
4*bf2c3715SXin Li #include <iostream>
5*bf2c3715SXin Li #include <fstream>
6*bf2c3715SXin Li #include <iomanip>
7*bf2c3715SXin Li #include <Eigen/Jacobi>
8*bf2c3715SXin Li #include <Eigen/Householder>
9*bf2c3715SXin Li #include <Eigen/IterativeLinearSolvers>
10*bf2c3715SXin Li #include <Eigen/LU>
11*bf2c3715SXin Li #include <unsupported/Eigen/SparseExtra>
12*bf2c3715SXin Li //#include <Eigen/SparseLU>
13*bf2c3715SXin Li #include <Eigen/SuperLUSupport>
14*bf2c3715SXin Li // #include <unsupported/Eigen/src/IterativeSolvers/Scaling.h>
15*bf2c3715SXin Li #include <bench/BenchTimer.h>
16*bf2c3715SXin Li #include <unsupported/Eigen/IterativeSolvers>
17*bf2c3715SXin Li using namespace std;
18*bf2c3715SXin Li using namespace Eigen;
19*bf2c3715SXin Li
main(int argc,char ** args)20*bf2c3715SXin Li int main(int argc, char **args)
21*bf2c3715SXin Li {
22*bf2c3715SXin Li SparseMatrix<double, ColMajor> A;
23*bf2c3715SXin Li typedef SparseMatrix<double, ColMajor>::Index Index;
24*bf2c3715SXin Li typedef Matrix<double, Dynamic, Dynamic> DenseMatrix;
25*bf2c3715SXin Li typedef Matrix<double, Dynamic, 1> DenseRhs;
26*bf2c3715SXin Li VectorXd b, x, tmp;
27*bf2c3715SXin Li BenchTimer timer,totaltime;
28*bf2c3715SXin Li //SparseLU<SparseMatrix<double, ColMajor> > solver;
29*bf2c3715SXin Li // SuperLU<SparseMatrix<double, ColMajor> > solver;
30*bf2c3715SXin Li ConjugateGradient<SparseMatrix<double, ColMajor>, Lower,IncompleteCholesky<double,Lower> > solver;
31*bf2c3715SXin Li ifstream matrix_file;
32*bf2c3715SXin Li string line;
33*bf2c3715SXin Li int n;
34*bf2c3715SXin Li // Set parameters
35*bf2c3715SXin Li // solver.iparm(IPARM_THREAD_NBR) = 4;
36*bf2c3715SXin Li /* Fill the matrix with sparse matrix stored in Matrix-Market coordinate column-oriented format */
37*bf2c3715SXin Li if (argc < 2) assert(false && "please, give the matrix market file ");
38*bf2c3715SXin Li
39*bf2c3715SXin Li timer.start();
40*bf2c3715SXin Li totaltime.start();
41*bf2c3715SXin Li loadMarket(A, args[1]);
42*bf2c3715SXin Li cout << "End charging matrix " << endl;
43*bf2c3715SXin Li bool iscomplex=false, isvector=false;
44*bf2c3715SXin Li int sym;
45*bf2c3715SXin Li getMarketHeader(args[1], sym, iscomplex, isvector);
46*bf2c3715SXin Li if (iscomplex) { cout<< " Not for complex matrices \n"; return -1; }
47*bf2c3715SXin Li if (isvector) { cout << "The provided file is not a matrix file\n"; return -1;}
48*bf2c3715SXin Li if (sym != 0) { // symmetric matrices, only the lower part is stored
49*bf2c3715SXin Li SparseMatrix<double, ColMajor> temp;
50*bf2c3715SXin Li temp = A;
51*bf2c3715SXin Li A = temp.selfadjointView<Lower>();
52*bf2c3715SXin Li }
53*bf2c3715SXin Li timer.stop();
54*bf2c3715SXin Li
55*bf2c3715SXin Li n = A.cols();
56*bf2c3715SXin Li // ====== TESTS FOR SPARSE TUTORIAL ======
57*bf2c3715SXin Li // cout<< "OuterSize " << A.outerSize() << " inner " << A.innerSize() << endl;
58*bf2c3715SXin Li // SparseMatrix<double, RowMajor> mat1(A);
59*bf2c3715SXin Li // SparseMatrix<double, RowMajor> mat2;
60*bf2c3715SXin Li // cout << " norm of A " << mat1.norm() << endl; ;
61*bf2c3715SXin Li // PermutationMatrix<Dynamic, Dynamic, int> perm(n);
62*bf2c3715SXin Li // perm.resize(n,1);
63*bf2c3715SXin Li // perm.indices().setLinSpaced(n, 0, n-1);
64*bf2c3715SXin Li // mat2 = perm * mat1;
65*bf2c3715SXin Li // mat.subrows();
66*bf2c3715SXin Li // mat2.resize(n,n);
67*bf2c3715SXin Li // mat2.reserve(10);
68*bf2c3715SXin Li // mat2.setConstant();
69*bf2c3715SXin Li // std::cout<< "NORM " << mat1.squaredNorm()<< endl;
70*bf2c3715SXin Li
71*bf2c3715SXin Li cout<< "Time to load the matrix " << timer.value() <<endl;
72*bf2c3715SXin Li /* Fill the right hand side */
73*bf2c3715SXin Li
74*bf2c3715SXin Li // solver.set_restart(374);
75*bf2c3715SXin Li if (argc > 2)
76*bf2c3715SXin Li loadMarketVector(b, args[2]);
77*bf2c3715SXin Li else
78*bf2c3715SXin Li {
79*bf2c3715SXin Li b.resize(n);
80*bf2c3715SXin Li tmp.resize(n);
81*bf2c3715SXin Li // tmp.setRandom();
82*bf2c3715SXin Li for (int i = 0; i < n; i++) tmp(i) = i;
83*bf2c3715SXin Li b = A * tmp ;
84*bf2c3715SXin Li }
85*bf2c3715SXin Li // Scaling<SparseMatrix<double> > scal;
86*bf2c3715SXin Li // scal.computeRef(A);
87*bf2c3715SXin Li // b = scal.LeftScaling().cwiseProduct(b);
88*bf2c3715SXin Li
89*bf2c3715SXin Li /* Compute the factorization */
90*bf2c3715SXin Li cout<< "Starting the factorization "<< endl;
91*bf2c3715SXin Li timer.reset();
92*bf2c3715SXin Li timer.start();
93*bf2c3715SXin Li cout<< "Size of Input Matrix "<< b.size()<<"\n\n";
94*bf2c3715SXin Li cout<< "Rows and columns "<< A.rows() <<" " <<A.cols() <<"\n";
95*bf2c3715SXin Li solver.compute(A);
96*bf2c3715SXin Li // solver.analyzePattern(A);
97*bf2c3715SXin Li // solver.factorize(A);
98*bf2c3715SXin Li if (solver.info() != Success) {
99*bf2c3715SXin Li std::cout<< "The solver failed \n";
100*bf2c3715SXin Li return -1;
101*bf2c3715SXin Li }
102*bf2c3715SXin Li timer.stop();
103*bf2c3715SXin Li float time_comp = timer.value();
104*bf2c3715SXin Li cout <<" Compute Time " << time_comp<< endl;
105*bf2c3715SXin Li
106*bf2c3715SXin Li timer.reset();
107*bf2c3715SXin Li timer.start();
108*bf2c3715SXin Li x = solver.solve(b);
109*bf2c3715SXin Li // x = scal.RightScaling().cwiseProduct(x);
110*bf2c3715SXin Li timer.stop();
111*bf2c3715SXin Li float time_solve = timer.value();
112*bf2c3715SXin Li cout<< " Time to solve " << time_solve << endl;
113*bf2c3715SXin Li
114*bf2c3715SXin Li /* Check the accuracy */
115*bf2c3715SXin Li VectorXd tmp2 = b - A*x;
116*bf2c3715SXin Li double tempNorm = tmp2.norm()/b.norm();
117*bf2c3715SXin Li cout << "Relative norm of the computed solution : " << tempNorm <<"\n";
118*bf2c3715SXin Li // cout << "Iterations : " << solver.iterations() << "\n";
119*bf2c3715SXin Li
120*bf2c3715SXin Li totaltime.stop();
121*bf2c3715SXin Li cout << "Total time " << totaltime.value() << "\n";
122*bf2c3715SXin Li // std::cout<<x.transpose()<<"\n";
123*bf2c3715SXin Li
124*bf2c3715SXin Li return 0;
125*bf2c3715SXin Li }
126