-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkluFactor.cpp
More file actions
214 lines (190 loc) · 6.01 KB
/
kluFactor.cpp
File metadata and controls
214 lines (190 loc) · 6.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
/**
* @file kluFactor.cpp
* @author Slaven Peles (peless@ornl.gov)
* @author Kasia Swirydowicz (kasia.swirydowicz@amd.com)
* @brief Example solving linear systems using KLU factorization
*
* A series of linear systems is read from files specified at command line
* input and solved with KLU solver, using full factorization for each
* system. It is assumed that all systems in the series have the same sparsity
* pattern, so the analysis is done only once for the entire series.
*
*/
#include <iomanip>
#include <iostream>
#include <sstream>
#include "ExampleHelper.hpp"
#include <resolve/GramSchmidt.hpp>
#include <resolve/LinSolverDirectKLU.hpp>
#include <resolve/LinSolverIterativeFGMRES.hpp>
#include <resolve/matrix/Coo.hpp>
#include <resolve/matrix/Csc.hpp>
#include <resolve/matrix/Csr.hpp>
#include <resolve/matrix/MatrixHandler.hpp>
#include <resolve/matrix/io.hpp>
#include <resolve/utilities/params/CliOptions.hpp>
#include <resolve/vector/Vector.hpp>
#include <resolve/vector/VectorHandler.hpp>
#include <resolve/workspace/LinAlgWorkspace.hpp>
using namespace ReSolve::constants;
/// Prints help message describing system usage.
void printHelpInfo()
{
std::cout << "\nkluFactor.exe loads from files and solves a series of linear systems.\n\n";
std::cout << "System matrices are in files with names <pathname>XX.mtx, where XX are\n";
std::cout << "consecutive integer numbers 00, 01, 02, ...\n\n";
std::cout << "System right hand side vectors are stored in files with matching numbering\n";
std::cout << "and file extension.\n\n";
std::cout << "Usage:\n\t./";
std::cout << "kluFactor.exe -m <matrix pathname> -r <rhs pathname> -n <number of systems>\n\n";
std::cout << "Optional features:\n";
std::cout << "\t-h\tPrints this message.\n";
std::cout << "\t-i\tEnables iterative refinement.\n\n";
}
int main(int argc, char* argv[])
{
// Use the same data types as those you specified in ReSolve build.
using index_type = ReSolve::index_type;
using vector_type = ReSolve::vector::Vector;
using namespace ReSolve::examples;
using namespace ReSolve;
CliOptions options(argc, argv);
bool is_help = options.hasKey("-h");
if (is_help)
{
printHelpInfo();
return 0;
}
bool is_iterative_refinement = options.hasKey("-i");
index_type num_systems = 0;
auto opt = options.getParamFromKey("-n");
if (opt)
{
num_systems = std::stoi((opt->second).c_str());
}
else
{
std::cout << "Incorrect input!\n\n";
printHelpInfo();
return 1;
}
std::string matrix_path_name("");
opt = options.getParamFromKey("-m");
if (opt)
{
matrix_path_name = opt->second;
}
else
{
std::cout << "Incorrect input!\n\n";
printHelpInfo();
return 1;
}
std::string rhs_path_name("");
opt = options.getParamFromKey("-r");
if (opt)
{
rhs_path_name = opt->second;
}
else
{
std::cout << "Incorrect input!\n\n";
printHelpInfo();
return 1;
}
std::string file_extension("");
opt = options.getParamFromKey("-e");
if (opt)
{
file_extension = opt->second;
}
else
{
file_extension = "mtx";
}
std::string fileId;
std::string rhsId;
std::string matrix_file_name_full;
std::string rhs_file_name_full;
matrix::Csr* A = nullptr;
LinAlgWorkspaceCpu workspace;
ExampleHelper<LinAlgWorkspaceCpu> helper(workspace);
MatrixHandler matrix_handler(&workspace);
VectorHandler vector_handler(&workspace);
vector_type* vec_rhs = nullptr;
vector_type* vec_x = nullptr;
LinSolverDirectKLU KLU;
GramSchmidt GS(&vector_handler, GramSchmidt::CGS2);
LinSolverIterativeFGMRES FGMRES(&matrix_handler, &vector_handler, &GS);
for (int i = 0; i < num_systems; ++i)
{
std::cout << "System " << i << ":\n";
std::ostringstream matname;
std::ostringstream rhsname;
matname << matrix_path_name << std::setfill('0') << std::setw(2) << i << "." << file_extension;
rhsname << rhs_path_name << std::setfill('0') << std::setw(2) << i << "." << file_extension;
matrix_file_name_full = matname.str();
rhs_file_name_full = rhsname.str();
std::ifstream mat_file(matrix_file_name_full);
if (!mat_file.is_open())
{
std::cout << "Failed to open file " << matrix_file_name_full << "\n";
return 1;
}
std::ifstream rhs_file(rhs_file_name_full);
if (!rhs_file.is_open())
{
std::cout << "Failed to open file " << rhs_file_name_full << "\n";
return 1;
}
bool is_expand_symmetric = true;
if (i == 0)
{
A = ReSolve::io::createCsrFromFile(mat_file, is_expand_symmetric);
vec_rhs = ReSolve::io::createVectorFromFile(rhs_file);
vec_x = new vector_type(A->getNumRows());
}
else
{
ReSolve::io::updateMatrixFromFile(mat_file, A);
ReSolve::io::updateVectorFromFile(rhs_file, vec_rhs);
}
printSystemInfo(matrix_file_name_full, A);
mat_file.close();
rhs_file.close();
std::cout << "COO to CSR completed. Expanded NNZ: " << A->getNnz() << std::endl;
// Now call direct solver
int status;
if (i == 0)
{
vec_rhs->setDataUpdated(ReSolve::memory::HOST);
KLU.setup(A);
status = KLU.analyze();
std::cout << "KLU analysis status: " << status << std::endl;
}
status = KLU.factorize();
std::cout << "KLU factorization status: " << status << std::endl;
status = KLU.solve(vec_rhs, vec_x);
std::cout << "KLU solve status: " << status << std::endl;
helper.resetSystem(A, vec_rhs, vec_x);
helper.printShortSummary();
if (is_iterative_refinement)
{
// Setup iterative refinement
FGMRES.setup(A);
FGMRES.setupPreconditioner("LU", &KLU);
// If refactorization produced finite solution do iterative refinement
if (std::isfinite(helper.getNormRelativeResidual()))
{
FGMRES.solve(vec_rhs, vec_x);
// Print summary
helper.printIrSummary(&FGMRES);
}
}
}
// now DELETE
delete A;
delete vec_rhs;
delete vec_x;
return 0;
}