-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparallel.c
More file actions
409 lines (265 loc) · 13 KB
/
parallel.c
File metadata and controls
409 lines (265 loc) · 13 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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <mpi.h>
size_t SIZE;
typedef unsigned int M_TYPE;
#define M_MPI_TYPE MPI_UNSIGNED
#define PLACEHOLDER "%6u"
void fill_random_matrix(M_TYPE m[]) {
size_t i, j;
for (i = 0; i < SIZE; i++)
for (j = 0; j < SIZE; j++)
m[(i * SIZE) + j] = (M_TYPE)(rand() % 100);
}
void matrix_transpose(M_TYPE m[], M_TYPE mT[]) {
size_t i, j;
for (i = 0; i < SIZE; i++)
for (j = 0; j < SIZE; j++)
mT[(j * SIZE) + i] = m[(i * SIZE) + j];
}
void print_matrix(M_TYPE m[]) {
size_t i, j;
for (i = 0; i < SIZE; i++) {
printf("\n\t| ");
for (j = 0; j < SIZE; j++)
printf(PLACEHOLDER " ", m[(i * SIZE) + j]);
printf("|");
}
}
// Use monodimensional arrays instead of matrices for A, B, and C.
void mmult_mono(M_TYPE a[], M_TYPE b[], M_TYPE c[], int numprocs, int procid, double *start, double *end) {
size_t i, j, k;
// Get initial time
*start = MPI_Wtime();
// Broadcasts data from one member of a group to all members of the group.
MPI_Bcast( b, // Pointer to the data buffer
SIZE * SIZE, // Number of data elements in the buffer
M_MPI_TYPE, // MPI data type of the elements in the send buffer
0, MPI_COMM_WORLD);
// Prepare the displacements and counts matrices. They are the same for both MPI_Scatterv and MPI_Gatherv.
// The idea is to split the rows of matrix A equally among all processors.
// The entire row goes to the assigned process, so the rows don't get fragmented.
int counts[numprocs], displacements[numprocs];
int displacement = 0;
for (i = 0; i < numprocs; i++) {
counts[i] = ((i < (SIZE % numprocs)) ? ((SIZE / numprocs) + 1) : (SIZE / numprocs)) * SIZE;
displacements[i] = displacement;
displacement += counts[i];
}
// Reception buffer declaration.
// For each process, this contains only the rows of A that have been assigned to that process.
int recvcount = ((procid < (SIZE % numprocs)) ? ((SIZE / numprocs) + 1) : (SIZE / numprocs)) * SIZE;
M_TYPE *recvbuf = malloc(recvcount * sizeof(M_TYPE));
// Scatters data from one member across all members of a group.
MPI_Scatterv( a, // Pointer to a buffer that contains the data to be sent by the root process
counts, // Number of elements to send to each process
displacements, // Locations of the data to send to each communicator process
M_MPI_TYPE, // MPI data type of each element in the buffer
recvbuf, // Pointer to a buffer that contains the data that is received on each process
recvcount, // Number of elements in the receive buffer
M_MPI_TYPE, // MPI data type of the elements in the receive buffer
0, MPI_COMM_WORLD);
// MPI_Barrier( MPI_COMM_WORLD);
// Send buffer declaration.
// For each process, the rows of C corresponding to the rows of A assigned to that process
// will be calculated in this buffer.
int sendcount = ((procid < (SIZE % numprocs)) ? ((SIZE / numprocs) + 1) : (SIZE / numprocs)) * SIZE;
M_TYPE *sendbuf = malloc(sendcount * sizeof(M_TYPE));
for (i = 0; i < sendcount / SIZE; i++)
for (j = 0; j < SIZE; j++)
for (k = 0; k < SIZE; k++)
sendbuf[(i * SIZE) + j] += recvbuf[(i * SIZE) + k] * b[(k * SIZE) + j];
// Gathers variable data from all members of a group to one member.
MPI_Gatherv( sendbuf, // Pointer to a buffer that contains the data to be sent to the root process
sendcount, // Number of elements in the send buffer
M_MPI_TYPE, // MPI data type of each element in the buffer
c, // Pointer to a buffer on the root process that contains the data that is received from each process, including data that is sent by the root process
counts, // Number of elements that is received from each process
displacements, // Location, relative to the recvbuf parameter, of the data from each communicator process
M_MPI_TYPE, // MPI data type of each element in buffer
0, MPI_COMM_WORLD);
// MPI_Barrier( MPI_COMM_WORLD);
// Get final time
*end = MPI_Wtime();
}
// Monodim. + transposition of B to access both A and B row-wise in order to reduce cache misses.
void mmult_transp(M_TYPE a[], M_TYPE bT[], M_TYPE c[], int numprocs, int procid, double *start, double *end) {
size_t i, j, k;
// Get initial time
*start = MPI_Wtime();
// Broadcasts data from one member of a group to all members of the group.
MPI_Bcast( bT, // Pointer to the data buffer
SIZE * SIZE, // Number of data elements in the buffer
M_MPI_TYPE, // MPI data type of the elements in the send buffer
0, MPI_COMM_WORLD);
// Prepare the displacements and counts matrices. They are the same for both MPI_Scatterv and MPI_Gatherv.
// The idea is to split the rows of matrix A equally among all processors.
// The entire row goes to the assigned process, so the rows don't get fragmented.
int counts[numprocs], displacements[numprocs];
int displacement = 0;
for (i = 0; i < numprocs; i++) {
counts[i] = ((i < (SIZE % numprocs)) ? ((SIZE / numprocs) + 1) : (SIZE / numprocs)) * SIZE;
displacements[i] = displacement;
displacement += counts[i];
}
// Reception buffer declaration.
// For each process, this contains only the rows of A that have been assigned to that process.
int recvcount = ((procid < (SIZE % numprocs)) ? ((SIZE / numprocs) + 1) : (SIZE / numprocs)) * SIZE;
M_TYPE *recvbuf = malloc(recvcount * sizeof(M_TYPE));
// Scatters data from one member across all members of a group.
MPI_Scatterv( a, // Pointer to a buffer that contains the data to be sent by the root process
counts, // Number of elements to send to each process
displacements, // Locations of the data to send to each communicator process
M_MPI_TYPE, // MPI data type of each element in the buffer
recvbuf, // Pointer to a buffer that contains the data that is received on each process
recvcount, // Number of elements in the receive buffer
M_MPI_TYPE, // MPI data type of the elements in the receive buffer
0, MPI_COMM_WORLD);
// MPI_Barrier( MPI_COMM_WORLD);
// Send buffer declaration.
// For each process, the rows of C corresponding to the rows of A assigned to that process
// will be calculated in this buffer.
int sendcount = ((procid < (SIZE % numprocs)) ? ((SIZE / numprocs) + 1) : (SIZE / numprocs)) * SIZE;
M_TYPE *sendbuf = malloc(sendcount * sizeof(M_TYPE));
for (i = 0; i < sendcount / SIZE; i++)
for (j = 0; j < SIZE; j++)
for (k = 0; k < SIZE; k++)
sendbuf[(i * SIZE) + j] += recvbuf[(i * SIZE) + k] * bT[(j * SIZE) + k];
// Gathers variable data from all members of a group to one member.
MPI_Gatherv( sendbuf, // Pointer to a buffer that contains the data to be sent to the root process
sendcount, // Number of elements in the send buffer
M_MPI_TYPE, // MPI data type of each element in the buffer
c, // Pointer to a buffer on the root process that contains the data that is received from each process, including data that is sent by the root process
counts, // Number of elements that is received from each process
displacements, // Location, relative to the recvbuf parameter, of the data from each communicator process
M_MPI_TYPE, // MPI data type of each element in buffer
0, MPI_COMM_WORLD);
// MPI_Barrier( MPI_COMM_WORLD);
// Get final time
*end = MPI_Wtime();
}
// Monodim. + change the order of the three loops from i-j-k to k-i-j.
void mmult_kij(M_TYPE a[], M_TYPE b[], M_TYPE c[], int numprocs, int procid, double *start, double *end) {
size_t i, j, k;
// Get initial time
*start = MPI_Wtime();
// Broadcasts data from one member of a group to all members of the group.
MPI_Bcast( b, // Pointer to the data buffer
SIZE * SIZE, // Number of data elements in the buffer
M_MPI_TYPE, // MPI data type of the elements in the send buffer
0, MPI_COMM_WORLD);
// Prepare the displacements and counts matrices. They are the same for both MPI_Scatterv and MPI_Gatherv.
// The idea is to split the rows of matrix A equally among all processors.
// The entire row goes to the assigned process, so the rows don't get fragmented.
int counts[numprocs], displacements[numprocs];
int displacement = 0;
for (i = 0; i < numprocs; i++) {
counts[i] = ((i < (SIZE % numprocs)) ? ((SIZE / numprocs) + 1) : (SIZE / numprocs)) * SIZE;
displacements[i] = displacement;
displacement += counts[i];
}
// Reception buffer declaration.
// For each process, this contains only the rows of A that have been assigned to that process.
int recvcount = ((procid < (SIZE % numprocs)) ? ((SIZE / numprocs) + 1) : (SIZE / numprocs)) * SIZE;
M_TYPE *recvbuf = malloc(recvcount * sizeof(M_TYPE));
// Scatters data from one member across all members of a group.
MPI_Scatterv( a, // Pointer to a buffer that contains the data to be sent by the root process
counts, // Number of elements to send to each process
displacements, // Locations of the data to send to each communicator process
M_MPI_TYPE, // MPI data type of each element in the buffer
recvbuf, // Pointer to a buffer that contains the data that is received on each process
recvcount, // Number of elements in the receive buffer
M_MPI_TYPE, // MPI data type of the elements in the receive buffer
0, MPI_COMM_WORLD);
// MPI_Barrier( MPI_COMM_WORLD);
// Send buffer declaration.
// For each process, the rows of C corresponding to the rows of A assigned to that process
// will be calculated in this buffer.
int sendcount = ((procid < (SIZE % numprocs)) ? ((SIZE / numprocs) + 1) : (SIZE / numprocs)) * SIZE;
M_TYPE *sendbuf = malloc(sendcount * sizeof(M_TYPE));
for (k = 0; k < SIZE; k++)
for (i = 0; i < sendcount / SIZE; i++)
for (j = 0; j < SIZE; j++)
sendbuf[(i * SIZE) + j] += recvbuf[(i * SIZE) + k] * b[(k * SIZE) + j];
// Gathers variable data from all members of a group to one member.
MPI_Gatherv( sendbuf, // Pointer to a buffer that contains the data to be sent to the root process
sendcount, // Number of elements in the send buffer
M_MPI_TYPE, // MPI data type of each element in the buffer
c, // Pointer to a buffer on the root process that contains the data that is received from each process, including data that is sent by the root process
counts, // Number of elements that is received from each process
displacements, // Location, relative to the recvbuf parameter, of the data from each communicator process
M_MPI_TYPE, // MPI data type of each element in buffer
0, MPI_COMM_WORLD);
// MPI_Barrier( MPI_COMM_WORLD);
// Get final time
*end = MPI_Wtime();
}
int main(int argc, char **argv) {
// Check that the arguments are at least 3: the executable file and the size of the matrix.
// If an additional character is entered, the print function is enabled.
if (argc < 3) {
printf("\n USAGE:\n\t %s SIZE <0 | 1 | 2> [any character for enable verbose mode]\n", argv[0]);
printf("\t 0 --> Use monodimensional arrays instead of matrices.\n");
printf("\t 1 --> (0) + transpose the matrix B to access both A and B row-wise (less cache misses).\n");
printf("\t 2 --> (0) + change the order of the three loops from i-j-k to k-i-j.\n");
exit(1);
}
int numprocs, procid;
double start, end;
SIZE = (size_t)atoi(argv[1]);
M_TYPE *a = malloc(SIZE * SIZE * sizeof(M_TYPE)),
*b = malloc(SIZE * SIZE * sizeof(M_TYPE)),
*c = malloc(SIZE * SIZE * sizeof(M_TYPE));
M_TYPE *bT = malloc(SIZE * SIZE * sizeof(M_TYPE));
// Random initialization
srand((unsigned int)time(NULL));
// Initialize the MPI environment
MPI_Init(&argc, &argv);
// Get the number of processes
MPI_Comm_size(MPI_COMM_WORLD, &numprocs);
// Get the rank of the process
MPI_Comm_rank(MPI_COMM_WORLD, &procid);
// Process 0 fills the input matrices and broadcasts them to the rest
// (actually, only the relevant stripe of A is sent to each process)
if (procid == 0) {
fill_random_matrix(a);
fill_random_matrix(b);
matrix_transpose(b, bT);
}
switch (atoi(argv[2])) {
case 0:
mmult_mono(a, b, c, numprocs, procid, &start, &end);
break;
case 1:
mmult_transp(a, bT, c, numprocs, procid, &start, &end);
break;
case 2:
mmult_kij(a, b, c, numprocs, procid, &start, &end);
break;
default:
printf("\n USAGE:\n\t %s SIZE <0 | 1 | 2> [any character for enable verbose mode]\n", argv[0]);
printf("\t 0 --> Use monodimensional arrays instead of matrices.\n");
printf("\t 1 --> (0) + transpose the matrix B to access both A and B row-wise (less cache misses).\n");
printf("\t 2 --> (0) + change the order of the three loops from i-j-k to k-i-j.\n");
exit(1);
break;
}
if (procid == 0) {
// If "print mode" is enabled, print the two matrices to be added and the solution matrix
if (argc > 3) {
printf("\n\n");
print_matrix(a);
printf("\n\n\t * \n");
print_matrix(b);
printf("\n\n\t = \n");
print_matrix(c);
printf("\n\n");
}
printf("\n\n");
printf("Execution time: %1.5f milliseconds.\n", (end - start) * 1000);
}
// Finalize the MPI environment
MPI_Finalize();
return 0;
}