【问题标题】:Summing Python Objects with MPI's Allreduce使用 MPI 的 Allreduce 对 Python 对象求和
【发布时间】:2015-10-02 00:26:19
【问题描述】:

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

A = Counter({a:1, b:2, c:3})
B = Counter({b:1, c:2, d:3})

这样

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

我想对所有相关节点进行同样的操作,

MPI.Allreduce(send_counter, recv_counter, MPI.SUM)

但是,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]

当我告诉 MPI 函数时,我这样做了:

dictSumOp = MPI.Op.Create(dict_sum, commute=True)

在代码中我将其用作

the_result = comm.allreduce(mydict, dictSumOp)

但是,它抛出了unsupported operand '+' for type dict。所以我写了

the_result = comm.allreduce(mydict, op=dictSumOp)

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

【问题讨论】:

  • 你能把操作变成列表推导式吗?

标签: python dictionary parallel-processing mpi mpi4py


【解决方案1】:

MPI 和 MPI4py 都不知道特别是 Counters 的任何内容,因此您需要创建自己的归约操作才能使其工作;这对于任何其他类型的 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

这里我们采用了一个函数,它将两个计数器对象相加,并使用 MPI.Op.Create 从它们中创建了一个 MPI 运算符; mpi4py 将 unpickle 对象,运行此函数以成对组合这些项目,然后 pickle 部分结果并将其发送到下一个任务。

还要注意,我们使用的是(小写)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})

只需进行适度的更改即可使用通用字典:

#!/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

跑步给予

$ mpirun -np 2 python dict_reduce.py 
0 {'a': 1, 'c': 'Hello World!', 'd': 3}
1 {'a': 1, 'c': 'Hello World!', 'd': 3}

【讨论】:

  • 我看不到 `datatype' 是如何使用的。
  • 不是;它是 MPI API 的一部分,但它并不容易与 mpi4py 的 Python 对象酸洗一起有意义地使用。原则上,您可以使用它对不同的 MPI 数据类型执行不同的操作。
  • 有新的进展,请看上面的编辑,谢谢你到目前为止的帮助。
  • @kηives - 您的更新现在是一个完全独立的问题;一种或另一种方式,您没有将字典(计数器,这是您的问题所在)传递给函数,我们无法在没有看到代码的情况下诊断为什么会这样。当您运行我提供的示例时,它有效吗?
  • 您的代码按照编写的方式工作,但如果我编写totcounter = comm.allreduce(myCounter, op=counterSumOp),它会引发相同的NoneType 错误。您确定您的代码使用的是您创建的函数而不是 op=SUM 默认值吗?
猜你喜欢
  • 2020-09-07
  • 2015-11-08
  • 2013-12-03
  • 1970-01-01
  • 2022-01-01
  • 1970-01-01
  • 2011-12-31
  • 2014-07-28
  • 1970-01-01
相关资源
最近更新 更多