-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparallel.py
More file actions
70 lines (56 loc) · 1.25 KB
/
parallel.py
File metadata and controls
70 lines (56 loc) · 1.25 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
'''
Documentation, License etc.
@package parallel
'''
import sys
import numpy as np
import matplotlib.pyplot as plt
import time
from math import sqrt
from joblib import Parallel, delayed
from joblib.pool import has_shareable_memory
ncpus = int(sys.argv[1])
def square(x):
return sqrt(x)
def rndarray(n):
t = np.arange(n)
a = np.random.rand(n)
return t, a
def sinarray(n,f):
t = np.arange(n)
a = np.sin(f*t)
return t, a
def FFT(t,a):
b = np.fft.fft(a)
f = np.fft.fftfreq(t.shape[-1])
return f, b
def aaf(n,f):
t = np.arange(n)
a = np.sin(f*t)
b = np.fft.fft(a)
f = np.fft.fftfreq(t.shape[-1])
return f, b
if __name__ == "__main__":
n = 100000
l = []
type = "parallel"
#type = "serial"
if type=="serial":
starttime = time.time()
print type
for v in np.arange(0.1,20.0,0.1):
t, a = sinarray(n, v)
f, b = FFT(t,a)
l.append((f, b))
f, b = l[0]
endtime = time.time()
print(endtime-starttime)
elif type=="parallel":
print type
starttime = time.time()
l = Parallel(n_jobs=ncpus)(delayed(aaf)(n,v) for v in np.arange(0.1,100.0,0.1))
f, b = l[0]
endtime = time.time()
print(endtime-starttime)
# plt.plot(f, b.real, f, b.imag)
# plt.show()