I'm just thinking about the reason behind using MPI_Bcast
because when I do not broadcast integer N to all ranks, they can see the N. Look at the code and its result. Both before and after broadcasting, the integer N is visible to all ranks. so, what's the point here?
Also, Does this make sense that using MPI_Bcast
changes the order that each rank is called?
#include <iostream>
#include "mpi.h"
using namespace std;
int main()
{
MPI_Init(NULL, NULL);
int rank, size;
int N=9 ;
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Comm_size(MPI_COMM_WORLD, &size);
cout << " Hello from rank : " << rank << " N is: " << N << endl;
MPI_Bcast(&N, 1, MPI_INT, 0, MPI_COMM_WORLD);
cout << " Hello from rank : " << rank << " N is: " <<N<<endl;
MPI_Finalize();
}
Result:
Hello from rank : 1 N is: 9
Hello from rank : 3 N is: 9
Hello from rank : 0 N is: 9
Hello from rank : 2 N is: 9
Hello from rank : 0 N is: 9
Hello from rank : 1 N is: 9
Hello from rank : 2 N is: 9
Hello from rank : 3 N is: 9