xref: /aosp_15_r20/external/eigen/bench/spbench/spbenchsolver.cpp (revision bf2c37156dfe67e5dfebd6d394bad8b2ab5804d4)
1*bf2c3715SXin Li #include <bench/spbench/spbenchsolver.h>
2*bf2c3715SXin Li 
bench_printhelp()3*bf2c3715SXin Li void bench_printhelp()
4*bf2c3715SXin Li {
5*bf2c3715SXin Li     cout<< " \nbenchsolver : performs a benchmark of all the solvers available in Eigen \n\n";
6*bf2c3715SXin Li     cout<< " MATRIX FOLDER : \n";
7*bf2c3715SXin Li     cout<< " The matrices for the benchmark should be collected in a folder specified with an environment variable EIGEN_MATRIXDIR \n";
8*bf2c3715SXin Li     cout<< " The matrices are stored using the matrix market coordinate format \n";
9*bf2c3715SXin Li     cout<< " The matrix and associated right-hand side (rhs) files are named respectively \n";
10*bf2c3715SXin Li     cout<< " as MatrixName.mtx and MatrixName_b.mtx. If the rhs does not exist, a random one is generated. \n";
11*bf2c3715SXin Li     cout<< " If a matrix is SPD, the matrix should be named as MatrixName_SPD.mtx \n";
12*bf2c3715SXin Li     cout<< " If a true solution exists, it should be named as MatrixName_x.mtx; \n"     ;
13*bf2c3715SXin Li     cout<< " it will be used to compute the norm of the error relative to the computed solutions\n\n";
14*bf2c3715SXin Li     cout<< " OPTIONS : \n";
15*bf2c3715SXin Li     cout<< " -h or --help \n    print this help and return\n\n";
16*bf2c3715SXin Li     cout<< " -d matrixdir \n    Use matrixdir as the matrix folder instead of the one specified in the environment variable EIGEN_MATRIXDIR\n\n";
17*bf2c3715SXin Li     cout<< " -o outputfile.xml \n    Output the statistics to a xml file \n\n";
18*bf2c3715SXin Li     cout<< " --eps <RelErr> Sets the relative tolerance for iterative solvers (default 1e-08) \n\n";
19*bf2c3715SXin Li     cout<< " --maxits <MaxIts> Sets the maximum number of iterations (default 1000) \n\n";
20*bf2c3715SXin Li 
21*bf2c3715SXin Li }
main(int argc,char ** args)22*bf2c3715SXin Li int main(int argc, char ** args)
23*bf2c3715SXin Li {
24*bf2c3715SXin Li 
25*bf2c3715SXin Li   bool help = ( get_options(argc, args, "-h") || get_options(argc, args, "--help") );
26*bf2c3715SXin Li   if(help) {
27*bf2c3715SXin Li     bench_printhelp();
28*bf2c3715SXin Li     return 0;
29*bf2c3715SXin Li   }
30*bf2c3715SXin Li 
31*bf2c3715SXin Li   // Get the location of the test matrices
32*bf2c3715SXin Li   string matrix_dir;
33*bf2c3715SXin Li   if (!get_options(argc, args, "-d", &matrix_dir))
34*bf2c3715SXin Li   {
35*bf2c3715SXin Li     if(getenv("EIGEN_MATRIXDIR") == NULL){
36*bf2c3715SXin Li       std::cerr << "Please, specify the location of the matrices with -d mat_folder or the environment variable EIGEN_MATRIXDIR \n";
37*bf2c3715SXin Li       std::cerr << " Run with --help to see the list of all the available options \n";
38*bf2c3715SXin Li       return -1;
39*bf2c3715SXin Li     }
40*bf2c3715SXin Li     matrix_dir = getenv("EIGEN_MATRIXDIR");
41*bf2c3715SXin Li   }
42*bf2c3715SXin Li 
43*bf2c3715SXin Li   std::ofstream statbuf;
44*bf2c3715SXin Li   string statFile ;
45*bf2c3715SXin Li 
46*bf2c3715SXin Li   // Get the file to write the statistics
47*bf2c3715SXin Li   bool statFileExists = get_options(argc, args, "-o", &statFile);
48*bf2c3715SXin Li   if(statFileExists)
49*bf2c3715SXin Li   {
50*bf2c3715SXin Li     statbuf.open(statFile.c_str(), std::ios::out);
51*bf2c3715SXin Li     if(statbuf.good()){
52*bf2c3715SXin Li       statFileExists = true;
53*bf2c3715SXin Li       printStatheader(statbuf);
54*bf2c3715SXin Li       statbuf.close();
55*bf2c3715SXin Li     }
56*bf2c3715SXin Li     else
57*bf2c3715SXin Li       std::cerr << "Unable to open the provided file for writing... \n";
58*bf2c3715SXin Li   }
59*bf2c3715SXin Li 
60*bf2c3715SXin Li   // Get the maximum number of iterations and the tolerance
61*bf2c3715SXin Li   int maxiters = 1000;
62*bf2c3715SXin Li   double tol = 1e-08;
63*bf2c3715SXin Li   string inval;
64*bf2c3715SXin Li   if (get_options(argc, args, "--eps", &inval))
65*bf2c3715SXin Li     tol = atof(inval.c_str());
66*bf2c3715SXin Li   if(get_options(argc, args, "--maxits", &inval))
67*bf2c3715SXin Li     maxiters = atoi(inval.c_str());
68*bf2c3715SXin Li 
69*bf2c3715SXin Li   string current_dir;
70*bf2c3715SXin Li   // Test the real-arithmetics matrices
71*bf2c3715SXin Li   Browse_Matrices<double>(matrix_dir, statFileExists, statFile,maxiters, tol);
72*bf2c3715SXin Li 
73*bf2c3715SXin Li   // Test the complex-arithmetics matrices
74*bf2c3715SXin Li   Browse_Matrices<std::complex<double> >(matrix_dir, statFileExists, statFile, maxiters, tol);
75*bf2c3715SXin Li 
76*bf2c3715SXin Li   if(statFileExists)
77*bf2c3715SXin Li   {
78*bf2c3715SXin Li     statbuf.open(statFile.c_str(), std::ios::app);
79*bf2c3715SXin Li     statbuf << "</BENCH> \n";
80*bf2c3715SXin Li     cout << "\n Output written in " << statFile << " ...\n";
81*bf2c3715SXin Li     statbuf.close();
82*bf2c3715SXin Li   }
83*bf2c3715SXin Li 
84*bf2c3715SXin Li   return 0;
85*bf2c3715SXin Li }
86*bf2c3715SXin Li 
87*bf2c3715SXin Li 
88