-
Notifications
You must be signed in to change notification settings - Fork 0
/
mpi_2d_heat.cpp
322 lines (287 loc) · 12.2 KB
/
mpi_2d_heat.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
// Code written by Hammad Ather on Jan 23rd, 2022.
#include <iostream>
#include <math.h>
#include <mpi.h>
#define MASTER 0
#define BEGIN 1
using namespace std;
void convergence(float * worker_region, float * worker_region_copy, int timestamp_index, int taskid, int region_size_row,
int region_size_col, bool &convergence_flag, float C){
bool change = true;
for(int i = 0; i < region_size_row; i++){
for(int j = 0; j < region_size_col; j++){
if(abs(*((worker_region + i * region_size_col) + j) - *((worker_region_copy + i * region_size_col) + j)) > C){
change = false;
break;
}
}
if (change == false)
break;
}
if (change == true) {
convergence_flag = true;
cout << "\nTask ID " << taskid << " has converged!\n";
}
else {
if ((timestamp_index + 1) % 5 == 0){
float max = 0.0;
for(int i = 0; i < region_size_row; i++){
for(int j = 0; j < region_size_col; j++){
if(abs(*((worker_region + i * region_size_col) + j) - *((worker_region_copy + i * region_size_col) + j)) > max){
max = abs(*((worker_region + i * region_size_col) + j) - *((worker_region_copy + i * region_size_col) + j));
}
}
}
cout << "\nTask ID: " << taskid << " maximum update difference = " << max << "\n";
}
}
}
void update(float * worker_region, float * col_left, float *col_right, float *row_below, float *row_above, int left, int right, int above,
int below, int timestamp_index, int taskID, int region_size_row, int region_size_col, bool &convergence_flag, float C){
float * worker_region_copy = new float[region_size_col * region_size_row];
for (int i = 0; i < region_size_row; i++){
for (int j = 0; j < region_size_col; j++){
*((worker_region_copy + i * region_size_col) + j) = *((worker_region + i * region_size_col) + j);
}
}
int row_start = 0, row_end = region_size_row, col_start = 0, col_end = region_size_col;
if (above == -1){
row_start = 1;
row_end = region_size_row;
}
if (below == -1){
row_start = 0;
row_end = region_size_row - 1;
}
if (left == -1){
col_start = 1;
col_end = region_size_col;
}
if (right == -1){
col_start = 0;
col_end = region_size_col - 1;
}
float left_val = 0.0, right_val = 0.0 , above_val = 0.0, below_val = 0.0;
for (int i = row_start; i < row_end; i++){
for(int j = col_start; j < col_end; j++){
float val = *((worker_region_copy + i * region_size_col) + j);
if ((j + 1) >= region_size_col){
right_val = *(col_right + i);
}else{
right_val = *((worker_region_copy + i * region_size_col) + (j + 1));
}
if ((j - 1) < 0){
left_val = *(col_left + i);
}else{
left_val = *((worker_region_copy + i * region_size_col) + (j - 1));
}
if ((i + 1) >= region_size_row){
below_val = *(row_below + j);
}else{
below_val = *((worker_region_copy + (i + 1) * region_size_col) + j);
}
if ((i - 1) < 0){
above_val = *(row_above + j);
}else{
above_val = *((worker_region_copy + (i - 1) * region_size_col) + j);
}
float new_val = (right_val + left_val + above_val + below_val)/4;
*((worker_region + i * region_size_col) + j) = new_val;
}
}
convergence(worker_region, worker_region_copy, timestamp_index, taskID, region_size_row, region_size_col, convergence_flag, C);
}
void send(int left, int right, int below, int above, float * worker_region, float * col_left, float * col_right, float * row_below,
float * row_above, int region_size_row, int region_size_col, MPI_Status status){
float * col = new float[region_size_row];
float * row = new float[region_size_col];
MPI_Request request;
if(left != -1){
for (int k = 0; k < region_size_row; k++){
col[k] = *((worker_region + k * region_size_row) + 0);
}
MPI_Isend(col, region_size_row, MPI_FLOAT, left, BEGIN, MPI_COMM_WORLD, &request);
MPI_Irecv(col_left, region_size_row, MPI_FLOAT, left, BEGIN, MPI_COMM_WORLD, &request);
MPI_Wait(&request, &status);
}
if(right != -1){
for (int k = 0; k < region_size_row; k++){
col[k] = *((worker_region + k * region_size_row) + (region_size_col - 1));
}
MPI_Isend(col, region_size_row, MPI_FLOAT, right, BEGIN, MPI_COMM_WORLD, &request);
MPI_Irecv(col_right, region_size_row, MPI_FLOAT, right, BEGIN, MPI_COMM_WORLD, &request);
MPI_Wait(&request, &status);
}
if(below != -1){
for (int k = 0; k < region_size_col; k++){
row[k] = *((worker_region + (region_size_row - 1) * region_size_col) + k);
}
MPI_Isend(row, region_size_col, MPI_FLOAT, below, BEGIN, MPI_COMM_WORLD, &request);
MPI_Irecv(row_below, region_size_col, MPI_FLOAT, below, BEGIN, MPI_COMM_WORLD, &request);
MPI_Wait(&request, &status);
}
if(above != -1){
for (int k = 0; k < region_size_col; k++){
row[k] = *((worker_region + 0 * region_size_col) + k);
}
MPI_Isend(row, region_size_col, MPI_FLOAT, above, BEGIN, MPI_COMM_WORLD, &request);
MPI_Irecv(row_above, region_size_col, MPI_FLOAT, above, BEGIN, MPI_COMM_WORLD, &request);
MPI_Wait(&request, &status);
}
}
int main(int argc, char *argv[]){
MPI_Init(&argc, &argv);
int number_of_processes, taskid;
MPI_Comm_size(MPI_COMM_WORLD, &number_of_processes);
MPI_Comm_rank(MPI_COMM_WORLD,&taskid);
MPI_Status status;
int M = 1024, N = 2048;
int timestamps = 100;
float C = 0.004;
int region_size_row, region_size_col;
// Calculating region size depending upon the number of processes
if(number_of_processes == 1){
region_size_row = M;
region_size_col = N;
}
else if(number_of_processes == 2){
region_size_row = M;
region_size_col = N/2;
}else if(number_of_processes == 4 || number_of_processes == 16){
region_size_row = M/sqrt(number_of_processes);
region_size_col = N/sqrt(number_of_processes);
}else if(number_of_processes == 8){
region_size_row = M/2;
region_size_col = N/4;
}else if(number_of_processes == 32){
region_size_row = M/4;
region_size_col = N/8;
}
float ** V = new float*[M];
float * regions = new float[number_of_processes * region_size_row * region_size_col];
float * worker_region = new float[region_size_col * region_size_row];
float * col_right = new float[region_size_row];
float * col_left = new float[region_size_row];
float * row_below = new float[region_size_col];
float * row_above = new float[region_size_col];
bool convergence = false;
int timestamp_index = 0;
int left, right, below, above;
if(taskid == MASTER){
// Initializing the grid with default values
for(int i = 0; i < M; ++i)
V[i] = new float[N];
for(int i = 0; i < M; i++){
for(int j = 0; j < N; j++){
V[i][j] = 0.0;
}
}
for(int i = 0; i < M; i++){
V[i][0] = 1.0;
V[i][N-1] = 1.0;
}
for(int i = 0; i < N; i++){
V[0][i] = 1.0;
V[M-1][i] = 1.0;
}
int index = 0, col = 0, row = 0, mul = 0, col_mul = 0;
int num_region = 0, dest = 0, flag = 0;
// Creating regions from the grid V
for (int i = 0; i < number_of_processes; i++){
for (int j = 0; j < region_size_row; j++){
col = region_size_col * col_mul;
for (int k = 0; k < region_size_col; k++){
*(regions + i * region_size_row * region_size_col + j * region_size_col + k) = V[row][col];
col++;
}
row++;
}
if(col == N){
mul = mul + 1;
col = 0;
col_mul = 0;
if(flag == 0){
num_region = i;
flag = 1;
}
}
else{
col_mul = col_mul + 1;
col = region_size_col * col_mul;
}
row = 0;
row = region_size_row * mul;
}
// Determining the neighbours of a region and then sending the region and its neighbours to that that specifc worker task
int org_region = num_region;
for (int i = 1; i < number_of_processes; i++){
dest = i;
if (i<=num_region){
left = i - 1;
if(i != num_region){
right = i + 1;
}else{
right = -1;
}
}else{
num_region = num_region + org_region + 1;
right = i + 1;
left = -1;
}
if ((i - org_region - 1) >= 0){
above = i - org_region - 1;
}else{
above = -1;
}
if ((i + org_region + 1) < number_of_processes){
below = i + org_region + 1;
}else{
below = -1;
}
MPI_Send(regions + i * region_size_row * region_size_col, region_size_row * region_size_col, MPI_FLOAT, dest, BEGIN, MPI_COMM_WORLD);
MPI_Send(&left, 1, MPI_INT, dest, BEGIN, MPI_COMM_WORLD);
MPI_Send(&right, 1, MPI_INT, dest, BEGIN, MPI_COMM_WORLD);
MPI_Send(&above, 1, MPI_INT, dest, BEGIN, MPI_COMM_WORLD);
MPI_Send(&below, 1, MPI_INT, dest, BEGIN, MPI_COMM_WORLD);
}
// Determining the neighbours of the MASTER region depending upon the number of processes
if (number_of_processes == 1)
left = -1, right = -1, above = -1, below = -1;
else if (number_of_processes == 2)
left = -1, right = 1, above = -1, below = -1;
else
left = -1, right = 1, above = -1, below = org_region + 1;
for (int i = 0; i < region_size_row; i++){
for(int j = 0; j < region_size_col; j++){
*((worker_region + i * region_size_col) + j) = *(regions + 0 * region_size_row * region_size_col + i * region_size_col + j);
}
}
while(timestamp_index < timestamps){
if (number_of_processes != 1){
send(left, right, below, above, worker_region, col_left, col_right, row_below, row_above, region_size_row, region_size_col, status);
}
if(convergence == false){
update(worker_region, col_left, col_right, row_below, row_above, left, right, above, below, timestamp_index,
taskid, region_size_row, region_size_col, convergence, C);
}
timestamp_index++;
}
}
else if(taskid != MASTER){
// Each worker thread receives its region and information about the neighbours of that region
MPI_Recv(worker_region, region_size_row * region_size_col, MPI_FLOAT, MASTER, BEGIN, MPI_COMM_WORLD, &status);
MPI_Recv(&left, 1, MPI_INT, MASTER, BEGIN, MPI_COMM_WORLD, &status);
MPI_Recv(&right, 1, MPI_INT, MASTER, BEGIN, MPI_COMM_WORLD, &status);
MPI_Recv(&above, 1, MPI_INT, MASTER, BEGIN, MPI_COMM_WORLD, &status);
MPI_Recv(&below, 1, MPI_INT, MASTER, BEGIN, MPI_COMM_WORLD, &status);
while(timestamp_index < timestamps){
send(left, right, below, above, worker_region, col_left, col_right, row_below, row_above, region_size_row, region_size_col, status);
if(convergence == false){
update(worker_region, col_left, col_right, row_below, row_above, left, right, above, below, timestamp_index,
taskid, region_size_row, region_size_col, convergence, C);
}
timestamp_index++;
}
}
MPI_Finalize();
}