int main int argc char argv int rank procNum MPI_Init argc argv MPI_Co

 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
int main ( int argc, char *argv[] ) {
int rank, procNum;
MPI_Init(&argc, &argv);
MPI_Comm_size(MPI_COMM_WORLD, &procNum);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
int startTime = MPI_Wtime();
//matrix N*M, vector M;
//(N - количество строк, M - количество столбцов)
const int N = 8000, M = 8000;
const int buf_N = N/procNum;
double* matrix = new double[N*M*sizeof(double)];
double* vector = new double[N*sizeof(double)];
double* buf_matr = new double[M*buf_N*sizeof(double)];
double* res = new double[M*sizeof(double)];
double* buf_res = new double[M*sizeof(double)];
if (rank == 0)
init(matrix, vector, N, M);
//loop for best time estimate
for (int i=0; i < 100; i++){
MPI_Bcast(vector, M, MPI_DOUBLE, 0, MPI_COMM_WORLD);
MPI_Scatter(matrix, M*buf_N, MPI_DOUBLE, buf_matr, M*buf_N, MPI_DOUBLE, 0, MPI_COMM_WORLD);
buf_res = multiply_matrix(buf_matr, vector, buf_N, M);
MPI_Gather(buf_res, buf_N, MPI_DOUBLE, res, buf_N, MPI_DOUBLE, 0, MPI_COMM_WORLD);
}
if (rank == 0){
cout << "proc num: " << procNum << "; time: " << MPI_Wtime()-startTime << "\n";
}
delete[] matrix;
delete[] vector;
delete[] buf_matr;
delete[] res;
delete[] buf_res;
MPI_Finalize();
return 0;
}