-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLaplace-Approximation.c
505 lines (436 loc) · 11.3 KB
/
Laplace-Approximation.c
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
493
494
495
496
497
498
499
500
501
502
503
504
/****
* MPI Version of SOR solver algorithm for Laplace Approximation
* Task-2
****/
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <mpi.h>
#include <string.h>
#define MASTER 1 /* Messaage Tags */
#define SLAVE 2
#define MSGINIT 0
#define MAX_SIZE 4096 /* Maxium allowed Size of the Matrix */
int mtype; /* MPI Message Tag Type */
int N; /* Given Size of the Matrix */
int maxnum; /* Maximum number allowed in the Matrix Initialization */
char *Init; /* Matrix initialization type */
double difflimit; /* Stop Condition */
double w; /* Relaxation Factor */
int PRINT; /* To View Output */
static double A[MAX_SIZE+2][MAX_SIZE+2]; /* (+2) - boundary elements */
MPI_Status status;
/* forward declarations */
int work(int, int);
int singlework();
void Init_Matrix(int);
void Print_Matrix();
void Init_Default();
int Read_Options(int, char **);
int
main(int argc, char **argv)
{
int iter, prank, np;
MPI_Init(&argc,&argv);
MPI_Comm_size(MPI_COMM_WORLD, &np);
MPI_Comm_rank(MPI_COMM_WORLD, &prank);
Init_Default(); /* Init default values */
Read_Options(argc,argv); /* Read arguments */
if(np == 1) /* Only Master node will work if number of nodes are 1. */
{
if(prank == 0)
{
Init_Matrix(prank);
iter = singlework();
if(PRINT == 1){
printf("\nInput Matrix processed by %d node\n", np);
Print_Matrix();
}
printf("\nNumber of iterations :%d\n", iter);
}
}
if(np>1) /* When the number of nodes are greater than 1, the work is distributed among them by master node */
{
if(prank == 0) /* Master Node */
{
Init_Matrix(prank); /* The matrix is initialized in master node, and it is sent to other child nodes */
iter = work(prank, np);
if(PRINT == 1) {
printf("\n Input Matrix Processed by : %d nodes\n", np);
Print_Matrix();
}
printf("\nNumber of iterations = %d\n", iter);
}
else {
/* Child or Slave nodes receive their matrix part and begin their work */
work(prank, np);
}
}
MPI_Finalize();
}
int singlework()
{
int m, n;
int iteration = 0;
int finished = 0;
double maxi, sum, prevmax_even, prevmax_odd;
prevmax_even = 0.0;
prevmax_odd = 0.0;
while(!finished)
{
if((iteration%2) == 0)
{
for(m = 1; m < N+1; m++)
{
for(n = 1; n < N+1; n++)
{
if(((m+n) % 2) == 0)
{
A[m][n] = (1 - w) * A[m][n] + w *(A[m-1][n] + A[m+1][n] + A[m][n - 1] + A[m][n + 1])/4;
}
}
}
/* Calculating the maximum sum of the elements by every processor node*/
maxi = -999999.0;
for (m = 1; m < N + 1; m++) {
sum = 0.0;
for (n = 1; n < N+1; n++) {
sum += A[m][n];
}
if(sum > maxi)
maxi = sum;
}
if(fabs(maxi - prevmax_even) <= difflimit)
finished = 1;
prevmax_even = maxi;
}
if((iteration%2) == 1)
{
for(m = 1; m < N+1; m++)
{
for(n = 1; n < N+1; n++)
{
if(((m+n) % 2) == 1)
{
A[m][n] = (1 - w) * A[m][n] + w *(A[m-1][n] + A[m+1][n] + A[m][n - 1] + A[m][n + 1])/4;
}
}
}
/* Calculating the maximum sum of the elements by every processor node*/
maxi = -999999.0;
for (m = 1; m < N + 1; m++) {
sum = 0.0;
for (n = 1; n < N+1; n++) {
sum += A[m][n];
}
if(sum > maxi)
maxi = sum;
}
if(fabs(maxi - prevmax_odd) <= difflimit)
finished = 1;
prevmax_odd = maxi;
}
iteration++;
if (iteration > 100000)
{
/* exit if we don't converge fast enough */
printf("Max number of iterations reached! Exit!\n");
finished = 1;
}
}
return iteration;
}
int
work(int rank, int p)
{
int cols = N + 2; /* Number of columns to process by each node */
int offset = N/p; /* Number of rows to perform laplace approximation */
int noffset = offset; /* To send parts of matrix to child nodes */
int rows = offset + 2; /* Number of rows required to perform laplace approximation */
int iteration = 0; /* Number of iterations */
int finished = 0; /* To terminate the process */
int m, n, i, j, x, y; /* Looping variables */
int dest, src; /* Looping variables */
double maxi; /* Variables to calculate the max sum of elements */
double sum;
double prevmax_even, prevmax_odd;
prevmax_even = 0.0;
prevmax_odd = 0.0;
if(rank == 0) /* Sending individual parts to child nodes */
{
for(dest = 1; dest < p; dest++)
{
for(i = 0; i < rows; i++)
{
MPI_Send(&A[noffset + i][0], cols, MPI_DOUBLE, dest, MSGINIT, MPI_COMM_WORLD);
}
noffset += offset;
}
}
if(rank != 0) /* Each child node recceives their corresponding part according to the noffset */
{
for(j = 0; j<rows; j++)
{
MPI_Recv(&A[j][0], cols, MPI_DOUBLE, 0, MSGINIT, MPI_COMM_WORLD, &status);
}
}
while (!finished)
{
/* Updating boundary rows */
if (rank != 0) /* Each node sends the top row shared with other nodes except master node */
{
mtype = SLAVE;
MPI_Send(&A[1][0], cols, MPI_DOUBLE, rank-1 ,mtype, MPI_COMM_WORLD);
mtype = MASTER;
MPI_Recv(&A[0][0], cols, MPI_DOUBLE, rank-1, mtype, MPI_COMM_WORLD, &status);
}
if (rank != p-1)
{ /* Each node sends the bottom row shared with other nodes */
mtype = SLAVE;
MPI_Recv(&A[offset+1][0], cols , MPI_DOUBLE, rank+1, mtype, MPI_COMM_WORLD, &status);
mtype = MASTER;
MPI_Send(&A[offset][0], cols, MPI_DOUBLE, rank+1, mtype, MPI_COMM_WORLD);
}
/* Calculating odd or even elements based on iteration */
/* Calculate Part-A - Even elements */
if((iteration%2) == 0)
{
for(m = 1; m < offset+1; m++)
{
for(n = 1; n < N+1; n++)
{
if(((m+n) % 2) == 0)
{
A[m][n] = (1 - w) * A[m][n] + w *(A[m-1][n] + A[m+1][n] + A[m][n - 1] + A[m][n + 1])/4;
}
}
}
/* Calculating the maximum sum of the elements by every processor node*/
maxi = -999999.0;
for (m = 1; m < offset + 1; m++) {
sum = 0.0;
for (n = 1; n < N+1; n++) {
sum += A[m][n];
}
if(sum > maxi)
maxi = sum;
}
double maxsum1;
/* Finding maximum sum across all the nodes */
MPI_Allreduce(&maxi, &maxsum1, 1, MPI_DOUBLE, MPI_MAX, MPI_COMM_WORLD);
/* Compare the sum with the prev sum, i.e., check whether we are done or not */
if(fabs(maxsum1 - prevmax_even) <= difflimit)
finished = 1;
prevmax_even = maxsum1;
}
/* Calculate Part-B Odd elements */
if ((iteration%2) == 1)
{
for(m = 1; m < offset+1; m++)
{
for(n = 1; n < N+1; n++)
{
if(((m+n) % 2) == 1)
{
A[m][n] = (1 - w) * A[m][n] + w *(A[m-1][n] + A[m+1][n] + A[m][n - 1] + A[m][n + 1])/4;
}
}
}
/* Calculating the maximum sum of the elements by every processor node*/
maxi = -999999.0;
for (m = 1; m < offset + 1; m++) {
sum = 0.0;
for (n = 1; n < N+1; n++) {
sum += A[m][n];
}
if(sum > maxi)
maxi = sum;
}
double maxsum2;
/* Finding maximum sum across all the nodes */
MPI_Allreduce(&maxi, &maxsum2, 1, MPI_DOUBLE, MPI_MAX, MPI_COMM_WORLD);
/* Compare the sum with the prev sum, i.e., check whether we are done or not */
if(fabs(maxsum2 - prevmax_odd) <= difflimit)
finished = 1;
prevmax_odd = maxsum2;
}
iteration++;
if (iteration > 100000)
{
/* exit if we don't converge fast enough */
printf("Max number of iterations reached! Exit!\n");
finished = 1;
}
}
/*Sending results back to the Master node processor */
if (rank != 0)
{
mtype = SLAVE;
for(x = 1; x < offset+1; x++)
{
MPI_Send(&A[x][0], N+2, MPI_DOUBLE, 0, mtype, MPI_COMM_WORLD);
}
}
/* Offset to locate the position of returning result rows in matrix A back in the master node */
int recvoffset = offset+1;
if(rank == 0)
{
mtype = SLAVE;
for(src = 1; src < p; src++)
{
for(y = 0; y<offset; y++)
{
MPI_Recv(&A[recvoffset][0], N+2, MPI_DOUBLE, src, mtype, MPI_COMM_WORLD, &status);
recvoffset++;
}
}
}
return iteration;
}
void
Init_Matrix(int rank)
{
int i, j, dmmy;
printf("\nsize = %dx%d ",N,N);
printf("\nmaxnum = %d \n",maxnum);
printf("difflimit = %.7lf \n",difflimit);
printf("Init = %s \n",Init);
printf("w = %f \n\n",w);
printf("Initializing matrix...");
/* Initialize all grid elements, including the boundary */
for (i = 0; i < N+2; i++) {
for (j = 0; j < N+2; j++) {
A[i][j] = 0.0;
}
}
if (strcmp(Init,"count") == 0) {
for (i = 1; i < N+1; i++){
for (j = 1; j < N+1; j++) {
A[i][j] = (double)i/2;
}
}
}
if (strcmp(Init,"rand") == 0) {
for (i = 1; i < N+1; i++){
for (j = 1; j < N+1; j++) {
A[i][j] = (rand() % maxnum) + 1.0;
}
}
}
if (strcmp(Init,"fast") == 0) {
for (i = 1; i < N+1; i++){
dmmy++;
for (j = 1; j < N+1; j++) {
dmmy++;
if ((dmmy%2) == 0)
A[i][j] = 1.0;
else
A[i][j] = 5.0;
}
}
}
/* Set the border to the same values as the outermost rows/columns */
/* fix the corners */
A[0][0] = A[1][1];
A[0][N+1] = A[1][N];
A[N+1][0] = A[N][1];
A[N+1][N+1] = A[N][N];
/* fix the top and bottom rows */
for (i = 1; i < N+1; i++) {
A[0][i] = A[1][i];
A[N+1][i] = A[N][i];
}
/* fix the left and right columns */
for (i = 1; i < N+1; i++) {
A[i][0] = A[i][1];
A[i][N+1] = A[i][N];
}
printf("done in node: %d \n", rank);
if (PRINT == 1)
Print_Matrix();
}
void
Print_Matrix()
{
int i, j;
for (i=0; i<N+2 ;i++){
for (j=0; j<N+2 ;j++){
printf(" %f",A[i][j]);
}
printf("\n");
}
printf("\n");
}
void
Init_Default()
{
N = 2048;
difflimit = 0.00001*N;
Init = "rand";
maxnum = 15.0;
w = 0.5;
PRINT = 0;
}
int
Read_Options(int argc, char **argv)
{
char *prog;
prog = *argv;
while (++argv, --argc > 0)
if (**argv == '-')
switch ( *++*argv ) {
case 'n':
--argc;
N = atoi(*++argv);
difflimit = 0.00001*N;
break;
case 'h':
printf("\nHELP: try sor -u \n\n");
exit(0);
break;
case 'u':
printf("\nUsage: sor [-n problemsize]\n");
printf(" [-d difflimit] 0.1-0.000001 \n");
printf(" [-D] show default values \n");
printf(" [-h] help \n");
printf(" [-I init_type] fast/rand/count \n");
printf(" [-m maxnum] max random no \n");
printf(" [-P print_switch] 0/1 \n");
printf(" [-w relaxation_factor] 1.0-0.1 \n\n");
exit(0);
break;
case 'D':
printf("\nDefault: n = %d ", N);
printf("\n difflimit = %f ", difflimit);
printf("\n Init = rand" );
printf("\n maxnum = 5 ");
printf("\n w = 0.5 \n");
printf("\n P = 0 \n\n");
exit(0);
break;
case 'I':
--argc;
Init = *++argv;
break;
case 'm':
--argc;
maxnum = atoi(*++argv);
break;
case 'd':
--argc;
difflimit = atof(*++argv);
break;
case 'w':
--argc;
w = atof(*++argv);
break;
case 'P':
--argc;
PRINT = atoi(*++argv);
break;
default:
printf("%s: ignored option: -%s\n", prog, *argv);
printf("HELP: try %s -u \n\n", prog);
break;
}
}