Skip to content

Commit 83c31b3

Browse files
committed
added iterative refinement and other changes
1 parent 7ad6958 commit 83c31b3

File tree

2 files changed

+80
-79
lines changed

2 files changed

+80
-79
lines changed

examples/kluRefactor.cpp

+28-27
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include <resolve/matrix/MatrixHandler.hpp>
1212
#include <resolve/vector/VectorHandler.hpp>
1313
#include <resolve/LinSolverDirectKLU.hpp>
14+
#include <resolve/LinSolverIterativeFGMRES.hpp>
1415
#include <resolve/workspace/LinAlgWorkspace.hpp>
1516
#include "ExampleHelper.hpp"
1617
#include <resolve/utilities/params/CliOptions.hpp>
@@ -57,6 +58,7 @@ int main(int argc, char *argv[])
5758
} else {
5859
std::cout << "Incorrect input!\n";
5960
printHelpInfo();
61+
return 1;
6062
}
6163

6264
std::string matrix_path_name("");
@@ -66,6 +68,7 @@ int main(int argc, char *argv[])
6668
} else {
6769
std::cout << "Incorrect input!\n";
6870
printHelpInfo();
71+
return 1;
6972
}
7073

7174
std::string rhs_path_name("");
@@ -75,6 +78,7 @@ int main(int argc, char *argv[])
7578
} else {
7679
std::cout << "Incorrect input!\n";
7780
printHelpInfo();
81+
return 1;
7882
}
7983

8084
std::string fileId;
@@ -85,17 +89,15 @@ int main(int argc, char *argv[])
8589
matrix::Csr* A = nullptr;
8690
LinAlgWorkspaceCpu workspace;
8791
ExampleHelper<LinAlgWorkspaceCpu> helper(workspace);
88-
MatrixHandler* matrix_handler = new MatrixHandler(&workspace);
89-
VectorHandler* vector_handler = new VectorHandler(&workspace);
90-
real_type* rhs = nullptr;
91-
real_type* x = nullptr;
92+
MatrixHandler matrix_handler(&workspace);
93+
VectorHandler vector_handler(&workspace);
9294

9395
vector_type* vec_rhs = nullptr;
9496
vector_type* vec_x = nullptr;
95-
vector_type* vec_r = nullptr;
9697

9798
LinSolverDirectKLU* KLU = new LinSolverDirectKLU;
98-
99+
GramSchmidt GS(&vector_handler, GramSchmidt::CGS2);
100+
LinSolverIterativeFGMRES FGMRES(&matrix_handler, &vector_handler, &GS);
99101
for (int i = 0; i < num_systems; ++i)
100102
{
101103
std::cout << "System " << i << ":\n";
@@ -104,41 +106,37 @@ int main(int argc, char *argv[])
104106
std::ostringstream rhsname;
105107
matname << matrix_path_name << std::setfill('0') << std::setw(2) << i << ".mtx";
106108
rhsname << rhs_path_name << std::setfill('0') << std::setw(2) << i << ".mtx";
109+
matrix_file_name_full = matname.str();
110+
rhs_file_name_full = rhsname.str();
107111
std::ifstream mat_file(matrix_file_name_full);
108112
if(!mat_file.is_open())
109113
{
110114
std::cout << "Failed to open file " << matrix_file_name_full << "\n";
111-
return -1;
115+
return 1;
112116
}
113117
std::ifstream rhs_file(rhs_file_name_full);
114118
if(!rhs_file.is_open())
115119
{
116120
std::cout << "Failed to open file " << rhs_file_name_full << "\n";
117-
return -1;
121+
return 1;
118122
}
119123
bool is_expand_symmetric = true;
120124
if (i == 0) {
121125
A = ReSolve::io::createCsrFromFile(mat_file, is_expand_symmetric);
122126

123-
rhs = ReSolve::io::createArrayFromFile(rhs_file);
124-
x = new real_type[A->getNumRows()];
125-
vec_rhs = new vector_type(A->getNumRows());
127+
vec_rhs = ReSolve::io::createVectorFromFile(rhs_file);
126128
vec_x = new vector_type(A->getNumRows());
127-
vec_r = new vector_type(A->getNumRows());
128129
} else {
129130
ReSolve::io::updateMatrixFromFile(mat_file, A);
130-
ReSolve::io::updateArrayFromFile(rhs_file, &rhs);
131+
ReSolve::io::updateVectorFromFile(rhs_file, vec_rhs);
131132
}
132133
printSystemInfo(matrix_file_name_full, A);
133134
mat_file.close();
134135
rhs_file.close();
135136

136137
// Update data.
137138
if (i < 2) {
138-
vec_rhs->copyDataFrom(rhs, ReSolve::memory::HOST, ReSolve::memory::HOST);
139139
vec_rhs->setDataUpdated(ReSolve::memory::HOST);
140-
} else {
141-
vec_rhs->copyDataFrom(rhs, ReSolve::memory::HOST, ReSolve::memory::HOST);
142140
}
143141
std::cout<<"COO to CSR completed. Expanded NNZ: "<< A->getNnz()<<std::endl;
144142
//Now call direct solver
@@ -156,25 +154,28 @@ int main(int argc, char *argv[])
156154
}
157155
status = KLU->solve(vec_rhs, vec_x);
158156
std::cout<<"KLU solve status: "<<status<<std::endl;
159-
vec_r->copyDataFrom(rhs, ReSolve::memory::HOST, ReSolve::memory::HOST);
160-
161-
matrix_handler->setValuesChanged(true, ReSolve::memory::HOST);
162-
163-
matrix_handler->matvec(A, vec_x, vec_r, &ONE, &MINUSONE, ReSolve::memory::HOST);
164157

