[ACCEPTED]-OpenMPI MPI_Barrier problems-openmpi
The order in which your print out lines 11 appear on your terminal is not necessarily 10 the order in which things are printed. You 9 are using a shared resource (stdout
) for that 8 so there always must be an ordering problem. (And 7 fflush
doesn't help here, stdout
is line buffered anyhow.)
You 6 could try to prefix your output with a timestamp 5 and save all of this to different files, one 4 per MPI process.
Then to inspect your log 3 you could merge the two files together and 2 sort according to the timestamp.
Your problem 1 should disappear, then.
There is nothing wrong with MPI_Barrier().
As 19 Jens mentioned, the reason why you are not seeing the 18 output you expected is because stdout is 17 buffered on each processes. There is no 16 guarantee that prints from multiple processes 15 will be displayed on the calling process 14 in order. (If stdout from each process is 13 be transferred to the main process for printing 12 in real time, that will lead to lots of 11 unnecessary communication!)
If you want to 10 convince yourself that the barrier works, you 9 could try writing to a file instead. Having 8 multiple processes writing to a single file 7 may lead to extra complications, so you 6 could have each proc writing to one file, then 5 after the barrier, swap the files they write 4 to. For example:
Proc-0 Proc-1
| |
f0.write(..) f1.write(...)
| |
x ~~ barrier ~~ x
| |
f1.write(..) f0.write(...)
| |
END END
Sample implementation:
#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 3 running the code, you should get the following 2 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, the "after Barrier" statements 1 will always appear later.
Output ordering is not guaranteed in MPI 11 programs.
This is not related to MPI_Barrier 10 at all.
Also, I would not spend too much 9 time on worrying about output ordering with 8 MPI programs.
The most elegant way to achieve 7 this, if you really want to, is to let the 6 processes send their messages to one rank, say, rank 5 0, and let rank 0 print the output in the 4 order it received them or ordered by ranks.
Again, dont 3 spend too much time on trying to order the 2 output from MPI programs. It is not practical 1 and is of little use.
Adding to the previous answers here, your 4 MPI_BARRIER works fine.
Though, if you just 3 intend to see it working, you can force 2 pause the execution (SLEEP(1)
) for a moment to let 1 the output catch up.
More Related questions
We use cookies to improve the performance of the site. By staying on our site, you agree to the terms of use of cookies.