-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
341 lines (301 loc) · 9.5 KB
/
Copy pathmain.cpp
File metadata and controls
341 lines (301 loc) · 9.5 KB
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
#include <stdlib.h>
#include <ostream>
#include <iostream>
#include <mpi.h>
#include <Row.hpp>
#include <Column.hpp>
#define MCW MPI_COMM_WORLD
int ROWCOUNT = 20;
int COLUMNCOUNT = 20;
int TIMESTEPS = 20;
int TIME_PROGRAM = 1;
void initializeMpi(int argc, char *argv[],int &rank, int &size){
MPI_Init(&argc,&argv);
MPI_Comm_rank(MCW,&rank);
MPI_Comm_size(MCW,&size);
}
int** initGrid(int **arr,int rows, int columns){
arr = new int*[rows];
for(int i = 0; i < rows; i++){
arr[i] = new int[columns];
}
return arr;
}
int** fillGrid(int **arr,int rows, int columns){
for(int i = 0; i < rows; i++){
for(int j = 0; j < columns; j++){
arr[i][j] = rand()%2;
}
}
return arr;
}
// print the processes grid with ghost rows and columns
void printProcessGrid(int **board,int rows, int columns){
std::cout << "" << std::endl;
for(int i = 0; i < rows; i++){
for(int j = 0; j < columns; j++){
std::cout << board[i][j] << " ";
}
std::cout << "" << std::endl;
}
}
// print the processes grid without ghost rows and columns
void printActualProcessGrid(int **board,int rows, int columns){
std::cout << "" << std::endl;
for(int i = 1; i < rows-1; i++){
for(int j = 1; j < columns-1; j++){
std::cout << board[i][j] << " ";
}
std::cout << "" << std::endl;
}
}
// prints the combined grid of all proccesses without ghost rows and columns
void printWholeGrid(int **board,int rank, int rows, int columns){
std::cout << "" << std::endl;
for(int i = 1; i < rows-1; i++){
for(int j = 1; j < columns-1; j++){
std::cout << board[i][j] << " ";
}
std::cout << "" << std::endl;
}
}
int** copyGrid(int **copyTo, int **copyFrom,int row, int column){
for(int i = 0; i < row; i++){
for(int j = 0; j < column; j++){
copyTo[i][j] = copyFrom[i][j];
}
}
return copyTo;
}
int getNextLowestRank(int rank, int size){
int nextLowestRank = rank-1;
if(nextLowestRank == -1){
nextLowestRank = size-1;
}
return nextLowestRank;
}
int getNextHighestRank(int rank, int size){
int nextHighestRank = rank+1;
if(nextHighestRank == size){
nextHighestRank = 0;
}
return nextHighestRank;
}
int getTopSource(int rank, int size){
int source = rank+1;
if(source == size){
source = 0;
}
return source;
}
int getBottomSource(int rank, int size){
int source = rank-1;
if(source == -1){
source = size-1;
}
return source;
}
int** ghostRowToGrid(int** grid, int* topGhostRow, int* bottomGhostRow, int rows, int columns){
for(int i = 0; i < rows; i++){
for(int j = 0; j < columns; j++){
if(i == 0){
grid[i][j] = topGhostRow[j];
}else if( i == rows-1){
grid[i][j] = bottomGhostRow[j];
}
}
}
return grid;
}
int** ghostColumnToGrid(int** grid, int* leftGhostColumn, int* rightGhostColumn, int rows, int columns){
for(int i = 0; i < rows; i++){
for(int j = 0; j < columns; j++){
if(j == 0){
grid[i][j] = rightGhostColumn[i];
}else if( j == columns-1){
grid[i][j] = leftGhostColumn[i];
}
}
}
return grid;
}
int** fillGhostRows(int** currentGrid, int rank, int size, int rows, int columns){
Row bottomGhostRow(columns);
Row topGhostRow(columns);
bottomGhostRow.createGhostRow(currentGrid, 1);
topGhostRow.createGhostRow(currentGrid, rows-2);
// distributed memory is defined send and recive ghost rows
// distributed memory is not defined no extra tasks is needed
if(size > 1){
int nextLowestRank = getNextLowestRank(rank,size);
MPI_Send(bottomGhostRow.getGhostRow(),columns,MPI_INT,nextLowestRank,0,MCW);
int nextHighestRank = getNextHighestRank(rank,size);
MPI_Send(topGhostRow.getGhostRow(),columns,MPI_INT,nextHighestRank,1,MCW);
int topSource = getTopSource(rank,size);
int bottomSource = getBottomSource(rank,size);
MPI_Recv(bottomGhostRow.getGhostRow(),columns,MPI_INT,topSource,0,MCW,MPI_STATUS_IGNORE);
MPI_Recv(topGhostRow.getGhostRow(),columns,MPI_INT,bottomSource,1,MCW,MPI_STATUS_IGNORE);
}
currentGrid = ghostRowToGrid(currentGrid,topGhostRow.getGhostRow(),bottomGhostRow.getGhostRow(),rows,columns);
return currentGrid;
}
int** fillGhostColumns(int** currentGrid, int rank, int size, int rows, int columns){
Column leftGhostColumn(rows);
Column rightGhostColumn(rows);
leftGhostColumn.createGhostColumn(currentGrid,1);
rightGhostColumn.createGhostColumn(currentGrid,columns-2);
currentGrid = ghostColumnToGrid(currentGrid,leftGhostColumn.getGhostColumn(),rightGhostColumn.getGhostColumn(),rows,columns);
return currentGrid;
}
int main(int argc, char *argv[]){
int rank;
int size;
double start;
MPI_Status status;
initializeMpi(argc, argv,rank,size);
MPI_Barrier(MCW);
if(rank == 0 && TIME_PROGRAM == 1){
start = MPI_Wtime();
}
srand(time(NULL) + rank);
int rows = (ROWCOUNT/size);
int columns = 0;
// give the remainder of the work to the last proccess
if(rank == size-1){
int rowRemainder = ROWCOUNT%size;
rows = rows+rowRemainder;
}
// add two for the ghost rows and columns
rows = rows +2;
columns = COLUMNCOUNT+2;
int **currentGrid;
currentGrid = initGrid(currentGrid,rows,columns);
currentGrid = fillGrid(currentGrid,rows,columns);
int **nextGrid;
nextGrid = initGrid(nextGrid,rows,columns);
nextGrid = copyGrid(nextGrid,currentGrid,rows,columns);
for(int i = 0; i < TIMESTEPS; i++){
currentGrid = fillGhostRows(currentGrid, rank, size, rows, columns);
currentGrid = fillGhostColumns(currentGrid, rank, size, rows, columns);
MPI_Barrier(MCW);
// TO DO put all of this into its own function
if(rank !=0){
// convert 2d pointer to 2d array
// we use rows-2 because we do not want the ghos rows included
int bk[(rows-2)][COLUMNCOUNT] = {};
// i and j start at 1 and rows and columns get subtracted by one so we ignore the ghost rows
// bk[i-1][j-1]is so the array will start at 0 and not 1
for(int i = 1; i < rows-1; i++){
for(int j = 1; j < columns-1; j++){
bk[i-1][j-1] = currentGrid[i][j];
}
}
MPI_Send(bk,(rows-2)*COLUMNCOUNT,MPI_INT,0,3,MCW);
}else{
// rank 0 recives grids from other proccesses and organizes them
// add a count varible to keep track of the amount of ranks that have been proccesed
// starts at 1 because 0 will proccess itself
int count = 1;
// create a new 3d array so we can order the grids by rank
int arr[size][ROWCOUNT][COLUMNCOUNT] = {};
// creates a new array that keeps track of ranks row sizes
int rankRowSizes[size] = {};
// sets 0 to its row size not counting the ghost row
rankRowSizes[0] = (rows-2);
// copys rank zero's actual grid (no ghost rows or columns) to the array
// i and j start at 1 and rows and columns get subtracted by one so we ignore the ghost rows
// bk[i-1][j-1] is so the array will start at 0 and not 1
for(int i = 1; i < rows-1; i++){
for(int j = 1; j < columns-1; j++){
arr[0][i-1][j-1] = currentGrid[i][j];
}
}
// infinitly loop to make sure we recive all the ranks data
if(size > 1){
while(1){
count ++;
int sendSize = 0;
MPI_Probe(MPI_ANY_SOURCE, 3, MPI_COMM_WORLD, &status);
// gets the size of the data getting recived
MPI_Get_count(&status, MPI_INT, &sendSize);
// gets the senders rank;
int sendRank = status.MPI_SOURCE;
// gets the rows and columns of the data getting recived (this does not include ghost rows)
int y = COLUMNCOUNT;
int x = sendSize/(COLUMNCOUNT);
// creates a new array so we can recive the data
int actualGrid[x][y] = {};
// fills the recived data row size
rankRowSizes[sendRank] = x;
// Finally we recive the data
MPI_Recv(actualGrid,sendSize,MPI_INT,sendRank,3,MCW,&status);
// copys the data in the correct order
for(int i = 0; i < x; i++){
for(int j = 0; j < y; j++){
arr[sendRank][i][j] = actualGrid[i][j];
}
}
// once all the ranks have been proccesed we break out of our loop
if(count == size){
break;
}
}
}
// print current Grid
std::cout << "Current Grid" << std::endl;
// print the current grid
for (int ranks = 0; ranks < size; ranks++){
for(int i = 0; i < rankRowSizes[ranks]; i++){
for(int j = 0; j < COLUMNCOUNT; j++){
std::cout << arr[ranks][i][j] << " ";
}
std::cout << "" << std::endl;
}
}
}
//threading
// Do not include the ghost rows and columns
for (int i = 1; i < rows-1; ++i){
for (int j = 1; j < columns-1; ++j){
int alive;
// This directive lets you define a parallel region containing a single for directive in one step.
#pragma omp parallel for private(alive)
alive = 0;
// search though whole grid ghosts included
// find alive neighbors
for (int r = i-1; r <= i+1; ++r){
for (int c = j-1; c <= j+1; ++c){
if(r != i || c != j){
if(currentGrid[r][c] == 1){
alive = alive + 1;
}
}
}
}
// apply rules for game
if(nextGrid[i][j] == 1){
if(alive == 2 || alive == 3){
nextGrid[i][j] = 1;
continue;
}
}
if(nextGrid[i][j] == 0){
if(alive == 3){
nextGrid[i][j] = 1;
continue;
}
}
nextGrid[i][j] = 0;
}
}
// TODO spawn threads to do this
// copy next grid into current
currentGrid = copyGrid(currentGrid,nextGrid,rows,columns);
}
MPI_Barrier(MCW);
if(rank == 0 && TIME_PROGRAM == 1){
double end = MPI_Wtime();
std::cout << "The process took " << end - start << " seconds to run." << std::endl;
}
MPI_Finalize();
}