【问题标题】:MPI + tqdm - updating progressbar from one process onlyMPI + tqdm - 仅从一个进程更新进度条
【发布时间】:2019-05-23 09:59:18
【问题描述】:

我有一个脚本,它设置为使用mpiexec 同时运行多个进程。我正在使用mpi4py 来管理来自 python 的 MPI 内容。我也很喜欢tqdm 的进度条。问题是多个进程可以以各种方式破坏tqdm,因为多个实例打印到同一个屏幕。

有没有办法告诉tqdm 只在某些情况下打印更新?我正在寻找类似以下的内容:

from mpi4py import MPI
from tqdm import trange
from time import sleep

t = trange(10)
for i in t:
    sleep(0.5)
    if MPI.COMM_WORLD.rank == 0:
        t.update_progress()  # <--- Not working, but I want this.

【问题讨论】:

    标签: python mpi4py tqdm


    【解决方案1】:

    是的,您可能正在寻找以下内容:

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

    【讨论】:

    • pbar不会被mpi4py的子进程继承。
    【解决方案2】:

    正确答案如下:

    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)
    

    【讨论】:

      猜你喜欢
      • 2021-07-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-07-28
      • 1970-01-01
      相关资源
      最近更新 更多