def dict_gather(comm, d, op='<STR_LIT>', assert_all_have_data=True):
if comm is None: return d<EOL>alldicts = comm.allgather(d)<EOL>size = comm.size<EOL>k2li = defaultdict(list)<EOL>for d in alldicts:<EOL><INDENT>for (k,v) in d.items():<EOL><INDENT>k2li[k].append(v)<EOL><DEDENT><DEDENT>result = {}<EOL>for (k,li) in k2li.items():<EOL><INDENT>if assert_all_have_data:<EOL><INDENT>assert len(li)==size, "<STR_LIT>" % (len(li), size, k)<EOL><DEDENT>if op=='<STR_LIT>':<EOL><INDENT>result[k] = np.mean(li, axis=<NUM_LIT:0>)<EOL><DEDENT>elif op=='<STR_LIT>':<EOL><INDENT>result[k] = np.sum(li, axis=<NUM_LIT:0>)<EOL><DEDENT>else:<EOL><INDENT>assert <NUM_LIT:0>, op<EOL><DEDENT><DEDENT>return result<EOL>
Perform a reduction operation over dicts