MPI collects an array in the root process

I am new to MPI. I have 4 processes: processes 1 to 3 fill the vector and send it to process 0, and process 0 collects the vectors into one very long vector. I have code that works (too long to post), but process 0 recv runs awkwardly and very slowly.

In the abstract, the code does the following:

MPI::Init();
int id = MPI::COMM_WORLD.Get_rank();

if(id>0) {
    double* my_array = new double[n*m]; //n,m are int
    Populate(my_array, id);
    MPI::COMM_WORLD.Send(my_array,n*m,MPI::DOUBLE,0,50);
}

if(id==0) {
    double* all_arrays = new double[3*n*m];
    /* Slow Code Starts Here */
    double startcomm = MPI::Wtime();
    for (int i=1; i<=3; i++) {
    MPI::COMM_WORLD.Recv(&all_arrays[(i-1)*m*n],n*m,MPI::DOUBLE,i,50);
    }
    double endcomm = MPI::Wtime();
    //Process 0 has more operations...
}
MPI::Finalize();

It turns out that it endcomm - startcommis 50% of the total time (0.7 seconds compared to 1.5 seconds to complete the program).

Is there a better way to get vectors from processes 1-3 and keep them in process 0 all_arrays?

I checked MPI :: Comm :: Going, but I'm not sure how to use it. In particular, will I let me indicate that the 1st order array is the first array in all_arrays, the second is the second array, etc.? Thank.

: "" "if" :

MPI_Gather(my_array,n*m,MPI_DOUBLE,
    &all_arrays[(id-1)*m*n],n*m,MPI_DOUBLE,0,MPI_COMM_WORLD);

. , "" , , ? ?

+3
1

, MPI_Gather . anl MPI_Gather:

int MPI_Gather(void *sendbuf, int sendcnt, MPI_Datatype sendtype, 
               void *recvbuf, int recvcnt, MPI_Datatype recvtype, 
               int root, MPI_Comm comm)

sendbuf - (my_array). recvbuf - (all_arrays) , . , , . .

EDIT:

, sendbuf , MPI_Gatherv ( @ ).

+5

All Articles