-
Notifications
You must be signed in to change notification settings - Fork 0
/
Matrix Multiplication.c
218 lines (202 loc) · 5.93 KB
/
Matrix Multiplication.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
/*
Parallel Programming
By : Abdallah Okasha
FCI-CU
*/
#include <stdio.h>
#include <string.h>
#include "mpi.h"
int main(int argc, char * argv[]) {
int my_rank; /* rank of process */
int p; /* number of process */
int dest; /* rank of reciever */
int tag = 0; /* tag for messages */
MPI_Status status; /* return status for */
/* Start up MPI */
int r1, c1, r2, c2, i, j, k, x, y, index, size;
MPI_Init(&argc, &argv);
/* Find out process rank */
MPI_Comm_rank(MPI_COMM_WORLD, &my_rank);
/* Find out number of process */
MPI_Comm_size(MPI_COMM_WORLD, &p);
if (my_rank == 0) {
/* Read input */
printf("Please enter the dimensions of the first matrix\n");
scanf("%d %d", &r1, &c1);
printf("Please enter the dimensions of the second matrix\n");
scanf("%d %d", &r2, &c2);
/* Declare Matrix-1 */
int** mat_1 = (int **) malloc(r1 * sizeof(int *));
for (i = 0; i < r1; i++)
mat_1[i] = (int*) malloc(c1 * sizeof(int));
/* Declare matrix-2 */
int** mat_2 = (int **) malloc(r2 * sizeof(int *));
for (i = 0; i < r2; i++)
mat_2[i] = (int *) malloc(c2 * sizeof(int));
/* read first matrix */
printf("Enter ur first matrix:\n");
for (i = 0; i < r1; i++) {
for (j = 0; j < c1; j++) {
scanf("%d", &mat_1[i][j]);
}
}
/* read first matrix */
printf("Enter ur second matrix:\n");
for (i = 0; i < r2; i++) {
for (j = 0; j < c2; j++) {
scanf("%d", &mat_2[i][j]);
}
}
printf("Input : \n");
printf("The first matrix \n");
for (i = 0; i < r1; i++) {
for (j = 0; j < c1; j++)
printf("%d ", mat_1[i][j]);
printf("\n");
}
printf("The second matrix \n");
for (i = 0; i < r2; i++) {
for (j = 0; j < c2; j++)
printf("%d ", mat_2[i][j]);
printf("\n");
}
/* initialize size */
index = 0;
size = r1 / (p - 1);
/* check condition of multiplication*/
if (c1 != r2)
printf("these matrices can't be multiplied!\n");
/* Sending... */
for (dest = 1; dest < p; dest++) {
if (dest == p - 1) {
/* last process takes the rest of the matrix rows */
size = size + (r1 % (p - 1));
}
/* send first matrix (sub) */
x = size, y = c1;
/* send the number of rows */
MPI_Send(&x, 1, MPI_INT, dest, tag, MPI_COMM_WORLD);
/*send the size of each row */
MPI_Send(&y, 1, MPI_INT, dest, tag, MPI_COMM_WORLD);
printf("size : ");
printf("%d", size);
printf("\n");
int *row = (int *) malloc(c1 * sizeof(int));
printf("Send rows :\n");
for (i = index; i < index + size; i++) {
for (j = 0; j < c1; j++) {
row[j] = mat_1[i][j];
printf("%d ", row[j]);
}
printf("\n");
/* send row by row of first matrix (sub) */
MPI_Send(row, c1, MPI_INT, dest, tag, MPI_COMM_WORLD);
}
/* send the second matrix */
/* send the number of rows */
MPI_Send(&r2, 1, MPI_INT, dest, tag, MPI_COMM_WORLD);
/*send the size of each row */
MPI_Send(&c2, 1, MPI_INT, dest, tag, MPI_COMM_WORLD);
/* update row size */
row = (int *) malloc(c2 * sizeof(int));
for (i = 0; i < r2; i++) {
for (j = 0; j < c2; j++) {
row[j] = mat_2[i][j];
}
/* send row by row of second matrix */
MPI_Send(row, c2, MPI_INT, dest, tag, MPI_COMM_WORLD);
}
index += size;
}
/* initialize size */
index = 0;
size = r1 / (p - 1);
/* Receiving... */
printf("=====================================\n");
printf("Result Matrix:\n");
for (dest = 1; dest < p; dest++) {
if (dest == p - 1) {
/* last process takes the rest of the matrix rows */
size = size + (r1 % (p - 1));
}
for (i = index; i < index + size; i++) {
int *row = (int *) malloc(c2 * sizeof(int));
MPI_Recv(row, c2, MPI_INT, dest, tag, MPI_COMM_WORLD, &status);
for (j = 0; j < c2; j++)
printf("%d ", row[j]);
printf("\n");
}
index += size;
}
} else if (my_rank != 0) {
/* Receive number of row of first matrix */
MPI_Recv(&x, 1, MPI_INT, 0, tag, MPI_COMM_WORLD, &status);
/* Receive size of each row in first matrix */
MPI_Recv(&y, 1, MPI_INT, 0, tag, MPI_COMM_WORLD, &status);
int** m1 = (int **) malloc(x * sizeof(int *));
/* Receiving the first matrix */
for (i = 0; i < x; i++)
m1[i] = (int*) malloc(y * sizeof(int));
//printf("Received rows:\n");
for (i = 0; i < x; i++) {
int *row = (int *) malloc(y * sizeof(int));
MPI_Recv(row, y, MPI_INT, 0, tag, MPI_COMM_WORLD, &status);
m1[i] = row;
//for (j = 0; j < y; j++)
//printf("%d ", row[j]);
//printf("\n");
}
/* Receive number of row of second matrix */
MPI_Recv(&r2, 1, MPI_INT, 0, tag, MPI_COMM_WORLD, &status);
/* Receive size of each row in second matrix */
MPI_Recv(&c2, 1, MPI_INT, 0, tag, MPI_COMM_WORLD, &status);
int** m2 = (int **) malloc(r2 * sizeof(int *));
/* Receiving the Second matrix */
for (i = 0; i < r2; i++)
m2[i] = (int*) malloc(c2 * sizeof(int));
for (i = 0; i < r2; i++) {
int *row = (int *) malloc(c2 * sizeof(int));
MPI_Recv(row, c2, MPI_INT, 0, tag, MPI_COMM_WORLD, &status);
m2[i] = row;
}
/* Multiplication */
int** m3 = (int **) malloc(x * sizeof(int *));
/* Receiving the Second matrix */
for (i = 0; i < x; i++)
m3[i] = (int*) malloc(c2 * sizeof(int));
/*
printf("\n");
printf("m1:\n");
for (i = 0; i < x; i++) {
for (j = 0; j < y; j++)
printf("%d ", m1[i][j]);
printf("\n");
}
printf("m2:\n");
for (i = 0; i < r2; i++) {
for (j = 0; j < c2; j++)
printf("%d ", m2[i][j]);
printf("\n");
}
*/
for (i = 0; i < x; i++) {
for (j = 0; j < c2; j++) {
m3[i][j] = 0;
for (k = 0; k < r2; k++)
m3[i][j] += (m1[i][k] * m2[k][j]);
//printf("%d ", m3[i][j]);
}
}
/* sending the result matrix */
int *row = (int *) malloc(c2 * sizeof(int));
for (i = 0; i < x; i++) {
row = m3[i];
//for (j = 0 ; j < c2; j++)
//printf("%d ", row[j]);
MPI_Send(row, c2, MPI_INT, 0, tag, MPI_COMM_WORLD);
}
}
/* shutdown MPI */
MPI_Finalize();
return 0;
}