forked from kjpolak/python-paralellization-experiments
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmrpi.py
More file actions
executable file
·41 lines (31 loc) · 1.15 KB
/
mrpi.py
File metadata and controls
executable file
·41 lines (31 loc) · 1.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#!/usr/bin/env python
from random import random
from math import pi
from multiprocessing import Pool
from time import time
# Assume radius equals 1
num_of_points = 1000000
num_of_workers = 2
real_number_of_points = num_of_points/num_of_workers*num_of_workers
def get_point():
return (random(), random())
def in_circle(point):
return 1 >= point[0]**2 + point[1]**2
def generate_points(x):
return [get_point() for i in xrange(x)]
def get_those_in_circle(x):
return filter(in_circle, x)
if __name__ == "__main__":
workers = Pool(processes=num_of_workers)
start = time()
points = workers.map(generate_points,
(real_number_of_points/num_of_workers
for i in xrange(num_of_workers)))
points_in_circle = workers.map(get_those_in_circle, points)
num_of_points_in_circle = workers.map(len, points_in_circle)
num_of_points_in_circle = reduce(lambda x,y: x+y, num_of_points_in_circle)
mypi = float(num_of_points_in_circle) / float(real_number_of_points) * 4.0
end = time()
print "calculated value of pi: %f" % mypi
print "done in %f seconds" % (end-start)
print "pi value from math lib: %f" % pi