0

My question is pretty simple, when I write:

//Compile with: mpicc temp.c
//Run with: mpirun -n 4 ./a.out
#include <mpi.h>
#include <stdio.h>

int main(int argc, char **argv){
  MPI_Init(&argc,&argv);

  int nbr, rank;
  MPI_Comm_size(MPI_COMM_WORLD, &nbr);
  MPI_Comm_rank(MPI_COMM_WORLD, &rank);

  printf("%d before\n",rank);
  MPI_Barrier(MPI_COMM_WORLD);
  printf("%d after\n",rank);

  MPI_Finalize();
}

with 4 processes, I get:

3 before
3 after
2 before
2 after
1 before
1 after
0 before
0 after

Why isn't it all the "before" then all the "after" ? I really need that behavior. Thanks for your help !

Richard
  • 56,349
  • 34
  • 180
  • 251
Izyr
  • 13
  • 2
  • Possible duplicate of [Does a call to MPI\_Barrier affect every thread in an MPI process?](https://stackoverflow.com/questions/5473482/does-a-call-to-mpi-barrier-affect-every-thread-in-an-mpi-process) – Claies Dec 01 '18 at 22:38
  • I've edited your question to include a Minimum Working Example that should demonstrate the problem. However: I don't observe the problem on my end. If this minimum working example doesn't match your situation, you should edit it to produce an example that does cause the problem. – Richard Dec 01 '18 at 23:04
  • Possible duplicate of [MPI Barrier not working in loops](https://stackoverflow.com/questions/50332772/mpi-barrier-not-working-in-loops) – Gilles Gouaillardet Dec 01 '18 at 23:33
  • Output is ordered on each MPI task, but there is no global order. Note that unless you are running on a single node, `flush(stdout)` will not help you. – Gilles Gouaillardet Dec 01 '18 at 23:36

0 Answers0