Révision 271 ETSN/MyDFT_2.py
MyDFT_2.py (revision 271) | ||
---|---|---|
3 | 3 |
import numpy as np |
4 | 4 |
import pyopencl as cl |
5 | 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 | 6 |
# Naive Discrete Fourier Transform |
19 | 7 |
def MyDFT(x,y): |
20 | 8 |
from numpy import pi,cos,sin |
... | ... | |
39 | 27 |
Y[i]=np.sum(np.add(np.multiply(np.sin(i*nj),x),np.multiply(np.cos(i*nj),y))) |
40 | 28 |
return(X,Y) |
41 | 29 |
|
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 | 30 |
import sys |
245 | 31 |
import time |
246 | 32 |
|
... | ... | |
252 | 38 |
SIZE=int(sys.argv[1]) |
253 | 39 |
print("Size of vectors set to %i" % SIZE) |
254 | 40 |
except: |
255 |
SIZE=50000
|
|
41 |
SIZE=256
|
|
256 | 42 |
print("Size of vectors set to default size %i" % SIZE) |
257 | 43 |
|
258 |
# a_np = np.random.rand(SIZE).astype(np.float32) |
|
259 |
# b_np = np.random.rand(SIZE).astype(np.float32) |
|
260 |
|
|
261 | 44 |
a_np = np.ones(SIZE).astype(np.float32) |
262 | 45 |
b_np = np.ones(SIZE).astype(np.float32) |
263 | 46 |
|
47 |
C_np = np.zeros(SIZE).astype(np.float32) |
|
48 |
D_np = np.zeros(SIZE).astype(np.float32) |
|
49 |
C_np[0] = np.float32(SIZE) |
|
50 |
D_np[0] = np.float32(SIZE) |
|
51 |
|
|
264 | 52 |
# Native & Naive Implementation |
265 | 53 |
print("Performing naive implementation") |
266 | 54 |
TimeIn=time.time() |
... | ... | |
268 | 56 |
NativeElapsed=time.time()-TimeIn |
269 | 57 |
NativeRate=int(SIZE/NativeElapsed) |
270 | 58 |
print("NativeRate: %i" % NativeRate) |
271 |
print(c_np,d_np)
|
|
59 |
print("Precision: ",np.linalg.norm(c_np-C_np),np.linalg.norm(d_np-D_np))
|
|
272 | 60 |
|
273 | 61 |
# Native & Numpy Implementation |
274 | 62 |
print("Performing Numpy implementation") |
... | ... | |
277 | 65 |
NumpyElapsed=time.time()-TimeIn |
278 | 66 |
NumpyRate=int(SIZE/NumpyElapsed) |
279 | 67 |
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 |
|
|
68 |
print("Precision: ",np.linalg.norm(e_np-C_np),np.linalg.norm(f_np-D_np)) |
Formats disponibles : Unified diff