-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain2d.c
295 lines (217 loc) · 8.37 KB
/
main2d.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
/**
* @file: main2d.c
* @author: Chanho Eom
* @date: 24-Apr-2023
* */
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <math.h>
#include <mpi.h>
#include "poisson1d.h"
#include "jacobi.h"
#include "function.h"
#include "decomp1d.h"
#define maxit 10000
int main(int argc, char **argv)
{
double a[maxn][maxn], b[maxn][maxn], f[maxn][maxn];
double a1[maxn][maxn], b1[maxn][maxn], f1[maxn][maxn];
double a2[maxn][maxn], b2[maxn][maxn], f2[maxn][maxn];
int nx, ny;
int myid, nprocs;
int nbrleft, nbrright, nbrup, nbrdown;
int s[2], e[2];
int it;
double glob_diff, glob_diff1, glob_diff2;
double glob_grid_diff, glob_grid_diff1, glob_grid_diff2 ;
double ldiff, ldiff1, ldiff2;
double t1, t2;
double tol=1.0E-11;
MPI_Init(&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD, &myid);
MPI_Comm_size(MPI_COMM_WORLD, &nprocs);
MPI_Comm comm;
int dims[2] = {0, 0};
int periods[2] = {0, 0};
int coords[2] = {0, 0};
if( myid == 0 ){
if(argc > 2){
fprintf(stderr,"---->Usage: mpirun -np <nproc> %s <nx>\n",argv[0]);
fprintf(stderr,"---->(for this code nx=ny)\n");
MPI_Abort(MPI_COMM_WORLD, 1);}w
if( argc == 2 ){
nx = atoi(argv[1]);}
if( argc == 1 ){
nx=15;}
if( nx > maxn-2 ){
fprintf(stderr,"grid size too large\n");
exit(1);}
}
MPI_Bcast(&nx, 1, MPI_INT, 0, MPI_COMM_WORLD);
ny = nx;
printf("(myid: %d) nx = %d, ny = %d\n", myid, nx, ny);
MPI_Dims_create(nprocs, 2, dims);
if (myid == 0){
printf("The paif of processors: (x, y) = (%d, %d)\n", dims[0], dims[1]);
}
init_full_grids(a, b, f);
init_full_grids(a1, b1, f1);
init_full_grids(a2, b2, f2);
MPI_Cart_create(MPI_COMM_WORLD, 2, dims, periods, 0, &comm);
MPI_Cart_coords(comm, myid, 2, coords);
MPI_Cart_shift(comm, 0, 1, &nbrleft, &nbrright);
MPI_Cart_shift(comm, 1, 1, &nbrdown, &nbrup);
decomp_2d(nx, myid, dims, coords, s, e);
printf("(myid %d with (%d, %d)), x_axis: (%d, %d), y_axis: (%d, %d), nbrleft: %d, nbrright: %d, nbrdown: %d, nbrup: %d\n", myid, coords[0], coords[1], s[0], e[0], s[1], e[1], nbrleft, nbrright, nbrdown, nbrup);
// Exercise.2 Question.2 => Poisson Initial Condition by C. Eom
init_basic_2d(a, b, f, nx, ny, s, e);
init_basic_2d(a1, b1, f1, nx, ny, s, e);
init_basic_2d(a2, b2, f2, nx, ny, s, e);
t1 = MPI_Wtime();
MPI_Win wina, winb;
MPI_Win winc, wind;
// Exercise.3 Qiestion.1 => Create thwindows for RMA
MPI_Win_create(&a1[s[0] -1][0], maxn * maxn * sizeof(double), sizeof(double), MPI_INFO_NULL, MPI_COMM_WORLD, &wina);
MPI_Win_create(&b1[s[0] -1][0], maxn * maxn * sizeof(double), sizeof(double), MPI_INFO_NULL, MPI_COMM_WORLD, &winb);
MPI_Win_create(&a2[s[0] -1][0], maxn * maxn * sizeof(double), sizeof(double), MPI_INFO_NULL, MPI_COMM_WORLD, &winc);
MPI_Win_create(&b2[s[0] -1][0], maxn * maxn * sizeof(double), sizeof(double), MPI_INFO_NULL, MPI_COMM_WORLD, &wind);
glob_diff = 1000;
glob_diff1 = 1000;
glob_diff2 = 1000;
for(it=0; it<maxit; it++){
// Exercise.2 Question.2 => 2d ghost exchange routine with Sendrecv by C. Eom
exchang3_2d(a, ny, s, e, MPI_COMM_WORLD, nbrleft, nbrright, nbrdown, nbrup);
sweep_2d(a, f, nx, s, e, b);
exchang3_2d(b, nx, s, e, MPI_COMM_WORLD, nbrleft, nbrright, nbrdown, nbrup);
sweep_2d(b, f, nx, s, e, a);
ldiff = griddiff_2d(a, b, nx, s, e);
MPI_Allreduce(&ldiff, &glob_diff, 1, MPI_DOUBLE, MPI_SUM, MPI_COMM_WORLD);
if(myid==0 && it%10==0){
printf("Non_RMA] (myid %d) locdiff: %lf; glob_diff: %lf\n",myid, ldiff, glob_diff);
}
if( glob_diff < tol ){
if(myid==0){
printf("iterative solve converged\n");
}
break;
}
}
MPI_Barrier(MPI_COMM_WORLD);
for(it=0; it<maxit; it++){
// Exercise.3 Question.1 => Implementation of the 2D ghost exchange routin: RMA by C. Eom
exchang_2d_RMA(a1, ny, s, e, MPI_COMM_WORLD, nbrleft, nbrright, nbrdown, nbrup, wina);
sweep_2d(a1, f1, nx, s, e, b1);
exchang_2d_RMA(b1, nx, s, e, MPI_COMM_WORLD, nbrleft, nbrright, nbrdown, nbrup, winb);
sweep_2d(b1, f1, nx, s, e, a1);
ldiff1 = griddiff_2d(a1, b1, nx, s, e);
MPI_Allreduce(&ldiff1, &glob_diff1, 1, MPI_DOUBLE, MPI_SUM, MPI_COMM_WORLD);
if(myid==0 && it%10==0){
printf("RMA] (myid %d) locdiff: %lf; glob_diff: %lf\n",myid, ldiff1, glob_diff1);
}
if( glob_diff1 < tol){
if(myid==0){
printf("iterative solve converged\n");
}
break;
}
}
MPI_Barrier(MPI_COMM_WORLD);
// Exercuse,3 Question.2 => Get all processors from MPI_COMM_WORLD
MPI_Group world_group;
MPI_Comm_group(MPI_COMM_WORLD, &world_group);
// Exercise.3 Question.2 => Create two groups for PSCW
MPI_Group source[2], dest[2];
if (nbrright != MPI_PROC_NULL) {
int ranks[1] = {nbrright};
MPI_Group_incl(world_group, 1, ranks, &source[0]);}
if (nbrleft != MPI_PROC_NULL) {
int ranks[1] = {nbrleft};
MPI_Group_incl(world_group, 1, ranks, &dest[0]);}
if (nbrup != MPI_PROC_NULL) {
int ranks[1] = {nbrup};
MPI_Group_incl(world_group, 1, ranks, &source[1]);}
if (nbrdown != MPI_PROC_NULL) {
int ranks[1] = {nbrdown};
MPI_Group_incl(world_group, 1, ranks, &dest[1]);}
for(it=0; it<maxit; it++){
// Exercise.3 Question.2 => Implementation of the 2D ghost exchange routin: RMA PSCW by C. Eom
exchang_2d_RMA_pscw(a2, ny, s, e, MPI_COMM_WORLD, nbrleft, nbrright, nbrdown, nbrup, winc, source, dest);
sweep_2d(a2, f2, nx, s, e, b2);
exchang_2d_RMA_pscw(b2, nx, s, e, MPI_COMM_WORLD, nbrleft, nbrright, nbrdown, nbrup, wind, source, dest);
sweep_2d(b2, f2, nx, s, e, a2);
ldiff2 = griddiff_2d(a2, b2, nx, s, e);
MPI_Allreduce(&ldiff2, &glob_diff2, 1, MPI_DOUBLE, MPI_SUM, MPI_COMM_WORLD);
if(myid==0 && it%10==0){
printf("RMA PSCW] (myid %d) locdiff: %lf; glob_diff: %lf\n",myid, ldiff2, glob_diff2);
}
if( glob_diff2 < tol){
if(myid==0){
printf("iterative solve converged\n");
}
break;
}
}
t2=MPI_Wtime();
printf("DONE! (it: %d)\n",it);
//MPI_Barrier(MPI_COMM_WORLD);
if( myid == 0 ){
if( it == maxit ){
fprintf(stderr,"Failed to converge\n");
}
printf("Run took %lf s\n",t2-t1);
}
// Excersie.2 Question.4 => Define Analytic solution by C.Eom
double solution[maxn][maxn];
solution_grid(solution, nx, ny);
// Exercise.2 Question.4 => Compare the results from finite difference method and analytic solution
ldiff = griddiff_2d(solution, a, nx, s, e);
MPI_Allreduce(&ldiff, &glob_grid_diff, 1, MPI_DOUBLE, MPI_SUM, MPI_COMM_WORLD);
ldiff1 = griddiff_2d(solution, a1, nx, s, e);
MPI_Allreduce(&ldiff1, &glob_grid_diff1, 1, MPI_DOUBLE, MPI_SUM, MPI_COMM_WORLD);
ldiff2 = griddiff_2d(solution, a2, nx, s, e);
MPI_Allreduce(&ldiff2, &glob_grid_diff2, 1, MPI_DOUBLE, MPI_SUM, MPI_COMM_WORLD);
if (myid == 0){
printf("\n===========================================================================\n");
printf("ver. Non_RMA] The global difference to the analytic solution on a grid size of %d is %f\n", nx, glob_grid_diff);
printf("ver. RMA] The global difference to the analytic solution on a grid size of %d is %f\n", nx, glob_grid_diff1);
printf("ver. RMA PSCW] The global difference to the analytic solution on a grid size of %d is %f\n", nx, glob_grid_diff2);
}
/*
print_in_order(a, MPI_COMM_WORLD, nx);
if( nprocs == 1 ){
print_grid_to_file("grid", a, nx, ny);
print_full_grid(a, nx);
}
*/
// Exercise.2 Question.4 => Gather the grid from the processors onto rank 0
gather_grid_2d(a, nx, ny, nprocs, myid, s, e, MPI_COMM_WORLD);
gather_grid_2d(a1, nx, ny, nprocs, myid, s, e, MPI_COMM_WORLD);
gather_grid_2d(a2, nx, ny, nprocs, myid, s, e, MPI_COMM_WORLD);
if(myid == 0){
printf("\n================== The gathered grid onto rank 0 (ver. NON_RMA) ==================\n");
print_full_grid(a, nx);
printf("\n================== The gathered grid onto rank 0 (ver. RMA) ==================\n");
print_full_grid(a1, nx);
printf("\n================== The gathered grid onto rank 0 (ver. RMA PSCW) ==================\n");
print_full_grid(a2, nx);
}
// Exercise.3 Question.3 => Compare the results from RMA Routines and Non-RMA Routine
double local1, local2;
double global1, global2;
local1 = griddiff_2d(a, a1, nx, s, e);
MPI_Allreduce(&local1, &global1, 1, MPI_DOUBLE, MPI_SUM, MPI_COMM_WORLD);
local2 = griddiff_2d(a, a2, nx, s, e);
MPI_Allreduce(&local2, &global2, 1, MPI_DOUBLE, MPI_SUM, MPI_COMM_WORLD);
if (myid == 0){
printf("\n===========================================================================\n");
printf("ver. RMA] The global difference to the Non_RAM routine on a grid size of %d is %f\n", nx, global1);
printf("ver. RMA PSCW] The global difference to the Non_RMA routine on a grid size of %d is %f\n", nx, global2);
}
MPI_Win_free(&wina);
MPI_Win_free(&winb);
MPI_Win_free(&winc);
MPI_Win_free(&wind);
MPI_Finalize();
return 0;
}