Monday, February 6, 2012
*Must* use parfor!
I was recently implementing a Monte Carlo simulation with 50000 particles for estimating a probability density function in which took me 1100 seconds of runtime. Using Matlab's profiling tool, I found that 99% of the time was being spent on generating the new set of 50000 sample points from the old sample points. I immediately switched to a parallel looping strategy with 8 workers in parallel. The new runtime dropped to 80 seconds. Next, I found that I was calling a ``coinflip'' function in every loop. I removed it from the loop and instead stored a sequence of coinflips in memory using Matlab's binornd function and simply accessed this vector of ``already flipped coins'' in the parfor. The final runtime was 40 seconds.