4

I created an array and I want to send it with MPI. The first part works well (I think?) but I have problems with the second part. I guess it's the receive part and how i malloc the array? Please take a look:

if (myrank > 1)
{
    //first part
    int rows = 5;
    int cols = 4;
    int realcount = 0;
    int (*sendarray)[cols] = malloc(sizeof *sendarray * rows);
    for (int j = 0; j < 100; ++j)
    {
        for (int k = 0; k < 100; ++k)
        {
            for (int l = 0; l < 100; ++l)
            {
                if(realcount==rows){
                    int newnum = (rows + 2) * 2;
                    int (*newptr)[cols] = realloc(sendarray, sizeof *newptr * newnum);
                    rows = newnum;
                    sendarray = newptr;
                }
                /*
                    other stuff and checks
                */
                if(checks)
                {
                    sendarray[realcount][0] = j;
                    sendarray[realcount][1] = k;
                    sendarray[realcount][2] = l;
                    sendarray[realcount][3] = max;
                    ++realcount;
                }
            }
        }
    }
    //Send array
    MPI_Request req;
    MPI_Isend(sendarray, realcount, MPI_INT, 1, 1, MPI_COMM_WORLD, &req);
    MPI_Wait(&req, MPI_STATUS_IGNORE);
}else if(myrank == 1){
    //second part
    //for-loop: check incomming data for each task > 1
    for () {
        i = task thats going to send data
        int amount = 0;
        int cols = 4;
        MPI_Status status;
        MPI_Probe(i, 1, MPI_COMM_WORLD, &status);
        MPI_Get_count(&status, MPI_INT, &amount);
        int (*recv_buf)[cols] = malloc(sizeof *recv_buf * amount);
        MPI_Recv(recv_buf, amount, MPI_INT, i, 1, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
        for (int var = 0; var < amount; ++var) {
            //wrong data here
            fprintf(fp, "{ \"x\": %d, \"y\": %d, \"z\": %d, \"value\": %d },", recv_buf[var][0], recv_buf[var][1], recv_buf[var][2], recv_buf[var][3]);
        }
        free(recv_buf);
    }
}

Thank you very much for your help. I took a look at https://stackoverflow.com/a/13535563/2521647 and https://stackoverflow.com/a/14051503/2521647

Community
  • 1
  • 1
Julius
  • 102
  • 1
  • 9

1 Answers1

1

You tell MPI to send and receive realcount many MPI_INTs, but your data are actually 4 MPI_INTs.

I don't know if this is the only issue, but this could explain, why you get a different filling for your array than you were expecting.

edit: In your case you need to do something like:

MPI_Isend(sendarray, realcount*cols, MPI_INT, 1, 1, MPI_COMM_WORLD, &req);

and

     int (*recv_buf)[cols] = malloc(sizeof *recv_buf * amount/4);
     ...
     for (int var = 0; var < amount/4; ++var) {

or use a derived datatype to describe the 4 integers you want to send.

haraldkl
  • 3,809
  • 26
  • 44