title: Multiprocessing.Pool makes Numpy matrix multiplication slower
body: <p>So, I am playing around with <code>multiprocessing.Pool</code> and <code>Numpy</code>, but it seems I missed some important point. Why is the <code>pool</code> version much slower? I looked at <code>htop</code> and I can see several processes be created, but they all share one of the CPUs adding up to ~100%. </p>
  
  <pre><code>$ cat test_multi.py 
  import numpy as np
  from timeit import timeit
  from multiprocessing import Pool
  
  
  def mmul(matrix):
      for i in range(100):
          matrix = matrix * matrix
      return matrix
  
  if __name__ == '__main__':
      matrices = []
      for i in range(4):
          matrices.append(np.random.random_integers(100, size=(1000, 1000)))
  
      pool = Pool(8)
      print timeit(lambda: map(mmul, matrices), number=20)
      print timeit(lambda: pool.map(mmul, matrices), number=20)
  
  $ python test_multi.py 
  16.0265390873
  19.097837925
  </code></pre>
  
  <p><strong>[update]</strong> </p>
  
  <ul>
  <li>changed to <code>timeit</code> for benchmarking processes</li>
  <li>init Pool with a number of my cores</li>
  <li>changed computation so that there is more computation and less memory transfer (I hope)</li>
  </ul>
  
  <p>Still no change. <code>pool</code> version is still slower and I can see in <code>htop</code> that only one core is used also several processes are spawned.</p>
  
  <p><strong>[update2]</strong></p>
  
  <p>At the moment I am reading about @Jan-Philip Gehrcke's suggestion to use <code>multiprocessing.Process()</code> and <code>Queue</code>. But in the meantime I would like to know:</p>
  
  <ol>
  <li>Why does my example work for tiago? What could be the reason it is not working on my machine<a href="https://stackoverflow.com/questions/15414027/multiprocessing-pool-makes-numpy-matrix-multiplication-slower#comment22155148_15414027">1</a>? </li>
  <li>Is in my example code any copying between the processes? I intended my code to give each thread one matrix of the matrices list.</li>
  <li>Is my code a bad example, because I use <code>Numpy</code>?</li>
  </ol>
  
  <p>I learned that often one gets better answer, when the others know my end goal so: I have a lot of files, which are atm loaded and processed in a serial fashion. The processing is CPU intense, so I assume much could be gained by parallelization. My aim is it to call the python function that analyses a file in parallel. Furthermore this function is just an interface to C code, I assume, that makes a difference. </p>
  
  <p><a href="https://stackoverflow.com/questions/15414027/multiprocessing-pool-makes-numpy-matrix-multiplication-slower#comment22155148_15414027">1</a> Ubuntu 12.04, Python 2.7.3, i7 860 @ 2.80 -  Please leave a comment if you need more info.</p>
  
  <p><strong>[update3]</strong></p>
  
  <p>Here are the results from Stefano's example code. For some reason there is no speed up. :/</p>
  
  <pre><code>testing with 16 matrices
  base  4.27
     1  5.07
     2  4.76
     4  4.71
     8  4.78
    16  4.79
  testing with 32 matrices
  base  8.82
     1 10.39
     2 10.58
     4 10.73
     8  9.46
    16  9.54
  testing with 64 matrices
  base 17.38
     1 19.34
     2 19.62
     4 19.59
     8 19.39
    16 19.34
  </code></pre>
  
  <p><strong>[update 4] answer to <a href="https://stackoverflow.com/questions/15414027/multiprocessing-pool-makes-numpy-matrix-multiplication-slower#comment22155148_15414027">Jan-Philip Gehrcke's comment</a></strong></p>
  
  <p>Sorry that I haven't made myself clearer. As I wrote in Update 2 my main goal is it to parallelize many serial calls of a 3rd party Python library function. This function is an interface to some C code. I was recommended to use <code>Pool</code>, but this didn't work, so I tried something simpler, the shown above example with <code>numpy</code>. But also there I could not achieve a performance improvement, even though it looks for me 'emberassing parallelizable`. So I assume I must have missed something important. This information is what I am looking for with this question and bounty.</p>
  
  <p><strong>[update 5]</strong></p>
  
  <p>Thanks for all your tremendous input. But reading through your answers only creates more questions for me. For that reason I will read about the <a href="http://www.scipy.org/ParallelProgramming" rel="noreferrer">basics</a> and create new SO questions when I have a clearer understanding of what I don't know.</p>