165158
helper.resetSystem(A, vec_rhs, vec_x);
166159
helper.printShortSummary();
160+
if (is_iterative_refinement) {
161+
// Setup iterative refinement
162+
FGMRES.setup(A);
163+
FGMRES.setupPreconditioner("LU", KLU);
164+
165+
// If refactorization produced finite solution do iterative refinement
166+
if (std::isfinite(helper.getNormRelativeResidual())) {
167+
FGMRES.solve(vec_rhs, vec_x);
168+
169+
// Print summary
170+
helper.printIrSummary(&FGMRES);
171+
}
172+
}
167173
}
168174

169175
//now DELETE
170176
delete A;
171177
delete KLU;
172-
delete [] x;
173-
delete [] rhs;
174-
delete vec_r;
178+
delete vec_rhs;
175179
delete vec_x;
176-
delete matrix_handler;
177-
delete vector_handler;
178-
179180
return 0;
180181
}

examples/kluStandAlone.cpp

+52-52
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,15 @@
11
#include <string>
22
#include <iostream>
33
#include <iomanip>
4-
#include <cmath>
5-
6-
#include <resolve/matrix/Coo.hpp>
4+
#include <sstream>
75
#include <resolve/matrix/Csr.hpp>
8-
#include <resolve/matrix/Csc.hpp>
96
#include <resolve/vector/Vector.hpp>
107
#include <resolve/matrix/io.hpp>
118
#include <resolve/matrix/MatrixHandler.hpp>
129
#include <resolve/vector/VectorHandler.hpp>
1310
#include <resolve/LinSolverDirectKLU.hpp>
1411
#include <resolve/workspace/LinAlgWorkspace.hpp>
12+
#include <resolve/LinSolverIterativeFGMRES.hpp>
1513
#include "ExampleHelper.hpp"
1614
#include <resolve/utilities/params/CliOptions.hpp>
1715

@@ -21,20 +19,19 @@ using namespace ReSolve::constants;
2119
void printHelpInfo()
2220
{
2321
std::cout << "\nLoads 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";
2723
std::cout << "Optional features:\n\t-h\tPrints this message.\n";
2824
std::cout << "\t-i\tEnables iterative refinement.\n\n";
2925
}
3026

3127
int main(int argc, char *argv[])
3228
{
33-
// Use the same data types as those you specified in ReSolve build.
29+
using index_type = ReSolve::index_type;
3430
using real_type = ReSolve::real_type;
3531
using vector_type = ReSolve::vector::Vector;
36-
using namespace ReSolve::examples;
3732
using namespace ReSolve;
33+
using namespace ReSolve::examples;
34+
3835
CliOptions options(argc, argv);
3936
CliOptions::Option* opt = nullptr;
4037

@@ -46,93 +43,96 @@ int main(int argc, char *argv[])
4643

4744
bool is_iterative_refinement = options.hasKey("-i");
4845

49-
std::string matrix_file_name("");
46+
std::string matrix_path_name("");
5047
opt = options.getParamFromKey("-m");
5148
if (opt) {
52-
matrix_file_name = opt->second;
49+
matrix_path_name = opt->second;
5350
} else {
5451
std::cout << "Incorrect input!\n";
5552
printHelpInfo();
53+
return 1;
5654
}
5755

58-
std::string rhs_file_name("");
56+
std::string rhs_path_name("");
5957
opt = options.getParamFromKey("-r");
6058
if (opt) {
61-
rhs_file_name = opt->second;
59+
rhs_path_name = opt->second;
6260
} else {
6361
std::cout << "Incorrect input!\n";
6462
printHelpInfo();
63+
return 1;
6564
}
6665

6766
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);
7471
vector_type* vec_rhs = nullptr;
75-
vector_type* vec_x = nullptr;
76-
vector_type* vec_r = nullptr;
72+
vector_type* vec_x = nullptr;
7773

7874
LinSolverDirectKLU* KLU = new LinSolverDirectKLU;
79-
// Create a helper object (computing errors, printing summaries, etc.)
80-
examples::ExampleHelper<LinAlgWorkspaceCpu> helper(*workspace);
8175

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);
8380
if(!mat_file.is_open())
8481
{
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;
8784
}
88-
std::ifstream rhs_file(rhs_file_name);
85+
86+
std::ifstream rhs_file(rhs_path_name);
8987
if(!rhs_file.is_open())
9088
{
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;
9391
}
92+
9493
bool is_expand_symmetric = true;
9594
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);
10096
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);
10499
mat_file.close();
105100
rhs_file.close();
106101

107-
vec_rhs->copyDataFrom(rhs, ReSolve::memory::HOST, ReSolve::memory::HOST);
108102
vec_rhs->setDataUpdated(ReSolve::memory::HOST);
109103
std::cout << "COO to CSR completed. Expanded NNZ: " << A->getNnz() << std::endl;
110-
//Now call direct solver
104+
105+
// Direct solver
111106
int status;
112107
KLU->setup(A);
113108
status = KLU->analyze();
114-
std::cout<<"KLU analysis status: "<<status<<std::endl;
109+
std::cout << "KLU analysis status: " << status << std::endl;
115110
status = KLU->factorize();
116111
std::cout << "KLU factorization status: " << status << std::endl;
112+
113+
// Solve the system
117114
status = KLU->solve(vec_rhs, vec_x);
118115
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);
124116

117+
helper.resetSystem(A, vec_rhs, vec_x);
125118
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+
}
126131

127-
//now DELETE
132+
// Cleanup
128133
delete A;
129134
delete KLU;
130-
delete [] x;
131-
delete [] rhs;
132-
delete vec_r;
135+
delete vec_rhs;
133136
delete vec_x;
134-
delete matrix_handler;
135-
delete vector_handler;
136-
137137
return 0;
138-
}
138+
}

0 commit comments

Comments
 (0)