0

I'm trying to parallelize a simulation that is replicated many times using pathos.multiprocessing. However, the map function keeps returning the same value for each iteration. Perhaps my understanding of map is incorrect? This is what my code looks like:

def simulate(_):
    # simulation occurs here
    return result

reps = 10

# without parallelization
serial_result = []
for _ in range(reps):
    result.append(simulate())
print(result) 
# prints [5.6, 5.8, 4.2, 5.6, 5.8, 5.0, 12.4, 5.8, 4.4, 4.6]

# with parallelization
from pathos.multiprocessing import ProcessingPool as Pool
pool = Pool(10)
parallel_result = pool.map(simulate, range(reps))
print(parallel_result) 
# prints [4.0, 4.0, 4.0, 4.0, 4.0, 4.0, 4.0, 4.0, 4.0, 4.0] 
hoffee
  • 470
  • 3
  • 16
  • 1
    I've had the same issue myself... `multprocessing` reuses the same random seed, you have to make sure the seed is different for each process. – Mike McKerns Jul 19 '19 at 19:27
  • I see. Adding `np.random.seed()` to my `simulate` function seems to have fixed my issue. I suppose using the replication number as the seed would still allow me to reproduce my results? – hoffee Jul 19 '19 at 19:41
  • Yes, you should still be able to seed your calculations. Note that there's both a numpy random seed and a python random seed. – Mike McKerns Jul 19 '19 at 22:26

0 Answers0