我正在使用我使用字典和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 …