-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRedBlack_MPI.c
262 lines (223 loc) · 8.16 KB
/
RedBlack_MPI.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
//Parallel Red Black Method using MPI
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<mpi.h>
//Memory allocation for matrices
double** allocate2DArray(int rows, int cols) {
double **arr = (double **)malloc(rows * sizeof(double *));
for (int i = 0; i < rows; i++) {
arr[i] = (double *)malloc(cols * sizeof(double));
}
return arr;
}
//Memory deallocation for matrices
void deallocate2DArray(double **arr, int rows) {
for (int i = 0; i < rows; i++) {
free(arr[i]);
}
free(arr);
}
int main(){
MPI_Init(NULL,NULL);
double t1,t2;
t1 = MPI_Wtime();
int i,j,k,my_id,size,tag1 = 0, tag2 = 1;
MPI_Status status;
MPI_Comm_size(MPI_COMM_WORLD,&size);
MPI_Comm_rank(MPI_COMM_WORLD,&my_id);
//Mesh Parameters
int xmin = 0, xmax = 1, ymin = 0, ymax = 1;
int N = 33;
int N_local = N/size;
if(my_id == size-1){
N_local += N%size;
}
double delta = (double)(xmax - xmin)/(N-1);
//Memory Allocation
double **phi, **phi_exact;
double **q;
phi = allocate2DArray(N_local, N);
phi_exact = allocate2DArray(N_local, N);
q = allocate2DArray(N_local, N);
double ystart = ymin + delta*my_id*(N/size);
//Initial Guesses and Boundary Conditions
double x,y;
for(i = 0; i<N_local ;i++){
for(j = 0; j<N; j++){
x = xmin + delta*j;
y = ystart + delta*i;
phi[i][j] = 0;
if (j == 0)
phi[i][j] = exp(-2*y);
if (j == N-1)
phi[i][j] = exp(1-2*y);
}
}
if(my_id == 0){
for(j = 0; j<N; j++){
x = xmin + delta*j;
phi[0][j] = exp(x);
}
}
if(my_id == size-1){
for(j = 0; j<N; j++){
x = xmin + delta*j;
phi[N_local-1][j] = exp(x-2);
}
}
//RHS of Poisson equation
for(i = 0; i<N_local ;i++){
for(j = 0; j<N; j++){
x = xmin + j*delta;
y = ystart + i*delta;
q[i][j] = 5*exp(x)*exp(-2*y);
}
}
//Exact Solution
for(i = 0; i<N_local; i++){
for(j = 0; j<N; j++){
x = xmin + delta*j;
y = ystart + delta*i;
phi_exact[i][j] = exp(x)*exp(-2*y);
}
}
//Norm of exact solution
double norm_exact = 0;
for(i = 0; i<N_local; i++){
for(j = 0; j<N; j++){
norm_exact += phi_exact[i][j]*phi_exact[i][j];
}
}
double norm_exact_global;
MPI_Allreduce(&norm_exact,&norm_exact_global,1,MPI_DOUBLE,MPI_SUM,MPI_COMM_WORLD);
norm_exact_global = sqrt(norm_exact_global);
int iter = 0;
double err = 1;
//Buffers for send receive
double phi_top[N];
double phi_bot[N];
//Main loop
while(err > 0.0001){
//Odd Points
//Sending the ghost nodes required at the top from i-1th processor to ith processor
if(my_id != size-1)
MPI_Send(phi[N_local-1],N,MPI_DOUBLE,my_id+1,tag1,MPI_COMM_WORLD);
if(my_id != 0){
MPI_Recv(&phi_top,N,MPI_DOUBLE,my_id-1,tag1,MPI_COMM_WORLD,&status);
for(j = 1; j<N-1; j++){
if( ((N/size)*my_id + j) % 2 == 1){
phi[0][j] = 0.25*(phi_top[j] + phi[1][j] + phi[0][j+1] + phi[0][j-1] - delta*delta*q[0][j]);
}
}
}
//Sending the ghost nodes required at the bottom from i+1th processor to ith processor
if(my_id !=0)
MPI_Send(phi[0],N,MPI_DOUBLE,my_id-1,tag2,MPI_COMM_WORLD);
if(my_id != size-1){
MPI_Recv(&phi_bot,N,MPI_DOUBLE,my_id+1,tag2,MPI_COMM_WORLD,&status);
for(j = 1; j<N-1; j++){
if( ((N/size)*my_id + N_local - 1 + j) % 2 == 1){
phi[N_local-1][j] = 0.25*(phi[N_local-2][j] + phi_bot[j] + phi[N_local-1][j+1] + phi[N_local-1][j-1] - delta*delta*q[N_local-1][j]);
}
}
}
//Updating interior phi values
for(i = 1; i<N_local-1; i++){
for(j = 1; j<N-1; j++){
if( ((N/size)*my_id + i + j) % 2 == 1){
phi[i][j] = 0.25*(phi[i+1][j] + phi[i-1][j] + phi[i][j+1] + phi[i][j-1] - delta*delta*q[i][j]);
}
}
}
//Even Points
//Sending the ghost nodes required at the top from i-1th processor to ith processor
if(my_id != size-1)
MPI_Send(phi[N_local-1],N,MPI_DOUBLE,my_id+1,tag1,MPI_COMM_WORLD);
if(my_id !=0){
MPI_Recv(&phi_top,N,MPI_DOUBLE,my_id-1,tag1,MPI_COMM_WORLD,&status);
for(j = 1; j<N-1; j++){
if( ((N/size)*my_id + j) % 2 == 0 ){
phi[0][j] = 0.25*(phi_top[j] + phi[1][j] + phi[0][j+1] + phi[0][j-1] - delta*delta*q[0][j]);
}
}
}
//Sending the ghost nodes required at the bottom from i+1th processor to ith processor
if(my_id !=0)
MPI_Send(phi[0],N,MPI_DOUBLE,my_id-1,tag2,MPI_COMM_WORLD);
if(my_id != size-1){
MPI_Recv(&phi_bot,N,MPI_DOUBLE,my_id+1,tag2,MPI_COMM_WORLD,&status);
for(j = 1; j<N-1; j++){
if( ((N/size)*my_id + N_local - 1 +j) % 2 == 0){
phi[N_local-1][j] = 0.25*(phi[N_local-2][j] + phi_bot[j] + phi[N_local-1][j+1] + phi[N_local-1][j-1] - delta*delta*q[N_local-1][j]);
}
}
}
//Updating interior phi values
for(i = 1; i<N_local-1; i++){
for(j = 1; j<N-1; j++){
if( ((N/size)*my_id + i + j) % 2 == 0){
phi[i][j] = 0.25*(phi[i+1][j] + phi[i-1][j] + phi[i][j+1] + phi[i][j-1] - delta*delta*q[i][j]);
}
}
}
//Error Calculation
double err_local = 0;
err = 0;
for (i = 0; i<N_local; i++){
for(j = 0; j<N; j++){
err_local += (phi_exact[i][j] - phi[i][j])*(phi_exact[i][j] - phi[i][j]);
}
}
MPI_Allreduce(&err_local,&err,1,MPI_DOUBLE,MPI_SUM,MPI_COMM_WORLD);
err = sqrt(err)/norm_exact_global;
iter += 1;
}
t2 = MPI_Wtime();
if(my_id ==0){
printf("Number of iterations for Parallel Red Black Method using MPI are : %d\n",iter);
printf("The problem size is : %d x %d\n",N,N);
printf("The error is : %lf\n",err);
printf("The time taken with %d processors is : %lf\n",size,t2-t1);
}
//Gathering the results using MPI_Gatherv as each processor has different number of elements
int *recvcounts = (int*)malloc(size*sizeof(int));
for(i = 0; i<size; i++){
recvcounts[i] = N*(N/size);}
recvcounts[size-1] += N*(N%size);
int *displs = (int*)malloc(size * sizeof(int));
int disp = 0;
for (int i = 0; i < size; i++) {
displs[i] = disp;
disp += recvcounts[i];
}
//Allocating memory for send receive buffers
double *phi_send = (double*)malloc(N*N_local*sizeof(double));
double *phi_global_recv = (double*)malloc(N*N*sizeof(double));
double **phi_global = (double**)malloc(N*sizeof(double*));
for(i = 0; i<N; i++){
phi_global[i] = (double*)malloc(N*sizeof(double));
}
//Converting matrices to arrays before sending
for(i = 0; i<N_local; i++){
for(j = 0; j<N; j++){
phi_send[i*N + j] = phi[i][j];
}
}
//Collecting all arrays on processor 0
MPI_Gatherv(phi_send,N*N_local,MPI_DOUBLE,phi_global_recv,recvcounts,displs,MPI_DOUBLE,0,MPI_COMM_WORLD);
if(my_id == 0){
//Converting the received array into a matrix
for(i = 0; i<N; i++){
for(j = 0; j<N; j++){
phi_global[i][j] = phi_global_recv[i*N + j];
}
}
}
//Freeing the alllocated memory
deallocate2DArray(phi, N_local);
deallocate2DArray(phi_exact, N_local);
deallocate2DArray(q, N_local);
MPI_Finalize();
return 0;
}