3

I have a value on local array (named lvotes) for each of the processors (assume 3 processors), and first element of each is storing a value, i.e.:

P0 : 4
P1 : 6
p2 : 7

Now, using MPI_Gather, I want gather them all in P0, so It will look like :

P0 : 4, 6, 7

I used gather this way:

MPI_Gather(lvotes, P, MPI_INT, lvotes, 1, MPI_INT, 0, MPI_COMM_WORLD);

But I get problems. It's my first time coding in MPI. I could use any suggestion. Thanks

0

1 Answer 1

9

This is a common issue with people using the gather/scatter collectives for the first time; in both the send and receive counts you specify the count of items to send to or receive from each process. So although it's true that you'll be, in total, getting (say) P items, if P is the number of processors, that's not what you specify to the gather operation; you specify you are sending a count of 1, and receiving a count of 1 (from each process). Like so:

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <mpi.h>

int main ( int argc, char **argv ) {

    int rank;
    int size;

    int lvotes;
    int *gvotes;

    MPI_Init ( &argc, &argv );
    MPI_Comm_rank ( MPI_COMM_WORLD, &rank );
    MPI_Comm_size ( MPI_COMM_WORLD, &size );

    if (rank == 0)
        gvotes = malloc(size * sizeof(int) );

    /* everyone sets their first lvotes element */
    lvotes = rank+4;

    /* Gather to process 0 */
    MPI_Gather(&lvotes, 1, MPI_INT, /* send 1 int from lvotes.. */
                gvotes, 1, MPI_INT, /* gather 1 int each process into lvotes */
               0, MPI_COMM_WORLD); /* ... to root process 0 */


    printf("P%d: %d\n", rank, lvotes);
    if (rank == 0) {
        printf("P%d: Gathered ", rank);
        for (int i=0; i<size; i++)
            printf("%d ", gvotes[i]);
        printf("\n");
    }

    if (rank == 0)
        free(gvotes);

    MPI_Finalize();

    return 0;
}

Running gives

$ mpirun -np 3 ./gather
P1: 5
P2: 6
P0: 4
P0: Gathered 4 5 6 
Sign up to request clarification or add additional context in comments.

1 Comment

Sweet baby jesus, thank you! I've been fighting with this all day!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.