How to send a multidimensional, dynamic and growing array using MPI_Isend

I created an array and I want to send it using MPI. The first part works well (I think?), But I have problems with the second part. I assume this is part of the trick and how can I allocate an array? Please see:

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 have taken a look at https://stackoverflow.com/a/166164/169 and ...

+4
source share
1 answer

You tell MPI to send and receive real MPI_INT percentages, but your data is actually 4 MPI_INT.

, , , , .

edit: - :

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

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

, 4 , .

+1

Source: https://habr.com/ru/post/1606142/


All Articles