int coords[DIMENSION];
MPI_Cart_coords(custom_communicator, rank, DIMENSION, coords);
#ifdef DEBUG == 3
printf("Coords %d %d\n", coords[0], coords[1]);
#endif
//сген. значения матриц 0го узла
if(coords[Y] == 0 && coords[X] == 0){
#ifdef DEBUG == 3
printf("Num procs: %d\n", size);
#endif
a = generate_double_arr(ROWS_A*COLS_A);
fill_matrix_random_values(a, ROWS_A, COLS_A);
b = generate_double_arr(ROWS_B*COLS_B);
fill_matrix_random_values(b, ROWS_B, COLS_B);
c = generate_double_arr(ROWS_A*COLS_B);
#if DEBUG == 3
printf("Matrix a\n");
print_matrix(a, ROWS_A, COLS_A);
printf("Matrix b\n");
print_matrix(b, ROWS_B, COLS_B);
#endif
}
int numberRows = ROWS_A / dims[Y];
int numberColumns = COLS_B / dims[X];
double *partA = generate_double_arr(numberRows * COLS_A);
double *partB = generate_double_arr(numberColumns * ROWS_B);
double *partC = generate_double_arr(numberColumns * numberRows);
int rowsFlags[2]={0, 1};
MPI_Comm rows;
MPI_Cart_sub(custom_communicator, rowsFlags, &rows);
int colsFlags[2]={1, 0};
MPI_Comm cols;
MPI_Cart_sub(custom_communicator, colsFlags, &cols);
if(coords[X] == 0){
MPI_Scatter(a, numberRows * COLS_A, MPI_DOUBLE, partA, numberRows * COLS_A, MPI_DOUBLE, 0, cols);
}
MPI_Bcast(partA, numberRows * COLS_A, MPI_DOUBLE, 0, rows);
MPI_Datatype column;
MPI_Datatype resized_column;
MPI_Type_vector(ROWS_B, numberColumns, COLS_B, MPI_DOUBLE, &column);
MPI_Type_commit(&column);
MPI_Type_create_resized(column, 0, (size_t)(numberColumns * sizeof(double)), &resized_column);
MPI_Type_commit(&resized_column);
if(coords[Y] == 0){
MPI_Scatter(b, 1, resized_column, partB, numberColumns * ROWS_B, MPI_DOUBLE, 0, rows);
}
MPI_Bcast(partB, numberColumns * ROWS_B, MPI_DOUBLE, 0, cols);
//посчитали
part_multiply(partC, partA, partB, numberRows, numberColumns);
MPI_Datatype block;
MPI_Datatype block_resized;
MPI_Type_vector(numberRows, numberColumns, COLS_B, MPI_DOUBLE, &block);
MPI_Type_commit(&block);
MPI_Type_create_resized(block, 0, (size_t)(numberColumns * sizeof(double)), &block_resized);
MPI_Type_commit(&block_resized);
int recv_counts[size];
for(size_t i = 0; i < size; i++){
recv_counts[i] = 1;
}
int displs[size];
for (int i = 0; i < dims[Y]; ++i) {
for (int j = 0; j < dims[X]; ++j) {
displs[i * dims[X] + j] = i * dims[X] * numberRows + j;
}
}
MPI_Gatherv(partC, numberColumns*numberRows, MPI_DOUBLE, c, recv_counts, displs, block_resized, 0, custom_communicator);
free(partA);
free(partB);
free(partC);
MPI_Type_free(&column);
MPI_Type_free(&resized_column);
MPI_Type_free(&block);
MPI_Type_free(&block_resized);
double end_time = MPI_Wtime();
if(rank == 0){
double time = end_time-start_time;
double boost = SEQTIME / time;
double efficiency = (boost / size) * 100;
printf("time: %f\n",time);
printf("boost = %f\n", boost);
printf("efficiency = %f\n", efficiency);
}
MPI_Finalize();