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