Python: edit matrix row in parallel
<p>here is my problem:<br>
I would like to define an array of persons and change the entries of this array in a for loop. Since I also would like to see the asymptotics of the resulting distribution, I want to repeat this simulation quiet a lot, thus I'm using a matrix to store the several array in each row. I know how to do this with two for loops:  </p>

<pre><code>import random
import numpy as np

nobs = 100
rep = 10**2
steps = 10**2
dmoney = 1
state = np.matrix([[10] * nobs] * rep)
for i in range(steps):
    for j in range(rep)
        sample = random.sample(range(state.shape[1]),2)
        state[j,sample[0]] = state[j,sample[0]] + dmoney
        state[j,sample[1]] = state[j,sample[1]] - dmoney
</code></pre>

<p>I thought I use the multiprocessing library but I don't know how to do it, because in my simple mind, the workers manipulate the same global matrix in parallel, which I read is not a good idea.<br>
So, how can I do this, to speed up calculations?</p>

<p>Thanks in advance.</p>

<p>OK, so this might not be much use, I haven't profiled it to see if there's a speed-up, but list comprehensions will be a little faster than normal loops anyway.</p>

<pre><code>...
y_ix = np.arange(rep) # create once as same for each loop
for i in range(steps):
    # presumably the two locations in the population to swap need refreshing each loop
    x_ix = np.array([np.random.choice(nobs, 2) for j in range(rep)])
    state[y_ix, x_ix[:,0]] += dmoney
    state[y_ix, x_ix[:,1]] -= dmoney
</code></pre>

<p>PS what numpy splits over multiple processors depends on what libraries have been included when compiled (BLAS etc). You will be able to find info on line about this.</p>

<p>EDIT I can confirm, after comparing the original with the numpy indexed version above, that the original method is faster!</p>

python matrix multiprocessing row