-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmm.cpp
More file actions
244 lines (211 loc) · 6.3 KB
/
Copy pathmm.cpp
File metadata and controls
244 lines (211 loc) · 6.3 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
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
#include <vector>
#include <mpi.h>
#include <cmath>
#include <fstream>
#include <sstream>
#include <iostream>
#include <time.h>
#define MAT1_ROW 0
#define MAT1_COL 1
#define MAT2_ROW 2
#define MAT2_COL 3
#define COLUMN 4
#define ROW 5
#define FINAL 6
#define TEST true
using namespace std;
/*
* This function is taken from http://www.guyrutenberg.com/2007/09/22/profiling-code-using-clock_gettime/
*
*/
timespec diff(timespec start, timespec end)
{
timespec temp;
if ((end.tv_nsec-start.tv_nsec)<0) {
temp.tv_sec = end.tv_sec-start.tv_sec-1;
temp.tv_nsec = 1000000000+end.tv_nsec-start.tv_nsec;
} else {
temp.tv_sec = end.tv_sec-start.tv_sec;
temp.tv_nsec = end.tv_nsec-start.tv_nsec;
}
return temp;
}
/**
* @param y y dimension
* @param x x dimensio
* @param column number of column in matrix
* @return 1D index
*/
int getMatrixIndex(int y, int x, int column)
{
return (y * column) + x;
}
/**
* Wait for data from other procesors
* @param recv_from wait on data form this procesor id
* @param store_data store received data
* @param tag either ROW or COLUMN
* @param received_count
*/
void recv_data(int recv_from,int *store_data, int tag,int *received_count)
{
MPI_Status status;
MPI_Recv(store_data, 1, MPI_INT, recv_from, tag, MPI_COMM_WORLD, &status);
(*received_count)++;
return;
}
/**
* Main algorithm for all procesors in matrix
* Receive data, make the calculation and send the data to another procesor
* @param my_id id of procesor
*/
void mesh_algorithm(int my_id)
{
int mat1_row, mat1_col,mat2_row,mat2_col = 0;
int received = 0;
int c = 0;
int recv_x, recv_y = 0;
int data_x, data_y = 0;
int x, y;
MPI_Status status;
//Get dimensions from first procesor
MPI_Recv(&mat1_row, 1, MPI_INT, 0, MAT1_ROW, MPI_COMM_WORLD, &status);
MPI_Recv(&mat1_col, 1, MPI_INT, 0, MAT1_COL, MPI_COMM_WORLD, &status);
MPI_Recv(&mat2_row, 1, MPI_INT, 0, MAT2_ROW, MPI_COMM_WORLD, &status);
MPI_Recv(&mat2_col, 1, MPI_INT, 0, MAT2_COL, MPI_COMM_WORLD, &status);
//Calculate my x,y index and index form who i will be receiving
x = my_id % mat2_col;
y = my_id / mat2_col;
recv_x = (x < 1) ? 0 : getMatrixIndex(y, x-1, mat2_col);
recv_y = (y < 1) ? 0 : getMatrixIndex(y-1, x, mat2_col);
while (received < (mat1_col + mat2_row))
{
//Wait for data
recv_data(recv_x,&data_x,ROW,&received);
recv_data(recv_y,&data_y,COLUMN,&received);
c += data_x * data_y;
//If id is not at border of mesh send to another processor
if ( x!=(mat2_col-1) )
{
MPI_Send(&data_x, 1, MPI_INT, getMatrixIndex(y, x+1, mat2_col), ROW, MPI_COMM_WORLD);
}
if ( y!=(mat1_row-1) )
{
MPI_Send(&data_y, 1, MPI_INT, getMatrixIndex(y+1, x, mat2_col), COLUMN, MPI_COMM_WORLD);
}
}
//Send final data to first procesor
MPI_Send(&c, 1, MPI_INT, 0, FINAL, MPI_COMM_WORLD);
return;
}
int getMatrix(int *size, vector<int> *matrix, string filename)
{
string line ;
int tmp_num;
ifstream matrixFile(filename.c_str());
getline(matrixFile, line );
*size = stoi(line );
while (getline(matrixFile, line ))
{
istringstream lineStream(line );
while (lineStream >> tmp_num)
{
matrix->push_back(tmp_num);
}
}
return 0;
}
int main(int argc, char *argv[])
{
timespec start_time;
timespec end_time;
int proces_num;
int my_id = 0;
vector<int> output;
int mat1_row;
int mat1_col;
int mat2_row;
int mat2_col;
//Init OpenMPI
MPI_Init(&argc, &argv);
MPI_Comm_size(MPI_COMM_WORLD, &proces_num);
MPI_Comm_rank(MPI_COMM_WORLD, &my_id);
MPI_Status status;
if (my_id == 0)
{
vector<int> matrix1;
vector<int> matrix2;
getMatrix(&mat1_row, &matrix1, "mat1");
getMatrix(&mat2_col, &matrix2, "mat2");
mat1_col = matrix1.size() / mat1_row;
mat2_row = matrix2.size() / mat2_col;
//Send matrix dimension to everybody
for (int i = 0; i < proces_num; i++)
{
MPI_Send(&mat1_col, 1, MPI_INT, i, MAT1_COL, MPI_COMM_WORLD);
MPI_Send(&mat1_row, 1, MPI_INT, i, MAT1_ROW, MPI_COMM_WORLD);
MPI_Send(&mat2_col, 1, MPI_INT, i, MAT2_COL, MPI_COMM_WORLD);
MPI_Send(&mat2_row, 1, MPI_INT, i, MAT2_ROW, MPI_COMM_WORLD);
}
//Senda data from first matrix to specific procesors
for (int i = 0; i < mat1_row; i++)
{
for (int j = 0; j < mat1_col; j++)
{
int tmp_index = getMatrixIndex(i, j, mat1_col);
int send_id = getMatrixIndex(i, 0, mat2_col);
MPI_Send(&matrix1[tmp_index], 1, MPI_INT, send_id, ROW, MPI_COMM_WORLD);
}
}
//Send data from second matrix to specific procesors
for (int i = 0; i < mat2_col; i++)
{
for (int j = 0; j < mat2_row; j++)
{
int tmp_index = getMatrixIndex(j, i, mat2_col);
int send_id = getMatrixIndex(0, i, mat2_col);
MPI_Send(&matrix2[tmp_index], 1, MPI_INT, send_id, COLUMN, MPI_COMM_WORLD);
}
}
}
if(TEST)
{
clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &start_time);
}
//Core of mesh algorithm
mesh_algorithm(my_id);
//First process receive the data from everobdy else and prints it
if (my_id == 0)
{
int tmp;
for (int i = 0; i < proces_num; i++)
{
MPI_Recv(&tmp, 1, MPI_INT, i, FINAL, MPI_COMM_WORLD, &status);
output.push_back(tmp);
}
tmp=0;
cout << mat1_row << ":" << mat2_col <<endl;
for(int i= 0; i<output.size(); i++)
{
tmp++;
if (tmp==mat2_col)
{
cout<<output[i]<<endl;
tmp=0;
}
else
{
cout<<output[i]<<" ";
}
}
}
//Wait for everbody and calculate time
MPI_Barrier(MPI_COMM_WORLD);
if(TEST && my_id==0){
clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &end_time);
timespec difference = diff(start_time,end_time);
cout << difference.tv_sec << ":" << difference.tv_nsec <<endl;
}
MPI_Finalize();
return 0;
}