I am trying to understand the mpi_bcast. more specifically, in the below code I am expecting the end result would be buf0->1990 and buf1-->1991.
#include <mpi.h>
#include <stdio.h>
int main(int argc, char** argv) {
int rank;
int buf0;
int buf1;
const int root=0;
MPI_Init(&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
if(rank == 0){
buf0 = 1990;
} else {
buf1 = 1991;
}
printf("[%d]: Before Bcast, buf0 is %d\n", rank, buf0);
printf("[%d]: Before Bcast, buf1 is %d\n", rank, buf1);
/* everyone calls bcast, data is taken from root and ends up in everyone's buf */
MPI_Bcast(&buf0, 1, MPI_INT, 0, MPI_COMM_WORLD);
MPI_Bcast(&buf1, 1, MPI_INT, 1, MPI_COMM_WORLD);
printf("[%d]: After Bcast, buf0 is %d\n", rank, buf0);
printf("[%d]: After Bcast, buf1 is %d\n", rank, buf1);
MPI_Finalize();
return 0;
}
However the output that I get is as follows, I was expecting that both the threads would have the value of buf0 and buf1 syncronized however buf1 for thread 0 is 0. Am I missing something here.
mpirun -np 2 mpi_counter_1
[0]: Before Bcast, buf0 is 1990
[0]: Before Bcast, buf1 is 0
[1]: Before Bcast, buf0 is 0
[1]: Before Bcast, buf1 is 1991
[0]: After Bcast, buf0 is 1990
[0]: After Bcast, buf1 is 0
[1]: After Bcast, buf0 is 1990
[1]: After Bcast, buf1 is 1991