MPI 广播和仅传递广播数据作为输入有什么区别?

问题描述 投票:0回答:1

我试图更深入地了解

comm.bcast
方法在
mpi4py
中的工作原理。 据我了解,它将数据广播到所有进程。

但是,在偶然的实验中,我发现如果我将数据作为输入传递到方法中,它仍然会进行广播,并且不会改变。

例如,无论我是否调用

comm.bcast
,下面的代码都会按预期执行:


input_data = [['a','f','d'], ['b','c', 'd']] 
def process_chunk(include_list, chunk): 
    print('i am processing chunk ', chunk) 
    return [x for x in chunk if x in include_list] 

def process_data(input_data): 
    from mpi4py import MPI 
    comm = MPI.COMM_WORLD 
    size = comm.Get_size() 
    rank = comm.Get_rank() 

    data = None 
    if rank == 0: 
        data = input_data 
        print('we will be scattering:', data)   

    include_list = ['a','d','f'] 
    ### if I comment out the next line, nothing changes 
    #include_list = comm.bcast(include_list, root=0) 
    data = comm.scatter(data, root=0) 
    data = process_chunk(include_list, data) 
    newData = comm.gather(data, root=0)   

    if rank == 0: 
        print('master:',newData)   
    return   

process_data(input_data) 

我使用命令

mpirun -n 2 python3 process_chunk.py
运行上述代码(其中
process_chunk.py
是包含上述代码的文件),并得到以下输出:

we will be scattering: [['a', 'f', 'd'], ['b', 'c', 'd']]
i am processing chunk  ['a', 'f', 'd']
i am processing chunk  ['b', 'c', 'd']
master: [['a', 'f', 'd'], ['d']]

我的问题是:如果我可以将必要的信息(在本例中为

include_list
)作为特定方法的输入传递,并且所有级别似乎都收到它,那么
comm.bcast
有什么用?

python-3.x mpi4py
1个回答
0
投票

如果您正在根处理器上执行任何操作,例如读取操作,并且您想要广播到所有处理器,那么它很方便。如今,使用 MPI-2 版本,您可以进行并行读写操作,在这种情况下,comm.bcast 就变得过时了。他们获得了新版本以实现向后兼容性。

© www.soinside.com 2019 - 2024. All rights reserved.