-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path1drow.c
More file actions
101 lines (80 loc) · 2.56 KB
/
1drow.c
File metadata and controls
101 lines (80 loc) · 2.56 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
/*
* 1drow.c
*
* MPI code for performing parallel dense matrix-vector multiplication using 1D
* rowwise partitioning.
*
* Written by cetinsamet -*- cetin.samet@metu.edu.tr
* April, 2019
*
*/
#include <mpi.h>
#include <stdio.h>
#include <stdlib.h>
#include <cblas.h>
int main(int argc, char *argv[]) {
int i, j;
int rank, size;
double minStart, maxEnd;
MPI_Init(&argc, &argv);
MPI_Comm_size(MPI_COMM_WORLD, &size);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
int N = atoi(argv[1]);
int pSize = N / size;
/* Initialize result vector */
double* RESULT;
RESULT = (double*) malloc(N * sizeof(double));
/* Initialize matrix */
double **matrix = (double **) malloc(N * sizeof(double*));
for(i = 0; i < N; i++)
matrix[i] = (double *) malloc(N * sizeof(double));
for (i = 0; i < N; ++i) {
for (j = 0; j < N; ++j)
matrix[i][j] = i + j;
}
/* Initialize vector */
double* vector;
vector = (double*) malloc(N * sizeof(double));
for (i = 0; i < N; ++i)
vector[i] = i;
/* Initialize local matrix */
double **p_matrix = (double **) malloc(pSize * sizeof(double*));
for(i = 0; i < pSize; i++)
p_matrix[i] = (double *) malloc(N * sizeof(double));
for (i = 0; i < pSize; ++i) {
for (j = 0; j < N; ++j)
p_matrix[i][j] = matrix[(rank * pSize) + i][j];
}
/* Initialize local vector */
double* p_vector;
p_vector = (double*) malloc(N * sizeof(double));
for (i = 0; i < pSize; ++i)
p_vector[(rank * pSize) + i] = vector[(rank * pSize) + i];
double pStart = MPI_Wtime(); // <-- Start Time
/* Broadcast all vector partitions to all other processes */
for (i = 0; i < size; ++i) {
for (j = 0; j < pSize; ++j)
MPI_Bcast(&p_vector[(i * pSize) + j], 1, MPI_DOUBLE, i, MPI_COMM_WORLD);
}
/* Calculate each independent dot product */
for (i = 0; i < pSize; ++i)
RESULT[(rank * pSize) + i] = cblas_ddot(N, p_matrix[i], 1, p_vector, 1);
/* Broadcast all result partitions to all other processes */
for (i = 0; i < size; ++i) {
for (j = 0; j < pSize; ++j)
MPI_Bcast(&RESULT[(i * pSize) + j], 1, MPI_DOUBLE, i, MPI_COMM_WORLD);
}
double pEnd = MPI_Wtime(); // <-- Stop Measuring Time
MPI_Reduce(&pStart, &minStart, 1, MPI_DOUBLE, MPI_MIN, 0, MPI_COMM_WORLD);
MPI_Reduce(&pEnd, &maxEnd, 1, MPI_DOUBLE, MPI_MAX, 0, MPI_COMM_WORLD);
/* Print results */
if (rank == 0) {
printf("RESULT of MATRIX-VECTOR MULTIPLICATION:\n");
for (i = 0; i < N; ++i)
printf("%f ", RESULT[i]);
printf("\n");
printf("Vector size: %d\tElapsed time: %f\n", N, maxEnd - minStart);
}
MPI_Finalize();
return 0;
}