Révision 271 ETSN/MyDFT_3.py
MyDFT_3.py (revision 271) | ||
---|---|---|
4 | 4 |
import pyopencl as cl |
5 | 5 |
from numpy import pi,cos,sin |
6 | 6 |
|
7 |
# piling 16 arithmetical functions |
|
8 |
def MySillyFunction(x): |
|
9 |
return(np.power(np.sqrt(np.log(np.exp(np.arctanh(np.tanh(np.arcsinh(np.sinh(np.arccosh(np.cosh(np.arctan(np.tan(np.arcsin(np.sin(np.arccos(np.cos(x))))))))))))))),2)) |
|
10 |
|
|
11 |
# Native Operation under Numpy (for prototyping & tests |
|
12 |
def NativeAddition(a_np,b_np): |
|
13 |
return(a_np+b_np) |
|
14 |
|
|
15 |
# Native Operation with MySillyFunction under Numpy (for prototyping & tests |
|
16 |
def NativeSillyAddition(a_np,b_np): |
|
17 |
return(MySillyFunction(a_np)+MySillyFunction(b_np)) |
|
18 |
|
|
19 | 7 |
# Naive Discrete Fourier Transform |
20 | 8 |
def MyDFT(x,y): |
21 |
from numpy import pi,cos,sin |
|
22 | 9 |
size=x.shape[0] |
23 | 10 |
X=np.zeros(size).astype(np.float32) |
24 | 11 |
Y=np.zeros(size).astype(np.float32) |
... | ... | |
44 | 31 |
@numba.njit(parallel=True) |
45 | 32 |
def NumbaDFT(x,y): |
46 | 33 |
size=x.shape[0] |
47 |
X=np.zeros(size) |
|
48 |
Y=np.zeros(size) |
|
34 |
X=np.zeros(size).astype(np.float32)
|
|
35 |
Y=np.zeros(size).astype(np.float32)
|
|
49 | 36 |
nj=np.multiply(2.0*np.pi/size,np.arange(size)).astype(np.float32) |
50 | 37 |
for i in numba.prange(size): |
51 | 38 |
X[i]=np.sum(np.subtract(np.multiply(np.cos(i*nj),x),np.multiply(np.sin(i*nj),y))) |
52 | 39 |
Y[i]=np.sum(np.add(np.multiply(np.sin(i*nj),x),np.multiply(np.cos(i*nj),y))) |
53 | 40 |
return(X,Y) |
54 | 41 |
|
55 |
# CUDA complete operation |
|
56 |
def CUDAAddition(a_np,b_np): |
|
57 |
import pycuda.autoinit |
|
58 |
import pycuda.driver as drv |
|
59 |
import numpy |
|
60 |
|
|
61 |
from pycuda.compiler import SourceModule |
|
62 |
mod = SourceModule(""" |
|
63 |
__global__ void sum(float *dest, float *a, float *b) |
|
64 |
{ |
|
65 |
// const int i = threadIdx.x; |
|
66 |
const int i = blockIdx.x; |
|
67 |
dest[i] = a[i] + b[i]; |
|
68 |
} |
|
69 |
""") |
|
70 |
|
|
71 |
# sum = mod.get_function("sum") |
|
72 |
sum = mod.get_function("sum") |
|
73 |
|
|
74 |
res_np = numpy.zeros_like(a_np) |
|
75 |
sum(drv.Out(res_np), drv.In(a_np), drv.In(b_np), |
|
76 |
block=(1,1,1), grid=(a_np.size,1)) |
|
77 |
return(res_np) |
|
78 |
|
|
79 |
# CUDA Silly complete operation |
|
80 |
def CUDASillyAddition(a_np,b_np): |
|
81 |
import pycuda.autoinit |
|
82 |
import pycuda.driver as drv |
|
83 |
import numpy |
|
84 |
|
|
85 |
from pycuda.compiler import SourceModule |
|
86 |
TimeIn=time.time() |
|
87 |
mod = SourceModule(""" |
|
88 |
__device__ float MySillyFunction(float x) |
|
89 |
{ |
|
90 |
return(pow(sqrt(log(exp(atanh(tanh(asinh(sinh(acosh(cosh(atan(tan(asin(sin(acos(cos(x))))))))))))))),2)); |
|
91 |
} |
|
92 |
|
|
93 |
__global__ void sillysum(float *dest, float *a, float *b) |
|
94 |
{ |
|
95 |
const int i = blockIdx.x; |
|
96 |
dest[i] = MySillyFunction(a[i]) + MySillyFunction(b[i]); |
|
97 |
} |
|
98 |
""") |
|
99 |
Elapsed=time.time()-TimeIn |
|
100 |
print("Definition of kernel : %.3f" % Elapsed) |
|
101 |
|
|
102 |
TimeIn=time.time() |
|
103 |
# sum = mod.get_function("sum") |
|
104 |
sillysum = mod.get_function("sillysum") |
|
105 |
Elapsed=time.time()-TimeIn |
|
106 |
print("Synthesis of kernel : %.3f" % Elapsed) |
|
107 |
|
|
108 |
TimeIn=time.time() |
|
109 |
res_np = numpy.zeros_like(a_np) |
|
110 |
Elapsed=time.time()-TimeIn |
|
111 |
print("Allocation on Host for results : %.3f" % Elapsed) |
|
112 |
|
|
113 |
TimeIn=time.time() |
|
114 |
sillysum(drv.Out(res_np), drv.In(a_np), drv.In(b_np), |
|
115 |
block=(1,1,1), grid=(a_np.size,1)) |
|
116 |
Elapsed=time.time()-TimeIn |
|
117 |
print("Execution of kernel : %.3f" % Elapsed) |
|
118 |
return(res_np) |
|
119 |
|
|
120 |
# OpenCL complete operation |
|
121 |
def OpenCLAddition(a_np,b_np): |
|
122 |
|
|
123 |
# Context creation |
|
124 |
ctx = cl.create_some_context() |
|
125 |
# Every process is stored in a queue |
|
126 |
queue = cl.CommandQueue(ctx) |
|
127 |
|
|
128 |
TimeIn=time.time() |
|
129 |
# Copy from Host to Device using pointers |
|
130 |
mf = cl.mem_flags |
|
131 |
a_g = cl.Buffer(ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=a_np) |
|
132 |
b_g = cl.Buffer(ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=b_np) |
|
133 |
Elapsed=time.time()-TimeIn |
|
134 |
print("Copy from Host 2 Device : %.3f" % Elapsed) |
|
135 |
|
|
136 |
TimeIn=time.time() |
|
137 |
# Definition of kernel under OpenCL |
|
138 |
prg = cl.Program(ctx, """ |
|
139 |
__kernel void sum( |
|
140 |
__global const float *a_g, __global const float *b_g, __global float *res_g) |
|
141 |
{ |
|
142 |
int gid = get_global_id(0); |
|
143 |
res_g[gid] = a_g[gid] + b_g[gid]; |
|
144 |
} |
|
145 |
""").build() |
|
146 |
Elapsed=time.time()-TimeIn |
|
147 |
print("Building kernels : %.3f" % Elapsed) |
|
148 |
|
|
149 |
TimeIn=time.time() |
|
150 |
# Memory allocation on Device for result |
|
151 |
res_g = cl.Buffer(ctx, mf.WRITE_ONLY, a_np.nbytes) |
|
152 |
Elapsed=time.time()-TimeIn |
|
153 |
print("Allocation on Device for results : %.3f" % Elapsed) |
|
154 |
|
|
155 |
TimeIn=time.time() |
|
156 |
# Synthesis of function "sum" inside Kernel Sources |
|
157 |
knl = prg.sum # Use this Kernel object for repeated calls |
|
158 |
Elapsed=time.time()-TimeIn |
|
159 |
print("Synthesis of kernel : %.3f" % Elapsed) |
|
160 |
|
|
161 |
TimeIn=time.time() |
|
162 |
# Call of kernel previously defined |
|
163 |
knl(queue, a_np.shape, None, a_g, b_g, res_g) |
|
164 |
Elapsed=time.time()-TimeIn |
|
165 |
print("Execution of kernel : %.3f" % Elapsed) |
|
166 |
|
|
167 |
TimeIn=time.time() |
|
168 |
# Creation of vector for result with same size as input vectors |
|
169 |
res_np = np.empty_like(a_np) |
|
170 |
Elapsed=time.time()-TimeIn |
|
171 |
print("Allocation on Host for results: %.3f" % Elapsed) |
|
172 |
|
|
173 |
TimeIn=time.time() |
|
174 |
# Copy from Device to Host |
|
175 |
cl.enqueue_copy(queue, res_np, res_g) |
|
176 |
Elapsed=time.time()-TimeIn |
|
177 |
print("Copy from Device 2 Host : %.3f" % Elapsed) |
|
178 |
|
|
179 |
return(res_np) |
|
180 |
|
|
181 |
# OpenCL complete operation |
|
182 |
def OpenCLSillyAddition(a_np,b_np): |
|
183 |
|
|
184 |
# Context creation |
|
185 |
ctx = cl.create_some_context() |
|
186 |
# Every process is stored in a queue |
|
187 |
queue = cl.CommandQueue(ctx) |
|
188 |
|
|
189 |
TimeIn=time.time() |
|
190 |
# Copy from Host to Device using pointers |
|
191 |
mf = cl.mem_flags |
|
192 |
a_g = cl.Buffer(ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=a_np) |
|
193 |
b_g = cl.Buffer(ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=b_np) |
|
194 |
Elapsed=time.time()-TimeIn |
|
195 |
print("Copy from Host 2 Device : %.3f" % Elapsed) |
|
196 |
|
|
197 |
TimeIn=time.time() |
|
198 |
# Definition of kernel under OpenCL |
|
199 |
prg = cl.Program(ctx, """ |
|
200 |
|
|
201 |
float MySillyFunction(float x) |
|
202 |
{ |
|
203 |
return(pow(sqrt(log(exp(atanh(tanh(asinh(sinh(acosh(cosh(atan(tan(asin(sin(acos(cos(x))))))))))))))),2)); |
|
204 |
} |
|
205 |
|
|
206 |
__kernel void sillysum( |
|
207 |
__global const float *a_g, __global const float *b_g, __global float *res_g) |
|
208 |
{ |
|
209 |
int gid = get_global_id(0); |
|
210 |
res_g[gid] = MySillyFunction(a_g[gid]) + MySillyFunction(b_g[gid]); |
|
211 |
} |
|
212 |
|
|
213 |
__kernel void sum( |
|
214 |
__global const float *a_g, __global const float *b_g, __global float *res_g) |
|
215 |
{ |
|
216 |
int gid = get_global_id(0); |
|
217 |
res_g[gid] = a_g[gid] + b_g[gid]; |
|
218 |
} |
|
219 |
""").build() |
|
220 |
Elapsed=time.time()-TimeIn |
|
221 |
print("Building kernels : %.3f" % Elapsed) |
|
222 |
|
|
223 |
TimeIn=time.time() |
|
224 |
# Memory allocation on Device for result |
|
225 |
res_g = cl.Buffer(ctx, mf.WRITE_ONLY, a_np.nbytes) |
|
226 |
Elapsed=time.time()-TimeIn |
|
227 |
print("Allocation on Device for results : %.3f" % Elapsed) |
|
228 |
|
|
229 |
TimeIn=time.time() |
|
230 |
# Synthesis of function "sillysum" inside Kernel Sources |
|
231 |
knl = prg.sillysum # Use this Kernel object for repeated calls |
|
232 |
Elapsed=time.time()-TimeIn |
|
233 |
print("Synthesis of kernel : %.3f" % Elapsed) |
|
234 |
|
|
235 |
TimeIn=time.time() |
|
236 |
# Call of kernel previously defined |
|
237 |
CallCL=knl(queue, a_np.shape, None, a_g, b_g, res_g) |
|
238 |
# |
|
239 |
CallCL.wait() |
|
240 |
Elapsed=time.time()-TimeIn |
|
241 |
print("Execution of kernel : %.3f" % Elapsed) |
|
242 |
|
|
243 |
TimeIn=time.time() |
|
244 |
# Creation of vector for result with same size as input vectors |
|
245 |
res_np = np.empty_like(a_np) |
|
246 |
Elapsed=time.time()-TimeIn |
|
247 |
print("Allocation on Host for results: %.3f" % Elapsed) |
|
248 |
|
|
249 |
TimeIn=time.time() |
|
250 |
# Copy from Device to Host |
|
251 |
cl.enqueue_copy(queue, res_np, res_g) |
|
252 |
Elapsed=time.time()-TimeIn |
|
253 |
print("Copy from Device 2 Host : %.3f" % Elapsed) |
|
254 |
|
|
255 |
return(res_np) |
|
256 |
|
|
257 | 42 |
import sys |
258 | 43 |
import time |
259 | 44 |
|
... | ... | |
265 | 50 |
SIZE=int(sys.argv[1]) |
266 | 51 |
print("Size of vectors set to %i" % SIZE) |
267 | 52 |
except: |
268 |
SIZE=50000
|
|
53 |
SIZE=256
|
|
269 | 54 |
print("Size of vectors set to default size %i" % SIZE) |
270 | 55 |
|
271 |
# a_np = np.random.rand(SIZE).astype(np.float32) |
|
272 |
# b_np = np.random.rand(SIZE).astype(np.float32) |
|
273 |
|
|
274 | 56 |
a_np = np.ones(SIZE).astype(np.float32) |
275 | 57 |
b_np = np.ones(SIZE).astype(np.float32) |
276 | 58 |
|
59 |
C_np = np.zeros(SIZE).astype(np.float32) |
|
60 |
D_np = np.zeros(SIZE).astype(np.float32) |
|
61 |
C_np[0] = np.float32(SIZE) |
|
62 |
D_np[0] = np.float32(SIZE) |
|
63 |
|
|
277 | 64 |
# Native & Naive Implementation |
278 | 65 |
print("Performing naive implementation") |
279 | 66 |
TimeIn=time.time() |
... | ... | |
281 | 68 |
NativeElapsed=time.time()-TimeIn |
282 | 69 |
NativeRate=int(SIZE/NativeElapsed) |
283 | 70 |
print("NativeRate: %i" % NativeRate) |
284 |
|
|
71 |
print("Precision: ",np.linalg.norm(c_np-C_np),np.linalg.norm(d_np-D_np)) |
|
72 |
|
|
285 | 73 |
# Native & Numpy Implementation |
286 | 74 |
print("Performing Numpy implementation") |
287 | 75 |
TimeIn=time.time() |
... | ... | |
289 | 77 |
NumpyElapsed=time.time()-TimeIn |
290 | 78 |
NumpyRate=int(SIZE/NumpyElapsed) |
291 | 79 |
print("NumpyRate: %i" % NumpyRate) |
292 |
|
|
293 |
print(np.linalg.norm(c_np-e_np)) |
|
294 |
print(np.linalg.norm(d_np-f_np)) |
|
80 |
print("Precision: ",np.linalg.norm(e_np-C_np),np.linalg.norm(f_np-D_np)) |
|
295 | 81 |
|
296 |
# Native & Numpy Implementation
|
|
82 |
# Native & Numba Implementation
|
|
297 | 83 |
print("Performing Numba implementation") |
298 | 84 |
TimeIn=time.time() |
299 | 85 |
g_np,h_np=NumbaDFT(a_np,b_np) |
300 |
NumpyElapsed=time.time()-TimeIn |
|
301 |
NumpyRate=int(SIZE/NumpyElapsed) |
|
302 |
print("NumpyRate: %i" % NumpyRate) |
|
303 |
|
|
304 |
print(np.linalg.norm(c_np-g_np)) |
|
305 |
print(np.linalg.norm(d_np-h_np)) |
|
306 |
|
|
307 |
# # OpenCL Implementation |
|
308 |
# TimeIn=time.time() |
|
309 |
# # res_cl=OpenCLAddition(a_np,b_np) |
|
310 |
# res_cl=OpenCLSillyAddition(a_np,b_np) |
|
311 |
# OpenCLElapsed=time.time()-TimeIn |
|
312 |
# OpenCLRate=int(SIZE/OpenCLElapsed) |
|
313 |
# print("OpenCLRate: %i" % OpenCLRate) |
|
314 |
|
|
315 |
# # CUDA Implementation |
|
316 |
# TimeIn=time.time() |
|
317 |
# # res_cuda=CUDAAddition(a_np,b_np) |
|
318 |
# res_cuda=CUDASillyAddition(a_np,b_np) |
|
319 |
# CUDAElapsed=time.time()-TimeIn |
|
320 |
# CUDARate=int(SIZE/CUDAElapsed) |
|
321 |
# print("CUDARate: %i" % CUDARate) |
|
322 |
|
|
323 |
# print("OpenCLvsNative ratio: %f" % (OpenCLRate/NativeRate)) |
|
324 |
# print("CUDAvsNative ratio: %f" % (CUDARate/NativeRate)) |
|
325 |
|
|
326 |
# # Check on OpenCL with Numpy: |
|
327 |
# print(res_cl - res_np) |
|
328 |
# print(np.linalg.norm(res_cl - res_np)) |
|
329 |
# try: |
|
330 |
# assert np.allclose(res_np, res_cl) |
|
331 |
# except: |
|
332 |
# print("Results between Native & OpenCL seem to be too different!") |
|
333 |
|
|
334 |
# # Check on CUDA with Numpy: |
|
335 |
# print(res_cuda - res_np) |
|
336 |
# print(np.linalg.norm(res_cuda - res_np)) |
|
337 |
# try: |
|
338 |
# assert np.allclose(res_np, res_cuda) |
|
339 |
# except: |
|
340 |
# print("Results between Native & CUDA seem to be too different!") |
|
341 |
|
|
342 |
|
|
86 |
NumbaElapsed=time.time()-TimeIn |
|
87 |
NumbaRate=int(SIZE/NumbaElapsed) |
|
88 |
print("NumbaRate: %i" % NumbaRate) |
|
89 |
print("Precision: ",np.linalg.norm(g_np-C_np),np.linalg.norm(h_np-D_np)) |
Formats disponibles : Unified diff