I'm trying to implement a program using MPI, for which I need to have a block of code to be executed in a particular processor and until the execution completes other processors must wait.I thought it can be achieved using MPI_Barrier (though I'm not clear with its actual functionality) and tried out the following program.
#include<mpi.h>
#include<stdio.h>
int main(int argc, char **argv) {
int rank=0,size;
MPI_Init(&argc,&argv);
MPI_Comm_rank(MPI_COMM_WORLD,&rank);
MPI_Comm_size(MPI_COMM_WORLD,&size);
if(rank == 0){ //Block 1
printf("\nRank 0 Before Barrier");
}
MPI_Barrier(MPI_COMM_WORLD);
if(rank==1){
printf("\nRank 1 After Barrier");
printf("\nRank 1 After Barrier");
}
if(rank==2){
printf("\nRank 2 After Barrier");
}
MPI_Finalize();
}
I got the following output when I executed with np as 3
Rank 1 After Barrier
Rank 0 Before BarrierRank 2 After BarrierRank 1 After Barrier
How could I possibly make the other processors to wait until Block 1 completes its execution in the processor with Rank 0?
Intended Output
Rank 0 Before Barrier
Rank 1 After Barrier //After this, it might be interchanged
Rank 1 After Barrier
Rank 2 After Barrier
fflush(stdout);
after everyprintf
statement. – eduffy