articles_code / parallel_article / py_multiprocessing /

''' listing 6:

Multiprocessing based code to estimate the value of PI
using monte carlo sampling 
Uses workers:

import random
import multiprocessing
from multiprocessing import Pool

#caculate the number of points in the unit circle
#out of n points
def monte_carlo_pi_part(n):
    count = 0
    for i in range(n):
        # if it is within the unit circle
        if x*x + y*y <= 1:
    return count

if __name__=='__main__':
    np = multiprocessing.cpu_count()
    print 'You have {0:1d} CPUs'.format(np)

    # Nummber of points to use for the Pi estimation
    n = 10000000
    # iterable with a list of points to generate in each worker
    # each worker process gets n/np number of points to calculate Pi from

    part_count=[n/np for i in range(np)]

    #Create the worker pool
    pool = Pool(processes=np)   

    # parallel map, part_count)

    print "Esitmated value of Pi:: ", sum(count)/(n*1.0)*4