-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrpi0_gpu_fft.py
239 lines (190 loc) · 7.24 KB
/
rpi0_gpu_fft.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
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
# http://www.aholme.co.uk/GPU_FFT/Main.htm
# http://www.peteronion.org.uk/FFT/FastFourier.html
# You need to use sudo because of the GPU
# sudo -E python rpi0_gpu_fft.py
import ctypes
from math import log2
import numpy as np
import pathlib
gpu_fft = ctypes.CDLL(pathlib.Path.cwd().joinpath('rpi0_gpu_fft.so'))
fft1d = gpu_fft.fft1d
fft1d.restype = ctypes.c_int32
fft1d.argtypes = [
ctypes.c_uint32,
ctypes.c_uint32,
np.ctypeslib.ndpointer(dtype=np.float32, flags='ALIGNED,C_CONTIGUOUS'),
np.ctypeslib.ndpointer(dtype=np.float32, flags='ALIGNED,C_CONTIGUOUS')]
ifft1d = gpu_fft.ifft1d
ifft1d.restype = ctypes.c_int32
ifft1d.argtypes = [
ctypes.c_uint32,
ctypes.c_uint32,
np.ctypeslib.ndpointer(dtype=np.float32, flags='ALIGNED,C_CONTIGUOUS'),
np.ctypeslib.ndpointer(dtype=np.float32, flags='ALIGNED,C_CONTIGUOUS')]
fft2d = gpu_fft.fft2d
fft2d.restype = ctypes.c_int32
fft2d.argtypes = [
ctypes.c_uint32,
ctypes.c_uint32,
np.ctypeslib.ndpointer(dtype=np.float32, flags='ALIGNED,C_CONTIGUOUS'),
np.ctypeslib.ndpointer(dtype=np.float32, flags='ALIGNED,C_CONTIGUOUS')]
ifft2d = gpu_fft.ifft2d
ifft2d.restype = ctypes.c_int32
ifft2d.argtypes = [
ctypes.c_uint32,
ctypes.c_uint32,
np.ctypeslib.ndpointer(dtype=np.float32, flags='ALIGNED,C_CONTIGUOUS'),
np.ctypeslib.ndpointer(dtype=np.float32, flags='ALIGNED,C_CONTIGUOUS')]
def check_error(res):
assert res != -1, "Unable to enable V3D. Please check your firmware is up to date.\n"
assert res != -2, f"Shape {N}x{M} not supported. Try between {2**8} and {2**22}.\n"
assert res != -3, "Out of memory. Try a smaller batch or increase GPU memory.\n"
assert res != -4, "Unable to map Videocore peripherals into ARM memory space.\n"
assert res != -5, "Can't open libbcm_host.\n"
def gpu_fft1d(input_array):
assert len(input_array.shape) == 2
N = input_array.shape[0]
M = input_array.shape[1]
assert (M & (M-1) == 0) and M != 0, "Power of 2, please!"
output_array = np.empty((N,2*M),dtype=np.float32)
# input_complex = input_array.astype(np.complex64)
# res = fft1d(N, M, input_complex.view(dtype=np.float32).reshape((N,2*M)), output_array)
res = fft1d(N, M, input_array.astype(dtype=np.float32), output_array)
if res != 0:
check_error(res)
return output_array.view(dtype=np.complex64)
def gpu_ifft1d(input_array):
assert len(input_array.shape) == 2
N = input_array.shape[0]
M = input_array.shape[1]
assert (M & (M-1) == 0) and M != 0, "Power of 2, please!"
input_complex = input_array.astype(np.complex64)
# output_array = np.empty((N,2*M),dtype=np.float32)
# res = ifft1d(N, M, input_complex.view(dtype=np.float32).reshape((N,2*M)), output_array)
output_array = np.empty((N,M),dtype=np.float32)
res = ifft1d(N, M, input_complex.view(dtype=np.float32).reshape((N,2*M)), output_array)
if res != 0:
check_error(res)
# return output_array.view(dtype=np.complex64)
return output_array
def gpu_fft2d(input_array):
assert len(input_array.shape) == 2
N = input_array.shape[0]
assert (N & (N-1) == 0) and N != 0, "Power of 2, please!"
M = input_array.shape[1]
assert (M & (M-1) == 0) and M != 0, "Power of 2, please!"
output_array = np.empty((N,2*M),dtype=np.float32)
res = fft2d(N, M, input_array.astype(np.float32), output_array)
if res != 0:
check_error(res)
return output_array.view(dtype=np.complex64)
def gpu_ifft2d(input_array):
assert len(input_array.shape) == 2
N = input_array.shape[0]
assert (N & (N-1) == 0) and N != 0, "Power of 2, please!"
M = input_array.shape[1]
assert (M & (M-1) == 0) and M != 0, "Power of 2, please!"
input_complex = input_array.astype(np.complex64)
# output_array = np.empty((N,2*M),dtype=np.float32)
# res = ifft2d(N, M, input_complex.view(dtype=np.float32).reshape((N,2*M)), output_array)
output_array = np.empty((N,M),dtype=np.float32)
res = ifft2d(N, M, input_complex.view(dtype=np.float32).reshape((N,2*M)), output_array)
if res != 0:
check_error(res)
# return output_array.view(dtype=np.complex64)
return output_array
if __name__ == "__main__":
"""Testing...
"""
import time
# import matplotlib.pyplot as plt
N = 1
M = 2**16
print(f"Testing the FFT/IFFT 1D... Length {M} for {N} times")
# i = np.ones((N,M), dtype=np.float32).astype(np.complex64)*3.1415
i = np.ones((N,M), dtype=np.float32)*3.1415
i[:,2:int(M/2)] = 0.0
trials = 10
time_v = []
for j in range(trials):
time_init = time.monotonic()
res = np.fft.fft(i)
i2 = np.fft.ifft(res)
time_end = time.monotonic()-time_init
print(f"CPU FFT/IFFT 1D time {N}x{M}: {time_end}")
print("FFT1D")
print(res[0,:4])
print(res.shape,res.dtype)
print("IFFT1D")
print(i2[0,:4])
print(i2.shape,i2.dtype)
time_v.append(time_end)
del res, i2
cpu1_avg = sum(time_v)/trials
print(f"CPU FFT/IFFT 1D time {N}x{M}: avg time {cpu1_avg}")
time_v = []
for j in range(trials):
time_init = time.monotonic()
res = gpu_fft1d(i)
i2 = gpu_ifft1d(res)
time_end = time.monotonic()-time_init
print(f"GPU FFT/IFFT 1D time {N}x{M}: {time_end}")
print("FFT1D")
print(res[0,:4])
print(res.shape,res.dtype)
print("IFFT1D")
print(i2[0,:4])
print(i2.shape,i2.dtype)
time_v.append(time_end)
del res, i2
gpu1_avg = sum(time_v)/trials
print(f"GPU FFT/IFFT 1D time {N}x{M}: avg time {gpu1_avg}")
del i
print(f"GPU/CPU FFT/IFFT 1D time {N}x{M}: avg time ({trials} trials): {cpu1_avg/gpu1_avg:0.4f}")
N = 1024
M = 1024
print(f"Testing the FFT/IFFT 2D... {M}x{N}")
i = np.ones((N,M), dtype=np.float32)*3.1415
i[:,2:int(M/2)] = 0.0
time_v = []
for j in range(trials):
time_init = time.monotonic()
res = np.fft.fft2(i)
i2 = np.fft.ifft2(res)
time_end = time.monotonic()-time_init
print(f"CPU FFT/IFFT 2D time {N}x{M}: {time_end}")
print("FFT2D")
print(res[0,:4])
print(res[-1,:4])
print(res.shape,res.dtype)
print("IFFT2D")
print(i2[0,:4])
print(i2[-1,:4])
print(i2.shape,i2.dtype)
time_v.append(time_end)
del res, i2
cpu1_avg = sum(time_v)/trials
print(f"CPU FFT/IFFT 2D time {N}x{M}: avg {cpu1_avg}")
time_v = []
for j in range(trials):
time_init = time.monotonic()
res = gpu_fft2d(i)
i2 = gpu_ifft2d(res)
time_end = time.monotonic()-time_init
print(f"GPU FFT/IFFT 2D time {N}x{M}: {time_end}")
print("FFT2D")
print(res[0,:4])
print(res[-1,:4])
print(res.shape,res.dtype)
print("IFFT2D")
print(i2[0,:4])
print(i2[-1,:4])
print(i2.shape,i2.dtype)
time_v.append(time_end)
del res, i2
gpu1_avg = sum(time_v)/trials
print(f"GPU FFT/IFFT 2D time {N}x{M}: avg {gpu1_avg}")
print(f"GPU/CPU FFT/IFFT 2D time {N}x{M}: avg time ({trials} trials): {cpu1_avg/gpu1_avg:0.4f}")
# # plt.figure(figsize=(10,10))
# # plt.imshow(re, cmap='gray')
# # plt.show()