-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathP2PSolution.cpp
229 lines (184 loc) · 5.14 KB
/
P2PSolution.cpp
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
// Hw1.cpp : Defines the entry point for the console application.
#include <iostream>
#include <fstream>
#include <mpi.h>
#include <chrono>
using namespace std;
using namespace std::chrono;
const int MAX_WIDTH = 720;
const int MAX_HEIGHT = 720;
void VectorMatrixMultiplication(int h, double *i_matrix, double *i_vector, double *o_vector);
void GetMatrixFromFile(int &w, int &h, double *i_matrix);
void GetVectorFromFile(int &h, double *i_vector);
void WriteResultToFile(int h, double *o_vector);
int main(int argc, char **argv)
{
MPI_Init(&argc, &argv);
int m_w = 0, m_h = 0, v_h = 0, comm_sz = 0, my_rank = 0;
double i_matrix[MAX_HEIGHT * MAX_WIDTH];
double i_vector[MAX_WIDTH];
double o_vector[MAX_HEIGHT];
// Apply algorithm.
MPI_Comm_size(MPI_COMM_WORLD, &comm_sz);
MPI_Comm_rank(MPI_COMM_WORLD, &my_rank);
// Processor 0 reads data from file.
if (my_rank == 0)
{
// Get input matrix from file.
GetMatrixFromFile(m_w, m_h, i_matrix);
// Get input vector from file.
GetVectorFromFile(v_h, i_vector);
}
// Else do nothing.
auto start = high_resolution_clock::now();
VectorMatrixMultiplication(m_h, i_matrix, i_vector, o_vector);
auto stop = high_resolution_clock::now();
auto duration = duration_cast<microseconds>(stop - start);
if (my_rank == 0)
{
cout << "Time taken by function: " << duration.count()<< " microseconds" << endl;
}
MPI_Finalize();
return 0;
}
void GetVectorFromFile(int &h, double *i_vector)
{
ifstream infile;
infile.open("BigX.txt");
// First line consists width and height.
int w = 0;/*unused*/
infile >> h >> w;
if (h > MAX_HEIGHT)
{
h = MAX_HEIGHT;
}
// Else do nothing.
int index = 0;
while (!infile.eof() && index < MAX_HEIGHT)
{
infile >> i_vector[index];
index++;
}
// End of the loop.
infile.close();
}
void GetMatrixFromFile(int &w, int &h, double *i_matrix)
{
ifstream infile;
infile.open("BigA.txt");
// First line consists width and height.
infile >> h >> w;
if (h > MAX_HEIGHT)
{
h = MAX_HEIGHT;
}
// Else do nothing.
if (w > MAX_WIDTH)
{
w = MAX_WIDTH;
}
// Else do nothing.
long long index = 0;
while (!infile.eof() && index < (MAX_HEIGHT * MAX_WIDTH))
{
infile >> i_matrix[index];
index++;
}
// End of the loop.
infile.close();
}
void WriteResultToFile(int h, double *o_vector)
{
ofstream oVectorFile;
oVectorFile.open ("p2pResult.txt");
oVectorFile<<h<<" "<<"1\n";
for (int i = 0; i < h; i++)
{
oVectorFile <<o_vector[i]<<"\n";
}
// End of the loop.
oVectorFile.close();
}
void VectorMatrixMultiplication(int h, double *i_matrix, double *i_vector, double *o_vector)
{
int my_rank = 0, procCount = 0;
MPI_Comm_rank (MPI_COMM_WORLD, &my_rank);
MPI_Comm_size (MPI_COMM_WORLD, &procCount);
double localresult[MAX_HEIGHT / procCount];// local output vector
double matrix[MAX_HEIGHT][MAX_WIDTH]; //local matrix
double timer = MPI_Wtime();
MPI_Barrier(MPI_COMM_WORLD);
//Send input vector to other processors.
if (my_rank == 0)
{
for (int i = 1; i < procCount; i++)
{
MPI_Send(i_vector, MAX_WIDTH, MPI_DOUBLE, i, 0, MPI_COMM_WORLD);
}
// End of the loop.
}
else
{
MPI_Recv(i_vector, MAX_WIDTH, MPI_DOUBLE, 0, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
}
// Send input matrix to other processors.
if (my_rank == 0)
{
// processor 0 matrix.
for (int i=0 ; i < MAX_HEIGHT/procCount; i++)
{
for (int j=0;j < MAX_WIDTH;j++)
{
matrix[i][j] = i_matrix[j + (MAX_WIDTH * i) ];
}
// End of the loop.
}
// End of the loop.
for (int i = 1; i < procCount; i++)
{
MPI_Send(i_matrix + ( (MAX_WIDTH * MAX_HEIGHT/procCount) * (i) ), (MAX_WIDTH * MAX_HEIGHT)/procCount, MPI_DOUBLE, i, 0, MPI_COMM_WORLD);
}
// End of the loop.
}
// Receive matrix by other processors.
else
{
MPI_Recv(matrix, (MAX_WIDTH * MAX_HEIGHT/procCount), MPI_DOUBLE, 0, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
}
for (int i = 0; i < (MAX_HEIGHT/procCount); i++)
{
for (int j = 0;j < MAX_WIDTH; j++)
{
localresult[i] += i_vector[j] * matrix[i][j];
}
// End of the loop.
}
// End of the loop.
// Gather result.
if (my_rank == 0)
{
// Processor 0 result data.
for (int i = 0; i < MAX_HEIGHT/procCount; i++)
{
o_vector[i] = localresult[i];
}
// End of the loop.
// Other processor result data.
for (int i = 1; i < procCount; i++)
{
MPI_Recv(o_vector + ( (MAX_HEIGHT/procCount) * (i) ),(MAX_HEIGHT)/procCount, MPI_DOUBLE, i, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
}
// End of the loop.
}
else
{
MPI_Send(localresult, MAX_HEIGHT/procCount, MPI_DOUBLE, 0, 0, MPI_COMM_WORLD);
}
timer = MPI_Wtime()-timer;
if (my_rank==0)
{
WriteResultToFile(h, o_vector);
cout << "Time Needed for all ops = "<<timer<<endl;
}
// Else do nothing.s
}