-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMPI_function.cpp
More file actions
326 lines (297 loc) · 8.74 KB
/
MPI_function.cpp
File metadata and controls
326 lines (297 loc) · 8.74 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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
#include <iostream>
#include <mpi.h>
#include "MPI_function.hpp"
#include "matrix.hpp"
// Distributed sub-matrix to the other cores
double* deliver_sub_matrix(double* data_matrix, unsigned int matrix_size, int nb_proc)
{
int world_rank(-1);
double* sub_matrix;
int nb_row = matrix_size / nb_proc;
int last_nb_row = matrix_size % nb_proc;
MPI_Comm_rank(MPI_COMM_WORLD, &world_rank);
if(world_rank == 0)
{
for(int i = 1; i < nb_proc; i++)
{
int shift_value = nb_row * i * matrix_size + last_nb_row * matrix_size;
MPI_Send(&matrix_size, 1, MPI_UNSIGNED, i, SUB_MATRIX_SIZE_TAG, MPI_COMM_WORLD);
MPI_Send((data_matrix + shift_value), nb_row * matrix_size, MPI_DOUBLE, i,
SUB_MATRIX_TAG, MPI_COMM_WORLD);
}
sub_matrix = new double[matrix_size * (nb_row + last_nb_row)];
std::copy(data_matrix, data_matrix + ((nb_row + last_nb_row) * matrix_size),
sub_matrix);
}
return sub_matrix;
}
// All other cores receive their data
double* receives_sub_matrix(unsigned int* matrix_size, int nb_proc)
{
int world_rank(-1);
int nb_row;
double* sub_matrix;
MPI_Status status;
MPI_Comm_rank(MPI_COMM_WORLD, &world_rank);
if(world_rank != 0)
{
MPI_Recv(matrix_size, 1, MPI_UNSIGNED, 0, SUB_MATRIX_SIZE_TAG, MPI_COMM_WORLD,
&status);
nb_row = *matrix_size / nb_proc;
sub_matrix = new double[(*matrix_size) * nb_row];
MPI_Recv(sub_matrix, nb_row * (*matrix_size), MPI_DOUBLE, 0, SUB_MATRIX_TAG,
MPI_COMM_WORLD, &status);
}
return sub_matrix;
}
// Distributed sub-vector to the other cores
double* deliver_sub_vector(double* data_vector, unsigned int vector_size, int nb_proc)
{
int world_rank(-1);
double* sub_vector;
int nb_row = vector_size / nb_proc;
int last_nb_row = vector_size % nb_proc;
MPI_Comm_rank(MPI_COMM_WORLD, &world_rank);
if(world_rank == 0)
{
for(int i = 1; i < nb_proc; i++)
{
int shift_value = nb_row * i + last_nb_row;
MPI_Send(&vector_size, 1, MPI_UNSIGNED, i, SUB_VECTOR_SIZE_TAG, MPI_COMM_WORLD);
MPI_Send((data_vector + shift_value), nb_row, MPI_DOUBLE, i, SUB_VECTOR_TAG,
MPI_COMM_WORLD);
}
sub_vector = new double[nb_row + last_nb_row];
std::copy(data_vector, data_vector + (nb_row + last_nb_row), sub_vector);
}
return sub_vector;
}
// All other cores receive their data
double* receives_sub_vector(int nb_proc)
{
int world_rank(-1);
unsigned int vector_size;
int nb_row;
double* sub_vector;
MPI_Status status;
MPI_Comm_rank(MPI_COMM_WORLD, &world_rank);
if(world_rank != 0)
{
MPI_Recv(&vector_size, 1, MPI_UNSIGNED, 0, SUB_VECTOR_SIZE_TAG, MPI_COMM_WORLD,
&status);
nb_row = vector_size / nb_proc;
sub_vector = new double[nb_row];
MPI_Recv(sub_vector, nb_row, MPI_DOUBLE, 0, SUB_VECTOR_TAG, MPI_COMM_WORLD, &status);
}
return sub_vector;
}
// Gather all the sub-results in rank order
double* gather_result(double* sub_result, unsigned int matrix_size)
{
int nb_proc(-1);
int world_rank(-1);
MPI_Comm_rank(MPI_COMM_WORLD, &world_rank);
MPI_Comm_size(MPI_COMM_WORLD, &nb_proc);
double* result = new double[matrix_size];
int* nb_sent_element = new int[nb_proc];
int* shift = new int[nb_proc];
int nb_row = matrix_size / nb_proc;
int last_nb_row = matrix_size % nb_proc;
nb_sent_element[0] = nb_row + last_nb_row;
shift[0] = 0;
for(int i = 1; i < nb_proc; i++)
{
nb_sent_element[i] = nb_row;
shift[i] = shift[i - 1] + nb_sent_element[i - 1];
}
if(world_rank == 0)
{
MPI_Gatherv(sub_result, nb_row + last_nb_row, MPI_DOUBLE, result, nb_sent_element,
shift, MPI_DOUBLE, 0, MPI_COMM_WORLD);
}
else
{
MPI_Gatherv(sub_result, nb_row, MPI_DOUBLE, result, nb_sent_element, shift,
MPI_DOUBLE, 0, MPI_COMM_WORLD);
}
return result;
}
/* Performs the matrix-vector product, taking care of communication between nodes. */
double* pmv_2(double* matrix, double* vector, unsigned int matrix_size,
unsigned int nb_proc)
{
int world_rank;
unsigned int i, j, k;
unsigned int start_index;
unsigned int stop_index;
unsigned int local_nb_row;
MPI_Request* request;
MPI_Status* status;
double* result;
double* next_vector;
double* vector_tmp;
int nb_row = matrix_size / nb_proc;
int last_nb_row = matrix_size % nb_proc;
unsigned int* dest_rank = new unsigned int[nb_proc - 1];
unsigned int* source_rank = new unsigned int[nb_proc - 1];
MPI_Comm_rank(MPI_COMM_WORLD, &world_rank);
// Creation of the list of neighbors for each core
k = world_rank;
for(i = 0; i < nb_proc - 1; i++)
{
if(k == (nb_proc - 1) && world_rank != 0)
{
source_rank[i] = 0;
k = 0;
}
else if(k + 1 != world_rank)
{
source_rank[i] = k + 1;
k++;
}
}
k = world_rank;
for(i = 0; i < nb_proc - 1; i++)
{
if(k == 0 && world_rank != (nb_proc - 1))
{
dest_rank[i] = nb_proc - 1;
k = nb_proc - 1;
}
else if(k - 1 != world_rank)
{
dest_rank[i] = k - 1;
k--;
}
}
if(world_rank == 0)
{
result = new double[nb_row + last_nb_row];
local_nb_row = nb_row + last_nb_row;
}
else
{
result = new double[nb_row];
local_nb_row = nb_row;
}
if(world_rank == 0)
{
for(k = 0; k < nb_proc - 1; k++)
{
request = new MPI_Request[2];
status = new MPI_Status[2];
next_vector = new double[nb_row];
MPI_Irecv(next_vector, nb_row, MPI_DOUBLE, source_rank[k], SWAP_VECTOR,
MPI_COMM_WORLD, &(request[0]));
MPI_Isend(vector, nb_row + last_nb_row, MPI_DOUBLE, dest_rank[k], SWAP_VECTOR,
MPI_COMM_WORLD, &(request[1]));
if(k == 0)
{
start_index = 0;
stop_index = nb_row + last_nb_row;
for(i = 0; i < local_nb_row; i++)
{
result[i] = 0;
for(j = start_index; j < stop_index; j++)
{
result[i] += matrix[i * matrix_size + j] * vector[j - start_index];
}
}
}
else
{
start_index = source_rank[k - 1] * nb_row + last_nb_row;
stop_index = (source_rank[k - 1] + 1) * nb_row + last_nb_row;
for(i = 0; i < local_nb_row; i++)
{
for(j = start_index; j < stop_index; j++)
{
result[i] += matrix[i * matrix_size + j] * vector_tmp[j - start_index];
}
}
}
MPI_Wait(request, status);
vector_tmp = next_vector;
}
}
else
{
for(k = 0; k < nb_proc - 1; k++)
{
request = new MPI_Request[2];
status = new MPI_Status[2];
if(source_rank[k] == 0)
{
next_vector = new double[nb_row + last_nb_row];
MPI_Irecv(next_vector, nb_row + last_nb_row, MPI_DOUBLE, source_rank[k],
SWAP_VECTOR, MPI_COMM_WORLD, &(request[0]));
}
else
{
next_vector = new double[nb_row];
MPI_Irecv(next_vector, nb_row, MPI_DOUBLE, source_rank[k], SWAP_VECTOR,
MPI_COMM_WORLD, &(request[0]));
}
MPI_Isend(vector, nb_row, MPI_DOUBLE, dest_rank[k], SWAP_VECTOR, MPI_COMM_WORLD,
&(request[1]));
if(k == 0)
{
start_index = world_rank * nb_row + last_nb_row;
stop_index = (world_rank + 1) * nb_row + last_nb_row;
for(i = 0; i < local_nb_row; i++)
{
result[i] = 0;
for(j = start_index; j < stop_index; j++)
{
result[i] += matrix[i * matrix_size + j] * vector[j - start_index];
}
}
}
else if(source_rank[k - 1] == 0)
{
start_index = 0;
stop_index = nb_row + last_nb_row;
for(i = 0; i < local_nb_row; i++)
{
for(j = start_index; j < stop_index; j++)
{
result[i] += matrix[i * matrix_size + j] * vector_tmp[j - start_index];
}
}
}
else
{
start_index = source_rank[k - 1] * nb_row + last_nb_row;
stop_index = (source_rank[k - 1] + 1) * nb_row + last_nb_row;
for(i = 0; i < local_nb_row; i++)
{
for(j = start_index; j < stop_index; j++)
{
result[i] += matrix[i * matrix_size + j] * vector_tmp[j - start_index];
}
}
}
MPI_Wait(request, status);
vector_tmp = next_vector;
}
}
if(source_rank[nb_proc - 2] == 0)
{
start_index = 0;
stop_index = nb_row + last_nb_row;
}
else
{
start_index = source_rank[nb_proc - 2] * nb_row + last_nb_row;
stop_index = (source_rank[nb_proc - 2] + 1) * nb_row + last_nb_row;
}
for(i = 0; i < local_nb_row; i++)
{
for(j = start_index; j < stop_index; j++)
{
result[i] += matrix[i * matrix_size + j] * vector_tmp[j - start_index];
}
}
delete[] next_vector;
return result;
}