-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmc.py
58 lines (33 loc) · 1.34 KB
/
mc.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
import multiprocessing,random
from multiprocessing import Pool
#caculate the number of points in the unit circle
#out of n points
def points_in_square_n_cicle(n):
count = 0
for i in range(n):
x=random.random()
y=random.random()
# τα σημεία τα θέλω μέσα στον μοναδιαίο κύκλο
if x**2 + y**2 <= 1:
count+=1
#return
return count
if __name__=='__main__':
p = multiprocessing.cpu_count()
print('Τα CPU που έχουμε είναι', p)
n = 10**8 #το δείγμα
# n = 10**9
#n = 10**2
pi_parts=[] #καθε διεργασία μοιράζεται n/p σημεία
for i in range(p):
pi_parts.append(n//p)
#Φτιαχνω worker pool (The Pool class represents a pool of worker processes.
# It has methods which allows tasks to be offloaded to the worker processes
# in a few different ways.)
#pool = Pool(processes=8)
#pool = Pool(processes=4)
pool = Pool(processes=p) #χρησιμοποιώ και τους 12 logical processors που εχει το πσ μ
# τώρα κάνουμε mapping across processes
s_points=pool.map(points_in_square_n_cicle, pi_parts)
pi_tetarta=sum(s_points)/(n*1.0)
print("Estimated Pi:: ", 4*pi_tetarta)