-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmap-reduce_calcR.py
89 lines (41 loc) · 1.49 KB
/
map-reduce_calcR.py
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#coding=utf-8
import random
import multiprocessing
from multiprocessing import Process
class MapReduce(object):
def __init__(self, map_func, reduce_func, workers_num=None):
self.map_func = map_func
self.reduce_func = reduce_func
self.workers_num = workers_num
if not workers_num:
workers_num = multiprocessing.cpu_count()*2
self.pool = multiprocessing.Pool(workers_num)
def __call__(self, inputs):
map_result = self.pool.map(self.map_func, inputs)
reduce_result = self.reduce_func(map_result)
return reduce_result
def calculator(*args):
print multiprocessing.current_process().name,' processing'
points, circle_round = args[0]
points_in_circle = 0
for i in range(points):
# 这里其实只取了1/4圆
x = random.random()*circle_round
y = random.random()*circle_round
if (x**2 + y**2) < circle_round**2:
points_in_circle += 1
return points_in_circle
def count_circle_points(points_list):
return sum(points_list)
if __name__ == '__main__':
# 半径
CIRCLE_ROUND = 10
# 总点数
POINTS = 10000000
# 总进程数
WORKERS_NUM = 10
map_reduce = MapReduce(calculator, count_circle_points, WORKERS_NUM)
inputs = [(POINTS/WORKERS_NUM, CIRCLE_ROUND)] * WORKERS_NUM
all_points_in_circle = map_reduce(inputs)
ac_as = float(all_points_in_circle)/POINTS
print 'pi approach to:%7f'%(4*ac_as)