title: How to create a shared 2D array in python multiprocessing
body: <p>I need to process a large matrix using python's multiprocessing. That said I need <strong><em>a 2D array</em></strong>. That array should be shared/accessed/updated by different child processes in Synchronous manner. (Therefore I use <code>mp.Manager</code>).</p>
  
  <p>On creating an array, <code>multiprocess.Array()</code> let me create a 1D array. I tried to convert the "Shared 1D array" into a 2D numpy array using <code>np.frombuffer()</code> (see the <code>CreateArray</code> function).</p>
  
  <p>In the function,<code>CreateArray(3,4)</code> command creates a shared array of length 12 (=3x4). However the <code>arr = np.frombuffer(mp_arr.get_obj())</code> command creates a numpy array of length 6!!!. Eventually <code>b = arr.reshape((n, m))</code> command fails to convert the array of size 6 into 3x4 matrix.</p>
  
  <p>How to fix this and get a <em>2D shared</em> array?</p>
  
  <p><strong>EDIT:</strong>
  With the correction the code creases a 2D array (thanks to GabrielC). But it looks the array is not shared. The function <code>addData(array)</code> assigns some values in the matrix. But when printed from the <code>main</code> I get only zeros.</p>
  
  <p><strong><em>Problem:</em></strong> How to fix this and get a 2D <em>shared</em> array?</p>
  
  <pre><code>import multiprocessing as mp
  import numpy as np
  import ctypes as c
  
  def CreateArray(n,m):
      mp_arr=mp.Array('i',n*m)
  #    arr = np.frombuffer(mp_arr.get_obj())  #This command must be corrected. Thanks to GabrielC
      arr = np.frombuffer(mp_arr.get_obj(),c.c_int)  # mp_arr and arr share the same memory
      # make it two-dimensional
      print('np array len=',len(arr))
      b = arr.reshape((n, m))  # b and arr share the same memory
      return b
  
  def addData(array):
      n,m=np.shape(array)
      i=0
      for nn in range(n):
          for mm in range(m):
              array[nn][mm]=i
              i=i+1
      print(array)
  
  if __name__=='__main__':
      with mp.Manager() as manager:
          Myarray=CreateArray(3,4)
          p1=mp.Process(target=addData,args=(Myarray,))
          p1.start()
          p1.join()
          print(Myarray)
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/i1JKu.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/i1JKu.png" alt="enter image description here"></a></p>