-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsample7.cpp
233 lines (192 loc) · 5.95 KB
/
sample7.cpp
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
/******************************************************
* C++ Library of the Linear Conjugate Gradient Methods (LibLCG)
*
* Copyright (C) 2022 Yi Zhang ([email protected])
*
* LibLCG is distributed under a dual licensing scheme. You can
* redistribute it and/or modify it under the terms of the GNU Lesser
* General Public License (LGPL) as published by the Free Software Foundation,
* either version 2 of the License, or (at your option) any later version.
* You should have received a copy of the GNU Lesser General Public
* License along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* If the terms and conditions of the LGPL v.2. would prevent you from
* using the LibLCG, please consider the option to obtain a commercial
* license for a fee. These licenses are offered by the LibLCG developing
* team. As a rule, licenses are provided "as-is", unlimited in time for
* a one time fee. Please send corresponding requests to: [email protected].
* Please do not forget to include some description of your company and the
* realm of its activities. Also add information on how to contact you by
* electronic and paper mail.
******************************************************/
#include "iostream"
#include "fstream"
#include "../lib/solver_eigen.h"
#include "../lib/preconditioner_eigen.h"
typedef std::complex<double> complex_d;
typedef Eigen::SparseMatrix<std::complex<double>, Eigen::RowMajor> spmat_cd;
typedef Eigen::Triplet<complex_d> triplt_cd;
typedef Eigen::VectorXcd vector_cd;
void read(std::string filePath, int *pN, int *pnz, complex_d **cooVal,
int **cooRowIdx, int **cooColIdx, complex_d **b)
{
std::ifstream in(filePath, std::ios::binary);
in.read((char*)pN, sizeof(int));
in.read((char*)pnz, sizeof(int));
*cooVal = new complex_d[*pnz]{};
*cooRowIdx = new int[*pnz]{};
*cooColIdx = new int[*pnz]{};
*b = new complex_d[*pN]{};
for (int i = 0; i < *pnz; ++i)
{
in.read((char*)&(*cooRowIdx)[i], sizeof(int));
in.read((char*)&(*cooColIdx)[i], sizeof(int));
in.read((char*)&(*cooVal)[i], sizeof(complex_d));
}
in.read((char*)(*b), sizeof(complex_d)*(*pN));
return;
}
void readAnswer(std::string filePath, int *pN, complex_d **x)
{
std::ifstream in(filePath, std::ios::binary);
in.read((char*)pN, sizeof(int));
*x = new complex_d[*pN]{};
in.read((char*)(*x), sizeof(complex_d)*(*pN));
return;
}
double max_diff(const vector_cd &a, const vector_cd &b)
{
double max = -1;
complex_d t;
for (int i = 0; i < a.size(); i++)
{
t = a[i] - b[i];
max = lcg_max(std::sqrt(std::norm(t)), max);
}
return max;
}
class TESTFUNC : public CLCG_EIGEN_Solver
{
public:
TESTFUNC(int n);
~TESTFUNC();
void set_kernel(int *row_id, int *col_id, complex_d *val, int nz_size);
void set_preconditioner();
//定义共轭梯度中Ax的算法
void AxProduct(const vector_cd &x, vector_cd &prod_Ax, lcg_matrix_e layout, clcg_complex_e conjugate)
{
if (conjugate == Conjugate) prod_Ax = kernel.conjugate() * x;
else prod_Ax = kernel * x;
return;
}
void MxProduct(const vector_cd &x, vector_cd &prod_Mx, lcg_matrix_e layout, clcg_complex_e conjugate)
{
// No preconditioning
//prod_Mx = x;
// Preconditioning using the diagonal kernel
//prod_Mx = p.cwiseProduct(x);
// Preconditioning using the ILUT/IC
clcg_solve_lower_triangle(l_tri, x, p);
clcg_solve_upper_triangle(u_tri, p, prod_Mx);
return;
}
private:
// 普通二维数组做核矩阵
spmat_cd kernel, l_tri, u_tri;
vector_cd p;
int n_size;
};
TESTFUNC::TESTFUNC(int n)
{
n_size = n;
kernel.resize(n_size, n_size);
kernel.setZero();
p.resize(n_size);
}
TESTFUNC::~TESTFUNC()
{
kernel.resize(0, 0);
l_tri.resize(0, 0);
u_tri.resize(0, 0);
p.resize(0);
}
void TESTFUNC::set_kernel(int *row_id, int *col_id, complex_d *val, int nz_size)
{
std::vector<triplt_cd> val_triplt;
for (size_t i = 0; i < nz_size; i++)
{
val_triplt.push_back(triplt_cd(row_id[i], col_id[i], val[i]));
}
kernel.setFromTriplets(val_triplt.begin(), val_triplt.end());
return;
}
void TESTFUNC::set_preconditioner()
{
// 1 Preconditioning using the incomplete LU decomposition
/*
for (size_t i = 0; i < n_size; i++)
{
p[i] = 1.0/kernel.coeff(i, i);
}
*/
// 2. Preconditioning using the incomplete LU decomposition
//incomplete_LU(kernel, l_tri, u_tri);
// 3. Preconditioning using the incomplete Cholesky decomposition
clcg_incomplete_Cholesky(kernel, l_tri);
u_tri = l_tri.transpose();
// 4. Preconditioning using compressed incomplete decompositions
/*
vector_cd one = Eigen::VectorXcd::Ones(n_size);
vector_cd x = Eigen::VectorXcd::Zero(n_size);
solve_lower_triangle(l_tri, one, x);
solve_upper_triangle(u_tri, x, p);
*/
return;
}
int main(int argc, char const *argv[]) try
{
std::string inputPath = "data/case_1K_cA";
std::string answerPath = "data/case_1K_cB";
int N;
int nz;
complex_d *A;
int *rowIdxA;
int *colIdxA;
complex_d *b;
read(inputPath, &N, &nz, &A, &rowIdxA, &colIdxA, &b);
complex_d *ans_x;
readAnswer(answerPath, &N, &ans_x);
std::clog << "N = " << N << std::endl;
std::clog << "nz = " << nz << std::endl;
TESTFUNC test(N);
test.set_kernel(rowIdxA, colIdxA, A, nz);
test.set_preconditioner();
vector_cd B, ANS;
B.resize(N);
ANS.resize(N);
for (size_t i = 0; i < N; i++)
{
B[i] = b[i];
ANS[i] = ans_x[i];
}
/********************准备工作完成************************/
clcg_para self_para = clcg_default_parameters();
self_para.epsilon = 1e-6;
self_para.abs_diff = 1;
test.set_clcg_parameter(self_para);
test.set_report_interval(10);
Eigen::VectorXcd m = Eigen::VectorXcd::Constant(N, std::complex<double>(0.0, 0.0));
test.MinimizePreconditioned(m, B, CLCG_PCG);
std::clog << "maximal difference: " << max_diff(ANS, m) << std::endl << std::endl;
m.setZero();
test.MinimizePreconditioned(m, B, CLCG_PBICG);
std::clog << "maximal difference: " << max_diff(ANS, m) << std::endl << std::endl;
ANS.resize(0);
B.resize(0);
m.resize(0);
return 0;
}
catch (std::exception &e)
{
std::cerr << e.what() << std::endl;
}