-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprogram.py
More file actions
52 lines (36 loc) · 1.22 KB
/
Copy pathprogram.py
File metadata and controls
52 lines (36 loc) · 1.22 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
import numpy as np
from time import time
def f(x):
x2 = x * x
return np.cos(x2) * np.exp(-x2 / 2) / np.sqrt((2 * np.pi))
def methode(arg):
# Gauss-Legendre three-point formula
a, b, n, s = arg
wi = (b - a) / n
xi = np.linspace(a, b, n + 1)
mid = (xi[:-1] + xi[1:]) * 0.5
p = (wi * np.sqrt(3)) / (2 * np.sqrt(5)) # p for points
return (wi / 18) * np.sum(5 * f(mid - p) + 8 * f(mid) + 5 * f(mid + p))
def pool_liste(arg, np_cpu):
A, B, N, seed = arg
pool_N = N // np_cpu
rest = N % np_cpu
pr = np.linspace(A, B, np_cpu + 1) # pool range
liste = [(pr[i], pr[i + 1], pool_N, seed + i) for i in range(np_cpu)]
liste.append((pr[np_cpu - 1], pr[np_cpu], pool_N + rest, seed + np_cpu))
return liste
if __name__ == "__main__":
import multiprocessing as mp
N = 10**7
A = -np.sqrt(np.pi / 2)
B = np.sqrt(np.pi / 2)
seed = int(time())
arg = (A, B, N, seed)
tmp = time()
print(methode((arg)))
print(time() - tmp, "without parallelization")
liste = pool_liste(arg, mp.cpu_count())
tmp = time()
with mp.Pool(processes=mp.cpu_count()) as pool:
print(sum(pool.imap(methode, liste)))
print(time() - tmp, "with parallelization")