I am using a sparse tensor array manipulation I built using dictionaries and Counters in Python. I would like to make it possible to use this array manipulation in parallel. The bottom line is that I have ended up having Counters on each node which I would like to add together using MPI.Allreduce (or another nice solution). For instance with Counters one can do this
A = Counter({a:1, b:2, c:3})
B = Counter({b:1, c:2, d:3})
such that
C = A+B = Counter({a:1, b:3, c:5, d:3}).
I would like to do this same operation but with all the relevant nodes,
MPI.Allreduce(send_counter, recv_counter, MPI.SUM)
however, MPI doesn't seem to recognize this operation on dictionaries/Counters, throwing an error expecting a buffer or a list/tuple
. Is my best option a `User-Defined Operation,' or is there a way to get Allreduce to add Counters? Thanks,
EDIT (7/14/15): I have attempted to create a user operation for dictionaries but there have been some discrepancies. I wrote the following
def dict_sum(dict1, dict2, datatype):
for key in dict2:
try:
dict1[key] += dict2[key]
except KeyError:
dict1[key] = dict2[key]
and when I told MPI about the function I did this:
dictSumOp = MPI.Op.Create(dict_sum, commute=True)
and in the code I used it as
the_result = comm.allreduce(mydict, dictSumOp)
However, it threw unsupported operand '+' for type dict
. so I wrote
the_result = comm.allreduce(mydict, op=dictSumOp)
and now it throws dict1[key] += dict2[key]
TypeError: 'NoneType' object has no attribute '__getitem__'
so apparently
it wants to know those things are dictionaries? How do I tell it they do have type dictionary?