简体   繁体   中英

MPI_Gather - Send data to the master

I want to use MPI_Gather to send data to the rank 0. But I am doing something wrong:

int size;
int rank;
int i;
int b;
MPI_Init(&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Comm_size(MPI_COMM_WORLD, &size);
int* bb= new int[size];

//slave
if(rank!=0) {·
    b=13;·
    printf("slave %d \n", b);
    // sleep(5);
    MPI_Gather(&b,1,MPI_INT,bb,1,MPI_INT,0,MPI_COMM_WORLD);
}

//master
if(rank==0)
{
    b=12;
    printf("master %d \n", b);
    for (i = 0; i < size; i++) {
        printf("%d\t", bb[i]);
    }
}

MPI_Finalize();
return 0;

I am expecting bb=[12,13]. Do you know how to do it? Thank you.

The master process must also call MPI_Gather . You should move the call out of the conditional block:

//slave
if(rank!=0) {
    b=13;
    printf("slave %d \n", b);
}
else {
    b=12;
    printf("master %d \n", b);
}

// all
MPI_Gather(&b,1,MPI_INT,bb,1,MPI_INT,0,MPI_COMM_WORLD);

//master
if(rank==0)
{        
    for (i = 0; i < size; i++) {
        printf("%d\t", bb[i]);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM