-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfox_mpi.c
319 lines (285 loc) · 11.3 KB
/
fox_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
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
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <time.h>
#include <mpi.h>
#define DEBUG 0 /* enable to display debug information */
#define CHECK 0 /* enable to test the parallel algorithm with the serial */
/*
* used links:
* https://parallel.ru/vvv/mpi.html
* https://www.mpich.org/static/docs/v3.1/www3/MPI_Type_create_subarray.html
* https://www.mpich.org/static/docs/v3.1/www3/MPI_Type_create_resized.html
* https://www.mpich.org/static/docs/v3.1/www3/MPI_Type_commit.html
* https://www.mpich.org/static/docs/v3.1/www3/MPI_Scatterv.html
* https://www.mpich.org/static/docs/v3.1/www3/MPI_Barrier.html
* https://www.mpich.org/static/docs/v3.1/www3/MPI_Bcast.html
* https://www.mpich.org/static/docs/v3.1/www3/MPI_Sendrecv_replace.html
* https://www.mpich.org/static/docs/v3.1/www3/MPI_Gatherv.html
*
*/
typedef struct {
MPI_Comm grid_comm; /* handle to global grid communicator */
MPI_Comm row_comm; /* row communicator */
MPI_Comm col_comm; /* column communicator */
int n_proc; /* number of processors */
int grid_dim; /* dimension of the grid, = sqrt(n_proc) */
int my_row; /* row position of a processor in a grid */
int my_col; /* column position of a procesor in a grid */
int my_rank; /* rank within the grid */
} GridInfo;
/**
* [FoxAlgorithm: parallel fox matrix multiplication algorithm]
*/
void FoxAlgorithm(double *A, double *B, double *C, int size, GridInfo *grid);
/**
* [grid_init: process grid initialization]
*/
void grid_init(GridInfo *grid);
/**
* [matrix functions]
*/
void matrix_creation(double **pA, double **pB, double **pC, int size);
void matrix_removal(double **pA, double **pB, double **pC);
void matrix_init(double *A, double *B, int size, int sup);
void matrix_dot(double *A, double *B, double *C, int n);
int matrix_check(double *A, double *B, int n);
void matrix_print(double *A, int n);
void grid_init(GridInfo *grid)
{
int old_rank;
int dimensions[2];
int wrap_around[2];
int coordinates[2];
int free_coords[2];
/* get the overall information before overlaying cart_grid */
MPI_Comm_size(MPI_COMM_WORLD, &(grid->n_proc));
MPI_Comm_rank(MPI_COMM_WORLD, &old_rank);
grid->grid_dim = (int)sqrt(grid->n_proc);
/* ERROR check: */
if (grid->grid_dim * grid->grid_dim != grid->n_proc) {
printf("[!] \'-np\' is a perfect square!\n");
exit(-1);
}
/* set the dimensions */
dimensions[0] = dimensions[1] = grid->grid_dim;
wrap_around[0] = wrap_around[1] = 1;
MPI_Cart_create(MPI_COMM_WORLD, 2, dimensions, wrap_around, 1, &(grid->grid_comm));
/* since we have set reorder to true, this might have changed the ranks */
MPI_Comm_rank(grid->grid_comm, &(grid->my_rank));
/* get the cartesian coordinates for the current process */
MPI_Cart_coords(grid->grid_comm, grid->my_rank, 2, coordinates);
/* set the coordinate values for the current process */
grid->my_row = coordinates[0];
grid->my_col = coordinates[1];
/* create row communicators */
free_coords[0] = 0;
free_coords[1] = 1;
MPI_Cart_sub(grid->grid_comm, free_coords, &(grid->row_comm));
/* create column communicators */
free_coords[0] = 1;
free_coords[1] = 0;
MPI_Cart_sub(grid->grid_comm, free_coords, &(grid->col_comm));
}
void matrix_creation(double **pA, double **pB, double **pC, int size)
{
*pA = (double *)malloc(size * size * sizeof(double));
*pB = (double *)malloc(size * size * sizeof(double));
*pC = (double *)calloc(size * size, sizeof(double));
}
void matrix_init(double *A, double *B, int size, int sup)
{
srand(time(NULL));
for (int i = 0; i < size * size; ++i) {
*(A + i) = rand() % sup + 1;
*(B + i) = rand() % sup + 1;
}
}
void matrix_dot(double *A, double *B, double *C, int size)
{
for (int i = 0; i < size; ++i) {
for (int j = 0; j < size; ++j) {
for (int k = 0; k < size; ++k) {
C[i * size + j] += A[i * size + k] * B[k * size + j];
}
}
}
}
int matrix_check(double *A, double *B, int size)
{
for (int i = 0; i < size * size; ++i) {
if (*(A + i) != *(B + i)) {
return 0;
}
}
return 1;
}
void matrix_print(double *A, int size)
{
printf("---~---~---~---~---~---~---~---\n");
for (int i = 0; i < size * size; ++i) {
printf("%.2lf ", *(A + i));
if ((i + 1) % size == 0){
printf("\n");
}
}
printf("---~---~---~---~---~---~---~---\n\n");
}
void matrix_removal(double **pA, double **pB, double **pC)
{
free(*pA);
free(*pB);
free(*pC);
}
/* ------------------------------ FoxAlgorithm ------------------------------ */
void FoxAlgorithm(double *A, double *B, double *C, int size, GridInfo *grid)
{
double *buff_A = (double*)calloc(size * size, sizeof(double));
MPI_Status status;
int root;
int src = (grid->my_row + 1) % grid->grid_dim;
int dst = (grid->my_row -1 + grid->grid_dim) % grid->grid_dim;
/**
* For each iterations:
* 1. find the blocks that are forming a diagonal
* 2. shared that block on the row it belongs to
* 3. multiply the updated A (or buff_A) with B onto C
* 4. shift the B blocks upward
*/
for (int stage = 0; stage < grid->grid_dim; ++stage) {
root = (grid->my_row + stage) % grid->grid_dim;
if (root == grid->my_col) {
MPI_Bcast(A, size * size, MPI_DOUBLE, root, grid->row_comm);
matrix_dot(A, B, C, size);
} else {
MPI_Bcast(buff_A, size * size, MPI_DOUBLE, root, grid->row_comm);
matrix_dot(buff_A, B, C, size);
}
MPI_Sendrecv_replace(B, size * size, MPI_DOUBLE, dst, 0, src, 0, grid->col_comm, &status);
}
}
/* -------------------------------------------------------------------------- */
int main(int argc, char **argv)
{
double *pA, *pB, *pC;
double *local_pA, *local_pB, *local_pC;
int matrix_size = 100;
if (argc == 2) {
sscanf(argv[1], "%d", &matrix_size);
}
/* -------------------------------------------------- */
MPI_Init(&argc, &argv);
/* -------------------------------------------------- */
GridInfo grid;
grid_init(&grid);
/* ERROR check: */
if (matrix_size % grid.grid_dim != 0) {
printf("[!] matrix_size mod sqrt(n_processes) != 0 !\n");
exit(-1);
}
/* -------------------------------------------------- */
if (grid.my_rank == 0) {
matrix_creation(&pA, &pB, &pC, matrix_size);
matrix_init(pA, pB, matrix_size, 10);
#if DEBUG
printf("pA (size=%d):\n", matrix_size); matrix_print(pA, matrix_size);
printf("pB (size=%d):\n", matrix_size); matrix_print(pB, matrix_size);
// printf("pC (size=%d):\n", matrix_size); matrix_print(pC, matrix_size);
#endif
}
int local_matrix_size = matrix_size / grid.grid_dim;
matrix_creation(&local_pA, &local_pB, &local_pC, local_matrix_size);
/* -------------------------------------------------- */
MPI_Datatype blocktype, type;
int array_size[2] = {matrix_size, matrix_size};
int subarray_sizes[2] = {local_matrix_size, local_matrix_size};
int array_start[2] = {0, 0};
MPI_Type_create_subarray(2, array_size, subarray_sizes, array_start,
MPI_ORDER_C, MPI_DOUBLE, &blocktype);
MPI_Type_create_resized(blocktype, 0, local_matrix_size * sizeof(double), &type);
MPI_Type_commit(&type);
/* -------------------------------------------------- */
int displs[grid.n_proc];
int sendcounts[grid.n_proc];
if (grid.my_rank == 0) {
for (int i = 0; i < grid.n_proc; ++i) {
sendcounts[i] = 1;
}
int disp = 0;
for (int i = 0; i < grid.grid_dim; ++i) {
for (int j = 0; j < grid.grid_dim; ++j) {
displs[i * grid.grid_dim + j] = disp;
disp += 1;
}
disp += (local_matrix_size - 1) * grid.grid_dim;
}
}
/* -------------------------------------------------- */
MPI_Scatterv(pA, sendcounts, displs, type, local_pA,
local_matrix_size * local_matrix_size, MPI_DOUBLE, 0, MPI_COMM_WORLD);
MPI_Scatterv(pB, sendcounts, displs, type, local_pB,
local_matrix_size * local_matrix_size, MPI_DOUBLE, 0, MPI_COMM_WORLD);
/* -------------------------------------------------- */
#if DEBUG
int print_rank = 0;
while (print_rank < grid.n_proc) {
if (grid.my_rank == print_rank) {
printf("%d. local_pA (size=%d):\n", grid.my_rank, local_matrix_size);
matrix_print(local_pA, local_matrix_size);
printf("%d. local_pB (size=%d):\n", grid.my_rank, local_matrix_size);
matrix_print(local_pB, local_matrix_size);
}
print_rank++;
MPI_Barrier(grid.grid_comm);
}
#endif
/* -------------------------------------------------- */
double start_time, end_time;
MPI_Barrier(grid.grid_comm);
if (grid.my_rank == 0) {
start_time = MPI_Wtime();
}
/* vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv */
/* -------------------------------------------------- */
FoxAlgorithm(local_pA, local_pB, local_pC, local_matrix_size, &grid);
/* -------------------------------------------------- */
/* ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ */
MPI_Barrier(grid.grid_comm);
if (grid.my_rank == 0) {
end_time = MPI_Wtime() - start_time;
}
/* collect submatrices from all processes: */
MPI_Gatherv(local_pC, local_matrix_size*local_matrix_size, MPI_DOUBLE, pC, sendcounts, displs, type, 0, MPI_COMM_WORLD);
/* -------------------------------------------------- */
if (grid.my_rank == 0) {
// FoxAlgorithm_time | number_processes | matrix_size"
printf("%.5lf, %d, %d\n", end_time, grid.n_proc, matrix_size);
#if DEBUG
/* Sequential implementation comparison */
printf("pC:\n"); matrix_print(pC, matrix_size);
double *pD = (double *)calloc(matrix_size * matrix_size, sizeof(double));
start_time = MPI_Wtime();
matrix_dot(pA, pB, pD, matrix_size);
end_time = MPI_Wtime() - start_time;
printf("pD:\n"); matrix_print(pD, matrix_size);
printf("dot_time: %.5lf\n", end_time);
printf("matrix_check: %s\n", matrix_check(pC, pD, matrix_size) ? "yes" : "NO");
free(pD);
#endif
#if CHECK
double *pD = (double *)calloc(matrix_size * matrix_size, sizeof(double));
start_time = MPI_Wtime();
matrix_dot(pA, pB, pD, matrix_size);
end_time = MPI_Wtime() - start_time;
printf("dot_time: %.5lf\n", end_time);
printf("matrix_check: %s\n", matrix_check(pC, pD, matrix_size) ? "yes" : "NO");
free(pD);
#endif
matrix_removal(&pA, &pB, &pC);
}
matrix_removal(&local_pA, &local_pB, &local_pC);
/* -------------------------------------------------- */
MPI_Finalize();
/* -------------------------------------------------- */
return 0;
}