title: How to use prange in cython?
body: <p>I'm trying to solve a 2D-Ising model with Monte Carlo approach.</p>
  
  <p>As it is slow I used Cython to accelerate the code execution. I would like to push it even further and parallelize the Cython code. My idea is to split the 2D-lattice in two, so for any point on a lattice has it's nearest neigbours on the other lattice. This way I can randomly choose one lattice and I can flip all the spins and this could be done in parallel since all those spins are independent.</p>
  
  <p>So far this is my code :<br><sub><em>( inspired from <a href="http://jakevdp.github.io/blog/2017/12/11/live-coding-cython-ising-model/" rel="nofollow noreferrer">http://jakevdp.github.io/blog/2017/12/11/live-coding-cython-ising-model/</a> )</em></sub> </p>
  
  <pre><code>%load_ext Cython
  %%cython 
  cimport cython
  cimport numpy as np
  import numpy as np
  from cython.parallel cimport prange
  
  @cython.boundscheck(False)
  @cython.wraparound(False)
  
  def cy_ising_step(np.int64_t[:, :] field,float beta):
  
      cdef int N = field.shape[0]
      cdef int M = field.shape[1]
  
  
      cdef int offset = np.random.randint(0,2)
  
      cdef np.int64_t[:,] n_update = np.arange(offset,N,2,dtype=np.int64)
  
      cdef int m,n,i,j
  
      for m in prange(M,nogil=True):
          i = m % 2
          for j in range(n_update.shape[0]) :
              n = n_update[j]
  
              cy_spin_flip(field,(n+i) %N,m%M,beta)
  
      return np.array(field,dtype=np.int64)
  
  
  cdef cy_spin_flip(np.int64_t[:, :] field,int n,int m, float beta=0.4,float J=1.0):
  
      cdef int N = field.shape[0]
      cdef int M = field.shape[1]
  
      cdef float dE = 2*J*field[n,m]*(field[(n-1)%N,m]+field[(n+1)%N,m]+field[n,(m-1)%M]+field[n,(m+1)%M])
  
      if dE &lt;= 0 :
          field[n,m] *= -1
  
      elif np.exp(-dE * beta) &gt; np.random.rand():
          field[n,m] *= -1
  </code></pre>
  
  <p>I tried using a <strong><code>prange</code></strong>-constructor but I'm having a lots of troubles with GIL-lock. I'am new to Cython and parallel computing so I could easily have missed something.</p>
  
  <p><strong>The error :</strong></p>
  
  <pre><code>Discarding owned Python object not allowed without gil
  Calling gil-requiring function not allowed without gil
  </code></pre>