There is nothing wrong with MPI_Barrier ().
Like Jens mentioned , the reason you don't see the expected result is because stdout is buffered for each process. There is no guarantee that fingerprints from several processes will be displayed in the calling process in order. (If stdout from each process is transferred to the main process for printing in real time, this will lead to a lot of unnecessary communication!)
If you want to convince yourself that the barrier works, you can try writing a file. The presence of several processes writing to one file can lead to additional complications, so you can write each record to a single file, and then after the barrier exchange the files to which they write. For example:
Proc-0 Proc-1 | | f0.write(..) f1.write(...) | | x ~~ barrier ~~ x | | f1.write(..) f0.write(...) | | END END
Implementation Example:
#include "mpi.h" #include <stdio.h> #include <stdlib.h> int main(int argc, char **argv) { char filename[20]; int rank, size; FILE *fp; MPI_Init(&argc, &argv); MPI_Comm_rank(MPI_COMM_WORLD, &rank); MPI_Comm_size(MPI_COMM_WORLD, &size); if (rank < 2) { /* proc 0 and 1 only */ sprintf(filename, "file_%d.out", rank); fp = fopen(filename, "w"); fprintf(fp, "P%d: before Barrier\n", rank); fclose(fp); } MPI_Barrier(MPI_COMM_WORLD); if (rank < 2) { /* proc 0 and 1 only */ sprintf(filename, "file_%d.out", (rank==0)?1:0 ); fp = fopen(filename, "a"); fprintf(fp, "P%d: after Barrier\n", rank); fclose(fp); } MPI_Finalize(); return 0; }
After running the code, you should get the following results:
[ me@home ]$ cat file_0.out P0: before Barrier P1: after Barrier [ me@home ]$ cat file_1.out P1: before Barrier P0: after Barrier
For all files, post-barrier statements will always be displayed later.
Shawn chin
source share