title: MPI4Py: OpenMPI How to Update Dictionary across processes?
body: <p>In my scenario, I have an environment that I'm trying to sample from. Each process will sample multiple times from this environment.</p>
  
  <pre class="lang-py prettyprint-override"><code>import numpy as np
  class EnvSim(object):
      @staticmethod
      def get():
          return np.random.randint(0, 2000)
  
  from collections import defaultdict
  class Dict(object):
      def __init__(self):
          self.d = defaultdict(int)
  
      def update(self, key):
          self.d[key] += 1
          print(key)
          data_array = [np.empty(1, dtype=np.int) for _ in range(num_cpu)]
          data_array[proc_id()] = np.array([key], dtype=np.int)
          MPI.COMM_WORLD.Bcast(data_array[proc_id()], root=proc_id())
          for data in data_array:
              self.d[data.tolist()[0]] += 1
  
  </code></pre>
  
  <p>The goal is for each OpenMPI process to share what they've sampled from the environment synchronously or asynchronously. Is <code>Bcast</code> the right method to use here or should I use something else?</p>
  
  <p>This is the main statement that I used to execute my program :( currently this is not working.</p>
  
  <pre class="lang-py prettyprint-override"><code>def mpi_fork(n, bind_to_core=False):
      """
      Re-launches the current script with workers linked by MPI.
  
      Args:
          n (int): Number of process to split into.
  
          bind_to_core (bool): Bind each MPI process to a core.
      """
      if n&lt;=1:
          return
      if os.getenv("IN_MPI") is None:
          env = os.environ.copy()
          env.update(
              MKL_NUM_THREADS="1",
              OMP_NUM_THREADS="1",
              IN_MPI="1"
          )
          args = ["mpirun", "-np", str(n)]
          if bind_to_core:
              args += ["-bind-to", "core"]
          args += [sys.executable] + sys.argv
          subprocess.check_call(args, env=env)
          sys.exit()
  
  if __name__ == '__main__':
      num_cpu = 3
      mpi_fork(num_cpu)
  
      dic = Dict()
  
      for _ in range(3):
          exp = EnvSim.get()
          dic.update(exp)
  
      print(dic.d)
  
  </code></pre>