Newer
Older
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
/**
* Author: Baptiste Coudray
* School: HEPIA
* Class: ITI-3
* Year 2020-2021
*/
#include <stdio.h>
#include <stdint.h>
#include <mpi.h>
#include <math.h>
#include <stdbool.h>
#include "../gol.h"
#define INDEX_2D_TO_1D(y, x, nb_columns) ((y) * nb_columns + (x))
#define NORTH_INDEX 0
#define EAST_INDEX 1
#define SOUTH_INDEX 2
#define WEST_INDEX 3
#define NORTH_ROW_TAG 0
#define EAST_COLUMN_TAG 1
#define SOUTH_ROW_TAG 2
#define WEST_COLUMN_TAG 3
#define NORTH_EAST_CELL_TAG 4
#define SOUTH_EAST_CELL_TAG 5
#define SOUTH_WEST_CELL_TAG 6
#define NORTH_WEST_CELL_TAG 7
#define CHUNK_BOARD_TAG 8
#define ROOT_RANK 0
#define FPS 60
int createGridCommunicators(MPI_Comm *cartComm, MPI_Comm *rowComm, MPI_Comm *colComm, int nProc) {
int gridN = (int) sqrt(nProc);
int dimensions[2] = {gridN, gridN};
int periods[2] = {true, true}; // Cyclic on row-column
MPI_Cart_create(MPI_COMM_WORLD, 2, dimensions, periods, 1, cartComm);
/* Create row communicator */
int remainDims[2] = {false, true};
MPI_Cart_sub(*cartComm, remainDims, rowComm);
/* Create column communicator */
remainDims[0] = true; // rows
remainDims[1] = false; // columns
MPI_Cart_sub(*cartComm, remainDims, colComm);
return gridN;
}
int *divideBoard(int n, int chunkN, int nProc) {
int *indexes = calloc((size_t) nProc * 2, sizeof(int));
for (int i = 0, y = 0, x = 0; i < nProc; ++i) {
indexes[i * 2] = y;
indexes[i * 2 + 1] = x;
x += chunkN;
if (x >= n) {
x = 0;
y += chunkN;
}
}
return indexes;
}
void initChunkBoard(int8_t *chunkBoard, int chunkN) {
for (int i = 0; i < chunkN; ++i) {
for (int j = 0; j < chunkN; ++j) {
chunkBoard[INDEX_2D_TO_1D(i, j, chunkN)] = rand() % 2;
}
}
}
void shareAndBuildEnvelope(int8_t *chunkBoardMyEnvelope, int8_t *chunkBoardEnvelope, MPI_Comm rowComm,
MPI_Comm colComm, int gridN, int coordinates[2], int chunkN, int chunkM) {
int coordinateY = coordinates[0];
int coordinateX = coordinates[1];
MPI_Request requests[16] = {0};
int iRequest = 0;
// North
{
int8_t *chunkBoardMyEnvelopeNorth = &chunkBoardMyEnvelope[INDEX_2D_TO_1D(NORTH_INDEX, 0, chunkN)];
int8_t *chunkBoardEnvelopeNorth = &chunkBoardEnvelope[INDEX_2D_TO_1D(NORTH_INDEX, 1, chunkM)];
int destSource = (coordinateY - 1) < 0 ? (gridN - 1) : (coordinateY - 1);
MPI_Isend(chunkBoardMyEnvelopeNorth, chunkN, MPI_INT8_T, destSource, NORTH_ROW_TAG, colComm,
&requests[iRequest++]);
/* Neighbour send south row, which correspond to north envelope */
MPI_Irecv(chunkBoardEnvelopeNorth, chunkN, MPI_INT8_T, destSource, SOUTH_ROW_TAG, colComm,
&requests[iRequest++]);
}
// East
{
int8_t *chunkBoardMyEnvelopeEast = &chunkBoardMyEnvelope[INDEX_2D_TO_1D(EAST_INDEX, 0, chunkN)];
int8_t *chunkBoardEnvelopeEast = &chunkBoardEnvelope[INDEX_2D_TO_1D(EAST_INDEX, 1, chunkM)];
int destSource = (coordinateX + 1) % gridN;
MPI_Isend(chunkBoardMyEnvelopeEast, chunkN, MPI_INT8_T, destSource, EAST_COLUMN_TAG, rowComm,
&requests[iRequest++]);
/* Neighbour send west column, which correspond to east envelope */
MPI_Irecv(chunkBoardEnvelopeEast, chunkN, MPI_INT8_T, destSource, WEST_COLUMN_TAG, rowComm,
&requests[iRequest++]);
}
// South
{
int8_t *chunkBoardMyEnvelopeSouth = &chunkBoardMyEnvelope[INDEX_2D_TO_1D(SOUTH_INDEX, 0, chunkN)];
int8_t *chunkBoardEnvelopeSouth = &chunkBoardEnvelope[INDEX_2D_TO_1D(SOUTH_INDEX, 1, chunkM)];
int destSource = (coordinateY + 1) % gridN;
MPI_Isend(chunkBoardMyEnvelopeSouth, chunkN, MPI_INT8_T, destSource, SOUTH_ROW_TAG, colComm,
&requests[iRequest++]);
/* Neighbour send north row, which correspond to south envelope */
MPI_Irecv(chunkBoardEnvelopeSouth, chunkN, MPI_INT8_T, destSource, NORTH_ROW_TAG, colComm,
&requests[iRequest++]);
}
// West
{
int8_t *chunkBoardMyEnvelopeWest = &chunkBoardMyEnvelope[INDEX_2D_TO_1D(WEST_INDEX, 0, chunkN)];
int8_t *chunkBoardEnvelopeWest = &chunkBoardEnvelope[INDEX_2D_TO_1D(WEST_INDEX, 1, chunkM)];
int destSource = (coordinateX - 1) < 0 ? (gridN - 1) : (coordinateX - 1);
MPI_Isend(chunkBoardMyEnvelopeWest, chunkN, MPI_INT8_T, destSource, WEST_COLUMN_TAG, rowComm,
&requests[iRequest++]);
/* Neighbour send east column, which correspond to west envelope */
MPI_Irecv(chunkBoardEnvelopeWest, chunkN, MPI_INT8_T, destSource, EAST_COLUMN_TAG, rowComm,
&requests[iRequest++]);
}
int8_t missingCells[4] = {0};
// North-East
{
int8_t *chunkBoardMyEnvelopeNorthEast = &chunkBoardMyEnvelope[INDEX_2D_TO_1D(NORTH_INDEX, chunkN - 1, chunkN)];
int destSrcY = (coordinateY - 1) < 0 ? gridN - 1 : coordinateY - 1;
int destSrcX = (coordinateX + 1) % gridN;
int destSource = INDEX_2D_TO_1D(destSrcY, destSrcX, gridN);
MPI_Isend(chunkBoardMyEnvelopeNorthEast, 1, MPI_INT8_T, destSource, NORTH_EAST_CELL_TAG, MPI_COMM_WORLD,
&requests[iRequest++]);
/* Neighbour send south-west cell, which correspond to north-east cell */
MPI_Irecv(&missingCells[1], 1, MPI_INT8_T, destSource, SOUTH_WEST_CELL_TAG, MPI_COMM_WORLD,
&requests[iRequest++]);
}
// South-East
{
int8_t *chunkBoardMyEnvelopeSouthEast = &chunkBoardMyEnvelope[INDEX_2D_TO_1D(SOUTH_INDEX, chunkN - 1, chunkN)];
int destSrcY = (coordinateY + 1) % gridN;
int destSrcX = (coordinateX + 1) % gridN;
int destSource = INDEX_2D_TO_1D(destSrcY, destSrcX, gridN);
MPI_Isend(chunkBoardMyEnvelopeSouthEast, 1, MPI_INT8_T, destSource, SOUTH_EAST_CELL_TAG, MPI_COMM_WORLD,
&requests[iRequest++]);
/* Neighbour send north-west cell, which correspond to south-east cell */
MPI_Irecv(&missingCells[2], 1, MPI_INT8_T, destSource, NORTH_WEST_CELL_TAG, MPI_COMM_WORLD,
&requests[iRequest++]);
}
// South-West
{
int8_t *chunkBoardMyEnvelopeSouthWest = &chunkBoardMyEnvelope[INDEX_2D_TO_1D(SOUTH_INDEX, 0, chunkN)];
int destSrcY = (coordinateY + 1) % gridN;
int destSrcX = (coordinateX - 1) < 0 ? gridN - 1 : coordinateX - 1;
int destSource = INDEX_2D_TO_1D(destSrcY, destSrcX, gridN);
MPI_Isend(chunkBoardMyEnvelopeSouthWest, 1, MPI_INT8_T, destSource, SOUTH_WEST_CELL_TAG, MPI_COMM_WORLD,
&requests[iRequest++]);
/* Neighbour send north-east cell, which correspond to south-west cell */
MPI_Irecv(&missingCells[3], 1, MPI_INT8_T, destSource, NORTH_EAST_CELL_TAG, MPI_COMM_WORLD,
&requests[iRequest++]);
}
// North-West
{
int8_t *chunkBoardMyEnvelopeNorthWest = &chunkBoardMyEnvelope[INDEX_2D_TO_1D(NORTH_INDEX, 0, chunkN)];
int destSrcY = (coordinateY - 1) < 0 ? gridN - 1 : coordinateY - 1;
int destSrcX = (coordinateX - 1) < 0 ? gridN - 1 : coordinateX - 1;
int destSource = INDEX_2D_TO_1D(destSrcY, destSrcX, gridN);
MPI_Isend(chunkBoardMyEnvelopeNorthWest, 1, MPI_INT8_T, destSource, NORTH_WEST_CELL_TAG, MPI_COMM_WORLD,
&requests[iRequest++]);
/* Neighbour send south-east cell, which correspond to north-west cell */
MPI_Irecv(&missingCells[0], 1, MPI_INT8_T, destSource, SOUTH_EAST_CELL_TAG, MPI_COMM_WORLD,
&requests[iRequest]);
}
MPI_Waitall(16, requests, MPI_STATUSES_IGNORE);
/* Copy missing cells into the envelope */
chunkBoardEnvelope[INDEX_2D_TO_1D(NORTH_INDEX, chunkN, chunkM)] = chunkBoardEnvelope[INDEX_2D_TO_1D(
EAST_INDEX, 0, chunkM)] = missingCells[1];
chunkBoardEnvelope[INDEX_2D_TO_1D(SOUTH_INDEX, chunkN, chunkM)] = chunkBoardEnvelope[INDEX_2D_TO_1D(EAST_INDEX,
chunkN,
chunkM)] = missingCells[2];
chunkBoardEnvelope[INDEX_2D_TO_1D(SOUTH_INDEX, 0, chunkM)] = chunkBoardEnvelope[INDEX_2D_TO_1D(WEST_INDEX,
chunkN,
chunkM)] = missingCells[3];
chunkBoardEnvelope[INDEX_2D_TO_1D(NORTH_INDEX, 0, chunkM)] = chunkBoardEnvelope[INDEX_2D_TO_1D(WEST_INDEX,
0,
chunkM)] = missingCells[0];
}
void chunkBoardToBoard(int8_t *board, int n, const int8_t *chunkBoard, int chunkN, const int *indexes, int rank) {
int y = indexes[rank * 2];
int x = indexes[rank * 2 + 1];
for (int i = 0; i < chunkN; ++i) {
for (int j = 0; j < chunkN; ++j) {
board[INDEX_2D_TO_1D(y + i, x + j, n)] = chunkBoard[INDEX_2D_TO_1D(i, j, chunkN)];
}
}
}
int main(int argc, char *argv[]) {
if (argc < 3) {
printf("%s <deviceName> <boardN>\n", argv[0]);
exit(0);
}
int myRank;
int nProc;
double start = MPI_Wtime();
/* MPI Initialization */
MPI_Init(&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD, &myRank);
MPI_Comm_size(MPI_COMM_WORLD, &nProc);
srand((unsigned int) myRank);
MPI_Comm cartComm, rowComm, colComm;
int gridN = createGridCommunicators(&cartComm, &rowComm, &colComm, nProc);
int myCartRank;
MPI_Comm_rank(cartComm, &myCartRank);
int coordinates[2] = {0};
MPI_Cart_coords(cartComm, myCartRank, 2, coordinates);
/* Futhark Initialization */
struct futhark_context_config *contextConfig = futhark_context_config_new();
int nbDevices = atoi(argv[1]);
char device[3] = {0};
snprintf(device, sizeof(device), "#%d", myRank % nbDevices);
futhark_context_config_set_device(contextConfig, device);
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
struct futhark_context *futharkContext = futhark_context_new(contextConfig);
/* GoL Initialization */
int boardN = atoi(argv[2]);
int boardNN = boardN * boardN;
int chunkN = (int) (boardN / sqrt(nProc));
int chunkNN = chunkN * chunkN;
int chunkM = chunkN + 2;
int *indexes = divideBoard(boardN, chunkN, nProc);
int8_t *board = myRank == ROOT_RANK ? calloc((size_t) boardNN, sizeof(int8_t)) : NULL;
int8_t *chunkBoard = calloc((size_t) chunkNN, sizeof(int8_t));
int8_t *chunkBoardMyEnvelope = calloc(((size_t) (4 * chunkN)), sizeof(int8_t));
int8_t *chunkBoardEnvelope = calloc(((size_t) (4 * chunkM)), sizeof(int8_t));
initChunkBoard(chunkBoard, chunkN);
struct futhark_i8_2d *futChunkBoard = futhark_new_i8_2d(futharkContext, chunkBoard, chunkN, chunkN);
futhark_context_sync(futharkContext);
struct futhark_i8_2d *futChunkBoardMyEnvelope;
futhark_entry_get_envelope(futharkContext, &futChunkBoardMyEnvelope, futChunkBoard);
futhark_context_sync(futharkContext);
futhark_values_i8_2d(futharkContext, futChunkBoardMyEnvelope, chunkBoardMyEnvelope);
futhark_context_sync(futharkContext);
shareAndBuildEnvelope(chunkBoardMyEnvelope, chunkBoardEnvelope, rowComm, colComm, gridN, coordinates, chunkN,
chunkM);
struct futhark_i8_2d *futChunkBoardEnvelope = futhark_new_i8_2d(futharkContext, chunkBoardEnvelope, 4, chunkM);
futhark_context_sync(futharkContext);
struct futhark_i8_2d *futNextChunkBoard;
futhark_entry_next_chunk_board(futharkContext, &futNextChunkBoard, futChunkBoard, futChunkBoardEnvelope);
futhark_context_sync(futharkContext);
futhark_values_i8_2d(futharkContext, futNextChunkBoard, chunkBoard);
futhark_context_sync(futharkContext);
if (myRank == ROOT_RANK) {
chunkBoardToBoard(board, boardN, chunkBoard, chunkN, indexes, myRank);
int8_t *tmpChunkBoard = calloc((size_t) chunkNN, sizeof(int8_t));
MPI_Status status = {0};
for (int i = 0; i < nProc - 1; ++i) {
MPI_Recv(tmpChunkBoard, chunkNN, MPI_INT8_T, MPI_ANY_SOURCE, CHUNK_BOARD_TAG, MPI_COMM_WORLD, &status);
chunkBoardToBoard(board, boardN, tmpChunkBoard, chunkN, indexes, status.MPI_SOURCE);
}
free(tmpChunkBoard);
} else {
MPI_Send(chunkBoard, chunkNN, MPI_INT8_T, ROOT_RANK, CHUNK_BOARD_TAG, MPI_COMM_WORLD);
}
futhark_context_sync(futharkContext);
futhark_free_i8_2d(futharkContext, futChunkBoard);
futhark_free_i8_2d(futharkContext, futChunkBoardMyEnvelope);
futhark_free_i8_2d(futharkContext, futChunkBoardEnvelope);
futhark_free_i8_2d(futharkContext, futNextChunkBoard);
free(indexes);
free(chunkBoard);
free(chunkBoardEnvelope);
free(chunkBoardMyEnvelope);
if (myRank == ROOT_RANK) {
free(board);
}
futhark_context_free(futharkContext);
futhark_context_config_free(contextConfig);
MPI_Comm_free(&cartComm);
MPI_Comm_free(&rowComm);
MPI_Comm_free(&colComm);
double finish = MPI_Wtime();
if (myRank == ROOT_RANK) {
printf("%d;%f\n", nProc, finish - start);
}
MPI_Finalize();
return 0;
}