I've a problem when send the matrix by process with rank = 0 to other processes. The output should be:
[150][170][190][210][230][250][270][290][310][330]
[225][255][285][315][345][375][405][435][465][495]
[300][340][380][420][460][500][540][580][620][660]
[375][425][475][525][575][625][675][725][775][825]
[450][510][570][630][690][750][810][870][930][990]
[525][595][665][735][805][875][945][1015][1085][1155]
[600][680][760][840][920][1000][1080][1160][1240][1320]
[675][765][855][945][1035][1125][1215][1305][1395][1485]
[750][850][950][1050][1150][1250][1350][1450][1550][1650]
[825][935][1045][1155][1265][1375][1485][1595][1705][1815]
but it return:
[150][170][190][210][230][250][270][290][310][330]
[225][255][285][315][345][375][405][435][465][495]
[300][340][380][420][460][500][540][580][620][660]
[375][425][475][525][575][625][675][725][775][825]
[450][510][570][630][690][750][810][870][930][990]
[525][595][665][735][805][875][945][1015][1085][1155]
[600][680][760][840][920][1000][1080][1160][1240][1320]
[675][765][855][945][1035][1125][1215][1305][1395][1485]
[750][850][950][1050][1150][1250][1350][1450][1550][1650]
[825][935][1045][1155][1265][1375][0][0][0][0]
Can you help me?
int main(int argc, char *argv[])
{
double **A = (double **)malloc(SIZE_N*sizeof(double *));
double **B = (double **)malloc(SIZE_N*sizeof(double *));
double **C = (double **)malloc(SIZE_N*sizeof(double *));
int g = 0;
for (g = 0; g < SIZE_N; g++) {
A[g] = (double *)malloc(SIZE_N * sizeof(double));
}
int g1 = 0;
for (g1 = 0; g1 < SIZE_N; g1++) {
B[g1] = (double *)malloc(SIZE_N * sizeof(double));
}
int g2 = 0;
for (g2 = 0; g2 < SIZE_N; g2++) {
C[g2] = (double *)malloc(SIZE_N * sizeof(double));
}
for (i = 0; i < SIZE_N; i++) {
for (j = 0; j < SIZE_N; j++) {
*(*(A+i)+j) = i+2;
*(*(B+i)+j) = i+j+3;
}
}
if (rank == 0) {
start_time = MPI_Wtime();
for (i = 1; i < size; i++) {
offset = SIZE_N/(size-1);
bottom = (i-1)*offset;
// (i+1)==size indica l'ultimo nodo
//se sei all'ultimo nodo e se ci sono righe in eccesso da elaborare
//assegnale tutte all'ultimo nodo
if((i+1)==size && SIZE_N%(size-1)!=0){
top = SIZE_N;
}
else{
top=bottom+offset;
}
MPI_Send(&bottom,1,MPI_INT,i,1,MPI_COMM_WORLD);
MPI_Send(&top,1,MPI_INT,i,2,MPI_COMM_WORLD);
}
i = 0;
for (i = 1; i < size; i++) {
MPI_Recv(&bottom,1,MPI_INT,i,4,MPI_COMM_WORLD,&status);
MPI_Recv(&top,1,MPI_INT,i,5,MPI_COMM_WORLD,&status);
MPI_Recv(&C[bottom][0],(top-bottom)*SIZE_N,MPI_DOUBLE,i,6,MPI_COMM_WORLD,&status);
MPI_Recv(&weak_time,1,MPI_DOUBLE,i,7,MPI_COMM_WORLD,&status);
last_weak += weak_time;
//printf("Weak_time process %d> %f\nLast_weak> %f\n",i,weak_time,last_weak);
}
end_time = MPI_Wtime();
i = 0;
for (i = 0; i < SIZE_N; i++) {
for (j = 0; j < SIZE_N; j++){
printf("[%.0f]", C[i][j]);
}
printf("\n");
}
printf("\n\nTempo di calcolo compreso di stampa = %f\n\n",(end_time-start_time));
fflush(stdout);
}
if (rank > 0) {
double start,end,time_result;
start = MPI_Wtime();
MPI_Recv(&bottom,1,MPI_INT,0,1,MPI_COMM_WORLD,&status);
MPI_Recv(&top,1,MPI_INT,0,2,MPI_COMM_WORLD,&status);
for (i = bottom; i < top; i++) {
for (j = 0; j < SIZE_N; j++) {
for (k = 0; k < SIZE_N; k++) {
C[i][j] = C[i][j] + (A[i][k] * B[k][j]);
}
}
}
MPI_Send(&bottom,1,MPI_INT,0,4,MPI_COMM_WORLD);
MPI_Send(&top,1,MPI_INT,0,5,MPI_COMM_WORLD);
MPI_Send(&C[bottom][0],(top-bottom)*SIZE_N,MPI_DOUBLE,0,6,MPI_COMM_WORLD);
end = MPI_Wtime();
time_result = end-start;
MPI_Send(&time_result,1,MPI_DOUBLE,0,7,MPI_COMM_WORLD);
}
MPI_Finalize();
return 0;}`