Question on Multiprocessing Performance
Background:
I have a function that is essentially a very large for-loop. Inside each iteration of the loop there are several calls to jitted functions, some of which are calling other jitted functions (with numba). The performance boost has been amazing with numba, but I want to take advantage of multiprocessing to get that last bit of speed.
I am using pool.map to parallelize. It works as expected and yields the same results as the for-loop style, so I think I am implementing it correctly.
Here is what's confusing to me:
I would expect that since I have 8 cores, turning this loop into a pool.map would produce roughly an 8x speedup. In reality I'm only getting a 40-50% speedup.
The grainy details of how jitting and parallel processing work are honestly still above my level of knowledge. The possible root causes for these unexpected results I have been able to think of are:
- Lots of overhead being incurred from going parallel in relation to what my code is actually doing
- Some kind of side effect of using numba everywhere
- My code is inefficient
I have considered using Cython to compile ahead of time instead of compiling just in time with numba, but I don't know if all that work would be worth it (I haven't used Cython much outside of simple stuff), or if it would even provide any benefit.
Actual question:
Is my assumption incorrect that multiprocessing always yields a speed increase proportional to the number of cores being used?
Edit: figured it might be relevant to say that the chunksize I am using is int( #iterations / #cores )
Posted Mar 8, 2020, 12:21 AM UTC · edited
Going parallel requires a constant amount of setup time which is probably what's causing your speedup to be less than 8x. Remember that the 8x speedup is your maximum possible speedup, not your guaranteed speedup. There's something called Amdahl's law which basically says that your code is both serial and parallel. Any setup involved (like reading from a file) is serial, and there's no way to speed that up according to Amdahl's law.
One thing you can do is to compare your parallelized code with the unparallelized code on a larger input size and see if you get a proportionally larger speedup. This would help show that there's a constant-time unparallelizable part in your code.
That makes sense, I thought I was taking this into consideration by timing only the function in question with:
But I will look into Amdahl's law and compare very large inputs to see if maybe I am including things that cannot be parallelized. Thank you for your suggestions.