(int*)malloc(n * n * sizeof(int));
C = (int*)malloc(n * n * sizeof(int));
matGene(A, saveN, n);
matGene(B, saveN, n);
printf("\n");
printf(" Matrix_A: (%d×%d)\n", saveN, saveN);
// printMatrix(A, saveN, saveN, n);
printf(" Matrix_B: (%d×%d)\n", saveN, saveN);
// printMatrix(B, saveN, saveN, n);
printf("\n");
printf(" —----------------------------------------------------------------------------------\n");
printf(" Computing matrix product using multiplicateMatrix_block \n");
printf(" —----------------------------------------------------------------------------------\n\n");
// Prepare data
beginTime = MPI_Wtime();
// include begin not end
// beginRow = ((my_rank - 1) / b) * each_row, endRow = beginRow + each_row;
// beginColumn = ((my_rank - 1) % b) * each_column, endColumn = beginColumn + each_column;
// Send: A[beginRow:endRow, :], B[:, beginColumn:endColumn]
for (int i = 1; i < comm_sz; i++)
{
int beginRow = ((i - 1) / b) * each_row;
int beginColumn = ((i - 1) % b) * each_column;
// A: beginRow ~ endRow
MPI_Send(&A[beginRow * n], each_row * n, MPI_INT, i, i, MPI_COMM_WORLD);
// First address Number of bytes Type Destination Message identifier Communication domain
// B: n times, once a row
for (int j = 0; j < n; j++)
{
MPI_Send(&B[j * n + beginColumn], each_column, MPI_INT, i, i * n + j + comm_sz, MPI_COMM_WORLD);
}
}
// Recv: C[beginRow:endRow, beginColumn:endColumn]
for (int i = 1; i < comm_sz; i++) {
int beginRow = ((i - 1) / b) * each_row;
int endRow = beginRow + each_row;
int beginColumn = ((i - 1) % b) * each_column;
for (int j = beginRow; j < endRow; j++)
{
MPI_Recv(&C[j * n + beginColumn], each_column, MPI_INT, i, each_row * i + (j - beginRow), MPI_COMM_WORLD, &status);
}
}
endTime = MPI_Wtime();
printf("Matrix_C: (%d×%d) CPU running time is: %lfs\n\n\n", saveN, saveN, endTime - beginTime);
// printf("Matrix_C: (%d×%d)\n", saveN, saveN);
// printMatrix(C, saveN, saveN, n);
free(A);
free(B);
free(C);
}
else {
int *partA, *partB, *partC;
partA = (int*)malloc(each_row * n * sizeof(int));
partB = (int*)malloc(n * each_column * sizeof(int));
partC = (int*)malloc(each_row * each_column * sizeof(int));
// include begin not end
// beginRow = ((my_rank - 1) / b) * each_row, endRow = beginRow + each_row;
// beginColumn = ((my_rank - 1) % b) * each_column, endColumn = beginColumn + each_column;
// Recv: partA, partB
MPI_Recv(&partA[0 * n + 0], each_row * n, MPI_INT, 0, my_rank, MPI_COMM_WORLD, &status);
/* Receive 2 rows of value of A
printf("\n--partA----process number: %d-----\n", my_rank);
for (int i = 0; i < each_row; i++)
{
for (int j = 0; j < n; j++)
{
printf("%d ", partA[i*n+j]);
}
printf("\n");
}
*/
for (int j = 0; j < n; j++)
{
MPI_Recv(&partB[j * each_column], each_column, MPI_INT, 0, my_rank * n + j + comm_sz, MPI_COMM_WORLD, &status);
}
/*
// Receive the value of B
printf("\n--partB----process number: %d-----\n", my_rank);
for (int i = 0; i < n; i++)
{
for (int j = 0; j < each_column; j++)
{
printf("%d ", partB[i * each_column + j]);
}
printf("\n");
}
*/
multiplicateMatrix(partA, partB, partC, each_row, n-addnum, each_column,n);
/*
printf("\n--partc----process number: %d-----\n", my_rank);
for (int i = 0; i < each_row; i++)
{
for (int j = 0; j < each_column; j++)
{
printf("%d ", partC[i * each_column + j]);
}
printf("\n");
}
*/
// Send: partC
for (int j = 0; j < each_row; j++) {
MPI_Send(&partC[j * each_column], each_column, MPI_INT, 0, each_row * my_rank + j, MPI_COMM_WORLD);
}
free(partA);
free(partB);
free(partC);
}
}
MPI_Finalize();
return переделай на с++