1

Say I have a simple program, foo.py:

from mpi4py import MPI
comm = MPI.COMM_WORLD
rank = comm.Get_rank()

# Some stuff happens here

print "Done"

and run it using mpiexec -n 4 python foo.py, it prints "Done" 4 times.

How do I get it to print "Done" only once, at the end?

I tried

if rank == 3:
    print "Done"

but this is not guaranteed to be last. The documentation regarding I/O for mpi4py is not complete.

binaryfunt
  • 6,401
  • 5
  • 37
  • 59
  • 2
    You can use a `MPI.COMM_WORLD.Barrier()` to make sure that all processes have reached that point in the code. – francis Apr 20 '17 at 17:27
  • Oh, so if I put that before `if rank == 3` (or even `if rank == 0` then) it would print "Done" last? – binaryfunt Apr 20 '17 at 18:12
  • Yes, and you can also flush the output of Python print for all processes, before the barrier, just to make sure. See http://stackoverflow.com/questions/230751/how-to-flush-output-of-python-print – francis Apr 21 '17 at 07:00

0 Answers0