0

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;}`
Shiro
  • 2,610
  • 2
  • 20
  • 36
Stefano
  • 85
  • 7
  • `double **` is not a matrix (aka 2D array! A pointer is not an array. – too honest for this site Jun 14 '17 at 19:11
  • How can I fix the problem? – Stefano Jun 14 '17 at 19:46
  • Possible duplicate of [Sending and receiving 2D array over MPI](https://stackoverflow.com/questions/5901476/sending-and-receiving-2d-array-over-mpi) – Zulan Jun 14 '17 at 20:14
  • `*(*(A+i)+j)` ... holy ... I would recommend you change your learning materials if that is the advocated way to access this kind of data structure. (`A[i][j]`) – Zulan Jun 14 '17 at 20:15
  • @Zulan: Your "dup" is C++, the question C. Different languages! – too honest for this site Jun 14 '17 at 20:25
  • @Olaf doesn't matter. You could verbatim copy the answer there as answer to this question here. If you feel that would be the best way to do it ... go ahead. – Zulan Jun 14 '17 at 20:29
  • @Zulan: Your dup's question lacks a [mcve], this one required information ("does not work" is not a valid problem description). The proposed answer does not use a 2D array (like OP here). Both approaches are plain wrong. If you want a 2D array, use one. I don't support further decay of quality of this site. There are reasons we don't answer bad questions. We are not a debugging service. (as a side-note: the code in the answer is bad C++ and would be bad C, too - feel free to figure out why). – too honest for this site Jun 14 '17 at 20:32
  • I'm Sorry but I didn't want to create any problems. My question is: why the code return 0 at the end of the printf? – Stefano Jun 14 '17 at 22:35

0 Answers0