Реализация алгоритма
MPI_Init(&argc, &argv);
MPI_Comm_size(MPI_COMM_WORLD, &size);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
if (rank ==0){
get_n(n);
a = new double[n*n];
b = new double[n*n];
c = new double[n*n];
get_matrix(a, n, 0, 5);
get_matrix(b, n, 1, 4);
set_nulls(c, n*n);
if (n<=10){
print_matrix(a, n, 'A');
print_matrix(b, n, 'B');
}
count = new int[size];
displ = new int[size];
count_1 = new int[size];
displ_1 = new int[size];
get_indexes(n, size, count, count_1, displ, displ_1);
ar = new int[size];
ar1 = new int[size];
for (int j=0; j
ar[j] =1;
ar1[j] = j;
}
dim[0] = n;
}
MPI_Bcast(dim, 1, MPI_INT, 0, MPI_COMM_WORLD);
n = dim[0];
MPI_Scatterv(count, ar, ar1, MPI_INT, n_rows, 1, MPI_INT, 0, MPI_COMM_WORLD);
num_a_row = n_rows[0];
MPI_Scatterv(displ, ar, ar1, MPI_INT, start, 1, MPI_INT, 0, MPI_COMM_WORLD);
MPI_Barrier(MPI_COMM_WORLD);
time_par=MPI_Wtime();
int n_elem = num_a_row * n;
local_a = new double[n_elem];
set_nulls(local_a, n_elem);
MPI_Scatterv(a, count_1, displ_1, MPI_DOUBLE, local_a, n_elem, MPI_DOUBLE, 0, MPI_COMM_WORLD);
local_a_2D = get_mem_2D(num_a_row, n);
set_2D(local_a, local_a_2D, num_a_row, n);
delete[] local_a;
local_c_2D = get_mem_2D(num_a_row, n);
set_nulls_2D(local_c_2D, num_a_row, n);
for (int i=0; i
num_b_row = n_rows[0];
int end = start[0] + num_b_row;
int n_elem = num_b_row * n;
double *local_b = new double[n_elem];
set_nulls(local_b, n_elem);
MPI_Scatterv(b, count_1, displ_1, MPI_DOUBLE, local_b, n_elem, MPI_DOUBLE, 0, MPI_COMM_WORLD);
double **local_b_2D = get_mem_2D(num_b_row, n);
set_2D(local_b, local_b_2D, num_b_row, n);
delete[] local_b;
for (int i1=0; i1 < num_a_row; i1++)
for (int m = start[0]; m < end; m++ )
for (int j = 0; j < n; j++)
local_c_2D[i1][j] += local_a_2D[i1][m] * local_b_2D[m-start[0]][j];
if (rank == 0){
shift(count, size);
shift(count_1, size);
shift(displ, size);
shift(displ_1,size);
}
MPI_Scatterv(count, ar, ar1, MPI_INT, n_rows, 1, MPI_INT, 0, MPI_COMM_WORLD);
MPI_Scatterv(displ, ar, ar1, MPI_INT, start, 1, MPI_INT, 0, MPI_COMM_WORLD);
free_mem_2D(local_b_2D, num_b_row);
}
local_c = new double[num_a_row*n];
set_nulls(local_c, num_a_row*n);
for (int i=0; i
for (int j=0; j
local_c[i*n + j] = local_c_2D[i][j];
}
free_mem_2D(local_c_2D, num_a_row);
MPI_Gatherv(local_c, num_a_row*n, MPI_DOUBLE, c, count_1, displ_1, MPI_DOUBLE, 0, MPI_COMM_WORLD);
delete[] local_c;
MPI_Barrier(MPI_COMM_WORLD);
time_par=MPI_Wtime() - time_par;
if (rank == 0) {
if (n<=10){
printf("\nResult of parallel algorithm.");
print_matrix(c, n, 'C');
}
printf("\nTime of parallel algorithm wtime: %5.3f\n", time_par);
|