How to output the results in each iteration in parallel computation?

Hi all, in order to observe the progress of the running code, I want to output the computational results from FEniCS/FEniCSX in each iteration. And here is a minimal example:

from mpi4py import MPI
import time

Iter = 0
while Iter < 3:
    Iter += 1
    if MPI.COMM_WORLD.rank == 0:
        print('Iter=%d: some results obtained in FEniCS/FEniCSX' %Iter)
    else:
        pass
    time.sleep(1)

When we run the code in serial, it can give us the result in each iteration immediately:
Iter 1:

Iter=1: some results obtained in FEniCS/FEniCSX

Iter 2:

Iter=2: some results obtained in FEniCS/FEniCSX

Iter 3:

Iter=3: some results obtained in FEniCS/FEniCSX

However, if we run the code in parallel, then it will NOT give us any results until all the calculations are finished. After finishing all the calculations, the results will be given in one shot. Then it is meaningless in terms of observing the progress.

Iter=1: some results obtained in FEniCS/FEniCSX
Iter=2: some results obtained in FEniCS/FEniCSX
Iter=3: some results obtained in FEniCS/FEniCSX

So do we have a way to output the result immediately to the terminal even if we run the code in parallel? I am using VS Code and Docker.

Thanks!

1 Like

Hello @Yingqi_Jia ,

One way to do it would be to flush stdout, either:

  1. Import sys and use sys.stdout.flush() after your print statement

Or, you could just tell the print statement to flush:

  1. print("Hello I am rank",MPI.COMM_WORLD.rank,flush=True)
3 Likes

Hi @timwong,

It is exactly what I want. Thank you very much!