1
1
#include < string>
2
2
#include < iostream>
3
3
#include < iomanip>
4
- #include < cmath>
5
-
6
- #include < resolve/matrix/Coo.hpp>
4
+ #include < sstream>
7
5
#include < resolve/matrix/Csr.hpp>
8
- #include < resolve/matrix/Csc.hpp>
9
6
#include < resolve/vector/Vector.hpp>
10
7
#include < resolve/matrix/io.hpp>
11
8
#include < resolve/matrix/MatrixHandler.hpp>
12
9
#include < resolve/vector/VectorHandler.hpp>
13
10
#include < resolve/LinSolverDirectKLU.hpp>
14
11
#include < resolve/workspace/LinAlgWorkspace.hpp>
12
+ #include < resolve/LinSolverIterativeFGMRES.hpp>
15
13
#include " ExampleHelper.hpp"
16
14
#include < resolve/utilities/params/CliOptions.hpp>
17
15
@@ -21,20 +19,19 @@ using namespace ReSolve::constants;
21
19
void printHelpInfo ()
22
20
{
23
21
std::cout << " \n Loads from files and solves a linear system.\n\n " ;
24
- std::cout << " System matrix is in a file with name <matrix file name>\n " ;
25
- std::cout << " System right hand side vector is stored in a file with name <rhs file name>\n\n " ;
26
- std::cout << " kluStandAlone.exe -m <matrix file name> -r <rhs file name> \n\n " ;
22
+ std::cout << " Usage:\n\t ./kluStandAlone.exe -m <matrix pathname> -r <rhs pathname>\n\n " ;
27
23
std::cout << " Optional features:\n\t -h\t Prints this message.\n " ;
28
24
std::cout << " \t -i\t Enables iterative refinement.\n\n " ;
29
25
}
30
26
31
27
int main (int argc, char *argv[])
32
28
{
33
- // Use the same data types as those you specified in ReSolve build.
29
+ using index_type = ReSolve::index_type;
34
30
using real_type = ReSolve::real_type;
35
31
using vector_type = ReSolve::vector::Vector;
36
- using namespace ReSolve ::examples;
37
32
using namespace ReSolve ;
33
+ using namespace ReSolve ::examples;
34
+
38
35
CliOptions options (argc, argv);
39
36
CliOptions::Option* opt = nullptr ;
40
37
@@ -46,93 +43,96 @@ int main(int argc, char *argv[])
46
43
47
44
bool is_iterative_refinement = options.hasKey (" -i" );
48
45
49
- std::string matrix_file_name (" " );
46
+ std::string matrix_path_name (" " );
50
47
opt = options.getParamFromKey (" -m" );
51
48
if (opt) {
52
- matrix_file_name = opt->second ;
49
+ matrix_path_name = opt->second ;
53
50
} else {
54
51
std::cout << " Incorrect input!\n " ;
55
52
printHelpInfo ();
53
+ return 1 ;
56
54
}
57
55
58
- std::string rhs_file_name (" " );
56
+ std::string rhs_path_name (" " );
59
57
opt = options.getParamFromKey (" -r" );
60
58
if (opt) {
61
- rhs_file_name = opt->second ;
59
+ rhs_path_name = opt->second ;
62
60
} else {
63
61
std::cout << " Incorrect input!\n " ;
64
62
printHelpInfo ();
63
+ return 1 ;
65
64
}
66
65
67
66
matrix::Csr* A = nullptr ;
68
- LinAlgWorkspaceCpu* workspace = new LinAlgWorkspaceCpu ();
69
- MatrixHandler* matrix_handler = new MatrixHandler (workspace);
70
- VectorHandler* vector_handler = new VectorHandler (workspace);
71
- real_type* rhs = nullptr ;
72
- real_type* x = nullptr ;
73
-
67
+ LinAlgWorkspaceCpu workspace;
68
+ ExampleHelper<LinAlgWorkspaceCpu> helper (workspace);
69
+ MatrixHandler matrix_handler (&workspace);
70
+ VectorHandler vector_handler (&workspace);
74
71
vector_type* vec_rhs = nullptr ;
75
- vector_type* vec_x = nullptr ;
76
- vector_type* vec_r = nullptr ;
72
+ vector_type* vec_x = nullptr ;
77
73
78
74
LinSolverDirectKLU* KLU = new LinSolverDirectKLU;
79
- // Create a helper object (computing errors, printing summaries, etc.)
80
- examples::ExampleHelper<LinAlgWorkspaceCpu> helper (*workspace);
81
75
82
- std::ifstream mat_file (matrix_file_name);
76
+ // Load the system
77
+ std::cout << " Solving the system:\n " ;
78
+
79
+ std::ifstream mat_file (matrix_path_name);
83
80
if (!mat_file.is_open ())
84
81
{
85
- std::cout << " Failed to open file " << matrix_file_name << " \n " ;
86
- return - 1 ;
82
+ std::cout << " Failed to open file " << matrix_path_name << " \n " ;
83
+ return 1 ;
87
84
}
88
- std::ifstream rhs_file (rhs_file_name);
85
+
86
+ std::ifstream rhs_file (rhs_path_name);
89
87
if (!rhs_file.is_open ())
90
88
{
91
- std::cout << " Failed to open file " << rhs_file_name << " \n " ;
92
- return - 1 ;
89
+ std::cout << " Failed to open file " << rhs_path_name << " \n " ;
90
+ return 1 ;
93
91
}
92
+
94
93
bool is_expand_symmetric = true ;
95
94
A = ReSolve::io::createCsrFromFile (mat_file, is_expand_symmetric);
96
-
97
- rhs = ReSolve::io::createArrayFromFile (rhs_file);
98
- x = new real_type[A->getNumRows ()];
99
- vec_rhs = new vector_type (A->getNumRows ());
95
+ vec_rhs = ReSolve::io::createVectorFromFile (rhs_file);
100
96
vec_x = new vector_type (A->getNumRows ());
101
- vec_r = new vector_type (A->getNumRows ());
102
- helper.resetSystem (A, vec_rhs, vec_x);
103
- printSystemInfo (matrix_file_name, A);
97
+
98
+ printSystemInfo (matrix_path_name, A);
104
99
mat_file.close ();
105
100
rhs_file.close ();
106
101
107
- vec_rhs->copyDataFrom (rhs, ReSolve::memory::HOST, ReSolve::memory::HOST);
108
102
vec_rhs->setDataUpdated (ReSolve::memory::HOST);
109
103
std::cout << " COO to CSR completed. Expanded NNZ: " << A->getNnz () << std::endl;
110
- // Now call direct solver
104
+
105
+ // Direct solver
111
106
int status;
112
107
KLU->setup (A);
113
108
status = KLU->analyze ();
114
- std::cout<< " KLU analysis status: " << status<< std::endl;
109
+ std::cout << " KLU analysis status: " << status << std::endl;
115
110
status = KLU->factorize ();
116
111
std::cout << " KLU factorization status: " << status << std::endl;
112
+
113
+ // Solve the system
117
114
status = KLU->solve (vec_rhs, vec_x);
118
115
std::cout << " KLU solve status: " << status << std::endl;
119
- vec_r->copyDataFrom (rhs, ReSolve::memory::HOST, ReSolve::memory::HOST);
120
-
121
- matrix_handler->setValuesChanged (true , ReSolve::memory::HOST);
122
-
123
- matrix_handler->matvec (A, vec_x, vec_r, &ONE, &MINUSONE, ReSolve::memory::HOST);
124
116
117
+ helper.resetSystem (A, vec_rhs, vec_x);
125
118
helper.printShortSummary ();
119
+ if (is_iterative_refinement) {
120
+ GramSchmidt GS (&vector_handler, GramSchmidt::CGS2);
121
+ LinSolverIterativeFGMRES FGMRES (&matrix_handler, &vector_handler, &GS);
122
+ // Setup iterative refinement
123
+ FGMRES.setup (A);
124
+ FGMRES.setupPreconditioner (" LU" , KLU);
125
+ // If refactorization produced finite solution do iterative refinement
126
+ if (std::isfinite (helper.getNormRelativeResidual ())) {
127
+ FGMRES.solve (vec_rhs, vec_x);
128
+ helper.printIrSummary (&FGMRES);
129
+ }
130
+ }
126
131
127
- // now DELETE
132
+ // Cleanup
128
133
delete A;
129
134
delete KLU;
130
- delete [] x;
131
- delete [] rhs;
132
- delete vec_r;
135
+ delete vec_rhs;
133
136
delete vec_x;
134
- delete matrix_handler;
135
- delete vector_handler;
136
-
137
137
return 0 ;
138
- }
138
+ }
0 commit comments