-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathserial.cpp
More file actions
359 lines (291 loc) · 6.98 KB
/
serial.cpp
File metadata and controls
359 lines (291 loc) · 6.98 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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
#include <iostream>
#include <cstdlib>
#include <cmath>
#include <fstream>
#include <string>
#include <sstream>
using namespace std;
class vector
{
public:
double *val;
int length;
vector():length(0){};
vector(const int &t_n){
length=t_n;
val= new double[length];
};
~vector(){
length=0;
delete[] val;
}
void print(){
for(int i=0;i<length;i++){
cout<<val[i]<<endl;
}
}
};
class matrix{
public:int nnz,nrows,ncols,*indi,*indj;
double *val;
//Default Constructor
matrix():nrows(0),ncols(0),nnz(0){};
//Secondary Constructor
//matrix(const int &t_nrows,const int &t_ncols,const int &t_nnz);
~matrix(){delete[] val; delete[] indj; delete[] indi; ncols=0;nrows=0;nnz=0;};
matrix(const int &t_nrows,const int &t_ncols,const int &t_nnz){
nrows=t_nrows;
ncols=t_ncols;
nnz=t_nnz;
val = new double[nnz];
indj = new int[nnz];
indi = new int[nrows+1];
}
void print(){
for(int i=0;i<nrows;i++){
cout<<" ["<<indi[i]<<" , "<<indj[i]<<" ]";
cout<<val[i]<<endl;
}
}
};
void SpMV(vector &y,const matrix &M, const vector &x){
double sum=0;
for(int i = 0 ; i<M.nrows;i++){
sum=0;
for (int j=M.indi[i];j<M.indi[i+1];j++){
sum+=M.val[j]*x.val[M.indj[j]];
}
y.val[i]=sum;
}
}
void SpMadd(matrix &C,matrix &A,matrix &B){
int w[A.nrows],tnnz=0;
double ww[A.nrows];
int ldg=-2;
for(int i=0;i<A.nrows;i++){
w[i]=-1;
ww[i]=0;
}
for(int i=0;i<A.nrows;i++){
for(int j=A.indi[i];j<A.indi[i+1];j++){
w[A.indj[j]]=ldg;
ldg=A.indj[j];
tnnz++;
}
for(int j=B.indi[i];j<B.indi[i+1];j++){
if(w[B.indj[j]]!=-1){
w[B.indj[j]]=ldg;
ldg=B.indj[j];
tnnz++;
}
}
}
C.nnz=tnnz;
C.val = new double[C.nnz];
C.indj = new int[C.nnz];
for(int i=0;i<A.nrows;i++){
for(int j=A.indi[i];j<A.indi[i+1];j++){
w[A.indj[j]]=ldg;
ldg=A.indj[j];
tnnz++;
}
for(int j=B.indi[i];j<B.indi[i+1];j++){
if(w[B.indj[j]]!=-1){
w[B.indj[j]]=ldg;
ldg=B.indj[j];
tnnz++;
}
}
}
}
void VVadd(vector &y,const vector &a, const vector &b){
for(int i = 0 ; i<y.length; i++){
y.val[i]=a.val[i]+b.val[i];
}
}
void VVdic(vector &y,const vector &a, const vector &b){
for(int i = 0 ; i<y.length; i++){
y.val[i]=a.val[i]-b.val[i];
}
}
void VVmul(double &y,const vector &a, const vector &b){
double sum=0;
for(int i = 0 ; i<a.length; i++){
sum=sum+a.val[i]*b.val[i];
}
y=sum;
}
void CVmul(vector &y,double &a, const vector &b){
for(int i = 0 ; i<y.length; i++){
y.val[i]=a*b.val[i];
}
}
void inverse(matrix &K,const matrix &A){
for (int i=0; i<A.nrows; i++)
{
K.val[i]=1/A.val[i];
K.indi[i]=A.indi[i];
K.indj[i]=A.indj[i];
}
K.indi[A.nrows]=A.indi[A.nrows];
}
double norm(double &y,const vector &x){
double sum=0;
for (int i=0; i<x.length; i++){
sum=sum+pow(x.val[i],2.0);
}
y=sqrt(sum);
}
void Jacobi(matrix &K,const matrix &A){
for (int i=0; i<A.nrows; i++)
{ K.indi[i]=i;
for(int j=A.indi[i]; j<A.indi[i+1]; j++){
if (A.indj[j]==i){
K.val[i]=A.val[j];
K.indj[i]=A.indj[j];
}
}
}
K.indi[A.nrows]=A.nrows;
}
void Bicgstab(vector &x,const matrix &A,const vector &b,const matrix &K,double &tol,int &Nmax){
vector r(b.length);
vector rhat(b.length);
vector v(b.length);
vector p(b.length);
vector y(b.length);
vector z(b.length);
vector t(b.length);
vector s(b.length);
vector helpv(b.length);
vector helpv2(b.length);
matrix Kinv(K.nrows,K.ncols,K.nnz);
double rho=1;
double alpha=1;
double omega=1;
double rho2;
double beta;
double w1;
double w2;
double help;
double help2;
//arxikopoiisi x=0 v=0 p=0
for (int i=0; i<b.length; i++) {
x.val[i]=0;
v.val[i]=0;
p.val[i]=0;
//cout<<"sto diaolo";
}
//antistrofi toy K. tha xreiastei stin poreia
inverse(Kinv,K);
//residual
SpMV(helpv,A,x);
VVdic(r,b,helpv);
for (int i=0; i<b.length; i++){
rhat.val[i]=r.val[i];
}
//epilysi
for (int iter=0; iter<Nmax; iter++){
rho2=rho;
VVmul(rho,rhat,r);
beta=( (rho/rho2)*(alpha/omega));
CVmul(helpv,omega,v);
VVdic(helpv2,p,helpv);
CVmul(helpv,beta,helpv2);
VVadd(p,r,helpv);
SpMV(y,Kinv,p);
SpMV(v,A,y);
VVmul(help,rhat,v);
alpha=rho/help;
CVmul(helpv,alpha,v);
VVdic(s,r,helpv);
SpMV(z,Kinv,s);
SpMV(t,A,z);
SpMV(helpv,Kinv,t);
SpMV(helpv2,Kinv,s);
VVmul(w1,helpv,helpv2);
VVmul(w2,helpv,helpv);
omega=w1/w2;
CVmul(helpv,alpha,y);
CVmul(helpv2,omega,z);
VVadd(helpv,x,helpv);
VVadd(x,helpv,helpv2);
CVmul(helpv,omega,t);
VVdic(r,s,helpv);
for (int i=0; i<x.length; i++){
cout<<x.val[i]<<endl;
}
cout<<endl;
norm(help,r);
norm(help2,rhat);
if (help<(tol*help2)){
cout<<"Number of iterations is:"<<iter+1<<endl;
break;
}
}
}
int main(void){
//pername ton pinaka
std::ifstream file("A.txt");
std::string str;
int k=0,nnz=0,size=0;
int n; // proti grammh - size
getline(file, str);
std::stringstream stream(str);
stream>>n;
nnz=n;
//cout<<nnz<<endl;
getline(file, str); // deuterh grammh gia to size
std::stringstream stream2(str);
stream2>>n;
size=n;
//cout<<size<<endl;
matrix C(size,size,nnz); // dhlwnoume ton pinaka
getline(file, str); // trith grammh gia to val[]
std::stringstream stream3(str);
double v;
int i=0;
while(stream3>>v){
if(!stream) break;
C.val[i]=v;
i++;
}
getline(file, str); // 4h grammh gia to indj[]
std::stringstream stream4(str);
i=0;
while(stream4>>n){
if(!stream) break;
C.indj[i]=n;
i++;
}
getline(file, str); // 5h grammh gia to indi[]
std::stringstream stream5(str);
i=0;
while(stream5>>n){
if(!stream) break;
C.indi[i]=n;
i++;
}
std::ifstream file2("b.txt"); // dhmiourgoume to vector
std::string str2;
getline(file2, str2);
std::stringstream streamB1(str2);
streamB1>>n;
size=n;
vector z(size);
getline(file2, str2); // fortonoume tis times sto vector mas
std::stringstream streamB2(str2);
i=0;
while(streamB2>>v){
if(!stream) break;
z.val[i]=v;
i++;
}
//prakseis
matrix M(size, size,size);
vector x(size);
double tol=pow(10.0,-10.0);
int max=80;
Jacobi(M,C);
Bicgstab(x,C,z,M,tol,max);
}