code: def mpi_weighted_mean(comm, local_name2valcount):
      
      all_name2valcount = comm.gather(local_name2valcount)
      if comm.rank == 0:
          name2sum = defaultdict(float)
          name2count = defaultdict(float)
          for n2vc in all_name2valcount:
              for (name, (val, count)) in n2vc.items():
                  try:
                      val = float(val)
                  except ValueError:
                      if comm.rank == 0:
                          warnings.warn('WARNING: tried to compute mean on non-float {}={}'.format(name, val))
                  else:
                      name2sum[name] += val * count
                      name2count[name] += count
          return {name : name2sum[name] / name2count[name] for name in name2sum}
      else:
          return {}
docs: Perform a weighted average over dicts that are each on a different node
      Input: local_name2valcount: dict mapping key -> (value, count)
      Returns: key -> mean