4

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
user2766839
  • 177
  • 3
  • 10
  • 2
    Your code works just fine for me (mpich 3.0.4 and gcc 4.9.3) as it should (AFAICS). The behaviour you experience looks very fishy to me. This can be a mistake in compiling and/or running the code (discrepancy between the MPI library used at compilation time, and the launcher). Or this can be a bug in the MPI library (that seems unlikely since that would be the hell of a bug). – Gilles Mar 02 '16 at 08:04
  • 2
    I agree with @Gilles, one thing that looks a little bit fishy is the missing `./` before the call to `mpi_counter_1` (see [this answer](http://stackoverflow.com/a/6331085/620382)). Maybe it is using a different binary than you expect? – Zulan Mar 02 '16 at 08:32
  • Agree with the previous two comments. The code works fine on OpenMPI 1.10 and gnu 4.8.x compilers. – Harald Mar 02 '16 at 10:21
  • Was interesting, I inited the buf0 and buf1 to 0 and now it works fine. Not sure what could cause it. – user2766839 Mar 03 '16 at 00:29
  • The bug is gone, so I'm voting to close. – Gilles Mar 04 '16 at 11:45

0 Answers0