Skip to content

Instantly share code, notes, and snippets.

@nmaas87
Forked from amitsaha/pi_mp.py
Last active December 26, 2016 15:20
Show Gist options
  • Select an option

  • Save nmaas87/941b6934b51c90f462172ed63718b602 to your computer and use it in GitHub Desktop.

Select an option

Save nmaas87/941b6934b51c90f462172ed63718b602 to your computer and use it in GitHub Desktop.

Revisions

  1. nmaas87 revised this gist Dec 26, 2016. 1 changed file with 9 additions and 6 deletions.
    15 changes: 9 additions & 6 deletions pi_mp.py
    Original file line number Diff line number Diff line change
    @@ -10,7 +10,7 @@
    import random
    import multiprocessing
    from multiprocessing import Pool

    import timeit

    #caculate the number of points in the unit circle
    #out of n points
    @@ -28,10 +28,8 @@ def monte_carlo_pi_part(n):
    #return
    return count


    if __name__=='__main__':

    np = multiprocessing.cpu_count()
    def calc():
    np = multiprocessing.cpu_count()
    print 'You have {0:1d} CPUs'.format(np)

    # Nummber of points to use for the Pi estimation
    @@ -49,4 +47,9 @@ def monte_carlo_pi_part(n):
    # parallel map
    count=pool.map(monte_carlo_pi_part, part_count)

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

    if __name__=='__main__':
    time = timeit.Timer ("calc()","from __main__ import calc, monte_carlo_pi_part")
    print(time.timeit(1))

  2. @amitsaha amitsaha created this gist Mar 14, 2012.
    52 changes: 52 additions & 0 deletions pi_mp.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,52 @@
    ''' listing 6: pi_mp.py
    Multiprocessing based code to estimate the value of PI
    using monte carlo sampling
    Ref: http://math.fullerton.edu/mathews/n2003/montecarlopimod.html
    Uses workers:
    http://docs.python.org/library/multiprocessing.html#module-multiprocessing.pool
    '''

    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):
    x=random.random()
    y=random.random()

    # if it is within the unit circle
    if x*x + y*y <= 1:
    count=count+1

    #return
    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
    # http://docs.python.org/library/multiprocessing.html#module-multiprocessing.pool
    pool = Pool(processes=np)

    # parallel map
    count=pool.map(monte_carlo_pi_part, part_count)

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