-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmessagePassing.c
More file actions
214 lines (176 loc) · 6.82 KB
/
messagePassing.c
File metadata and controls
214 lines (176 loc) · 6.82 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
#include <mpi.h>
#include <stdlib.h>
#include <time.h>
#include "matrix.h"
//#include "messagePassing.h"
#include "In_Out.h"
#define PATH_TO_TESTS "matrices.txt"
#define PATH_TO_RESULTS "results.txt"
#define PATH_TO_TIMES "performance.txt"
#define MAX_TEST_CASES 10
matrix firsts[MAX_TEST_CASES];
matrix seconds[MAX_TEST_CASES];
matrix results[MAX_TEST_CASES];
int rank; //rank of current task
int num; //number of tasks
int low, up; //boundaries of portions to compute by each task
int part;
MPI_Status status; // store statusus of MPI_Recv
MPI_Request req; //capture request of MPI_Isend
struct timeval start, end;
int main(int argc, char* argv[])
{
MPI_Init(&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Comm_size(MPI_COMM_WORLD, &num);
if(num == 1)
{
printf("MessagePassing with one job only doesn't make sense.\n");
exit(0);
}
if(rank == 0) //initial work by master
{
printf("Reading matrices from InputFile...\n");
for (int i = 0; i < MAX_TEST_CASES; ++i)
{
parseMatrices(PATH_TO_TESTS, i, &firsts[i], &seconds[i]);
}
printf("Calculating testcases...\n");
gettimeofday(&start, NULL);
}
for (int cur = 0; cur < MAX_TEST_CASES; ++cur)
{
if(rank == 0)
{
if (!matchDimensions(&firsts[cur], &seconds[cur]
, &results[cur])) exit(-1);
part = firsts[cur].rows / (num - 1); //how many rows for each worker?
for (int i = 1; i < num; ++i)
{
//send dimensions of allocated matrices to workers for them to
//allocate
MPI_Isend(&(firsts[cur].rows), 1, MPI_INT, i, 7+cur, MPI_COMM_WORLD, &req);
MPI_Isend(&(firsts[cur].columns), 1, MPI_INT, i, 8+cur, MPI_COMM_WORLD, &req);
MPI_Isend(&(seconds[cur].rows), 1, MPI_INT, i, 9+cur, MPI_COMM_WORLD, &req);
MPI_Isend(&(seconds[cur].columns), 1, MPI_INT, i, 10+cur, MPI_COMM_WORLD, &req);
MPI_Isend(&(results[cur].rows), 1, MPI_INT, i, 11+cur, MPI_COMM_WORLD, &req);
MPI_Isend(&(results[cur].columns), 1, MPI_INT, i, 12+cur, MPI_COMM_WORLD, &req);
low = part * (i - 1);
//if A's rows are not dividable by the number of workers, last worker
//gets all remaining rows
if (i == num - 1 && firsts[cur].rows % (num - 1) != 0) up = firsts[cur].rows;
else up = low + part;
// printf("Master Sending...\n");
//non-blocking sends of boundaries to current worker
MPI_Isend(&low, 1, MPI_INT, i, 1+cur, MPI_COMM_WORLD, &req);
MPI_Isend(&up, 1, MPI_INT, i, 2+cur, MPI_COMM_WORLD, &req);
//non-blocking send of portion of matrix A to current worker
MPI_Isend(&(firsts[cur].values[low * firsts[cur].columns]), (up - low)
* firsts[cur].columns, MPI_DOUBLE, i, 3+cur, MPI_COMM_WORLD, &req);
}
// printf("Master done Sending.\n");
}
if(rank > 0)
{
MPI_Recv(&(firsts[cur].rows), 1, MPI_INT, 0, 7+cur, MPI_COMM_WORLD, &status);
MPI_Recv(&(firsts[cur].columns), 1, MPI_INT, 0, 8+cur, MPI_COMM_WORLD, &status);
MPI_Recv(&(seconds[cur].rows), 1, MPI_INT, 0, 9+cur, MPI_COMM_WORLD, &status);
MPI_Recv(&(seconds[cur].columns), 1, MPI_INT, 0, 10+cur, MPI_COMM_WORLD, &status);
MPI_Recv(&(results[cur].rows), 1, MPI_INT, 0, 11+cur, MPI_COMM_WORLD, &status);
MPI_Recv(&(results[cur].columns), 1, MPI_INT, 0, 12+cur, MPI_COMM_WORLD, &status);
//allocate matrices in worker's memory
firsts[cur].values = alloc_matrix(firsts[cur].rows, firsts[cur].columns);
seconds[cur].values = alloc_matrix(seconds[cur].rows, seconds[cur].columns);
//result matrix gets calloced, because its elements need to be 0 at start
results[cur].values = calloc(sizeof(double),
results[cur].rows * results[cur].columns);
}
MPI_Bcast(&(seconds[cur].values[0]), seconds[cur].rows * seconds[cur].columns
, MPI_DOUBLE, 0, MPI_COMM_WORLD);
if (rank > 0) //worker tasks
{
//recieving boundaries
// printf("Slave recieving...\n");
MPI_Recv(&low, 1, MPI_INT, 0, 1+cur, MPI_COMM_WORLD, &status);
MPI_Recv(&up, 1, MPI_INT, 0, 2+cur, MPI_COMM_WORLD, &status);
//recieving portion of A
MPI_Recv(&(firsts[cur].values[low * firsts[cur].columns])
, (up - low) * firsts[cur].columns, MPI_DOUBLE
, 0, 3+cur, MPI_COMM_WORLD, &status);
// printf("Slave done recieving.\n");
for (int i = low; i < up; ++i)
{
for (int j = 0; j < seconds[cur].columns; ++j)
{
for (int k = 0; k < seconds[cur].rows; ++k)
{
results[cur].values[i * results[cur].columns + j] +=
firsts[cur].values[i * firsts[cur].columns + k]
* seconds[cur].values[k * seconds[cur].columns + j];
}
}
}
// printf("Slave sending...\n");
MPI_Isend(&low, 1, MPI_INT, 0, 4+cur, MPI_COMM_WORLD, &req);
MPI_Isend(&up, 1, MPI_INT, 0, 5+cur, MPI_COMM_WORLD, &req);
MPI_Isend(&(results[cur].values[low * results[cur].columns])
, (up - low) * results[cur].columns
, MPI_DOUBLE, 0, 6+cur, MPI_COMM_WORLD, &req);
// printf("Slave done sending.\n");
}
if (rank == 0)
{
// printf("Master recieving...\n");
for (int i = 1; i < num; ++i)
{
MPI_Recv(&low, 1, MPI_INT, i, 4+cur, MPI_COMM_WORLD, &status);
MPI_Recv(&up, 1, MPI_INT, i, 5+cur, MPI_COMM_WORLD, &status);
MPI_Recv(&(results[cur].values[low * results[cur].columns])
, (up - low) * seconds[cur].columns
, MPI_DOUBLE, i, 6+cur, MPI_COMM_WORLD, &status);
}
// printf("Master done recieving.\n");
}
}
if (rank == 0) //end work by master
{
gettimeofday(&end, NULL);
matrix originalResults[MAX_TEST_CASES];
printf("Reading results from sequential Implementation for comparision...\n");
for (int i = 0; i < MAX_TEST_CASES; ++i)
{
parseMatrices(PATH_TO_RESULTS, i, &originalResults[i], NULL);
}
for (int i = 0; i < MAX_TEST_CASES; ++i)
{
if(!compareMatrices(&results[i], &originalResults[i]))
{
printf("MessagePassing-Implementation has faults!");
MPI_Finalize();
exit(0);
}
}
FILE* performance;
performance = fopen(PATH_TO_TIMES, "a");
fprintf(performance,"MessagePassing-Implementation took %lf seconds for all testcases.\n"
, getDifference(start, end));
fclose(performance);
for (int i = 0; i < MAX_TEST_CASES; ++i)
{
free(firsts[i].values);
free(seconds[i].values);
free(results[i].values);
free(originalResults[i].values);
}
}
// gettimeofday(&end, NULL);
// printMatrix(&result, "");
// printf("MPI-Implementation took %.3lf milliseconds.\n", (end.tv_sec
// - start.tv_sec + 1E-6 * (end.tv_usec - start.tv_usec)*1000));
// free(A.values);
// free(B.values);
// free(result.values);
// }
MPI_Finalize();
exit(0);
}