使用MPI的Allreduce汇总Python对象

kηi*_*ves 5 python parallel-processing dictionary mpi mpi4py

我正在使用我使用字典和Python中的计数器构建的稀疏张量数组操作.我想可以并行使用这个数组操作.最重要的是,我最终在每个节点上都有计数器,我想用MPI.Allreduce(或另一个不错的解决方案)加在一起.例如,使用计数器可以做到这一点

A = Counter({a:1, b:2, c:3})
B = Counter({b:1, c:2, d:3})
Run Code Online (Sandbox Code Playgroud)

这样的

C = A+B = Counter({a:1, b:3, c:5, d:3}).
Run Code Online (Sandbox Code Playgroud)

我想做同样的操作,但所有相关的节点,

MPI.Allreduce(send_counter, recv_counter, MPI.SUM)
Run Code Online (Sandbox Code Playgroud)

但是,MPI似乎没有在字典/计数器上识别此操作,从而引发错误expecting a buffer or a list/tuple.我最好的选择是"用户定义的操作",还是有办法让Allreduce添加计数器?谢谢,

编辑(2015年7月14日):我试图为字典创建用户操作,但存在一些差异.我写了以下内容

def dict_sum(dict1, dict2, datatype):
    for key in dict2:
        try:
            dict1[key] += dict2[key]
        except KeyError:
            dict1[key] = dict2[key]
Run Code Online (Sandbox Code Playgroud)

当我告诉MPI关于我这样做的功能时:

dictSumOp = MPI.Op.Create(dict_sum, commute=True)
Run Code Online (Sandbox Code Playgroud)

在我用它的代码中

the_result = comm.allreduce(mydict, dictSumOp)
Run Code Online (Sandbox Code Playgroud)

然而,它扔了unsupported operand '+' for type dict.所以我写了

the_result = comm.allreduce(mydict, op=dictSumOp)
Run Code Online (Sandbox Code Playgroud)

现在它dict1[key] += dict2[key] TypeError: 'NoneType' object has no attribute '__getitem__' 显然它想要知道那些东西是字典吗?我如何告诉它他们确实有类型字典?

Jon*_*rsi 7

MPI和MPI4py都不知道有关计数器的任何信息,所以你需要创建自己的还原操作才能使用它; 对于任何其他类型的python对象,这都是相同的:

#!/usr/bin/env python
from mpi4py import MPI
import collections

def addCounter(counter1, counter2, datatype):
    for item in counter2:
        counter1[item] += counter2[item]
    return counter1

if __name__=="__main__":

    comm = MPI.COMM_WORLD

    if comm.rank == 0:
        myCounter = collections.Counter({'a':1, 'b':2, 'c':3})
    else:
        myCounter = collections.Counter({'b':1, 'c':2, 'd':3})


    counterSumOp = MPI.Op.Create(addCounter, commute=True)

    totcounter = comm.allreduce(myCounter, op=counterSumOp)
    print comm.rank, totcounter
Run Code Online (Sandbox Code Playgroud)

这里我们采用了一个函数,该函数对两个计数器对象进行求和,并用MPI.Op.Create创建了一个MPI运算符.mpi4py将取消对象,运行此函数以成对组合这些项目,然后挑选部分结果并将其发送到下一个任务.

还要注意我们正在使用(小写)allreduce,它适用于任意python对象,而不是(大写)Allreduce,它适用于numpy数组或它们的道德等价物(缓冲区,映射到MPI API的Fortran/C数组)是设计上的).

跑步给出:

$ mpirun -np 2 python ./counter_reduce.py 
0 Counter({'c': 5, 'b': 3, 'd': 3, 'a': 1})
1 Counter({'c': 5, 'b': 3, 'd': 3, 'a': 1})

$ mpirun -np 4 python ./counter_reduce.py 
0 Counter({'c': 9, 'd': 9, 'b': 5, 'a': 1})
2 Counter({'c': 9, 'd': 9, 'b': 5, 'a': 1})
1 Counter({'c': 9, 'd': 9, 'b': 5, 'a': 1})
3 Counter({'c': 9, 'd': 9, 'b': 5, 'a': 1})
Run Code Online (Sandbox Code Playgroud)

只有适度的更改才能使用通用字典:

#!/usr/bin/env python
from mpi4py import MPI

def addCounter(counter1, counter2, datatype):
    for item in counter2:
        if item in counter1:
            counter1[item] += counter2[item]
        else:
            counter1[item] = counter2[item]
    return counter1

if __name__=="__main__":

    comm = MPI.COMM_WORLD

    if comm.rank == 0:
        myDict = {'a':1, 'c':"Hello "}
    else:
        myDict = {'c':"World!", 'd':3}

    counterSumOp = MPI.Op.Create(addCounter, commute=True)

    totDict = comm.allreduce(myDict, op=counterSumOp)
    print comm.rank, totDict
Run Code Online (Sandbox Code Playgroud)

跑步给予

$ mpirun -np 2 python dict_reduce.py 
0 {'a': 1, 'c': 'Hello World!', 'd': 3}
1 {'a': 1, 'c': 'Hello World!', 'd': 3}
Run Code Online (Sandbox Code Playgroud)