root / ETSN / MySteps_4.py @ 274
Historique | Voir | Annoter | Télécharger (6,96 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 |
# CUDA complete operation
|
19 |
def CUDAAddition(a_np,b_np): |
20 |
import pycuda.autoinit |
21 |
import pycuda.driver as drv |
22 |
import numpy |
23 |
|
24 |
from pycuda.compiler import SourceModule |
25 |
mod = SourceModule("""
|
26 |
__global__ void sum(float *dest, float *a, float *b)
|
27 |
{
|
28 |
// const int i = threadIdx.x;
|
29 |
const int i = blockIdx.x;
|
30 |
dest[i] = a[i] + b[i];
|
31 |
}
|
32 |
""")
|
33 |
|
34 |
sum = mod.get_function("sum")
|
35 |
|
36 |
res_np = numpy.zeros_like(a_np) |
37 |
sum(drv.Out(res_np), drv.In(a_np), drv.In(b_np),
|
38 |
block=(1,1,1), grid=(a_np.size,1)) |
39 |
return(res_np)
|
40 |
|
41 |
# OpenCL complete operation
|
42 |
def OpenCLAddition(a_np,b_np): |
43 |
|
44 |
# Context creation
|
45 |
ctx = cl.create_some_context() |
46 |
# Every process is stored in a queue
|
47 |
queue = cl.CommandQueue(ctx) |
48 |
|
49 |
TimeIn=time.time() |
50 |
# Copy from Host to Device using pointers
|
51 |
mf = cl.mem_flags |
52 |
a_g = cl.Buffer(ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=a_np) |
53 |
b_g = cl.Buffer(ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=b_np) |
54 |
Elapsed=time.time()-TimeIn |
55 |
print("Copy from Host 2 Device : %.3f" % Elapsed)
|
56 |
|
57 |
TimeIn=time.time() |
58 |
# Definition of kernel under OpenCL
|
59 |
prg = cl.Program(ctx, """
|
60 |
__kernel void sum(
|
61 |
__global const float *a_g, __global const float *b_g, __global float *res_g)
|
62 |
{
|
63 |
int gid = get_global_id(0);
|
64 |
res_g[gid] = a_g[gid] + b_g[gid];
|
65 |
}
|
66 |
""").build()
|
67 |
Elapsed=time.time()-TimeIn |
68 |
print("Building kernels : %.3f" % Elapsed)
|
69 |
|
70 |
TimeIn=time.time() |
71 |
# Memory allocation on Device for result
|
72 |
res_g = cl.Buffer(ctx, mf.WRITE_ONLY, a_np.nbytes) |
73 |
Elapsed=time.time()-TimeIn |
74 |
print("Allocation on Device for results : %.3f" % Elapsed)
|
75 |
|
76 |
TimeIn=time.time() |
77 |
# Synthesis of function "sum" inside Kernel Sources
|
78 |
knl = prg.sum # Use this Kernel object for repeated calls
|
79 |
Elapsed=time.time()-TimeIn |
80 |
print("Synthesis of kernel : %.3f" % Elapsed)
|
81 |
|
82 |
TimeIn=time.time() |
83 |
# Call of kernel previously defined
|
84 |
knl(queue, a_np.shape, None, a_g, b_g, res_g)
|
85 |
Elapsed=time.time()-TimeIn |
86 |
print("Execution of kernel : %.3f" % Elapsed)
|
87 |
|
88 |
TimeIn=time.time() |
89 |
# Creation of vector for result with same size as input vectors
|
90 |
res_np = np.empty_like(a_np) |
91 |
Elapsed=time.time()-TimeIn |
92 |
print("Allocation on Host for results: %.3f" % Elapsed)
|
93 |
|
94 |
TimeIn=time.time() |
95 |
# Copy from Device to Host
|
96 |
cl.enqueue_copy(queue, res_np, res_g) |
97 |
Elapsed=time.time()-TimeIn |
98 |
print("Copy from Device 2 Host : %.3f" % Elapsed)
|
99 |
|
100 |
return(res_np)
|
101 |
|
102 |
# OpenCL complete operation
|
103 |
def OpenCLSillyAddition(a_np,b_np): |
104 |
|
105 |
# Context creation
|
106 |
ctx = cl.create_some_context() |
107 |
# Every process is stored in a queue
|
108 |
queue = cl.CommandQueue(ctx) |
109 |
|
110 |
TimeIn=time.time() |
111 |
# Copy from Host to Device using pointers
|
112 |
mf = cl.mem_flags |
113 |
a_g = cl.Buffer(ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=a_np) |
114 |
b_g = cl.Buffer(ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=b_np) |
115 |
Elapsed=time.time()-TimeIn |
116 |
print("Copy from Host 2 Device : %.3f" % Elapsed)
|
117 |
|
118 |
TimeIn=time.time() |
119 |
# Definition of kernel under OpenCL
|
120 |
prg = cl.Program(ctx, """
|
121 |
|
122 |
float MySillyFunction(float x)
|
123 |
{
|
124 |
return(pow(sqrt(log(exp(atanh(tanh(asinh(sinh(acosh(cosh(atan(tan(asin(sin(acos(cos(x))))))))))))))),2));
|
125 |
}
|
126 |
|
127 |
__kernel void sillysum(
|
128 |
__global const float *a_g, __global const float *b_g, __global float *res_g)
|
129 |
{
|
130 |
int gid = get_global_id(0);
|
131 |
res_g[gid] = MySillyFunction(a_g[gid]) + MySillyFunction(b_g[gid]);
|
132 |
}
|
133 |
|
134 |
__kernel void sum(
|
135 |
__global const float *a_g, __global const float *b_g, __global float *res_g)
|
136 |
{
|
137 |
int gid = get_global_id(0);
|
138 |
res_g[gid] = a_g[gid] + b_g[gid];
|
139 |
}
|
140 |
""").build()
|
141 |
Elapsed=time.time()-TimeIn |
142 |
print("Building kernels : %.3f" % Elapsed)
|
143 |
|
144 |
TimeIn=time.time() |
145 |
# Memory allocation on Device for result
|
146 |
res_g = cl.Buffer(ctx, mf.WRITE_ONLY, a_np.nbytes) |
147 |
Elapsed=time.time()-TimeIn |
148 |
print("Allocation on Device for results : %.3f" % Elapsed)
|
149 |
|
150 |
TimeIn=time.time() |
151 |
# Synthesis of function "sillysum" inside Kernel Sources
|
152 |
knl = prg.sillysum # Use this Kernel object for repeated calls
|
153 |
Elapsed=time.time()-TimeIn |
154 |
print("Synthesis of kernel : %.3f" % Elapsed)
|
155 |
|
156 |
TimeIn=time.time() |
157 |
# Call of kernel previously defined
|
158 |
CallCL=knl(queue, a_np.shape, None, a_g, b_g, res_g)
|
159 |
#
|
160 |
CallCL.wait() |
161 |
Elapsed=time.time()-TimeIn |
162 |
print("Execution of kernel : %.3f" % Elapsed)
|
163 |
|
164 |
TimeIn=time.time() |
165 |
# Creation of vector for result with same size as input vectors
|
166 |
res_np = np.empty_like(a_np) |
167 |
Elapsed=time.time()-TimeIn |
168 |
print("Allocation on Host for results: %.3f" % Elapsed)
|
169 |
|
170 |
TimeIn=time.time() |
171 |
# Copy from Device to Host
|
172 |
cl.enqueue_copy(queue, res_np, res_g) |
173 |
Elapsed=time.time()-TimeIn |
174 |
print("Copy from Device 2 Host : %.3f" % Elapsed)
|
175 |
|
176 |
return(res_np)
|
177 |
|
178 |
import sys |
179 |
import time |
180 |
|
181 |
if __name__=='__main__': |
182 |
|
183 |
# Size of input vectors definition based on stdin
|
184 |
import sys |
185 |
try:
|
186 |
SIZE=int(sys.argv[1]) |
187 |
print("Size of vectors set to %i" % SIZE)
|
188 |
except:
|
189 |
SIZE=50000
|
190 |
print("Size of vectors set to default size %i" % SIZE)
|
191 |
|
192 |
a_np = np.random.rand(SIZE).astype(np.float32) |
193 |
b_np = np.random.rand(SIZE).astype(np.float32) |
194 |
|
195 |
# Native Implementation
|
196 |
TimeIn=time.time() |
197 |
# res_np=NativeSillyAddition(a_np,b_np)
|
198 |
res_np=NativeAddition(a_np,b_np) |
199 |
NativeElapsed=time.time()-TimeIn |
200 |
NativeRate=int(SIZE/NativeElapsed)
|
201 |
print("NativeRate: %i" % NativeRate)
|
202 |
|
203 |
# OpenCL Implementation
|
204 |
TimeIn=time.time() |
205 |
# res_cl=OpenCLSillyAddition(a_np,b_np)
|
206 |
res_cl=OpenCLAddition(a_np,b_np) |
207 |
OpenCLElapsed=time.time()-TimeIn |
208 |
OpenCLRate=int(SIZE/OpenCLElapsed)
|
209 |
print("OpenCLRate: %i" % OpenCLRate)
|
210 |
|
211 |
# CUDA Implementation
|
212 |
TimeIn=time.time() |
213 |
res_cuda=CUDAAddition(a_np,b_np) |
214 |
CUDAElapsed=time.time()-TimeIn |
215 |
CUDARate=int(SIZE/CUDAElapsed)
|
216 |
print("CUDARate: %i" % CUDARate)
|
217 |
|
218 |
print("OpenCLvsNative ratio: %f" % (OpenCLRate/NativeRate))
|
219 |
print("CUDAvsNative ratio: %f" % (CUDARate/NativeRate))
|
220 |
|
221 |
# Check on OpenCL with Numpy:
|
222 |
print(res_cl - res_np) |
223 |
print(np.linalg.norm(res_cl - res_np)) |
224 |
try:
|
225 |
assert np.allclose(res_np, res_cl)
|
226 |
except:
|
227 |
print("Results between Native & OpenCL seem to be too different!")
|
228 |
|
229 |
# Check on CUDA with Numpy:
|
230 |
print(res_cuda - res_np) |
231 |
print(np.linalg.norm(res_cuda - res_np)) |
232 |
try:
|
233 |
assert np.allclose(res_np, res_cuda)
|
234 |
except:
|
235 |
print("Results between Native & CUDA seem to be too different!")
|
236 |
|
237 |
|