/
mpi_mat_vect_time.c
499 lines (461 loc) · 17.6 KB
/
mpi_mat_vect_time.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
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
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
/* File: mpi_mat_vect_time.c
*
* Purpose: Implement parallel matrix-vector multiplication using
* one-dimensional arrays to store the vectors and the
* matrix. Vectors use block distributions and the
* matrix is distributed by block rows. This version
* generates a random matrix A and a random vector x.
* It prints out the run-time.
*
* Compile: mpicc -g -Wall -o mpi_mat_vect_time mpi_mat_vect_time.c
* Run: mpiexec -n <number of processes> ./mpi_mat_vect_time
*
* Input: Dimensions of the matrix (m = number of rows, n
* = number of columns)
* Output: Elapsed time for execution of the multiplication
*
* Notes:
* 1. Number of processes should evenly divide both m and n
* 2. Define DEBUG for verbose output, including the product
* vector y
*
* IPP: Section 3.6.2 (pp. 122 and ff.)
*/
#include <stdio.h>
#include <stdlib.h>
#include <mpi.h>
void Check_for_error(int local_ok, char fname[], char message[],
MPI_Comm comm);
void Get_dims(int* m_p, int* local_m_p, int* n_p, int* local_n_p,
int my_rank, int comm_sz, MPI_Comm comm);
void Allocate_arrays(double** local_A_pp, double** local_x_pp,
double** local_y_pp, int local_m, int n, int local_n,
MPI_Comm comm);
void Read_matrix(char prompt[], double local_A[], int m, int local_m,
int n, int my_rank, MPI_Comm comm);
void Read_vector(char prompt[], double local_vec[], int n, int local_n,
int my_rank, MPI_Comm comm);
void Generate_matrix(double local_A[], int local_m, int n);
void Generate_vector(double local_x[], int local_n);
void Print_matrix(char title[], double local_A[], int m, int local_m,
int n, int my_rank, MPI_Comm comm);
void Print_vector(char title[], double local_vec[], int n,
int local_n, int my_rank, MPI_Comm comm);
void Mat_vect_mult(double local_A[], double local_x[],
double local_y[], int local_m, int n, int local_n,
MPI_Comm comm);
#define DEBUG
/*-------------------------------------------------------------------*/
int main(int argc, char **argv) {
double* local_A;
double* local_B;
double* local_C;
int m, local_m, n, local_n;
int my_rank, comm_sz;
MPI_Comm comm;
double start, finish, loc_elapsed, elapsed;
MPI_Init(&argc, &argv);
comm = MPI_COMM_WORLD;
MPI_Comm_size(comm, &comm_sz);
MPI_Comm_rank(comm, &my_rank);
n=atoi(argv[1]);
m=n;
local_m = m/comm_sz;
local_n = n/comm_sz;
/*Get_dims(&m, &local_m, &n, &local_n, my_rank, comm_sz, comm);*/
Allocate_arrays(&local_A, &local_B, &local_C, local_m, n, local_n, comm);
// Read_matrix("A", local_A, m, local_m, n, my_rank, comm);
srandom(my_rank);
Generate_matrix(local_A, local_m, n);
Generate_matrix(local_B, local_m, n);
# ifdef DEBUG
Print_matrix("A", local_A, m, local_m, n, my_rank, comm);
Print_matrix("B", local_B, m, local_m, n, my_rank, comm);
# endif
// Read_vector("x", local_x, n, local_n, my_rank, comm);
/*Generate_vector(local_x, local_n);*/
# ifdef DEBUG
/*Print_vector("x", local_x, n, local_n, my_rank, comm);*/
# endif
MPI_Barrier(comm);
for(i = 0; i < 100; i++) {
start = MPI_Wtime();
Mat_vect_mult(local_A, local_B, local_C, local_m, n, local_n, comm);
finish = MPI_Wtime();
loc_elapsed = finish-start;
MPI_Reduce(&loc_elapsed, &elapsed, 1, MPI_DOUBLE, MPI_MAX, 0, comm);
if (my_rank == 0)
printf("Elapsed time = %e\n", elapsed);
}
# ifdef DEBUG
Print_matrix("C", local_C, m, local_m, n, my_rank, comm);
# endif
free(local_A);
free(local_B);
free(local_C);
MPI_Finalize();
return 0;
} /* main */
/*-------------------------------------------------------------------
* Function: Check_for_error
* Purpose: Check whether any process has found an error. If so,
* print message and terminate all processes. Otherwise,
* continue execution.
* In args: local_ok: 1 if calling process has found an error, 0
* otherwise
* fname: name of function calling Check_for_error
* message: message to print if there's an error
* comm: communicator containing processes calling
* Check_for_error: should be MPI_COMM_WORLD.
*
* Note:
* The communicator containing the processes calling Check_for_error
* should be MPI_COMM_WORLD.
*/
void Check_for_error(
int local_ok /* in */,
char fname[] /* in */,
char message[] /* in */,
MPI_Comm comm /* in */) {
int ok;
MPI_Allreduce(&local_ok, &ok, 1, MPI_INT, MPI_MIN, comm);
if (ok == 0) {
int my_rank;
MPI_Comm_rank(comm, &my_rank);
if (my_rank == 0) {
fprintf(stderr, "Proc %d > In %s, %s\n", my_rank, fname,
message);
fflush(stderr);
}
MPI_Finalize();
exit(-1);
}
} /* Check_for_error */
/*-------------------------------------------------------------------
* Function: Get_dims
* Purpose: Get the dimensions of the matrix and the vectors from
* stdin.
* In args: my_rank: calling processes rank in comm
* comm_sz: number of processes in comm
* comm: communicator containing all processes calling
* Get_dims
* Out args: m_p: global number of rows of A and components in y
* local_m_p: local number of rows of A and components of y
* n_p: global number of cols of A and components of x
* local_n_p: local number of components of x
*
* Errors: if either m or n isn't positive or if m or n isn't evenly
* divisible by comm_sz, the program prints an error message
* and quits.
* Note:
* All processes in comm should call Get_dims
*/
void Get_dims(
int* m_p /* out */,
int* local_m_p /* out */,
int* n_p /* out */,
int* local_n_p /* out */,
int my_rank /* in */,
int comm_sz /* in */,
MPI_Comm comm /* in */) {
int local_ok = 1;
if (my_rank == 0) {
printf("Enter the number of rows\n");
scanf("%d", m_p);
printf("Enter the number of columns\n");
scanf("%d", n_p);
}
MPI_Bcast(m_p, 1, MPI_INT, 0, comm);
MPI_Bcast(n_p, 1, MPI_INT, 0, comm);
if (*m_p <= 0 || *n_p <= 0 || *m_p % comm_sz != 0
|| *n_p % comm_sz != 0) local_ok = 0;
Check_for_error(local_ok, "Get_dims",
"m and n must be positive and evenly divisible by comm_sz",
comm);
*local_m_p = *m_p/comm_sz;
*local_n_p = *n_p/comm_sz;
} /* Get_dims */
/*-------------------------------------------------------------------
* Function: Allocate_arrays
* Purpose: Allocate storage for local parts of A, x, and y
* In args: local_m: local number of rows of A and components of y
* n: global and local number of cols of A and global
* number of components of x
* local_n: local number of components of x
* comm: communicator containing all calling processes
* Out args: local_A_pp: local storage for matrix (m/comm_sz rows, n cols)
* local_x_pp: local storage for x (n/comm_sz components)
* local_y_pp: local_storage for y (m/comm_sz components)
*
* Errors: if a malloc fails, the program prints a message and all
* processes quit
* Note:
* Communicator should be MPI_COMM_WORLD because of call to
* Check_for_errors
*/
void Allocate_arrays(
double** local_A_pp /* out */,
double** local_B_pp /* out */,
double** local_C_pp /* out */,
int local_m /* in */,
int n /* in */,
int local_n /* in */,
MPI_Comm comm /* in */) {
int local_ok = 1;
*local_A_pp = malloc(local_m*n*sizeof(double));
*local_B_pp = malloc(local_m*n*sizeof(double));
*local_C_pp = malloc(local_m*n*sizeof(double));
if (*local_A_pp == NULL || local_B_pp == NULL ||
local_C_pp == NULL) local_ok = 0;
Check_for_error(local_ok, "Allocate_arrays",
"Can't allocate local arrays", comm);
} /* Allocate_arrays */
/*-------------------------------------------------------------------
* Function: Read_matrix
* Purpose: Read in the matrix and distribute among the processes
* using a block row distribution
* In args: prompt: description of matrix (e.g., "A")
* m: global number of rows of A
* local_m: local number of rows of A
* n: global and local number of cols of A
* my_rank: process rank in communicator comm
* comm: communicator containing processes calling
* Read_matrix
* Out args: local_A: the local matrix
*
* Errors: if malloc of temporary storage fails on process 0, the
* program prints a message and all processes quit
* Note:
* 1. Communicator should be MPI_COMM_WORLD because of call to
* Check_for_errors
* 2. local_m and n should be the same on each process
*/
void Read_matrix(
char prompt[] /* in */,
double local_A[] /* out */,
int m /* in */,
int local_m /* in */,
int n /* in */,
int my_rank /* in */,
MPI_Comm comm /* in */) {
double* A = NULL;
int local_ok = 1;
int i, j;
if (my_rank == 0) {
A = malloc(m*n*sizeof(double));
if (A == NULL) local_ok = 0;
Check_for_error(local_ok, "Read_matrix",
"Can't allocate temporary matrix", comm);
printf("Enter the matrix %s\n", prompt);
for (i = 0; i < m; i++)
for (j = 0; j < n; j++)
scanf("%lf", &A[i*n+j]);
MPI_Scatter(A, local_m*n, MPI_DOUBLE,
local_A, local_m*n, MPI_DOUBLE, 0, comm);
free(A);
} else {
Check_for_error(local_ok, "Read_matrix",
"Can't allocate temporary matrix", comm);
MPI_Scatter(A, local_m*n, MPI_DOUBLE,
local_A, local_m*n, MPI_DOUBLE, 0, comm);
}
} /* Read_matrix */
/*-------------------------------------------------------------------
* Function: Read_vector
* Purpose: Read a vector from stdin and distribute among the
* processes using a block distribution
* In args: prompt: description of vector (e.g., "x")
* n: global order of vector
* local_n: local order of vector (n/comm_sz)
*
* Errors: if malloc of temporary storage fails on process 0, the
* program prints a message and all processes quit
* Notes:
* 1. Communicator should be MPI_COMM_WORLD because of call to
* Check_for_errors
* 2. local_n should be the same on all processes
*/
void Read_vector(
char prompt[] /* in */,
double local_vec[] /* out */,
int n /* in */,
int local_n /* in */,
int my_rank /* in */,
MPI_Comm comm /* in */) {
double* vec = NULL;
int i, local_ok = 1;
if (my_rank == 0) {
vec = malloc(n*sizeof(double));
if (vec == NULL) local_ok = 0;
Check_for_error(local_ok, "Read_vector",
"Can't allocate temporary vector", comm);
printf("Enter the vector %s\n", prompt);
for (i = 0; i < n; i++)
scanf("%lf", &vec[i]);
MPI_Scatter(vec, local_n, MPI_DOUBLE,
local_vec, local_n, MPI_DOUBLE, 0, comm);
free(vec);
} else {
Check_for_error(local_ok, "Read_vector",
"Can't allocate temporary vector", comm);
MPI_Scatter(vec, local_n, MPI_DOUBLE,
local_vec, local_n, MPI_DOUBLE, 0, comm);
}
} /* Read_vector */
/*-------------------------------------------------------------------*/
void Generate_matrix(
double local_A[] /* out */,
int local_m /* in */,
int n /* in */) {
int i, j;
for (i = 0; i < local_m; i++)
for (j = 0; j < n; j++)
local_A[i*n + j] = ((double) random())/((double) RAND_MAX);
} /* Generate_matrix */
/*-------------------------------------------------------------------*/
void Generate_vector(
double local_x[] /* out */,
int local_n /* in */) {
int i;
for (i = 0; i < local_n; i++)
local_x[i] = ((double) random())/((double) RAND_MAX);
} /* Generate_vector */
/*-------------------------------------------------------------------
* Function: Print_matrix
* Purpose: Print a matrix distributed by block rows to stdout
* In args: title: name of matrix
* local_A: calling process' part of matrix
* m: global number of rows
* local_m: local number of rows (m/comm_sz)
* n: global (and local) number of cols
* my_rank: calling process' rank in comm
* comm: communicator containing all processes
* Errors: if malloc of local storage on process 0 fails, all
* processes quit.
* Notes:
* 1. comm should be MPI_COMM_WORLD because of call to Check_for_errors
* 2. local_m should be the same on all the processes
*/
void Print_matrix(
char title[] /* in */,
double local_A[] /* in */,
int m /* in */,
int local_m /* in */,
int n /* in */,
int my_rank /* in */,
MPI_Comm comm /* in */) {
double* A = NULL;
int i, j, local_ok = 1;
if (my_rank == 0) {
A = malloc(m*n*sizeof(double));
if (A == NULL) local_ok = 0;
Check_for_error(local_ok, "Print_matrix",
"Can't allocate temporary matrix", comm);
MPI_Gather(local_A, local_m*n, MPI_DOUBLE,
A, local_m*n, MPI_DOUBLE, 0, comm);
printf("\nThe matrix %s\n", title);
for (i = 0; i < m; i++) {
for (j = 0; j < n; j++)
printf("%f ", A[i*n+j]);
printf("\n");
}
printf("\n");
free(A);
} else {
Check_for_error(local_ok, "Print_matrix",
"Can't allocate temporary matrix", comm);
MPI_Gather(local_A, local_m*n, MPI_DOUBLE,
A, local_m*n, MPI_DOUBLE, 0, comm);
}
} /* Print_matrix */
/*-------------------------------------------------------------------
* Function: Print_vector
* Purpose: Print a vector with a block distribution
* In args: title: name of vector
* local_vec: calling process' part of vector
* n: global number of components
* local_n: local number of components (n/comm_sz)
* my_rank: calling process' rank in comm
* comm: communicator containing all processes
* Errors: if malloc of local storage on process 0 fails, all
* processes quit.
* Notes:
* 1. comm should be MPI_COMM_WORLD because of call to Check_for_errors
* 2. local_n should be the same on all the processes
*/
void Print_vector(
char title[] /* in */,
double local_vec[] /* in */,
int n /* in */,
int local_n /* in */,
int my_rank /* in */,
MPI_Comm comm /* in */) {
double* vec = NULL;
int i, local_ok = 1;
if (my_rank == 0) {
vec = malloc(n*sizeof(double));
if (vec == NULL) local_ok = 0;
Check_for_error(local_ok, "Print_vector",
"Can't allocate temporary vector", comm);
MPI_Gather(local_vec, local_n, MPI_DOUBLE,
vec, local_n, MPI_DOUBLE, 0, comm);
printf("\nThe vector %s\n", title);
for (i = 0; i < n; i++)
printf("%f ", vec[i]);
printf("\n");
free(vec);
} else {
Check_for_error(local_ok, "Print_vector",
"Can't allocate temporary vector", comm);
MPI_Gather(local_vec, local_n, MPI_DOUBLE,
vec, local_n, MPI_DOUBLE, 0, comm);
}
} /* Print_vector */
/*-------------------------------------------------------------------
* Function: Mat_vect_mult
* Purpose: Multiply a matrix A by a vector x. The matrix is distributed
* by block rows and the vectors are distributed by blocks
* In args: local_A: calling process' rows of matrix A
* local_x: calling process' components of vector x
* local_m: calling process' number of rows
* n: global (and local) number of columns
* local_n: calling process' number of components of x
* comm: communicator containing all calling processes
* Errors: if malloc of local storage on any process fails, all
* processes quit.
* Notes:
* 1. comm should be MPI_COMM_WORLD because of call to Check_for_errors
* 2. local_m and local_n should be the same on all the processes
*/
void Mat_vect_mult(
double local_A[] /* in */,
double local_B[] /* in */,
double local_C[] /* out */,
int local_m /* in */,
int n /* in */,
int local_n /* in */,
MPI_Comm comm /* in */) {
double* x;
double* col;
int local_i, i, j;
int local_ok = 1;
x = malloc(n*sizeof(double));
col = malloc(local_n*sizeof(double));
if (x == NULL) local_ok = 0;
Check_for_error(local_ok, "Mat_vect_mult",
"Can't allocate temporary vector", comm);
for(i = 0; i < n; i++) {
for(j = 0; j < local_n; j++) {
col[j] = local_B[j*n+i];
}
MPI_Allgather(col, local_n, MPI_DOUBLE,
x, local_n, MPI_DOUBLE, comm);
for (local_i = 0; local_i < local_m; local_i++) {
local_C[local_i*n+i] = 0.0;
for (j = 0; j < n; j++)
local_C[local_i*n+i] += local_A[local_i*n+j]*x[j];
}
}
free(col);
free(x);
} /* Mat_vect_mult */