/
WaveEquation.cpp
502 lines (396 loc) · 14.8 KB
/
WaveEquation.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
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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
/*
* Filename: WaveEquation.h
*
* Author: llyr-who.
*
* GitHub: llyr-who/PARALAAOMPI
*
* Discription:
*
* This code is an implementation of the all-at-once formulation of the
* wave equation.
*
*/
#include <iostream>
#include <iomanip>
#include <mpi.h>
#include <math.h>
#include <getopt.h>
#include "MatrixHelper.h"
#include "ParallelRoutines.h"
using namespace std;
// Declarations of routines used in GMRES
// (This will idealy be moved to a separate file in future)
void Update(std::complex<double>*x,int lengthofx, int k,int m, std::complex<double>*h,std::complex<double>*s,std::vector<std::complex<double>*>v);
void GeneratePlaneRotation(std::complex<double> &dx, std::complex<double> &dy, std::complex<double> &cs, std::complex<double> &sn);
void ApplyPlaneRotation(std::complex<double> &dx, std::complex<double> &dy, std::complex<double> &cs, std::complex<double> &sn);
int main(int argc, char * argv[])
{
// DECLARATION OF VARIABLES
double start,end; /* Used in timing the GMRES routine */
int i,j,k, N = 32,L=64; /* N is the number of spatial steps, L is the number of time steps */
double h = 1.0/(N-1); /* The size of the spatial discretisaion step */
double timestep = 1.0/L; /* Timestep length */
std::complex<double> *A,*x,*q,*y,*pointertolargeblocked,*b;
std::complex<double> *massContig,*stiffContig;
std::complex<double> *F,*D,*Ft;
std::vector<double> timesteps,times,perts;
int totalnodes,mynode;
std::vector<std::complex<double>*> Wblocks,Ablocks;
std::complex<double>** UMonolithic,**UtMonolithic;
MPI_Init(&argc,&argv);
MPI_Comm_size(MPI_COMM_WORLD, &totalnodes);
MPI_Comm_rank(MPI_COMM_WORLD, &mynode);
// RESERVATION OF MEMORY
int opt;
while((opt = getopt(argc, argv, "N:L:")) != -1) {
switch(opt) {
case 'N':
N = atoi(optarg);
break;
case 'L':
L = atoi(optarg);
break;
default:
cerr << "argument parsing problem" << std::endl;
exit(EXIT_FAILURE);
}
}
// Intermediate calculation vectors
y = new std::complex<double>[N*L];
x = new std::complex<double>[N*L];
q = new std::complex<double>[N*L];
// Right hand side vector
b = new std::complex<double>[N*L];
// All vectors are given to all nodes. While this increases the
// memory requirments of the program it significantly reduces
// the communication cost.
// FORMATION OF THE MATRICES ON NODE 0
// We take a different ideology with matrices. Due to thier size
// only small matrices will be distributed across all nodes.
if(mynode==0)
{
// Reservation of memory on the master node.
tridiag mass; CreateTridiag(N,mass);
tridiag stiff; CreateTridiag(N,stiff);
F = CreateMatrixContiguous(L,L);
D = CreateMatrixContiguous(L,L);
Ft = CreateMatrixContiguous(L,L);
// Formation of the mass and stiffness matrix.
// The Fourier basis matrix F, its transpose Ft,
// and the diagonal matrix of eigenvalues D are
// also formed.
FormMassStiff(h,N,mass,stiff);
FormFourier_Diag_FourierTranspose(L,F,D,Ft);
//
// FORMATION OF W: BEGIN
//
// W is the large tridiagonal matrix that needs to be inverted.
// Reference: See Goddard Wathen
// Temporary structures used in formation of matrices
tridiag stiffA0;CreateTridiag(N,stiffA0);
SetTriDiagEqualTo(N,stiff,stiffA0);
MultiplyTriDiagByConst(N,timestep*timestep,stiffA0);
// At this point we have
// stiffA0 = timestep*timestep*K
std::vector<tridiag> tridaigVec;
// Reserve memory
for(int i=0;i<L;i++)
{
tridiag temp; CreateTridiag(N,temp);
tridaigVec.push_back(temp);
}
// Multiply A1 by eigenvalue and ADD to A0
for(int i=0;i<L;i++)
{
tridiag temp;CreateTridiag(N,temp);
SetTriDiagEqualTo(N,mass,temp);
MultiplyTriDiagByConst(N,(1.0 - 2.0*D[i*L+i] + D[i*L+i]*D[i*L+i]),temp);
AddTriDiag(N,temp,stiffA0,tridaigVec[i]);
}
// Reserve memory for contiguous counterparts
for(int i=0;i<L;i++)
{
std::complex<double> *pointertolargeblocked = new std::complex<double>[3*N-2];
Wblocks.push_back(pointertolargeblocked);
}
// Fill the contiguous counterparts
for(int i=0;i<L;i++)
{
for(j=0;j<N-1;j++) Wblocks[i][j] = std::get<0>(tridaigVec[i])[j];
for(j=0;j<N ;j++) Wblocks[i][N-1+j] = std::get<1>(tridaigVec[i])[j];
for(j=0;j<N-1;j++) Wblocks[i][2*N-1+j] = std::get<2>(tridaigVec[i])[j];
Wblocks[i][N-1] = 1;
Wblocks[i][2*N-2] = 1;
}
//
// FORMATION OF W: END
//
// We form the blocks of A and pass the -M as a seperate argument
//
// FORMATION OF Ablocks: BEGIN
//
// We form the monolithic A by considering
// block diagonal entries tobe A0 = M + timestep[i]*K
// and the subdiagonal entries to have
// A1 = -M.
tridaigVec.clear();
// Reserve memory
for(int i=0;i<L;i++)
{
tridiag temp; CreateTridiag(N,temp);
tridaigVec.push_back(temp);
}
// Multiply A1 by timestep and package
for(int i=0;i<L;i++)
{
tridiag temp;CreateTridiag(N,temp);
SetTriDiagEqualTo(N,stiff,temp);
MultiplyTriDiagByConst(N,timestep*timestep,temp);
AddTriDiag(N,temp,mass,tridaigVec[i]);
}
// Reserve memory for contiguous counterparts
for(int i=0;i<L;i++)
{
std::complex<double> *pointertolargeblocked = new std::complex<double>[3*N-2];
Ablocks.push_back(pointertolargeblocked);
}
// Fill the contiguous counterparts
for(int i=0;i<L;i++)
{
for(j=0;j<N-1;j++) Ablocks[i][j] = std::get<0>(tridaigVec[i])[j];
for(j=0;j<N ;j++) Ablocks[i][N-1+j] = std::get<1>(tridaigVec[i])[j];
for(j=0;j<N-1;j++) Ablocks[i][2*N-1+j] = std::get<2>(tridaigVec[i])[j];
Ablocks[i][N-1] = 1;
Ablocks[i][2*N-2] = 1;
}
//
// FORMATION OF Ablocks: END
//
// U and U^* of the paper
UMonolithic = CreateMatrix(L,L);
UtMonolithic = CreateMatrix(L,L);
for(i=0;i<L;i++)
{
for(j=0;j<L;j++)
{
UMonolithic[i][j] = F[j*L+i];
UtMonolithic[i][j] = Ft[j*L+i];
}
}
// Reservation of memory for massContig
massContig = new std::complex<double>[3*N-2];
// Formation of contiguous mass
for(j=0;j<N-1;j++) massContig[j] = std::get<0>(mass)[j];
for(j=0;j<N ;j++) massContig[N-1+j] = std::get<1>(mass)[j];
for(j=0;j<N-1;j++) massContig[2*N-1+j] = std::get<2>(mass)[j];
//
// FORMATION OF b: BEGIN
//
// b = [Mu_0,-Mu_0, ... 0]
// Initial condition
std::complex<double>* u0 = new std::complex<double>[N];
std::complex<double>* U0 = new std::complex<double>[N];
// The smooth initial condition.
for(i=0;i<N;i++) u0[i] = sin(2*M_PI*i*h);
/*
// The non-smooth initial condition.
for(i=0;i<N;i++)
{
if(i*h<0.5-1.0/8 || i*h > 0.5+1.0/8)
u0[i] = 0.0;
else
{
u0[i] = cos(4*M_PI*(i*h-0.5))*cos(4*M_PI*(i*h-0.5));
}
}
*/
std::complex<double> prod =0;
U0[0] = massContig[N-1]*u0[0] + massContig[2*N-1]*u0[1];
for(j=1;j<N-1;j++)
{
U0[j]= massContig[j-1]*u0[j-1] + massContig[N-1+j]*u0[j] + massContig[2*N-1+j]*u0[j+1];
}
U0[N-1] = massContig[2*N-2]*u0[N-1] + massContig[N-2]*u0[N-2];
for(i=0;i<N;i++) b[i] = U0[i];
for(i=N;i<N+1;i++) b[i] = -1.0*U0[i-N];
//
// FORMATION OF b: END
//
}
// The right hand size is broadcast. This is consistent
// with our ethos for this code.
MPI_Bcast(b,N*L,MPI_DOUBLE_COMPLEX,0,MPI_COMM_WORLD);
//
// THE CALCULATION PHASE
//
// Temp vectors used in GMRES rotuine
std::complex<double>* temp = new std::complex<double>[N*L];
std::complex<double>* temp2 = new std::complex<double>[N*L];
// Residual vector
std::complex<double>* r0 = new std::complex<double>[N*L];
std::complex<double> normb,beta,resid; /* norm of preconditioned RHS*/
// If we set the tolerance to 10^{-4} we get a fixed iteration count of 2 for
// sufficiently large values of n and l. For 10^{-5} we get a fixed iteration
// count of 3.
std::complex<double> tol = 0.0001; /* tolerance */
int complete = 0;
int max_iter = 10;
int m = max_iter;
std::complex<double>* s = new std::complex<double>[m+1];
std::complex<double>* sn = new std::complex<double>[m+1];
std::complex<double>* cs = new std::complex<double>[m+1];
std::complex<double>* H = CreateMatrixContiguous(m+1,m+1);
// Standard procedure is to measure the time taken to complete
// the calculation on the master node
if(mynode == 0)
{
start = MPI_Wtime();
}
// ----------------------Calculaution Phase Begin ----------------------------------------------------
// As we are measuring on node 0, we want all processes to
// "meet up" before entering the calculation stage.
MPI_Barrier(MPI_COMM_WORLD);
// ---------------------------- GMRES BEGIN ----------------------------------------------------------
// Our GMRES routine follows Trefethens NLA very closely.
// The sub routines are C++ implementations of the Wiki MATLAB implementation.
j=1;
// Calculation of ||P^{-1}b||
ApplyPreconditioner(mynode,totalnodes,N,L,UMonolithic,UtMonolithic,Wblocks,b,temp);
CalculateNorm(mynode,totalnodes,N,L,temp,normb);
// Calculation of r = P^{-1}(b-A*x)
MultiplyByWaveSystem(mynode,totalnodes,N,L,Ablocks,massContig,x,temp);
VectorSubtraction(mynode,totalnodes,N,L,b,temp,temp2);
ApplyPreconditioner(mynode,totalnodes,N,L,UMonolithic,UtMonolithic,Wblocks,temp2,r0);
// Calculate beta = ||r||
CalculateNorm(mynode,totalnodes,N,L,r0,beta);
if(normb.real() ==0)
normb = 1;
resid = beta/normb;
if(resid.real() < tol.real())
{
tol = resid;
max_iter = 0;
complete = 1;
}
std::vector<std::complex<double>*> v;
for(i=0;i<m+1;i++)
{
std::complex<double> *pointer = new std::complex<double>[N*L];
v.push_back(pointer);
}
while(j<=max_iter)
{
// Calculate v[0] = r0 * (1/beta)
SetEqualTo(mynode,totalnodes,N,L,r0,v[0],(1.0/beta));
s[0] = beta;
for(i=0;i<m && j <= max_iter;i++,j++)
{
if(complete) continue;
//========================================== ARNODLI BEGIN
MultiplyByWaveSystem(mynode,totalnodes,N,L,Ablocks,massContig,v[i],temp);
ApplyPreconditioner(mynode,totalnodes,N,L,UMonolithic,UtMonolithic,Wblocks,temp,temp2);
for(k=0;k<=i;k++)
{
// w = temp2
std::complex<double> dotprodoutput;
DotProduct(mynode,totalnodes,N,L,temp2,v[k],dotprodoutput);
H[k+i*m] = dotprodoutput;
PlusEqualTo(mynode,totalnodes,N,L,v[k],temp2,-1.0*dotprodoutput);
}
CalculateNorm(mynode,totalnodes,N,L,temp2,H[(i+1)+i*m]);
SetEqualTo(mynode,totalnodes,N,L,temp2,v[i+1],(1.0/H[(i+1)+i*m]));
//========================================= ARNOLDI END
for(k=0;k<i;k++)
ApplyPlaneRotation(H[k+i*m],H[(k+1)+i*m],cs[k],sn[k]);
GeneratePlaneRotation(H[i+i*m], H[(i+1)+i*m], cs[i], sn[i]);
ApplyPlaneRotation(H[i+i*m], H[(i+1)+i*m], cs[i], sn[i]);
ApplyPlaneRotation(s[i], s[i+1], cs[i], sn[i]);
resid = std::abs(s[i+1]);
if(resid.real()/normb.real() < tol.real())
{
Update(x,N*L,i,m,H,s,v);
tol = resid;
max_iter = j;
complete = 1;
}
}
if(complete) continue;
Update(x,N*L,m-1,m,H,s,v);
// Calculation of r = P^{-1}(b-A*x)
MultiplyByWaveSystem(mynode,totalnodes,N,L,Ablocks,massContig,x,temp);
VectorSubtraction(mynode,totalnodes,N,L,b,temp,temp2);
ApplyPreconditioner(mynode,totalnodes,N,L,UMonolithic,UtMonolithic,Wblocks,temp2,r0);
// Calculate beta = ||r||
CalculateNorm(mynode,totalnodes,N,L,r0,beta);
resid = beta/normb;
if(resid.real() < tol.real())
{
tol = resid;
max_iter = j;
complete = 1;
}
if (complete)continue;
}
//
// --------------------------- GMRES END ----------------------------------------------------------
//
// ----------------------Calculaution Phase End ---------------------------------------------------
MPI_Barrier(MPI_COMM_WORLD);
if(mynode == 0)
{
end = MPI_Wtime();
std::cout<< " Time taken for the calculation to complete" << std::endl;
std::cout << end- start << std::endl;
std::cout << std::endl;
std::cout << " How many iterations did it take for GMRES to terminate? " << std::endl;
std::cout << j << std::endl;
}
MPI_Finalize();
}
// Definitions of the routines used in GMRES
void Update(std::complex<double>*x,int lengthofx, int k,int m, std::complex<double>*h,std::complex<double>*s,std::vector<std::complex<double>*>v)
{
std::complex<double>*y = new std::complex<double>[k+1];
for(int i=0;i<k+1;i++) y[i] = s[i];
for(int i=k;i>=0;i--)
{
if(h[i+i*m] == 0.0) continue;
y[i] = y[i]/h[i+i*m];
for(int j=i-1; j>= 0;j--)
{
y[j] -= h[j+i*m]*y[i];
}
}
for(int j=0;j<=k;j++)
{
for(int mm =0;mm<lengthofx;mm++)
x[mm] += v[j][mm]*y[j];
}
}
void GeneratePlaneRotation(std::complex<double> &dx, std::complex<double> &dy, std::complex<double> &cs, std::complex<double> &sn)
{
if (dy == 0.0)
{
cs = 1.0;
sn = 0.0;
}
else if (fabs(dy.real()) > fabs(dx.real()))
{
std::complex<double> temp = dx / dy;
sn = 1.0 / sqrt( 1.0 + temp*temp );
cs = temp * sn;
}
else
{
std::complex<double> temp = dy / dx;
cs = 1.0 / sqrt( 1.0 + temp*temp );
sn = temp * cs;
}
}
void ApplyPlaneRotation(std::complex<double> &dx, std::complex<double> &dy, std::complex<double> &cs, std::complex<double> &sn)
{
std::complex<double> temp = cs * dx + sn * dy;
dy = -sn * dx + cs * dy;
dx = temp;
}