MPI + tqdm-仅从一个进程更新进度条

时间:2019-05-23 09:59:18

标签: python mpi4py tqdm

我有一个脚本,该脚本被设置为与with open('test.txt', 'r') as f: print (f.read()) 一起同时运行多个进程。我正在使用mpiexec通过python管理MPI内容。我也非常喜欢mpi4py作为进度条。问题在于,多个进程可能以各种方式中断tqdm,因为多个实例打印到同一屏幕上。

是否可以告诉tqdm仅在某些情况下打印更新?我正在寻找类似以下的内容:

tqdm

2 个答案:

答案 0 :(得分:0)

是的,您可能正在寻找以下方面的东西:

pbar = tqdm.tqdm(total = len(inputs))
for i in t:
    if something_with_i:
         pbar.update(1)

答案 1 :(得分:0)

这是正确的答案:

  

https://stackoverflow.com/a/36094541/9217178

我投入了2美分:

from tqdm import tqdm
from mpi4py import MPI

communicator = MPI.COMM_WORLD
rank = communicator.Get_rank()
nb_process = communicator.Get_size()
total = 100
compute_tag = 0
end_tag = 99

if rank == 0:
    # rank 0 is the master node to update progress bar
    pbar = tqdm(total=total)
    update_msg = None

else:
    update_msg = None

#******************************************************************* I'm a Barrier
communicator.Barrier()
if rank == 0:

    remaining = nb_process - 1
    while remaining > 0:
        s = MPI.Status()
        communicator.Probe(status=s)
        if s.tag == compute_tag:
            update_msg = communicator.recv(tag=compute_tag)  
            #note: send/recv for basic python instances and Send/Recv for numpy array

            # only master to update pbar
            pbar.update(1)
        elif s.tag == end_tag:
            update_msg = communicator.recv(tag=end_tag)

            # remaining drops to 0, loop ends
            remaining -= -1
            print('remaining: {}', remaining)

else:
    for i in range(10):
        #compute something and send a message to master node (rank 0)
        communicator.send(update_msg, dest=0, tag=compute_tag)

    # when this node ends send a eng_tag message
    communicator.send(update_msg, tag=end_tag)