-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathtest_1d_3o.py
More file actions
133 lines (116 loc) · 3.16 KB
/
test_1d_3o.py
File metadata and controls
133 lines (116 loc) · 3.16 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
import matplotlib.pyplot as plt
import numpy as np
import cupy as cp
from numba import cuda, jit, njit, vectorize
# from numba import njit
from scipy.signal import convolve as spc
from scipy.signal import fftconvolve as spfc
import time
results = {}
def t(f):
def wrapper(*args):
start = time.time()
for i in range(10):
a = f(*args)
print(f.__name__, time.time()-start)
data = [[args[0].size, args[1].size, time.time()-start]]
if f.__name__ in results:
results[f.__name__] += data
else:
results[f.__name__] = data
return a
return wrapper
@t
@njit
def test_3_nbconv(x,k): #numba_conv
l = x.size - k.shape[0] + 1
y = np.zeros(l)
for n in range(l):
for i in range(k.shape[0]):
for j in range(k.shape[1]):
for l in range(k.shape[2]):
y[n] += x[i+n] * x[j+n] * x[l+n] * k[i,j,l]
return y
@cuda.jit
def test_3_nbcj(x,k,y): #numba_conv
for n in range(y.size):
for i in range(k.shape[0]):
for j in range(k.shape[1]):
for l in range(k.shape[2]):
y[n] += x[i+n] * x[j+n] * x[l+n] * k[i,j,l]
@cuda.jit
def test_3_nbcj_grid(x,k,y): #numba_conv
n = cuda.grid(1)
if (0 <= n) and (n < y.size):
for i in range(k.shape[0]):
for j in range(k.shape[1]):
for l in range(k.shape[2]):
y[n] += x[i+n] * x[j+n] * x[l+n] * k[i,j,l]
@t
def test_3_nbc(x, k):
l = x.size - k.shape[0] + 1
y = cp.zeros(l)
test_3_nbcj[1,32](x, k, y)
return y
@t
def test_3_nbcg(x, k):
l = x.size - k.shape[0] + 1
y = cp.zeros(l)
th = 128
b = y.size//th+1
# print(b,th)
test_3_nbcj_grid[b,th](x, k, y)
return y
@t
def test_3_nbcg1024(x, k):
l = x.size - k.shape[0] + 1
y = cp.zeros(l)
th = 1024
b = y.size//th+1
# print(b,th)
test_3_nbcj_grid[b,th](x, k, y)
return y
def test_3_valid(n,m):
np.random.seed(0)
x = np.random.uniform(-1,1,n)
k = np.random.uniform(-1,1,(m,m,m))
xc = cuda.to_device(x)
kc = cuda.to_device(k)
if True:
nbconv = test_3_nbconv(x,k)
nbc = test_3_nbc(xc,kc)
nbcg = test_3_nbcg(xc,kc)
nbcg1024 = test_3_nbcg1024(xc,kc)
# print(nbconv)
# print(nbc)
# print(nbcg)
print(np.all([
np.isclose(nbc,nbconv)
,np.isclose(nbcg,nbconv)
,np.isclose(nbcg1024,nbconv)
])
, n, m
)
def test_3_plot():
for i in range(1,6):
test_3_valid(4**i, 2**i)
print(results)
for k in results:
a = np.asarray(results[k])
x = [4**(i+1) * 2**(3*(i+1)) for i in range(a.shape[0])]
y = a[:,2]
plt.plot(x[1:], y[1:], label=k[7:])
plt.text(x[-1], y[-1], str(k[7:]))
plt.xscale('log')
plt.yscale('log')
plt.legend()
plt.show()
if __name__ == '__main__':
# test_3_valid(7, 2)
# test_3_valid(7, 3)
# test_3_valid(7, 4)
# test_3_valid(17, 12)
# test_3_valid(17, 13)
# test_3_valid(17, 14)
# test_3_valid(64, 8)
test_3_plot()