- Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathscript.py
More file actions
Latest commit
165 lines (135 loc) · 5.13 KB
/
Copy pathscript.py
File metadata and controls
165 lines (135 loc) · 5.13 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
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
importbuild.__simple_cpp_pythonaswow
importgetopt
importmath
importoperator
importtime
importsys
importmatplotlib.pyplotasplt
frompathosimportmultiprocessing
fromfunctoolsimportreduce
fromcollectionsimportdefaultdict
frompandasimportDataFrame
defpython_inner_loop(M):
# computes pi/8 as a series
returnreduce(operator.add, [1/ ((4*k+1) * (4*k+3)) forkinrange(M)], 0)
defpython_outer_loop(xf, N, M):
# computes the series of cos(n * theta) + sin(n * theta)
pool=multiprocessing.Pool()
v=pool.map(lambdan: math.cos((n+1) *xf(M)) +math.sin((n+1) *xf(M)), [nforninrange(N)])
returnreduce(operator.add, v, 0)
deffull_python(N, M):
returnpython_outer_loop(python_inner_loop, N, M)
defmixed_op(pool, lr, n, M):
lr[n] =pool.apply_async(lambda : python_inner_loop(M))
defmixed_get(lr, n):
returnlr[n].get()
if__name__=="__main__":
try:
options, remainder=getopt.getopt(sys.argv[1:],
"x:n:",
["xarg=", "narg="])
exceptgetopt.GetoptErroraserr:
# print help information and exit:
print(err) # will print something like "option -a not recognized"
usage()
sys.exit(2)
N=1000
foropt, arginoptions:
ifoptin ('-n', '--narg'):
n=int(arg)
start=time.time()
r=full_python(N, 10*N)
end=time.time()
print('full python result: '+str(r) +' in '+str(end-start) +' seconds')
start=time.time()
r=wow.full_cpp(N, 10*N)
end=time.time()
print('full c++ result: '+str(r) +' in '+str(end-start) +' seconds')
start=time.time()
r=wow.cpp_outer_loop_gil(lambdaM: python_inner_loop(M), N, 10*N)
end=time.time()
print('mixed result (GIL): '+str(r) +' in '+str(end-start) +' seconds')
start=time.time()
pool=multiprocessing.Pool()
lr= [None]*N
r=wow.cpp_outer_loop_process(lambdan, M : mixed_op(pool, lr, n, M), lambdan : mixed_get(lr, n), N, 10*N)
pool.close()
end=time.time()
print('mixed result (process): '+str(r) +' in '+str(end-start) +' seconds')
x=math.pi/8
r=0.5* ((1.0/math.tan(x/2)) -1+ (math.sin(x* (N+0.5)) -math.cos(x* (N+0.5))) /math.sin(x/2))
print('exact result: '+str(r))
# Statistical analysis
print('Statistic analysis...')
N=1000
d=defaultdict(list)
index= [100, 1000, 10000, 100000, 1000000]
forMinindex:
print("N={0}, M={1}".format(N, M))
start=time.time()
full_python(N, M)
end=time.time()
ref=end-start
d['full_python'].append(1.0)
print(' full python:', str(end-start))
start=time.time()
wow.cpp_outer_loop_gil(lambdam: python_inner_loop(m), N, M)
end=time.time()
d['mixed_gil'].append(ref/ (end-start))
print(' mixed gil:', str(end-start))
start=time.time()
pool=multiprocessing.Pool()
lr= [None]*N
wow.cpp_outer_loop_process(lambdan, m : mixed_op(pool, lr, n, m), lambdan : mixed_get(lr, n), N, M)
pool.close()
end=time.time()
d['mixed_process'].append(ref/ (end-start))
print(' mixed process:', str(end-start))
start=time.time()
wow.full_cpp(N, M)
end=time.time()
d['full_cpp'].append(ref/ (end-start))
print(' full cpp:', str(end-start))
df=DataFrame(data=d, index=index)
df.plot.bar(rot=0, logy=True)
plt.xlabel('M')
plt.ylabel('(log) speedup compared to full python')
plt.axhline(y=1, linewidth=3, color='gray', linestyle='dashed')
plt.savefig('comparison_M.png', transparent=True)
plt.show()
M=10000
d=defaultdict(list)
index= [10, 100, 1000, 10000, 100000]
forNinindex:
print("N={0}, M={1}".format(N, M))
start=time.time()
full_python(N, M)
end=time.time()
ref=end-start
d['full_python'].append(1.0)
print(' full python:', str(end-start))
start=time.time()
wow.cpp_outer_loop_gil(lambdam: python_inner_loop(m), N, M)
end=time.time()
d['mixed_gil'].append(ref/ (end-start))
print(' mixed gil:', str(end-start))
start=time.time()
pool=multiprocessing.Pool()
lr= [None]*N
wow.cpp_outer_loop_process(lambdan, m : mixed_op(pool, lr, n, m), lambdan : mixed_get(lr, n), N, M)
pool.close()
end=time.time()
d['mixed_process'].append(ref/ (end-start))
print(' mixed process:', str(end-start))
start=time.time()
wow.full_cpp(N, M)
end=time.time()
d['full_cpp'].append(ref/ (end-start))
print(' full cpp:', str(end-start))
df=DataFrame(data=d, index=index)
df.plot.bar(rot=0, logy=True)
plt.xlabel('N')
plt.ylabel('(log) speedup compared to full python')
plt.axhline(y=1, linewidth=3, color='gray', linestyle='dashed')
plt.savefig('comparison_N.png', transparent=True)
plt.show()