0

I have a python function that returns multi-dimensional numpy array as output. It's a Monte Carlo simulation with random numbers and I would like to run this function n times with the same input, collect the data and do the average. Is there a simple way to use multiprocessing to achieve this?

egwene sedai
  • 403
  • 1
  • 4
  • 16

1 Answers1

2

For example, calc average with simple square function using 4 processes:

from multiprocessing import Pool

def f(x):
    return x*x

if __name__ == '__main__':
    pool = Pool(processes=4)
    n = 10000000
    inputs = xrange(n)
    # calc f output using 4 processes
    out_list = pool.map(f, inputs)
    # average  
    print sum(out_list) / float(n)
ndpu
  • 22,225
  • 6
  • 54
  • 69